Skip to content

Commit 582fc5c

Browse files
Merge pull request #33 from ODIN-PROTOCOL/v0.7.10-upgrade-handler-fixes
V0.7.10 upgrade handler fixes
2 parents adf36a0 + 6df6699 commit 582fc5c

File tree

2 files changed

+133
-55
lines changed

2 files changed

+133
-55
lines changed

app/upgrades/v7_10/upgrades.go

Lines changed: 117 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,46 @@ import (
44
"encoding/base64"
55
"fmt"
66
"log"
7+
"time"
78

89
sdk "github.com/cosmos/cosmos-sdk/types"
910
"github.com/cosmos/cosmos-sdk/types/module"
1011
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
1112

1213
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
1314

14-
sdkerrors "cosmossdk.io/errors"
1515
"cosmossdk.io/math"
1616
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
17+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1718

1819
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1920

2021
"github.com/ODIN-PROTOCOL/odin-core/app/keepers"
2122
"github.com/ODIN-PROTOCOL/odin-core/app/upgrades"
22-
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
2323
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
2424
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
2525
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
26+
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
27+
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
28+
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
2629
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
2730
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2831
)
2932

30-
// Old addresses
31-
const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk"
32-
const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
33+
const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
34+
const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk" // Prod
35+
// const DefiantLabAccAddress = "odin1t6hn2c9hrc33fa5slh9wtvv4ew2qhygl0rmc4q"
3336

34-
// New addresses
35-
const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
37+
//const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
38+
const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
3639
const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
3740

3841
// PubKeys
39-
const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s="
42+
const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s=" // Prod
43+
//const OdinMainnet3ValPubKey = "f7pqqa+1Rkl+5j13R6iBnnKAR7bhNrOV8Cc0RfpSzjs=" // Test
44+
45+
const DefiantLabPubKey = "Aw22yXnDmYKzQ1CeHh6A+PD1043vsbSBH5FmuAWIlkS7" // Prod
46+
// const DefiantLabPubKey = "A8gI+6AHMv9Tg37JyrxSP16hUH76Umr4krXfIEqOQJMo" // Test
4047

4148

4249
func getBalance(
@@ -60,6 +67,30 @@ func getBalance(
6067
}
6168
}
6269

70+
func CreateNewAccount(ctx sdk.Context, authKeeper keeper.AccountKeeper, address sdk.AccAddress, secpPubKey []byte) error {
71+
// Check if the account already exists
72+
account := authKeeper.GetAccount(ctx, address)
73+
if account != nil {
74+
return fmt.Errorf("account %s already exists", address.String())
75+
}
76+
77+
// Optionally, set any initial values or parameters for the new account
78+
// For example, you might want to set an initial balance using the bank module
79+
var pubkey cryptotypes.PubKey = &secp256k1.PubKey{Key: secpPubKey}
80+
81+
// Create a new account with the address and public key
82+
newAccount := authKeeper.NewAccountWithAddress(ctx, address)
83+
if newAccount == nil {
84+
return fmt.Errorf("failed to create new account for address %s", address.String())
85+
}
86+
87+
newAccount.SetPubKey(pubkey)
88+
log.Printf("New account created %v: %v", address.String(), pubkey.String())
89+
90+
// Save the new account to the state
91+
authKeeper.SetAccount(ctx, newAccount)
92+
return nil
93+
}
6394

