-
Notifications
You must be signed in to change notification settings - Fork 175
/
upgrade_handler.go
137 lines (114 loc) · 5.1 KB
/
upgrade_handler.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
package unity
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)
// Address of the account which will have all juno sent to the unity proposal
var addressesToBeAdjusted = []string{
"juno1aeh8gqu9wr4u8ev6edlgfq03rcy6v5twfn0ja8",
}
// UnityContractByteAddress is the bytes of the public key for the address of the Unity contract
// $ junod keys parse juno1nz96hjc926e6a74gyvkwvtt0qhu22wx049c6ph6f4q8kp3ffm9xq5938mr
// human: juno
// bytes: 5BEF9E5318ED6716A11179C70B06656E9FB91D241A1C594F344B325D9110D94C
const UnityContractByteAddress = "5BEF9E5318ED6716A11179C70B06656E9FB91D241A1C594F344B325D9110D94C"
// ClawbackCoinFromAccount undelegate all amount in adjusted address (bypass 28 day unbonding), and send it to dead address
func ClawbackCoinFromAccount(ctx sdk.Context, accAddr sdk.AccAddress, staking *stakingkeeper.Keeper, bank *bankkeeper.BaseKeeper) {
bondDenom := staking.BondDenom(ctx)
now := ctx.BlockHeader().Time
// this loop will complete all delegator's active redelegations
for _, activeRedelegation:= range staking.GetRedelegations(ctx, accAddr, 65535) {
// src/dest validator addresses of this redelegation
redelegationSrc, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorSrcAddress)
redelegationDst, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorDstAddress)
// set all entry completionTime to now so we can complete redelegation
for i := range activeRedelegation.Entries {
activeRedelegation.Entries[i].CompletionTime = now
}
staking.SetRedelegation(ctx, activeRedelegation)
_, err := staking.CompleteRedelegation(ctx, accAddr, redelegationSrc, redelegationDst)
if err != nil {
panic(err)
}
}
// this loop will turn all delegator's delegations into unbonding delegations
for _, delegation := range staking.GetAllDelegatorDelegations(ctx, accAddr) {
validatorValAddr := delegation.GetValidatorAddr()
_, found := staking.GetValidator(ctx, validatorValAddr)
if !found {
continue
}
_, err := staking.Undelegate(ctx, accAddr, validatorValAddr, delegation.GetShares()) //nolint:errcheck // nolint because otherwise we'd have a time and nothing to do with it.
if err != nil {
panic(err)
}
}
// this loop will complete all delegator's unbonding delegations
for _, unbondingDelegation := range staking.GetAllUnbondingDelegations(ctx, accAddr) {
// validator address of this unbonding delegation
validatorStringAddr := unbondingDelegation.ValidatorAddress
validatorValAddr, _ := sdk.ValAddressFromBech32(validatorStringAddr)
// set all entry completionTime to now so we can complete unbonding delegation
for i := range unbondingDelegation.Entries {
unbondingDelegation.Entries[i].CompletionTime = now
}
staking.SetUnbondingDelegation(ctx, unbondingDelegation)
_, err := staking.CompleteUnbonding(ctx, accAddr, validatorValAddr)
if err != nil {
panic(err)
}
}
// account balance after finishing unbonding
accCoin := bank.GetBalance(ctx, accAddr, bondDenom)
// get Unity Contract Address and send coin to this address
destAcc, _ := sdk.AccAddressFromHex(UnityContractByteAddress)
err := bank.SendCoins(ctx, accAddr, destAcc, sdk.NewCoins(accCoin))
if err != nil {
panic(err)
}
}
// CreateUpgradeHandler make upgrade handler
func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, staking *stakingkeeper.Keeper, bank *bankkeeper.BaseKeeper) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
for _, addrString := range addressesToBeAdjusted {
accAddr, _ := sdk.AccAddressFromBech32(addrString)
ClawbackCoinFromAccount(ctx, accAddr, staking, bank)
}
// force an update of validator min commission
// we already did this for moneta
// but validators could have snuck in changes in the
// interim
// and via state sync to post-moneta
validators := staking.GetAllValidators(ctx)
// hard code this because we don't want
// a) a fork or
// b) immediate reaction with additional gov props
minCommissionRate := sdk.NewDecWithPrec(5, 2)
for _, v := range validators {
if v.Commission.Rate.LT(minCommissionRate) {
if v.Commission.MaxRate.LT(minCommissionRate) {
v.Commission.MaxRate = minCommissionRate
}
v.Commission.Rate = minCommissionRate
v.Commission.UpdateTime = ctx.BlockHeader().Time
// call the before-modification hook since we're about to update the commission
staking.BeforeValidatorModified(ctx, v.GetOperator())
staking.SetValidator(ctx, v)
}
}
// Set wasm old version to 1 if we want to call wasm's InitGenesis ourselves
// in this upgrade logic ourselves
// vm[wasm.ModuleName] = wasm.ConsensusVersion
// otherwise we run this, which will run wasm.InitGenesis(wasm.DefaultGenesis())
// and then override it after
newVM, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return newVM, err
}
// override here
return newVM, err
}
}