Skip to content

Commit

Permalink
Cleaner ExportState via document for SDK v47+ (#740)
Browse files Browse the repository at this point in the history
* feat: ExportState to document for SDK v47+

* rename var: isNewGenesis

* test: SDK v45 & 47 state exports

* fix: read export data from proper file location

* Remove unused apptoml halt-height
  • Loading branch information
Reecepbcups authored Sep 7, 2023
1 parent 61dd192 commit 8907a51
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
24 changes: 23 additions & 1 deletion chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,32 @@ func (tn *ChainNode) ExportState(ctx context.Context, height int64) (string, err
tn.lock.Lock()
defer tn.lock.Unlock()

stdout, stderr, err := tn.ExecBin(ctx, "export", "--height", fmt.Sprint(height))
var (
doc = "state_export.json"
docPath = path.Join(tn.HomeDir(), doc)

isNewGenesis = tn.Chain.Config().UsingNewGenesisCommand

command = []string{"export", "--height", fmt.Sprint(height), "--home", tn.HomeDir()}
)

if isNewGenesis {
command = append(command, "--output-document", docPath)
}

stdout, stderr, err := tn.ExecBin(ctx, command...)
if err != nil {
return "", err
}

if isNewGenesis {
content, err := tn.ReadFile(ctx, doc)
if err != nil {
return "", err
}
return string(content), nil
}

// output comes to stderr on older versions
return string(stdout) + string(stderr), nil
}
Expand Down
116 changes: 116 additions & 0 deletions examples/cosmos/chain_export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cosmos_test

import (
"context"
"testing"
"time"

"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

func TestJunoStateExport(t *testing.T) {
// SDK v45
CosmosChainStateExportTest(t, "juno", "v15.0.0", false)
// SDK v47
CosmosChainStateExportTest(t, "juno", "v16.0.0", true)
}

func CosmosChainStateExportTest(t *testing.T, name, version string, useNewGenesisCmd bool) {
if testing.Short() {
t.Skip("skipping in short mode")
}

numVals := 1
numFullNodes := 0

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: name,
ChainName: name,
Version: version,
ChainConfig: ibc.ChainConfig{
Denom: "ujuno",
UsingNewGenesisCommand: useNewGenesisCmd,
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

chain := chains[0].(*cosmos.CosmosChain)

ic := interchaintest.NewInterchain().
AddChain(chain)

ctx := context.Background()
client, network := interchaintest.DockerSetup(t)

require.NoError(t, ic.Build(ctx, nil, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
}))
t.Cleanup(func() {
_ = ic.Close()
})

HaltChainAndExportGenesis(ctx, t, chain, nil, 3)
}

func HaltChainAndExportGenesis(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, relayer ibc.Relayer, haltHeight int64) {
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Minute*2)
defer timeoutCtxCancel()

err := testutil.WaitForBlocks(timeoutCtx, int(haltHeight), chain)
require.NoError(t, err, "chain did not halt at halt height")

err = chain.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")

state, err := chain.ExportState(ctx, int64(haltHeight))
require.NoError(t, err, "error exporting state")

appToml := make(testutil.Toml)

for _, node := range chain.Nodes() {
err := node.OverwriteGenesisFile(ctx, []byte(state))
require.NoError(t, err)
}

for _, node := range chain.Nodes() {
err := testutil.ModifyTomlConfigFile(
ctx,
zap.NewExample(),
node.DockerClient,
node.TestName,
node.VolumeName,
"config/app.toml",
appToml,
)
require.NoError(t, err)
}

err = chain.StartAllNodes(ctx)
require.NoError(t, err, "error starting node(s)")

timeoutCtx, timeoutCtxCancel = context.WithTimeout(ctx, time.Minute*2)
defer timeoutCtxCancel()

err = testutil.WaitForBlocks(timeoutCtx, int(5), chain)
require.NoError(t, err, "chain did not produce blocks after halt")

height, err := chain.Height(ctx)
require.NoError(t, err, "error getting height after halt")

require.Greater(t, int64(height), haltHeight, "height did not increment after halt")
}

0 comments on commit 8907a51

Please sign in to comment.