Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ABCI++): update/fix testutil tests #16088

Merged
merged 9 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions testutil/integration/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"fmt"

cmtabcitypes "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
cmtabcitypes "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -27,13 +26,21 @@ const appName = "integration-app"
type App struct {
*baseapp.BaseApp

ctx sdk.Context
logger log.Logger
queryHelper *baseapp.QueryServiceTestHelper
ctx sdk.Context
logger log.Logger
moduleManager module.Manager
queryHelper *baseapp.QueryServiceTestHelper
}

// NewIntegrationApp creates an application for testing purposes. This application is able to route messages to their respective handlers.
func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*storetypes.KVStoreKey, appCodec codec.Codec, modules ...module.AppModule) *App {
// NewIntegrationApp creates an application for testing purposes. This application
// is able to route messages to their respective handlers.
func NewIntegrationApp(
sdkCtx sdk.Context,
logger log.Logger,
keys map[string]*storetypes.KVStoreKey,
appCodec codec.Codec,
modules ...module.AppModule,
) *App {
db := dbm.NewMemDB()

interfaceRegistry := codectypes.NewInterfaceRegistry()
Expand All @@ -45,7 +52,7 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseapp.SetChainID(appName))
bApp.MountKVStores(keys)

bApp.SetInitChainer(func(ctx sdk.Context, req *cmtabcitypes.RequestInitChain) (*cmtabcitypes.ResponseInitChain, error) {
bApp.SetInitChainer(func(ctx sdk.Context, _ *cmtabcitypes.RequestInitChain) (*cmtabcitypes.ResponseInitChain, error) {
for _, mod := range modules {
if m, ok := mod.(module.HasGenesis); ok {
m.InitGenesis(ctx, appCodec, m.DefaultGenesis(appCodec))
Expand Down Expand Up @@ -77,25 +84,25 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
ctx := sdkCtx.WithBlockHeader(cmtproto.Header{ChainID: appName}).WithIsCheckTx(true)

return &App{
BaseApp: bApp,

logger: logger,
ctx: ctx,
queryHelper: baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry),
BaseApp: bApp,
logger: logger,
ctx: ctx,
moduleManager: *moduleManager,
queryHelper: baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry),
}
}

// RunMsg allows to run a message and return the response.
// RunMsg provides the ability to run a message and return the response.
// In order to run a message, the application must have a handler for it.
// These handlers are registered on the application message service router.
// The result of the message execution is returned as a Any type.
// The result of the message execution is returned as an Any type.
// That any type can be unmarshaled to the expected response type.
// If the message execution fails, an error is returned.
func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) {
// set options
cfg := Config{}
cfg := &Config{}
for _, opt := range option {
opt(&cfg)
opt(cfg)
}

if cfg.AutomaticCommit {
Expand All @@ -104,11 +111,14 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) {

if cfg.AutomaticBeginEndBlock {
height := app.LastBlockHeight() + 1
app.logger.Info("Running beging block", "height", height)
app.BeginBlock(cmtabcitypes.RequestBeginBlock{Header: cmtproto.Header{Height: height, ChainID: appName}})
ctx := app.ctx.WithBlockHeight(height).WithChainID(appName)

app.logger.Info("Running BeginBlock", "height", height)
app.moduleManager.BeginBlock(ctx)

Check warning

Code scanning / gosec

Errors unhandled.

Errors unhandled.

defer func() {
app.logger.Info("Running end block", "height", height)
app.EndBlock(cmtabcitypes.RequestEndBlock{})
app.logger.Info("Running EndBlock", "height", height)
app.moduleManager.EndBlock(ctx)

Check warning

Code scanning / gosec

Errors unhandled.

Errors unhandled.
}()
}

Expand Down Expand Up @@ -137,8 +147,8 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) {
return response, nil
}

// Context returns the application context.
// It can be unwraped to a sdk.Context, with the sdk.UnwrapSDKContext function.
// Context returns the application context. It can be unwrapped to a sdk.Context,
// with the sdk.UnwrapSDKContext function.
func (app *App) Context() context.Context {
return app.ctx
}
Expand All @@ -153,9 +163,11 @@ func (app *App) QueryHelper() *baseapp.QueryServiceTestHelper {
func CreateMultiStore(keys map[string]*storetypes.KVStoreKey, logger log.Logger) storetypes.CommitMultiStore {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, logger, metrics.NewNoOpMetrics())

for key := range keys {
cms.MountStoreWithDB(keys[key], storetypes.StoreTypeIAVL, db)
}

_ = cms.LoadLatestVersion()
return cms
}
39 changes: 15 additions & 24 deletions testutil/testnet/cometstarter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
cmttypes "github.com/cometbft/cometbft/types"

servercmtlog "github.com/cosmos/cosmos-sdk/server/log"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)
Expand All @@ -27,21 +28,15 @@ import (
// As CometStart is more broadly used in the codebase,
// the number of available methods on CometStarter will grow.
type CometStarter struct {
logger log.Logger

app abcitypes.Application

cfg *cmtcfg.Config
valPrivKey cmted25519.PrivKey
genesis []byte

rootDir string

rpcListen bool

logger log.Logger
app abcitypes.Application
cfg *cmtcfg.Config
valPrivKey cmted25519.PrivKey
genesis []byte
rootDir string
rpcListen bool
tcpAddrChooser func() string

startTries int
startTries int
}

// NewCometStarter accepts a minimal set of arguments to start comet with an ABCI app.
Expand Down Expand Up @@ -92,16 +87,12 @@ func NewCometStarter(
// and bumping it up to 12 makes it almost never fail.
const defaultStartTries = 12
return &CometStarter{
logger: log.NewNopLogger(),

app: app,

logger: log.NewNopLogger(),
app: app,
cfg: cfg,
genesis: genesis,
valPrivKey: valPrivKey,

rootDir: rootDir,

rootDir: rootDir,
startTries: defaultStartTries,
}
}
Expand Down Expand Up @@ -132,8 +123,8 @@ func (s *CometStarter) Start() (n *node.Node, err error) {
return nil, err
}

// Wrap this defer in an anonymous function so we don't immediately evaluate n,
// which would always be nil at thi spoint.
// Wrap this defer in an anonymous function so we don't immediately evaluate
// n, which would always be nil at this point.
defer func() {
globalCometMu.Release(n)
}()
Expand Down Expand Up @@ -165,7 +156,7 @@ func (s *CometStarter) Start() (n *node.Node, err error) {
nodeKey,
proxy.NewLocalClientCreator(s.app),
appGenesisProvider,
node.DefaultDBProvider,
cmtcfg.DefaultDBProvider,
node.DefaultMetricsProvider(s.cfg.Instrumentation),
servercmtlog.CometLoggerWrapper{Logger: s.logger},
)
Expand Down
6 changes: 3 additions & 3 deletions testutil/testnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func NewNetwork(nVals int, createCometStarter func(int) *CometStarter) (Nodes, e
return
}

// Notify that the new node's switch is available,
// so this node can be peered with the other nodes.
switchCh <- n.PEXReactor().Switch
// Notify that the new node's switch is available, so this node can be
// peered with the other nodes.
switchCh <- n.Switch()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made me laugh, one api is private but a lower layer is still public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol smh


// And assign the node into its correct index in the ordered slice.
nodes[i] = n
Expand Down
19 changes: 15 additions & 4 deletions testutil/testnet/nodehelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ import (
// If totalWait has elapsed and the desired height has not been reached,
// an error is returned.
func WaitForNodeHeight(n *node.Node, desiredHeight int64, totalWait time.Duration) error {
const backoff = 100 * time.Millisecond
attempts := int64(totalWait / backoff)
const backOff = 100 * time.Millisecond
attempts := int64(totalWait / backOff)

// In Comet 0.37, the consensus state was exposed directly on the Node.
// As of 0.38, the node no longer exposes consensus state,
// but the consensus state is available as a field on the RPC environment.
//
// Luckily, in 0.38 the RPC environment is no longer a package-level singleton,
// so retrieving the RPC environment for a single node should be safe.
env, err := n.ConfigureRPC()
if err != nil {
return fmt.Errorf("failed to configure RPC to reach into consensus state: %w", err)
}

curHeight := int64(-1)
for i := int64(0); i < attempts; i++ {
curHeight = n.ConsensusState().GetLastHeight()
curHeight = env.ConsensusState.GetState().LastBlockHeight

if curHeight < desiredHeight {
time.Sleep(backoff)
time.Sleep(backOff)
continue
}

Expand Down