Skip to content

Commit

Permalink
add soroban-rpc testing for the prng change
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman committed Sep 20, 2023
1 parent 3252e05 commit bd53632
Showing 1 changed file with 111 additions and 0 deletions.
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))
}

0 comments on commit bd53632

Please sign in to comment.