forked from noble-assets/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_test.go
293 lines (232 loc) · 8.88 KB
/
upgrade_test.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
package interchaintest_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
sdkupgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/noble-assets/noble/v7/cmd"
interchaintest "github.com/strangelove-ventures/interchaintest/v4"
"github.com/strangelove-ventures/interchaintest/v4/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v4/ibc"
"github.com/strangelove-ventures/interchaintest/v4/testreporter"
"github.com/strangelove-ventures/interchaintest/v4/testutil"
upgradetypes "github.com/strangelove-ventures/paramauthority/x/upgrade/types"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
const (
ghcrRepo = "ghcr.io/noble-assets/noble"
containerUidGid = "1025:1025"
haltHeightDelta = uint64(10) // will propose upgrade this many blocks in the future
blocksAfterUpgrade = uint64(10)
)
type ParamsQueryResponse struct {
Subspace string `json:"subspace"`
Key string `json:"key"`
Value string `json:"value"`
}
type chainUpgrade struct {
image ibc.DockerImage
upgradeName string // if upgradeName is empty, assumes patch/rolling update
emergency bool
preUpgrade func(t *testing.T, ctx context.Context, noble *cosmos.CosmosChain, paramAuthority ibc.Wallet)
postUpgrade func(t *testing.T, ctx context.Context, noble *cosmos.CosmosChain, paramAuthority ibc.Wallet)
}
func ghcrImage(version string) ibc.DockerImage {
return ibc.DockerImage{
Repository: ghcrRepo,
Version: version,
UidGid: containerUidGid,
}
}
func testNobleChainUpgrade(
t *testing.T,
chainID string,
genesisVersionImage ibc.DockerImage,
genesisTokenFactoryDenomMetadata DenomMetadata,
numberOfValidators int,
numberOfFullNodes int,
upgrades []chainUpgrade,
) {
if testing.Short() {
t.Skip()
}
t.Parallel()
ctx := context.Background()
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)
client, network := interchaintest.DockerSetup(t)
var gw genesisWrapper
cs := nobleChainSpec(ctx, &gw, chainID, numberOfValidators, numberOfFullNodes, false, false, false, false)
cs.ChainConfig.PreGenesis = func(cc ibc.ChainConfig) error {
val := gw.chain.Validators[0]
var err error
gw.tfRoles, err = createTokenfactoryRoles(ctx, genesisTokenFactoryDenomMetadata, val, true)
if err != nil {
return err
}
gw.paramAuthority, err = createParamAuthAtGenesis(ctx, val)
return err
}
cs.ChainConfig.ModifyGenesis = func(cc ibc.ChainConfig, b []byte) ([]byte, error) {
g := make(map[string]interface{})
if err := json.Unmarshal(b, &g); err != nil {
return nil, fmt.Errorf("failed to unmarshal genesis file: %w", err)
}
if err := modifyGenesisTokenfactory(g, "tokenfactory", genesisTokenFactoryDenomMetadata, gw.tfRoles, true); err != nil {
return nil, err
}
if err := modifyGenesisParamAuthority(g, gw.paramAuthority.FormattedAddress()); err != nil {
return nil, err
}
if err := modifyGenesisDowntimeWindow(g); err != nil {
return nil, err
}
out, err := json.Marshal(&g)
if err != nil {
return nil, fmt.Errorf("failed to marshal genesis bytes to json: %w", err)
}
return out, nil
}
cs.ChainConfig.Images = []ibc.DockerImage{genesisVersionImage}
logger := zaptest.NewLogger(t)
cf := interchaintest.NewBuiltinChainFactory(logger, []*interchaintest.ChainSpec{cs})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
gw.chain = chains[0].(*cosmos.CosmosChain)
noble := gw.chain
ic := interchaintest.NewInterchain().
AddChain(noble)
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
})
chainCfg := noble.Config()
cmd.SetPrefixes(chainCfg.Bech32Prefix)
for _, upgrade := range upgrades {
if upgrade.preUpgrade != nil {
upgrade.preUpgrade(t, ctx, noble, gw.paramAuthority)
}
if upgrade.upgradeName == "" {
// patch/rolling upgrade
if upgrade.emergency {
err = noble.StopAllNodes(ctx)
require.NoError(t, err, "could not stop nodes for emergency upgrade")
noble.UpgradeVersion(ctx, client, upgrade.image.Repository, upgrade.image.Version)
err = noble.StartAllNodes(ctx)
require.NoError(t, err, "could not start nodes for emergency upgrade")
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), noble)
require.NoError(t, err, "chain did not produce blocks after emergency upgrade")
} else {
// stage new version
for _, n := range noble.Nodes() {
n.Image = upgrade.image
}
noble.UpgradeVersion(ctx, client, upgrade.image.Repository, upgrade.image.Version)
// do rolling update on half the vals
for i, n := range noble.Validators {
if i%2 == 0 {
continue
}
// shutdown
require.NoError(t, n.StopContainer(ctx))
require.NoError(t, n.RemoveContainer(ctx))
// startup
require.NoError(t, n.CreateNodeContainer(ctx))
require.NoError(t, n.StartContainer(ctx))
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
require.NoError(t, testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), noble))
}
// blocks should still be produced after rolling update
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), noble)
require.NoError(t, err, "chain did not produce blocks after upgrade")
// stop all nodes to bring rest of vals up to date
err = noble.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")
err = noble.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")
timeoutCtx, timeoutCtxCancel = context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), noble)
require.NoError(t, err, "chain did not produce blocks after upgrade")
}
} else {
// halt upgrade
height, err := noble.Height(ctx)
require.NoError(t, err, "error fetching height before submit upgrade proposal")
haltHeight := height + haltHeightDelta
broadcaster := cosmos.NewBroadcaster(t, noble)
upgradePlan := sdkupgradetypes.Plan{
Name: upgrade.upgradeName,
Height: int64(haltHeight),
Info: upgrade.upgradeName + " chain upgrade",
}
wallet := cosmos.NewWallet(
gw.paramAuthority.KeyName(),
gw.paramAuthority.Address(),
gw.paramAuthority.Mnemonic(),
chainCfg,
)
_, err = cosmos.BroadcastTx(
ctx,
broadcaster,
wallet,
&upgradetypes.MsgSoftwareUpgrade{
Authority: gw.paramAuthority.FormattedAddress(),
Plan: upgradePlan,
},
)
require.NoError(t, err, "error submitting software upgrade tx")
stdout, stderr, err := noble.Validators[0].ExecQuery(ctx, "upgrade", "plan")
require.NoError(t, err, "error submitting software upgrade tx")
logger.Debug("Upgrade", zap.String("plan_stdout", string(stdout)), zap.String("plan_stderr", string(stderr)))
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
height, err = noble.Height(ctx)
require.NoError(t, err, "error fetching height before upgrade")
// this should timeout due to chain halt at upgrade height.
_ = testutil.WaitForBlocks(timeoutCtx, int(haltHeight-height)+1, noble)
height, err = noble.Height(ctx)
require.NoError(t, err, "error fetching height after chain should have halted")
// make sure that chain is halted
require.Equal(t, haltHeight, height, "height is not equal to halt height")
// bring down nodes to prepare for upgrade
err = noble.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")
// upgrade all nodes
for _, n := range noble.Nodes() {
n.Image = upgrade.image
}
noble.UpgradeVersion(ctx, client, upgrade.image.Repository, upgrade.image.Version)
// start all nodes back up.
// validators reach consensus on first block after upgrade height
// and chain block production resumes.
err = noble.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")
timeoutCtx, timeoutCtxCancel = context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), noble)
require.NoError(t, err, "chain did not produce blocks after upgrade")
height, err = noble.Height(ctx)
require.NoError(t, err, "error fetching height after upgrade")
require.GreaterOrEqual(t, height, haltHeight+blocksAfterUpgrade, "height did not increment enough after upgrade")
}
if upgrade.postUpgrade != nil {
upgrade.postUpgrade(t, ctx, noble, gw.paramAuthority)
}
}
}