-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinit.go
263 lines (216 loc) · 9.17 KB
/
init.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
package integrationtests
import (
"context"
"flag"
"fmt"
"sync"
"testing"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
protobufgrpc "github.com/gogo/protobuf/grpc"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/CoreumFoundation/coreum/app"
"github.com/CoreumFoundation/coreum/pkg/client"
"github.com/CoreumFoundation/coreum/pkg/config"
"github.com/CoreumFoundation/coreum/pkg/config/constant"
feemodeltypes "github.com/CoreumFoundation/coreum/x/feemodel/types"
)
// stringsFlag allows setting a value multiple times to collect a list, as in -I=val1 -I=val2.
type stringsFlag []string
func (m *stringsFlag) String() string {
if len(*m) == 0 {
return ""
}
return fmt.Sprint(*m)
}
func (m *stringsFlag) Set(val string) error {
*m = append(*m, val)
return nil
}
// Chains defines the all chains used for the tests.
type Chains struct {
Coreum CoreumChain
Gaia Chain
Osmosis Chain
}
var (
gaiaChainSyncOnce sync.Once
osmosisChainSyncOnce sync.Once
)
var (
ctx context.Context
coreumChain CoreumChain
gaiaChain Chain
osmosisChain Chain
runUnsafe bool
)
var (
coreumGRPCAddress string
coreumRPCAddress string
coreumFundingMnemonic string
coreumStakerMnemonics stringsFlag
GaiaGRPCAddress string
GaiaRPCAddress string
GaiaFundingMnemonic string
OsmosisGRPCAddress string
OsmosisRPCAddress string
OsmosisFundingMnemonic string
)
func init() { //nolint:funlen // will be shortened after the crust merge
flag.BoolVar(&runUnsafe, "run-unsafe", false, "run unsafe tests for example ones related to governance")
flag.StringVar(&coreumGRPCAddress, "coreum-grpc-address", "localhost:9090", "GRPC address of cored node started by znet")
flag.StringVar(&coreumRPCAddress, "coreum-rpc-address", "http://localhost:26657", "RPC address of cored node started by znet")
flag.StringVar(&coreumFundingMnemonic, "coreum-funding-mnemonic", "sad hobby filter tray ordinary gap half web cat hard call mystery describe member round trend friend beyond such clap frozen segment fan mistake", "Funding account mnemonic required by tests")
flag.Var(&coreumStakerMnemonics, "coreum-staker-mnemonic", "Staker account mnemonics required by tests, supports multiple")
flag.StringVar(&GaiaGRPCAddress, "gaia-grpc-address", "localhost:9080", "GRPC address of gaia node started by znet")
flag.StringVar(&GaiaRPCAddress, "gaia-rpc-address", "http://localhost:26557", "RPC address of gaia node started by znet")
flag.StringVar(&GaiaFundingMnemonic, "gaia-funding-mnemonic", "sad hobby filter tray ordinary gap half web cat hard call mystery describe member round trend friend beyond such clap frozen segment fan mistake", "Funding account mnemonic required by tests")
flag.StringVar(&OsmosisGRPCAddress, "osmosis-grpc-address", "localhost:9070", "GRPC address of osmosis node started by znet")
flag.StringVar(&OsmosisRPCAddress, "osmosis-rpc-address", "http://localhost:26457", "RPC address of osmosis node started by znet")
flag.StringVar(&OsmosisFundingMnemonic, "osmosis-funding-mnemonic", "sad hobby filter tray ordinary gap half web cat hard call mystery describe member round trend friend beyond such clap frozen segment fan mistake", "Funding account mnemonic required by tests")
// accept testing flags
testing.Init()
// parse additional flags
flag.Parse()
ctx = context.Background()
// set the default staker mnemonic used in the dev znet by default
if len(coreumStakerMnemonics) == 0 {
coreumStakerMnemonics = []string{
"biology rigid design broccoli adult hood modify tissue swallow arctic option improve quiz cliff inject soup ozone suffer fantasy layer negative eagle leader priority",
"enemy fix tribe swift alcohol metal salad edge episode dry tired address bless cloth error useful define rough fold swift confirm century wasp acoustic",
"act electric demand cancel duck invest below once obvious estate interest solution drink mango reason already clean host limit stadium smoke census pattern express",
}
}
queryCtx, queryCtxCancel := context.WithTimeout(ctx, client.DefaultContextConfig().TimeoutConfig.RequestTimeout)
defer queryCtxCancel()
// ********** Coreum **********
coreumGRPCClient, err := grpc.Dial(coreumGRPCAddress, grpc.WithInsecure())
if err != nil {
panic(errors.WithStack(err))
}
coreumSettings := queryCommonSettings(queryCtx, coreumGRPCClient)
coreumClientCtx := client.NewContext(client.DefaultContextConfig(), app.ModuleBasics).
WithGRPCClient(coreumGRPCClient)
coreumFeemodelParamsRes, err := feemodeltypes.NewQueryClient(coreumClientCtx).Params(queryCtx, &feemodeltypes.QueryParamsRequest{})
if err != nil {
panic(errors.WithStack(err))
}
coreumSettings.GasPrice = coreumFeemodelParamsRes.Params.Model.InitialGasPrice
coreumSettings.CoinType = constant.CoinType
coreumSettings.RPCAddress = coreumRPCAddress
config.SetSDKConfig(coreumSettings.AddressPrefix, constant.CoinType)
coreumRPClient, err := sdkclient.NewClientFromNode(coreumRPCAddress)
if err != nil {
panic(errors.WithStack(err))
}
coreumChain = NewCoreumChain(NewChain(
coreumGRPCClient,
coreumRPClient,
coreumSettings,
coreumFundingMnemonic), coreumStakerMnemonics)
}
// NewCoreumTestingContext returns the configured coreum chain and new context for the integration tests.
func NewCoreumTestingContext(t *testing.T) (context.Context, CoreumChain) {
testCtx, testCtxCancel := context.WithCancel(ctx)
t.Cleanup(testCtxCancel)
return testCtx, coreumChain
}
// GetGaiaChain returns the configured gaia chain.
func GetGaiaChain(t *testing.T) Chain {
gaiaChainSyncOnce.Do(func() {
queryCtx, queryCtxCancel := context.WithTimeout(ctx, client.DefaultContextConfig().TimeoutConfig.RequestTimeout)
defer queryCtxCancel()
gaiaGRPClient, err := grpc.Dial(GaiaGRPCAddress, grpc.WithInsecure())
if err != nil {
panic(errors.WithStack(err))
}
gaiaSettings := queryCommonSettings(queryCtx, gaiaGRPClient)
gaiaSettings.GasPrice = sdk.MustNewDecFromStr("0.01")
gaiaSettings.GasAdjustment = 1.5
gaiaSettings.CoinType = sdk.CoinType // gaia coin type
gaiaSettings.RPCAddress = GaiaRPCAddress
gaiaRPClient, err := sdkclient.NewClientFromNode(GaiaRPCAddress)
if err != nil {
panic(errors.WithStack(err))
}
gaiaChain = NewChain(
gaiaGRPClient,
gaiaRPClient,
gaiaSettings,
GaiaFundingMnemonic)
})
return gaiaChain
}
// NewOsmosisTestingChain returns the configured osmosis chain and new context for the integration tests.
func GetOsmosisChain(t *testing.T) Chain {
osmosisChainSyncOnce.Do(func() {
queryCtx, queryCtxCancel := context.WithTimeout(ctx, client.DefaultContextConfig().TimeoutConfig.RequestTimeout)
defer queryCtxCancel()
osmosisGRPClient, err := grpc.Dial(OsmosisGRPCAddress, grpc.WithInsecure())
if err != nil {
panic(errors.WithStack(err))
}
osmosisChainSettings := queryCommonSettings(queryCtx, osmosisGRPClient)
osmosisChainSettings.GasPrice = sdk.MustNewDecFromStr("0.01")
osmosisChainSettings.GasAdjustment = 1.5
osmosisChainSettings.CoinType = sdk.CoinType // osmosis coin type
osmosisChainSettings.RPCAddress = OsmosisRPCAddress
osmosisRPClient, err := sdkclient.NewClientFromNode(OsmosisRPCAddress)
if err != nil {
panic(errors.WithStack(err))
}
osmosisChain = NewChain(
osmosisGRPClient,
osmosisRPClient,
osmosisChainSettings,
OsmosisFundingMnemonic)
})
return osmosisChain
}
// queryCommonSettings queries chain settings from running chains.
func queryCommonSettings(ctx context.Context, grpcClient protobufgrpc.ClientConn) ChainSettings {
clientCtx := client.NewContext(client.DefaultContextConfig(), app.ModuleBasics).
WithGRPCClient(grpcClient)
infoBeforeRes, err := tmservice.NewServiceClient(clientCtx).GetNodeInfo(ctx, &tmservice.GetNodeInfoRequest{})
if err != nil {
panic(fmt.Sprintf("can't get node info, err: %s", err))
}
chainID := infoBeforeRes.DefaultNodeInfo.Network
paramsRes, err := stakingtypes.NewQueryClient(clientCtx).Params(ctx, &stakingtypes.QueryParamsRequest{})
if err != nil {
panic(fmt.Sprintf("can't get staking params, err: %s", err))
}
denom := paramsRes.Params.BondDenom
accountsRes, err := authtypes.NewQueryClient(clientCtx).Accounts(ctx, &authtypes.QueryAccountsRequest{})
if err != nil {
panic(fmt.Sprintf("can't get account params, err: %s", err))
}
var addressPrefix string
for _, account := range accountsRes.Accounts {
if account != nil && account.TypeUrl == fmt.Sprintf("/%s", proto.MessageName(&authtypes.BaseAccount{})) {
var acc authtypes.BaseAccount
if err := proto.Unmarshal(account.Value, &acc); err != nil {
panic(fmt.Sprintf("can't unpack account, err: %s", err))
}
addressPrefix, _, err = bech32.DecodeAndConvert(acc.Address)
if err != nil {
panic(fmt.Sprintf("can't extract address prefix address:%s, err: %s", acc.Address, err))
}
break
}
}
if addressPrefix == "" {
panic("address prefix is empty")
}
return ChainSettings{
ChainID: chainID,
Denom: denom,
AddressPrefix: addressPrefix,
}
}