-
Notifications
You must be signed in to change notification settings - Fork 6
/
upgrade.go
177 lines (154 loc) · 5.7 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
package configurer
import (
"cosmossdk.io/math"
"encoding/json"
"fmt"
appparams "github.com/chain4energy/c4e-chain/app/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"os"
"testing"
"time"
"github.com/chain4energy/c4e-chain/tests/e2e/configurer/chain"
"github.com/chain4energy/c4e-chain/tests/e2e/configurer/config"
"github.com/chain4energy/c4e-chain/tests/e2e/containers"
"github.com/chain4energy/c4e-chain/tests/e2e/initialization"
)
type UpgradeSettings struct {
IsEnabled bool
Version string
OldInitialAppStateBytes []byte
MigrationChaining bool
}
type UpgradeConfigurer struct {
baseConfigurer
upgradeVersion string
upgradeLegacyProposal bool
}
var _ Configurer = (*UpgradeConfigurer)(nil)
func NewUpgradeConfigurer(t *testing.T, chainConfigs []*chain.Config, setupTests setupFn, containerManager *containers.Manager, upgradeVersion string, upgradeLegacyProposal bool) Configurer {
return &UpgradeConfigurer{
baseConfigurer: baseConfigurer{
chainConfigs: chainConfigs,
containerManager: containerManager,
setupTests: setupTests,
syncUntilHeight: defaultSyncUntilHeight,
t: t,
},
upgradeVersion: upgradeVersion,
upgradeLegacyProposal: upgradeLegacyProposal,
}
}
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("", "chain4energy-e2e-testnet-")
if err != nil {
return err
}
validatorConfigBytes, err := json.Marshal(chainConfig.ValidatorInitConfigs)
if err != nil {
return err
}
chainInitResource, err := uc.containerManager.RunChainInitResource(chainConfig.Id, int(chainConfig.VotingPeriod), int(chainConfig.ExpeditedVotingPeriod), validatorConfigBytes, tmpDir, chainConfig.AppStateBytes)
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) RunSetup() error {
return uc.setupTests(uc)
}
func (uc *UpgradeConfigurer) RunUpgrade() error {
return uc.runProposalUpgrade()
}
func (uc *UpgradeConfigurer) runProposalUpgrade() error {
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)
if uc.upgradeLegacyProposal { // TODO: fix after new upgrade
node.SubmitLegacyUpgradeProposal(uc.upgradeVersion, chainConfig.UpgradePropHeight, sdk.NewCoin(appparams.MicroC4eUnit, math.NewInt(config.InitialMinDeposit)))
} else {
node.SubmitUpgradeProposal(uc.upgradeVersion, chainConfig.UpgradePropHeight, sdk.NewCoin(appparams.MicroC4eUnit, math.NewInt(config.InitialMinDeposit)))
}
chainConfig.LatestProposalNumber += 1
node.DepositProposal(chainConfig.LatestProposalNumber)
}
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) 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.C4eRepository = containers.CurrentBranchC4eRepository
uc.containerManager.C4eTag = containers.CurrentBranchC4eTag
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 height reached successful on chain %s", chainConfig.Id)
chainConfig.WaitUntilHeight(propHeight + config.UpgradeBufferBlocks)
uc.t.Logf("upgrade successful on chain %s", chainConfig.Id)
return nil
}