From e8d91bc3290dfe39b6c925589d51cc0b7579e5a3 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Krieger Date: Thu, 15 Aug 2024 09:51:27 -0300 Subject: [PATCH] fix(dispatcher): check epoch before finishing This asserts the input epoch correctness before finish_epoch_if_needed and enqueue_input. --- offchain/dispatcher/src/drivers/context.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/offchain/dispatcher/src/drivers/context.rs b/offchain/dispatcher/src/drivers/context.rs index a8063bbcf..04180ea5a 100644 --- a/offchain/dispatcher/src/drivers/context.rs +++ b/offchain/dispatcher/src/drivers/context.rs @@ -65,6 +65,13 @@ impl Context { broker: &impl BrokerSend, ) -> Result<(), BrokerFacadeError> { let input_block_number = input.block_added.number.as_u64(); + let input_epoch = self.calculate_epoch(input_block_number); + self.last_finished_epoch.map(|last_finished_epoch| { + // Asserting that the calculated epoch comes after the last finished epoch. + // (If last_finished_epoch == None then we don't need the assertion.) + assert!(input_epoch > last_finished_epoch) + }); + self.finish_epoch_if_needed(input_block_number, broker) .await?; @@ -76,13 +83,6 @@ impl Context { .inc(); self.inputs_sent += 1; - - let input_epoch = self.calculate_epoch(input_block_number); - self.last_finished_epoch.map(|last_finished_epoch| { - // Asserting that the calculated epoch comes after the last finished epoch. - // (If last_finished_epoch == None then we don't need the assertion.) - assert!(input_epoch > last_finished_epoch) - }); self.last_input_epoch = Some(input_epoch); Ok(())