This commit is contained in:
rigelrozanski 2018-09-18 12:46:04 -04:00
parent 431fdb8695
commit 24e43b9869
7 changed files with 105 additions and 74 deletions

View File

@ -96,7 +96,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
auth.ProtoBaseAccount, // prototype auth.ProtoBaseAccount, // prototype
) )
// add handlers // add handlers and hooks
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper) app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace)) app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams) app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
@ -208,6 +208,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.StakeData) slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.StakeData)
gov.InitGenesis(ctx, app.govKeeper, genesisState.GovData) gov.InitGenesis(ctx, app.govKeeper, genesisState.GovData)
distr.InitGenesis(ctx, app.distrKeeper, genesisState.DistrData)
err = GaiaValidateGenesisState(genesisState) err = GaiaValidateGenesisState(genesisState)
if err != nil { if err != nil {
// TODO find a way to do this w/o panics // TODO find a way to do this w/o panics
@ -235,6 +236,7 @@ func (app *GaiaApp) ExportAppStateAndValidators() (appState json.RawMessage, val
genState := GenesisState{ genState := GenesisState{
Accounts: accounts, Accounts: accounts,
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper), StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
DistrData: distr.WriteGenesis(ctx, app.distrKeeper),
GovData: gov.WriteGenesis(ctx, app.govKeeper), GovData: gov.WriteGenesis(ctx, app.govKeeper),
} }
appState, err = codec.MarshalJSONIndent(app.cdc, genState) appState, err = codec.MarshalJSONIndent(app.cdc, genState)
@ -244,3 +246,39 @@ func (app *GaiaApp) ExportAppStateAndValidators() (appState json.RawMessage, val
validators = stake.WriteValidators(ctx, app.stakeKeeper) validators = stake.WriteValidators(ctx, app.stakeKeeper)
return appState, validators, nil return appState, validators, nil
} }
//______________________________________________________________________________________________
// Combine Staking Hooks
type Hooks struct {
dh distr.Hooks
sh slashing.Hooks
}
var _ sdk.StakingHooks = Hooks{}
// nolint
func (h Hooks) OnValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorCreated(ctx, addr)
}
func (h Hooks) OnValidatorCommissionChange(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorCommissionChange(ctx, addr)
}
func (h Hooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorRemoved(ctx, addr)
}
func (h Hooks) OnValidatorBonded(ctx sdk.Context, addr sdk.ConsAddress) {
h.sh.OnValidatorBonded(ctx, addr)
}
func (h Hooks) OnValidatorBeginBonded(ctx sdk.Context, addr sdk.ConsAddress) {
h.sh.OnValidatorBeginBonding(ctx, addr)
}
func (h Hooks) OnDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
h.dh.OnDelegationCreated(ctx, delAddr, valAddr)
}
func (h Hooks) OnDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
h.dh.OnDelegationSharesModified(ctx, delAddr, valAddr)
}
func (h Hooks) OnDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
h.dh.OnDelegationRemoved(ctx, delAddr, valAddr)
}

View File

