Skip to content

Commit

Permalink
chore!: refactor testnode to expose appcreator (celestiaorg#1991)
Browse files Browse the repository at this point in the history
## Overview

This refactor is mainly targeting celestiaorg#1990, but doing so in a way that uses
common patters to avoid continuing to blow up the api

closes celestiaorg#1990

## Checklist

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [x] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords
  • Loading branch information
evan-forbes authored Jun 27, 2023
1 parent 4725fd8 commit ce0be95
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 248 deletions.
10 changes: 3 additions & 7 deletions app/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,9 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.accounts[i] = tmrand.Str(20)
}

cctx, _, _ := testnode.NewNetwork(
t,
testnode.DefaultParams(),
testnode.DefaultTendermintConfig(),
testnode.DefaultAppConfig(),
s.accounts,
)
cfg := testnode.DefaultConfig().WithAccounts(s.accounts)

cctx, _, _ := testnode.NewNetwork(t, cfg)

s.cctx = cctx
s.ecfg = encoding.MakeConfig(app.ModuleEncodingRegisters...)
Expand Down
12 changes: 3 additions & 9 deletions app/test/qgb_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ func TestQGBRPCQueries(t *testing.T) {
if testing.Short() {
t.Skip("skipping QGB integration test in short mode.")
}
tmCfg := testnode.DefaultTendermintConfig()
tmCfg.Consensus.TargetHeightDuration = time.Millisecond
cfg := testnode.DefaultConfig()

cctx, _, _ := testnode.NewNetwork(t, cfg)

cctx, _, _ := testnode.NewNetwork(
t,
testnode.DefaultParams(),
tmCfg,
testnode.DefaultAppConfig(),
[]string{},
)
h, err := cctx.WaitForHeightWithTimeout(105, 2*time.Minute)
require.NoError(t, err, h)
require.Greater(t, h, int64(101))
Expand Down
17 changes: 4 additions & 13 deletions app/test/square_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func TestSquareSizeIntegrationTest(t *testing.T) {
type SquareSizeIntegrationTest struct {
suite.Suite

accounts []string
cctx testnode.Context
rpcAddr, grpcAddr string
ecfg encoding.Config
Expand All @@ -44,19 +43,11 @@ func (s *SquareSizeIntegrationTest) SetupSuite() {
t := s.T()
t.Log("setting up square size integration test")
s.ecfg = encoding.MakeConfig(app.ModuleEncodingRegisters...)
accounts := []string{}

cparams := testnode.DefaultParams()
cctx, rpcAddr, grpcAddr := testnode.NewNetwork(
t,
cparams,
testnode.DefaultTendermintConfig(),
testnode.DefaultAppConfig(),
accounts,
testnode.ImmediateProposals(s.ecfg.Codec), // pass param changes in 5 seconds
)
cfg := testnode.DefaultConfig().
WithGenesisOptions(testnode.ImmediateProposals(s.ecfg.Codec))

cctx, rpcAddr, grpcAddr := testnode.NewNetwork(t, cfg)

s.accounts = accounts
s.cctx = cctx
s.rpcAddr = rpcAddr
s.grpcAddr = grpcAddr
Expand Down
12 changes: 10 additions & 2 deletions app/test/std_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand"
)

func TestStandardSDKIntegrationTestSuite(t *testing.T) {
Expand All @@ -47,8 +48,15 @@ type StandardSDKIntegrationTestSuite struct {
func (s *StandardSDKIntegrationTestSuite) SetupSuite() {
t := s.T()
t.Log("setting up integration test suite")
accounts, cctx := testnode.DefaultNetwork(t)
s.accounts = accounts

accounts := make([]string, 300)
for i := 0; i < len(accounts); i++ {
accounts[i] = tmrand.Str(9)
}

cfg := testnode.DefaultConfig().WithAccounts(accounts)
cctx, _, _ := testnode.NewNetwork(t, cfg)
s.accounts = cfg.Accounts
s.ecfg = encoding.MakeConfig(app.ModuleEncodingRegisters...)
s.cctx = cctx
}
Expand Down
12 changes: 5 additions & 7 deletions test/cmd/txsim/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ func setup(t testing.TB) (keyring.Keyring, string, string) {
cparams := testnode.DefaultParams()
cparams.Block.MaxBytes = int64(appconsts.DefaultSquareSizeUpperBound*appconsts.DefaultSquareSizeUpperBound) * appconsts.ContinuationSparseShareContentSize

cctx, rpcAddr, grpcAddr := testnode.NewNetwork(
t,
cparams,
testnode.DefaultTendermintConfig(),
testnode.DefaultAppConfig(),
[]string{testfactory.TestAccName},
)
cfg := testnode.DefaultConfig().
WithConsensusParams(cparams).
WithAccounts([]string{testfactory.TestAccName})

cctx, rpcAddr, grpcAddr := testnode.NewNetwork(t, cfg)

return cctx.Keyring, rpcAddr, grpcAddr
}
10 changes: 3 additions & 7 deletions test/txsim/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,9 @@ func TestTxSimulator(t *testing.T) {
func Setup(t testing.TB) (keyring.Keyring, string, string) {
t.Helper()

cctx, rpcAddr, grpcAddr := testnode.NewNetwork(
t,
testnode.DefaultParams(),
testnode.DefaultTendermintConfig(),
testnode.DefaultAppConfig(),
[]string{},
)
cfg := testnode.DefaultConfig()

cctx, rpcAddr, grpcAddr := testnode.NewNetwork(t, cfg)

return cctx.Keyring, rpcAddr, grpcAddr
}
157 changes: 157 additions & 0 deletions test/util/testnode/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package testnode

import (
"time"

"github.com/celestiaorg/celestia-app/cmd/celestia-appd/cmd"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/tendermint/tendermint/config"
tmconfig "github.com/tendermint/tendermint/config"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)

// Config is the configuration of a test node.
type Config struct {
// ChainID is the chain ID of the network.
ChainID string
// TmConfig is the Tendermint configuration used for the network.
TmConfig *tmconfig.Config
// AppConfig is the application configuration of the test node.
AppConfig *srvconfig.Config
// ConsensusParams are the consensus parameters of the test node.
ConsensusParams *tmproto.ConsensusParams
// AppOptions are the application options of the test node.
AppOptions *KVAppOptions
// GenesisOptions are the genesis options of the test node.
GenesisOptions []GenesisOption
// Accounts are the accounts of the test node.
Accounts []string
// AppCreator is used to create the application for the testnode.
AppCreator srvtypes.AppCreator
// SupressLogs
SupressLogs bool
}

// WithChainID sets the ChainID and returns the Config.
func (c *Config) WithChainID(s string) *Config {
c.ChainID = s
return c
}

// WithTendermintConfig sets the TmConfig and returns the *Config.
func (c *Config) WithTendermintConfig(conf *tmconfig.Config) *Config {
c.TmConfig = conf
return c
}

// WithAppConfig sets the AppConfig and returns the Config.
func (c *Config) WithAppConfig(conf *srvconfig.Config) *Config {
c.AppConfig = conf
return c
}

// WithConsensusParams sets the ConsensusParams and returns the Config.
func (c *Config) WithConsensusParams(params *tmproto.ConsensusParams) *Config {
c.ConsensusParams = params
return c
}

// WithAppOptions sets the AppOptions and returns the Config.
func (c *Config) WithAppOptions(opts *KVAppOptions) *Config {
c.AppOptions = opts
return c
}

// WithGenesisOptions sets the GenesisOptions and returns the Config.
func (c *Config) WithGenesisOptions(opts ...GenesisOption) *Config {
c.GenesisOptions = opts
return c
}

// WithAccounts sets the Accounts and returns the Config.
func (c *Config) WithAccounts(accs []string) *Config {
c.Accounts = accs
return c
}

// WithAppCreator sets the AppCreator and returns the Config.
func (c *Config) WithAppCreator(creator srvtypes.AppCreator) *Config {
c.AppCreator = creator
return c
}

// WithSupressLogs sets the SupressLogs and returns the Config.
func (c *Config) WithSupressLogs(sl bool) *Config {
c.SupressLogs = sl
return c
}

func DefaultConfig() *Config {
tmcfg := DefaultTendermintConfig()
tmcfg.Consensus.TargetHeightDuration = 1 * time.Millisecond
cfg := &Config{}
return cfg.
WithAccounts([]string{}).
WithChainID(tmrand.Str(6)).
WithTendermintConfig(DefaultTendermintConfig()).
WithAppConfig(DefaultAppConfig()).
WithConsensusParams(DefaultParams()).
WithAppOptions(DefaultAppOptions()).
WithGenesisOptions().
WithAppCreator(cmd.NewAppServer).
WithSupressLogs(true)
}

type KVAppOptions struct {
options map[string]interface{}
}

// Get implements AppOptions
func (ao *KVAppOptions) Get(o string) interface{} {
return ao.options[o]
}

// Set adds an option to the KVAppOptions
func (ao *KVAppOptions) Set(o string, v interface{}) {
ao.options[o] = v
}

// DefaultAppOptions returns the default application options.
func DefaultAppOptions() *KVAppOptions {
opts := &KVAppOptions{options: make(map[string]interface{})}
opts.Set(server.FlagPruning, pruningtypes.PruningOptionNothing)
return opts
}

func DefaultParams() *tmproto.ConsensusParams {
cparams := types.DefaultConsensusParams()
cparams.Block.TimeIotaMs = 1
cparams.Block.MaxBytes = appconsts.DefaultMaxBytes
return cparams
}

func DefaultTendermintConfig() *config.Config {
tmCfg := config.DefaultConfig()
// Reduce the target height duration so that blocks are produced faster
// during tests.
tmCfg.Consensus.TargetHeightDuration = 300 * time.Millisecond
tmCfg.Consensus.TimeoutPropose = 200 * time.Millisecond

// set the mempool's MaxTxBytes to allow the testnode to accept a
// transaction that fills the entire square. Any blob transaction larger
// than the square size will still fail no matter what.
tmCfg.Mempool.MaxTxBytes = appconsts.DefaultMaxBytes

// remove all barriers from the testnode being able to accept very large
// transactions and respond to very queries with large responses (~200MB was
// chosen only as an arbitrary large number).
tmCfg.RPC.MaxBodyBytes = 200_000_000

return tmCfg
}
Loading

0 comments on commit ce0be95

Please sign in to comment.