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

Storing call_data in quotes and order_quotes tables #3124

Merged
merged 83 commits into from
Dec 11, 2024

Conversation

mstrug
Copy link
Contributor

@mstrug mstrug commented Nov 14, 2024

Description

Added two new non-null columns: metadata (json) and verified (boolean) into tables quotes and order_quotes.
Existing rows in database will be updated with {} and false values.
metadata column is dedicated to store any additional data for the quote in json format. Currently it is used to store interactions received for /quote response from the solvers, but it is prepared to store other data in future through versioned QuoteMetadata struct as required.
Database module only checks json validity, upper layers (orderbook, autopilot) uses QuoteMetadata struct for storing specific data in the database.
Metadata can be used for verification/auditing of quotes/orders.
Added database migration scripts (update and revert) and updated database readme file.

How to test

Existing tests, also added dedicated e2e tests and unit tests for this new functionality .

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Given that we now want to store the data in the db the PR also needs a db migration file (database/sql directory) and an update in the db Readme for that.

crates/orderbook/src/database/orders.rs Outdated Show resolved Hide resolved
Copy link

Reminder: Please update the DB Readme.


Caused by:

@mstrug mstrug marked this pull request as ready for review November 15, 2024 22:28
@mstrug mstrug requested a review from a team as a code owner November 15, 2024 22:28
@squadgazzz
Copy link
Contributor

Reference discussion: link

Let's avoid sharing links to private discussions in open-source projects and instead share all the required information in the description. @mstrug, could you update the description explaining the reasoning and other considerations, if any?

@mstrug mstrug marked this pull request as draft November 23, 2024 00:20
@m-lord-renkse
Copy link
Contributor

@mstrug is this ready for review?

@mstrug
Copy link
Contributor Author

mstrug commented Dec 9, 2024

@mstrug is this ready for review?

Yes

crates/database/src/orders.rs Show resolved Hide resolved
crates/shared/src/order_quoting.rs Outdated Show resolved Hide resolved
crates/orderbook/src/database/orders.rs Show resolved Hide resolved
crates/orderbook/src/database/orders.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Just a few remaining nits.

crates/shared/src/trade_finding/mod.rs Show resolved Hide resolved
-- This migration script is reversible.

-- Step 1: Add two new columns to the quotes table
ALTER TABLE quotes
Copy link
Contributor

Choose a reason for hiding this comment

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

for historic quotes verified columns value could be wrong

That's true although I'd prefer a cleaner DB model over perfect historic data that will never be read. Also if anybody really cares about the accuracy of that data we know when the migration got applied and can detect historic values with the {} metadata value.

crates/database/src/orders.rs Outdated Show resolved Hide resolved
@mstrug
Copy link
Contributor Author

mstrug commented Dec 11, 2024

the {} metadata value.

To support deserialization of QuoteMetadata from empty json object I added QuoteMetadataDeserializationHelper enum which is used internally in QuoteMetadata::try_from(serde_json::Value). And also I've added unit tests to verify that.

Comment on lines 107 to 111
metadata: quote.data.metadata.try_into().map_err(
|e: serde_json::Error| {
AddOrderError::MetadataSerializationFailed(e.into())
},
)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
metadata: quote.data.metadata.try_into().map_err(
|e: serde_json::Error| {
AddOrderError::MetadataSerializationFailed(e.into())
},
)?,
metadata: quote.data.metadata.try_into().map_err(AddOrderError::MetadataSerializationFailed)?,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To apply that change, I also had to updated definition of AddOrderError::MetadataSerializationFailed to take serde_json::Error as its unnamed field.

#[serde(untagged)]
enum QuoteMetadataDeserializationHelper {
Data(QuoteMetadata),
Empty {},
Copy link
Contributor

Choose a reason for hiding this comment

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

If we were to add this to QuoteMetadata wouldn't work directly without the need of another enum? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for QuoteMetadata I use #[serde(tag = "version")] to achieve this flattening and converting QuoteMetadata variants to version json tag, which implies requiring that version tag exists in the input json, which is not possible for empty json object.
Generally, to get automatic conversion from empty json object {} to Empty {} variant, we need to use #[serde(untagged)] which cannot be used on QuoteMetadata where we use #[serde(tag = "version")].

Alternative approach that I have also considered is a custom implementation of the serde deserialize trait, but this becomes more complicated and requires more code (additional implementation of visitor trait) comparing to helper type approach.

@@ -44,6 +51,12 @@ pub enum TradeKind {
Regular(Trade),
}

impl Default for TradeKind {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: cannot this be directly derived?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately it cannot be derived directly, as Rust currently is not supporting #[default] attribute on non-unit enum variants.

Copy link
Contributor

@m-lord-renkse m-lord-renkse left a comment

Choose a reason for hiding this comment

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

Nice job!!

@mstrug mstrug merged commit 5b26d73 into main Dec 11, 2024
11 checks passed
@mstrug mstrug deleted the feature/storing-call-data-in-quotes branch December 11, 2024 12:44
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants