Skip to content

Commit 5c313b9

Browse files
committed
Simplify the slashing db import/export CLI
1 parent dacc508 commit 5c313b9

File tree

7 files changed

+58
-95
lines changed

7 files changed

+58
-95
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ other clients and bringing further performance optimizations.
66

77
**New features:**
88

9-
* A new `slashingProtection` sub-command offering import and export options
10-
for the EIP-3076 slashing protection interchange format. Please see the
11-
the prepared [migration guides](https://nimbus.guide/migrate.html) outlining
12-
the safest way to migrate to Nimbus from other clients.
9+
* A new `slashingdb` sub-command offering import and export options for the
10+
EIP-3076 slashing protection interchange format. Please see the the prepared
11+
[migration guides](https://nimbus.guide/migrate.html) outlining the safest
12+
way to migrate to Nimbus from other clients.
1313

1414
* Pruning of the slashing protection database and transition to more optimal
1515
queries *->* significant reduction of disk and CPU usage on nodes running

beacon_chain/conf.nim

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ type
7676
file
7777

7878
SlashProtCmd* = enum
79-
exportAll = "Export the whole validator slashing protection DB to json."
80-
`import` = "Import the slashing protection json to the node."
81-
`export` = "Export specified validators slashing protection data to json"
79+
`import` = "Import a EIP-3076 slashing protection interchange file"
80+
`export` = "Export a EIP-3076 slashing protection interchange file"
8281
# migrateAll = "Export and remove the whole validator slashing protection DB."
8382
# migrate = "Export and remove specified validators from Nimbus."
8483

@@ -496,18 +495,20 @@ type
496495
name: "url" }: Uri
497496

498497
of slashingdb:
499-
# TODO: when this is not set, confutils crashes
500-
interchangeFile* {.
501-
desc: "Path to the slashing interchange file"
502-
name: "interchange" .}: Option[string]
503-
504498
case slashingdbCmd* {.command.}: SlashProtCmd
505-
of SlashProtCmd.exportAll, SlashProtCmd.`import`:
506-
discard
499+
of SlashProtCmd.`import`:
500+
importedInterchangeFile* {.
501+
desc: "EIP-3076 slashing protection interchange file to import"
502+
argument .}: InputFile
507503
of SlashProtCmd.`export`:
508-
validators* {.
509-
argument
510-
desc: "One or more validators to export".}: seq[string]
504+
exportedValidators* {.
505+
desc: "Limit the export to specific validators " &
506+
"(specified as numeric indices or public keys)"
507+
abbr: "v"
508+
name: "validator" }: seq[string]
509+
exportedInterchangeFile* {.
510+
desc: "EIP-3076 slashing protection interchange file to export"
511+
argument }: OutFile
511512

512513
ValidatorClientConf* = object
513514
logLevel* {.

beacon_chain/nimbus_beacon_node.nim

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,42 +1936,17 @@ proc doSlashingExport(conf: BeaconNodeConf) {.raises: [IOError, Defect].}=
19361936
# TODO: Make it read-only https://github.com/status-im/nim-eth/issues/312
19371937
let db = SlashingProtectionDB.loadUnchecked(dir, filetrunc, readOnly = false)
19381938

1939-
let interchange = block:
1940-
if conf.interchangeFile.isSome():
1941-
string(conf.interchangeFile.unsafeGet())
1942-
else:
1943-
conf.validatorsDir() & "/" & "interchange-" & "" & ".json"
1944-
1945-
db.exportSlashingInterchange(interchange)
1939+
let interchange = conf.exportedInterchangeFile.string
1940+
db.exportSlashingInterchange(interchange, conf.exportedValidators)
19461941
echo "Export finished: '", dir/filetrunc & ".sqlite3" , "' into '", interchange, "'"
19471942

1948-
proc doSlashingPartialExport(conf: BeaconNodeConf) {.raises: [IOError, Defect].} =
1949-
let
1950-
dir = conf.validatorsDir()
1951-
filetrunc = SlashingDbName
1952-
# TODO: Make it read-only https://github.com/status-im/nim-eth/issues/312
1953-
let db = SlashingProtectionDB.loadUnchecked(dir, filetrunc, readOnly = false)
1954-
1955-
let interchange = block:
1956-
if conf.interchangeFile.isSome():
1957-
string(conf.interchangeFile.unsafeGet())
1958-
else:
1959-
conf.validatorsDir() & "/" & "interchange-" & "" & ".json"
1960-
1961-
db.exportPartialSlashingInterchange(conf.validators, interchange)
1962-
echo "Partial export finished: '", dir/filetrunc/".sqlite3" , "' into '", interchange, "'"
1963-
19641943
proc doSlashingImport(conf: BeaconNodeConf) {.raises: [SerializationError, IOError, Defect].} =
19651944
let
19661945
dir = conf.validatorsDir()
19671946
filetrunc = SlashingDbName
19681947
# TODO: Make it read-only https://github.com/status-im/nim-eth/issues/312
19691948

1970-
let interchange = block:
1971-
if conf.interchangeFile.isSome():
1972-
string(conf.interchangeFile.unsafeGet())
1973-
else:
1974-
conf.validatorsDir() & "/" & "interchange-" & "" & ".json"
1949+
let interchange = conf.importedInterchangeFile.string
19751950

19761951
var spdir: SPDIR
19771952
try:
@@ -2003,10 +1978,8 @@ proc doSlashingImport(conf: BeaconNodeConf) {.raises: [SerializationError, IOErr
20031978
proc doSlashingInterchange(conf: BeaconNodeConf) {.raises: [Defect, CatchableError].} =
20041979
doAssert conf.cmd == slashingdb
20051980
case conf.slashingdbCmd
2006-
of SlashProtCmd.exportAll:
2007-
conf.doSlashingExport()
20081981
of SlashProtCmd.`export`:
2009-
conf.doSlashingPartialExport()
1982+
conf.doSlashingExport()
20101983
of SlashProtCmd.`import`:
20111984
conf.doSlashingImport()
20121985

beacon_chain/validators/slashing_protection.nim

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
import
1111
# stdlib
12-
std/os,
12+
std/[os, algorithm, sequtils],
1313
# Status
1414
eth/db/[kvstore, kvstore_sqlite3],
15-
stew/results, chronicles,
15+
stew/[results, byteutils],
16+
chronicles,
1617
# Internal
1718
../spec/[datatypes, digest, crypto],
1819
./slashing_protection_common,
@@ -167,13 +168,6 @@ proc loadUnchecked*(
167168
except:
168169
result.disagreementBehavior = kChooseV1
169170

170-
try:
171-
result.db_v1.fromRawDB(kvstore result.db_v2.getRawDBHandle())
172-
result.modes.incl(kCompleteArchiveV1)
173-
except:
174-
doAssert result.modes.card > 0, "Couldn't open the DB."
175-
result.disagreementBehavior = kChooseV2
176-
177171
proc close*(db: SlashingProtectionDB) =
178172
## Close a slashing protection database
179173
db.db_v2.close()
@@ -307,3 +301,24 @@ proc inclSPDIR*(db: SlashingProtectionDB, spdir: SPDIR): SlashingImportStatus
307301
proc toSPDIR*(db: SlashingProtectionDB): SPDIR
308302
{.raises: [IOError, Defect].} =
309303
db.db_v2.toSPDIR()
304+
305+
proc exportSlashingInterchange*(
306+
db: SlashingProtectionDB,
307+
path: string,
308+
validatorsWhiteList: seq[string] = @[],
309+
prettify = true) {.raises: [Defect, IOError].} =
310+
## Export a database to the Slashing Protection Database Interchange Format
311+
# We could modify toSPDIR to do the filtering directly
312+
# but this is not a performance sensitive operation.
313+
# so it's better to keep it simple.
314+
var spdir = db.toSPDIR()
315+
316+
if validatorsWhiteList.len > 0:
317+
# O(a log b) with b the number of validators to keep
318+
# and a the total number of validators in DB
319+
let validators = validatorsWhiteList.sorted()
320+
spdir.data.keepItIf(validators.binarySearch("0x" & it.pubkey.PubKeyBytes.toHex()) != -1)
321+
322+
Json.saveFile(path, spdir, prettify)
323+
echo "Exported slashing protection DB to '", path, "'"
324+

beacon_chain/validators/slashing_protection_common.nim

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,6 @@ proc readValue*(r: var JsonReader, a: var (SlotString or EpochString))
222222
except ValueError:
223223
raiseUnexpectedValue(r, "Integer in a string expected")
224224

225-
proc exportSlashingInterchange*(
226-
db: auto,
227-
path: string, prettify = true) {.raises: [Defect, IOError].} =
228-
## Export a database to the Slashing Protection Database Interchange Format
229-
let spdir = db.toSPDIR()
230-
Json.saveFile(path, spdir, prettify)
231-
echo "Exported slashing protection DB to '", path, "'"
232-
233-
proc exportPartialSlashingInterchange*(
234-
db: SlashingProtectionDB_Concept,
235-
validators: seq[string],
236-
path: string, prettify = true) {.raises: [Defect, IOError].} =
237-
## Export a database to the Slashing Protection Database Interchange Format
238-
# We could modify toSPDIR to do the filtering directly
239-
# but this is not a performance sensitive operation.
240-
# so it's better to keep it simple.
241-
var spdir = db.toSPDIR()
242-
243-
# O(a log b) with b the number of validators to keep
244-
# and a the total number of validators in DB
245-
let validators = validators.sorted()
246-
spdir.data.keepItIf(validators.binarySearch("0x" & it.pubkey.PubKeyBytes.toHex()) != -1)
247-
248-
Json.saveFile(path, spdir, prettify)
249-
echo "Exported slashing protection DB to '", path, "'"
250-
251225
proc importSlashingInterchange*(
252226
db: auto,
253227
path: string): SlashingImportStatus {.raises: [Defect, IOError, SerializationError].} =

docs/slashing_interchange.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Importing and exporting validators is available via the following commands:
44

5-
- `path/to/nimbus_beacon_node slashingdb import --interchange=infile.json`
6-
- `path/to/nimbus_beacon_node slashingdb exportAll --interchange=outfile.json`
7-
- `path/to/nimbus_beacon_node slashingdb export --interchange=outfile.json --validators=0xAAAA...AAA --validators=0xBBBB...BBBB --validators=0xCCCC...CCCC`
5+
- `path/to/nimbus_beacon_node slashingdb import infile.json`
6+
- `path/to/nimbus_beacon_node slashingdb export outfile.json`
7+
- `path/to/nimbus_beacon_node slashingdb export outfile.json --validator=0xAAAA...AAA --validator=0xBBBB...BBBB --validator=0xCCCC...CCCC`
88

99
## Importing new validators
1010

@@ -13,7 +13,7 @@ Importing and exporting validators is available via the following commands:
1313
The default command for import into the database is:
1414

1515
```
16-
build/nimbus_beacon_node slashingdb import --interchange=interchange.json
16+
build/nimbus_beacon_node slashingdb import interchange.json
1717
```
1818

1919
### With specified validators folder
@@ -22,23 +22,23 @@ The validators folder contains the valdiators setup.
2222
By default it is `path/to/datadir/validators`
2323

2424
```
25-
build/nimbus_beacon_node slashingdb exportAll --interchange=interchange.json --validators-dir=path/to/validatorsdir/
25+
build/nimbus_beacon_node slashingdb export interchange.json --validators-dir=path/to/validatorsdir/
2626
```
2727

2828
### With the data-dir folder
2929

3030
The data-dir contains the beacon node setup.
3131

3232
```
33-
build/nimbus_beacon_node slashingdb exportAll --interchange=interchange.json --data-dir=path/to/datadir/
33+
build/nimbus_beacon_node slashingdb export interchange.json --data-dir=path/to/datadir/
3434
```
3535

3636
## Exporting all validators
3737

3838
The default command for exporting the database is:
3939

4040
```
41-
build/nimbus_beacon_node slashingdb exportAll --interchange=interchange.json
41+
build/nimbus_beacon_node slashingdb export interchange.json
4242
```
4343

4444
On success you will have a message similar to:
@@ -54,22 +54,22 @@ The validators folder contains the valdiators setup.
5454
By default it is `path/to/datadir/validators`
5555

5656
```
57-
build/nimbus_beacon_node slashingdb exportAll --interchange=interchange.json --validators-dir=path/to/validatorsdir/
57+
build/nimbus_beacon_node slashingdb export interchange.json --validators-dir=path/to/validatorsdir/
5858
```
5959

6060
### With the data-dir folder
6161

6262
The data-dir contains the beacon node setup.
6363

6464
```
65-
build/nimbus_beacon_node slashingdb exportAll --interchange=interchange.json --data-dir=path/to/datadir/
65+
build/nimbus_beacon_node slashingdb export interchange.json --data-dir=path/to/datadir/
6666
```
6767

6868
## Partial exports
6969

7070
Partial export can be done by specifying the public keys of the relevant validators.
71-
The `--validators` command can be specified multiple time, once per validator.
71+
The `--validator` command can be specified multiple time, once per validator.
7272

7373
```
74-
build/nimbus_beacon_node slashingdb export --interchange=interchange.json --validators=0xb5da853a51d935da6f3bd46934c719fcca1bbf0b493264d3d9e7c35a1023b73c703b56d598edf0239663820af36ec615
74+
build/nimbus_beacon_node slashingdb export interchange.json --validator=0xb5da853a51d935da6f3bd46934c719fcca1bbf0b493264d3d9e7c35a1023b73c703b56d598edf0239663820af36ec615
7575
```

0 commit comments

Comments
 (0)