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

78 lines
2.7 KiB
Go
Raw Normal View History

2019-09-23 18:23:00 +00:00
package validatorvesting
import (
"bytes"
"time"
2019-10-02 13:10:28 +00:00
tmtime "github.com/tendermint/tendermint/types/time"
2019-09-23 18:23:00 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2019-10-02 13:10:28 +00:00
"github.com/kava-labs/kava/x/validator-vesting/internal/keeper"
2019-09-23 18:23:00 +00:00
abci "github.com/tendermint/tendermint/abci/types"
)
// 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) {
2019-10-02 13:10:28 +00:00
previousBlockTime := tmtime.Canonical(time.Unix(0, 0))
2019-09-23 18:23:00 +00:00
if ctx.BlockHeight() > 1 {
previousBlockTime = k.GetPreviousBlockTime(ctx)
}
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)
for _, key := range validatorVestingKeys {
2019-09-27 18:48:57 +00:00
acc := k.GetAccountFromAuthKeeper(ctx, key[1:])
2019-09-23 18:23:00 +00:00
if voteInfos.ContainsValidatorAddress(acc.ValidatorAddress) {
vote := voteInfos.MustFilterByValidatorAddress(acc.ValidatorAddress)
if !vote.SignedLastBlock {
// if the validator explicitly missed signing the block, increment the missing sign count
k.UpdateMissingSignCount(ctx, acc.GetAddress(), true)
} else {
k.UpdateMissingSignCount(ctx, acc.GetAddress(), false)
}
} else {
// if the validator was not a voting member of the validator set, increment the missing sign count
k.UpdateMissingSignCount(ctx, acc.GetAddress(), true)
}
// check if a period ended in the last block
2019-09-27 18:48:57 +00:00
endTimes := k.GetPeriodEndTimes(ctx, key[1:])
2019-09-23 18:23:00 +00:00
for i, t := range endTimes {
if currentBlockTime.Unix() >= t && previousBlockTime.Unix() < t {
2019-09-27 18:48:57 +00:00
k.UpdateVestedCoinsProgress(ctx, key[1:], i)
2019-09-23 18:23:00 +00:00
}
}
2019-09-23 19:12:18 +00:00
// handle any new/remaining debt on the account
2019-09-27 18:48:57 +00:00
k.HandleVestingDebt(ctx, key[1:], currentBlockTime)
2019-09-23 18:23:00 +00:00
}
k.SetPreviousBlockTime(ctx, currentBlockTime)
}
// VoteInfos an array of abci.VoteInfo
type VoteInfos []abci.VoteInfo
// ContainsValidatorAddress returns true if the input validator address is found in the VoteInfos array
func (vis VoteInfos) ContainsValidatorAddress(consAddress sdk.ConsAddress) bool {
for _, vi := range vis {
votingAddress := sdk.ConsAddress(vi.Validator.Address)
if bytes.Equal(consAddress, votingAddress) {
return true
}
}
return false
}
// MustFilterByValidatorAddress returns the VoteInfo that has a validator address matching the input validator address
func (vis VoteInfos) MustFilterByValidatorAddress(consAddress sdk.ConsAddress) abci.VoteInfo {
for i, vi := range vis {
votingAddress := sdk.ConsAddress(vi.Validator.Address)
if bytes.Equal(consAddress, votingAddress) {
return vis[i]
}
}
panic("validator address not found")
}