-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpoa_to_pos_test.go
191 lines (143 loc) · 6.46 KB
/
poa_to_pos_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
package e2e
import (
"context"
"fmt"
"strconv"
"testing"
"time"
sdkmath "cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingttypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/docker/docker/client"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
upgradetypes "cosmossdk.io/x/upgrade/types"
)
const (
chainName = "poa"
/*
This upgrade handler is just noop with:
storeUpgrades := storetypes.StoreUpgrades{
Deleted: []string{
"poa",
},
}
*/
upgradeName = "v2-remove-poa"
// create a simapp that does not have PoA code in it, then you:
// make local-image && docker image tag poa:local ghcr.io/reecepbcups/poa:bare-pos-feb-4-2025 && docker push
upgradeRepo, upgradeVersion = "ghcr.io/reecepbcups/poa", "bare-pos-feb-4-2025"
haltHeightDelta = int64(9) // will propose upgrade this many blocks in the future
blocksAfterUpgrade = int64(3)
)
func TestPoAToPoSUpgrade(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
t.Parallel()
t.Log(chainName, POAImage.Repository, POAImage.Version, upgradeName)
numVals, numNodes := 4, 0
cfg := POACfg
chains := interchaintest.CreateChainWithConfig(t, numVals, numNodes, "poa", "", cfg)
chain := chains[0].(*cosmos.CosmosChain)
ctx, ic, client, _ := interchaintest.BuildInitialChain(t, chains, false)
t.Cleanup(func() {
_ = ic.Close()
})
userFunds := sdkmath.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
chainUser := users[0]
// upgrade
height, err := chain.Height(ctx)
require.NoError(t, err, "error fetching height before submit upgrade proposal")
haltHeight := height + haltHeightDelta
propIdStr := SubmitUpgradeProposal(t, ctx, chain, chainUser, upgradeName, haltHeight)
propId, err := strconv.ParseUint(propIdStr, 10, 64)
require.NoError(t, err, "failed to convert proposal ID to uint64")
err = chain.VoteOnProposalAllValidators(ctx, propIdStr, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")
_, err = cosmos.PollForProposalStatus(ctx, chain, height, height+haltHeightDelta, propId, govv1beta1.StatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")
height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height before upgrade")
t.Logf("height before upgrade: %d", height)
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*10)
defer timeoutCtxCancel()
// this should timeout due to chain halt at upgrade height.
_ = testutil.WaitForBlocks(timeoutCtx, int(haltHeight-height)+1, chain)
// bring down nodes to prepare for upgrade
err = chain.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")
// upgrade version on all nodes
chain.UpgradeVersion(ctx, client, upgradeRepo, upgradeVersion)
// start all nodes back up. validators reach consensus on first block after upgrade height
// and chain block production resumes.
err = chain.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")
timeoutCtx, timeoutCtxCancel = context.WithTimeout(ctx, time.Second*30)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), chain)
require.NoError(t, err, "chain did not produce blocks after upgrade")
verifyStakingFunctions(t, ctx, chain)
}
func verifyStakingFunctions(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain) {
vals, err := chain.StakingQueryValidators(ctx, stakingttypes.BondStatusBonded)
require.NoError(t, err, "error querying validators")
validator := vals[0]
val, err := chain.StakingQueryValidator(ctx, validator.OperatorAddress)
require.NoError(t, err, "error querying validator")
before := val.Tokens
t.Logf("before validator: %s", val)
err = chain.GetNode().StakingDelegate(ctx, "validator", validator.OperatorAddress, "7000000"+chain.Config().Denom)
require.NoError(t, err, "error delegating to validator")
val, err = chain.StakingQueryValidator(ctx, validator.OperatorAddress)
require.NoError(t, err, "error querying validator")
t.Logf("after validator: %s", val)
after := val.Tokens
require.True(t, after.GT(before), "after tokens is not greater than before")
}
func UpgradeNodes(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, client *client.Client, haltHeight int64, upgradeRepo, upgradeBranchVersion string) {
// bring down nodes to prepare for upgrade
t.Log("stopping node(s)")
err := chain.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")
// upgrade version on all nodes
t.Log("upgrading node(s)")
chain.UpgradeVersion(ctx, client, upgradeRepo, upgradeBranchVersion)
// start all nodes back up.
// validators reach consensus on first block after upgrade height
// and chain block production resumes.
t.Log("starting node(s)")
err = chain.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*60)
defer timeoutCtxCancel()
err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), chain)
require.NoError(t, err, "chain did not produce blocks after upgrade")
height, err := chain.Height(ctx)
require.NoError(t, err, "error fetching height after upgrade")
require.GreaterOrEqual(t, height, haltHeight+blocksAfterUpgrade, "height did not increment enough after upgrade")
}
func SubmitUpgradeProposal(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, upgradeName string, haltHeight int64) string {
upgradeMsg := []cosmos.ProtoMessage{
&upgradetypes.MsgSoftwareUpgrade{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Plan: upgradetypes.Plan{
Name: upgradeName,
Height: int64(haltHeight),
Info: "",
},
},
}
proposal, err := chain.BuildProposal(upgradeMsg, "Chain Upgrade 1", "Summary desc", "ipfs://CID", fmt.Sprintf(`500000000%s`, chain.Config().Denom), user.FormattedAddress(), false)
require.NoError(t, err, "error building proposal")
txProp, err := chain.SubmitProposal(ctx, user.KeyName(), proposal)
t.Log("txProp", txProp)
require.NoError(t, err, "error submitting proposal")
return txProp.ProposalID
}