hook, genesis, and withdraw address
This commit is contained in:
parent
2eff69a575
commit
431fdb8695
|
@ -16,6 +16,7 @@ import (
|
|||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
distr "github.com/cosmos/cosmos-sdk/x/distribution"
|
||||
"github.com/cosmos/cosmos-sdk/x/gov"
|
||||
"github.com/cosmos/cosmos-sdk/x/ibc"
|
||||
"github.com/cosmos/cosmos-sdk/x/params"
|
||||
|
@ -45,6 +46,8 @@ type GaiaApp struct {
|
|||
keyStake *sdk.KVStoreKey
|
||||
tkeyStake *sdk.TransientStoreKey
|
||||
keySlashing *sdk.KVStoreKey
|
||||
keyDistr *sdk.KVStoreKey
|
||||
tkeyDistr *sdk.TransientStoreKey
|
||||
keyGov *sdk.KVStoreKey
|
||||
keyFeeCollection *sdk.KVStoreKey
|
||||
keyParams *sdk.KVStoreKey
|
||||
|
@ -57,6 +60,7 @@ type GaiaApp struct {
|
|||
ibcMapper ibc.Mapper
|
||||
stakeKeeper stake.Keeper
|
||||
slashingKeeper slashing.Keeper
|
||||
distrKeeper distr.Keeper
|
||||
govKeeper gov.Keeper
|
||||
paramsKeeper params.Keeper
|
||||
}
|
||||
|
@ -76,6 +80,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
|
|||
keyIBC: sdk.NewKVStoreKey("ibc"),
|
||||
keyStake: sdk.NewKVStoreKey("stake"),
|
||||
tkeyStake: sdk.NewTransientStoreKey("transient_stake"),
|
||||
keyDistr: sdk.NewKVStoreKey("distr"),
|
||||
tkeyDistr: sdk.NewTransientStoreKey("transient_distr"),
|
||||
keySlashing: sdk.NewKVStoreKey("slashing"),
|
||||
keyGov: sdk.NewKVStoreKey("gov"),
|
||||
keyFeeCollection: sdk.NewKVStoreKey("fee"),
|
||||
|
@ -95,6 +101,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
|
|||
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
|
||||
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
|
||||
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.RegisterCodespace(stake.DefaultCodespace))
|
||||
app.distrKeeper = distr.NewKeeper(app.cdc, app.keyDistr, app.tkeyStake, app.bankKeeper, app.RegisterCodespace(stake.DefaultCodespace))
|
||||
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
|
||||
app.stakeKeeper = app.stakeKeeper.WithValidatorHooks(app.slashingKeeper.ValidatorHooks())
|
||||
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
|
||||
|
@ -105,6 +112,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
|
|||
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
|
||||
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.bankKeeper)).
|
||||
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
|
||||
AddRoute("distr", stake.NewHandler(app.distrKeeper)).
|
||||
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper)).
|
||||
AddRoute("gov", gov.NewHandler(app.govKeeper))
|
||||
|
||||
|
@ -134,6 +142,7 @@ func MakeCodec() *codec.Codec {
|
|||
ibc.RegisterCodec(cdc)
|
||||
bank.RegisterCodec(cdc)
|
||||
stake.RegisterCodec(cdc)
|
||||
distr.RegisterCodec(cdc)
|
||||
slashing.RegisterCodec(cdc)
|
||||
gov.RegisterCodec(cdc)
|
||||
auth.RegisterCodec(cdc)
|
||||
|
@ -145,6 +154,7 @@ func MakeCodec() *codec.Codec {
|
|||
// application updates every end block
|
||||
func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
||||
tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
|
||||
distr.BeginBlocker(ctx, req, app.distrKeeper)
|
||||
|
||||
return abci.ResponseBeginBlock{
|
||||
Tags: tags.ToKVPairs(),
|
||||
|
@ -156,6 +166,10 @@ func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) ab
|
|||
func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
||||
tags := gov.EndBlocker(ctx, app.govKeeper)
|
||||
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
||||
|
||||
// distribute rewards
|
||||
distr.EndBlocker(ctx, app.distrKeeper)
|
||||
|
||||
// Add these new validators to the addr -> pubkey map.
|
||||
app.slashingKeeper.AddValidators(ctx, validatorUpdates)
|
||||
return abci.ResponseEndBlock{
|
||||
|
|
|
@ -100,12 +100,26 @@ type DelegationSet interface {
|
|||
fn func(index int64, delegation Delegation) (stop bool))
|
||||
}
|
||||
|
||||
// validator event hooks
|
||||
// These can be utilized to communicate between a staking keeper
|
||||
// and another keeper which must take particular actions when
|
||||
// validators are bonded and unbonded. The second keeper must implement
|
||||
// this interface, which then the staking keeper can call.
|
||||
//_______________________________________________________________________________
|
||||
// Event Hooks
|
||||
// These can be utilized to communicate between a staking keeper and another
|
||||
// keeper which must take particular actions when validators/delegators change
|
||||
// state. The second keeper must implement this interface, which then the
|
||||
// staking keeper can call.
|
||||
|
||||
// event hooks for staking validator object
|
||||
type ValidatorHooks interface {
|
||||
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created
|
||||
OnValidatorBondModified(ctx Context, address ValAddress) // Must be called when a validator's bond amount is modified
|
||||
OnValidatorRemoved(ctx Context, address ValAddress) // Must be called when a validator is deleted
|
||||
|
||||
OnValidatorBonded(ctx Context, address ConsAddress) // Must be called when a validator is bonded
|
||||
OnValidatorBeginUnbonding(ctx Context, address ConsAddress) // Must be called when a validator begins unbonding
|
||||
}
|
||||
|
||||
// event hooks for staking delegator object
|
||||
type DelegatorHooks interface {
|
||||
OnDelegationCreated(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation is created
|
||||
OnDelegationSharesModified(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation's shares are modified
|
||||
OnDelegationRemoved(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation is removed
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package distribution
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
)
|
||||
|
||||
// XXX TODO
|
||||
// Register concrete types on codec codec
|
||||
func RegisterCodec(cdc *codec.Codec) {
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package distribution
|
||||
|
||||
import (
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||
)
|
||||
|
||||
// InitGenesis sets the pool and parameters for the provided keeper and
|
||||
// initializes the IntraTxCounter. For each validator in data, it sets that
|
||||
// validator in the keeper along with manually setting the indexes. In
|
||||
// addition, it also sets any delegations found in data. Finally, it updates
|
||||
// the bonded validators.
|
||||
// Returns final validator set after applying all declaration and delegations
|
||||
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.Validator, err error) {
|
||||
keeper.SetFeePool(ctx, data.FeePool)
|
||||
|
||||
for _, vdi := range data.ValidatorDistInfos {
|
||||
keeper.SetValidatorDistInfo(ctx, vdi)
|
||||
}
|
||||
for _, ddi := range data.DelegatorDistInfos {
|
||||
keeper.SetDelegatorDistInfo(ctx, ddi)
|
||||
}
|
||||
for _, dw := range data.DelegatorWithdrawAddrs {
|
||||
keeper.SetDelegatorWithdrawAddr(ctx, dw.DelegatorAddr, dw.WithdrawAddr)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// WriteGenesis returns a GenesisState for a given context and keeper. The
|
||||
// GenesisState will contain the pool, and validator/delegator distribution info's
|
||||
func WriteGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
|
||||
feePool := keeper.GetFeePool(ctx)
|
||||
vdis := keeper.GetAllVDIs(ctx)
|
||||
ddis := keeper.GetAllDDIs(ctx)
|
||||
dws := keeper.GetAllDWs(ctx)
|
||||
|
||||
return GenesisState{
|
||||
FeePool: feePool,
|
||||
ValidatorDistInfos: vdis,
|
||||
DelegatorDistInfos: ddis,
|
||||
DelegatorWithdrawAddrs: dws,
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
package stake
|
||||
package distribution
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
package stake
|
||||
package distribution
|
||||
|
||||
import "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
|
||||
/*
|
||||
## Create or modify delegation distribution
|
||||
|
@ -28,7 +30,39 @@ validator entitled reward tokens must be simultaneously withdrawn from
|
|||
*/
|
||||
|
||||
// Create a new validator distribution record
|
||||
func (k Keeper) onValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) {
|
||||
func (k Keeper) onValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
|
||||
height := ctx.BlockHeight()
|
||||
vdi := types.ValidatorDistInfo{
|
||||
OperatorAddr: addr,
|
||||
GlobalWithdrawalHeight: height,
|
||||
Pool: DecCoins{},
|
||||
PoolCommission: DecCoins{},
|
||||
DelAccum: NewTotalAccum(height),
|
||||
}
|
||||
k.SetValidatorDistInfo(ctx, vdi)
|
||||
}
|
||||
|
||||
// Withdrawal all distubution rewards // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX
|
||||
func (k Keeper) onValidatorBondModified(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
slashingPeriod := ValidatorSlashingPeriod{
|
||||
ValidatorAddr: address,
|
||||
StartHeight: ctx.BlockHeight(),
|
||||
EndHeight: 0,
|
||||
SlashedSoFar: sdk.ZeroDec(),
|
||||
}
|
||||
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
|
||||
}
|
||||
|
||||
// Withdrawal all validator distribution rewards and cleanup the distribution record
|
||||
func (k Keeper) onValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
k.RemoveValidatorDistInfo(ctx, addr)
|
||||
}
|
||||
|
||||
//_________________________________________________________________________________________
|
||||
|
||||
// Create a new validator distribution record
|
||||
func (k Keeper) onDelegationCreated(ctx sdk.Context, address sdk.ConsAddress) {
|
||||
slashingPeriod := ValidatorSlashingPeriod{
|
||||
ValidatorAddr: address,
|
||||
StartHeight: ctx.BlockHeight(),
|
||||
|
@ -49,6 +83,38 @@ var _ sdk.ValidatorHooks = ValidatorHooks{}
|
|||
|
||||
// nolint
|
||||
func (k Keeper) ValidatorHooks() sdk.ValidatorHooks { return ValidatorHooks{k} }
|
||||
func (v ValidatorHooks) OnValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) {
|
||||
v.k.onValidatorBonded(ctx, address)
|
||||
func (v ValidatorHooks) OnValidatorCreated(ctx sdk.Context, addr sdk.VlAddress) {
|
||||
v.k.OnValidatorCreated(ctx, address)
|
||||
}
|
||||
func (v ValidatorHooks) OnValidatorBondModified(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
v.k.OnValidatorBondModified(ctx, address)
|
||||
}
|
||||
func (v ValidatorHooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
v.k.OnValidatorRemoved(ctx, address)
|
||||
}
|
||||
func (v ValidatorHooks) OnValidatorBonded(_ sdk.Context, _ sdk.ConsAddress) {}
|
||||
func (v ValidatorHooks) OnValidatorBeginBonded(_ sdk.Context, _ sdk.ConsAddress) {}
|
||||
|
||||
//_________________________________________________________________________________________
|
||||
|
||||
// Wrapper struct for sdk.DelegationHooks
|
||||
type DelegationHooks struct {
|
||||
k Keeper
|
||||
}
|
||||
|
||||
var _ sdk.DelegationHooks = DelegationHooks{}
|
||||
|
||||
// nolint
|
||||
func (k Keeper) DelegationHooks() sdk.DelegationHooks { return DelegationHooks{k} }
|
||||
func (d DelegationHooks) OnDelegatoinCreated(ctx sdk.Context,
|
||||
delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
|
||||
d.k.OnDelegatoinCreated(ctx, address)
|
||||
}
|
||||
func (d DelegationHooks) OnDelegationSharesModified(ctx sdk.Context,
|
||||
delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
|
||||
d.k.OnDelegationSharesModified(ctx, address)
|
||||
}
|
||||
func (d DelegationHooks) OnDelegationRemoved(ctx sdk.Context,
|
||||
delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
|
||||
d.k.OnDelegationRemoved(ctx, address)
|
||||
}
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
package stake
|
||||
|
||||
//// keeper of the staking store
|
||||
//type Keeper struct {
|
||||
//storeKey sdk.StoreKey
|
||||
//cdc *codec.Codec
|
||||
//bankKeeper bank.Keeper
|
||||
|
||||
//// codespace
|
||||
//codespace sdk.CodespaceType
|
||||
//}
|
||||
|
||||
//func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.CodespaceType) Keeper {
|
||||
//keeper := Keeper{
|
||||
//storeKey: key,
|
||||
//cdc: cdc,
|
||||
//bankKeeper: ck,
|
||||
//codespace: codespace,
|
||||
//}
|
||||
//return keeper
|
||||
//}
|
||||
|
||||
////_________________________________________________________________________
|
||||
|
||||
//// cummulative power of the non-absent prevotes
|
||||
//func (k Keeper) GetTotalPrecommitVotingPower(ctx sdk.Context) sdk.Dec {
|
||||
//store := ctx.KVStore(k.storeKey)
|
||||
|
||||
//// get absent prevote indexes
|
||||
//absents := ctx.AbsentValidators()
|
||||
|
||||
//TotalPower := sdk.ZeroDec()
|
||||
//i := int32(0)
|
||||
//iterator := store.SubspaceIterator(ValidatorsBondedKey)
|
||||
//for ; iterator.Valid(); iterator.Next() {
|
||||
|
||||
//skip := false
|
||||
//for j, absentIndex := range absents {
|
||||
//if absentIndex > i {
|
||||
//break
|
||||
//}
|
||||
|
||||
//// if non-voting validator found, skip adding its power
|
||||
//if absentIndex == i {
|
||||
//absents = append(absents[:j], absents[j+1:]...) // won't need again
|
||||
//skip = true
|
||||
//break
|
||||
//}
|
||||
//}
|
||||
//if skip {
|
||||
//continue
|
||||
//}
|
||||
|
||||
//bz := iterator.Value()
|
||||
//var validator Validator
|
||||
//k.cdc.MustUnmarshalBinary(bz, &validator)
|
||||
//TotalPower = TotalPower.Add(validator.Power)
|
||||
//i++
|
||||
//}
|
||||
//iterator.Close()
|
||||
//return TotalPower
|
||||
//}
|
||||
|
||||
////_______________________________________________________________________
|
||||
|
||||
//// XXX TODO trim functionality
|
||||
|
||||
//// retrieve all the power changes which occur after a height
|
||||
//func (k Keeper) GetPowerChangesAfterHeight(ctx sdk.Context, earliestHeight int64) (pcs []PowerChange) {
|
||||
//store := ctx.KVStore(k.storeKey)
|
||||
|
||||
//iterator := store.SubspaceIterator(PowerChangeKey) //smallest to largest
|
||||
//for ; iterator.Valid(); iterator.Next() {
|
||||
//pcBytes := iterator.Value()
|
||||
//var pc PowerChange
|
||||
//k.cdc.MustUnmarshalBinary(pcBytes, &pc)
|
||||
//if pc.Height < earliestHeight {
|
||||
//break
|
||||
//}
|
||||
//pcs = append(pcs, pc)
|
||||
//}
|
||||
//iterator.Close()
|
||||
//return
|
||||
//}
|
||||
|
||||
//// set a power change
|
||||
//func (k Keeper) setPowerChange(ctx sdk.Context, pc PowerChange) {
|
||||
//store := ctx.KVStore(k.storeKey)
|
||||
//b := k.cdc.MustMarshalBinary(pc)
|
||||
//store.Set(GetPowerChangeKey(pc.Height), b)
|
||||
//}
|
|
@ -27,11 +27,44 @@ func (k Keeper) SetDelegatorDistInfo(ctx sdk.Context, ddi types.DelegatorDistInf
|
|||
store.Set(GetDelegationDistInfoKey(ddi.DelegatorAddr, ddi.ValOperatorAddr), b)
|
||||
}
|
||||
|
||||
// remove a delegator distribution info
|
||||
func (k Keeper) RemoveDelegatorDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
|
||||
valOperatorAddr sdk.ValAddress) {
|
||||
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
store.Delete(GetDelegationDistInfoKey(DelegatorAddr, ValOperatorAddr))
|
||||
}
|
||||
|
||||
//___________________________________________________________________________________________
|
||||
|
||||
// get the delegator withdraw address, return the delegator address if not set
|
||||
func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
|
||||
b := store.Get(GetDelegatorWithdrawAddrKey(delAddr))
|
||||
if b == nil {
|
||||
return delAddr
|
||||
}
|
||||
return sdk.AccAddress{b}
|
||||
}
|
||||
|
||||
// set the delegator withdraw address
|
||||
func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
store.Set(GetDelegatorWithdrawAddrKey(ddi.DelegatorAddr), withdrawAddr.Bytes())
|
||||
}
|
||||
|
||||
// remove a delegator withdraw info
|
||||
func (k Keeper) RemoveDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
store.Delete(GetDelegatorWithdrawAddrKey(delAddr))
|
||||
}
|
||||
|
||||
//___________________________________________________________________________________________
|
||||
|
||||
// withdraw all the rewards for a single delegation
|
||||
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr,
|
||||
withdrawAddr sdk.AccAddress, validatorAddr sdk.ValAddress) {
|
||||
func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress,
|
||||
validatorAddr sdk.ValAddress) {
|
||||
|
||||
height := ctx.BlockHeight()
|
||||
pool := k.sk.GetPool(ctx)
|
||||
|
@ -44,15 +77,17 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr,
|
|||
validator.Tokens, validator.DelegatorShares, validator.Commission)
|
||||
|
||||
k.SetFeePool(ctx, feePool)
|
||||
withdrawAddr := k.GetDelegatorWithdrawAddr(delegatorAddr)
|
||||
k.ck.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
//___________________________________________________________________________________________
|
||||
|
||||
// return all rewards for all delegations of a delegator
|
||||
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr, withdrawAddr sdk.AccAddress) {
|
||||
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.AccAddress) {
|
||||
height := ctx.BlockHeight()
|
||||
withdraw = GetDelegatorRewardsAll(ctx, delegatorAddr, height)
|
||||
withdraw = k.GetDelegatorRewardsAll(ctx, delegatorAddr, height)
|
||||
withdrawAddr := k.GetDelegatorWithdrawAddr(delegatorAddr)
|
||||
k.coinsKeeper.AddCoins(withdrawAddr, withdraw.Amount.TruncateDecimal())
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package keeper
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
// Get the set of all validator-distribution-info's with no limits, used during genesis dump
|
||||
func (k Keeper) GetAllVDIs(ctx sdk.Context) (vdis []types.ValidatorDistInfo) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, ValidatorDistInfoKey)
|
||||
defer iterator.Close()
|
||||
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var vdi types.ValidatorDistInfo
|
||||
k.cdc.MustUnmarshalBinary(iterator.Value(), &vdi)
|
||||
vdis = append(vdis, vdi)
|
||||
}
|
||||
return vdis
|
||||
}
|
||||
|
||||
// Get the set of all delegator-distribution-info's with no limits, used during genesis dump
|
||||
func (k Keeper) GetAllDDIs(ctx sdk.Context) (ddis []types.DelegatorDistInfo) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, DelegatorDistInfoKey)
|
||||
defer iterator.Close()
|
||||
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var ddi types.DelegatorDistInfo
|
||||
k.cdc.MustUnmarshalBinary(iterator.Value(), &ddi)
|
||||
ddis = append(ddis, ddi)
|
||||
}
|
||||
return ddis
|
||||
}
|
||||
|
||||
// Get the set of all delegator-withdraw addresses with no limits, used during genesis dump
|
||||
func (k Keeper) GetAllDWs(ctx sdk.Context) (dws []types.DelegatorWithdrawInfo) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
iterator := sdk.KVStorePrefixIterator(store, DelegatorDistInfoKey)
|
||||
defer iterator.Close()
|
||||
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
addr := iterator.Key()[1:]
|
||||
dw := types.DelegatorWithdrawInfo{
|
||||
DelegatorAddr: sdk.AccAddress{iterator.Key()},
|
||||
WithdrawAddr: sdk.AccAddress{iterator.Value()},
|
||||
}
|
||||
dws = append(dws, dw)
|
||||
}
|
||||
return dws
|
||||
}
|
|
@ -6,9 +6,10 @@ import (
|
|||
|
||||
// keys/key-prefixes
|
||||
var (
|
||||
FeePoolKey = []byte{0x00} // key for global distribution state
|
||||
ValidatorDistInfoKey = []byte{0x01} // prefix for each key to a validator distribution
|
||||
DelegatorDistInfoKey = []byte{0x02} // prefix for each key to a delegation distribution
|
||||
FeePoolKey = []byte{0x00} // key for global distribution state
|
||||
ValidatorDistInfoKey = []byte{0x01} // prefix for each key to a validator distribution
|
||||
DelegatorDistInfoKey = []byte{0x02} // prefix for each key to a delegation distribution
|
||||
DelegatorWithdrawInfoKey = []byte{0x03} // prefix for each key to a delegator withdraw info
|
||||
|
||||
// transient
|
||||
ProposerKey = []byte{0x00} // key for storing the proposer operator address
|
||||
|
@ -30,3 +31,8 @@ func GetDelegationDistInfoKey(delAddr sdk.AccAddress, valOperatorAddr sdk.ValAdd
|
|||
func GetDelegationDistInfosKey(delAddr sdk.AccAddress) []byte {
|
||||
return append(DelegatorDistInfoKey, delAddr.Bytes()...)
|
||||
}
|
||||
|
||||
// gets the prefix for a delegator's withdraw info
|
||||
func GetDelegatorWithdrawAddrKey(delAddr sdk.AccAddress) []byte {
|
||||
return append(DelegatorWithdrawInfoKey, delAddr.Bytes()...)
|
||||
}
|
||||
|
|
|
@ -27,7 +27,13 @@ func (k Keeper) SetValidatorDistInfo(ctx sdk.Context, vdi types.ValidatorDistInf
|
|||
store.Set(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr), b)
|
||||
}
|
||||
|
||||
// XXX TODO
|
||||
// remove a validator distribution info
|
||||
func (k Keeper) RemoveValidatorDistInfo(ctx sdk.Context, valAddr sdk.ValAddress) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
store.Delete(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr))
|
||||
}
|
||||
|
||||
// withdrawal all the validator rewards including the commission
|
||||
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context,
|
||||
operatorAddr sdk.ValAddress, withdrawAddr sdk.AccAddress) {
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
|
|||
|
||||
// distribution info for a delegation
|
||||
type DelegatorDistInfo struct {
|
||||
DelegatorAddr sdk.AccAddress
|
||||
ValOperatorAddr sdk.ValAddress
|
||||
WithdrawalHeight int64 // last time this delegation withdrew rewards
|
||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
|
||||
ValOperatorAddr sdk.ValAddress `json:"val_operator_addr"`
|
||||
WithdrawalHeight int64 `json:"withdrawal_height"` // last time this delegation withdrew rewards
|
||||
}
|
||||
|
||||
// withdraw rewards from delegator
|
||||
|
@ -27,3 +27,11 @@ func (di DelegatorDistInfo) WithdrawRewards(g Global, vi ValidatorDistInfo,
|
|||
|
||||
return di, g, withdrawalTokens
|
||||
}
|
||||
|
||||
//_____________________________________________________________________
|
||||
|
||||
// withdraw address for the delegation rewards
|
||||
type DelegatorWithdrawInfo struct {
|
||||
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
|
||||
WithdrawAddr sdk.AccAddress `json:"withdraw_addr"`
|
||||
}
|
||||
|
|
|
@ -8,6 +8,13 @@ type TotalAccum struct {
|
|||
Accum sdk.Dec `json:"accum"`
|
||||
}
|
||||
|
||||
func NewTotalAccum(height int64) TotalAccum {
|
||||
return TotalAccum{
|
||||
UpdateHeight: height,
|
||||
Accum: sdk.ZeroDec(),
|
||||
}
|
||||
}
|
||||
|
||||
// update total validator accumulation factor
|
||||
func (ta TotalAccum) Update(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum {
|
||||
blocks := height - ta.UpdateHeight
|
||||
|
@ -30,3 +37,12 @@ func (f FeePool) UpdateTotalValAccum(height int64, totalBondedTokens Dec) FeePoo
|
|||
f.ValAccum = f.ValAccum.Update(height, totalBondedTokens)
|
||||
return f
|
||||
}
|
||||
|
||||
// zero fee pool
|
||||
func InitialFeePool() FeePool {
|
||||
return FeePool{
|
||||
ValAccum: NewwTotalAccum(0),
|
||||
Pool: DecCoins{},
|
||||
CommunityPool: DecCoins{},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package types
|
||||
|
||||
import sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
// GenesisState - all distribution state that must be provided at genesis
|
||||
type GenesisState struct {
|
||||
FeePool FeePool `json:"fee_pool"`
|
||||
ValidatorDistInfos []ValidatorDistInfo `json:"validator_dist_infos"`
|
||||
DelegatorDistInfos []DelegatorDistInfo `json:"delegator_dist_infos"`
|
||||
DelegatorDistInfos []sdk.AccAddress `json:"delegator_dist_infos"`
|
||||
}
|
||||
|
||||
func NewGenesisState(feePool FeePool, vdis []ValidatorDistInfo, ddis []DelegatorDistInfo) GenesisState {
|
||||
return GenesisState{
|
||||
FeePool: feePool,
|
||||
ValidatorDistInfos: vdis,
|
||||
DelegatorDistInfos: ddis,
|
||||
}
|
||||
}
|
||||
|
||||
// get raw genesis raw message for testing
|
||||
func DefaultGenesisState() GenesisState {
|
||||
return GenesisState{
|
||||
FeePool: InitialFeePool(),
|
||||
}
|
||||
}
|
|
@ -4,13 +4,13 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
|
|||
|
||||
// distribution info for a particular validator
|
||||
type ValidatorDistInfo struct {
|
||||
OperatorAddr sdk.ValAddress
|
||||
OperatorAddr sdk.ValAddress `json:"operator_addr"`
|
||||
|
||||
GlobalWithdrawalHeight int64 // last height this validator withdrew from the global pool
|
||||
Pool DecCoins // rewards owed to delegators, commission has already been charged (includes proposer reward)
|
||||
PoolCommission DecCoins // commission collected by this validator (pending withdrawal)
|
||||
GlobalWithdrawalHeight int64 `json:"global_withdrawal_height"` // last height this validator withdrew from the global pool
|
||||
Pool DecCoins `json:"pool"` // rewards owed to delegators, commission has already been charged (includes proposer reward)
|
||||
PoolCommission DecCoins `json:"pool_commission"` // commission collected by this validator (pending withdrawal)
|
||||
|
||||
DelAccum TotalAccum `json:"dek_accum"` // total proposer pool accumulation factor held by delegators
|
||||
DelAccum TotalAccum `json:"del_accum"` // total proposer pool accumulation factor held by delegators
|
||||
}
|
||||
|
||||
// update total delegator accumululation
|
||||
|
|
Loading…
Reference in New Issue