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

test: e2e testing final #79

Merged
merged 31 commits into from
May 20, 2024
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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,8 @@ format: use-main
@find . -name '*.go' -type f -not -path "*.git*" -not -path "./client/docs/statik/statik.go" -not -name '*.pb.go' -not -name '*.pulsar.go' -not -name '*.gw.go' | xargs go run github.com/client9/misspell/cmd/misspell -w
@find . -name '*.go' -type f -not -path "*.git*" -not -path "./client/docs/statik/statik.go" -not -name '*.pb.go' -not -name '*.pulsar.go' -not -name '*.gw.go' | xargs go run golang.org/x/tools/cmd/goimports -w -local github.com/skip-mev/feemarket

.PHONY: format
mocks: use-main
@go generate ./...
make format

.PHONY: format mocks
43 changes: 24 additions & 19 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
)

var (
minBaseFee = sdkmath.LegacyNewDec(10)
baseFee = sdkmath.LegacyNewDec(1000000)

// config params
numValidators = 3
numFullNodes = 1
Expand All @@ -34,28 +37,30 @@ var (
genesisKV = []cosmos.GenesisKV{
{
Key: "app_state.feemarket.params",
Value: feemarkettypes.NewParams(
feemarkettypes.DefaultWindow,
feemarkettypes.DefaultAlpha,
feemarkettypes.DefaultBeta,
feemarkettypes.DefaultTheta,
feemarkettypes.DefaultDelta,
feemarkettypes.DefaultTargetBlockUtilization,
feemarkettypes.DefaultMaxBlockUtilization,
sdkmath.LegacyNewDec(1000),
feemarkettypes.DefaultMinLearningRate,
feemarkettypes.DefaultMaxLearningRate,
feemarkettypes.DefaultFeeDenom,
true,
),
Value: feemarkettypes.Params{
Alpha: feemarkettypes.DefaultAlpha,
Beta: feemarkettypes.DefaultBeta,
Theta: feemarkettypes.DefaultTheta,
Delta: feemarkettypes.DefaultDelta,
MinBaseFee: minBaseFee,
MinLearningRate: feemarkettypes.DefaultMinLearningRate,
MaxLearningRate: feemarkettypes.DefaultMaxLearningRate,
TargetBlockUtilization: feemarkettypes.DefaultTargetBlockUtilization / 4,
MaxBlockUtilization: feemarkettypes.DefaultMaxBlockUtilization,
Window: feemarkettypes.DefaultWindow,
FeeDenom: feemarkettypes.DefaultFeeDenom,
Enabled: true,
DistributeFees: false,
},
},
{
Key: "app_state.feemarket.state",
Value: feemarkettypes.NewState(
feemarkettypes.DefaultWindow,
sdkmath.LegacyNewDec(1000),
feemarkettypes.DefaultMaxLearningRate,
),
Value: feemarkettypes.State{
BaseFee: baseFee,
LearningRate: feemarkettypes.DefaultMaxLearningRate,
Window: make([]uint64, feemarkettypes.DefaultWindow),
Index: 0,
},
},
}

Expand Down
137 changes: 99 additions & 38 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"

"cosmossdk.io/math"

coretypes "github.com/cometbft/cometbft/rpc/core/types"
rpctypes "github.com/cometbft/cometbft/rpc/core/types"
comettypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -26,6 +26,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/skip-mev/chaintestutil/sample"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
Expand Down Expand Up @@ -113,63 +114,58 @@ func (s *TestSuite) SimulateTx(ctx context.Context, user cosmos.User, height uin
func (s *TestSuite) QueryParams() types.Params {
s.T().Helper()

// cast chain to cosmos-chain
cosmosChain, ok := s.chain.(*cosmos.CosmosChain)
s.Require().True(ok)
// get nodes
nodes := cosmosChain.Nodes()
s.Require().True(len(nodes) > 0)
// get grpc address
grpcAddr := s.chain.GetHostGRPCAddress()

// make params query to first node
resp, _, err := nodes[0].ExecQuery(context.Background(), "feemarket", "params")
// create the client
cc, err := grpc.Dial(grpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
s.Require().NoError(err)

// unmarshal params
var params types.Params
err = s.cdc.UnmarshalJSON(resp, &params)
// create the oracle client
c := types.NewQueryClient(cc)

resp, err := c.Params(context.Background(), &types.ParamsRequest{})
s.Require().NoError(err)
return params

return resp.Params
}

func (s *TestSuite) QueryState() types.State {
s.T().Helper()

// cast chain to cosmos-chain
cosmosChain, ok := s.chain.(*cosmos.CosmosChain)
s.Require().True(ok)
// get nodes
nodes := cosmosChain.Nodes()
s.Require().True(len(nodes) > 0)
// get grpc address
grpcAddr := s.chain.GetHostGRPCAddress()

// make params query to first node
resp, _, err := nodes[0].ExecQuery(context.Background(), "feemarket", "state")
// create the client
cc, err := grpc.Dial(grpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
s.Require().NoError(err)

// unmarshal state
var state types.State
err = s.cdc.UnmarshalJSON(resp, &state)
// create the oracle client
c := types.NewQueryClient(cc)

resp, err := c.State(context.Background(), &types.StateRequest{})
s.Require().NoError(err)
return state

return resp.State
}

func (s *TestSuite) QueryBaseFee() sdk.Coins {
func (s *TestSuite) QueryBaseFee() sdk.DecCoins {
s.T().Helper()

// cast chain to cosmos-chain
cosmosChain, ok := s.chain.(*cosmos.CosmosChain)
s.Require().True(ok)
// get nodes
nodes := cosmosChain.Nodes()
s.Require().True(len(nodes) > 0)
// get grpc address
grpcAddr := s.chain.GetHostGRPCAddress()

// make params query to first node
resp, _, err := nodes[0].ExecQuery(context.Background(), "feemarket", "base-fee")
// create the client
cc, err := grpc.Dial(grpcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
s.Require().NoError(err)

// unmarshal state
fees, err := sdk.ParseCoinsNormalized(string(resp))
// create the oracle client
c := types.NewQueryClient(cc)

resp, err := c.BaseFee(context.Background(), &types.BaseFeeRequest{})
s.Require().NoError(err)
return fees

return resp.Fees
}

// QueryValidators queries for all the network's validators
Expand Down Expand Up @@ -373,16 +369,40 @@ func (s *TestSuite) keyringDirFromNode() string {
return localDir
}

func (s *TestSuite) SendCoinsMultiBroadcast(ctx context.Context, sender, receiver ibc.Wallet, amt, fees sdk.Coins, gas int64, numMsg int) (*coretypes.ResultBroadcastTxCommit, error) {
cc, ok := s.chain.(*cosmos.CosmosChain)
if !ok {
panic("unable to assert ibc.Chain as CosmosChain")
}

msgs := make([]sdk.Msg, numMsg)
for i := 0; i < numMsg; i++ {
msgs[i] = &banktypes.MsgSend{
FromAddress: sender.FormattedAddress(),
ToAddress: receiver.FormattedAddress(),
Amount: amt,
}
}

tx := s.CreateTx(cc, sender, fees.String(), gas, msgs...)

// get an rpc endpoint for the chain
c := cc.Nodes()[0].Client
return c.BroadcastTxCommit(ctx, tx)
}

// SendCoins creates a executes a SendCoins message and broadcasts the transaction.
func (s *TestSuite) SendCoins(ctx context.Context, keyName, sender, receiver string, amt, fees sdk.Coins, gas int64) (string, error) {
cc, ok := s.chain.(*cosmos.CosmosChain)
if !ok {
panic("unable to assert ibc.Chain as CosmosChain")
}

resp, err := s.ExecTx(
ctx,
cc,
keyName,
false,
"bank",
"send",
sender,
Expand Down Expand Up @@ -441,12 +461,16 @@ func (s *TestSuite) GetAndFundTestUsers(
}

// ExecTx executes a cli command on a node, waits a block and queries the Tx to verify it was included on chain.
func (s *TestSuite) ExecTx(ctx context.Context, chain *cosmos.CosmosChain, keyName string, command ...string) (string, error) {
func (s *TestSuite) ExecTx(ctx context.Context, chain *cosmos.CosmosChain, keyName string, blocking bool, command ...string) (string, error) {
node := chain.Validators[0]

resp, err := node.ExecTx(ctx, keyName, command...)
s.Require().NoError(err)

if !blocking {
return resp, nil
}

height, err := chain.Height(context.Background())
s.Require().NoError(err)
s.WaitForHeight(chain, height+1)
Expand All @@ -457,3 +481,40 @@ func (s *TestSuite) ExecTx(ctx context.Context, chain *cosmos.CosmosChain, keyNa

return string(stdout), nil
}

// CreateTx creates a new transaction to be signed by the given user, including a provided set of messages
func (s *TestSuite) CreateTx(chain *cosmos.CosmosChain, user cosmos.User, fee string, gas int64, msgs ...sdk.Msg) []byte {
bc := cosmos.NewBroadcaster(s.T(), chain)

ctx := context.Background()
// create tx factory + Client Context
txf, err := bc.GetFactory(ctx, user)
s.Require().NoError(err)

cc, err := bc.GetClientContext(ctx, user)
s.Require().NoError(err)

txf = txf.WithSimulateAndExecute(true)

txf, err = txf.Prepare(cc)
s.Require().NoError(err)

// get gas for tx
txf = txf.WithGas(uint64(gas))
txf = txf.WithGasAdjustment(0)
txf = txf.WithGasPrices("")
txf = txf.WithFees(fee)

// update sequence number
txf = txf.WithSequence(txf.Sequence())

// sign the tx
txBuilder, err := txf.BuildUnsignedTx(msgs...)
s.Require().NoError(err)
s.Require().NoError(tx.Sign(cc.CmdContext, txf, cc.GetFromName(), txBuilder, true))

// encode and return
bz, err := cc.TxConfig.TxEncoder()(txBuilder.GetTx())
s.Require().NoError(err)
return bz
}
Loading
Loading