-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathtestconfig.go
267 lines (231 loc) · 7.89 KB
/
testconfig.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package testconfig
import (
"encoding/json"
"fmt"
"os"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/strangelove-ventures/ibctest/v6/ibc"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/ibc-go/e2e/semverutil"
"github.com/cosmos/ibc-go/e2e/testvalues"
)
const (
// ChainImageEnv specifies the image that the chains will use. If left unspecified, it will
// default to being determined based on the specified binary. E.g. ghcr.io/cosmos/ibc-go-simd
ChainImageEnv = "CHAIN_IMAGE"
// ChainATagEnv specifies the tag that Chain A will use.
ChainATagEnv = "CHAIN_A_TAG"
// ChainBTagEnv specifies the tag that Chain B will use. If unspecified
// the value will default to the same value as Chain A.
ChainBTagEnv = "CHAIN_B_TAG"
// GoRelayerTagEnv specifies the go relayer version. Defaults to "main"
GoRelayerTagEnv = "RLY_TAG"
// ChainBinaryEnv binary is the binary that will be used for both chains.
ChainBinaryEnv = "CHAIN_BINARY"
// ChainUpgradeTagEnv specifies the upgrade version tag
ChainUpgradeTagEnv = "CHAIN_UPGRADE_TAG"
// defaultBinary is the default binary that will be used by the chains.
defaultBinary = "simd"
// defaultRlyTag is the tag that will be used if no relayer tag is specified.
// all images are here https://github.com/cosmos/relayer/pkgs/container/relayer/versions
defaultRlyTag = "v2.2.0-rc2"
// defaultChainTag is the tag that will be used for the chains if none is specified.
defaultChainTag = "main"
)
func getChainImage(binary string) string {
if binary == "" {
binary = defaultBinary
}
return fmt.Sprintf("ghcr.io/cosmos/ibc-go-%s", binary)
}
// TestConfig holds various fields used in the E2E tests.
type TestConfig struct {
ChainAConfig ChainConfig
ChainBConfig ChainConfig
RlyTag string
UpgradeTag string
}
type ChainConfig struct {
Image string
Tag string
Binary string
}
// FromEnv returns a TestConfig constructed from environment variables.
func FromEnv() TestConfig {
chainBinary, ok := os.LookupEnv(ChainBinaryEnv)
if !ok {
chainBinary = defaultBinary
}
chainATag, ok := os.LookupEnv(ChainATagEnv)
if !ok {
chainATag = defaultChainTag
}
chainBTag, ok := os.LookupEnv(ChainBTagEnv)
if !ok {
chainBTag = chainATag
}
rlyTag, ok := os.LookupEnv(GoRelayerTagEnv)
if !ok {
rlyTag = defaultRlyTag
}
// TODO: remove hard coded value
rlyTag = "andrew-tendermint_v0.37"
chainAImage := getChainImage(chainBinary)
specifiedChainImage, ok := os.LookupEnv(ChainImageEnv)
if ok {
chainAImage = specifiedChainImage
}
chainBImage := chainAImage
upgradeTag, ok := os.LookupEnv(ChainUpgradeTagEnv)
if !ok {
upgradeTag = ""
}
return TestConfig{
ChainAConfig: ChainConfig{
Image: chainAImage,
Tag: chainATag,
Binary: chainBinary,
},
ChainBConfig: ChainConfig{
Image: chainBImage,
Tag: chainBTag,
Binary: chainBinary,
},
RlyTag: rlyTag,
UpgradeTag: upgradeTag,
}
}
func GetChainATag() string {
chainATag, ok := os.LookupEnv(ChainATagEnv)
if !ok {
panic(fmt.Sprintf("no environment variable specified for %s", ChainATagEnv))
}
return chainATag
}
func GetChainBTag() string {
chainBTag, ok := os.LookupEnv(ChainBTagEnv)
if !ok {
return GetChainATag()
}
return chainBTag
}
// ChainOptions stores chain configurations for the chains that will be
// created for the tests. They can be modified by passing ChainOptionConfiguration
// to E2ETestSuite.GetChains.
type ChainOptions struct {
ChainAConfig *ibc.ChainConfig
ChainBConfig *ibc.ChainConfig
}
// ChainOptionConfiguration enables arbitrary configuration of ChainOptions.
type ChainOptionConfiguration func(options *ChainOptions)
// DefaultChainOptions returns the default configuration for the chains.
// These options can be configured by passing configuration functions to E2ETestSuite.GetChains.
func DefaultChainOptions() ChainOptions {
tc := FromEnv()
chainACfg := newDefaultSimappConfig(tc.ChainAConfig, "simapp-a", "chain-a", "atoma")
chainBCfg := newDefaultSimappConfig(tc.ChainBConfig, "simapp-b", "chain-b", "atomb")
return ChainOptions{
ChainAConfig: &chainACfg,
ChainBConfig: &chainBCfg,
}
}
// newDefaultSimappConfig creates an ibc configuration for simd.
func newDefaultSimappConfig(cc ChainConfig, name, chainID, denom string) ibc.ChainConfig {
return ibc.ChainConfig{
Type: "cosmos",
Name: name,
ChainID: chainID,
Images: []ibc.DockerImage{
{
Repository: cc.Image,
Version: cc.Tag,
},
},
Bin: cc.Binary,
Bech32Prefix: "cosmos",
CoinType: fmt.Sprint(sdk.GetConfig().GetCoinType()),
Denom: denom,
GasPrices: fmt.Sprintf("0.00%s", denom),
GasAdjustment: 1.3,
TrustingPeriod: "508h",
NoHostMount: false,
ModifyGenesis: defaultModifyGenesis(),
}
}
// govGenesisFeatureReleases represents the releases the governance module genesis
// was upgraded from v1beta1 to v1.
var govGenesisFeatureReleases = semverutil.FeatureReleases{
MajorVersion: "v7",
}
// defaultModifyGenesis will only modify governance params to ensure the voting period and minimum deposit
// are functional for e2e testing purposes.
func defaultModifyGenesis() func(ibc.ChainConfig, []byte) ([]byte, error) {
return func(chainConfig ibc.ChainConfig, genbz []byte) ([]byte, error) {
genDoc, err := tmtypes.GenesisDocFromJSON(genbz)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis bytes into genesis doc: %w", err)
}
var appState genutiltypes.AppMap
if err := json.Unmarshal(genDoc.AppState, &appState); err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis bytes into app state: %w", err)
}
govGenBz, err := modifyGovAppState(chainConfig, appState[govtypes.ModuleName])
if err != nil {
return nil, err
}
appState[govtypes.ModuleName] = govGenBz
genDoc.AppState, err = json.Marshal(appState)
if err != nil {
return nil, err
}
bz, err := tmjson.MarshalIndent(genDoc, "", " ")
if err != nil {
return nil, err
}
return bz, nil
}
}
// modifyGovAppState takes the existing gov app state and marshals it to either a govv1 GenesisState
// or a govv1beta1 GenesisState depending on the simapp version.
func modifyGovAppState(chainConfig ibc.ChainConfig, govAppState json.RawMessage) ([]byte, error) {
cfg := testutil.MakeTestEncodingConfig()
cdc := codec.NewProtoCodec(cfg.InterfaceRegistry)
govv1.RegisterInterfaces(cfg.InterfaceRegistry)
govv1beta1.RegisterInterfaces(cfg.InterfaceRegistry)
shouldUseGovV1 := govGenesisFeatureReleases.IsSupported(chainConfig.Images[0].Version)
var govGenesisState gogoproto.Message
if shouldUseGovV1 {
govGenesisState = &govv1.GenesisState{}
} else {
govGenesisState = &govv1beta1.GenesisState{}
}
if err := cdc.UnmarshalJSON(govAppState, govGenesisState); err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis bytes into gov genesis state: %w", err)
}
switch v := govGenesisState.(type) {
case *govv1.GenesisState:
if v.Params == nil {
v.Params = &govv1.Params{}
}
// set correct minimum deposit using configured denom
v.Params.MinDeposit = sdk.NewCoins(sdk.NewCoin(chainConfig.Denom, govv1beta1.DefaultMinDepositTokens))
vp := testvalues.VotingPeriod
v.Params.VotingPeriod = &vp
case *govv1beta1.GenesisState:
v.DepositParams.MinDeposit = sdk.NewCoins(sdk.NewCoin(chainConfig.Denom, govv1beta1.DefaultMinDepositTokens))
v.VotingParams.VotingPeriod = testvalues.VotingPeriod
}
govGenBz, err := cdc.MarshalJSON(govGenesisState)
if err != nil {
return nil, fmt.Errorf("failed to marshal gov genesis state: %w", err)
}
return govGenBz, nil
}