From b8ae1b5750752fed3a5136716e2533336a0fdfdf Mon Sep 17 00:00:00 2001 From: Rootul P Date: Mon, 15 Jul 2024 16:46:57 +0200 Subject: [PATCH] refactor(testnode): create genTx on `Export` (#3673) Closes https://github.com/celestiaorg/celestia-app/issues/3662 Before, genTx transactions were created prematurely via [`WithValidators`](https://github.com/rootulp/celestia-app/blob/34cd4b34d99e27c5e8611abab6aaecc550499045/test/util/testnode/config.go#L123) which was problematic if a user tried to later override the chainID via `WithChainID` because the genTx transactions would use the default chain ID but the state machine would be created with a custom chain ID. This PR refactors the genTx creation to happen only when `genesis.Export` is invoked which is _after_ a user configures a custom chain ID. (cherry picked from commit 1703fe2c2b2e7b9302be5e9638144b953c15ff23) --- app/test/testnode_test.go | 18 ++++++++++++++++++ test/util/genesis/genesis.go | 15 ++++++--------- test/util/testnode/config.go | 4 ++-- 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 app/test/testnode_test.go diff --git a/app/test/testnode_test.go b/app/test/testnode_test.go new file mode 100644 index 0000000000..b33a87eaec --- /dev/null +++ b/app/test/testnode_test.go @@ -0,0 +1,18 @@ +package app_test + +import ( + "testing" + + "github.com/celestiaorg/celestia-app/v2/test/util/testnode" +) + +func Test_testnode(t *testing.T) { + t.Run("testnode can start a network with default chain ID", func(t *testing.T) { + testnode.NewNetwork(t, testnode.DefaultConfig()) + }) + t.Run("testnode can start a network with a custom chain ID", func(t *testing.T) { + chainID := "custom-chain-id" + config := testnode.DefaultConfig().WithChainID(chainID) + testnode.NewNetwork(t, config) + }) +} diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index ab2e8a93eb..228997f88b 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -167,14 +167,6 @@ func (g *Genesis) AddValidator(val Validator) error { return err } - // Add the validator's genesis transaction - gentx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) - if err != nil { - return err - } - - // install the validator - g.genTxs = append(g.genTxs, gentx) g.validators = append(g.validators, val) return nil } @@ -192,7 +184,12 @@ func (g *Genesis) NewValidator(val Validator) error { // Export returns the genesis document of the network. func (g *Genesis) Export() (*coretypes.GenesisDoc, error) { gentxs := make([]json.RawMessage, 0, len(g.genTxs)) - for _, genTx := range g.genTxs { + for _, val := range g.validators { + genTx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) + if err != nil { + return nil, err + } + bz, err := g.ecfg.TxConfig.TxJSONEncoder()(genTx) if err != nil { return nil, err diff --git a/test/util/testnode/config.go b/test/util/testnode/config.go index 18990d7c16..3659c8dd14 100644 --- a/test/util/testnode/config.go +++ b/test/util/testnode/config.go @@ -128,8 +128,8 @@ func DefaultConfig() *Config { WithAppOptions(DefaultAppOptions()). WithAppCreator(cmd.NewAppServer). WithSuppressLogs(true). - WithConsensusParams(DefaultConsensusParams()). - WithSuppressLogs(true) + // TODO: consider removing this line because default consensus params is invoked above. + WithConsensusParams(DefaultConsensusParams()) } func DefaultConsensusParams() *tmproto.ConsensusParams {