@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
distr "github.com/cosmos/cosmos-sdk/x/stake"
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types" stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -34,6 +35,7 @@ var (
type GenesisState struct { type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"` Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"` StakeData stake.GenesisState `json:"stake"`
DistrData distr.GenesisState `json:"distr_data"`
GovData gov.GenesisState `json:"gov"` GovData gov.GenesisState `json:"gov"`
} }
@ -194,6 +196,7 @@ func GaiaAppGenState(cdc *codec.Codec, appGenTxs []json.RawMessage) (genesisStat
genesisState = GenesisState{ genesisState = GenesisState{
Accounts: genaccs, Accounts: genaccs,
StakeData: stakeData, StakeData: stakeData,
DistrData: distr.DefaultGenesisState(),
GovData: gov.DefaultGenesisState(), GovData: gov.DefaultGenesisState(),
} }
return return

View File

@ -107,18 +107,17 @@ type DelegationSet interface {
// state. The second keeper must implement this interface, which then the // state. The second keeper must implement this interface, which then the
// staking keeper can call. // staking keeper can call.
// TODO refactor event hooks out to the receiver modules
// event hooks for staking validator object // event hooks for staking validator object
type ValidatorHooks interface { type StakingHooks interface {
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created 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 OnValidatorCommissionChange(ctx Context, address ValAddress) // Must be called when a validator's commission is modified
OnValidatorRemoved(ctx Context, address ValAddress) // Must be called when a validator is deleted 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 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 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 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 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 OnDelegationRemoved(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation is removed

View File

@ -1,8 +1,6 @@
package distribution package distribution
import ( import (
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/cosmos/cosmos-sdk/x/stake/types"
) )
@ -13,7 +11,7 @@ import (
// addition, it also sets any delegations found in data. Finally, it updates // addition, it also sets any delegations found in data. Finally, it updates
// the bonded validators. // the bonded validators.
// Returns final validator set after applying all declaration and delegations // 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) { func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
keeper.SetFeePool(ctx, data.FeePool) keeper.SetFeePool(ctx, data.FeePool)
for _, vdi := range data.ValidatorDistInfos { for _, vdi := range data.ValidatorDistInfos {
@ -25,8 +23,6 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res [
for _, dw := range data.DelegatorWithdrawAddrs { for _, dw := range data.DelegatorWithdrawAddrs {
keeper.SetDelegatorWithdrawAddr(ctx, dw.DelegatorAddr, dw.WithdrawAddr) keeper.SetDelegatorWithdrawAddr(ctx, dw.DelegatorAddr, dw.WithdrawAddr)
} }
return
} }
// WriteGenesis returns a GenesisState for a given context and keeper. The // WriteGenesis returns a GenesisState for a given context and keeper. The

View File

@ -1,6 +1,9 @@
package distribution package distribution
import "github.com/cosmos/cosmos-sdk/x/distribution/types" import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
/* /*
## Create or modify delegation distribution ## Create or modify delegation distribution
@ -43,15 +46,9 @@ func (k Keeper) onValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) {
k.SetValidatorDistInfo(ctx, vdi) k.SetValidatorDistInfo(ctx, vdi)
} }
// Withdrawal all distubution rewards // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX // Withdrawal all validator rewards
func (k Keeper) onValidatorBondModified(ctx sdk.Context, addr sdk.ValAddress) { func (k Keeper) onValidatorCommissionChange(ctx sdk.Context, addr sdk.ValAddress) {
slashingPeriod := ValidatorSlashingPeriod{ k.WithdrawValidatorRewardsAll(ctx, addr)
ValidatorAddr: address,
StartHeight: ctx.BlockHeight(),
EndHeight: 0,
SlashedSoFar: sdk.ZeroDec(),
}
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
} }
// Withdrawal all validator distribution rewards and cleanup the distribution record // Withdrawal all validator distribution rewards and cleanup the distribution record
@ -61,60 +58,56 @@ func (k Keeper) onValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
//_________________________________________________________________________________________ //_________________________________________________________________________________________
// Create a new validator distribution record // Create a new delegator distribution record
func (k Keeper) onDelegationCreated(ctx sdk.Context, address sdk.ConsAddress) { func (k Keeper) onDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress,
slashingPeriod := ValidatorSlashingPeriod{ valAddr sdk.ValAddress) {
ValidatorAddr: address,
StartHeight: ctx.BlockHeight(), ddi := types.DelegatorDistInfo{
EndHeight: 0, DelegatorAddr: delAddr,
SlashedSoFar: sdk.ZeroDec(), ValOperatorAddr: valAddr,
WithdrawalHeight: ctx.BlockHeight(),
} }
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod) k.SetDelegatorDistInfo(ctx, ddi)
}
// Withdrawal all validator rewards
func (k Keeper) onDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress,
valAddr sdk.ValAddress) {
k.WithdrawDelegationReward(ctx, delAddr, valAddr)
}
// Withdrawal all validator distribution rewards and cleanup the distribution record
func (k Keeper) onDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress,
valAddr sdk.ValAddress) {
k.RemoveDelegatorDistInfo(ctx, delAddr, valAddr)
} }
//_________________________________________________________________________________________ //_________________________________________________________________________________________
// Wrapper struct for sdk.ValidatorHooks // Wrapper struct
type ValidatorHooks struct { type Hooks struct {
k Keeper k Keeper
} }
var _ sdk.ValidatorHooks = ValidatorHooks{}
// nolint // nolint
func (k Keeper) ValidatorHooks() sdk.ValidatorHooks { return ValidatorHooks{k} } func (k Keeper) ValidatorHooks() sdk.ValidatorHooks { return ValidatorHooks{k} }
func (v ValidatorHooks) OnValidatorCreated(ctx sdk.Context, addr sdk.VlAddress) { func (h Hooks) OnValidatorCreated(ctx sdk.Context, addr sdk.VlAddress) {
v.k.OnValidatorCreated(ctx, address) v.k.onValidatorCreated(ctx, address)
} }
func (v ValidatorHooks) OnValidatorBondModified(ctx sdk.Context, addr sdk.ValAddress) { func (h Hooks) OnValidatorCommissionChange(ctx sdk.Context, addr sdk.ValAddress) {
v.k.OnValidatorBondModified(ctx, address) v.k.onValidatorCommissionChange(ctx, address)
} }
func (v ValidatorHooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) { func (h Hooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
v.k.OnValidatorRemoved(ctx, address) v.k.onValidatorRemoved(ctx, address)
} }
func (v ValidatorHooks) OnValidatorBonded(_ sdk.Context, _ sdk.ConsAddress) {} func (h Hooks) OnDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
func (v ValidatorHooks) OnValidatorBeginBonded(_ sdk.Context, _ sdk.ConsAddress) {} h.k.onDelegationCreated(ctx, delAddr, valAddr)
//_________________________________________________________________________________________
// Wrapper struct for sdk.DelegationHooks
type DelegationHooks struct {
k Keeper
} }
func (h Hooks) OnDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
var _ sdk.DelegationHooks = DelegationHooks{} d.k.onDelegationSharesModified(ctx, delAddr, valAddr)
// 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, func (h Hooks) OnDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
delAddr sdk.AccAddress, valAddr sdk.ValAddress) { h.k.onDelegationRemoved(ctx, delAddr, valAddr)
d.k.OnDelegationSharesModified(ctx, address)
}
func (d DelegationHooks) OnDelegationRemoved(ctx sdk.Context,
delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
d.k.OnDelegationRemoved(ctx, address)
} }

View File

@ -35,12 +35,13 @@ func (k Keeper) RemoveValidatorDistInfo(ctx sdk.Context, valAddr sdk.ValAddress)
// withdrawal all the validator rewards including the commission // withdrawal all the validator rewards including the commission
func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context,
operatorAddr sdk.ValAddress, withdrawAddr sdk.AccAddress) { operatorAddr sdk.ValAddress) {
// withdraw self-delegation // withdraw self-delegation
height := ctx.BlockHeight() height := ctx.BlockHeight()
validator := k.GetValidator(ctx, operatorAddr) validator := k.GetValidator(ctx, operatorAddr)
withdraw := k.GetDelegatorRewardsAll(ctx, validator.OperatorAddr, height) accAddr := sdk.AccAddress{operatorAddr.Bytes()}
withdraw := k.GetDelegatorRewardsAll(ctx, accAddr, height)
// withdrawal validator commission rewards // withdrawal validator commission rewards
pool := k.stakeKeeper.GetPool(ctx) pool := k.stakeKeeper.GetPool(ctx)
@ -51,5 +52,6 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context,
withdraw = withdraw.Add(commission) withdraw = withdraw.Add(commission)
k.SetFeePool(feePool) k.SetFeePool(feePool)
withdrawAddr := k.GetDelegatorWithdrawAddr(accAddr)
k.coinKeeper.AddCoins(withdrawAddr, withdraw.TruncateDecimal()) k.coinKeeper.AddCoins(withdrawAddr, withdraw.TruncateDecimal())
} }

View File

@ -10,11 +10,11 @@ import (
// keeper of the stake store // keeper of the stake store
type Keeper struct { type Keeper struct {
storeKey sdk.StoreKey storeKey sdk.StoreKey
storeTKey sdk.StoreKey storeTKey sdk.StoreKey
cdc *codec.Codec cdc *codec.Codec
bankKeeper bank.Keeper bankKeeper bank.Keeper
validatorHooks sdk.ValidatorHooks hooks sdk.StakingHooks
// codespace // codespace
codespace sdk.CodespaceType codespace sdk.CodespaceType
@ -33,11 +33,11 @@ func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, codespa
} }
// Set the validator hooks // Set the validator hooks
func (k Keeper) WithValidatorHooks(v sdk.ValidatorHooks) Keeper { func (k Keeper) WithHooks(sh sdk.StakingHooks) Keeper {
if k.validatorHooks != nil { if k.stakingHooks != nil {
panic("cannot set validator hooks twice") panic("cannot set validator hooks twice")
} }
k.validatorHooks = v k.validatorHooks = sh
return k return k
} }