Skip to content

Commit

Permalink
soroban-rpc: Fix integration tests (stellar#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms authored Sep 13, 2023
1 parent ebf3c0a commit 47fb001
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
7 changes: 4 additions & 3 deletions cmd/soroban-rpc/internal/test/get_ledger_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions cmd/soroban-rpc/internal/test/get_ledger_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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)
}
35 changes: 14 additions & 21 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -202,14 +195,15 @@ 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,
Sequence: 0,
},
IncrementSequenceNum: false,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(sourceAccount, testContract),
createInstallContractCodeOperation(sourceAccount, contractBinary),
},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Expand All @@ -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))
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -597,15 +589,16 @@ 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(),
Sequence: 0,
},
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,
Expand Down
11 changes: 7 additions & 4 deletions cmd/soroban-rpc/internal/test/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"context"
"crypto/sha256"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -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{
Expand All @@ -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))
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 47fb001

Please sign in to comment.