From 96dfe18a3d6ccf4f434dd1807701dabbd4f7313b Mon Sep 17 00:00:00 2001 From: henridf Date: Sat, 21 Jan 2023 07:34:04 +0100 Subject: [PATCH] make message_router.routeSignedBeaconBlock blob-aware (#4537) * make message_router.outeSignedBeaconBlock blob-aware * Update beacon_chain/spec/forks.nim * Update beacon_chain/spec/forks.nim --- beacon_chain/networking/eth2_network.nim | 20 +++++--------------- beacon_chain/spec/forks.nim | 11 ++++++++++- beacon_chain/validators/message_router.nim | 12 ++++++++---- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/beacon_chain/networking/eth2_network.nim b/beacon_chain/networking/eth2_network.nim index 59cb7a723..b44acbb0f 100644 --- a/beacon_chain/networking/eth2_network.nim +++ b/beacon_chain/networking/eth2_network.nim @@ -2653,23 +2653,13 @@ proc broadcastBeaconBlock*( let topic = getBeaconBlocksTopic(node.forkDigests.capella) node.broadcast(topic, blck) +proc broadcastBeaconBlockAndBlobsSidecar*( + node: Eth2Node, blck: eip4844.SignedBeaconBlockAndBlobsSidecar): Future[SendResult] = + let topic = getBeaconBlockAndBlobsSidecarTopic(node.forkDigests.eip4844) + node.broadcast(topic, blck) + from ../spec/datatypes/eip4844 import SignedBeaconBlock -proc broadcastBeaconBlock*( - node: Eth2Node, blck: eip4844.SignedBeaconBlock): Future[SendResult] = - debugRaiseAssert $eip4844ImplementationMissing & ": eth2_network.nim:broadcastBeaconBlock EIP4844 uses different approach (1)" - -proc broadcastBeaconBlock*( - node: Eth2Node, forked: ForkedSignedBeaconBlock): Future[SendResult] = - withBlck(forked): - when stateFork == BeaconStateFork.EIP4844: - debugRaiseAssert $eip4844ImplementationMissing & ": eth2_network.nim:broadcastBeaconBlock EIP4844 uses different approach (2)" - let f = newFuture[SendResult]() - f.fail(new CatchableError) - f - else: - node.broadcastBeaconBlock(blck) - proc broadcastSyncCommitteeMessage*( node: Eth2Node, msg: SyncCommitteeMessage, subcommitteeIdx: SyncSubcommitteeIndex): Future[SendResult] = diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index 336c90438..5d35cd175 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -9,6 +9,7 @@ import stew/assign2, + stew/results, chronicles, ../extras, "."/[ @@ -257,7 +258,7 @@ type # block gossip structures. It is for used only for # gossip-originating blocks, which are eventually separated into the # constituent parts before passing along into core functions. -type ForkySignedBeaconBlockMaybeBlobs* = +type ForkySignedBeaconBlockMaybeBlobs* = phase0.SignedBeaconBlock | altair.SignedBeaconBlock | bellatrix.SignedBeaconBlock | @@ -272,6 +273,14 @@ template toSignedBeaconBlock*(b: ForkySignedBeaconBlockMaybeBlobs): ForkySignedB else: b +func optBlobs*(b: ForkySignedBeaconBlockMaybeBlobs): + Opt[eip4844.BlobsSidecar] = + when b is phase0.SignedBeaconBlock or b is altair.SignedBeaconBlock or + b is bellatrix.SignedBeaconBlock or b is capella.SignedBeaconBlock: + Opt.none(eip4844.BlobsSidecar) + elif b is eip4844.SignedBeaconBlockAndBlobsSidecar: + Opt.some(b.blobs_sidecar) + template toFork*[T: phase0.BeaconState | phase0.HashedBeaconState]( t: type T): BeaconStateFork = BeaconStateFork.Phase0 diff --git a/beacon_chain/validators/message_router.nim b/beacon_chain/validators/message_router.nim index b21983143..87dec6367 100644 --- a/beacon_chain/validators/message_router.nim +++ b/beacon_chain/validators/message_router.nim @@ -107,7 +107,12 @@ proc routeSignedBeaconBlock*( # The block passed basic gossip validation - we can "safely" broadcast it # now. In fact, per the spec, we should broadcast it even if it later fails # to apply to our state. - res = await router[].network.broadcastBeaconBlock(blck) + + let res = + when blckAndBlobs is eip4844.SignedBeaconBlockAndBlobsSidecar: + await router[].network.broadcastBeaconBlockAndBlobsSidecar(blckAndBlobs) + else: + await router[].network.broadcastBeaconBlock(blck) if res.isOk(): beacon_blocks_sent.inc() @@ -121,9 +126,8 @@ proc routeSignedBeaconBlock*( blockRoot = shortLog(blck.root), blck = shortLog(blck.message), signature = shortLog(blck.signature), error = res.error() - let - newBlockRef = await router[].blockProcessor.storeBlock( - MsgSource.api, sendTime, blck, Opt.none(eip4844.BlobsSidecar)) + let newBlockRef = await router[].blockProcessor.storeBlock( + MsgSource.api, sendTime, blck, optBlobs(blckAndBlobs)) # The boolean we return tells the caller whether the block was integrated # into the chain