Skip to content

Commit 5daafd4

Browse files
authored
Slashing protection updates (#2333)
* Fix slashing protection always try to migrate at startup * Add CLI option for dual DB
1 parent 37838db commit 5daafd4

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

beacon_chain/conf.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ type
120120
desc: "Subscribe to all attestation subnet topics when gossiping"
121121
name: "subscribe-all-subnets" }: bool
122122

123+
# Can we use a set[enum]?
124+
testDualSlashingProtectionDBs* {.
125+
hidden
126+
defaultValue: false
127+
desc: "Use the the 2 slashing protection implementation at the same time to ensure no regression."
128+
name: "slashing-test-dual-db" }: bool
129+
123130
case cmd* {.
124131
command
125132
defaultValue: noCommand }: BNStartUpCmd
@@ -664,4 +671,3 @@ func defaultAdminListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): Valid
664671
template writeValue*(writer: var JsonWriter,
665672
value: TypedInputFile|InputFile|InputDir|OutPath|OutDir|OutFile) =
666673
writer.writeValue(string value)
667-

beacon_chain/nimbus_beacon_node.nim

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,24 @@ proc init*(T: type BeaconNode,
320320
topics &= getAttestationTopic(enrForkId.forkDigest, subnet)
321321
topics)
322322

323-
info "Loading slashing protection database", path = conf.validatorsDir()
324-
res.attachedValidators = ValidatorPool.init(
325-
SlashingProtectionDB.init(
326-
chainDag.headState.data.data.genesis_validators_root,
327-
conf.validatorsDir(), "slashing_protection"
323+
if conf.testDualSlashingProtectionDBs:
324+
info "Loading slashing protection database (dual DB mode)", path = conf.validatorsDir()
325+
res.attachedValidators = ValidatorPool.init(
326+
SlashingProtectionDB.init(
327+
chainDag.headState.data.data.genesis_validators_root,
328+
conf.validatorsDir(), "slashing_protection",
329+
modes = {kCompleteArchiveV1, kCompleteArchiveV2},
330+
disagreementBehavior = kChooseV2
331+
)
332+
)
333+
else:
334+
info "Loading slashing protection database", path = conf.validatorsDir()
335+
res.attachedValidators = ValidatorPool.init(
336+
SlashingProtectionDB.init(
337+
chainDag.headState.data.data.genesis_validators_root,
338+
conf.validatorsDir(), "slashing_protection"
339+
)
328340
)
329-
)
330341

331342
proc getWallTime(): BeaconTime = res.beaconClock.now()
332343

beacon_chain/validator_protection/slashing_protection.nim

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,6 @@ func version*(_: type SlashingProtectionDB): static int =
6767
# The highest DB version supported
6868
2
6969

70-
# DB Migration
71-
# -------------------------------------------------------------
72-
73-
proc requiresMigrationFromDB_v1(db: SlashingProtectionDB_v2): bool =
74-
## Migrate a v1 DB to v2.
75-
# Check if we have v2 data:
76-
let rawdb = kvstore db.getRawDBHandle()
77-
78-
let v1Root = rawdb.getMetadataTable_DbV1()
79-
if v1Root.isNone():
80-
return false
81-
82-
let v2Root = db.getMetadataTable_DbV2()
83-
if v2Root.isNone():
84-
return true
85-
86-
if v1Root != v2Root:
87-
fatal "Trying to merge-migrate slashing databases from different chains",
88-
v1Root = shortLog(v1Root.get()),
89-
v2Root = shortLog(v2Root.get())
90-
quit 1
91-
return true
92-
9370
# Resource Management
9471
# -------------------------------------------------------------
9572

@@ -113,12 +90,11 @@ proc init*(
11390
result.modes = modes
11491
result.disagreementBehavior = disagreementBehavior
11592

116-
result.db_v2 = SlashingProtectionDB_v2.initCompatV1(
93+
let (db, requiresMigration) = SlashingProtectionDB_v2.initCompatV1(
11794
genesis_validators_root,
11895
basePath, dbname
11996
)
120-
121-
let requiresMigration = result.db_v2.requiresMigrationFromDB_v1()
97+
result.db_v2 = db
12298

12399
let rawdb = kvstore result.db_v2.getRawDBHandle()
124100
if not rawdb.checkOrPutGenesis_DbV1(genesis_validators_root):
@@ -433,10 +409,4 @@ proc inclSPDIR*(db: SlashingProtectionDB, spdir: SPDIR): SlashingImportStatus
433409
# Sanity check
434410
# --------------------------------------------------------------
435411

436-
proc foo(db: SlashingProtectionDB_Concept) =
437-
discard
438-
439-
var x: SlashingProtectionDB
440-
foo(x) {.explain.}
441-
442412
static: doAssert SlashingProtectionDB is SlashingProtectionDB_Concept

beacon_chain/validator_protection/slashing_protection_v2.nim

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,24 +594,30 @@ proc getMetadataTable_DbV2*(db: SlashingProtectionDB_v2): Option[Eth2Digest] =
594594
proc initCompatV1*(T: type SlashingProtectionDB_v2,
595595
genesis_validators_root: Eth2Digest,
596596
basePath: string,
597-
dbname: string): T =
597+
dbname: string
598+
): tuple[db: SlashingProtectionDB_v2, requiresMigration: bool] =
598599
## Initialize a new slashing protection database
599600
## or load an existing one with matching genesis root
600601
## `dbname` MUST not be ending with .sqlite3
601602

602603
let alreadyExists = fileExists(basepath/dbname&".sqlite3")
603604

604-
result = T(backend: SqStoreRef.init(
605+
result.db = T(backend: SqStoreRef.init(
605606
basePath, dbname,
606607
keyspaces = ["kvstore"] # The key compat part
607608
).get())
608-
if alreadyExists and result.getMetadataTable_DbV2().isSome():
609-
result.checkDB(genesis_validators_root)
609+
if alreadyExists and result.db.getMetadataTable_DbV2().isSome():
610+
result.db.checkDB(genesis_validators_root)
611+
result.requiresMigration = false
612+
elif alreadyExists:
613+
result.db.setupDB(genesis_validators_root)
614+
result.requiresMigration = true
610615
else:
611-
result.setupDB(genesis_validators_root)
616+
result.db.setupDB(genesis_validators_root)
617+
result.requiresMigration = false
612618

613619
# Cached queries
614-
result.setupCachedQueries()
620+
result.db.setupCachedQueries()
615621

616622
# Resource Management
617623
# -------------------------------------------------------------

0 commit comments

Comments
 (0)