Skip to content

Commit

Permalink
feat: add tests and conform to new interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Zygimantass committed Jan 10, 2025
1 parent 0e0af2a commit ad31c59
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 2,323 deletions.
28 changes: 3 additions & 25 deletions core/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"math/big"

rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
"google.golang.org/grpc"

"github.com/skip-mev/petri/core/v2/provider"
Expand All @@ -25,8 +22,6 @@ type ChainI interface {
GetConfig() ChainConfig
GetGRPCClient(context.Context) (*grpc.ClientConn, error)
GetTMClient(context.Context) (*rpchttp.HTTP, error)
GetTxConfig() client.TxConfig
GetInterfaceRegistry() codectypes.InterfaceRegistry

GetValidators() []NodeI
GetFaucetWallet() WalletI
Expand All @@ -49,20 +44,13 @@ type ChainConfig struct {

BinaryName string // BinaryName is the name of the chain binary in the Docker image

Image provider.ImageDefinition // Image is the Docker ImageDefinition of the chain
SidecarImage provider.ImageDefinition // SidecarImage is the Docker ImageDefinition of the chain sidecar
Image provider.ImageDefinition // Image is the Docker ImageDefinition of the chain

GasPrices string // GasPrices are the minimum gas prices to set on the chain
GasAdjustment float64 // GasAdjustment is the margin by which to multiply the default gas prices
GasPrices string // GasPrices are the minimum gas prices to set on the chain

Bech32Prefix string // Bech32Prefix is the Bech32 prefix of the on-chain addresses

EncodingConfig testutil.TestEncodingConfig // EncodingConfig is the encoding config of the chain

HomeDir string // HomeDir is the home directory of the chain
SidecarHomeDir string // SidecarHomeDir is the home directory of the chain sidecar
SidecarPorts []string // SidecarPorts are the ports to expose on the chain sidecar
SidecarArgs []string // SidecarArgs are the arguments to launch the chain sidecar
HomeDir string // HomeDir is the home directory of the chain

CoinType string // CoinType is the coin type of the chain (e.g. 118)
ChainId string // ChainId is the chain ID of the chain
Expand Down Expand Up @@ -117,20 +105,10 @@ func (c *ChainConfig) ValidateBasic() error {
return fmt.Errorf("gas prices cannot be empty")
}

if c.GasAdjustment == 0 {
return fmt.Errorf("gas adjustment cannot be 0")
}

if err := c.Image.ValidateBasic(); err != nil {
return fmt.Errorf("image definition is invalid: %w", err)
}

if c.SidecarImage.Image != "" {
if err := c.SidecarImage.ValidateBasic(); err != nil {
return fmt.Errorf("sidecar image definition is invalid: %w", err)
}
}

if c.Bech32Prefix == "" {
return fmt.Errorf("bech32 prefix cannot be empty")
}
Expand Down
6 changes: 3 additions & 3 deletions core/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ type NodeConfig struct {

IsValidator bool // IsValidator denotes whether this node is a validator

Chain ChainI // Chain is the chain this node is running on
ChainConfig ChainConfig // ChainConfig is the config of the chain this node is running on
}

func (c NodeConfig) ValidateBasic() error {
if c.Name == "" {
return fmt.Errorf("name cannot be empty")
}

if c.Chain == nil {
return fmt.Errorf("chain cannot be nil")
if err := c.ChainConfig.ValidateBasic(); err != nil {
return err
}

return nil
Expand Down
41 changes: 3 additions & 38 deletions cosmos/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
sdkmath "cosmossdk.io/math"

rpchttp "github.com/cometbft/cometbft/rpc/client/http"
sdkclient "github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -72,7 +70,7 @@ func CreateChain(ctx context.Context, logger *zap.Logger, infraProvider provider
Index: i,
Name: validatorName,
IsValidator: true,
Chain: &chain,
ChainConfig: config,
})
if err != nil {
return err
Expand Down Expand Up @@ -100,7 +98,7 @@ func CreateChain(ctx context.Context, logger *zap.Logger, infraProvider provider
Index: i,
Name: nodeName,
IsValidator: true,
Chain: &chain,
ChainConfig: config,
})
if err != nil {
return err
Expand Down Expand Up @@ -424,30 +422,7 @@ func (c *Chain) WaitForBlocks(ctx context.Context, delta uint64) error {
return err
}

cur := start

for {
c.logger.Debug("waiting for blocks", zap.Uint64("desired_delta", delta), zap.Uint64("current_delta", cur-start))

if cur-start >= delta {
break
}

cur, err = c.Height(ctx)
if err != nil {
time.Sleep(2 * time.Second)
continue
}
// We assume the chain will eventually return a non-zero height, otherwise
// this may block indefinitely.
if cur == 0 {
time.Sleep(2 * time.Second)
continue
}

time.Sleep(2 * time.Second)
}
return nil
return c.WaitForHeight(ctx, start+delta)
}

// WaitForHeight blocks until the chain reaches block height desiredHeight
Expand Down Expand Up @@ -500,13 +475,3 @@ func (c *Chain) GetValidatorWallets() []petritypes.WalletI {
func (c *Chain) GetFaucetWallet() petritypes.WalletI {
return c.FaucetWallet
}

// GetTxConfig returns a Cosmos SDK TxConfig
func (c *Chain) GetTxConfig() sdkclient.TxConfig {
return c.Config.EncodingConfig.TxConfig
}

// GetInterfaceRegistry returns a Cosmos SDK InterfaceRegistry
func (c *Chain) GetInterfaceRegistry() codectypes.InterfaceRegistry {
return c.Config.EncodingConfig.InterfaceRegistry
}
70 changes: 70 additions & 0 deletions cosmos/chain/chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package chain_test

import (
"context"
"github.com/cosmos/cosmos-sdk/crypto/hd"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/skip-mev/petri/core/v2/provider"
"github.com/skip-mev/petri/core/v2/provider/docker"
"github.com/skip-mev/petri/core/v2/types"
"github.com/skip-mev/petri/cosmos/v2/chain"
"github.com/skip-mev/petri/cosmos/v2/node"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"testing"
"time"
)

const idAlphabet = "abcdefghijklqmnoqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

var defaultChainConfig = types.ChainConfig{
Denom: "stake",
Decimals: 6,
NumValidators: 1,
NumNodes: 0,
BinaryName: "/simd/simd",
Image: provider.ImageDefinition{
Image: "cosmossdk/simd:latest",
UID: "1000",
GID: "1000",
},
GasPrices: "0.0005stake",
Bech32Prefix: "cosmos",
HomeDir: "/gaia",
CoinType: "118",
ChainId: "stake-1",
WalletConfig: types.WalletConfig{
SigningAlgorithm: string(hd.Secp256k1.Name()),
Bech32Prefix: "cosmos",
HDPath: hd.CreateHDPath(118, 0, 0),
DerivationFn: hd.Secp256k1.Derive(),
GenerationFn: hd.Secp256k1.Generate(),
},
UseGenesisSubCommand: true,
NodeCreator: node.CreateNode,
}

func TestChainLifecycle(t *testing.T) {
ctx := context.Background()
logger, _ := zap.NewDevelopment()
providerName := gonanoid.MustGenerate(idAlphabet, 10)

p, err := docker.CreateProvider(ctx, logger, providerName)
require.NoError(t, err)
defer func(p provider.ProviderI, ctx context.Context) {
require.NoError(t, p.Teardown(ctx))
}(p, ctx)

c, err := chain.CreateChain(ctx, logger, p, defaultChainConfig)
require.NoError(t, err)

require.NoError(t, c.Init(ctx))
require.Len(t, c.GetValidators(), 1)
require.Len(t, c.GetNodes(), 0)

time.Sleep(1 * time.Second)

require.NoError(t, c.WaitForBlocks(ctx, 5))

require.NoError(t, c.Teardown(ctx))
}
32 changes: 0 additions & 32 deletions cosmos/cosmosutil/auth.go

This file was deleted.

Loading

0 comments on commit ad31c59

Please sign in to comment.