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"
2020-04-04 23:26:15 +00:00
"github.com/kava-labs/kava/x/validator-vesting/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-10-04 17:55:49 +00:00
acc := k . GetAccountFromAuthKeeper ( ctx , key )
2019-10-09 22:42:23 +00:00
if k . AccountIsVesting ( ctx , acc . GetAddress ( ) ) {
vote , found := voteInfos . FilterByValidatorAddress ( acc . ValidatorAddress )
if ! found || ! vote . SignedLastBlock {
if ctx . BlockHeight ( ) <= 1 {
// don't count missed blocks on block 1 since there is no vote history
k . UpdateMissingSignCount ( ctx , acc . GetAddress ( ) , false )
} else {
// if the validator was not found or explicitly didn't sign, increment the missing sign count
k . UpdateMissingSignCount ( ctx , acc . GetAddress ( ) , true )
}
} else {
k . UpdateMissingSignCount ( ctx , acc . GetAddress ( ) , false )
}
2019-09-23 18:23:00 +00:00
2019-10-09 22:42:23 +00:00
// check if a period ended in the last block
endTimes := k . GetPeriodEndTimes ( ctx , key )
2019-09-27 18:48:57 +00:00
2019-10-09 22:42:23 +00:00
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
}
2019-10-09 22:42:23 +00:00
// handle any new/remaining debt on the account
k . HandleVestingDebt ( ctx , key , currentBlockTime )
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
}