-
Notifications
You must be signed in to change notification settings - Fork 182
/
keeper.go
166 lines (144 loc) · 5.25 KB
/
keeper.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
package proto
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// ProtocolDefinition is the struct of app-upgrade detail info
type ProtocolDefinition struct {
Version uint64 `json:"version"`
Software string `json:"software"`
Height uint64 `json:"height"`
Threshold sdk.Dec `json:"threshold"`
}
// NewProtocolDefinition creates a new instance of ProtocolDefinition
func NewProtocolDefinition(version uint64, software string, height uint64, threshold sdk.Dec) ProtocolDefinition {
return ProtocolDefinition{
version,
software,
height,
threshold,
}
}
// AppUpgradeConfig is the struct of app-upgrade-specific params
type AppUpgradeConfig struct {
ProposalID uint64 `json:"proposal_id"`
ProtocolDef ProtocolDefinition `json:"protocol_def"`
}
// NewAppUpgradeConfig creates a new instance of AppUpgradeConfig
func NewAppUpgradeConfig(proposalID uint64, protocolDef ProtocolDefinition) AppUpgradeConfig {
return AppUpgradeConfig{
proposalID,
protocolDef,
}
}
// DefaultUpgradeConfig returns a default AppUpgradeConfig object
func DefaultUpgradeConfig(software string) AppUpgradeConfig {
return AppUpgradeConfig{
ProposalID: uint64(0),
ProtocolDef: NewProtocolDefinition(uint64(0), software, uint64(1), sdk.NewDecWithPrec(9, 1)),
}
}
// VersionKeeper shows the expected behaviour of a version keeper
type VersionKeeper interface {
GetCurrentVersionByStore(store sdk.KVStore) uint64
GetUpgradeConfigByStore(store sdk.KVStore) (upgradeConfig AppUpgradeConfig, found bool)
}
// ProtocolKeeper is designed for a protocol controller
type ProtocolKeeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
}
// NewProtocolKeeper creates a new instance of ProtocolKeeper
func NewProtocolKeeper(key sdk.StoreKey) ProtocolKeeper {
return ProtocolKeeper{key, cdc}
}
// GetCurrentVersionByStore gets the current version of protocol from store
func (pk ProtocolKeeper) GetCurrentVersionByStore(store sdk.KVStore) uint64 {
bz := store.Get(currentVersionKey)
if bz == nil {
return 0
}
var currentVersion uint64
pk.cdc.MustUnmarshalBinaryLengthPrefixed(bz, ¤tVersion)
return currentVersion
}
// GetCurrentVersion gets the current version from context
func (pk ProtocolKeeper) GetCurrentVersion(ctx sdk.Context) uint64 {
store := ctx.KVStore(pk.storeKey)
return pk.GetCurrentVersionByStore(store)
}
// GetUpgradeConfigByStore gets the upgrade config from store
func (pk ProtocolKeeper) GetUpgradeConfigByStore(store sdk.KVStore) (upgradeConfig AppUpgradeConfig, found bool) {
bz := store.Get(upgradeConfigKey)
if bz == nil {
return upgradeConfig, false
}
pk.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &upgradeConfig)
return upgradeConfig, true
}
// SetCurrentVersion sets current version
func (pk ProtocolKeeper) SetCurrentVersion(ctx sdk.Context, currentVersion uint64) {
store := ctx.KVStore(pk.storeKey)
bz := pk.cdc.MustMarshalBinaryLengthPrefixed(currentVersion)
store.Set(currentVersionKey, bz)
}
// GetLastFailedVersion gets last failed version
func (pk ProtocolKeeper) GetLastFailedVersion(ctx sdk.Context) uint64 {
store := ctx.KVStore(pk.storeKey)
bz := store.Get(lastFailedVersionKey)
if bz == nil {
return 0 // default value
}
var lastFailedVersion uint64
pk.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &lastFailedVersion)
return lastFailedVersion
}
// SetLastFailedVersion sets last failed version
func (pk ProtocolKeeper) SetLastFailedVersion(ctx sdk.Context, lastFailedVersion uint64) {
store := ctx.KVStore(pk.storeKey)
bz := pk.cdc.MustMarshalBinaryLengthPrefixed(lastFailedVersion)
store.Set(lastFailedVersionKey, bz)
}
// GetUpgradeConfig gets upgrade config
func (pk ProtocolKeeper) GetUpgradeConfig(ctx sdk.Context) (upgradeConfig AppUpgradeConfig, found bool) {
store := ctx.KVStore(pk.storeKey)
bz := store.Get(upgradeConfigKey)
if bz == nil {
return upgradeConfig, false
}
pk.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &upgradeConfig)
return upgradeConfig, true
}
// SetUpgradeConfig sets upgrade config
func (pk ProtocolKeeper) SetUpgradeConfig(ctx sdk.Context, upgradeConfig AppUpgradeConfig) {
store := ctx.KVStore(pk.storeKey)
bz := pk.cdc.MustMarshalBinaryLengthPrefixed(upgradeConfig)
store.Set(upgradeConfigKey, bz)
}
// ClearUpgradeConfig removes the upgrade config in the store
func (pk ProtocolKeeper) ClearUpgradeConfig(ctx sdk.Context) {
store := ctx.KVStore(pk.storeKey)
store.Delete(upgradeConfigKey)
}
// IsValidVersion checks whether the version is available
func (pk ProtocolKeeper) IsValidVersion(ctx sdk.Context, version uint64) bool {
currentVersion := pk.GetCurrentVersion(ctx)
lastFailedVersion := pk.GetLastFailedVersion(ctx)
return isValidVersion(currentVersion, lastFailedVersion, version)
}
// rule: new version should be currentVersion+1 or lastFailedVersion or lastFailedVersion+1
func isValidVersion(currentVersion uint64, lastFailedVersion uint64, version uint64) bool {
if currentVersion >= lastFailedVersion {
return currentVersion+1 == version
}
return lastFailedVersion == version || lastFailedVersion+1 == version
}
// String returns a human readable string representation of AppUpgradeConfig
func (auc AppUpgradeConfig) String() string {
return fmt.Sprintf(`AppUpgradeConfig:
ProposalID: %d
ProtocolDefinition: %v
`,
auc.ProposalID, auc.ProtocolDef)
}