-
Notifications
You must be signed in to change notification settings - Fork 607
/
upgrade.go
279 lines (238 loc) · 9.71 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
package configurer
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
appparams "github.com/osmosis-labs/osmosis/v14/app/params"
"github.com/osmosis-labs/osmosis/v14/tests/e2e/configurer/chain"
"github.com/osmosis-labs/osmosis/v14/tests/e2e/configurer/config"
"github.com/osmosis-labs/osmosis/v14/tests/e2e/containers"
"github.com/osmosis-labs/osmosis/v14/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 {
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("", "osmosis-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 {
const lockupWallet = "lockup-wallet"
const lockupWalletSuperfluid = "lockup-wallet-superfluid"
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
}
chainA.SendIBC(chainB, chainB.NodeConfigs[0].PublicAddress, initialization.OsmoToken)
chainB.SendIBC(chainA, chainA.NodeConfigs[0].PublicAddress, initialization.OsmoToken)
chainA.SendIBC(chainB, chainB.NodeConfigs[0].PublicAddress, initialization.StakeToken)
chainB.SendIBC(chainA, chainA.NodeConfigs[0].PublicAddress, initialization.StakeToken)
config.PreUpgradePoolId = chainANode.CreateBalancerPool("pool1A.json", initialization.ValidatorWalletName)
poolShareDenom := fmt.Sprintf("gamm/pool/%d", config.PreUpgradePoolId)
chainBNode.CreateBalancerPool("pool1B.json", initialization.ValidatorWalletName)
// enable superfluid assets on chainA
chainA.EnableSuperfluidAsset(poolShareDenom)
// setup wallets and send gamm tokens to these wallets (only chainA)
lockupWalletAddrA, lockupWalletSuperfluidAddrA := chainANode.CreateWallet(lockupWallet), chainANode.CreateWallet(lockupWalletSuperfluid)
chainANode.BankSend("10000000000000000000"+poolShareDenom, chainA.NodeConfigs[0].PublicAddress, lockupWalletAddrA)
chainANode.BankSend("10000000000000000000"+poolShareDenom, chainA.NodeConfigs[0].PublicAddress, lockupWalletSuperfluidAddrA)
// Upload the rate limiting contract to both chains (as they both will be updated)
uc.t.Logf("Uploading rate limiting contract to both chains")
_, err = chainA.SetupRateLimiting("", chainANode.QueryGovModuleAccount())
if err != nil {
return err
}
_, _ = chainB.SetupRateLimiting("", chainBNode.QueryGovModuleAccount())
if err != nil {
return err
}
// test lock and add to existing lock for both regular and superfluid lockups (only chainA)
chainA.LockAndAddToExistingLock(sdk.NewInt(1000000000000000000), poolShareDenom, lockupWalletAddrA, lockupWalletSuperfluidAddrA)
// LP to pools 833, 817, 810
// initialize lp wallets
amountOfEachTokenToLP := initialization.DefaultStrideDenomBalance / 1_000_000
shareOutMin := "1"
config.StrideMigrateWallet = chainANode.CreateWalletAndFund(config.StrideMigrateWallet, []string{
fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StOsmoDenom),
fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StJunoDenom),
fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StStarsDenom),
})
tokenInStOsmo := fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StOsmoDenom)
chainANode.JoinPoolExactAmountIn(tokenInStOsmo, initialization.StOSMO_OSMOPoolId, shareOutMin, config.StrideMigrateWallet)
tokenInStJuno := fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StJunoDenom)
chainANode.JoinPoolExactAmountIn(tokenInStJuno, initialization.StJUNO_JUNOPoolId, shareOutMin, config.StrideMigrateWallet)
tokenInStStars := fmt.Sprintf("%d%s", amountOfEachTokenToLP, initialization.StStarsDenom)
chainANode.JoinPoolExactAmountIn(tokenInStStars, initialization.StSTARS_STARSPoolId, shareOutMin, config.StrideMigrateWallet)
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 {
for validatorIdx, node := range chainConfig.NodeConfigs {
if validatorIdx == 0 {
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)
}
node.VoteYesProposal(initialization.ValidatorWalletName, chainConfig.LatestProposalNumber)
}
}
// 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.OsmosisRepository = containers.CurrentBranchOsmoRepository
uc.containerManager.OsmosisTag = containers.CurrentBranchOsmoTag
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
}