0g-chain/x/validator-vesting/spec/03_begin_block.md

46 lines
2.9 KiB
Markdown
Raw Normal View History

2019-09-23 19:12:18 +00:00
# Begin Block
2019-09-27 19:00:24 +00:00
At each `BeginBlock`, all validator vesting accounts are iterated over to update the status of the current vesting period. Note that the address of each account is retreived by iterating over the keys in the `validator-vesting` store, while the account objects are stored and accessed using the `auth` module's `AccountKeeper`. For each account, the block count is incremented, the missed sign count is incremented if the validator did not sign the block or was not found in the validator set. By comparing the blocktime of the current `BeginBlock`, with the value of `previousBlockTime` stored in the `validator-vesting` store, it is determined if the end of the current period has been reached. If the current period has ended, the `VestingPeriodProgress` field is updated to reflect if the coins for the ending period successfully vested or not. After updates are made regarding the status of the current vesting period, any outstanding debt on the account is attempted to be collected. If there is enough `SpendableBalance` on the account to cover the debt, coins are sent to the `ReturnAdress` or burned. If there is not enough `SpendableBalance` to cover the debt, all delegations of the account are `Unbonded`. Once those unbonding events reach maturity, the coins freed from the undonding will be used to cover the debt. Finally, the time of the previous block is stored in the validator vesting account keeper, which is used to determine when a period has ended.
2019-09-23 19:12:18 +00:00
```go
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
previousBlockTime := time.Time{}
if ctx.BlockHeight() > 1 {
previousBlockTime = k.GetPreviousBlockTime(ctx)
}
2019-09-27 19:00:24 +00:00
currentBlockTime := ctx.BlockTime()
2019-09-23 19:12:18 +00:00
var voteInfos VoteInfos
2019-09-27 19:00:24 +00:00
voteInfos = req.LastCommitInfo.GetVotes()
2019-09-23 19:12:18 +00:00
validatorVestingKeys := k.GetAllAccountKeys(ctx)
for _, key := range validatorVestingKeys {
2019-09-27 19:00:24 +00:00
acc := k.GetAccountFromAuthKeeper(ctx, key[1:])
2019-09-23 19:12:18 +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 19:00:24 +00:00
endTimes := k.GetPeriodEndTimes(ctx, key[1:])
2019-09-23 19:12:18 +00:00
for i, t := range endTimes {
if currentBlockTime.Unix() >= t && previousBlockTime.Unix() < t {
2019-09-27 19:00:24 +00:00
k.UpdateVestedCoinsProgress(ctx, key[1:], i)
2019-09-23 19:12:18 +00:00
}
}
// handle any new/remaining debt on the account
2019-09-27 19:00:24 +00:00
k.HandleVestingDebt(ctx, key[1:], currentBlockTime)
2019-09-23 19:12:18 +00:00
}
k.SetPreviousBlockTime(ctx, currentBlockTime)
}
```