0g-chain/x/validator-vesting/abci.go

71 lines
2.1 KiB
Go
Raw Normal View History

2019-09-23 18:23:00 +00:00
package validatorvesting
import (
"bytes"
2019-10-02 13:10:28 +00:00
2019-09-23 18:23:00 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2020-04-30 14:23:41 +00:00
2019-09-23 18:23:00 +00:00
abci "github.com/tendermint/tendermint/abci/types"
2020-04-30 14:13:31 +00:00
"github.com/kava-labs/kava/x/validator-vesting/keeper"
2019-09-23 18:23:00 +00:00
)
// BeginBlocker updates the vote signing information for each validator vesting account, updates account when period changes, and updates the previousBlockTime value in the store.
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
previousBlockTime, found := k.GetPreviousBlockTime(ctx)
if !found {
k.SetPreviousBlockTime(ctx, ctx.BlockTime())
return
2019-09-23 18:23:00 +00:00
}
2019-09-27 18:48:57 +00:00
currentBlockTime := ctx.BlockTime()
2019-09-23 18:23:00 +00:00
var voteInfos VoteInfos
2019-09-27 18:48:57 +00:00
voteInfos = req.LastCommitInfo.GetVotes()
2019-09-23 18:23:00 +00:00
validatorVestingKeys := k.GetAllAccountKeys(ctx)
2019-09-23 18:23:00 +00:00
for _, key := range validatorVestingKeys {
2019-10-04 17:55:49 +00:00
acc := k.GetAccountFromAuthKeeper(ctx, key)
if !k.AccountIsVesting(ctx, acc.GetAddress()) {
continue
}
vote, found := voteInfos.FilterByValidatorAddress(acc.ValidatorAddress)
2019-09-23 18:23:00 +00:00
var missedBlock bool
// if the validator was not found or explicitly didn't sign, increment the missing sign count
if (!found || !vote.SignedLastBlock) && ctx.BlockHeight() > 1 {
missedBlock = true
}
k.UpdateMissingSignCount(ctx, acc.GetAddress(), missedBlock)
2019-09-27 18:48:57 +00:00
// check if a period ended in the last block
endTimes := k.GetPeriodEndTimes(ctx, key)
for i, t := range endTimes {
if currentBlockTime.Unix() >= t && previousBlockTime.Unix() < t {
k.UpdateVestedCoinsProgress(ctx, key, i)
2019-09-23 18:23:00 +00:00
}
}
// handle any new/remaining debt on the account
k.HandleVestingDebt(ctx, key, currentBlockTime)
2019-09-23 18:23:00 +00:00
}
2019-09-23 18:23:00 +00:00
k.SetPreviousBlockTime(ctx, currentBlockTime)
}
// VoteInfos an array of abci.VoteInfo
type VoteInfos []abci.VoteInfo
2019-10-04 17:55:49 +00:00
// FilterByValidatorAddress returns the VoteInfo of the validator address matching the input validator address
// and a boolean for if the address was found.
func (vis VoteInfos) FilterByValidatorAddress(consAddress sdk.ConsAddress) (abci.VoteInfo, bool) {
2019-09-23 18:23:00 +00:00
for i, vi := range vis {
votingAddress := sdk.ConsAddress(vi.Validator.Address)
if bytes.Equal(consAddress, votingAddress) {
2019-10-04 17:55:49 +00:00
return vis[i], true
2019-09-23 18:23:00 +00:00
}
}
2019-10-04 17:55:49 +00:00
return abci.VoteInfo{}, false
2019-09-23 18:23:00 +00:00
}