Skip to content

Commit

Permalink
Only slash after signedBlocksWindow has passed
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
mdyring committed Oct 20, 2020
1 parent 51d610e commit 20f826d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
23 changes: 12 additions & 11 deletions x/slashing/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper, batch

blockTimes := sk.getBlockTimes()
blockTimes = append(blockTimes, ctx.BlockTime())
blockTimes = truncateByWindow(ctx.BlockTime(), blockTimes, signedBlocksWindow)
_, blockTimes = truncateByWindow(ctx.BlockTime(), blockTimes, signedBlocksWindow)
sk.setBlockTimes(batch, blockTimes)

sk.handlePendingPenalties(ctx, batch, validatorset(req.LastCommitInfo.Votes))
Expand Down Expand Up @@ -61,17 +61,18 @@ func validatorset(validators []abci.VoteInfo) func() map[string]bool {
}
}

func truncateByWindow(blockTime time.Time, times []time.Time, signedBlocksWindow time.Duration) []time.Time {
if len(times) == 0 {
return times
}
func truncateByWindow(blockTime time.Time, times []time.Time, signedBlocksWindow time.Duration) (bool, []time.Time) {

if len(times) > 0 && times[0].Add(signedBlocksWindow).Before(blockTime) {
// Remove timestamps outside of the time window we are watching
threshold := blockTime.Add(-signedBlocksWindow)

// Remove timestamps outside of the time window we are watching
threshold := blockTime.Add(-1 * signedBlocksWindow)
index := sort.Search(len(times), func(i int) bool {
return times[i].After(threshold)
})

index := sort.Search(len(times), func(i int) bool {
return times[i].After(threshold)
})
return true, times[index:]
}

return times[index:]
return false, times
}
9 changes: 7 additions & 2 deletions x/slashing/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,20 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, batch db.Batch, addr c
consAddr := sdk.ConsAddress(addr)

missedBlocks := k.getMissingBlocksForValidator(consAddr)
missedBlocks = truncateByWindow(ctx.BlockTime(), missedBlocks, k.SignedBlocksWindowDuration(ctx))
truncated, missedBlocks := truncateByWindow(ctx.BlockTime(), missedBlocks, k.SignedBlocksWindowDuration(ctx))

if !signed {
missedBlocks = append(missedBlocks, ctx.BlockTime())
}

k.setMissingBlocksForValidator(batch, consAddr, missedBlocks)
missedBlockCount := sdk.NewInt(int64(len(missedBlocks))).ToDec()

// Validator is only slashable if the signed block window is full (was truncated)
if !truncated {
return
}

missedBlockCount := sdk.NewInt(int64(len(missedBlocks))).ToDec()
missedRatio := missedBlockCount.QuoInt64(blockCount)

// TODO Only do this if missing is true?
Expand Down

0 comments on commit 20f826d

Please sign in to comment.