Skip to content

Commit

Permalink
Testing changes after roll-back
Browse files Browse the repository at this point in the history
  • Loading branch information
Satchitananda committed Feb 10, 2024
1 parent 4a15708 commit a73667e
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 46 deletions.
191 changes: 145 additions & 46 deletions app/upgrades/v7_10/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"log"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -23,20 +24,24 @@ import (
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"

// New addresses
const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
// Prod: const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk"
const DefiantLabAccAddress = "odin1t6hn2c9hrc33fa5slh9wtvv4ew2qhygl0rmc4q"

//const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"

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


func getBalance(
Expand All @@ -60,6 +65,28 @@ func getBalance(
}
}

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

// Create a new account with the address
newAccount := authKeeper.NewAccountWithAddress(ctx, address)

log.Printf("New account created %v: %v", address.String(), newAccount.GetAddress().String())
if newAccount == nil {
return fmt.Errorf("failed to create new account for address %s", 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

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

func getDelegations(
ctx sdk.Context,
Expand All @@ -70,8 +97,35 @@ 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
// This might involve setting up initial values for the validator's historical rewards, current rewards, and so on.

initialRewards := distrtypes.NewValidatorHistoricalRewards(sdk.NewDecCoins(sdk.NewDecCoin("loki", sdk.ZeroInt())), 0)
keepers.DistrKeeper.SetValidatorHistoricalRewards(ctx, validatorAddr, 0, initialRewards)

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){
// Initialize Validator Current Rewards
keepers.DistrKeeper.SetValidatorCurrentRewards(ctx, validatorAddr, distrtypes.NewValidatorCurrentRewards(sdk.NewDecCoins(sdk.NewDecCoin("loki", sdk.ZeroInt())), 0))

// Initialize Validator Accumulated Commission (if necessary)
keepers.DistrKeeper.SetValidatorAccumulatedCommission(ctx, validatorAddr, distrtypes.InitialValidatorAccumulatedCommission())

// Note: The actual initialization might require more or different steps depending on your version of the Cosmos SDK.
}

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 +144,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 +245,18 @@ 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, k stakingkeeper.Keeper, oldAddress sdk.AccAddress, newVal stakingtypes.Validator) {
// for _, delegation := range getDelegations(ctx, k, oldAddress) {
// delegation.ValidatorAddress = newVal.OperatorAddress
// log.Printf("Moving delegation from %v to %v", delegation.DelegatorAddress, newVal.GetMoniker())
// k.SetDelegation(ctx, delegation)
// }
// }


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 Down Expand Up @@ -262,40 +334,36 @@ 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
}
func SelfDelegate(ctx sdk.Context, stakingKeeper stakingkeeper.Keeper, bankKeeper bankkeeper.Keeper, delegatorAddr sdk.AccAddress, validator stakingtypes.Validator, amount sdk.Coin) error {
// Ensure the delegator (validator account) has enough balance for the delegation
if !bankKeeper.HasBalance(ctx, delegatorAddr, amount) {
return sdkerrors.Wrapf(errortypes.ErrInsufficientFunds, "not enough balance to self-delegate to validator: %s", validator.OperatorAddress)
}

_, err = stakingKeeper.Delegate(ctx, delegatorAddr, balance.Amount, stakingtypes.Unbonded, validator, false)
if err != nil {
return err
}
}
// 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, sdk.NewCoins(amount))
if err != nil {
return err
}

// Delegate tokens to the validator
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, amount.Amount, stakingtypes.Unbonded, validator, true)
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 +381,46 @@ func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
return err
}

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

// Moving DefiantLabs self-delegation
err = moveSelfDelegation(ctx, keepers, DefiantLabsOldAcc, DefiantLabsAcc, DefiantLabsValAddress)
// Donating 10K loki to DefiantLab to automate his self-delegation process

OdinMainnet3DonorAddr, err := sdk.AccAddressFromBech32(OdinMainnet3OldAccAddress)
if err != nil {
log.Printf("Error when moving self delegation %s", err)
log.Printf("account address is not valid bech32: %s: %s", OdinMainnet3OldAccAddress, err)
return err
}

log.Printf("Donating coins for self-delegation %v", DefiantLabsValAddress)
sendCoins(ctx, keepers.BankKeeper, OdinMainnet3DonorAddr, DefiantLabsAcc, sdk.NewCoins(sdk.NewCoin("loki", sdk.NewInt(10000))))

// Moving delegations
moveDelegations(ctx, keepers, DefiantLabsOldAcc, DefiantLabsVal)
keepers.DistrKeeper.SetWithdrawAddr(ctx, DefiantLabsAcc, DefiantLabsAcc)

DefiantLabsVal.Jailed = false
keepers.StakingKeeper.SetValidator(ctx, DefiantLabsVal)
// sending balances
ctx.Logger().Info(fmt.Sprintf("Sending tokens from %s to %s", DefiantLabOldAccAddress, DefiantLabAccAddress))

balance, err := getBalance(ctx, *keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, DefiantLabsOldAcc)
if err != nil {
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, DanVal)

// Self delegating to new validator
selfDel := keepers.StakingKeeper.Delegation(ctx, DefiantLabsAcc, DefiantLabsValAddress)
if selfDel == nil {
err := SelfDelegate(ctx, *keepers.StakingKeeper, keepers.BankKeeper, DefiantLabsAcc, DanVal, sdk.NewCoin("loki", math.NewInt(1000)))
if err != nil {
log.Printf("Error when self delegating to %v", DefiantLabsValAddress)
return err
}
}
return nil
}

Expand Down Expand Up @@ -382,7 +474,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 @@ -399,7 +491,14 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {

// 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))

0 comments on commit a73667e

Please sign in to comment.