Skip to content

Commit

Permalink
Add test upgrading Protocol 20 to Protocol 21 (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Apr 22, 2024
1 parent 738a3e4 commit 97f755f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 9 deletions.
13 changes: 4 additions & 9 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
}
125 changes: 125 additions & 0 deletions cmd/soroban-rpc/internal/test/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 97f755f

Please sign in to comment.