Skip to content

Commit

Permalink
chore: switch from cosmos-sdk/network to testnode package (#3118)
Browse files Browse the repository at this point in the history
Closes: #829

This refactors the three tests we have that use the CLI commands:
`mint`, `blob` and `blobstream` to use the `testnode` package instead of
`cosmos-sdk`'s `network` package.

The motivating reason for this is that cosmos-sdk's network package
doesn't support initialising the `ConsensusParams` in the genesis file
which we need to set the app version correctly

---------

Co-authored-by: Rootul P <[email protected]>
  • Loading branch information
cmwaters and rootulp authored Feb 22, 2024
1 parent 9fe864c commit 62f40dc
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 278 deletions.
2 changes: 1 addition & 1 deletion test/testground/network/consensus_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (cn *ConsensusNode) StartNode(ctx context.Context, baseDir string) error {
}

cn.cmtNode = tmNode
cctx := testnode.NewContext(ctx, cn.kr, ucfg.TmConfig, cn.params.ChainID)
cctx := testnode.NewContext(ctx, cn.kr, ucfg.TmConfig, cn.params.ChainID, ucfg.AppConfig.API.Address)

cctx, stopNode, err := testnode.StartNode(tmNode, cctx)
cn.stopFuncs = append(cn.stopFuncs, stopNode)
Expand Down
8 changes: 6 additions & 2 deletions test/util/genesis/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/tendermint/tendermint/crypto"
)

const (
DefaultInitialBalance = 1e15 // 1 billion TIA
)

