From f7cddcc8ab7d7c10e4844ff700788b13790aa7ac Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Wed, 10 Mar 2021 16:53:42 +0100 Subject: [PATCH] Fix #2393 (#2395) * Fix #2393 * check both * Fix shortLog(int64) --- beacon_chain/validators/slashing_protection.nim | 4 ++-- .../validators/slashing_protection_common.nim | 8 ++++++-- beacon_chain/validators/slashing_protection_v1.nim | 11 +++++++---- beacon_chain/validators/slashing_protection_v2.nim | 8 +++++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/beacon_chain/validators/slashing_protection.nim b/beacon_chain/validators/slashing_protection.nim index 43b58e3e8d..0a6153eda4 100644 --- a/beacon_chain/validators/slashing_protection.nim +++ b/beacon_chain/validators/slashing_protection.nim @@ -337,8 +337,8 @@ proc pruneBlocks*( proc pruneAttestations*( db: SlashingProtectionDB, validator: ValidatorPubkey, - newMinSourceEpoch: Epoch, - newMinTargetEpoch: Epoch) = + newMinSourceEpoch: int64, + newMinTargetEpoch: int64) = ## Prune all blocks from a validator before the specified newMinSlot ## This is intended for interchange import to ensure ## that in case of a gap, we don't allow signing in that gap. diff --git a/beacon_chain/validators/slashing_protection_common.nim b/beacon_chain/validators/slashing_protection_common.nim index 387e132523..3f3d255589 100644 --- a/beacon_chain/validators/slashing_protection_common.nim +++ b/beacon_chain/validators/slashing_protection_common.nim @@ -117,7 +117,7 @@ type # Pruning # -------------------------------------------- db.pruneBlocks(ValidatorPubKey, Slot) - db.pruneAttestations(ValidatorPubKey, Epoch, Epoch) + db.pruneAttestations(ValidatorPubKey, int64, int64) db.pruneAfterFinalization(Epoch) # Interchange @@ -446,4 +446,8 @@ proc importInterchangeV5Impl*( # Now prune everything that predates # this interchange file max slot - db.pruneAttestations(parsedKey, Epoch maxValidSourceEpochSeen, Epoch maxValidTargetEpochSeen) + if maxValidSourceEpochSeen < 0 or maxValidTargetEpochSeen < 0: + doAssert maxValidSourceEpochSeen == -1 and maxValidTargetEpochSeen == -1 + notice "No attestation found in slashing interchange file" + return + db.pruneAttestations(parsedKey, maxValidSourceEpochSeen, maxValidTargetEpochSeen) diff --git a/beacon_chain/validators/slashing_protection_v1.nim b/beacon_chain/validators/slashing_protection_v1.nim index 920ee909ca..d35987d79a 100644 --- a/beacon_chain/validators/slashing_protection_v1.nim +++ b/beacon_chain/validators/slashing_protection_v1.nim @@ -955,16 +955,19 @@ proc pruneBlocks*(db: SlashingProtectionDB_v1, validator: ValidatorPubkey, newMi proc pruneAttestations*( db: SlashingProtectionDB_v1, validator: ValidatorPubkey, - newMinSourceEpoch: Epoch, - newMinTargetEpoch: Epoch) = + newMinSourceEpoch: int64, + newMinTargetEpoch: int64) = ## Prune all blocks from a validator before the specified newMinSlot ## This is intended for interchange import. ## ## Note: the Database v1 does not support pruning. + ## + ## Negative source/target epoch of -1 can be received if no attestation was imported + ## In that case nothing is done warn "Slashing DB pruning is not supported on the v1 of our database. Request ignored.", validator = shortLog(validator), - newMinSourceEpoch = shortLog(newMinSourceEpoch), - newMinTargetEpoch = shortLog(newMinTargetEpoch) + newMinSourceEpoch = newMinSourceEpoch, + newMinTargetEpoch = newMinTargetEpoch proc pruneAfterFinalization*( db: SlashingProtectionDB_v1, diff --git a/beacon_chain/validators/slashing_protection_v2.nim b/beacon_chain/validators/slashing_protection_v2.nim index 9d3337a7cb..337523a6f5 100644 --- a/beacon_chain/validators/slashing_protection_v2.nim +++ b/beacon_chain/validators/slashing_protection_v2.nim @@ -1020,14 +1020,16 @@ proc pruneBlocks*(db: SlashingProtectionDB_v2, validator: ValidatorPubkey, newMi proc pruneAttestations*( db: SlashingProtectionDB_v2, validator: ValidatorPubkey, - newMinSourceEpoch: Epoch, - newMinTargetEpoch: Epoch) = + newMinSourceEpoch: int64, + newMinTargetEpoch: int64) = ## Prune all blocks from a validator before the specified newMinSlot ## This is intended for interchange import. + ## Negative source/target epoch of -1 can be received if no attestation was imported + ## In that case nothing is done (since we used signed int in SQLite) let valID = db.getOrRegisterValidator(validator) let status = db.sqlPruneValidatorAttestations.exec( - (valID, int64 newMinSourceEpoch, int64 newMinTargetEpoch)) + (valID, newMinSourceEpoch, newMinTargetEpoch)) doAssert status.isOk(), "SQLite error when pruning validator attestations: " & $status.error & "\n" & "for validator: 0x" & validator.toHex() &