Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Events Orderbook] Implement querying the orderbook at the latest block #735

Merged
merged 5 commits into from
May 6, 2020

Conversation

nlordell
Copy link
Contributor

@nlordell nlordell commented May 6, 2020

This PR implements querying the latest open orderbook.

This is implemented by keeping a separate "confirmed" auction state, built from events that are part of confirmed blocks, and not subject to reorgs, and a "latest" auction state that is built by cloning the "confirmed" state and applying events in between the confirmed block and the latest block.

The "latest" state is implemented to be discardable and rebuilt on every update so that it is not prone to errors stemming from reorgs, nor does complex logic for rolling back events need to be implemented. Unfortunately, the current implementation is slightly inefficient. I created #734 as an issue to capture a potential solution to the problem.

This closes #719

Test Plan

Run the e2e test to ensure the event based orderbook on the latest block matches the onchain queried orderbook:

$ yarn test-streamed-orderbook

  Streamed Orderbook
    init
==> building streamed orderbook...
==> querying onchain orderbook...
==> comparing orderbooks...
      ✓ should successfully apply all events and match on-chain orderbook (287940ms)


  1 passing (5m)

@nlordell nlordell requested a review from a team May 6, 2020 05:03
Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Would be nice to unit test the clone behavior.

@@ -132,7 +133,10 @@ export class StreamedOrderbook {
* Retrieves the current open orders in the orderbook.
*/
public getOpenOrders(): IndexedOrder<bigint>[] {
return this.state.getOrders(this.batch)
this.throwOnInvalidState()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this helper a lot!

* Creates a copy of the auction state that can apply events independently
* without modifying the original state.
*/
public copy(): AuctionState {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Can we add a unit test that this actually creates a deep copy? It seems to work because bigint seems to be an
immutable type (I think with BN we would have to also copy the data of the maps).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you think the test is sufficient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks!

@nlordell nlordell changed the base branch from ev-ob-4.1 to master May 6, 2020 15:43
@nlordell nlordell merged commit 35a2ee2 into master May 6, 2020
@nlordell nlordell deleted the ev-ob-5 branch May 6, 2020 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Events Orderbook] Implement orderbook querying
3 participants