Skip to content

Commit 17d35e1

Browse files
committed
Allow the node to start when it fails to initialize the Eth1 monitor
* Avoid hangs when wss:// is specified for a non-secure HTTP server * Produce an ERROR when the web3 provider is unsupported, but still launch the node
1 parent 5e45e74 commit 17d35e1

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

beacon_chain/eth1_monitor.nim

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,14 @@ proc init*(T: type Eth1Monitor,
400400
var web3Url = web3Url
401401
fixupWeb3Urls web3Url
402402

403-
let web3 = try: await newWeb3(web3Url)
404-
except CatchableError as err:
405-
return err "Failed to setup web3 connection"
403+
let web3Fut = newWeb3(web3Url)
404+
yield web3Fut or sleepAsync(chronos.seconds(5))
405+
if (not web3Fut.finished) or web3Fut.failed:
406+
web3Fut.cancel()
407+
return err "Failed to setup web3 connection"
408+
406409
let
410+
web3 = web3Fut.read
407411
ns = web3.contractSender(DepositContract, depositContractAddress)
408412
dataProvider = Web3DataProviderRef(url: web3Url, web3: web3, ns: ns)
409413

@@ -415,7 +419,7 @@ proc init*(T: type Eth1Monitor,
415419
of rinkeby: "4"
416420
of goerli: "5"
417421
if expectedNetwork != providerNetwork:
418-
return err("The specified we3 provider is not attached to the " &
422+
return err("The specified web3 provider is not attached to the " &
419423
$eth1Network.get & " network")
420424

421425
let
@@ -538,21 +542,6 @@ proc safeCancel(fut: var Future[void]) =
538542
proc stop*(m: Eth1Monitor) =
539543
safeCancel m.runFut
540544

541-
proc waitGenesis*(m: Eth1Monitor): Future[BeaconStateRef] {.async.} =
542-
if m.genesisState.isNil:
543-
if m.genesisStateFut.isNil:
544-
m.genesisStateFut = newFuture[void]("waitGenesis")
545-
546-
info "Awaiting genesis event"
547-
await m.genesisStateFut
548-
m.genesisStateFut = nil
549-
550-
if m.genesisState != nil:
551-
return m.genesisState
552-
else:
553-
doAssert bnStatus == BeaconNodeStatus.Stopping
554-
return new BeaconStateRef # cannot return nil...
555-
556545
proc syncBlockRange(m: Eth1Monitor, fromBlock, toBlock: Eth1BlockNumber) {.async.} =
557546
var currentBlock = fromBlock
558547
while currentBlock <= toBlock:
@@ -729,6 +718,23 @@ proc start(m: Eth1Monitor, delayBeforeStart: Duration) =
729718
proc start*(m: Eth1Monitor) {.inline.} =
730719
m.start(0.seconds)
731720

721+
proc waitGenesis*(m: Eth1Monitor): Future[BeaconStateRef] {.async.} =
722+
if m.genesisState.isNil:
723+
m.start()
724+
725+
if m.genesisStateFut.isNil:
726+
m.genesisStateFut = newFuture[void]("waitGenesis")
727+
728+
info "Awaiting genesis event"
729+
await m.genesisStateFut
730+
m.genesisStateFut = nil
731+
732+
if m.genesisState != nil:
733+
return m.genesisState
734+
else:
735+
doAssert bnStatus == BeaconNodeStatus.Stopping
736+
return new BeaconStateRef # cannot return nil...
737+
732738
proc getEth1BlockHash*(url: string, blockId: RtBlockIdentifier): Future[BlockHash] {.async.} =
733739
let web3 = await newWeb3(url)
734740
try:

beacon_chain/nimbus_beacon_node.nim

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,6 @@ func enrForkIdFromState(state: BeaconState): ENRForkID =
6666
next_fork_version: forkVer,
6767
next_fork_epoch: FAR_FUTURE_EPOCH)
6868

69-
proc startEth1Monitor(db: BeaconChainDB,
70-
eth1Network: Option[Eth1Network],
71-
conf: BeaconNodeConf): Future[Eth1Monitor] {.async.} =
72-
let eth1MonitorRes = await Eth1Monitor.init(
73-
db,
74-
conf.runtimePreset,
75-
conf.web3Url,
76-
conf.depositContractAddress.get,
77-
conf.depositContractDeployedAt.get,
78-
eth1Network)
79-
80-
result = if eth1MonitorRes.isOk:
81-
eth1MonitorRes.get
82-
else:
83-
fatal "Failed to start Eth1 monitor",
84-
reason = eth1MonitorRes.error,
85-
web3Url = conf.web3Url,
86-
depositContractAddress = conf.depositContractAddress.get,
87-
depositContractDeployedAt = conf.depositContractDeployedAt.get
88-
quit 1
89-
90-
result.start()
91-
9269
proc init*(T: type BeaconNode,
9370
rng: ref BrHmacDrbgContext,
9471
conf: BeaconNodeConf,
@@ -162,7 +139,23 @@ proc init*(T: type BeaconNode,
162139

163140
# TODO Could move this to a separate "GenesisMonitor" process or task
164141
# that would do only this - see Paul's proposal for this.
165-
eth1Monitor = await startEth1Monitor(db, eth1Network, conf)
142+
let eth1MonitorRes = await Eth1Monitor.init(
143+
db,
144+
conf.runtimePreset,
145+
conf.web3Url,
146+
conf.depositContractAddress.get,
147+
conf.depositContractDeployedAt.get,
148+
eth1Network)
149+
150+
if eth1MonitorRes.isErr:
151+
fatal "Failed to start Eth1 monitor",
152+
reason = eth1MonitorRes.error,
153+
web3Url = conf.web3Url,
154+
depositContractAddress = conf.depositContractAddress.get,
155+
depositContractDeployedAt = conf.depositContractDeployedAt.get
156+
quit 1
157+
else:
158+
eth1Monitor = eth1MonitorRes.get
166159

167160
genesisState = await eth1Monitor.waitGenesis()
168161
if bnStatus == BeaconNodeStatus.Stopping:
@@ -236,7 +229,22 @@ proc init*(T: type BeaconNode,
236229
conf.depositContractDeployedAt.isSome:
237230
# TODO(zah) if we don't have any validators attached,
238231
# we don't need a mainchain monitor
239-
eth1Monitor = await startEth1Monitor(db, eth1Network, conf)
232+
let eth1MonitorRes = await Eth1Monitor.init(
233+
db,
234+
conf.runtimePreset,
235+
conf.web3Url,
236+
conf.depositContractAddress.get,
237+
conf.depositContractDeployedAt.get,
238+
eth1Network)
239+
240+
if eth1MonitorRes.isErr:
241+
error "Failed to start Eth1 monitor",
242+
reason = eth1MonitorRes.error,
243+
web3Url = conf.web3Url,
244+
depositContractAddress = conf.depositContractAddress.get,
245+
depositContractDeployedAt = conf.depositContractDeployedAt.get
246+
else:
247+
eth1Monitor = eth1MonitorRes.get
240248

241249
let rpcServer = if conf.rpcEnabled:
242250
RpcServer.init(conf.rpcAddress, conf.rpcPort)
@@ -823,6 +831,10 @@ proc start(node: BeaconNode) =
823831
notice "Waiting for genesis", genesisIn = genesisTime.offset
824832

825833
waitFor node.initializeNetworking()
834+
835+
if node.eth1Monitor != nil:
836+
node.eth1Monitor.start()
837+
826838
node.run()
827839

828840
func formatGwei(amount: uint64): string =

0 commit comments

Comments
 (0)