Skip to content

Commit

Permalink
Use new historical_summaries beacon API endpoint in portal_bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme committed Oct 24, 2024
1 parent 0d4de33 commit 33e64b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
72 changes: 34 additions & 38 deletions fluffy/tools/portal_bridge/portal_bridge_beacon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import
../eth_data_exporter/cl_data_exporter,
./[portal_bridge_conf, portal_bridge_common]

const
largeRequestsTimeout = 120.seconds # For downloading large items such as states.
restRequestsTimeout = 30.seconds
const restRequestsTimeout = 30.seconds

# TODO: From nimbus_binary_common, but we don't want to import that.
proc sleepAsync(t: TimeDiff): Future[void] =
Expand Down Expand Up @@ -236,46 +234,44 @@ proc gossipHistoricalSummaries(
portalRpcClient: RpcClient,
cfg: RuntimeConfig,
forkDigests: ref ForkDigests,
): Future[Result[void, string]] {.async.} =
let state =
): Future[Result[void, string]] {.async: (raises: [CancelledError]).} =
let summariesOpt =
try:
notice "Downloading beacon state"
notice "Downloading beacon historical_summaries"
awaitWithTimeout(
restClient.getStateV2(StateIdent.init(StateIdentType.Finalized), cfg),
largeRequestsTimeout,
restClient.getHistoricalSummariesV1(
StateIdent.init(StateIdentType.Finalized), cfg
),
restRequestsTimeout,
):
return err("Attempt to download beacon state timed out")
except CatchableError as exc:
return err("Unable to download beacon state: " & exc.msg)
return err("Attempt to download historical_summaries timed out")
except RestError as exc:
return err("Unable to download historical_summaries: " & exc.msg)

if state == nil:
return err("No beacon state found")

withState(state[]):
when consensusFork >= ConsensusFork.Capella:
let
historical_summaries = forkyState.data.historical_summaries
proof = ?buildProof(state[])
epoch = forkyState.data.slot.epoch()
forkDigest = forkDigestAtEpoch(forkDigests[], epoch, cfg)
summariesWithProof = HistoricalSummariesWithProof(
epoch: epoch, historical_summaries: historical_summaries, proof: proof
)
if summariesOpt.isNone():
return err("No historical_summaries found")

contentKey = encode(historicalSummariesContentKey(epoch.uint64))
content = encodeSsz(summariesWithProof, forkDigest)

try:
let peers = await portalRpcClient.portal_beaconRandomGossip(
contentKey.asSeq().toHex(), content.toHex()
)
info "Beacon historical_summaries gossiped", peers, epoch

return ok()
except CatchableError as e:
return err("JSON-RPC error: " & $e.msg)
else:
return err("No historical_summaries pre Capella")
let
summaries = summariesOpt.get()
epoch = summaries.slot.epoch()
forkDigest = forkDigestAtEpoch(forkDigests[], epoch, cfg)
summariesWithProof = HistoricalSummariesWithProof(
epoch: epoch,
historical_summaries: summaries.historical_summaries,
proof: summaries.proof,
)
contentKey = encode(historicalSummariesContentKey(epoch.uint64))
content = encodeSsz(summariesWithProof, forkDigest)

try:
let peers = await portalRpcClient.portal_beaconRandomGossip(
contentKey.asSeq().toHex(), content.toHex()
)
info "Beacon historical_summaries gossiped", peers, epoch

ok()
except CatchableError as e:
err("JSON-RPC error: " & $e.msg)

proc runBeacon*(config: PortalBridgeConf) {.raises: [CatchableError].} =
notice "Launching Fluffy beacon chain bridge", cmdParams = commandLineParams()
Expand Down

0 comments on commit 33e64b4

Please sign in to comment.