From 2cf652ae823a9344fbe42f4f1c4e83cc443f1cb7 Mon Sep 17 00:00:00 2001
From: Patrice Tisserand
Date: Fri, 23 Aug 2024 09:40:10 +0200
Subject: [PATCH] fix(diri): ensure events are handled sorted by block number
---
crates/diri/src/lib.rs | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/crates/diri/src/lib.rs b/crates/diri/src/lib.rs
index 661d41420..2f09cb00d 100644
--- a/crates/diri/src/lib.rs
+++ b/crates/diri/src/lib.rs
@@ -76,11 +76,15 @@ impl Diri {
)
.await?;
- for (block_number, events) in blocks_events {
- let block_timestamp = self.block_time(BlockId::Number(block_number)).await?;
+ // Handle events sorted by block number
+ let mut block_numbers: Vec<&u64> = blocks_events.keys().collect();
+ block_numbers.sort();
+ for block_number in block_numbers {
+ let block_timestamp = self.block_time(BlockId::Number(*block_number)).await?;
+ let events = blocks_events.get(block_number).unwrap();
for any_event in events {
- let orderbook_event: Event = match any_event.try_into() {
+ let orderbook_event: Event = match any_event.clone().try_into() {
Ok(ev) => ev,
Err(e) => {
trace!("Event can't be deserialized: {e}");
@@ -92,38 +96,38 @@ impl Diri {
Event::OrderPlaced(ev) => {
trace!("OrderPlaced found: {:?}", ev);
self.storage
- .register_placed(block_number, block_timestamp, &ev.into())
+ .register_placed(*block_number, block_timestamp, &ev.into())
.await?;
}
Event::OrderCancelled(ev) => {
trace!("OrderCancelled found: {:?}", ev);
self.storage
- .register_cancelled(block_number, block_timestamp, &ev.into())
+ .register_cancelled(*block_number, block_timestamp, &ev.into())
.await?;
}
Event::OrderFulfilled(ev) => {
trace!("OrderFulfilled found: {:?}", ev);
self.storage
- .register_fulfilled(block_number, block_timestamp, &ev.into())
+ .register_fulfilled(*block_number, block_timestamp, &ev.into())
.await?;
}
Event::OrderExecuted(ev) => {
trace!("OrderExecuted found: {:?}", ev);
self.storage
- .register_executed(block_number, block_timestamp, &ev.into())
+ .register_executed(*block_number, block_timestamp, &ev.into())
.await?;
}
Event::RollbackStatus(ev) => {
trace!("RollbackStatus found: {:?}", ev);
self.storage
- .status_back_to_open(block_number, block_timestamp, &ev.into())
+ .status_back_to_open(*block_number, block_timestamp, &ev.into())
.await?;
}
_ => warn!("Orderbook event not handled: {:?}", orderbook_event),
};
}
- self.event_handler.on_block_processed(block_number).await;
+ self.event_handler.on_block_processed(*block_number).await;
}
Ok(())