2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2024-01-12 21:06:19 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-08-26 11:54:10 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
import
|
2024-01-26 22:04:08 +00:00
|
|
|
std/[atomics, json, os, strutils],
|
2024-02-15 13:50:25 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
confutils,
|
2022-08-26 11:54:10 +00:00
|
|
|
eth/keys,
|
2022-09-24 11:57:27 +00:00
|
|
|
json_rpc/rpcproxy,
|
2023-06-04 06:00:50 +00:00
|
|
|
beacon_chain/el/el_manager,
|
2022-08-26 11:54:10 +00:00
|
|
|
beacon_chain/gossip_processing/optimistic_processor,
|
|
|
|
beacon_chain/networking/topic_params,
|
|
|
|
beacon_chain/spec/beaconstate,
|
|
|
|
beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
2024-02-15 13:50:25 +00:00
|
|
|
beacon_chain/[light_client, nimbus_binary_common, version],
|
2022-09-24 11:57:27 +00:00
|
|
|
../nimbus/rpc/cors,
|
2022-12-02 15:09:31 +00:00
|
|
|
"."/rpc/[rpc_eth_api, rpc_utils],
|
2022-11-23 16:11:38 +00:00
|
|
|
./nimbus_verified_proxy_conf,
|
2022-09-28 08:01:32 +00:00
|
|
|
./block_cache
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
from beacon_chain/gossip_processing/block_processor import newExecutionPayload
|
|
|
|
from beacon_chain/gossip_processing/eth2_processor import toValidationResult
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
type OnHeaderCallback* = proc(s: cstring, t: int) {.cdecl, raises: [], gcsafe.}
|
2024-01-26 22:04:08 +00:00
|
|
|
type Context* = object
|
|
|
|
thread*: Thread[ptr Context]
|
|
|
|
configJson*: cstring
|
|
|
|
stop*: bool
|
|
|
|
onHeader*: OnHeaderCallback
|
|
|
|
|
|
|
|
proc cleanup*(ctx: ptr Context) =
|
|
|
|
dealloc(ctx.configJson)
|
|
|
|
freeShared(ctx)
|
|
|
|
|
2022-09-21 16:44:46 +00:00
|
|
|
func getConfiguredChainId(networkMetadata: Eth2NetworkMetadata): Quantity =
|
|
|
|
if networkMetadata.eth1Network.isSome():
|
|
|
|
let
|
|
|
|
net = networkMetadata.eth1Network.get()
|
2024-02-15 13:50:25 +00:00
|
|
|
chainId =
|
|
|
|
case net
|
2022-09-21 16:44:46 +00:00
|
|
|
of mainnet: 1.Quantity
|
2024-02-15 13:50:25 +00:00
|
|
|
of goerli: 5.Quantity
|
2022-09-21 16:44:46 +00:00
|
|
|
of sepolia: 11155111.Quantity
|
2023-09-13 02:32:38 +00:00
|
|
|
of holesky: 17000.Quantity
|
2022-09-21 16:44:46 +00:00
|
|
|
return chainId
|
|
|
|
else:
|
|
|
|
return networkMetadata.cfg.DEPOSIT_CHAIN_ID.Quantity
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
proc run*(
|
|
|
|
config: VerifiedProxyConf, ctx: ptr Context
|
|
|
|
) {.raises: [CatchableError], gcsafe.} =
|
2024-01-26 22:04:08 +00:00
|
|
|
var headerCallback: OnHeaderCallback
|
|
|
|
if ctx != nil:
|
|
|
|
headerCallback = ctx.onHeader
|
|
|
|
|
2022-08-30 05:40:03 +00:00
|
|
|
# Required as both Eth2Node and LightClient requires correct config type
|
|
|
|
var lcConfig = config.asLightClientConf()
|
|
|
|
|
2024-01-26 22:04:08 +00:00
|
|
|
{.gcsafe.}:
|
|
|
|
setupLogging(config.logLevel, config.logStdout, none(OutFile))
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2024-01-26 22:04:08 +00:00
|
|
|
try:
|
|
|
|
notice "Launching Nimbus verified proxy",
|
|
|
|
version = fullVersionStr, cmdParams = commandLineParams(), config
|
|
|
|
except Exception:
|
|
|
|
notice "commandLineParams() exception"
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-09-21 16:44:46 +00:00
|
|
|
let
|
|
|
|
metadata = loadEth2Network(config.eth2Network)
|
|
|
|
chainId = getConfiguredChainId(metadata)
|
2022-08-30 05:40:03 +00:00
|
|
|
|
2022-08-26 11:54:10 +00:00
|
|
|
for node in metadata.bootstrapNodes:
|
2022-08-30 05:40:03 +00:00
|
|
|
lcConfig.bootstrapNodes.add node
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
template cfg(): auto =
|
|
|
|
metadata.cfg
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
genesisState =
|
|
|
|
try:
|
2024-02-15 13:50:25 +00:00
|
|
|
template genesisData(): auto =
|
|
|
|
metadata.genesis.bakedBytes
|
|
|
|
|
|
|
|
newClone(
|
|
|
|
readSszForkedHashedBeaconState(
|
|
|
|
cfg, genesisData.toOpenArray(genesisData.low, genesisData.high)
|
|
|
|
)
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
except CatchableError as err:
|
|
|
|
raiseAssert "Invalid baked-in state: " & err.msg
|
|
|
|
|
2024-01-25 10:04:09 +00:00
|
|
|
genesisTime = getStateField(genesisState[], genesis_time)
|
2024-02-29 17:56:59 +00:00
|
|
|
beaconClock = BeaconClock.init(genesisTime).valueOr:
|
|
|
|
error "Invalid genesis time in state", genesisTime
|
|
|
|
quit QuitFailure
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
getBeaconTime = beaconClock.getBeaconTimeFn()
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
genesis_validators_root = getStateField(genesisState[], genesis_validators_root)
|
2022-08-26 11:54:10 +00:00
|
|
|
forkDigests = newClone ForkDigests.init(cfg, genesis_validators_root)
|
|
|
|
|
|
|
|
genesisBlockRoot = get_initial_beacon_block(genesisState[]).root
|
|
|
|
|
|
|
|
rng = keys.newRng()
|
|
|
|
|
|
|
|
netKeys = getRandomNetKeys(rng[])
|
|
|
|
|
|
|
|
network = createEth2Node(
|
2024-02-15 13:50:25 +00:00
|
|
|
rng, lcConfig, netKeys, cfg, forkDigests, getBeaconTime, genesis_validators_root
|
2022-08-30 05:40:03 +00:00
|
|
|
)
|
|
|
|
|
2022-09-28 08:01:32 +00:00
|
|
|
blockCache = BlockCache.new(uint32(64))
|
|
|
|
|
2022-09-24 11:57:27 +00:00
|
|
|
# TODO: for now we serve all cross origin requests
|
|
|
|
authHooks = @[httpCors(@[])]
|
|
|
|
|
|
|
|
clientConfig = config.web3url.asClientConfig()
|
2022-08-31 05:48:48 +00:00
|
|
|
|
2022-09-24 11:57:27 +00:00
|
|
|
rpcProxy = RpcProxy.new(
|
2024-02-15 13:50:25 +00:00
|
|
|
[initTAddress(config.rpcAddress, config.rpcPort)], clientConfig, authHooks
|
2022-08-31 05:48:48 +00:00
|
|
|
)
|
2022-08-30 05:40:03 +00:00
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
verifiedProxy = VerifiedRpcProxy.new(rpcProxy, blockCache, chainId)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
optimisticHandler = proc(
|
|
|
|
signedBlock: ForkedMsgTrustedSignedBeaconBlock
|
|
|
|
): Future[void] {.async: (raises: [CancelledError]).} =
|
2022-08-26 11:54:10 +00:00
|
|
|
notice "New LC optimistic block",
|
2024-02-15 13:50:25 +00:00
|
|
|
opt = signedBlock.toBlockId(), wallSlot = getBeaconTime().slotOrZero
|
2022-08-26 11:54:10 +00:00
|
|
|
withBlck(signedBlock):
|
2023-04-13 16:40:01 +00:00
|
|
|
when consensusFork >= ConsensusFork.Bellatrix:
|
2023-10-17 12:19:50 +00:00
|
|
|
if forkyBlck.message.is_execution_block:
|
2024-02-15 13:50:25 +00:00
|
|
|
template payload(): auto =
|
|
|
|
forkyBlck.message.body.execution_payload
|
|
|
|
|
2022-12-02 15:09:31 +00:00
|
|
|
blockCache.add(asExecutionData(payload.asEngineExecutionPayload()))
|
2024-02-15 13:50:25 +00:00
|
|
|
else:
|
|
|
|
discard
|
2022-08-26 11:54:10 +00:00
|
|
|
return
|
|
|
|
|
2024-02-15 13:50:25 +00:00
|
|
|
optimisticProcessor = initOptimisticProcessor(getBeaconTime, optimisticHandler)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
lightClient = createLightClient(
|
2024-02-15 13:50:25 +00:00
|
|
|
network, rng, lcConfig, cfg, forkDigests, getBeaconTime, genesis_validators_root,
|
2024-02-29 17:56:59 +00:00
|
|
|
LightClientFinalizationMode.Optimistic,
|
2024-02-15 13:50:25 +00:00
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
verifiedProxy.installEthApiHandlers()
|
2022-08-30 05:40:03 +00:00
|
|
|
|
2022-08-26 11:54:10 +00:00
|
|
|
info "Listening to incoming network requests"
|
2024-01-13 01:41:57 +00:00
|
|
|
network.registerProtocol(
|
2024-02-15 13:50:25 +00:00
|
|
|
PeerSync,
|
|
|
|
PeerSync.NetworkState.init(cfg, forkDigests, genesisBlockRoot, getBeaconTime),
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.phase0),
|
2024-02-15 13:50:25 +00:00
|
|
|
proc(signedBlock: phase0.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(optimisticProcessor.processSignedBeaconBlock(signedBlock))
|
|
|
|
,
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.altair),
|
2024-02-15 13:50:25 +00:00
|
|
|
proc(signedBlock: altair.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(optimisticProcessor.processSignedBeaconBlock(signedBlock))
|
|
|
|
,
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.bellatrix),
|
2024-02-15 13:50:25 +00:00
|
|
|
proc(signedBlock: bellatrix.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(optimisticProcessor.processSignedBeaconBlock(signedBlock))
|
|
|
|
,
|
|
|
|
)
|
2023-04-13 16:40:01 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.capella),
|
2024-02-15 13:50:25 +00:00
|
|
|
proc(signedBlock: capella.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(optimisticProcessor.processSignedBeaconBlock(signedBlock))
|
|
|
|
,
|
|
|
|
)
|
2023-04-13 16:40:01 +00:00
|
|
|
network.addValidator(
|
|
|
|
getBeaconBlocksTopic(forkDigests.deneb),
|
2024-02-15 13:50:25 +00:00
|
|
|
proc(signedBlock: deneb.SignedBeaconBlock): ValidationResult =
|
|
|
|
toValidationResult(optimisticProcessor.processSignedBeaconBlock(signedBlock))
|
|
|
|
,
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
lightClient.installMessageValidators()
|
2022-08-30 05:40:03 +00:00
|
|
|
|
2022-08-26 11:54:10 +00:00
|
|
|
waitFor network.startListening()
|
|
|
|
waitFor network.start()
|
2022-09-24 11:57:27 +00:00
|
|
|
waitFor rpcProxy.start()
|
2022-11-23 16:11:38 +00:00
|
|
|
waitFor verifiedProxy.verifyChaindId()
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
proc onFinalizedHeader(
|
2024-02-15 13:50:25 +00:00
|
|
|
lightClient: LightClient, finalizedHeader: ForkedLightClientHeader
|
|
|
|
) =
|
2023-01-27 14:57:48 +00:00
|
|
|
withForkyHeader(finalizedHeader):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2024-02-15 13:50:25 +00:00
|
|
|
info "New LC finalized header", finalized_header = shortLog(forkyHeader)
|
2024-01-26 22:04:08 +00:00
|
|
|
if headerCallback != nil:
|
|
|
|
try:
|
|
|
|
headerCallback(Json.encode(forkyHeader), 0)
|
|
|
|
except SerializationError as e:
|
|
|
|
notice "finalizedHeaderCallback exception"
|
|
|
|
|
2022-08-26 11:54:10 +00:00
|
|
|
proc onOptimisticHeader(
|
2024-02-15 13:50:25 +00:00
|
|
|
lightClient: LightClient, optimisticHeader: ForkedLightClientHeader
|
|
|
|
) =
|
2023-01-27 14:57:48 +00:00
|
|
|
withForkyHeader(optimisticHeader):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2024-02-15 13:50:25 +00:00
|
|
|
info "New LC optimistic header", optimistic_header = shortLog(forkyHeader)
|
2023-01-27 14:57:48 +00:00
|
|
|
optimisticProcessor.setOptimisticHeader(forkyHeader.beacon)
|
2024-01-26 22:04:08 +00:00
|
|
|
if headerCallback != nil:
|
|
|
|
try:
|
|
|
|
headerCallback(Json.encode(forkyHeader), 1)
|
|
|
|
except SerializationError as e:
|
2024-02-15 13:50:25 +00:00
|
|
|
notice "optimisticHeaderCallback exception"
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
lightClient.onFinalizedHeader = onFinalizedHeader
|
|
|
|
lightClient.onOptimisticHeader = onOptimisticHeader
|
|
|
|
lightClient.trustedBlockRoot = some config.trustedBlockRoot
|
|
|
|
|
|
|
|
func shouldSyncOptimistically(wallSlot: Slot): bool =
|
2023-01-27 14:57:48 +00:00
|
|
|
let optimisticHeader = lightClient.optimisticHeader
|
|
|
|
withForkyHeader(optimisticHeader):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
# Check whether light client has synced sufficiently close to wall slot
|
|
|
|
const maxAge = 2 * SLOTS_PER_EPOCH
|
|
|
|
forkyHeader.beacon.slot >= max(wallSlot, maxAge.Slot) - maxAge
|
|
|
|
else:
|
|
|
|
false
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
var blocksGossipState: GossipState = {}
|
|
|
|
proc updateBlocksGossipStatus(slot: Slot) =
|
|
|
|
let
|
|
|
|
isBehind = not shouldSyncOptimistically(slot)
|
|
|
|
|
|
|
|
targetGossipState = getTargetGossipState(
|
2022-12-09 17:00:45 +00:00
|
|
|
slot.epoch, cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH,
|
2024-05-26 07:13:59 +00:00
|
|
|
cfg.CAPELLA_FORK_EPOCH, cfg.DENEB_FORK_EPOCH, cfg.ELECTRA_FORK_EPOCH, isBehind,
|
2024-02-15 13:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
template currentGossipState(): auto =
|
|
|
|
blocksGossipState
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
if currentGossipState == targetGossipState:
|
|
|
|
return
|
|
|
|
|
|
|
|
if currentGossipState.card == 0 and targetGossipState.card > 0:
|
2024-02-15 13:50:25 +00:00
|
|
|
debug "Enabling blocks topic subscriptions", wallSlot = slot, targetGossipState
|
2022-08-26 11:54:10 +00:00
|
|
|
elif currentGossipState.card > 0 and targetGossipState.card == 0:
|
2024-02-15 13:50:25 +00:00
|
|
|
debug "Disabling blocks topic subscriptions", wallSlot = slot
|
2022-08-26 11:54:10 +00:00
|
|
|
else:
|
|
|
|
# Individual forks added / removed
|
|
|
|
discard
|
|
|
|
|
|
|
|
let
|
|
|
|
newGossipForks = targetGossipState - currentGossipState
|
|
|
|
oldGossipForks = currentGossipState - targetGossipState
|
|
|
|
|
|
|
|
for gossipFork in oldGossipForks:
|
2023-04-13 16:40:01 +00:00
|
|
|
let forkDigest = forkDigests[].atConsensusFork(gossipFork)
|
2022-08-26 11:54:10 +00:00
|
|
|
network.unsubscribe(getBeaconBlocksTopic(forkDigest))
|
|
|
|
|
|
|
|
for gossipFork in newGossipForks:
|
2023-04-13 16:40:01 +00:00
|
|
|
let forkDigest = forkDigests[].atConsensusFork(gossipFork)
|
2022-08-26 11:54:10 +00:00
|
|
|
network.subscribe(
|
2024-02-15 13:50:25 +00:00
|
|
|
getBeaconBlocksTopic(forkDigest), blocksTopicParams, enableTopicMetrics = true
|
|
|
|
)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
blocksGossipState = targetGossipState
|
|
|
|
|
|
|
|
proc onSecond(time: Moment) =
|
|
|
|
let wallSlot = getBeaconTime().slotOrZero()
|
2022-08-30 05:40:03 +00:00
|
|
|
updateBlocksGossipStatus(wallSlot + 1)
|
2022-08-26 11:54:10 +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())
|
|
|
|
lightClient.start()
|
|
|
|
|
|
|
|
asyncSpawn runOnSecondLoop()
|
|
|
|
while true:
|
|
|
|
poll()
|
2024-01-26 22:04:08 +00:00
|
|
|
if ctx != nil and ctx.stop:
|
|
|
|
# Cleanup
|
|
|
|
waitFor network.stop()
|
|
|
|
waitFor rpcProxy.stop()
|
|
|
|
ctx.cleanup()
|
|
|
|
# Notify client that cleanup is finished
|
|
|
|
headerCallback(nil, 2)
|
|
|
|
break
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2023-02-07 16:21:12 +00:00
|
|
|
{.pop.}
|
2024-02-15 13:50:25 +00:00
|
|
|
var config =
|
|
|
|
makeBannerAndConfig("Nimbus verified proxy " & fullVersionStr, VerifiedProxyConf)
|
2023-02-07 16:21:12 +00:00
|
|
|
{.push raises: [].}
|
2024-01-26 22:04:08 +00:00
|
|
|
run(config, nil)
|