Skip to content

Commit

Permalink
Merge pull request #104 from decentrio/intergrate-multistaking
Browse files Browse the repository at this point in the history
!feat: Intergrate multi-staking module
  • Loading branch information
jiujiteiro authored Feb 2, 2024
2 parents 798679a + d164330 commit 3230cf1
Show file tree
Hide file tree
Showing 12 changed files with 8,282 additions and 152 deletions.
98 changes: 43 additions & 55 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ import (
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/staking"

// staking
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

// multi-staking
multistaking "github.com/realio-tech/multi-staking-module/x/multi-staking"
multistakingkeeper "github.com/realio-tech/multi-staking-module/x/multi-staking/keeper"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"

evmante "github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/encoding"
srvflags "github.com/evmos/ethermint/server/flags"
Expand Down Expand Up @@ -141,7 +149,7 @@ var (
genutil.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
multistaking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(
Expand Down Expand Up @@ -177,6 +185,7 @@ var (
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner}, // used for secure addition and subtraction of balance using module account
assetmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
multistakingtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
// this line is used by starport scaffolding # stargate/app/maccPerms
}
)
Expand Down Expand Up @@ -217,22 +226,23 @@ type RealioNetwork struct {
memKeys map[string]*storetypes.MemoryStoreKey

// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
CrisisKeeper crisiskeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
MultiStakingKeeper multistakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
CrisisKeeper crisiskeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper

// Ethermint keepers
EvmKeeper *evmkeeper.Keeper
Expand Down Expand Up @@ -289,6 +299,8 @@ func New(
assetmoduletypes.StoreKey,
// ethermint keys
evmtypes.StoreKey, feemarkettypes.StoreKey,
// multi-staking keys
multistakingtypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)

Expand Down Expand Up @@ -376,6 +388,15 @@ func New(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

// multi-staking keeper
app.MultiStakingKeeper = *multistakingkeeper.NewKeeper(
appCodec,
app.AccountKeeper,
app.StakingKeeper,
app.BankKeeper,
keys[multistakingtypes.StoreKey],
)

// realio keeper
app.AssetKeeper = *assetmodulekeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -460,7 +481,7 @@ func New(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
multistaking.NewAppModule(appCodec, app.MultiStakingKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
params.NewAppModule(app.ParamsKeeper),
Expand Down Expand Up @@ -494,7 +515,7 @@ func New(
distrtypes.ModuleName,
slashingtypes.ModuleName,
evidencetypes.ModuleName,
stakingtypes.ModuleName,
multistakingtypes.ModuleName,
ibchost.ModuleName,
// no-op modules
ibctransfertypes.ModuleName,
Expand All @@ -516,7 +537,7 @@ func New(
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
govtypes.ModuleName,
stakingtypes.ModuleName,
multistakingtypes.ModuleName,
evmtypes.ModuleName,
feemarkettypes.ModuleName,
// no-op modules
Expand Down Expand Up @@ -550,7 +571,7 @@ func New(
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
stakingtypes.ModuleName,
multistakingtypes.ModuleName,
vestingtypes.ModuleName,
slashingtypes.ModuleName,
govtypes.ModuleName,
Expand Down Expand Up @@ -635,7 +656,7 @@ func New(

app.SetAnteHandler(ante.NewAnteHandler(options))

app.setupUpgradeHandlers()
app.setupUpgradeHandlers(appOpts)

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
Expand Down Expand Up @@ -851,36 +872,3 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino

return paramsKeeper
}

func (app *RealioNetwork) setupUpgradeHandlers() {
//// "v0.7.2" is a coordinated upgrade on testnet to upgrade sdk to v0.46.7
// planName := "v0.0.0"
// app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
//
// return app.mm.RunMigrations(ctx, app.configurator, fromVM)
// })
//
////// When a planned update height is reached, the old binary will panic
////// writing on disk the height and name of the update that triggered it
////// This will read that value, and execute the preparations for the upgrade.
// upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
// if err != nil {
// panic(fmt.Errorf("failed to read upgrade info from disk: %w", err))
//}
//
// if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
// return
//}
//
// var storeUpgrades *storetypes.StoreUpgrades
//
// switch upgradeInfo.Name {
// case planName:
// // no store upgrades here
//}
//
// if storeUpgrades != nil {
// // configure store loader that checks if version == upgradeHeight and applies store upgrades
// app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades))
//}
}
114 changes: 81 additions & 33 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"

"github.com/evmos/ethermint/encoding"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
Expand All @@ -40,22 +41,34 @@ var DefaultTestingAppInit func() (ibctesting.TestingApp, map[string]json.RawMess

// DefaultConsensusParams defines the default Tendermint consensus params used in
// Evmos testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 200000,
MaxGas: -1, // no limit
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
var (
DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 200000,
MaxGas: -1, // no limit
},
},
}
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
}
MultiStakingCoinA = multistakingtypes.MultiStakingCoin{
Denom: "ario",
Amount: sdk.NewIntFromUint64(1000000000000000000),
BondWeight: sdk.MustNewDecFromStr("1.23"),
}
MultiStakingCoinB = multistakingtypes.MultiStakingCoin{
Denom: "arst",
Amount: sdk.NewIntFromUint64(1000000000000000000),
BondWeight: sdk.MustNewDecFromStr("0.12"),
}
)

func init() {
feemarkettypes.DefaultMinGasPrice = sdk.ZeroDec()
Expand Down Expand Up @@ -128,58 +141,93 @@ func GenesisStateWithValSet(app *RealioNetwork, genesisState simapp.GenesisState
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)

// set multi staking genesis state
msCoinAInfo := multistakingtypes.MultiStakingCoinInfo{
Denom: MultiStakingCoinA.Denom,
BondWeight: MultiStakingCoinA.BondWeight,
}
msCoinBInfo := multistakingtypes.MultiStakingCoinInfo{
Denom: MultiStakingCoinB.Denom,
BondWeight: MultiStakingCoinB.BondWeight,
}
msCoinInfos := []multistakingtypes.MultiStakingCoinInfo{msCoinAInfo, msCoinBInfo}
validatorMsCoins := make([]multistakingtypes.ValidatorMultiStakingCoin, 0, len(valSet.Validators))
locks := make([]multistakingtypes.MultiStakingLock, 0, len(valSet.Validators))
lockCoins := sdk.NewCoins()

validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
bondCoins := sdk.NewCoins()

for i, val := range valSet.Validators {
valMsCoin := MultiStakingCoinA
if i%2 == 1 {
valMsCoin = MultiStakingCoinB
}

validatorMsCoins = append(validatorMsCoins, multistakingtypes.ValidatorMultiStakingCoin{
ValAddr: sdk.ValAddress(val.Address).String(),
CoinDenom: valMsCoin.Denom,
})

lockId := multistakingtypes.MultiStakingLockID(genAccs[0].GetAddress().String(), sdk.ValAddress(val.Address).String())

Check warning on line 173 in app/test_helpers.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var lockId should be lockID (revive)
lockRecord := multistakingtypes.NewMultiStakingLock(lockId, valMsCoin)

bondAmt := sdk.DefaultPowerReduction
locks = append(locks, lockRecord)
lockCoins = lockCoins.Add(valMsCoin.ToCoin())

for _, val := range valSet.Validators {
pk, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey)
pkAny, _ := codectypes.NewAnyWithValue(pk)
validator := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(val.Address).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: bondAmt,
Tokens: valMsCoin.BondValue(),
DelegatorShares: sdk.OneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.ZeroInt(),
}

validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))

bondCoins = bondCoins.Add(sdk.NewCoin(sdk.DefaultBondDenom, valMsCoin.BondValue()))
}
// set validators and delegations
stakingparams := stakingtypes.DefaultParams()
stakingparams.BondDenom = types.BaseDenom
stakingGenesis := stakingtypes.NewGenesisState(stakingparams, validators, delegations)
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)

multistakingGenesis := multistakingtypes.GenesisState{
MultiStakingLocks: locks,
MultiStakingUnlocks: []multistakingtypes.MultiStakingUnlock{},
MultiStakingCoinInfo: msCoinInfos,
ValidatorMultiStakingCoins: validatorMsCoins,
StakingGenesisState: *stakingGenesis,
}
genesisState[multistakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&multistakingGenesis)

// set mint genesis
mintGenesis := minttypes.DefaultGenesisState()
genesisState[minttypes.ModuleName] = app.AppCodec().MustMarshalJSON(mintGenesis)

balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: bondCoins,
})
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(multistakingtypes.ModuleName).String(),
Coins: lockCoins,
})

totalSupply := sdk.NewCoins()
for _, b := range balances {
// add genesis acc tokens to total supply
totalSupply = totalSupply.Add(b.Coins...)
}

for range delegations {
// add delegated tokens to total supply
totalSupply = totalSupply.Add(sdk.NewCoin(types.BaseDenom, bondAmt))
}

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(types.BaseDenom, bondAmt)},
})

// update total supply
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)
Expand Down
Loading

0 comments on commit 3230cf1

Please sign in to comment.