mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
f757d7ab15
* Update cosmos-sdk to v0.45.10-kava * Add RegisterNodeService to app * Update cosmos proto files * Update cosmos proto files * Use tagged v0.45.10-kava-v0.19-0.21 cosmos version * update x/auth/legacy to x/auth/migrations * Delete rest packages and registration * Remove rest from proposal handlers * Remove legacy types referencing removed sdk types * Remove legacy tx broadcast handler * Update incentive staking hooks to return error * Remove grpc replace directive, use new grpc version * Fix storetypes import * Update tally_handler with updated gov types * Delete legacy types * Use new gov default config * Update RegisterTendermintService params Signed-off-by: drklee3 <derrick@dlee.dev> * Replace sdk.StoreKey with storetypes.StoreKey * Replace sdk.Int#ToDec with sdk.NewDecFromInt * Replace sdk.NewUintFromBigInt with sdkmath.NewUintFromBigInt Signed-off-by: drklee3 <derrick@dlee.dev> * Update most intances of govtypes to govv1beta1 * Unpack coin slice for Coins#Sub and Coins#SafeSub Signed-off-by: drklee3 <derrick@dlee.dev> * Update committee gov codec registration Signed-off-by: drklee3 <derrick@dlee.dev> * Update migrate utils period_vesting Coins#Sub Signed-off-by: drklee3 <derrick@dlee.dev> * Update Coin#Sub in community proposal handler Signed-off-by: drklee3 <derrick@dlee.dev> * Update Coin#Sub, FundModuleAccount/FundAccount in banktestutil Signed-off-by: drklee3 <derrick@dlee.dev> * Update community, earn, kavadist proposal gov registration * Update evm cli client EthSecp256k1Type check * AccAddressFromHex to AccAddressFromHexUnsafe * Add mint DefaultInflationCalculationFn to earn test * Update use of removed staking.NewHandler * Rename FlagIAVLFastNode -> FlagDisableIAVLFastNode * cmd: Update new snapshot app option Signed-off-by: drklee3 <derrick@dlee.dev> * cmd: Add tendermint default config, use cosmos rpc status command Signed-off-by: drklee3 <derrick@dlee.dev> * Update ethermint import path github.com/tharsis/ethermint -> github.com/evmos/ethermint * Upgrade ibc-go to v6 * Update proto dependencies Signed-off-by: drklee3 <derrick@dlee.dev> * Update Tally handler test with new gov types * Update helpers.GenTx -> helpers.GenSignedMockTx * Update evmkeeper.NewKeeper params Signed-off-by: drklee3 <derrick@dlee.dev> * Update ante authz, tests * Add feemarket transient key, pass subspaces to evm/feemarket keepers * Update new ante decorators * Add new addModuleInitFlags to server commands * Pass codec to keyring.New in genaccounts * Pass codec to client keys add * Add SendCoins to evmutil bank_keeper * Use github.com/cosmos/iavl@v0.19.5 * Add ante HandlerOptions * Add unimplemented SendCoins to evmutil bank keeper Ethermint x/evm does not use this method * Update init-new-chain script to disable post-london blocks * Modify test genesis states to append 1 validator * Update tally handler test to use string values * Prevent querying balance for empty sdk.AccAddress in auction bidding test * Set default bond denom to ukava * Remove overwritten bank genesis total supply in committee proposal test Signed-off-by: drklee3 <derrick@dlee.dev> * Use ukava for testing staked balance * Disable minting in community proposal handler test Previously stake denom is used, which resulted in 0 minted coins * Update hard APYToSPY test expected value Increased iterations in sdk.ApproxRoot, updated closer to real value * Fix NewDecCoinsFromCoins bug in incentive collectDerivativeStakingRewards * Allow bkava earn incentive test values to match within small margin for rounding Signed-off-by: drklee3 <derrick@dlee.dev> * Update invalid denom in issuance message coin validation Colons are now valid in denoms Signed-off-by: drklee3 <derrick@dlee.dev> * Remove genesis validator in incentive delegation tests * Update pricefeed market test for invalid denom Signed-off-by: drklee3 <derrick@dlee.dev> * Update incentive delegator rewards test without genesis validator Signed-off-by: drklee3 <derrick@dlee.dev> * Add validator to export test * Clear bank state in minting tests Signed-off-by: drklee3 <derrick@dlee.dev> * Remove validator for no stake tally test Signed-off-by: drklee3 <derrick@dlee.dev> * Clear incentive state before InitGenesis in incentive genesis export test * Update swagger Signed-off-by: drklee3 <derrick@dlee.dev> * Update ethermint version to match replaced version * Remove legacy swagger * Add NewEthEmitEventDecorator * Remove redundant func for AddModuleInitFlags * Remove unused addBankBalanceForAddress func * Add SetIAVLLazyLoading option to app cmd * Use legacy.RegisterAminoMsg for committee msg concrete registration * Remove unnecessary Amino field * Add evm_util bankkeeper SendCoins comment * Update test method ResetBankState to DeleteGenesisValidatorCoins to be more clear * Validate incentive params.RewardsPerSecond to be non-zero * Validate swap pools to disallow colons in token denoms * Register all legacy amino types on gov modulecdc * Remove redundant Comittee interface registration * Pin goleveldb to v1.0.1-0.20210819022825-2ae1ddf74ef7 Causes failed to load state at height errors * Update ethermint to new pinned version with minGasPrices parse error fix * Update cosmos fork dependcy commit to include reverted account constructor patch * Update Cosmos v0.46.11 and cometbft v0.34.27 * Bump minimum go version to 1.19 * Update tendermint proto * Update internal testnet genesis * Move NewCanTransferDecorator before NewEthGasConsumeDecorator * Add hard borrow store tests (#1514) * add store tests for Borrow type * refactor Deposit tests to match * Fix old bep3 tests (#1515) * Update Ethermint to 1b17445 to fix duplicate proto registration * Add custom status command to use snake_case and stdout * Add SetInflation helper * Reduce ambiguity with evm CanSignEthTx error * Remove init genesis validator claim in test * Add disabled evmante.NewMinGasPriceDecorator with x/feemarket note * chore: use tagged versions for Cosmos and Ethermint forks * update kvtool & increase wait for ibc transfer test --------- Signed-off-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Robert Pirtle <astropirtle@gmail.com>
256 lines
8.3 KiB
Go
256 lines
8.3 KiB
Go
package app
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
|
|
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
earnkeeper "github.com/kava-labs/kava/x/earn/keeper"
|
|
liquidkeeper "github.com/kava-labs/kava/x/liquid/keeper"
|
|
liquidtypes "github.com/kava-labs/kava/x/liquid/types"
|
|
savingskeeper "github.com/kava-labs/kava/x/savings/keeper"
|
|
)
|
|
|
|
var _ govv1.TallyHandler = TallyHandler{}
|
|
|
|
// TallyHandler is the tally handler for kava
|
|
type TallyHandler struct {
|
|
gk govkeeper.Keeper
|
|
stk stakingkeeper.Keeper
|
|
svk savingskeeper.Keeper
|
|
ek earnkeeper.Keeper
|
|
lk liquidkeeper.Keeper
|
|
bk bankkeeper.Keeper
|
|
}
|
|
|
|
// NewTallyHandler creates a new tally handler.
|
|
func NewTallyHandler(
|
|
gk govkeeper.Keeper, stk stakingkeeper.Keeper, svk savingskeeper.Keeper,
|
|
ek earnkeeper.Keeper, lk liquidkeeper.Keeper, bk bankkeeper.Keeper,
|
|
) TallyHandler {
|
|
return TallyHandler{
|
|
gk: gk,
|
|
stk: stk,
|
|
svk: svk,
|
|
ek: ek,
|
|
lk: lk,
|
|
bk: bk,
|
|
}
|
|
}
|
|
|
|
func (th TallyHandler) Tally(
|
|
ctx sdk.Context,
|
|
proposal govv1.Proposal,
|
|
) (passes bool, burnDeposits bool, tallyResults govv1.TallyResult) {
|
|
results := make(map[govv1.VoteOption]sdk.Dec)
|
|
results[govv1.OptionYes] = sdk.ZeroDec()
|
|
results[govv1.OptionAbstain] = sdk.ZeroDec()
|
|
results[govv1.OptionNo] = sdk.ZeroDec()
|
|
results[govv1.OptionNoWithVeto] = sdk.ZeroDec()
|
|
|
|
totalVotingPower := sdk.ZeroDec()
|
|
currValidators := make(map[string]govv1.ValidatorGovInfo)
|
|
|
|
// fetch all the bonded validators, insert them into currValidators
|
|
th.stk.IterateBondedValidatorsByPower(ctx, func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
|
|
currValidators[validator.GetOperator().String()] = govv1.NewValidatorGovInfo(
|
|
validator.GetOperator(),
|
|
validator.GetBondedTokens(),
|
|
validator.GetDelegatorShares(),
|
|
sdk.ZeroDec(),
|
|
govv1.WeightedVoteOptions{},
|
|
)
|
|
|
|
return false
|
|
})
|
|
|
|
th.gk.IterateVotes(ctx, proposal.Id, func(vote govv1.Vote) bool {
|
|
// if validator, just record it in the map
|
|
voter, err := sdk.AccAddressFromBech32(vote.Voter)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
valAddrStr := sdk.ValAddress(voter.Bytes()).String()
|
|
if val, ok := currValidators[valAddrStr]; ok {
|
|
val.Vote = vote.Options
|
|
currValidators[valAddrStr] = val
|
|
}
|
|
|
|
// iterate over all delegations from voter, deduct from any delegated-to validators
|
|
th.stk.IterateDelegations(ctx, voter, func(index int64, delegation stakingtypes.DelegationI) (stop bool) {
|
|
valAddrStr := delegation.GetValidatorAddr().String()
|
|
|
|
if val, ok := currValidators[valAddrStr]; ok {
|
|
// There is no need to handle the special case that validator address equal to voter address.
|
|
// Because voter's voting power will tally again even if there will deduct voter's voting power from validator.
|
|
val.DelegatorDeductions = val.DelegatorDeductions.Add(delegation.GetShares())
|
|
currValidators[valAddrStr] = val
|
|
|
|
// delegation shares * bonded / total shares
|
|
votingPower := delegation.GetShares().MulInt(val.BondedTokens).Quo(val.DelegatorShares)
|
|
|
|
for _, option := range vote.Options {
|
|
subPower := votingPower.Mul(sdk.MustNewDecFromStr(option.Weight))
|
|
results[option.Option] = results[option.Option].Add(subPower)
|
|
}
|
|
totalVotingPower = totalVotingPower.Add(votingPower)
|
|
}
|
|
|
|
return false
|
|
})
|
|
|
|
// get voter bkava and update total voting power and results
|
|
addrBkava := th.getAddrBkava(ctx, voter).toCoins()
|
|
for _, coin := range addrBkava {
|
|
valAddr, err := liquidtypes.ParseLiquidStakingTokenDenom(coin.Denom)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
// reduce delegator shares by the amount of voter bkava for the validator
|
|
valAddrStr := valAddr.String()
|
|
if val, ok := currValidators[valAddrStr]; ok {
|
|
val.DelegatorDeductions = val.DelegatorDeductions.Add(sdk.NewDecFromInt(coin.Amount))
|
|
currValidators[valAddrStr] = val
|
|
}
|
|
|
|
// votingPower = amount of ukava coin
|
|
stakedCoins, err := th.lk.GetStakedTokensForDerivatives(ctx, sdk.NewCoins(coin))
|
|
if err != nil {
|
|
// error is returned only if the bkava denom is incorrect, which should never happen here.
|
|
panic(err)
|
|
}
|
|
votingPower := sdk.NewDecFromInt(stakedCoins.Amount)
|
|
|
|
for _, option := range vote.Options {
|
|
subPower := votingPower.Mul(sdk.MustNewDecFromStr(option.Weight))
|
|
results[option.Option] = results[option.Option].Add(subPower)
|
|
}
|
|
totalVotingPower = totalVotingPower.Add(votingPower)
|
|
}
|
|
|
|
th.gk.DeleteVote(ctx, vote.ProposalId, voter)
|
|
return false
|
|
})
|
|
|
|
// iterate over the validators again to tally their voting power
|
|
for _, val := range currValidators {
|
|
if len(val.Vote) == 0 {
|
|
continue
|
|
}
|
|
|
|
sharesAfterDeductions := val.DelegatorShares.Sub(val.DelegatorDeductions)
|
|
votingPower := sharesAfterDeductions.MulInt(val.BondedTokens).Quo(val.DelegatorShares)
|
|
|
|
for _, option := range val.Vote {
|
|
subPower := votingPower.Mul(sdk.MustNewDecFromStr(option.Weight))
|
|
results[option.Option] = results[option.Option].Add(subPower)
|
|
}
|
|
totalVotingPower = totalVotingPower.Add(votingPower)
|
|
}
|
|
|
|
tallyParams := th.gk.GetTallyParams(ctx)
|
|
tallyResults = govv1.NewTallyResultFromMap(results)
|
|
|
|
// TODO: Upgrade the spec to cover all of these cases & remove pseudocode.
|
|
// If there is no staked coins, the proposal fails
|
|
if th.stk.TotalBondedTokens(ctx).IsZero() {
|
|
return false, false, tallyResults
|
|
}
|
|
|
|
// If there is not enough quorum of votes, the proposal fails
|
|
percentVoting := totalVotingPower.Quo(sdk.NewDecFromInt(th.stk.TotalBondedTokens(ctx)))
|
|
if percentVoting.LT(sdk.MustNewDecFromStr(tallyParams.Quorum)) {
|
|
return false, true, tallyResults
|
|
}
|
|
|
|
// If no one votes (everyone abstains), proposal fails
|
|
if totalVotingPower.Sub(results[govv1.OptionAbstain]).Equal(sdk.ZeroDec()) {
|
|
return false, false, tallyResults
|
|
}
|
|
|
|
// If more than 1/3 of voters veto, proposal fails
|
|
if results[govv1.OptionNoWithVeto].Quo(totalVotingPower).GT(sdk.MustNewDecFromStr(tallyParams.VetoThreshold)) {
|
|
return false, true, tallyResults
|
|
}
|
|
|
|
// If more than 1/2 of non-abstaining voters vote Yes, proposal passes
|
|
if results[govv1.OptionYes].Quo(totalVotingPower.Sub(results[govv1.OptionAbstain])).GT(sdk.MustNewDecFromStr(tallyParams.Threshold)) {
|
|
return true, false, tallyResults
|
|
}
|
|
|
|
// If more than 1/2 of non-abstaining voters vote No, proposal fails
|
|
return false, false, tallyResults
|
|
}
|
|
|
|
// bkavaByDenom a map of the bkava denom and the amount of bkava for that denom.
|
|
type bkavaByDenom map[string]sdk.Int
|
|
|
|
func (bkavaMap bkavaByDenom) add(coin sdk.Coin) {
|
|
_, found := bkavaMap[coin.Denom]
|
|
if !found {
|
|
bkavaMap[coin.Denom] = sdk.ZeroInt()
|
|
}
|
|
bkavaMap[coin.Denom] = bkavaMap[coin.Denom].Add(coin.Amount)
|
|
}
|
|
|
|
func (bkavaMap bkavaByDenom) toCoins() sdk.Coins {
|
|
coins := sdk.Coins{}
|
|
for denom, amt := range bkavaMap {
|
|
coins = coins.Add(sdk.NewCoin(denom, amt))
|
|
}
|
|
return coins.Sort()
|
|
}
|
|
|
|
// getAddrBkava returns a map of validator address & the amount of bkava
|
|
// of the addr for each validator.
|
|
func (th TallyHandler) getAddrBkava(ctx sdk.Context, addr sdk.AccAddress) bkavaByDenom {
|
|
results := make(bkavaByDenom)
|
|
th.addBkavaFromWallet(ctx, addr, results)
|
|
th.addBkavaFromSavings(ctx, addr, results)
|
|
th.addBkavaFromEarn(ctx, addr, results)
|
|
return results
|
|
}
|
|
|
|
// addBkavaFromWallet adds all addr balances of bkava in x/bank.
|
|
func (th TallyHandler) addBkavaFromWallet(ctx sdk.Context, addr sdk.AccAddress, bkava bkavaByDenom) {
|
|
coins := th.bk.GetAllBalances(ctx, addr)
|
|
for _, coin := range coins {
|
|
if th.lk.IsDerivativeDenom(ctx, coin.Denom) {
|
|
bkava.add(coin)
|
|
}
|
|
}
|
|
}
|
|
|
|
// addBkavaFromSavings adds all addr deposits of bkava in x/savings.
|
|
func (th TallyHandler) addBkavaFromSavings(ctx sdk.Context, addr sdk.AccAddress, bkava bkavaByDenom) {
|
|
deposit, found := th.svk.GetDeposit(ctx, addr)
|
|
if !found {
|
|
return
|
|
}
|
|
for _, coin := range deposit.Amount {
|
|
if th.lk.IsDerivativeDenom(ctx, coin.Denom) {
|
|
bkava.add(coin)
|
|
}
|
|
}
|
|
}
|
|
|
|
// addBkavaFromEarn adds all addr deposits of bkava in x/earn.
|
|
func (th TallyHandler) addBkavaFromEarn(ctx sdk.Context, addr sdk.AccAddress, bkava bkavaByDenom) {
|
|
shares, found := th.ek.GetVaultAccountShares(ctx, addr)
|
|
if !found {
|
|
return
|
|
}
|
|
for _, share := range shares {
|
|
if th.lk.IsDerivativeDenom(ctx, share.Denom) {
|
|
if coin, err := th.ek.ConvertToAssets(ctx, share); err == nil {
|
|
bkava.add(coin)
|
|
}
|
|
}
|
|
}
|
|
}
|