/
upgrade.go
218 lines (178 loc) · 5.54 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
package types
import (
"fmt"
)
var UpgradeMgr = NewUpgradeManager(UpgradeConfig{})
const (
FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes
BEP9 = "BEP9" // https://github.com/bnb-chain/BEPs/pull/9
BEP12 = "BEP12" // https://github.com/bnb-chain/BEPs/pull/17
BEP3 = "BEP3" // https://github.com/bnb-chain/BEPs/pull/30
BEP8 = "BEP8" // https://github.com/bnb-chain/BEPs/pull/69
LaunchAscUpgrade = "LaunchAscUpgrade"
BEP82 = "BEP82" // https://github.com/bnb-chain/BEPs/pull/82
FixFailAckPackage = "FixFailAckPackage"
BEP128 = "BEP128" // https://github.com/bnb-chain/BEPs/pull/128
BEP153 = "BEP153" // https://github.com/bnb-chain/BEPs/pull/153
BEP159 = "BEP159" // https://github.com/bnb-chain/BEPs/pull/159
BEP159Phase2 = "BEP159Phase2" // phase 2 activation height of BEP159, enable create validator and active oracle relayer whitelist
BEP173 = "BEP173" // https://github.com/bnb-chain/BEPs/pull/173
FixDoubleSignChainId = "FixDoubleSignChainId"
)
var MainNetConfig = UpgradeConfig{
HeightMap: map[string]int64{},
}
type UpgradeConfig struct {
HeightMap map[string]int64
StoreKeyMap map[string]int64
MsgTypeMap map[string]int64
BeginBlockers map[int64][]func(ctx Context)
}
type UpgradeManager struct {
Config UpgradeConfig
Height int64
}
func NewUpgradeManager(config UpgradeConfig) *UpgradeManager {
return &UpgradeManager{
Config: config,
}
}
func (mgr *UpgradeManager) Reset() {
*mgr = *NewUpgradeManager(UpgradeConfig{})
}
func (mgr *UpgradeManager) AddConfig(config UpgradeConfig) {
for name, height := range config.HeightMap {
mgr.AddUpgradeHeight(name, height)
}
}
func (mgr *UpgradeManager) SetHeight(height int64) {
mgr.Height = height
}
func (mgr *UpgradeManager) GetHeight() int64 {
return mgr.Height
}
// run in every ABCI BeginBlock.
func (mgr *UpgradeManager) BeginBlocker(ctx Context) {
if beginBlockers, ok := mgr.Config.BeginBlockers[mgr.GetHeight()]; ok {
for _, beginBlocker := range beginBlockers {
beginBlocker(ctx)
}
}
}
func (mgr *UpgradeManager) RegisterBeginBlocker(name string, beginBlocker func(Context)) {
height := mgr.GetUpgradeHeight(name)
if height == 0 {
panic(fmt.Errorf("no UpgradeHeight found for %s", name))
}
if mgr.Config.BeginBlockers == nil {
mgr.Config.BeginBlockers = make(map[int64][]func(ctx Context))
}
if beginBlockers, ok := mgr.Config.BeginBlockers[height]; ok {
beginBlockers = append(beginBlockers, beginBlocker)
mgr.Config.BeginBlockers[height] = beginBlockers
} else {
mgr.Config.BeginBlockers[height] = []func(Context){beginBlocker}
}
}
func (mgr *UpgradeManager) AddUpgradeHeight(name string, height int64) {
if mgr.Config.HeightMap == nil {
mgr.Config.HeightMap = map[string]int64{}
}
mgr.Config.HeightMap[name] = height
}
func (mgr *UpgradeManager) GetUpgradeHeight(name string) int64 {
if mgr.Config.HeightMap == nil {
return 0
}
return mgr.Config.HeightMap[name]
}
func (mgr *UpgradeManager) RegisterStoreKeys(upgradeName string, storeKeyNames ...string) {
height := mgr.GetUpgradeHeight(upgradeName)
if height == 0 {
panic(fmt.Errorf("no UpgradeHeight found for %s", upgradeName))
}
if mgr.Config.StoreKeyMap == nil {
mgr.Config.StoreKeyMap = map[string]int64{}
}
for _, storeKeyName := range storeKeyNames {
mgr.Config.StoreKeyMap[storeKeyName] = height
}
}
func (mgr *UpgradeManager) RegisterMsgTypes(upgradeName string, msgTypes ...string) {
height := mgr.GetUpgradeHeight(upgradeName)
if height == 0 {
panic(fmt.Errorf("no UpgradeHeight found for %s", upgradeName))
}
if mgr.Config.MsgTypeMap == nil {
mgr.Config.MsgTypeMap = map[string]int64{}
}
for _, msgType := range msgTypes {
mgr.Config.MsgTypeMap[msgType] = height
}
}
func (mgr *UpgradeManager) GetStoreKeyHeight(storeKeyName string) int64 {
if mgr.Config.StoreKeyMap == nil {
return 0
}
return mgr.Config.StoreKeyMap[storeKeyName]
}
func (mgr *UpgradeManager) GetMsgTypeHeight(msgType string) int64 {
if mgr.Config.MsgTypeMap == nil {
return 0
}
return mgr.Config.MsgTypeMap[msgType]
}
func IsUpgradeHeight(name string) bool {
upgradeHeight := UpgradeMgr.GetUpgradeHeight(name)
if upgradeHeight == 0 {
return false
}
return upgradeHeight == UpgradeMgr.GetHeight()
}
func IsUpgrade(name string) bool {
upgradeHeight := UpgradeMgr.GetUpgradeHeight(name)
if upgradeHeight == 0 {
return false
}
return UpgradeMgr.GetHeight() >= upgradeHeight
}
func ShouldCommitStore(storeKeyName string) bool {
storeKeyHeight := UpgradeMgr.GetStoreKeyHeight(storeKeyName)
if storeKeyHeight == 0 {
return true
}
return UpgradeMgr.GetHeight() >= storeKeyHeight
}
func ShouldSetStoreVersion(storeKeyName string) bool {
storeKeyHeight := UpgradeMgr.GetStoreKeyHeight(storeKeyName)
if storeKeyHeight == 0 {
return false
}
return UpgradeMgr.GetHeight() == storeKeyHeight
}
func IsMsgTypeSupported(msgType string) bool {
msgTypeHeight := UpgradeMgr.GetMsgTypeHeight(msgType)
if msgTypeHeight == 0 {
return true
}
return UpgradeMgr.GetHeight() >= msgTypeHeight
}
func Upgrade(name string, before func(), in func(), after func()) {
// if no special logic for the UpgradeHeight, apply the `after` logic
if in == nil {
in = after
}
if IsUpgradeHeight(name) {
if in != nil {
in()
}
} else if IsUpgrade(name) {
if after != nil {
after()
}
} else {
if before != nil {
before()
}
}
}