Skip to content

Commit

Permalink
fix(diri): ensure events are handled sorted by block number (#425)
Browse files Browse the repository at this point in the history
## Description

Add block number sorting to ensure that events handling respect temporal
order.

<!--
Please do not leave this blank.
Describe the changes in this PR. What does it [add/remove/fix/replace]?

For crafting a good description, consider using ChatGPT to help
articulate your changes.
-->

## 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

<!--
Please use this format to link related issues: Fixes #<issue_number>
More info:
https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## 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?

<!-- Describe any additional tasks, if any, and provide steps. -->

## [optional] What gif best describes this PR or how it makes you feel?

<!-- Share a fun gif related to your PR! -->

### 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

<!--
Use keywords to close related issues. This ensures that the associated
issues will automatically close when the PR is merged.

- `Fixes #123` will close issue 123 when the PR is merged.
- `Closes #123` will also close issue 123 when the PR is merged.
- `Resolves #123` will also close issue 123 when the PR is merged.

You can also use multiple keywords in one comment:
- `Fixes #123, Resolves #456`

More info:
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->
  • Loading branch information
ptisserand authored Dec 6, 2024
1 parent 029b666 commit 1d5c4ea
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions crates/diri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ impl<S: Storage, E: EventHandler> Diri<S, E> {
)
.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}");
Expand All @@ -92,38 +96,38 @@ impl<S: Storage, E: EventHandler> Diri<S, E> {
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(())
Expand Down

0 comments on commit 1d5c4ea

Please sign in to comment.