import hook from fee-distr

This commit is contained in:
rigelrozanski 2018-09-22 16:29:42 -04:00
parent 6012a252bb
commit 6f35822d7d
9 changed files with 128 additions and 22 deletions

View File

@ -91,7 +91,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams) 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.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, 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.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.stakeKeeper = app.stakeKeeper.WithHooks(app.slashingKeeper.Hooks())
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace)) app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection) app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)

View File

@ -102,12 +102,25 @@ type DelegationSet interface {
fn func(index int64, delegation Delegation) (stop bool)) fn func(index int64, delegation Delegation) (stop bool))
} }
// validator event hooks //_______________________________________________________________________________
// These can be utilized to communicate between a staking keeper // Event Hooks
// and another keeper which must take particular actions when // These can be utilized to communicate between a staking keeper and another
// validators are bonded and unbonded. The second keeper must implement // keeper which must take particular actions when validators/delegators change
// this interface, which then the staking keeper can call. // state. The second keeper must implement this interface, which then the
type ValidatorHooks interface { // staking keeper can call.
// TODO refactor event hooks out to the receiver modules
// event hooks for staking validator object
type StakingHooks interface {
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created
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
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
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
} }

View File

@ -29,10 +29,10 @@ type Hooks struct {
k Keeper k Keeper
} }
var _ sdk.ValidatorHooks = Hooks{} var _ sdk.StakingHooks = Hooks{}
// Return the wrapper struct // Return the wrapper struct
func (k Keeper) ValidatorHooks() Hooks { func (k Keeper) Hooks() Hooks {
return Hooks{k} return Hooks{k}
} }
@ -45,3 +45,11 @@ func (h Hooks) OnValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) {
func (h Hooks) OnValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress) { func (h Hooks) OnValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress) {
h.k.onValidatorBeginUnbonding(ctx, address) h.k.onValidatorBeginUnbonding(ctx, address)
} }
// nolint - unused hooks
func (h Hooks) OnValidatorCreated(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) OnValidatorCommissionChange(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) OnValidatorRemoved(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) OnDelegationCreated(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}
func (h Hooks) OnDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}
func (h Hooks) OnDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}

View File

@ -27,7 +27,7 @@ func TestHandleDoubleSign(t *testing.T) {
// initial setup // initial setup
ctx, ck, sk, _, keeper := createTestInput(t) ctx, ck, sk, _, keeper := createTestInput(t)
sk = sk.WithValidatorHooks(keeper.ValidatorHooks()) sk = sk.WithHooks(keeper.Hooks())
amtInt := int64(100) amtInt := int64(100)
addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt) addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt)
got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, val, amt)) got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, val, amt))
@ -69,7 +69,7 @@ func TestSlashingPeriodCap(t *testing.T) {
// initial setup // initial setup
ctx, ck, sk, _, keeper := createTestInput(t) ctx, ck, sk, _, keeper := createTestInput(t)
sk = sk.WithValidatorHooks(keeper.ValidatorHooks()) sk = sk.WithHooks(keeper.Hooks())
amtInt := int64(100) amtInt := int64(100)
addr, amt := addrs[0], sdk.NewInt(amtInt) addr, amt := addrs[0], sdk.NewInt(amtInt)
valConsPubKey, valConsAddr := pks[0], sdk.ConsAddress(pks[0].Address()) valConsPubKey, valConsAddr := pks[0], sdk.ConsAddress(pks[0].Address())
@ -125,7 +125,7 @@ func TestHandleAbsentValidator(t *testing.T) {
// initial setup // initial setup
ctx, ck, sk, _, keeper := createTestInput(t) ctx, ck, sk, _, keeper := createTestInput(t)
sk = sk.WithValidatorHooks(keeper.ValidatorHooks()) sk = sk.WithHooks(keeper.Hooks())
amtInt := int64(100) amtInt := int64(100)
addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt) addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt)
sh := stake.NewHandler(sk) sh := stake.NewHandler(sk)

View File

