-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathchain.go
197 lines (167 loc) · 5.36 KB
/
chain.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
package e2e
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strconv"
tmrand "github.com/cometbft/cometbft/libs/rand"
dbm "github.com/cosmos/cosmos-db"
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v10/types"
wasmclienttypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v10/types"
providertypes "github.com/cosmos/interchain-security/v7/x/ccv/provider/types"
"cosmossdk.io/log"
evidencetypes "cosmossdk.io/x/evidence/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distribtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
paramsproptypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gaia "github.com/cosmos/gaia/v23/app"
gaiaparams "github.com/cosmos/gaia/v23/app/params"
metaprotocoltypes "github.com/cosmos/gaia/v23/x/metaprotocols/types"
)
const (
keyringPassphrase = "testpassphrase"
keyringAppName = "testnet"
)
var (
encodingConfig gaiaparams.EncodingConfig
cdc codec.Codec
txConfig client.TxConfig
)
func init() {
encodingConfig = gaiaparams.MakeEncodingConfig()
banktypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
authtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
authvesting.RegisterInterfaces(encodingConfig.InterfaceRegistry)
stakingtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
evidencetypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cryptocodec.RegisterInterfaces(encodingConfig.InterfaceRegistry)
govv1types.RegisterInterfaces(encodingConfig.InterfaceRegistry)
govv1beta1types.RegisterInterfaces(encodingConfig.InterfaceRegistry)
paramsproptypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
paramsproptypes.RegisterLegacyAminoCodec(encodingConfig.Amino)
upgradetypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
distribtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
providertypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
metaprotocoltypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ratelimittypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
wasmclienttypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cdc = encodingConfig.Marshaler
txConfig = encodingConfig.TxConfig
}
type chain struct {
dataDir string
id string
validators []*validator
accounts []*account //nolint:unused
// initial accounts in genesis
genesisAccounts []*account
genesisVestingAccounts map[string]sdk.AccAddress
}
func newChain() (*chain, error) {
tmpDir, err := os.MkdirTemp("", "gaia-e2e-testnet-")
if err != nil {
return nil, err
}
return &chain{
id: "chain-" + tmrand.Str(6),
dataDir: tmpDir,
}, nil
}
func (c *chain) configDir() string {
return fmt.Sprintf("%s/%s", c.dataDir, c.id)
}
func (c *chain) createAndInitValidators(count int) error {
// create a separate app dir for the tempApp so that wasmvm won't complain about file locks
tempAppDir := filepath.Join(gaia.DefaultNodeHome, strconv.Itoa(rand.Intn(10000)))
tempApplication := gaia.NewGaiaApp(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
tempAppDir,
gaia.EmptyAppOptions{},
gaia.EmptyWasmOptions,
)
defer func() {
if err := tempApplication.Close(); err != nil {
panic(err)
}
}()
genesisState := tempApplication.ModuleBasics.DefaultGenesis(encodingConfig.Marshaler)
for i := 0; i < count; i++ {
node := c.createValidator(i)
// generate genesis files
if err := node.init(genesisState); err != nil {
return err
}
c.validators = append(c.validators, node)
// create keys
if err := node.createKey("val"); err != nil {
return err
}
if err := node.createNodeKey(); err != nil {
return err
}
if err := node.createConsensusKey(); err != nil {
return err
}
}
return nil
}
func (c *chain) createAndInitValidatorsWithMnemonics(count int, mnemonics []string) error { //nolint:unused // this is called during e2e tests
tempApplication := gaia.NewGaiaApp(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
gaia.DefaultNodeHome,
gaia.EmptyAppOptions{},
gaia.EmptyWasmOptions,
)
defer func() {
if err := tempApplication.Close(); err != nil {
panic(err)
}
}()
genesisState := tempApplication.ModuleBasics.DefaultGenesis(encodingConfig.Marshaler)
for i := 0; i < count; i++ {
// create node
node := c.createValidator(i)
// generate genesis files
if err := node.init(genesisState); err != nil {
return err
}
c.validators = append(c.validators, node)
// create keys
if err := node.createKeyFromMnemonic("val", mnemonics[i]); err != nil {
return err
}
if err := node.createNodeKey(); err != nil {
return err
}
if err := node.createConsensusKey(); err != nil {
return err
}
}
return nil
}
func (c *chain) createValidator(index int) *validator {
return &validator{
chain: c,
index: index,
moniker: fmt.Sprintf("%s-gaia-%d", c.id, index),
}
}