/
suite.go
184 lines (149 loc) · 4.34 KB
/
suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package e2e
import (
"context"
"math/rand"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
const (
initBalance = 10000000000000
)
var r *rand.Rand
// initialize random generator with fixed seed for reproducibility
func init() {
s := rand.NewSource(1)
r = rand.New(s)
}
// TestSuite runs the feemarket e2e test-suite against a given interchaintest specification
type TestSuite struct {
suite.Suite
// spec
spec *interchaintest.ChainSpec
// chain
chain ibc.Chain
// interchain
ic *interchaintest.Interchain
// users
user1, user2, user3 ibc.Wallet
// denom
denom string
// overrides for key-ring configuration of the broadcaster
broadcasterOverrides *KeyringOverride
// bc is the RPC interface to the ITS network
bc *cosmos.Broadcaster
cdc codec.Codec
}
func NewE2ETestSuiteFromSpec(spec *interchaintest.ChainSpec) *TestSuite {
return &TestSuite{
spec: spec,
denom: "stake",
}
}
func (s *TestSuite) WithDenom(denom string) *TestSuite {
s.denom = denom
// update the bech32 prefixes
sdk.GetConfig().SetBech32PrefixForAccount(s.denom, s.denom+sdk.PrefixPublic)
sdk.GetConfig().SetBech32PrefixForValidator(s.denom+sdk.PrefixValidator, s.denom+sdk.PrefixValidator+sdk.PrefixPublic)
sdk.GetConfig().Seal()
return s
}
func (s *TestSuite) WithKeyringOptions(cdc codec.Codec, opts keyring.Option) {
s.broadcasterOverrides = &KeyringOverride{
cdc: cdc,
keyringOptions: opts,
}
}
func (s *TestSuite) SetupSuite() {
// build the chain
s.T().Log("building chain with spec", s.spec)
s.chain = ChainBuilderFromChainSpec(s.T(), s.spec)
// build the interchain
s.T().Log("building interchain")
ctx := context.Background()
s.ic = BuildInterchain(s.T(), ctx, s.chain)
cc, ok := s.chain.(*cosmos.CosmosChain)
if !ok {
panic("unable to assert ibc.Chain as CosmosChain")
}
// create the broadcaster
s.T().Log("creating broadcaster")
s.setupBroadcaster()
s.cdc = s.chain.Config().EncodingConfig.Codec
// get the users
s.user1 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)[0]
s.user2 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)[0]
s.user3 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)[0]
// create the broadcaster
s.T().Log("creating broadcaster")
s.setupBroadcaster()
}
func (s *TestSuite) TearDownSuite() {
// close the interchain
s.Require().NoError(s.ic.Close())
}
func (s *TestSuite) SetupSubTest() {
// wait for 1 block height
// query height
height, err := s.chain.(*cosmos.CosmosChain).Height(context.Background())
s.Require().NoError(err)
s.WaitForHeight(s.chain.(*cosmos.CosmosChain), height+1)
state := s.QueryState()
s.T().Log("state at block height", height+1, ":", state.String())
}
func (s *TestSuite) TestQueryParams() {
s.Run("query params", func() {
// query params
params := s.QueryParams()
// expect validate to pass
require.NoError(s.T(), params.ValidateBasic(), params)
})
}
func (s *TestSuite) TestQueryState() {
s.Run("query state", func() {
// query state
state := s.QueryState()
// expect validate to pass
require.NoError(s.T(), state.ValidateBasic(), state)
})
}
func (s *TestSuite) TestQueryBaseFee() {
s.Run("query base fee", func() {
// query base fee
fees := s.QueryBaseFee()
// expect validate to pass
require.NoError(s.T(), fees.Validate(), fees)
})
}
func (s *TestSuite) TestSendTxUpdating() {
ctx := context.Background()
// 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)
s.Run("expect fee market state to update", func() {
baseFee := s.QueryBaseFee()
gas := int64(1000000)
minBaseFee := baseFee.MulInt(sdk.NewInt(gas))
// send with the exact expected fee
txResp, err := s.SendCoins(
ctx,
cosmosChain,
s.user1.KeyName(),
s.user1.FormattedAddress(),
s.user2.FormattedAddress(),
sdk.NewCoins(sdk.NewCoin(cosmosChain.Config().Denom, sdk.NewInt(10000))),
minBaseFee,
gas,
)
s.Require().NoError(err, txResp)
s.T().Log(txResp)
})
}