Skip to content

Commit 065d72f

Browse files
arnetheduckzah
authored andcommitted
move head update to storeBlock
when blocks are supplied via rest, this ensures the newly posted head is chosen
1 parent 5e9625c commit 065d72f

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

beacon_chain/gossip_processing/block_processor.nim

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,11 @@ proc dumpBlock*[T](
151151
proc storeBlock*(
152152
self: var BlockProcessor,
153153
signedBlock: ForkySignedBeaconBlock,
154-
wallSlot: Slot): Result[BlockRef, BlockError] =
154+
wallSlot: Slot, queueTick: Moment = Moment.now(),
155+
validationDur = Duration()): Result[BlockRef, BlockError] =
155156
let
156157
attestationPool = self.consensusManager.attestationPool
158+
startTick = Moment.now()
157159

158160
type Trusted = typeof signedBlock.asTrusted()
159161
let blck = self.consensusManager.dag.addRawBlock(
@@ -170,6 +172,25 @@ proc storeBlock*(
170172
# was pruned from the ForkChoice.
171173
if blck.isErr:
172174
return err(blck.error[1])
175+
176+
let storeBlockTick = Moment.now()
177+
178+
# Eagerly update head: the incoming block "should" get selected
179+
self.consensusManager[].updateHead(wallSlot)
180+
181+
let
182+
updateHeadTick = Moment.now()
183+
queueDur = startTick - queueTick
184+
storeBlockDur = storeBlockTick - startTick
185+
updateHeadDur = updateHeadTick - storeBlockTick
186+
187+
beacon_store_block_duration_seconds.observe(storeBlockDur.toFloatSeconds())
188+
189+
debug "Block processed",
190+
localHeadSlot = self.consensusManager.dag.head.slot,
191+
blockSlot = blck.get().slot,
192+
validationDur, queueDur, storeBlockDur, updateHeadDur
193+
173194
ok(blck.get())
174195

175196
# Event Loop
@@ -188,30 +209,10 @@ proc processBlock(self: var BlockProcessor, entry: BlockEntry) =
188209
quit 1
189210

190211
let
191-
startTick = Moment.now()
192-
res = withBlck(entry.blck): self.storeBlock(blck, wallSlot)
193-
storeBlockTick = Moment.now()
194-
195-
if res.isOk():
196-
# Eagerly update head in case the new block gets selected
197-
self.consensusManager[].updateHead(wallSlot)
198-
199-
let
200-
updateHeadTick = Moment.now()
201-
queueDur = startTick - entry.queueTick
202-
storeBlockDur = storeBlockTick - startTick
203-
updateHeadDur = updateHeadTick - storeBlockTick
212+
res = withBlck(entry.blck):
213+
self.storeBlock(blck, wallSlot, entry.queueTick, entry.validationDur)
204214

205-
beacon_store_block_duration_seconds.observe(storeBlockDur.toFloatSeconds())
206-
207-
debug "Block processed",
208-
localHeadSlot = self.consensusManager.dag.head.slot,
209-
blockSlot = entry.blck.slot,
210-
validationDur = entry.validationDur,
211-
queueDur, storeBlockDur, updateHeadDur
212-
213-
entry.done()
214-
elif res.error() in {BlockError.Duplicate, BlockError.Old}:
215+
if res.isOk() or (res.error() in {BlockError.Duplicate, BlockError.Old}):
215216
# Duplicate and old blocks are ok from a sync point of view, so we mark
216217
# them as successful
217218
entry.done()

0 commit comments

Comments
 (0)