Skip to content

Commit 80ca7a2

Browse files
committed
Implement #1768
1 parent 17d35e1 commit 80ca7a2

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

beacon_chain/conf.nim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import
55
chronicles, chronicles/options as chroniclesOptions,
66
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
77
stew/io2, unicodedb/properties, normalize,
8+
eth/common/eth_types as commonEthTypes,
89
json_serialization, web3/[ethtypes, confutils_defs],
910
spec/[crypto, keystore, digest, datatypes, network],
1011
network_metadata, filepath
@@ -78,7 +79,7 @@ type
7879

7980
depositContractDeployedAt* {.
8081
desc: "The Eth1 block number or hash where the deposit contract has been deployed"
81-
name: "deposit-contract-block" }: Option[string]
82+
name: "deposit-contract-block" }: Option[BlockHashOrNumber]
8283

8384
nonInteractive* {.
8485
desc: "Do not display interative prompts. Quit on missing configuration"
@@ -451,6 +452,13 @@ func parseCmdArg*(T: type GraffitiBytes, input: TaintedString): T
451452
func completeCmdArg*(T: type GraffitiBytes, input: TaintedString): seq[string] =
452453
return @[]
453454

455+
func parseCmdArg*(T: type BlockHashOrNumber, input: TaintedString): T
456+
{.raises: [ValueError, Defect].} =
457+
init(BlockHashOrNumber, string input)
458+
459+
func completeCmdArg*(T: type BlockHashOrNumber, input: TaintedString): seq[string] =
460+
return @[]
461+
454462
func parseCmdArg*(T: type Checkpoint, input: TaintedString): T
455463
{.raises: [ValueError, Defect].} =
456464
let sepIdx = find(input.string, ':')

beacon_chain/eth1_monitor.nim

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type
4848
Eth1Monitor* = ref object
4949
db: BeaconChainDB
5050
preset: RuntimePreset
51+
depositContractDeployedAt: BlockHashOrNumber
5152

5253
dataProvider: Web3DataProviderRef
5354

@@ -395,7 +396,7 @@ proc init*(T: type Eth1Monitor,
395396
preset: RuntimePreset,
396397
web3Url: string,
397398
depositContractAddress: Eth1Address,
398-
depositContractDeployedAt: string,
399+
depositContractDeployedAt: BlockHashOrNumber,
399400
eth1Network: Option[Eth1Network]): Future[Result[T, string]] {.async.} =
400401
var web3Url = web3Url
401402
fixupWeb3Urls web3Url
@@ -422,31 +423,12 @@ proc init*(T: type Eth1Monitor,
422423
return err("The specified web3 provider is not attached to the " &
423424
$eth1Network.get & " network")
424425

425-
let
426-
previouslyPersistedTo = db.getEth1PersistedTo()
427-
knownStart = previouslyPersistedTo.get:
428-
# `previouslyPersistedTo` wall null, we start from scratch
429-
let deployedAtHash = if depositContractDeployedAt.startsWith "0x":
430-
try: BlockHash.fromHex depositContractDeployedAt
431-
except ValueError:
432-
return err "Invalid hex value specified for deposit-contract-block"
433-
else:
434-
let blockNum = try: parseBiggestUInt depositContractDeployedAt
435-
except ValueError:
436-
return err "Invalid nummeric value for deposit-contract-block"
437-
try:
438-
let blk = await dataProvider.getBlockByNumber(blockNum)
439-
blk.hash
440-
except CatchableError:
441-
return err("Failed to obtain block hash for block number " & $blockNum)
442-
Eth1Data(block_hash: deployedAtHash.asEth2Digest, deposit_count: 0)
443-
444426
return ok T(
445427
db: db,
446428
preset: preset,
429+
depositContractDeployedAt: depositContractDeployedAt,
447430
dataProvider: dataProvider,
448-
eth1Progress: newAsyncEvent(),
449-
eth1Chain: Eth1Chain(knownStart: knownStart))
431+
eth1Progress: newAsyncEvent())
450432

451433
proc allDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[Deposit] =
452434
for i in 0'u64 ..< totalDeposits:
@@ -643,6 +625,7 @@ proc handleEth1Progress(m: Eth1Monitor) {.async.} =
643625
# If we had the same code embedded in the new block headers event,
644626
# it could easily re-order the steps due to the interruptible
645627
# interleaved execution of async code.
628+
646629
var eth1SyncedTo = await m.dataProvider.getBlockNumber(
647630
m.eth1Chain.knownStart.block_hash.asBlockHash)
648631

@@ -686,6 +669,24 @@ proc run(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
686669
info "Starting Eth1 deposit contract monitoring",
687670
contract = $m.depositContractAddress, url = m.web3Url
688671

672+
let previouslyPersistedTo = m.db.getEth1PersistedTo()
673+
m.eth1Chain.knownStart = previouslyPersistedTo.get:
674+
# `previouslyPersistedTo` wall null, we start from scratch
675+
let deployedAtHash = if m.depositContractDeployedAt.isHash:
676+
m.depositContractDeployedAt.hash
677+
else:
678+
var blk: BlockObject
679+
while true:
680+
try:
681+
blk = await m.dataProvider.getBlockByNumber(m.depositContractDeployedAt.number)
682+
break
683+
except CatchableError as err:
684+
error "Failed to obtain details for the starting block of the deposit contract sync. " &
685+
"The Web3 provider may still be not fully synced", error = err.msg
686+
await sleepAsync(chronos.seconds(10))
687+
blk.hash.asEth2Digest
688+
Eth1Data(block_hash: deployedAtHash, deposit_count: 0)
689+
689690
await m.dataProvider.onBlockHeaders do (blk: Eth1BlockHeader)
690691
{.raises: [Defect], gcsafe.}:
691692
try:

beacon_chain/network_metadata.nim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import
22
tables, strutils, os,
33
stew/shims/macros, nimcrypto/hash,
4+
eth/common/eth_types as commonEthTypes,
45
web3/[ethtypes, conversions],
56
chronicles,
67
spec/presets,
@@ -47,7 +48,7 @@ type
4748
bootstrapNodes*: seq[string]
4849

4950
depositContractAddress*: Eth1Address
50-
depositContractDeployedAt*: string
51+
depositContractDeployedAt*: BlockHashOrNumber
5152

5253
# Please note that we are using `string` here because SSZ.decode
5354
# is not currently usable at compile time and we want to load the
@@ -123,6 +124,11 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
123124
else:
124125
""
125126

127+
depositContractDeployedAt = if depositContractBlock.len > 0:
128+
BlockHashOrNumber.init(depositContractBlock)
129+
else:
130+
BlockHashOrNumber(isHash: false, number: 1)
131+
126132
bootstrapNodes = if fileExists(bootstrapNodesPath):
127133
readFile(bootstrapNodesPath).splitLines()
128134
else:
@@ -137,7 +143,7 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
137143
runtimePreset: runtimePreset,
138144
bootstrapNodes: bootstrapNodes,
139145
depositContractAddress: depositContractAddress,
140-
depositContractDeployedAt: depositContractBlock,
146+
depositContractDeployedAt: depositContractDeployedAt,
141147
genesisData: genesisData)
142148

143149
except PresetIncompatible as err:
@@ -154,7 +160,7 @@ const
154160
# TODO(zah) Add bootstrap nodes for mainnet
155161
bootstrapNodes: @[],
156162
depositContractAddress: Eth1Address.fromHex "0x00000000219ab540356cBB839Cbe05303d7705Fa",
157-
depositContractDeployedAt: "11052984",
163+
depositContractDeployedAt: BlockHashOrNumber.init "11052984",
158164
genesisData: "")
159165
else:
160166
Eth2NetworkMetadata(

0 commit comments

Comments
 (0)