From 97f755faccc9ccc4bbea5dd154e5f32a48eb351d Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Mon, 22 Apr 2024 20:34:28 +0200 Subject: [PATCH] Add test upgrading Protocol 20 to Protocol 21 (#150) --- cmd/soroban-rpc/internal/test/integration.go | 13 +- cmd/soroban-rpc/internal/test/upgrade_test.go | 125 ++++++++++++++++++ 2 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 cmd/soroban-rpc/internal/test/upgrade_test.go diff --git a/cmd/soroban-rpc/internal/test/integration.go b/cmd/soroban-rpc/internal/test/integration.go index 22a83154..882e2250 100644 --- a/cmd/soroban-rpc/internal/test/integration.go +++ b/cmd/soroban-rpc/internal/test/integration.go @@ -95,13 +95,7 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test { if i.protocolVersion == 0 { // Default to the maximum supported protocol version - i.protocolVersion = maxSupportedProtocolVersion - // If the environment tells us that Core only supports up to certain version, - // use that. - maxSupportedCoreProtocolFromEnv := GetCoreMaxSupportedProtocol() - if maxSupportedCoreProtocolFromEnv != 0 && maxSupportedCoreProtocolFromEnv < maxSupportedProtocolVersion { - i.protocolVersion = maxSupportedCoreProtocolFromEnv - } + i.protocolVersion = GetCoreMaxSupportedProtocol() } proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: stellarCoreArchiveHost}) @@ -411,11 +405,12 @@ func findDockerComposePath() string { func GetCoreMaxSupportedProtocol() uint32 { str := os.Getenv("SOROBAN_RPC_INTEGRATION_TESTS_CORE_MAX_SUPPORTED_PROTOCOL") if str == "" { - return 0 + return maxSupportedProtocolVersion } version, err := strconv.ParseUint(str, 10, 32) if err != nil { - return 0 + return maxSupportedProtocolVersion } + return uint32(version) } diff --git a/cmd/soroban-rpc/internal/test/upgrade_test.go b/cmd/soroban-rpc/internal/test/upgrade_test.go new file mode 100644 index 00000000..6889e614 --- /dev/null +++ b/cmd/soroban-rpc/internal/test/upgrade_test.go @@ -0,0 +1,125 @@ +package test + +import ( + "context" + "testing" + "time" + + "github.com/creachadair/jrpc2" + "github.com/creachadair/jrpc2/jhttp" + "github.com/stellar/go/keypair" + "github.com/stellar/go/txnbuild" + "github.com/stellar/go/xdr" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db" +) + +func TestUpgradeFrom20To21(t *testing.T) { + if GetCoreMaxSupportedProtocol() != 21 { + t.Skip("Only test this for protocol 21") + } + test := NewTest(t, &TestConfig{ + ProtocolVersion: 20, + }) + + 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) + assert.NoError(t, err) + sendSuccessfulTransaction(t, client, sourceAccount, tx) + + // Upgrade to protocol 21 and re-upload the contract, which should cause a caching of the contract + // estimations + test.UpgradeProtocol(21) + // Wait for the ledger to advance, so that the simulation library passes the right protocol number + rpcDB := test.daemon.GetDB() + initialLedgerSequence, err := db.NewLedgerEntryReader(rpcDB).GetLatestLedgerSequence(context.Background()) + require.Eventually(t, + func() bool { + newLedgerSequence, err := db.NewLedgerEntryReader(rpcDB).GetLatestLedgerSequence(context.Background()) + require.NoError(t, err) + return newLedgerSequence > initialLedgerSequence + }, + time.Minute, + time.Second, + ) + + 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) + assert.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) + assert.NoError(t, err) + sendSuccessfulTransaction(t, client, sourceAccount, tx) + + contractID := getContractID(t, address, testSalt, StandaloneNetworkPassphrase) + contractFnParameterSym := xdr.ScSymbol("world") + params = preflightTransactionParams(t, client, txnbuild.TransactionParams{ + SourceAccount: &account, + IncrementSequenceNum: true, + Operations: []txnbuild.Operation{ + createInvokeHostOperation( + address, + contractID, + "hello", + xdr.ScVal{ + Type: xdr.ScValTypeScvSymbol, + Sym: &contractFnParameterSym, + }, + ), + }, + BaseFee: txnbuild.MinBaseFee, + Preconditions: txnbuild.Preconditions{ + TimeBounds: txnbuild.NewInfiniteTimeout(), + }, + }) + tx, err = txnbuild.NewTransaction(params) + assert.NoError(t, err) + + sendSuccessfulTransaction(t, client, sourceAccount, tx) +}