From 47fb0017f144dfe2cc3f488abc13334a1e0c812f Mon Sep 17 00:00:00 2001 From: tamirms Date: Wed, 13 Sep 2023 10:57:57 +0100 Subject: [PATCH] soroban-rpc: Fix integration tests (#955) --- .../internal/test/get_ledger_entries_test.go | 7 ++-- .../internal/test/get_ledger_entry_test.go | 7 ++-- .../test/simulate_transaction_test.go | 35 ++++++++----------- .../internal/test/transaction_test.go | 11 +++--- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/cmd/soroban-rpc/internal/test/get_ledger_entries_test.go b/cmd/soroban-rpc/internal/test/get_ledger_entries_test.go index d8154b3db..74e6dce30 100644 --- a/cmd/soroban-rpc/internal/test/get_ledger_entries_test.go +++ b/cmd/soroban-rpc/internal/test/get_ledger_entries_test.go @@ -83,11 +83,12 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) { kp := keypair.Root(StandaloneNetworkPassphrase) account := txnbuild.NewSimpleAccount(kp.Address(), 0) + contractBinary := getHelloWorldContract(t) params := preflightTransactionParams(t, client, txnbuild.TransactionParams{ SourceAccount: &account, IncrementSequenceNum: true, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(account.AccountID, testContract), + createInstallContractCodeOperation(account.AccountID, contractBinary), }, BaseFee: txnbuild.MinBaseFee, Preconditions: txnbuild.Preconditions{ @@ -110,7 +111,7 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) { txStatusResponse := getTransaction(t, client, sendTxResponse.Hash) require.Equal(t, methods.TransactionStatusSuccess, txStatusResponse.Status) - contractHash := sha256.Sum256(testContract) + contractHash := sha256.Sum256(contractBinary) contractKeyB64, err := xdr.MarshalBase64(xdr.LedgerKey{ Type: xdr.LedgerEntryTypeContractCode, ContractCode: &xdr.LedgerKeyContractCode{ @@ -153,6 +154,6 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) { var firstEntry xdr.LedgerEntryData require.NoError(t, xdr.SafeUnmarshalBase64(result.Entries[0].XDR, &firstEntry)) - require.Equal(t, testContract, firstEntry.MustContractCode().Code) + require.Equal(t, contractBinary, firstEntry.MustContractCode().Code) require.Equal(t, contractKeyB64, result.Entries[0].Key) } diff --git a/cmd/soroban-rpc/internal/test/get_ledger_entry_test.go b/cmd/soroban-rpc/internal/test/get_ledger_entry_test.go index 4b8e17572..f30af115f 100644 --- a/cmd/soroban-rpc/internal/test/get_ledger_entry_test.go +++ b/cmd/soroban-rpc/internal/test/get_ledger_entry_test.go @@ -75,11 +75,12 @@ func TestGetLedgerEntrySucceeds(t *testing.T) { kp := keypair.Root(StandaloneNetworkPassphrase) account := txnbuild.NewSimpleAccount(kp.Address(), 0) + contractBinary := getHelloWorldContract(t) params := preflightTransactionParams(t, client, txnbuild.TransactionParams{ SourceAccount: &account, IncrementSequenceNum: true, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(account.AccountID, testContract), + createInstallContractCodeOperation(account.AccountID, contractBinary), }, BaseFee: txnbuild.MinBaseFee, Preconditions: txnbuild.Preconditions{ @@ -91,7 +92,7 @@ func TestGetLedgerEntrySucceeds(t *testing.T) { sendSuccessfulTransaction(t, client, kp, tx) - contractHash := sha256.Sum256(testContract) + contractHash := sha256.Sum256(contractBinary) keyB64, err := xdr.MarshalBase64(xdr.LedgerKey{ Type: xdr.LedgerEntryTypeContractCode, ContractCode: &xdr.LedgerKeyContractCode{ @@ -110,5 +111,5 @@ func TestGetLedgerEntrySucceeds(t *testing.T) { assert.GreaterOrEqual(t, result.LatestLedger, result.LastModifiedLedger) var entry xdr.LedgerEntryData assert.NoError(t, xdr.SafeUnmarshalBase64(result.XDR, &entry)) - assert.Equal(t, testContract, entry.MustContractCode().Code) + assert.Equal(t, contractBinary, entry.MustContractCode().Code) } diff --git a/cmd/soroban-rpc/internal/test/simulate_transaction_test.go b/cmd/soroban-rpc/internal/test/simulate_transaction_test.go index e2df22533..ecf57fad8 100644 --- a/cmd/soroban-rpc/internal/test/simulate_transaction_test.go +++ b/cmd/soroban-rpc/internal/test/simulate_transaction_test.go @@ -23,14 +23,7 @@ import ( ) var ( - testContract = []byte("a contract") - testSalt = sha256.Sum256([]byte("a1")) - testContractId = []byte{ - 16, 98, 83, 23, 8, 235, 211, 5, - 62, 173, 70, 33, 7, 31, 219, 59, - 180, 75, 106, 249, 139, 196, 156, 192, - 113, 17, 184, 51, 142, 142, 94, 40, - } + testSalt = sha256.Sum256([]byte("a1")) ) func getHelloWorldContract(t *testing.T) []byte { @@ -202,6 +195,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) { client := jrpc2.NewClient(ch, nil) sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address() + contractBinary := getHelloWorldContract(t) params := txnbuild.TransactionParams{ SourceAccount: &txnbuild.SimpleAccount{ AccountID: sourceAccount, @@ -209,7 +203,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) { }, IncrementSequenceNum: false, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(sourceAccount, testContract), + createInstallContractCodeOperation(sourceAccount, contractBinary), }, BaseFee: txnbuild.MinBaseFee, Memo: nil, @@ -219,11 +213,9 @@ func TestSimulateTransactionSucceeds(t *testing.T) { } result := simulateTransactionFromTxParams(t, client, params) - testContractIdBytes := xdr.ScBytes(testContractId) - expectedXdr := xdr.ScVal{ - Type: xdr.ScValTypeScvBytes, - Bytes: &testContractIdBytes, - } + contractHash := sha256.Sum256(contractBinary) + contractHashBytes := xdr.ScBytes(contractHash[:]) + expectedXdr := xdr.ScVal{Type: xdr.ScValTypeScvBytes, Bytes: &contractHashBytes} assert.Greater(t, result.LatestLedger, int64(0)) assert.Greater(t, result.Cost.CPUInstructions, uint64(0)) assert.Greater(t, result.Cost.MemoryBytes, uint64(0)) @@ -235,14 +227,14 @@ func TestSimulateTransactionSucceeds(t *testing.T) { { Type: xdr.LedgerEntryTypeContractCode, ContractCode: &xdr.LedgerKeyContractCode{ - Hash: xdr.Hash(testContractId), + Hash: xdr.Hash(contractHash), }, }, }, }, - Instructions: 79653, + Instructions: 4961700, ReadBytes: 48, - WriteBytes: 64, + WriteBytes: 5468, }, RefundableFee: 20056, } @@ -261,7 +253,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) { assert.Equal(t, expectedXdr, resultXdr) // test operation which does not have a source account - withoutSourceAccountOp := createInstallContractCodeOperation("", testContract) + withoutSourceAccountOp := createInstallContractCodeOperation("", contractBinary) params = txnbuild.TransactionParams{ SourceAccount: &txnbuild.SimpleAccount{ AccountID: sourceAccount, @@ -290,7 +282,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) { }, IncrementSequenceNum: false, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(sourceAccount, testContract), + createInstallContractCodeOperation(sourceAccount, contractBinary), }, BaseFee: txnbuild.MinBaseFee, Memo: nil, @@ -597,6 +589,7 @@ func TestSimulateTransactionMultipleOperations(t *testing.T) { client := jrpc2.NewClient(ch, nil) sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address() + contractBinary := getHelloWorldContract(t) params := txnbuild.TransactionParams{ SourceAccount: &txnbuild.SimpleAccount{ AccountID: keypair.Root(StandaloneNetworkPassphrase).Address(), @@ -604,8 +597,8 @@ func TestSimulateTransactionMultipleOperations(t *testing.T) { }, IncrementSequenceNum: false, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(sourceAccount, testContract), - createCreateContractOperation(t, sourceAccount, testContract, StandaloneNetworkPassphrase), + createInstallContractCodeOperation(sourceAccount, contractBinary), + createCreateContractOperation(t, sourceAccount, contractBinary, StandaloneNetworkPassphrase), }, BaseFee: txnbuild.MinBaseFee, Memo: nil, diff --git a/cmd/soroban-rpc/internal/test/transaction_test.go b/cmd/soroban-rpc/internal/test/transaction_test.go index 14e7318bb..b53c8f25b 100644 --- a/cmd/soroban-rpc/internal/test/transaction_test.go +++ b/cmd/soroban-rpc/internal/test/transaction_test.go @@ -2,6 +2,7 @@ package test import ( "context" + "crypto/sha256" "fmt" "testing" "time" @@ -54,11 +55,12 @@ func TestSendTransactionSucceedsWithResults(t *testing.T) { address := kp.Address() account := txnbuild.NewSimpleAccount(address, 0) + contractBinary := getHelloWorldContract(t) params := preflightTransactionParams(t, client, txnbuild.TransactionParams{ SourceAccount: &account, IncrementSequenceNum: true, Operations: []txnbuild.Operation{ - createInstallContractCodeOperation(account.AccountID, testContract), + createInstallContractCodeOperation(account.AccountID, contractBinary), }, BaseFee: txnbuild.MinBaseFee, Preconditions: txnbuild.Preconditions{ @@ -77,8 +79,9 @@ func TestSendTransactionSucceedsWithResults(t *testing.T) { invokeHostFunctionResult, ok := opResults[0].MustTr().GetInvokeHostFunctionResult() assert.True(t, ok) assert.Equal(t, invokeHostFunctionResult.Code, xdr.InvokeHostFunctionResultCodeInvokeHostFunctionSuccess) - contractIDBytes := xdr.ScBytes(testContractId) - expectedScVal := xdr.ScVal{Type: xdr.ScValTypeScvBytes, Bytes: &contractIDBytes} + contractHash := sha256.Sum256(contractBinary) + contractHashBytes := xdr.ScBytes(contractHash[:]) + expectedScVal := xdr.ScVal{Type: xdr.ScValTypeScvBytes, Bytes: &contractHashBytes} var transactionMeta xdr.TransactionMeta assert.NoError(t, xdr.SafeUnmarshalBase64(response.ResultMetaXdr, &transactionMeta)) assert.True(t, expectedScVal.Equals(transactionMeta.V3.SorobanMeta.ReturnValue)) @@ -158,7 +161,7 @@ func TestSendTransactionFailedInLedger(t *testing.T) { address := kp.Address() account := txnbuild.NewSimpleAccount(address, 0) - op := createInstallContractCodeOperation(account.AccountID, testContract) + op := createInstallContractCodeOperation(account.AccountID, getHelloWorldContract(t)) // without the presources the tx will fail op.Ext = xdr.TransactionExt{ V: 1,