2022-05-31 10:45:37 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
2022-07-14 04:07:40 +00:00
|
|
|
chronicles, chronicles/chronos_tools, chronos,
|
2022-05-31 10:45:37 +00:00
|
|
|
eth/keys,
|
2022-07-14 04:07:40 +00:00
|
|
|
./eth1/eth1_monitor,
|
2022-08-25 03:53:59 +00:00
|
|
|
./gossip_processing/optimistic_processor,
|
|
|
|
./networking/topic_params,
|
2022-05-31 10:45:37 +00:00
|
|
|
./spec/beaconstate,
|
2022-08-25 03:53:59 +00:00
|
|
|
./spec/datatypes/[phase0, altair, bellatrix],
|
2022-05-31 10:45:37 +00:00
|
|
|
"."/[light_client, nimbus_binary_common, version]
|
|
|
|
|
2022-07-14 04:07:40 +00:00
|
|
|
from ./consensus_object_pools/consensus_manager import runForkchoiceUpdated
|
|
|
|
from ./gossip_processing/block_processor import newExecutionPayload
|
2022-08-25 03:53:59 +00:00
|
|
|
from ./gossip_processing/eth2_processor import toValidationResult
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
programMain:
|
|
|
|
var config = makeBannerAndConfig(
|
|
|
|
"Nimbus light client " & fullVersionStr, LightClientConf)
|
|
|
|
setupLogging(config.logLevel, config.logStdout, config.logFile)
|
|
|
|
|
|
|
|
notice "Launching light client",
|
|
|
|
version = fullVersionStr, cmdParams = commandLineParams(), config
|
|
|
|
|
|
|
|
let metadata = loadEth2Network(config.eth2Network)
|
|
|
|
for node in metadata.bootstrapNodes:
|
|
|
|
config.bootstrapNodes.add node
|
|
|
|
template cfg(): auto = metadata.cfg
|
|
|
|
|
|
|
|
let
|
|
|
|
genesisState =
|
|
|
|
try:
|
|
|
|
template genesisData(): auto = metadata.genesisData
|
|
|
|
newClone(readSszForkedHashedBeaconState(
|
|
|
|
cfg, genesisData.toOpenArrayByte(genesisData.low, genesisData.high)))
|
|
|
|
except CatchableError as err:
|
|
|
|
raiseAssert "Invalid baked-in state: " & err.msg
|
|
|
|
|
|
|
|
beaconClock = BeaconClock.init(
|
|
|
|
getStateField(genesisState[], genesis_time))
|
|
|
|
getBeaconTime = beaconClock.getBeaconTimeFn()
|
|
|
|
|
|
|
|
genesis_validators_root =
|
|
|
|
getStateField(genesisState[], genesis_validators_root)
|
|
|
|
forkDigests = newClone ForkDigests.init(cfg, genesis_validators_root)
|
|
|
|
|
|
|
|
genesisBlockRoot = get_initial_beacon_block(genesisState[]).root
|
|
|
|
|
|
|
|
rng = keys.newRng()
|
2022-07-13 21:26:16 +00:00
|
|
|
netKeys = getRandomNetKeys(rng[])
|
2022-05-31 10:45:37 +00:00
|
|
|
network = createEth2Node(
|
|
|
|
rng, config, netKeys, cfg,
|
|
|
|
forkDigests, getBeaconTime, genesis_validators_root)
|
|
|
|
|
2022-07-14 04:07:40 +00:00
|
|
|
eth1Monitor =
|
|
|
|
if config.web3Urls.len > 0:
|
2022-07-29 08:45:39 +00:00
|
|
|
let res = Eth1Monitor.init(
|
2022-07-14 04:07:40 +00:00
|
|
|
cfg, db = nil, getBeaconTime, config.web3Urls,
|
|
|
|
none(DepositContractSnapshot), metadata.eth1Network,
|
|
|
|
forcePolling = false,
|
2022-08-22 19:44:40 +00:00
|
|
|
rng[].loadJwtSecret(config, allowCreate = false),
|
|
|
|
true)
|
2022-07-29 08:45:39 +00:00
|
|
|
waitFor res.ensureDataProvider()
|
|
|
|
res
|
2022-07-14 04:07:40 +00:00
|
|
|
else:
|
|
|
|
nil
|
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
optimisticHandler = proc(signedBlock: ForkedMsgTrustedSignedBeaconBlock):
|
2022-07-14 04:07:40 +00:00
|
|
|
Future[void] {.async.} =
|
2022-07-20 18:17:21 +00:00
|
|
|
notice "New LC optimistic block",
|
2022-07-14 04:07:40 +00:00
|
|
|
opt = signedBlock.toBlockId(),
|
|
|
|
wallSlot = getBeaconTime().slotOrZero
|
|
|
|
withBlck(signedBlock):
|
|
|
|
when stateFork >= BeaconStateFork.Bellatrix:
|
|
|
|
if blck.message.is_execution_block:
|
|
|
|
template payload(): auto = blck.message.body.execution_payload
|
|
|
|
|
2022-08-29 12:16:35 +00:00
|
|
|
if eth1Monitor != nil and not payload.block_hash.isZero:
|
2022-07-20 18:17:21 +00:00
|
|
|
await eth1Monitor.ensureDataProvider()
|
|
|
|
|
|
|
|
# engine_newPayloadV1
|
|
|
|
discard await eth1Monitor.newExecutionPayload(payload)
|
|
|
|
|
|
|
|
# engine_forkchoiceUpdatedV1
|
|
|
|
discard await eth1Monitor.runForkchoiceUpdated(
|
|
|
|
headBlockRoot = payload.block_hash,
|
2022-08-25 23:34:02 +00:00
|
|
|
safeBlockRoot = payload.block_hash, # stub value
|
2022-07-20 18:17:21 +00:00
|
|
|
finalizedBlockRoot = ZERO_HASH)
|
2022-07-14 04:07:40 +00:00
|
|
|
else: discard
|
2022-08-25 03:53:59 +00:00
|
|
|
optimisticProcessor = initOptimisticProcessor(
|
|
|
|
getBeaconTime, optimisticHandler)
|
2022-07-14 04:07:40 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient = createLightClient(
|
2022-07-21 09:16:10 +00:00
|
|
|
network, rng, config, cfg, forkDigests, getBeaconTime,
|
|
|
|
genesis_validators_root, LightClientFinalizationMode.Optimistic)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
info "Listening to incoming network requests"
|
|
|
|
network.initBeaconSync(cfg, forkDigests, genesisBlockRoot, getBeaconTime)
|
2022-08-25 03:53:59 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.phase0),
|
|
|
|
proc (signedBlock: phase0.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(
|
|
|
|
optimisticProcessor.processSignedBeaconBlock(signedBlock)))
|
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.altair),
|
|
|
|
proc (signedBlock: altair.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(
|
|
|
|
optimisticProcessor.processSignedBeaconBlock(signedBlock)))
|
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.bellatrix),
|
|
|
|
proc (signedBlock: bellatrix.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(
|
|
|
|
optimisticProcessor.processSignedBeaconBlock(signedBlock)))
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.installMessageValidators()
|
|
|
|
waitFor network.startListening()
|
|
|
|
waitFor network.start()
|
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
proc onFinalizedHeader(
|
|
|
|
lightClient: LightClient, finalizedHeader: BeaconBlockHeader) =
|
2022-07-20 18:17:21 +00:00
|
|
|
info "New LC finalized header",
|
2022-08-25 03:53:59 +00:00
|
|
|
finalized_header = shortLog(finalizedHeader)
|
2022-07-14 04:07:40 +00:00
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
proc onOptimisticHeader(
|
|
|
|
lightClient: LightClient, optimisticHeader: BeaconBlockHeader) =
|
2022-07-20 18:17:21 +00:00
|
|
|
info "New LC optimistic header",
|
2022-08-25 03:53:59 +00:00
|
|
|
optimistic_header = shortLog(optimisticHeader)
|
|
|
|
optimisticProcessor.setOptimisticHeader(optimisticHeader)
|
2022-07-14 04:07:40 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.onFinalizedHeader = onFinalizedHeader
|
|
|
|
lightClient.onOptimisticHeader = onOptimisticHeader
|
|
|
|
lightClient.trustedBlockRoot = some config.trustedBlockRoot
|
2022-07-14 04:07:40 +00:00
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
# Full blocks gossip is required to portably drive an EL client:
|
2022-08-29 12:16:35 +00:00
|
|
|
# - EL clients may not sync when only driven with `forkChoiceUpdated`,
|
|
|
|
# e.g., Geth: "Forkchoice requested unknown head"
|
2022-08-25 03:53:59 +00:00
|
|
|
# - `newPayload` requires the full `ExecutionPayload` (most of block content)
|
|
|
|
# - `ExecutionPayload` block root is not available in `BeaconBlockHeader`,
|
|
|
|
# so won't be exchanged via light client gossip
|
|
|
|
#
|
|
|
|
# Future `ethereum/consensus-specs` versions may remove need for full blocks.
|
|
|
|
# Therefore, this current mechanism is to be seen as temporary; it is not
|
|
|
|
# optimized for reducing code duplication, e.g., with `nimbus_beacon_node`.
|
|
|
|
|
|
|
|
func shouldSyncOptimistically(wallSlot: Slot): bool =
|
|
|
|
# Check whether an EL is connected
|
|
|
|
if eth1Monitor == nil:
|
|
|
|
return false
|
|
|
|
|
|
|
|
# Check whether light client is used
|
|
|
|
let optimisticHeader = lightClient.optimisticHeader.valueOr:
|
|
|
|
return false
|
|
|
|
|
|
|
|
# Check whether light client has synced sufficiently close to wall slot
|
|
|
|
const maxAge = 2 * SLOTS_PER_EPOCH
|
|
|
|
if optimisticHeader.slot < max(wallSlot, maxAge.Slot) - maxAge:
|
|
|
|
return false
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
var blocksGossipState: GossipState = {}
|
|
|
|
proc updateBlocksGossipStatus(slot: Slot) =
|
|
|
|
let
|
|
|
|
isBehind = not shouldSyncOptimistically(slot)
|
|
|
|
|
|
|
|
targetGossipState = getTargetGossipState(
|
|
|
|
slot.epoch, cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH, isBehind)
|
|
|
|
|
|
|
|
template currentGossipState(): auto = blocksGossipState
|
|
|
|
if currentGossipState == targetGossipState:
|
|
|
|
return
|
2022-07-14 04:07:40 +00:00
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
if currentGossipState.card == 0 and targetGossipState.card > 0:
|
|
|
|
debug "Enabling blocks topic subscriptions",
|
|
|
|
wallSlot = slot, targetGossipState
|
|
|
|
elif currentGossipState.card > 0 and targetGossipState.card == 0:
|
|
|
|
debug "Disabling blocks topic subscriptions",
|
|
|
|
wallSlot = slot
|
|
|
|
else:
|
|
|
|
# Individual forks added / removed
|
|
|
|
discard
|
|
|
|
|
|
|
|
let
|
|
|
|
newGossipForks = targetGossipState - currentGossipState
|
|
|
|
oldGossipForks = currentGossipState - targetGossipState
|
|
|
|
|
|
|
|
for gossipFork in oldGossipForks:
|
|
|
|
let forkDigest = forkDigests[].atStateFork(gossipFork)
|
|
|
|
network.unsubscribe(getBeaconBlocksTopic(forkDigest))
|
|
|
|
|
|
|
|
for gossipFork in newGossipForks:
|
|
|
|
let forkDigest = forkDigests[].atStateFork(gossipFork)
|
|
|
|
network.subscribe(
|
|
|
|
getBeaconBlocksTopic(forkDigest), blocksTopicParams,
|
|
|
|
enableTopicMetrics = true)
|
|
|
|
|
|
|
|
blocksGossipState = targetGossipState
|
|
|
|
|
2022-09-16 22:48:53 +00:00
|
|
|
var nextExchangeTransitionConfTime = Moment.now + chronos.seconds(60)
|
2022-07-14 04:07:40 +00:00
|
|
|
proc onSecond(time: Moment) =
|
2022-09-16 22:48:53 +00:00
|
|
|
let wallSlot = getBeaconTime().slotOrZero()
|
|
|
|
|
2022-07-14 04:07:40 +00:00
|
|
|
# engine_exchangeTransitionConfigurationV1
|
|
|
|
if time > nextExchangeTransitionConfTime and eth1Monitor != nil:
|
2022-09-16 22:48:53 +00:00
|
|
|
nextExchangeTransitionConfTime = time + chronos.seconds(45)
|
|
|
|
if wallSlot.epoch >= cfg.BELLATRIX_FORK_EPOCH:
|
|
|
|
traceAsyncErrors eth1Monitor.exchangeTransitionConfiguration()
|
2022-07-14 04:07:40 +00:00
|
|
|
|
|
|
|
if checkIfShouldStopAtEpoch(wallSlot, config.stopAtEpoch):
|
|
|
|
quit(0)
|
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
updateBlocksGossipStatus(wallSlot + 1)
|
2022-07-14 04:07:40 +00:00
|
|
|
lightClient.updateGossipStatus(wallSlot + 1)
|
|
|
|
|
|
|
|
proc runOnSecondLoop() {.async.} =
|
|
|
|
let sleepTime = chronos.seconds(1)
|
|
|
|
while true:
|
|
|
|
let start = chronos.now(chronos.Moment)
|
|
|
|
await chronos.sleepAsync(sleepTime)
|
|
|
|
let afterSleep = chronos.now(chronos.Moment)
|
|
|
|
let sleepTime = afterSleep - start
|
|
|
|
onSecond(start)
|
|
|
|
let finished = chronos.now(chronos.Moment)
|
|
|
|
let processingTime = finished - afterSleep
|
|
|
|
trace "onSecond task completed", sleepTime, processingTime
|
|
|
|
|
|
|
|
onSecond(Moment.now())
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.start()
|
|
|
|
|
2022-07-14 04:07:40 +00:00
|
|
|
asyncSpawn runOnSecondLoop()
|
2022-05-31 10:45:37 +00:00
|
|
|
while true:
|
|
|
|
poll()
|