Skip to content

Commit 2fcf8fb

Browse files
committed
create validation race
1 parent 792690f commit 2fcf8fb

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

beacon_chain/gossip_processing/eth2_processor.nim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,41 @@ type
157157

158158
ValidationRes* = Result[void, ValidationError]
159159

160+
160161
func toValidationResult*(res: ValidationRes): ValidationResult =
161162
if res.isOk(): ValidationResult.Accept else: res.error()[0]
162163

164+
proc toValidationRace*(
165+
f1, f2: Future[ValidationRes]
166+
): Future[ValidationResult] {.async: (raises: [CancelledError]).} =
167+
let
168+
f1b = noCancel(f1)
169+
f2b = noCancel(f2)
170+
171+
var raced: FutureBase
172+
try:
173+
raced = await race([f1b, f2b])
174+
except ValueError:
175+
# This shouldn't normally happen with 2 futures
176+
return ValidationResult.Ignore
177+
178+
let
179+
first = if raced == f1b: f1 else: f2
180+
second = if raced == f1b: f2 else: f1
181+
182+
try:
183+
let res1 = await first
184+
if res1.isOk():
185+
return ValidationResult.Accept
186+
except CatchableError:
187+
discard
188+
189+
try:
190+
let res2 = await second
191+
return toValidationResult(res2)
192+
except CatchableError:
193+
return ValidationResult.Ignore
194+
163195
# Initialization
164196
# ------------------------------------------------------------------------------
165197

beacon_chain/nimbus_beacon_node.nim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,8 +2115,15 @@ proc installMessageValidators(node: BeaconNode) =
21152115
getBlobSidecarTopic(digest, subnet_id), proc (
21162116
blobSidecar: deneb.BlobSidecar
21172117
): Future[ValidationResult] {.async: (raises: [CancelledError]).} =
2118-
return toValidationResult(
2119-
await node.processor.processBlobSidecarFromEL(blobSidecar)))
2118+
let
2119+
fut1 =
2120+
(node.processor.processBlobSidecarFromEL(blobSidecar))
2121+
fut2 =
2122+
(node.processor.processBlobSidecar(MsgSource.gossip,
2123+
blobSidecar,
2124+
subnet_id))
2125+
2126+
return waitFor toValidationRace(fut1, fut2))
21202127

21212128
when consensusFork >= ConsensusFork.Deneb:
21222129
# blob_sidecar_{subnet_id}

0 commit comments

Comments
 (0)