@ -87,6 +87,11 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k
return err.Result() return err.Result()
} }
// call hooks
k.OnValidatorCreated(ctx, validator.OperatorAddr)
accAddr := sdk.AccAddress(validator.OperatorAddr)
k.OnDelegationCreated(ctx, accAddr, validator.OperatorAddr)
tags := sdk.NewTags( tags := sdk.NewTags(
tags.Action, tags.ActionCreateValidator, tags.Action, tags.ActionCreateValidator,
tags.DstValidator, []byte(msg.ValidatorAddr.String()), tags.DstValidator, []byte(msg.ValidatorAddr.String()),
@ -146,6 +151,9 @@ func handleMsgDelegate(ctx sdk.Context, msg types.MsgDelegate, k keeper.Keeper)
return err.Result() return err.Result()
} }
// call the hook if present
k.OnDelegationCreated(ctx, msg.DelegatorAddr, validator.OperatorAddr)
tags := sdk.NewTags( tags := sdk.NewTags(
tags.Action, tags.ActionDelegate, tags.Action, tags.ActionDelegate,
tags.Delegator, []byte(msg.DelegatorAddr.String()), tags.Delegator, []byte(msg.DelegatorAddr.String()),

View File

@ -67,6 +67,9 @@ func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) {
// remove a delegation from store // remove a delegation from store
func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) { func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) {
// call hook
k.OnDelegationRemoved(ctx, delegation.DelegatorAddr, delegation.ValidatorAddr)
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Delete(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr)) store.Delete(GetDelegationKey(delegation.DelegatorAddr, delegation.ValidatorAddr))
} }
@ -283,6 +286,9 @@ func (k Keeper) Delegate(ctx sdk.Context, delAddr sdk.AccAddress, bondAmt sdk.Co
func (k Keeper) unbond(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, func (k Keeper) unbond(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress,
shares sdk.Dec) (amount sdk.Dec, err sdk.Error) { shares sdk.Dec) (amount sdk.Dec, err sdk.Error) {
// call hook
k.OnDelegationSharesModified(ctx, delAddr, valAddr)
// check if delegation has any shares in it unbond // check if delegation has any shares in it unbond
delegation, found := k.GetDelegation(ctx, delAddr, valAddr) delegation, found := k.GetDelegation(ctx, delAddr, valAddr)
if !found { if !found {
@ -334,6 +340,9 @@ func (k Keeper) unbond(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValA
k.RemoveValidator(ctx, validator.OperatorAddr) k.RemoveValidator(ctx, validator.OperatorAddr)
} }
// call hook
k.OnDelegationSharesModified(ctx, delegation.DelegatorAddr, validator.OperatorAddr)
return amount, nil return amount, nil
} }

54
x/stake/keeper/hooks.go Normal file
View File

@ -0,0 +1,54 @@
//nolint
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Expose the hooks if present
func (k Keeper) OnValidatorCreated(ctx sdk.Context, address sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnValidatorCreated(ctx, address)
}
}
func (k Keeper) OnValidatorCommissionChange(ctx sdk.Context, address sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnValidatorCommissionChange(ctx, address)
}
}
func (k Keeper) OnValidatorRemoved(ctx sdk.Context, address sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnValidatorRemoved(ctx, address)
}
}
func (k Keeper) OnValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) {
if k.hooks != nil {
k.hooks.OnValidatorBonded(ctx, address)
}
}
func (k Keeper) OnValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress) {
if k.hooks != nil {
k.hooks.OnValidatorBeginUnbonding(ctx, address)
}
}
func (k Keeper) OnDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnDelegationCreated(ctx, delAddr, valAddr)
}
}
func (k Keeper) OnDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnDelegationSharesModified(ctx, delAddr, valAddr)
}
}
func (k Keeper) OnDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
if k.hooks != nil {
k.hooks.OnDelegationRemoved(ctx, delAddr, valAddr)
}
}

View File

@ -14,7 +14,7 @@ type Keeper struct {
storeTKey sdk.StoreKey storeTKey sdk.StoreKey
cdc *codec.Codec cdc *codec.Codec
bankKeeper bank.Keeper bankKeeper bank.Keeper
hooks sdk.ValidatorHooks hooks sdk.StakingHooks
// codespace // codespace
codespace sdk.CodespaceType codespace sdk.CodespaceType
@ -33,7 +33,7 @@ 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(sh sdk.ValidatorHooks) Keeper { func (k Keeper) WithHooks(sh sdk.StakingHooks) Keeper {
if k.hooks != nil { if k.hooks != nil {
panic("cannot set validator hooks twice") panic("cannot set validator hooks twice")
} }

View File

@ -633,10 +633,8 @@ func (k Keeper) beginUnbondingValidator(ctx sdk.Context, validator types.Validat
// also remove from the Bonded types.Validators Store // also remove from the Bonded types.Validators Store
store.Delete(GetValidatorsBondedIndexKey(validator.OperatorAddr)) store.Delete(GetValidatorsBondedIndexKey(validator.OperatorAddr))
// call the unbond hook if present // call hook
if k.hooks != nil { k.OnValidatorBeginUnbonding(ctx, validator.ConsAddress())
k.hooks.OnValidatorBeginUnbonding(ctx, validator.ConsAddress())
}
// return updated validator // return updated validator
return validator return validator
@ -668,10 +666,8 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.
tstore := ctx.TransientStore(k.storeTKey) tstore := ctx.TransientStore(k.storeTKey)
tstore.Set(GetTendermintUpdatesTKey(validator.OperatorAddr), bzABCI) tstore.Set(GetTendermintUpdatesTKey(validator.OperatorAddr), bzABCI)
// call the bond hook if present // call hook
if k.hooks != nil { k.OnValidatorBonded(ctx, validator.ConsAddress())
k.hooks.OnValidatorBonded(ctx, validator.ConsAddress())
}
// return updated validator // return updated validator
return validator return validator
@ -680,6 +676,9 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.
// remove the validator record and associated indexes // remove the validator record and associated indexes
func (k Keeper) RemoveValidator(ctx sdk.Context, address sdk.ValAddress) { func (k Keeper) RemoveValidator(ctx sdk.Context, address sdk.ValAddress) {
// call hook
k.OnValidatorRemoved(ctx, address)
// first retrieve the old validator record // first retrieve the old validator record
validator, found := k.GetValidator(ctx, address) validator, found := k.GetValidator(ctx, address)
if !found { if !found {
@ -739,3 +738,18 @@ func ensureValidatorFound(found bool, ownerAddr []byte) {
panic(fmt.Sprintf("validator record not found for address: %X\n", ownerAddr)) panic(fmt.Sprintf("validator record not found for address: %X\n", ownerAddr))
} }
} }
//__________________________________________________________________________
// XXX remove this code - this is should be superceded by commission work that bez is doing
// get a single validator
func (k Keeper) UpdateValidatorCommission(ctx sdk.Context, addr sdk.ValAddress, newCommission sdk.Dec) sdk.Error {
// call hook
k.OnValidatorCommissionChange(ctx, addr)
validator, _ := k.GetValidator(ctx, addr)
validator.Commission = newCommission
k.SetValidator(ctx, validator)
return nil
}