-
Notifications
You must be signed in to change notification settings - Fork 15
Independent logic for Partial and Full OrderValidation #1176
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1176 +/- ##
==========================================
+ Coverage 66.69% 67.15% +0.46%
==========================================
Files 135 135
Lines 27039 27223 +184
==========================================
+ Hits 18034 18282 +248
+ Misses 9005 8941 -64 |
self.pre_validate(PreOrderData::from(order.clone())) | ||
.await | ||
.map_err(ValidationError::Pre)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it weird that post_validate
calls pre_validate
. From the names I would expect you to have to call both:
validator.pre_validate()?;
// do stuff...
validator.post_validate()?;
Maybe pre-
/post-
prefixes can be changed (partial-
/full-
? Maybe something else)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, initially I had a call to each independently, but the only place this is done would be in Orderbook.add_order
. I was worried that future refactors might forget that then both need to be called, so I enforced this by calling the pre inside the post. I completely agree that the name could be changed to partial
and full
or maybe even partial_validate
and just validate
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun fact, I had already mentioned this here #1176 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe even partial_validate and just validate.
Works for me. @vkgnosis, from your comments, I think you also didn't find the names super clear, do you have any suggestions as well?
pub trait WarpReplyConverting { | ||
fn to_warp_reply(&self) -> (Json, StatusCode); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I think the trait here is cool, I don't see any generic code with something like T: WarpReplayConverting
and feels a bit of overkill.
I'm OK with leaving it in though, but maybe makes sense to move it to the api.rs
module because its not order validation specific?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will become more useful once #1176 comes into play (so I can implement this trait for a struct that is not declared inside this crate). Would that be a good reason for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I would still not use a trait in that situation (for mostly stylistic reasons), but there isn't anything wrong with doing so feel free to leave it in if you like it. I don't see a strong reason for using or not using a trait either way.
I think a more concrete reason to introduce such a trait would be the ability to get rid of all those *_response
(for example create_order_response
) methods and introduce a generic one:
pub fn convert_response<T, E>(result: Result<T, E>) -> impl Reply
where
T: serde::Serialize,
E: WarpReplyConverting,
{
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not suggesting this be done in this PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool I will defs do this (after).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now I will move the trait to api.rs
(i.e. before merging this).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good. Just some comments.
Its too bad GitHub UI doesn't catch that its a file rename, making it a bit harder to review.
I could rename it back for now and just do the rename later if you prefer. |
Its fine. It was mostly me complaining at the Github UI, which does it sometimes even when Git identifies changes as a file rename. |
Too late... already renamed back... Will change again later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually made it much easier to review! Tahnks.
Similar to the Pre-Order Validation introduced in #1169, we offload some of the order validation statements contained in
Orderbook.add_order
to a specialized structPostOrderValidator
. This makes it possible to test and maintain Order Validation without making changes to the orderbook logic itself.Test Plan
Added new unit tests for all possible routes in
PostOrderValidator.validate()
along with tests for helper methodmin_balance