Skip to content

Commit

Permalink
[BP: v7 <- #773] Simplify verbose boilerplate builder (#826)
Browse files Browse the repository at this point in the history
* Simplify verbose boilerplate builder (#773)

* qol: NewGenesisKV(key, value)

* RegisterInterfaces, CreateChainWithConfig, BuildInitialChain

* migrate examples to new functions ^^

* Add BuildChainWithRelayer abstraction

* Migrate examples to the new format

* 2 unique chainSpecs

* stop relayer on cleanup

* if links != nil, setup relayer

* stop relayer only if it was set

(cherry picked from commit 2c47fa0)

# Conflicts:
#	examples/ibc/interchain_accounts_test.go

* fix imports

* v7 imports

---------

Co-authored-by: Reece Williams <[email protected]>
Co-authored-by: Reece Williams <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2023
1 parent dd072db commit ef23cf6
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
with:
version: latest
only-new-issues: true
args: --timeout=5m
args: --timeout=10m

clippy-lint:
defaults:
Expand Down
5 changes: 4 additions & 1 deletion chain/cosmos/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ type GenesisKV struct {
}

func NewGenesisKV(key string, value interface{}) GenesisKV {
return GenesisKV{Key: key, Value: value}
return GenesisKV{
Key: key,
Value: value,
}
}

func ModifyGenesis(genesisKV []GenesisKV) func(ibc.ChainConfig, []byte) ([]byte, error) {
Expand Down
36 changes: 5 additions & 31 deletions examples/cosmos/chain_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

func TestJunoStateExport(t *testing.T) {
Expand All @@ -27,39 +26,14 @@ func CosmosChainStateExportTest(t *testing.T, name, version string) {
numVals := 1
numFullNodes := 0

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

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
// defaults to Juno
cfg := ibc.ChainConfig{}

chains := interchaintest.CreateChainWithConfig(t, numVals, numFullNodes, name, version, cfg)
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()
})
enableBlockDB := false
ctx, _, _, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

HaltChainAndExportGenesis(ctx, t, chain, nil, 3)
}
Expand Down
55 changes: 10 additions & 45 deletions examples/cosmos/chain_param_change_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cosmos_test

import (
"context"
"encoding/json"
"testing"

Expand All @@ -10,7 +9,6 @@ import (
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestJunoParamChange(t *testing.T) {
Expand All @@ -29,54 +27,21 @@ func CosmosChainParamChangeTest(t *testing.T, name, version string) {

// SDK v45 params for Juno genesis
shortVoteGenesis := []cosmos.GenesisKV{
{
Key: "app_state.gov.voting_params.voting_period",
Value: votingPeriod,
},
{
Key: "app_state.gov.deposit_params.max_deposit_period",
Value: maxDepositPeriod,
},
{
Key: "app_state.gov.deposit_params.min_deposit.0.denom",
Value: "ujuno",
},
cosmos.NewGenesisKV("app_state.gov.voting_params.voting_period", votingPeriod),
cosmos.NewGenesisKV("app_state.gov.deposit_params.max_deposit_period", maxDepositPeriod),
cosmos.NewGenesisKV("app_state.gov.deposit_params.min_deposit.0.denom", "ujuno"),
}

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

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
cfg := ibc.ChainConfig{
Denom: "ujuno",
ModifyGenesis: cosmos.ModifyGenesis(shortVoteGenesis),
}

chains := interchaintest.CreateChainWithConfig(t, numVals, numFullNodes, name, version, cfg)
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()
})
enableBlockDB := false
ctx, _, _, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

const userFunds = int64(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
Expand Down
20 changes: 4 additions & 16 deletions examples/cosmos/chain_upgrade_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,12 @@ func CosmosChainUpgradeIBCTest(t *testing.T, chainName, initialVersion, upgradeC

// SDK v45 params for Juno genesis
shortVoteGenesis := []cosmos.GenesisKV{
{
Key: "app_state.gov.voting_params.voting_period",
Value: votingPeriod,
},
{
Key: "app_state.gov.deposit_params.max_deposit_period",
Value: maxDepositPeriod,
},
{
Key: "app_state.gov.deposit_params.min_deposit.0.denom",
Value: "ujuno",
},
cosmos.NewGenesisKV("app_state.gov.voting_params.voting_period", votingPeriod),
cosmos.NewGenesisKV("app_state.gov.deposit_params.max_deposit_period", maxDepositPeriod),
cosmos.NewGenesisKV("app_state.gov.deposit_params.min_deposit.0.denom", "ujuno"),
}

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
chains := interchaintest.CreateChainsWithChainSpecs(t, []*interchaintest.ChainSpec{
{
Name: chainName,
ChainName: chainName,
Expand All @@ -66,9 +57,6 @@ func CosmosChainUpgradeIBCTest(t *testing.T, chainName, initialVersion, upgradeC
},
})

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

client, network := interchaintest.DockerSetup(t)

chain, counterpartyChain := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)
Expand Down
4 changes: 1 addition & 3 deletions examples/cosmos/light_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ func TestUpdateLightClients(t *testing.T) {
ctx := context.Background()

// Chains
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
chains := interchaintest.CreateChainsWithChainSpecs(t, []*interchaintest.ChainSpec{
{Name: "gaia", Version: gaiaVersion},
{Name: "osmosis", Version: osmosisVersion},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
gaia, osmosis := chains[0], chains[1]

// Relayer
Expand Down
8 changes: 1 addition & 7 deletions examples/cosmos/sdk_boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ func TestSDKBoundaries(t *testing.T) {
t.Run(testname, func(t *testing.T) {
t.Parallel()

cf := interchaintest.NewBuiltinChainFactory(
zaptest.NewLogger(t),
tt.chainSpecs,
)

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
chains := interchaintest.CreateChainsWithChainSpecs(t, tt.chainSpecs)

client, network := interchaintest.DockerSetup(t)

Expand Down
38 changes: 7 additions & 31 deletions examples/cosmos/state_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestCosmosHubStateSync(t *testing.T) {
Expand Down Expand Up @@ -46,39 +45,16 @@ func CosmosChainStateSyncTest(t *testing.T, chainName, version string) {

configFileOverrides["config/app.toml"] = appTomlOverrides

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: chainName,
ChainName: chainName,
Version: version,
ChainConfig: ibc.ChainConfig{
ConfigFileOverrides: configFileOverrides,
},
NumFullNodes: &nf,
},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
cfg := ibc.ChainConfig{
ConfigFileOverrides: configFileOverrides,
}

chains := interchaintest.CreateChainWithConfig(t, 1, nf, chainName, version, cfg)

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,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: true,
}))
t.Cleanup(func() {
_ = ic.Close()
})
enableBlockDB := false
ctx, _, _, _ := interchaintest.BuildInitialChain(t, chains, enableBlockDB)

// Wait for blocks so that nodes have a few state sync snapshot available
require.NoError(t, testutil.WaitForBlocks(ctx, stateSyncSnapshotInterval*2, chain))
Expand Down
65 changes: 22 additions & 43 deletions examples/ibc/interchain_accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ibc

import (
"context"
"encoding/json"
"strings"
"testing"
Expand All @@ -13,11 +12,8 @@ import (
"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/relayer"
"github.com/strangelove-ventures/interchaintest/v7/testreporter"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

// TestInterchainAccounts is a test case that performs simulations and assertions around some basic
Expand All @@ -29,15 +25,8 @@ func TestInterchainAccounts(t *testing.T) {

t.Parallel()

client, network := interchaintest.DockerSetup(t)

rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)

ctx := context.Background()

// Get both chains
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
// Create 2 chains from the same chain spec
chains := interchaintest.CreateChainsWithChainSpecs(t, []*interchaintest.ChainSpec{
{
Name: "icad",
ChainConfig: ibc.ChainConfig{
Expand All @@ -52,39 +41,29 @@ func TestInterchainAccounts(t *testing.T) {
},
})

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

chain1, chain2 := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Get a relayer instance
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayer.StartupFlags("-p", "events", "-b", "100"),
).Build(t, client, network)

// Build the network; spin up the chains and configure the relayer
const pathName = "test-path"
const relayerName = "relayer"

ic := interchaintest.NewInterchain().
AddChain(chain1).
AddChain(chain2).
AddRelayer(r, relayerName).
AddLink(interchaintest.InterchainLink{
Chain1: chain1,
Chain2: chain2,
Relayer: r,
Path: pathName,
})

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
}))
relayerFlags := []string{"-p", "events", "-b", "100"}
enableBlockDB := false
skipRelayerPathCreation := true

ctx, _, r, _, eRep, _, _ := interchaintest.BuildInitialChainWithRelayer(
t,
chains,
enableBlockDB,
ibc.CosmosRly,
relayerFlags,
[]interchaintest.InterchainLink{
{
Chain1: chain1,
Chain2: chain2,
Path: pathName,
},
},
skipRelayerPathCreation,
)

// Fund a user account on chain1 and chain2
const userFunds = int64(10_000_000_000)
Expand All @@ -93,7 +72,7 @@ func TestInterchainAccounts(t *testing.T) {
chain2User := users[1]

// Generate a new IBC path
err = r.GeneratePath(ctx, eRep, chain1.Config().ChainID, chain2.Config().ChainID, pathName)
err := r.GeneratePath(ctx, eRep, chain1.Config().ChainID, chain2.Config().ChainID, pathName)
require.NoError(t, err)

// Create new clients
Expand Down
Loading

0 comments on commit ef23cf6

Please sign in to comment.