type Account struct {
Name string
InitialTokens int64
Expand Down Expand Up @@ -55,9 +59,9 @@ func NewDefaultValidator(name string) Validator {
return Validator{
Account: Account{
Name: name,
InitialTokens: 999_999_999_999_999_999,
InitialTokens: DefaultInitialBalance,
},
Stake: 99_999_999_999_999_999, // save some tokens for fees
Stake: DefaultInitialBalance / 2, // save some tokens for fees
ConsensusKey: GenerateEd25519(NewSeed(r)),
NetworkKey: GenerateEd25519(NewSeed(r)),
}
Expand Down
2 changes: 1 addition & 1 deletion test/util/genesis/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func accountsToSDKTypes(addrs []string, pubkeys []cryptotypes.PubKey) ([]banktyp
pubKey := pubkeys[i]

balances := sdk.NewCoins(
sdk.NewCoin(appconsts.BondDenom, sdk.NewInt(999_999_999_999_999_999)),
sdk.NewCoin(appconsts.BondDenom, sdk.NewInt(DefaultInitialBalance)),
)

genBals[i] = banktypes.Balance{Address: addr, Coins: balances.Sort()}
Expand Down
154 changes: 0 additions & 154 deletions test/util/network/network.go

This file was deleted.

5 changes: 4 additions & 1 deletion test/util/testnode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
kibibyte = 1024 // bytes
mebibyte = 1_048_576 // bytes
DefaultValidatorAccountName = "validator"
DefaultInitialBalance = genesis.DefaultInitialBalance
)

type UniversalTestingConfig struct {
Expand Down Expand Up @@ -83,7 +84,7 @@ func (c *Config) WithTimeoutCommit(d time.Duration) *Config {
// WithFundedAccounts sets the genesis accounts and returns the Config.
func (c *Config) WithFundedAccounts(accounts ...string) *Config {
c.Genesis = c.Genesis.WithAccounts(
genesis.NewAccounts(999999999999999999, accounts...)...,
genesis.NewAccounts(DefaultInitialBalance, accounts...)...,
)
return c
}
Expand Down Expand Up @@ -153,5 +154,7 @@ func DefaultTendermintConfig() *tmconfig.Config {
// chosen only as an arbitrary large number).
tmCfg.RPC.MaxBodyBytes = 200 * mebibyte

tmCfg.RPC.TimeoutBroadcastTxCommit = time.Minute

return tmCfg
}
22 changes: 18 additions & 4 deletions test/util/testnode/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr stri
tmNode, app, err := NewCometNode(baseDir, &cfg.UniversalTestingConfig)
require.NoError(t, err)

cctx = NewContext(context.Background(), cfg.Genesis.Keyring(), tmCfg, cfg.Genesis.ChainID)

cctx, stopNode, err := StartNode(tmNode, cctx)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})

appCfg := cfg.AppConfig
appCfg.GRPC.Address = fmt.Sprintf("127.0.0.1:%d", mustGetFreePort())
appCfg.API.Address = fmt.Sprintf("tcp://127.0.0.1:%d", mustGetFreePort())

cctx = NewContext(ctx, cfg.Genesis.Keyring(), tmCfg, cfg.Genesis.ChainID, appCfg.API.Address)

cctx, stopNode, err := StartNode(tmNode, cctx)
require.NoError(t, err)

cctx, cleanupGRPC, err := StartGRPCServer(app, appCfg, cctx)
require.NoError(t, err)

apiServer, err := StartAPIServer(app, *appCfg, cctx)
require.NoError(t, err)

t.Cleanup(func() {
t.Log("tearing down testnode")
err := stopNode()
Expand All @@ -58,6 +66,12 @@ func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr stri
// failing the test.
t.Logf("error when cleaning up GRPC %v", err)
}
err = apiServer.Close()
if err != nil {
// the test has already completed so just log the error instead of
// failing the test.
t.Logf("error when closing API server %v", err)
}
})

return cctx, tmCfg.RPC.ListenAddress, appCfg.GRPC.Address
Expand Down
21 changes: 16 additions & 5 deletions test/util/testnode/node_interaction_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const (
type Context struct {
rootCtx context.Context
client.Context
apiAddress string
}

func NewContext(goCtx context.Context, kr keyring.Keyring, tmCfg *tmconfig.Config, chainID string) Context {
func NewContext(goCtx context.Context, kr keyring.Keyring, tmCfg *tmconfig.Config, chainID, apiAddress string) Context {
ecfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
cctx := client.Context{}.
WithKeyring(kr).
Expand All @@ -48,7 +49,7 @@ func NewContext(goCtx context.Context, kr keyring.Keyring, tmCfg *tmconfig.Confi
WithTxConfig(ecfg.TxConfig).
WithAccountRetriever(authtypes.AccountRetriever{})

return Context{rootCtx: goCtx, Context: cctx}
return Context{rootCtx: goCtx, Context: cctx, apiAddress: apiAddress}
}

func (c *Context) GoContext() context.Context {
Expand Down Expand Up @@ -96,13 +97,19 @@ func (c *Context) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, err
ctx, cancel := context.WithTimeout(c.rootCtx, t)
defer cancel()

var latestHeight int64
var (
latestHeight int64
err error
)
for {
select {
case <-ctx.Done():
return latestHeight, fmt.Errorf("timeout (%v) exceeded waiting for network to reach height", t)
if c.rootCtx.Err() != nil {
return latestHeight, c.rootCtx.Err()
}
return latestHeight, fmt.Errorf("timeout (%v) exceeded waiting for network to reach height %d. Got to height %d", t, h, latestHeight)
case <-ticker.C:
latestHeight, err := c.LatestHeight()
latestHeight, err = c.LatestHeight()
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -313,3 +320,7 @@ func (c *Context) HeightForTimestamp(timestamp time.Time) (int64, error) {
}
return 0, fmt.Errorf("could not find block with timestamp after %v", timestamp)
}

func (c *Context) APIAddress() string {
return c.apiAddress
}
35 changes: 34 additions & 1 deletion test/util/testnode/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"os"
"path"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/api"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
srvgrpc "github.com/cosmos/cosmos-sdk/server/grpc"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/rpc/client/local"
"google.golang.org/grpc"
Expand Down Expand Up @@ -56,13 +60,23 @@ func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Co
// Add the tendermint queries service in the gRPC router.
app.RegisterTendermintService(cctx.Context)

if a, ok := app.(srvtypes.ApplicationQueryService); ok {
a.RegisterNodeService(cctx.Context)
}

grpcSrv, err := srvgrpc.StartGRPCServer(cctx.Context, app, appCfg.GRPC)
if err != nil {
return Context{}, emptycleanup, err
}

nodeGRPCAddr := strings.Replace(appCfg.GRPC.Address, "0.0.0.0", "localhost", 1)
conn, err := grpc.Dial(nodeGRPCAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.Dial(
nodeGRPCAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.ForceCodec(codec.NewProtoCodec(cctx.InterfaceRegistry).GRPCCodec()),
),
)
if err != nil {
return Context{}, emptycleanup, err
}
Expand Down Expand Up @@ -99,3 +113,22 @@ func removeDir(rootDir string) error {
}
return os.RemoveAll(rootDir)
}

func StartAPIServer(app srvtypes.Application, appCfg srvconfig.Config, cctx Context) (*api.Server, error) {
apiSrv := api.New(cctx.Context, log.NewNopLogger())
app.RegisterAPIRoutes(apiSrv, appCfg.API)
errCh := make(chan error)
go func() {
if err := apiSrv.Start(appCfg); err != nil {
errCh <- err
}
}()

select {
case err := <-errCh:
return nil, err

case <-time.After(srvtypes.ServerStartTime): // assume server started successfully
}
return apiSrv, nil
}
2 changes: 1 addition & 1 deletion test/util/testnode/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func FundKeyringAccounts(accounts ...string) (keyring.Keyring, []banktypes.Balan

for i, addr := range addresses {
balances := sdk.NewCoins(
sdk.NewCoin(appconsts.BondDenom, sdk.NewInt(99999999999999999)),
sdk.NewCoin(appconsts.BondDenom, sdk.NewInt(DefaultInitialBalance)),
)

genBalances[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()}
Expand Down
Loading

0 comments on commit 62f40dc

Please sign in to comment.