Skip to content

Commit

Permalink
reset missed block counter and bit map
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Sep 10, 2024
1 parent 890fab3 commit 5aa71a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
21 changes: 0 additions & 21 deletions x/btcstaking/keeper/finality_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,6 @@ func (k Keeper) JailFinalityProvider(ctx context.Context, fpBTCPK []byte) error
return nil
}

// RevertSluggishFinalityProvider sets the Sluggish flag of the given finality provider
// to false
func (k Keeper) RevertSluggishFinalityProvider(ctx context.Context, fpBTCPK []byte) error {
// ensure finality provider exists
fp, err := k.GetFinalityProvider(ctx, fpBTCPK)
if err != nil {
return err
}

// ignore the finality provider is already slashed
// or detected as sluggish
if fp.IsSlashed() || fp.IsJailed() {
return nil
}

fp.Jailed = false
k.setFinalityProvider(ctx, fp)

return nil
}

// finalityProviderStore returns the KVStore of the finality provider set
// prefix: FinalityProviderKey
// key: Bitcoin secp256k1 PK
Expand Down
26 changes: 15 additions & 11 deletions x/finality/keeper/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,32 @@ func (k Keeper) HandleFinalityProviderLiveness(ctx context.Context, fpPk *types.
minHeight := signInfo.StartHeight + signedBlocksWindow
maxMissed := signedBlocksWindow - minSignedPerWindow

// if we are past the minimum height and the finality provider has missed too many blocks, punish them
// if the number of missed block reaches the threshold within the sliding window
// jail the finality provider
if height > minHeight && signInfo.MissedBlocksCounter > maxMissed {
updated = true

if err := k.jailSluggishFinalityProvider(ctx, fpPk); err != nil {
return fmt.Errorf("failed to jail sluggish finality provider %s: %w", fpPk.MarshalHex(), err)
}

signInfo.JailedUntil = sdkCtx.BlockHeader().Time.Add(params.JailDuration)
// we need to reset the counter & bitmap so that the finality provider won't be
// immediately jailed after unjailing.
signInfo.MissedBlocksCounter = 0
if err := k.DeleteMissedBlockBitmap(ctx, fpPk); err != nil {
return fmt.Errorf("failed to remove the missed block bit map: %w", err)
}

k.Logger(sdkCtx).Info(
"detected sluggish finality provider",
"finality provider is jailed",
"height", height,
"public_key", fpPk.MarshalHex(),
"missed_count", signInfo.MissedBlocksCounter,
"threshold", minSignedPerWindow,
"window_size", signedBlocksWindow,
)

// sluggish finality provider detected
if err := k.jailSluggishFinalityProvider(ctx, fpPk); err != nil {
panic(fmt.Errorf("failed to jail sluggish finality provider %s: %w", fpPk.MarshalHex(), err))
}
}

// Set the updated signing info
if updated {
signInfo.JailedUntil = sdkCtx.BlockHeader().Time.Add(params.JailDuration)
return k.FinalityProviderSigningTracker.Set(ctx, fpPk.MustMarshal(), *signInfo)
}

Expand Down

0 comments on commit 5aa71a5

Please sign in to comment.