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-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 ) {
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 )
2020-06-17 09:09:44 +00:00
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 )
2020-06-17 09:09:44 +00:00
if ! k . AccountIsVesting ( ctx , acc . GetAddress ( ) ) {
continue
}
vote , found := voteInfos . FilterByValidatorAddress ( acc . ValidatorAddress )
2019-09-23 18:23:00 +00:00
2020-06-17 09:09:44 +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
2020-06-17 09:09:44 +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
}
}
2020-06-17 09:09:44 +00:00
// handle any new/remaining debt on the account
k . HandleVestingDebt ( ctx , key , currentBlockTime )
2019-09-23 18:23:00 +00:00
}
2020-06-17 09:09:44 +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
}