6495
func getDelegations(
6596
ctx sdk.Context,
@@ -70,8 +101,34 @@ func getDelegations(
70101
return delegations
71102
}
72103

104+
func InitializeValidatorSigningInfo(ctx sdk.Context, slashingKeeper slashingkeeper.Keeper, stakingKeeper stakingkeeper.Keeper, consAddr sdk.ConsAddress) error {
105+
// Check if signing info already exists to avoid overwriting it
106+
_, found := slashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
107+
if !found {
108+
startHeight := ctx.BlockHeight()
109+
signingInfo := slashingtypes.NewValidatorSigningInfo(consAddr, startHeight, 0, time.Unix(0, 0), false, 0)
110+
slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
111+
}
112+
return nil
113+
}
114+
115+
116+
func InitializeValidatorDistributionInfo(ctx sdk.Context, keepers *keepers.AppKeepers, validatorAddr sdk.ValAddress) {
117+
// Initialize distribution information for the validator
118+
// set initial historical rewards (period 0) with reference count of 1
119+
keepers.DistrKeeper.SetValidatorHistoricalRewards(ctx, validatorAddr, 0, distrtypes.NewValidatorHistoricalRewards(sdk.DecCoins{}, 1))
120+
121+
// set current rewards (starting at period 1)
122+
keepers.DistrKeeper.SetValidatorCurrentRewards(ctx, validatorAddr, distrtypes.NewValidatorCurrentRewards(sdk.DecCoins{}, 1))
73123

74-
func createValidator(ctx sdk.Context, sk stakingkeeper.Keeper, dk distributionkeeper.Keeper, address string, pubKey cryptotypes.PubKey, description stakingtypes.Description, comission stakingtypes.Commission) (stakingtypes.Validator, error){
124+
// set accumulated commission
125+
keepers.DistrKeeper.SetValidatorAccumulatedCommission(ctx, validatorAddr, distrtypes.InitialValidatorAccumulatedCommission())
126+
127+
// set outstanding rewards
128+
keepers.DistrKeeper.SetValidatorOutstandingRewards(ctx, validatorAddr, distrtypes.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
129+
}
130+
131+
func createValidator(ctx sdk.Context, keeppers *keepers.AppKeepers, address string, pubKey cryptotypes.PubKey, description stakingtypes.Description, comission stakingtypes.Commission) (stakingtypes.Validator, error){
75132

76133
valAddr := sdk.ValAddress(address)
77134
minSelfDelegation := sdk.OneInt()
@@ -90,14 +147,27 @@ func createValidator(ctx sdk.Context, sk stakingkeeper.Keeper, dk distributionke
90147
validator.Commission = comission
91148

92149
// Update validators in the store
93-
sk.SetValidator(ctx, validator)
150+
keeppers.StakingKeeper.SetValidator(ctx, validator)
151+
152+
consAddr := sdk.ConsAddress(pubKey.Address())
153+
valconsAddr, err := validator.GetConsAddr()
154+
if err != nil {
155+
log.Printf("Error when converting validator consensus address to string: %s", err)
156+
return stakingtypes.Validator{}, err
157+
}
158+
159+
log.Printf("Created validator %v (%v:%v)", valAddr.String(), consAddr.String(), valconsAddr)
160+
161+
keeppers.StakingKeeper.SetValidatorByConsAddr(ctx, validator)
162+
InitializeValidatorSigningInfo(ctx, keeppers.SlashingKeeper, *keeppers.StakingKeeper, consAddr)
163+
InitializeValidatorDistributionInfo(ctx, keeppers, valAddr)
94164

95-
err = sk.Hooks().AfterValidatorCreated(ctx, valAddr)
165+
err = keeppers.StakingKeeper.Hooks().AfterValidatorCreated(ctx, valAddr)
96166
if err != nil {
97167
return stakingtypes.Validator{}, err
98168
}
99169

100-
err = dk.Hooks().AfterValidatorCreated(ctx, valAddr)
170+
err = keeppers.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valAddr)
101171
if err != nil {
102172
return stakingtypes.Validator{}, err
103173
}
@@ -178,13 +248,10 @@ func moveValidatorDelegations(ctx sdk.Context, k stakingkeeper.Keeper, d distrib
178248

179249
k.AddValidatorTokensAndShares(ctx, newVal, tokens.TruncateInt())
180250
k.RemoveValidatorTokensAndShares(ctx, oldVal, cumOldValShares)
181-
182-
k.SetValidatorByConsAddr(ctx, oldVal)
183-
k.SetValidatorByConsAddr(ctx, newVal)
184-
185251
return nil
186252
}
187253

254+
188255
func moveDelegations(ctx sdk.Context, keepers *keepers.AppKeepers, oldAddress sdk.AccAddress, newVal stakingtypes.Validator) error {
189256
for _, delegation := range getDelegations(ctx, *keepers.StakingKeeper, oldAddress) {
190257
log.Printf("Moving delegation from %v to %v", delegation.DelegatorAddress, newVal.OperatorAddress)
@@ -201,13 +268,13 @@ func moveDelegations(ctx sdk.Context, keepers *keepers.AppKeepers, oldAddress sd
201268
log.Printf("Error when running hook after adding delegation %v to %v", delegation.GetDelegatorAddr(), newVal.GetOperator())
202269
return err
203270
}
204-
keepers.StakingKeeper.SetDelegation(ctx, newDelegation)
205-
271+
206272
err = keepers.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delegation.GetDelegatorAddr(), newVal.GetOperator())
207273
if err != nil {
208274
log.Printf("Error when running hook after addig delegation %v to %v", delegation.GetDelegatorAddr(), newVal.GetOperator())
209275
return err
210276
}
277+
keepers.StakingKeeper.SetDelegation(ctx, newDelegation)
211278
}
212279
return nil
213280
}
@@ -262,40 +329,15 @@ func sendCoins(
262329
}
263330

264331

265-
func SelfDelegate(ctx sdk.Context, stakingKeeper stakingkeeper.Keeper, bankKeeper bankkeeper.Keeper, delegatorAddr sdk.AccAddress, validator stakingtypes.Validator, amount sdk.Coins) error {
266-
// Delegate tokens to the validator
267-
for _, balance := range amount {
268-
269-
// Ensure the delegator (validator account) has enough balance for the delegation
270-
if !bankKeeper.HasBalance(ctx, delegatorAddr, balance) {
271-
return sdkerrors.Wrapf(errortypes.ErrInsufficientFunds, "not enough balance to self-delegate to validator: %s", validator.OperatorAddress)
272-
}
273-
274-
// Send coins from the delegator's account to the module account (staking module account) as part of delegation
275-
err := bankKeeper.SendCoinsFromAccountToModule(ctx, delegatorAddr, stakingtypes.NotBondedPoolName, amount)
276-
if err != nil {
277-
return err
278-
}
279-
280-
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, balance.Amount, stakingtypes.Unbonded, validator, false)
281-
if err != nil {
282-
return err
283-
}
284-
}
285-
return nil
286-
}
287-
288-
289332
func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
290-
291333

292334
// Fixing self delegation
293335
DefiantLabsValAddress, err := addrToValAddr(DefiantLabAccAddress)
294336
if err != nil {
295337
return err
296338
}
297339

298-
DefiantLabsVal, found := keepers.StakingKeeper.GetValidator(ctx, DefiantLabsValAddress)
340+
DanVal, found := keepers.StakingKeeper.GetValidator(ctx, DefiantLabsValAddress)
299341
if !found {
300342
log.Printf("Validator with %v has not been found", DefiantLabsValAddress)
301343
return err
@@ -313,22 +355,33 @@ func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
313355
return err
314356
}
315357

316-
// Setting withdrawal address
358+
PubKeyBytes, err := base64.StdEncoding.DecodeString(DefiantLabPubKey)
359+
if err != nil {
360+
log.Printf("Error whend decoding public key from string %v", err)
361+
return err
362+
}
363+
364+
err = CreateNewAccount(ctx, keepers.AccountKeeper, DefiantLabsAcc, PubKeyBytes)
365+
if err != nil {
366+
log.Printf("Error when creating new account for %v: %s", DefiantLabsAcc, err)
367+
}
368+
317369
keepers.DistrKeeper.SetWithdrawAddr(ctx, DefiantLabsAcc, DefiantLabsAcc)
370+
371+
// sending balances
372+
ctx.Logger().Info(fmt.Sprintf("Sending tokens from %s to %s", DefiantLabOldAccAddress, DefiantLabAccAddress))
318373

319-
// Moving DefiantLabs self-delegation
320-
err = moveSelfDelegation(ctx, keepers, DefiantLabsOldAcc, DefiantLabsAcc, DefiantLabsValAddress)
374+
balance, err := getBalance(ctx, *keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, DefiantLabsOldAcc)
321375
if err != nil {
322-
log.Printf("Error when moving self delegation %s", err)
376+
log.Printf("Error when retrieving balance for address %s: %s", DefiantLabOldAccAddress, err)
323377
return err
324378
}
325-
379+
sendCoins(ctx, keepers.BankKeeper, DefiantLabsOldAcc, DefiantLabsAcc, balance)
380+
326381
// Moving delegations
327-
moveDelegations(ctx, keepers, DefiantLabsOldAcc, DefiantLabsVal)
382+
moveSelfDelegation(ctx, keepers, DefiantLabsOldAcc, DefiantLabsAcc, DefiantLabsValAddress)
383+
moveDelegations(ctx, keepers, DefiantLabsOldAcc, DanVal)
328384

329-
DefiantLabsVal.Jailed = false
330-
keepers.StakingKeeper.SetValidator(ctx, DefiantLabsVal)
331-
332385
return nil
333386
}
334387

@@ -382,7 +435,7 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
382435
return err
383436
}
384437

