From 1d5c4ead370337984e370a7cd683e2dc2a4ed9dd Mon Sep 17 00:00:00 2001
From: ptisserand
Date: Fri, 6 Dec 2024 16:33:05 +0100
Subject: [PATCH] fix(diri): ensure events are handled sorted by block number
(#425)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
## Description
Add block number sorting to ensure that events handling respect temporal
order.
## What type of PR is this? (check all applicable)
- [ ] 🍕 Feature (`feat:`)
- [x] 🐛 Bug Fix (`fix:`)
- [ ] 📝 Documentation Update (`docs:`)
- [ ] 🎨 Style (`style:`)
- [ ] 🧑💻 Code Refactor (`refactor:`)
- [ ] 🔥 Performance Improvements (`perf:`)
- [ ] ✅ Test (`test:`)
- [ ] 🤖 Build (`build:`)
- [ ] 🔁 CI (`ci:`)
- [ ] 📦 Chore (`chore:`)
- [ ] ⏩ Revert (`revert:`)
- [ ] 🚀 Breaking Changes (`BREAKING CHANGE:`)
## Related Tickets & Documents
## Added tests?
- [ ] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help
## Added to documentation?
- [ ] 📜 README.md
- [ ] 📓 Documentation
- [ ] 🙅 no documentation needed
## [optional] Are there any post-deployment tasks we need to perform?
## [optional] What gif best describes this PR or how it makes you feel?
### PR Title and Description Guidelines:
- Ensure your PR title follows semantic versioning standards. This helps
automate releases and changelogs.
- Use types like `feat:`, `fix:`, `chore:`, `BREAKING CHANGE:` etc. in
your PR title.
- Your PR title will be used as a commit message when merging. Make sure
it adheres to [Conventional Commits
standards](https://www.conventionalcommits.org/).
## Closing Issues
---
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(())