Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.7.10 upgrade handler fixes #33

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 117 additions & 55 deletions app/upgrades/v7_10/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,46 @@ import (
"encoding/base64"
"fmt"
"log"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"

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

sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

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

"github.com/ODIN-PROTOCOL/odin-core/app/keepers"
"github.com/ODIN-PROTOCOL/odin-core/app/upgrades"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// Old addresses
const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk"
const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk" // Prod
// const DefiantLabAccAddress = "odin1t6hn2c9hrc33fa5slh9wtvv4ew2qhygl0rmc4q"

// New addresses
const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
//const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"

// PubKeys
const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s="
const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s=" // Prod
//const OdinMainnet3ValPubKey = "f7pqqa+1Rkl+5j13R6iBnnKAR7bhNrOV8Cc0RfpSzjs=" // Test

const DefiantLabPubKey = "Aw22yXnDmYKzQ1CeHh6A+PD1043vsbSBH5FmuAWIlkS7" // Prod
// const DefiantLabPubKey = "A8gI+6AHMv9Tg37JyrxSP16hUH76Umr4krXfIEqOQJMo" // Test


func getBalance(
Expand All @@ -60,6 +67,30 @@ func getBalance(
}
}

func CreateNewAccount(ctx sdk.Context, authKeeper keeper.AccountKeeper, address sdk.AccAddress, secpPubKey []byte) error {
// Check if the account already exists
account := authKeeper.GetAccount(ctx, address)
if account != nil {
return fmt.Errorf("account %s already exists", address.String())
}

// Optionally, set any initial values or parameters for the new account
// For example, you might want to set an initial balance using the bank module
var pubkey cryptotypes.PubKey = &secp256k1.PubKey{Key: secpPubKey}

// Create a new account with the address and public key
newAccount := authKeeper.NewAccountWithAddress(ctx, address)
if newAccount == nil {
return fmt.Errorf("failed to create new account for address %s", address.String())
}

newAccount.SetPubKey(pubkey)
log.Printf("New account created %v: %v", address.String(), pubkey.String())

// Save the new account to the state
authKeeper.SetAccount(ctx, newAccount)
return nil
}

func getDelegations(
ctx sdk.Context,
Expand All @@ -70,8 +101,34 @@ func getDelegations(
return delegations
}

func InitializeValidatorSigningInfo(ctx sdk.Context, slashingKeeper slashingkeeper.Keeper, stakingKeeper stakingkeeper.Keeper, consAddr sdk.ConsAddress) error {
// Check if signing info already exists to avoid overwriting it
_, found := slashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
if !found {
startHeight := ctx.BlockHeight()
signingInfo := slashingtypes.NewValidatorSigningInfo(consAddr, startHeight, 0, time.Unix(0, 0), false, 0)
slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
}
return nil
}


func InitializeValidatorDistributionInfo(ctx sdk.Context, keepers *keepers.AppKeepers, validatorAddr sdk.ValAddress) {
// Initialize distribution information for the validator
// set initial historical rewards (period 0) with reference count of 1
keepers.DistrKeeper.SetValidatorHistoricalRewards(ctx, validatorAddr, 0, distrtypes.NewValidatorHistoricalRewards(sdk.DecCoins{}, 1))

// set current rewards (starting at period 1)
keepers.DistrKeeper.SetValidatorCurrentRewards(ctx, validatorAddr, distrtypes.NewValidatorCurrentRewards(sdk.DecCoins{}, 1))

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){
// set accumulated commission
keepers.DistrKeeper.SetValidatorAccumulatedCommission(ctx, validatorAddr, distrtypes.InitialValidatorAccumulatedCommission())

// set outstanding rewards
keepers.DistrKeeper.SetValidatorOutstandingRewards(ctx, validatorAddr, distrtypes.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
}

func createValidator(ctx sdk.Context, keeppers *keepers.AppKeepers, address string, pubKey cryptotypes.PubKey, description stakingtypes.Description, comission stakingtypes.Commission) (stakingtypes.Validator, error){

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

// Update validators in the store
sk.SetValidator(ctx, validator)
keeppers.StakingKeeper.SetValidator(ctx, validator)

consAddr := sdk.ConsAddress(pubKey.Address())
valconsAddr, err := validator.GetConsAddr()
if err != nil {
log.Printf("Error when converting validator consensus address to string: %s", err)
return stakingtypes.Validator{}, err
}

log.Printf("Created validator %v (%v:%v)", valAddr.String(), consAddr.String(), valconsAddr)

keeppers.StakingKeeper.SetValidatorByConsAddr(ctx, validator)
InitializeValidatorSigningInfo(ctx, keeppers.SlashingKeeper, *keeppers.StakingKeeper, consAddr)
InitializeValidatorDistributionInfo(ctx, keeppers, valAddr)

err = sk.Hooks().AfterValidatorCreated(ctx, valAddr)
err = keeppers.StakingKeeper.Hooks().AfterValidatorCreated(ctx, valAddr)
if err != nil {
return stakingtypes.Validator{}, err
}

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

k.AddValidatorTokensAndShares(ctx, newVal, tokens.TruncateInt())
k.RemoveValidatorTokensAndShares(ctx, oldVal, cumOldValShares)

k.SetValidatorByConsAddr(ctx, oldVal)
k.SetValidatorByConsAddr(ctx, newVal)

return nil
}


func moveDelegations(ctx sdk.Context, keepers *keepers.AppKeepers, oldAddress sdk.AccAddress, newVal stakingtypes.Validator) error {
for _, delegation := range getDelegations(ctx, *keepers.StakingKeeper, oldAddress) {
log.Printf("Moving delegation from %v to %v", delegation.DelegatorAddress, newVal.OperatorAddress)
Expand All @@ -201,13 +268,13 @@ func moveDelegations(ctx sdk.Context, keepers *keepers.AppKeepers, oldAddress sd
log.Printf("Error when running hook after adding delegation %v to %v", delegation.GetDelegatorAddr(), newVal.GetOperator())
return err
}
keepers.StakingKeeper.SetDelegation(ctx, newDelegation)


err = keepers.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delegation.GetDelegatorAddr(), newVal.GetOperator())
if err != nil {
log.Printf("Error when running hook after addig delegation %v to %v", delegation.GetDelegatorAddr(), newVal.GetOperator())
return err
}
keepers.StakingKeeper.SetDelegation(ctx, newDelegation)
}
return nil
}
Expand Down Expand Up @@ -262,40 +329,15 @@ func sendCoins(
}


func SelfDelegate(ctx sdk.Context, stakingKeeper stakingkeeper.Keeper, bankKeeper bankkeeper.Keeper, delegatorAddr sdk.AccAddress, validator stakingtypes.Validator, amount sdk.Coins) error {
// Delegate tokens to the validator
for _, balance := range amount {

// Ensure the delegator (validator account) has enough balance for the delegation
if !bankKeeper.HasBalance(ctx, delegatorAddr, balance) {
return sdkerrors.Wrapf(errortypes.ErrInsufficientFunds, "not enough balance to self-delegate to validator: %s", validator.OperatorAddress)
}

// Send coins from the delegator's account to the module account (staking module account) as part of delegation
err := bankKeeper.SendCoinsFromAccountToModule(ctx, delegatorAddr, stakingtypes.NotBondedPoolName, amount)
if err != nil {
return err
}

_, err = stakingKeeper.Delegate(ctx, delegatorAddr, balance.Amount, stakingtypes.Unbonded, validator, false)
if err != nil {
return err
}
}
return nil
}


func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {


// Fixing self delegation
DefiantLabsValAddress, err := addrToValAddr(DefiantLabAccAddress)
if err != nil {
return err
}

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

// Setting withdrawal address
PubKeyBytes, err := base64.StdEncoding.DecodeString(DefiantLabPubKey)
if err != nil {
log.Printf("Error whend decoding public key from string %v", err)
return err
}

err = CreateNewAccount(ctx, keepers.AccountKeeper, DefiantLabsAcc, PubKeyBytes)
if err != nil {
log.Printf("Error when creating new account for %v: %s", DefiantLabsAcc, err)
}

keepers.DistrKeeper.SetWithdrawAddr(ctx, DefiantLabsAcc, DefiantLabsAcc)

// sending balances
ctx.Logger().Info(fmt.Sprintf("Sending tokens from %s to %s", DefiantLabOldAccAddress, DefiantLabAccAddress))

// Moving DefiantLabs self-delegation
err = moveSelfDelegation(ctx, keepers, DefiantLabsOldAcc, DefiantLabsAcc, DefiantLabsValAddress)
balance, err := getBalance(ctx, *keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, DefiantLabsOldAcc)
if err != nil {
log.Printf("Error when moving self delegation %s", err)
log.Printf("Error when retrieving balance for address %s: %s", DefiantLabOldAccAddress, err)
return err
}

sendCoins(ctx, keepers.BankKeeper, DefiantLabsOldAcc, DefiantLabsAcc, balance)

// Moving delegations
moveDelegations(ctx, keepers, DefiantLabsOldAcc, DefiantLabsVal)
moveSelfDelegation(ctx, keepers, DefiantLabsOldAcc, DefiantLabsAcc, DefiantLabsValAddress)
moveDelegations(ctx, keepers, DefiantLabsOldAcc, DanVal)

DefiantLabsVal.Jailed = false
keepers.StakingKeeper.SetValidator(ctx, DefiantLabsVal)

return nil
}

Expand Down Expand Up @@ -382,7 +435,7 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
return err
}

Odin3Val, err := createValidator(ctx, *keepers.StakingKeeper, keepers.DistrKeeper, string(Odin3ValAddr), &Odin3PubKey, Odin3OldVal.Description, Odin3OldVal.Commission)
Odin3Val, err := createValidator(ctx, keepers, string(Odin3ValAddr), &Odin3PubKey, Odin3OldVal.Description, Odin3OldVal.Commission)
if err != nil {
return err
}
Expand All @@ -393,13 +446,22 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
if err != nil {
return err
}

moveSelfDelegation(ctx, keepers, OldMainnet3Addr, NewMainnet3Addr, Odin3ValAddr)
moveDelegations(ctx, keepers, sdk.AccAddress(OdinMainnet3OldAccAddress), Odin3Val)

Odin3Val.UpdateStatus(stakingtypes.Bonded)

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

// Showing all validator powers
log.Printf("Validator power after update:")
Expand Down
16 changes: 16 additions & 0 deletions convert_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys
import bech32

def convert_chain_address(prefix, address_string):
_, data = bech32.bech32_decode(address_string)
return bech32.bech32_encode(prefix, data)

if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: python convert_address.py <prefix> <address>")
sys.exit(1)

prefix = sys.argv[1]
address = sys.argv[2]

print(convert_chain_address(prefix, address))
Loading