Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set base prng to zero in preflight and invoke #975

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl Contract {
count
}

pub fn prng_u64_in_range(env: Env, low: u64, high: u64) -> u64 {
env.prng().u64_in_range(low..=high)
}

#[allow(unused_variables)]
pub fn multi_word_cmd(env: Env, contract_owner: String) {}
/// Logs a string with `hello ` in front.
Expand Down
26 changes: 26 additions & 0 deletions cmd/crates/soroban-test/tests/it/contract_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,29 @@ fn build() {
.assert()
.success();
}

#[test]
fn invoke_prng_u64_in_range_test() {
let sandbox = TestEnv::default();
let res = sandbox
.new_assert_cmd("contract")
.arg("deploy")
.arg("--wasm")
.arg(HELLO_WORLD.path())
.assert()
.success();
let stdout = String::from_utf8(res.get_output().stdout.clone()).unwrap();
let id = stdout.trim_end();
println!("{id}");
sandbox
.new_assert_cmd("contract")
.arg("invoke")
.arg("--id")
.arg(id)
.arg("--")
.arg("prng_u64_in_range")
.arg("--low=0")
.arg("--high=100")
.assert()
.success();
}
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl Cmd {
let h = Host::with_storage_and_budget(storage, budget);
h.switch_to_recording_auth(true)?;
h.set_source_account(source_account)?;
h.set_base_prng_seed([0; 32])?;

let mut ledger_info = state.ledger_info();
ledger_info.sequence_number += 1;
Expand Down
111 changes: 111 additions & 0 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,114 @@ func waitForLedgerEntryToExpire(t *testing.T, client *jrpc2.Client, expirationKe
}
require.True(t, expired)
}

func TestSimulateInvokePrng_u64_in_range(t *testing.T) {
test := NewTest(t)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
account := txnbuild.NewSimpleAccount(address, 0)

helloWorldContract := getHelloWorldContract(t)

params := preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(account.AccountID, helloWorldContract),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})

tx, err := txnbuild.NewTransaction(params)
require.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)

params = preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createCreateContractOperation(address, helloWorldContract),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})

tx, err = txnbuild.NewTransaction(params)
require.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)

contractID := getContractID(t, address, testSalt, StandaloneNetworkPassphrase)
authAddrArg := "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"
tx, err = txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
&txnbuild.CreateAccount{
Destination: authAddrArg,
Amount: "100000",
SourceAccount: address,
},
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})
require.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)
low := xdr.Uint64(1500)
high := xdr.Uint64(10000)
params = txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: false,
Operations: []txnbuild.Operation{
createInvokeHostOperation(
address,
contractID,
"prng_u64_in_range",
xdr.ScVal{
Type: xdr.ScValTypeScvU64,
U64: &low,
},
xdr.ScVal{
Type: xdr.ScValTypeScvU64,
U64: &high,
},
),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
}
tx, err = txnbuild.NewTransaction(params)

require.NoError(t, err)

txB64, err := tx.Base64()
require.NoError(t, err)

request := methods.SimulateTransactionRequest{Transaction: txB64}
var response methods.SimulateTransactionResponse
err = client.CallResult(context.Background(), "simulateTransaction", request, &response)
require.NoError(t, err)
require.Empty(t, response.Error)

// check the result
require.Len(t, response.Results, 1)
var obtainedResult xdr.ScVal
err = xdr.SafeUnmarshalBase64(response.Results[0].XDR, &obtainedResult)
require.NoError(t, err)
require.Equal(t, xdr.ScValTypeScvU64, obtainedResult.Type)
require.LessOrEqual(t, uint64(*obtainedResult.U64), uint64(high))
require.GreaterOrEqual(t, uint64(*obtainedResult.U64), uint64(low))
}
2 changes: 2 additions & 0 deletions cmd/soroban-rpc/lib/preflight/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub(crate) fn preflight_invoke_hf_op(
.context("cannot set debug diagnostic level")?;
host.set_ledger_info(ledger_info.clone())
.context("cannot set ledger info")?;
host.set_base_prng_seed([0; 32])
.context("cannot set base prng seed")?;
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved

// We make an assumption here:
// - if a transaction doesn't include any soroban authorization entries the client either
Expand Down
Loading