-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
417 lines (347 loc) · 11.8 KB
/
upgrade.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package configurer
import (
"encoding/json"
"fmt"
"os"
"sync"
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
appparams "github.com/percosis-labs/percosis/app/params"
v16 "github.com/percosis-labs/percosis/app/upgrades/v16"
"github.com/percosis-labs/percosis/tests/e2e/configurer/chain"
"github.com/percosis-labs/percosis/tests/e2e/configurer/config"
"github.com/percosis-labs/percosis/tests/e2e/containers"
"github.com/percosis-labs/percosis/tests/e2e/initialization"
)
type UpgradeSettings struct {
IsEnabled bool
Version string
ForkHeight int64 // non-zero height implies that this is a fork upgrade.
}
type UpgradeConfigurer struct {
baseConfigurer
upgradeVersion string
forkHeight int64 // forkHeight > 0 implies that this is a fork upgrade. Otherwise, proposal upgrade.
}
var _ Configurer = (*UpgradeConfigurer)(nil)
func NewUpgradeConfigurer(t *testing.T, chainConfigs []*chain.Config, setupTests setupFn, containerManager *containers.Manager, upgradeVersion string, forkHeight int64) Configurer {
t.Helper()
return &UpgradeConfigurer{
baseConfigurer: baseConfigurer{
chainConfigs: chainConfigs,
containerManager: containerManager,
setupTests: setupTests,
syncUntilHeight: forkHeight + defaultSyncUntilHeight,
t: t,
},
forkHeight: forkHeight,
upgradeVersion: upgradeVersion,
}
}
func (uc *UpgradeConfigurer) ConfigureChains() error {
for _, chainConfig := range uc.chainConfigs {
if err := uc.ConfigureChain(chainConfig); err != nil {
return err
}
}
return nil
}
func (uc *UpgradeConfigurer) ConfigureChain(chainConfig *chain.Config) error {
uc.t.Logf("starting upgrade e2e infrastructure for chain-id: %s", chainConfig.Id)
tmpDir, err := os.MkdirTemp("", "percosis-e2e-testnet-")
if err != nil {
return err
}
validatorConfigBytes, err := json.Marshal(chainConfig.ValidatorInitConfigs)
if err != nil {
return err
}
forkHeight := uc.forkHeight
if forkHeight > 0 {
forkHeight = forkHeight - config.ForkHeightPreUpgradeOffset
}
chainInitResource, err := uc.containerManager.RunChainInitResource(chainConfig.Id, int(chainConfig.VotingPeriod), int(chainConfig.ExpeditedVotingPeriod), validatorConfigBytes, tmpDir, int(forkHeight))
if err != nil {
return err
}
fileName := fmt.Sprintf("%v/%v-encode", tmpDir, chainConfig.Id)
uc.t.Logf("serialized init file for chain-id %v: %v", chainConfig.Id, fileName)
// loop through the reading and unmarshaling of the init file a total of maxRetries or until error is nil
// without this, test attempts to unmarshal file before docker container is finished writing
var initializedChain initialization.Chain
for i := 0; i < config.MaxRetries; i++ {
initializedChainBytes, _ := os.ReadFile(fileName)
err = json.Unmarshal(initializedChainBytes, &initializedChain)
if err == nil {
break
}
if i == config.MaxRetries-1 {
if err != nil {
return err
}
}
if i > 0 {
time.Sleep(1 * time.Second)
}
}
if err := uc.containerManager.PurgeResource(chainInitResource); err != nil {
return err
}
uc.initializeChainConfigFromInitChain(&initializedChain, chainConfig)
return nil
}
func (uc *UpgradeConfigurer) CreatePreUpgradeState() error {
// Create a WaitGroup to wait for all goroutines to complete
var wg sync.WaitGroup
chainA := uc.chainConfigs[0]
chainANode, err := chainA.GetDefaultNode()
if err != nil {
return err
}
chainB := uc.chainConfigs[1]
chainBNode, err := chainB.GetDefaultNode()
if err != nil {
return err
}
wg.Add(2)
go func() {
defer wg.Done()
chainA.SendIBC(chainB, chainB.NodeConfigs[0].PublicAddress, initialization.PercoToken)
chainA.SendIBC(chainB, chainB.NodeConfigs[0].PublicAddress, initialization.StakeToken)
}()
go func() {
defer wg.Done()
chainB.SendIBC(chainA, chainA.NodeConfigs[0].PublicAddress, initialization.PercoToken)
chainB.SendIBC(chainA, chainA.NodeConfigs[0].PublicAddress, initialization.StakeToken)
}()
// Wait for all goroutines to complete
wg.Wait()
wg.Add(2)
var daiPercoPoolIdv16 uint64
go func() {
defer wg.Done()
daiPercoPoolIdv16 = chainANode.CreateBalancerPool("daipercov16.json", initialization.ValidatorWalletName)
daiPercoShareDenom := fmt.Sprintf("gamm/pool/%d", daiPercoPoolIdv16)
chainANode.EnableSuperfluidAsset(chainA, daiPercoShareDenom)
}()
go func() {
defer wg.Done()
chainBNode.CreateBalancerPool("daipercov16.json", initialization.ValidatorWalletName)
}()
// Wait for all goroutines to complete
wg.Wait()
config.DaiPercoPoolIdv16 = daiPercoPoolIdv16
var (
poolShareDenom string
preUpgradePoolId uint64
preUpgradeStableSwapPoolId uint64
)
// Increment the WaitGroup counter for each goroutine
wg.Add(4)
go func() {
defer wg.Done()
preUpgradePoolId = chainANode.CreateBalancerPool("pool1A.json", initialization.ValidatorWalletName)
poolShareDenom = fmt.Sprintf("gamm/pool/%d", preUpgradePoolId)
chainANode.EnableSuperfluidAsset(chainA, poolShareDenom)
}()
go func() {
defer wg.Done()
chainBNode.CreateBalancerPool("pool1B.json", initialization.ValidatorWalletName)
}()
go func() {
defer wg.Done()
preUpgradeStableSwapPoolId = chainANode.CreateStableswapPool("stablePool.json", initialization.ValidatorWalletName)
}()
go func() {
defer wg.Done()
chainBNode.CreateStableswapPool("stablePool.json", initialization.ValidatorWalletName)
}()
// Wait for all goroutines to complete
wg.Wait()
config.PreUpgradePoolId = preUpgradePoolId
config.PreUpgradeStableSwapPoolId = preUpgradeStableSwapPoolId
fmt.Println("poolShareDenom: ", poolShareDenom)
var (
lockupWallet string
lockupWalletSuperfluid string
stableswapWallet string
)
wg.Add(3)
go func() {
defer wg.Done()
// Setup wallets and send tokens to wallets (only chainA)
lockupWallet = chainANode.CreateWalletAndFund(config.LockupWallet, []string{
"10000000000000000000" + poolShareDenom,
})
}()
go func() {
defer wg.Done()
lockupWalletSuperfluid = chainANode.CreateWalletAndFund(config.LockupWalletSuperfluid, []string{
"10000000000000000000" + poolShareDenom,
})
}()
go func() {
defer wg.Done()
stableswapWallet = chainANode.CreateWalletAndFund(config.StableswapWallet, []string{
"100000stake",
})
}()
// Wait for all goroutines to complete
wg.Wait()
config.LockupWallet = lockupWallet
config.LockupWalletSuperfluid = lockupWalletSuperfluid
config.StableswapWallet = stableswapWallet
wg.Add(6)
var errCh = make(chan error, 2)
oneDai := sdk.NewCoin(v16.DAIIBCDenom, sdk.NewInt(1000000000000000000))
go func() {
defer wg.Done()
// test swap exact amount in for stable swap pool (only chainA)A
chainANode.SwapExactAmountIn("2000stake", "1", fmt.Sprintf("%d", config.PreUpgradeStableSwapPoolId), "ufury", config.StableswapWallet)
}()
// Upload the rate limiting contract to both chains (as they both will be updated)
go func() {
defer wg.Done()
uc.t.Logf("Uploading rate limiting contract to chainA")
_, err := chainANode.SetupRateLimiting("", chainANode.QueryGovModuleAccount(), chainA)
errCh <- err
}()
go func() {
defer wg.Done()
uc.t.Logf("Uploading rate limiting contract to chainB")
_, err := chainBNode.SetupRateLimiting("", chainBNode.QueryGovModuleAccount(), chainB)
errCh <- err
}()
go func() {
defer wg.Done()
uc.t.Logf("Lock and add to existing lock for both regular and superfluid lockups on chainA")
chainANode.LockAndAddToExistingLock(chainA, sdk.NewInt(1000000000000000000), poolShareDenom, config.LockupWallet, config.LockupWalletSuperfluid)
}()
go func() {
defer wg.Done()
uc.t.Logf("Funding chainA's community pool with 1 DAI to be used for the upgrade")
communityPoolFunder := chainANode.CreateWalletAndFund("communityPoolFunder", []string{
oneDai.String(),
})
chainANode.FundCommunityPool(communityPoolFunder, oneDai.String())
}()
go func() {
defer wg.Done()
uc.t.Logf("Funding chainB's community pool with 1 DAI to be used for the upgrade")
communityPoolFunder := chainBNode.CreateWalletAndFund("communityPoolFunder", []string{
oneDai.String(),
})
chainBNode.FundCommunityPool(communityPoolFunder, oneDai.String())
}()
wg.Wait()
close(errCh)
for err := range errCh {
if err != nil {
return err
}
}
return nil
}
func (uc *UpgradeConfigurer) RunSetup() error {
return uc.setupTests(uc)
}
func (uc *UpgradeConfigurer) RunUpgrade() error {
var err error
if uc.forkHeight > 0 {
err = uc.runForkUpgrade()
} else {
err = uc.runProposalUpgrade()
}
if err != nil {
return err
}
// Check if the nodes are running
for chainIndex, chainConfig := range uc.chainConfigs {
chain := uc.baseConfigurer.GetChainConfig(chainIndex)
for validatorIdx := range chainConfig.NodeConfigs {
node := chain.NodeConfigs[validatorIdx]
// Check node status
_, err = node.Status()
if err != nil {
uc.t.Errorf("node is not running after upgrade, chain-id %s, node %s", chainConfig.Id, node.Name)
return err
}
uc.t.Logf("node %s upgraded successfully, address %s", node.Name, node.PublicAddress)
}
}
return nil
}
func (uc *UpgradeConfigurer) runProposalUpgrade() error {
// submit, deposit, and vote for upgrade proposal
// prop height = current height + voting period + time it takes to submit proposal + small buffer
for _, chainConfig := range uc.chainConfigs {
node, err := chainConfig.GetDefaultNode()
if err != nil {
return err
}
currentHeight, err := node.QueryCurrentHeight()
if err != nil {
return err
}
chainConfig.UpgradePropHeight = currentHeight + int64(chainConfig.VotingPeriod) + int64(config.PropSubmitBlocks) + int64(config.PropBufferBlocks)
node.SubmitUpgradeProposal(uc.upgradeVersion, chainConfig.UpgradePropHeight, sdk.NewCoin(appparams.BaseCoinUnit, sdk.NewInt(config.InitialMinDeposit)))
chainConfig.LatestProposalNumber += 1
node.DepositProposal(chainConfig.LatestProposalNumber, false)
propNumber := chainConfig.LatestProposalNumber
var wg sync.WaitGroup
for _, node := range chainConfig.NodeConfigs {
wg.Add(1)
go func(nodeConfig *chain.NodeConfig) {
defer wg.Done()
nodeConfig.VoteYesProposal(initialization.ValidatorWalletName, propNumber)
}(node)
}
wg.Wait()
}
// wait till all chains halt at upgrade height
for _, chainConfig := range uc.chainConfigs {
uc.t.Logf("waiting to reach upgrade height on chain %s", chainConfig.Id)
chainConfig.WaitUntilHeight(chainConfig.UpgradePropHeight)
uc.t.Logf("upgrade height reached on chain %s", chainConfig.Id)
}
// remove all containers so we can upgrade them to the new version
for _, chainConfig := range uc.chainConfigs {
for _, validatorConfig := range chainConfig.NodeConfigs {
err := uc.containerManager.RemoveNodeResource(validatorConfig.Name)
if err != nil {
return err
}
}
}
// remove all containers so we can upgrade them to the new version
for _, chainConfig := range uc.chainConfigs {
if err := uc.upgradeContainers(chainConfig, chainConfig.UpgradePropHeight); err != nil {
return err
}
}
return nil
}
func (uc *UpgradeConfigurer) runForkUpgrade() error {
for _, chainConfig := range uc.chainConfigs {
uc.t.Logf("waiting to reach fork height on chain %s", chainConfig.Id)
chainConfig.WaitUntilHeight(uc.forkHeight)
uc.t.Logf("fork height reached on chain %s", chainConfig.Id)
}
return nil
}
func (uc *UpgradeConfigurer) upgradeContainers(chainConfig *chain.Config, propHeight int64) error {
// upgrade containers to the locally compiled daemon
uc.t.Logf("starting upgrade for chain-id: %s...", chainConfig.Id)
uc.containerManager.PercosisRepository = containers.CurrentBranchPercoRepository
uc.containerManager.PercosisTag = containers.CurrentBranchPercoTag
for _, node := range chainConfig.NodeConfigs {
if err := node.Run(); err != nil {
return err
}
}
uc.t.Logf("waiting to upgrade containers on chain %s", chainConfig.Id)
chainConfig.WaitUntilHeight(propHeight)
uc.t.Logf("upgrade successful on chain %s", chainConfig.Id)
return nil
}