Skip to content

Commit

Permalink
test: make extendable (#112)
Browse files Browse the repository at this point in the history
* wip

* ok

* ok

* configurable

* async
  • Loading branch information
Alex Johnson authored Jun 11, 2024
1 parent a244093 commit daca8c8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 29 deletions.
9 changes: 8 additions & 1 deletion tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/strangelove-ventures/interchaintest/v8"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -103,12 +103,19 @@ var (
ModifyGenesis: cosmos.ModifyGenesis(genesisKV),
},
}

txCfg = e2e.TestTxConfig{
SmallSendsNum: 1,
LargeSendsNum: 400,
TargetIncreaseGasPrice: sdkmath.LegacyMustNewDecFromStr("0.1"),
}
)

func TestE2ETestSuite(t *testing.T) {
s := e2e.NewIntegrationSuite(
spec,
oracleImage,
txCfg,
)

suite.Run(t, s)
Expand Down
117 changes: 89 additions & 28 deletions tests/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e

import (
"context"
"fmt"
"math/rand"
"os"
"os/signal"
Expand Down Expand Up @@ -91,6 +92,28 @@ func GetOracleSideCar(node *cosmos.ChainNode) *cosmos.SidecarProcess {
return node.Sidecars[0]
}

type TestTxConfig struct {
SmallSendsNum int
LargeSendsNum int
TargetIncreaseGasPrice math.LegacyDec
}

func (tx *TestTxConfig) Validate() error {
if tx.SmallSendsNum < 1 || tx.LargeSendsNum < 1 {
return fmt.Errorf("sends num should be greater than 1")
}

if tx.TargetIncreaseGasPrice.IsNil() {
return fmt.Errorf("target increase gas price is nil")
}

if tx.TargetIncreaseGasPrice.LTE(math.LegacyZeroDec()) {
return fmt.Errorf("target increase gas price is less than or equal to 0")
}

return nil
}

// TestSuite runs the feemarket e2e test-suite against a given interchaintest specification
type TestSuite struct {
suite.Suite
Expand Down Expand Up @@ -131,6 +154,8 @@ type TestSuite struct {

// chain constructor
cc ChainConstructor

txConfig TestTxConfig
}

// Option is a function that modifies the TestSuite
Expand Down Expand Up @@ -178,7 +203,11 @@ func WithChainConstructor(cc ChainConstructor) Option {
}
}

func NewIntegrationSuite(spec *interchaintest.ChainSpec, oracleImage ibc.DockerImage, opts ...Option) *TestSuite {
func NewIntegrationSuite(spec *interchaintest.ChainSpec, oracleImage ibc.DockerImage, txCfg TestTxConfig, opts ...Option) *TestSuite {
if err := txCfg.Validate(); err != nil {
panic(err)
}

suite := &TestSuite{
spec: spec,
oracleConfig: DefaultOracleSidecar(oracleImage),
Expand All @@ -187,6 +216,7 @@ func NewIntegrationSuite(spec *interchaintest.ChainSpec, oracleImage ibc.DockerI
authority: authtypes.NewModuleAddress(govtypes.ModuleName),
icc: DefaultInterchainConstructor,
cc: DefaultChainConstructor,
txConfig: txCfg,
}

for _, opt := range opts {
Expand Down Expand Up @@ -336,13 +366,34 @@ func (s *TestSuite) TestSendTxDecrease() {
gas := int64(200000)
minBaseFee := sdk.NewDecCoinFromDec(defaultGasPrice.Denom, defaultGasPrice.Amount.Mul(math.LegacyNewDec(gas)))
minBaseFeeCoins := sdk.NewCoins(sdk.NewCoin(minBaseFee.Denom, minBaseFee.Amount.TruncateInt()))
sendAmt := int64(100000)
sendAmt := int64(100)

s.Run("expect fee market state to decrease", func() {
s.T().Log("performing sends...")
for {
// send with the exact expected defaultGasPrice
sig := make(chan struct{})
quit := make(chan struct{})
defer close(quit)

checkPrice := func(c, quit chan struct{}) {
select {
case <-time.After(500 * time.Millisecond):
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("gas price", gasPrice.String())

if gasPrice.Amount.Equal(params.MinBaseGasPrice) {
c <- struct{}{}
}
case <-quit:
return
}
}
go checkPrice(sig, quit)

select {
case <-sig:
break

case <-time.After(100 * time.Millisecond):
wg := sync.WaitGroup{}
wg.Add(3)

Expand All @@ -355,7 +406,7 @@ func (s *TestSuite) TestSendTxDecrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
1,
s.txConfig.SmallSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
Expand All @@ -371,7 +422,7 @@ func (s *TestSuite) TestSendTxDecrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
1,
s.txConfig.SmallSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
Expand All @@ -387,20 +438,14 @@ func (s *TestSuite) TestSendTxDecrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
1,
s.txConfig.SmallSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
s.Require().Equal(uint32(0), txResp.TxResult.Code, txResp.TxResult)
}()

wg.Wait()
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("base defaultGasPrice", gasPrice.String())

if gasPrice.Amount.Equal(params.MinBaseGasPrice) {
break
}
}

// wait for 5 blocks
Expand All @@ -426,17 +471,39 @@ func (s *TestSuite) TestSendTxIncrease() {
nodes := s.chain.Nodes()
s.Require().True(len(nodes) > 0)

baseGasPrice := s.QueryDefaultGasPrice()
gas := int64(20000100)
sendAmt := int64(200)

params := s.QueryParams()

gas := int64(params.MaxBlockUtilization)
sendAmt := int64(100)

s.Run("expect fee market gas price to increase", func() {
s.T().Log("performing sends...")
for {
sig := make(chan struct{})
quit := make(chan struct{})
defer close(quit)

checkPrice := func(c, quit chan struct{}) {
select {
case <-time.After(500 * time.Millisecond):
gasPrice := s.QueryDefaultGasPrice()
s.T().Log("gas price", gasPrice.String())

if gasPrice.Amount.GT(s.txConfig.TargetIncreaseGasPrice) {
c <- struct{}{}
}
case <-quit:
return
}
}
go checkPrice(sig, quit)

select {
case <-sig:
break

case <-time.After(100 * time.Millisecond):
// send with the exact expected baseGasPrice
baseGasPrice = s.QueryDefaultGasPrice()
baseGasPrice := s.QueryDefaultGasPrice()
minBaseFee := sdk.NewDecCoinFromDec(baseGasPrice.Denom, baseGasPrice.Amount.Mul(math.LegacyNewDec(gas)))
// add headroom
minBaseFeeCoins := sdk.NewCoins(sdk.NewCoin(minBaseFee.Denom, minBaseFee.Amount.Add(math.LegacyNewDec(10)).TruncateInt()))
Expand All @@ -453,7 +520,7 @@ func (s *TestSuite) TestSendTxIncrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
400,
s.txConfig.LargeSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
Expand All @@ -469,7 +536,7 @@ func (s *TestSuite) TestSendTxIncrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
400,
s.txConfig.LargeSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
Expand All @@ -485,20 +552,14 @@ func (s *TestSuite) TestSendTxIncrease() {
sdk.NewCoins(sdk.NewCoin(s.chain.Config().Denom, math.NewInt(sendAmt))),
minBaseFeeCoins,
gas,
400,
s.txConfig.LargeSendsNum,
)
s.Require().NoError(err, txResp)
s.Require().Equal(uint32(0), txResp.CheckTx.Code, txResp.CheckTx)
s.Require().Equal(uint32(0), txResp.TxResult.Code, txResp.TxResult)
}()

wg.Wait()
baseGasPrice = s.QueryDefaultGasPrice()
s.T().Log("gas price", baseGasPrice.String())

if baseGasPrice.Amount.GT(params.MinBaseGasPrice.Mul(math.LegacyNewDec(10))) {
break
}
}

// wait for 5 blocks
Expand Down

0 comments on commit daca8c8

Please sign in to comment.