Skip to content

Commit

Permalink
Trusted node sync
Browse files Browse the repository at this point in the history
Trusted node sync, aka checkpoint sync, allows syncing tyhe chain from a
trusted node instead of relying on a full sync from genesis.

Features include:

* sync from any slot, including the latest finalized slot
* backfill blocks either from the REST api (default) or p2p (#3263)

Future improvements:

* top up blocks between head in database and some other node - this
makes for an efficient backup tool
* recreate historical state to enable historical queries
  • Loading branch information
arnetheduck committed Jan 9, 2022
1 parent 20e700f commit 6160c7a
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 19 deletions.
36 changes: 27 additions & 9 deletions beacon_chain/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ const
defaultSigningNodeRequestTimeout* = 60

type
BNStartUpCmd* = enum
BNStartUpCmd* {.pure.} = enum
noCommand
createTestnet
deposits
wallets
record
web3
slashingdb
trustedNodeSync

WalletsCmd* {.pure.} = enum
create = "Creates a new EIP-2386 wallet"
Expand Down Expand Up @@ -177,9 +178,9 @@ type

case cmd* {.
command
defaultValue: noCommand }: BNStartUpCmd
defaultValue: BNStartUpCmd.noCommand }: BNStartUpCmd

of noCommand:
of BNStartUpCmd.noCommand:
bootstrapNodes* {.
desc: "Specifies one or more bootstrap nodes to use when connecting to the network"
abbr: "b"
Expand Down Expand Up @@ -417,7 +418,7 @@ type
defaultValue: false
name: "validator-monitor-totals" }: bool

of createTestnet:
of BNStartUpCmd.createTestnet:
testnetDepositsFile* {.
desc: "A LaunchPad deposits file for the genesis state validators"
name: "deposits-file" }: InputFile
Expand Down Expand Up @@ -451,7 +452,7 @@ type
desc: "Output file with list of bootstrap nodes for the network"
name: "output-bootstrap-file" }: OutFile

of wallets:
of BNStartUpCmd.wallets:
case walletsCmd* {.command.}: WalletsCmd
of WalletsCmd.create:
nextAccount* {.
Expand Down Expand Up @@ -484,7 +485,7 @@ type
of WalletsCmd.list:
discard

of deposits:
of BNStartUpCmd.deposits:
case depositsCmd* {.command.}: DepositsCmd
of DepositsCmd.createTestnetDeposits:
totalDeposits* {.
Expand Down Expand Up @@ -543,7 +544,7 @@ type
name: "epoch"
desc: "The desired exit epoch" }: Option[uint64]

of record:
of BNStartUpCmd.record:
case recordCmd* {.command.}: RecordCmd
of RecordCmd.create:
ipExt* {.
Expand Down Expand Up @@ -573,15 +574,15 @@ type
desc: "ENR URI of the record to print"
name: "enr" .}: Record

of web3:
of BNStartUpCmd.web3:
case web3Cmd* {.command.}: Web3Cmd
of Web3Cmd.test:
web3TestUrl* {.
argument
desc: "The web3 provider URL to test"
name: "url" }: Uri

of slashingdb:
of BNStartUpCmd.slashingdb:
case slashingdbCmd* {.command.}: SlashProtCmd
of SlashProtCmd.`import`:
importedInterchangeFile* {.
Expand All @@ -597,6 +598,23 @@ type
desc: "EIP-3076 slashing protection interchange file to export"
argument }: OutFile

of BNStartUpCmd.trustedNodeSync:
trustedNodeUrl* {.
desc: "URL of the REST API to sync from"
defaultValue: "http://localhost:5052"
name: "trusted-node-url"
.}: string

blockId* {.
desc: "Block id to sync to - this can be a block root, slot number, \"finalized\" or \"head\""
defaultValue: "finalized"
.}: string

backfillBlocks* {.
desc: "Backfill blocks directly from REST server instead of fetching via API"
defaultValue: true
name: "backfill"}: bool

ValidatorClientConf* = object
logLevel* {.
desc: "Sets the log level"
Expand Down
25 changes: 16 additions & 9 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import
# Local modules
"."/[
beacon_clock, beacon_chain_db, beacon_node, beacon_node_status,
conf, filepath, interop, nimbus_binary_common, statusbar,
conf, filepath, interop, nimbus_binary_common, statusbar, trusted_node_sync,
version],
./networking/[eth2_discovery, eth2_network, network_metadata],
./gossip_processing/[eth2_processor, block_processor, consensus_manager],
Expand Down Expand Up @@ -1875,7 +1875,6 @@ proc doSlashingImport(conf: BeaconNodeConf) {.raises: [SerializationError, IOErr
echo "Import finished: '", interchange, "' into '", dir/filetrunc & ".sqlite3", "'"

proc doSlashingInterchange(conf: BeaconNodeConf) {.raises: [Defect, CatchableError].} =
doAssert conf.cmd == slashingdb
case conf.slashingdbCmd
of SlashProtCmd.`export`:
conf.doSlashingExport()
Expand Down Expand Up @@ -1922,10 +1921,18 @@ programMain:
let rng = keys.newRng()

case config.cmd
of createTestnet: doCreateTestnet(config, rng[])
of noCommand: doRunBeaconNode(config, rng)
of deposits: doDeposits(config, rng[])
of wallets: doWallets(config, rng[])
of record: doRecord(config, rng[])
of web3: doWeb3Cmd(config)
of slashingdb: doSlashingInterchange(config)
of BNStartUpCmd.createTestnet: doCreateTestnet(config, rng[])
of BNStartUpCmd.noCommand: doRunBeaconNode(config, rng)
of BNStartUpCmd.deposits: doDeposits(config, rng[])
of BNStartUpCmd.wallets: doWallets(config, rng[])
of BNStartUpCmd.record: doRecord(config, rng[])
of BNStartUpCmd.web3: doWeb3Cmd(config)
of BNStartUpCmd.slashingdb: doSlashingInterchange(config)
of BNStartupCmd.trustedNodeSync:
# TODO use genesis state from metadata
waitFor doTrustedNodeSync(
getRuntimeConfig(config.eth2Network),
config.databaseDir,
config.trustedNodeUrl,
config.blockId,
config.backfillBlocks)
2 changes: 1 addition & 1 deletion beacon_chain/sync/sync_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ proc guardTask[A, B](man: SyncManager[A, B]) {.async.} =
man.workers[index].future = future
pending[index] = future

proc toTimeLeftString(d: Duration): string =
proc toTimeLeftString*(d: Duration): string =
if d == InfiniteDuration:
"--h--m"
else:
Expand Down
Loading

0 comments on commit 6160c7a

Please sign in to comment.