Skip to content

Commit

Permalink
Multiple cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jun 13, 2024
1 parent dbe9bcd commit 670d3d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
35 changes: 22 additions & 13 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ const (
type TestConfig struct {
historyArchiveProxyCallback func(*http.Request)
ProtocolVersion uint32
UseRealRPCVersion string
UseSQLitePath string
// Run a previously released version of RPC (in a container) instead of the current version
UseReleasedRPCVersion string
UseSQLitePath string
}

type Test struct {
Expand All @@ -64,9 +65,9 @@ type Test struct {

protocolVersion uint32

rpcContainerVersion string
rpcConfigMountDir string
rpcSQLiteMountDir string
rpcContainerVersion string
rpcContainerConfigMountDir string
rpcContainerSQLiteMountDir string

daemon *daemon.Daemon

Expand Down Expand Up @@ -96,7 +97,7 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {

sqlLitePath := ""
if cfg != nil {
i.rpcContainerVersion = cfg.UseRealRPCVersion
i.rpcContainerVersion = cfg.UseReleasedRPCVersion
i.historyArchiveProxyCallback = cfg.historyArchiveProxyCallback
i.protocolVersion = cfg.ProtocolVersion
sqlLitePath = cfg.UseSQLitePath
Expand All @@ -118,7 +119,7 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {

rpcCfg := i.getRPConfig(sqlLitePath)
if i.rpcContainerVersion != "" {
i.rpcConfigMountDir = i.createRPCContainerMountDir(rpcCfg)
i.rpcContainerConfigMountDir = i.createRPCContainerMountDir(rpcCfg)
}
i.runComposeCommand("up", "--detach", "--quiet-pull", "--no-color")
i.prepareShutdownHandlers()
Expand Down Expand Up @@ -203,7 +204,7 @@ func (i *Test) getRPConfig(sqlitePath string) map[string]string {
// The container needs to listen on all interfaces, not just localhost
bindHost = "0.0.0.0"
// The container needs to use the sqlite mount point
i.rpcSQLiteMountDir = filepath.Dir(sqlitePath)
i.rpcContainerSQLiteMountDir = filepath.Dir(sqlitePath)
sqlitePath = "/db/" + filepath.Base(sqlitePath)
stellarCoreURL = fmt.Sprintf("http://core:%d", stellarCorePort)
}
Expand Down Expand Up @@ -315,8 +316,8 @@ func (i *Test) runComposeCommand(args ...string) {
cmd.Env = append(
cmd.Env,
"RPC_IMAGE_TAG="+i.rpcContainerVersion,
"RPC_CONFIG_MOUNT_DIR="+i.rpcConfigMountDir,
"RPC_SQLITE_MOUNT_DIR="+i.rpcSQLiteMountDir,
"RPC_CONFIG_MOUNT_DIR="+i.rpcContainerConfigMountDir,
"RPC_SQLITE_MOUNT_DIR="+i.rpcContainerSQLiteMountDir,
)
}
if len(cmd.Env) > 0 {
Expand All @@ -338,9 +339,7 @@ func (i *Test) runComposeCommand(args ...string) {
func (i *Test) prepareShutdownHandlers() {
i.shutdownCalls = append(i.shutdownCalls,
func() {
if i.daemon != nil {
i.daemon.Close()
}
i.StopRPC()
if i.historyArchiveProxy != nil {
i.historyArchiveProxy.Close()
}
Expand Down Expand Up @@ -435,6 +434,16 @@ func (i *Test) UpgradeProtocol(version uint32) {
i.t.Fatalf("could not upgrade protocol in 10s")
}

func (i *Test) StopRPC() {
if i.daemon != nil {
i.daemon.Close()
i.daemon = nil
}
if i.rpcContainerVersion != "" {
i.runComposeCommand("down", "rpc", "-v")
}
}

// Cluttering code with if err != nil is absolute nonsense.
func panicIf(err error) {
if err != nil {
Expand Down
22 changes: 8 additions & 14 deletions cmd/soroban-rpc/internal/test/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (

// Test that every Soroban RPC version (within the current protocol) can migrate cleanly to the current version
// We cannot test prior protocol versions since the Transaction XDR used for the test could be incompatible
// TODO: find a way to test migrations between protocols
func TestMigrate(t *testing.T) {
if GetCoreMaxSupportedProtocol() != MaxSupportedProtocolVersion {
t.Skip("Only test this for the latest protocol: ", MaxSupportedProtocolVersion)
}
for _, originVersion := range getCurrentProtocolReleaseVersions(t) {
for _, originVersion := range getCurrentProtocolReleasedVersions(t) {
if originVersion == "21.1.0" {
// This version of the RPC container fails to even start with its captive core companion file
// (it fails Invalid configuration: DEPRECATED_SQL_LEDGER_STATE not set.)
Expand All @@ -47,8 +48,8 @@ func TestMigrate(t *testing.T) {
func testMigrateFromVersion(t *testing.T, version string) {
sqliteFile := filepath.Join(t.TempDir(), "soroban-rpc.db")
it := NewTest(t, &TestConfig{
UseRealRPCVersion: version,
UseSQLitePath: sqliteFile,
UseReleasedRPCVersion: version,
UseSQLitePath: sqliteFile,
})

ch := jhttp.NewChannel(it.sorobanRPCURL(), nil)
Expand All @@ -75,18 +76,11 @@ func testMigrateFromVersion(t *testing.T, version string) {
assert.NoError(t, err)
submitTransactionResponse := sendSuccessfulTransaction(t, client, kp, tx)

// Check the transaction with current RPC, but the previous network and sql database (causing a data migration)
// TODO: create a dedicated method
it.runComposeCommand("down", "rpc", "-v")
// Run the current RPC version, but the previous network and sql database (causing a data migration if needed)
it.StopRPC()
it = NewTest(t, &TestConfig{UseSQLitePath: sqliteFile})

// make sure that the instance is healthy
var healthResult methods.HealthCheckResult
err = client.CallResult(context.Background(), "getHealth", nil, &healthResult)
require.NoError(t, err)
require.Equal(t, "healthy", healthResult.Status)

// make sure that the transaction submitted before and its events exist
// make sure that the transaction submitted before and its events exist in current RPC
var transactionsResult methods.GetTransactionsResponse
getTransactions := methods.GetTransactionsRequest{
StartLedger: submitTransactionResponse.Ledger,
Expand All @@ -112,7 +106,7 @@ func testMigrateFromVersion(t *testing.T, version string) {
require.Equal(t, submitTransactionResponse.Ledger, uint32(eventsResult.Events[0].Ledger))
}

func getCurrentProtocolReleaseVersions(t *testing.T) []string {
func getCurrentProtocolReleasedVersions(t *testing.T) []string {
protocolStr := strconv.Itoa(MaxSupportedProtocolVersion)
_, currentFilename, _, _ := runtime.Caller(0)
currentDir := filepath.Dir(currentFilename)
Expand Down

0 comments on commit 670d3d8

Please sign in to comment.