Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Bellamy committed Dec 1, 2022
1 parent 7dc95ce commit 3c30fc9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/get_contract_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestGetContractDataSucceeds(t *testing.T) {
installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: testContract}.MarshalBinary()
assert.NoError(t, err)
contractHash := sha256.Sum256(installContractCodeArgs)
assert.Equal(t, contractHash, scVal.MustObj().MustContractCode().MustWasmId())
assert.Equal(t, xdr.Hash(contractHash), scVal.MustObj().MustContractCode().MustWasmId())
}

func assertSendTransaction(t *testing.T, client *jrpc2.Client, kp *keypair.Full, txnParams txnbuild.TransactionParams) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/get_ledger_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestGetLedgerEntrySucceeds(t *testing.T) {
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInvokeHostOperation(t, account.AccountID, true),
createInstallContractCodeOperation(t, account.AccountID, testContract, true),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
Expand Down
55 changes: 41 additions & 14 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,32 @@ var (
)

// createInvokeHostOperation creates a dummy InvokeHostFunctionOp. In this case by installing a contract code.
func createInvokeHostOperation(t *testing.T, sourceAccount string, includeFootprint bool) *txnbuild.InvokeHostFunction {
return createInstallContractCodeOperation(t, sourceAccount, testContract, includeFootprint)
func createInvokeHostOperation(t *testing.T, sourceAccount string, footprint xdr.LedgerFootprint, contractId xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunction {
var contractIdBytes []byte = contractId[:]
contractIdObj := &xdr.ScObject{
Type: xdr.ScObjectTypeScoBytes,
Bin: &contractIdBytes,
}
methodSymbol := xdr.ScSymbol(method)
parameters := xdr.ScVec{
xdr.ScVal{
Type: xdr.ScValTypeScvObject,
Obj: &contractIdObj,
},
xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &methodSymbol,
},
}
parameters = append(parameters, args...)
return &txnbuild.InvokeHostFunction{
Footprint: footprint,
Function: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeArgs: &parameters,
},
SourceAccount: sourceAccount,
}
}

func createInstallContractCodeOperation(t *testing.T, sourceAccount string, contractCode []byte, includeFootprint bool) *txnbuild.InvokeHostFunction {
Expand Down Expand Up @@ -149,7 +173,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
},
IncrementSequenceNum: false,
Operations: []txnbuild.Operation{
createInvokeHostOperation(t, sourceAccount, false),
createInstallContractCodeOperation(t, sourceAccount, testContract, false),
},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Expand All @@ -171,21 +195,21 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
assert.Equal(
t,
methods.SimulateTransactionResponse{
Footprint: "AAAAAAAAAAEAAAAGkvS4fCJA01o8HRusdDVaD5Z7F2lkyM3UfhQOjETmlDMAAAADAAAAAw==",
Footprint: "AAAAAAAAAAEAAAAH6p/Lga5Uop9rO/KThH0/1+mjaf0cgKyv7Gq9VxMX4MI=",
Cost: methods.SimulateTransactionCost{
CPUInstructions: result.Cost.CPUInstructions,
MemoryBytes: result.Cost.MemoryBytes,
},
Results: []methods.InvokeHostFunctionResult{
{XDR: "AAAABAAAAAEAAAAEAAAAIJL0uHwiQNNaPB0brHQ1Wg+WexdpZMjN1H4UDoxE5pQz"},
{XDR: "AAAABAAAAAEAAAAGAAAAIOqfy4GuVKKfazvyk4R9P9fpo2n9HICsr+xqvVcTF+DC"},
},
LatestLedger: result.LatestLedger,
},
result,
)

// test operation which does not have a source account
withoutSourceAccountOp := createInvokeHostOperation(t, "", false)
withoutSourceAccountOp := createInstallContractCodeOperation(t, "", testContract, false)
tx, err = txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: sourceAccount,
Expand Down Expand Up @@ -216,9 +240,11 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
Sequence: 0,
},
IncrementSequenceNum: false,
Operations: []txnbuild.Operation{createInvokeHostOperation(t, sourceAccount, false)},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(t, sourceAccount, testContract, false),
},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
Expand All @@ -244,8 +270,7 @@ func TestSimulateTransactionError(t *testing.T) {
client := jrpc2.NewClient(ch, nil)

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
invokeHostOp := createInvokeHostOperation(t, sourceAccount, false)
invokeHostOp.Function.InstallContractCodeArgs = nil
invokeHostOp := createInvokeHostOperation(t, sourceAccount, xdr.LedgerFootprint{}, xdr.Hash{}, "noMethod")
tx, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.Root(StandaloneNetworkPassphrase).Address(),
Expand Down Expand Up @@ -383,9 +408,11 @@ func TestSimulateTransactionDeadlineError(t *testing.T) {
Sequence: 0,
},
IncrementSequenceNum: false,
Operations: []txnbuild.Operation{createInvokeHostOperation(t, sourceAccount, false)},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(t, sourceAccount, testContract, false),
},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-rpc/internal/test/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestSendTransactionSucceedsWithResults(t *testing.T) {
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInvokeHostOperation(t, account.AccountID, true),
createInstallContractCodeOperation(t, account.AccountID, testContract, true),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestSendTransactionFailedInLedger(t *testing.T) {
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
// without the footprint the tx will fail
createInvokeHostOperation(t, account.AccountID, false),
createInstallContractCodeOperation(t, account.AccountID, testContract, false),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
Expand Down

0 comments on commit 3c30fc9

Please sign in to comment.