385-
Odin3Val, err := createValidator(ctx, *keepers.StakingKeeper, keepers.DistrKeeper, string(Odin3ValAddr), &Odin3PubKey, Odin3OldVal.Description, Odin3OldVal.Commission)
438+
Odin3Val, err := createValidator(ctx, keepers, string(Odin3ValAddr), &Odin3PubKey, Odin3OldVal.Description, Odin3OldVal.Commission)
386439
if err != nil {
387440
return err
388441
}
@@ -393,13 +446,22 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
393446
if err != nil {
394447
return err
395448
}
449+
450+
moveSelfDelegation(ctx, keepers, OldMainnet3Addr, NewMainnet3Addr, Odin3ValAddr)
396451
moveDelegations(ctx, keepers, sdk.AccAddress(OdinMainnet3OldAccAddress), Odin3Val)
397452

398453
Odin3Val.UpdateStatus(stakingtypes.Bonded)
399454

400455
// rewards and comission
401456
withdrawRewardsAndCommission(ctx, *keepers.StakingKeeper, keepers.DistrKeeper, Odin3OldValAddress, Odin3ValAddr)
402-
Odin3OldVal.UpdateStatus(stakingtypes.Unbonded)
457+
ctx.Logger().Info(fmt.Sprintf("Sending tokens from %s to %s", OldMainnet3Addr, NewMainnet3Addr))
458+
balance, err = getBalance(ctx, *keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, OldMainnet3Addr)
459+
if err != nil {
460+
log.Printf("Error when retrieving balance for address %s: %s", OldMainnet3Addr, err)
461+
return err
462+
}
463+
// sending balances
464+
sendCoins(ctx, keepers.BankKeeper, OldMainnet3Addr, NewMainnet3Addr, balance)
403465

404466
// Showing all validator powers
405467
log.Printf("Validator power after update:")

convert_address.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
import bech32
3+
4+
def convert_chain_address(prefix, address_string):
5+
_, data = bech32.bech32_decode(address_string)
6+
return bech32.bech32_encode(prefix, data)
7+
8+
if __name__ == '__main__':
9+
if len(sys.argv) != 3:
10+
print("Usage: python convert_address.py <prefix> <address>")
11+
sys.exit(1)
12+
13+
prefix = sys.argv[1]
14+
address = sys.argv[2]
15+
16+
print(convert_chain_address(prefix, address))

0 commit comments

Comments
 (0)