-
Notifications
You must be signed in to change notification settings - Fork 84
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
Draft: Create a wrap for eth::U256 in order to ensure correct serde #2086 #2416
Conversation
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
d9e0556
to
2ce009d
Compare
7103de8
to
d9f8c2c
Compare
I have read the CLA Document and I hereby sign the CLA |
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 I misunderstood the original issue but I thought we only want to have a new type that handles the (de)serialization without having to remember to use macros.
If we want to completely replace primitve_types::U256
with our new type I think we should look into implementing std::ops::Deref
for the new type. That way we probably wouldn't have to reimplement a bunch of functions for the new type that we are currently using on the existing type.
Alternatively if we want to actually completely replace primitive_types::U256
with the new type it might also make sense to add a new crate that re-exports all the primtive_types but replaces the regular U256
with the new U256
type. But if we already go that far could also consider forking the primitive_types
crate or even patching the behavior upstream.
d9f8c2c
to
2ac995e
Compare
@@ -51,7 +52,7 @@ pub enum Kind { | |||
/// The MEVBlocker private mempool. | |||
MEVBlocker { | |||
url: reqwest::Url, | |||
max_additional_tip: eth::U256, | |||
max_additional_tip: primitive_types::U256, |
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 did this to keep the same behavior.
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.
What same behaviour? Isn't eth::U256
just a re-export of primitive type?
27d1049
to
4b639d5
Compare
Would it be possible to split this activity into separate PRs moving from crate to crate(or any other separation kind)? Maybe, to also add tracking tasks to the original issue. |
cd66af2
to
28c8265
Compare
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.
The driver
and solvers
crate export a U256
type as part of the eth
module (e.g. here). Might be a good idea to export the new number::U256
instead of the current primitive_types::U256
instead to keep people from using the old type.
Overall I'm still not sure what the best solution is for this. :/
Maybe @fleupold, @sunce86 and @squadgazzz have an opinion as well?
501c058
to
ceaefb0
Compare
That looks convenient. |
ceaefb0
to
d41de45
Compare
It is a good idea, but I think the best is to do it in a followup issue, not to have a lot of more changes in this one, wdyt? |
d41de45
to
f8c0f85
Compare
Makes sense. I keep thinking that the new type is equivalent to the old one so it shouldn't introduce more changes. But actually we'd have to dereference in a lot of places then. |
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 alright to me. Let's give it a try.
f8c0f85
to
1015cc6
Compare
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.
LG. Do we have a follow-up issue? Could you link it here, please?
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 don't want to be negative about this PR (I realize a lot of work went into making all these changes), but what are we really gaining here? Maybe we were a bit too optimistic about the original issue in the first place?
We have a new type with a bunch of boilerplate conversion but now have to remember instead of using #[serde_as(as = "HexOrDecimalU256")]
to use number::256
(if most of other callsites continue using eth::U256
this may be as challenging). I assume we cannot get a clippy warning for this?
I guess the real win will only come when we replace the eth::U256
export with the new type (making confusion less likely)?
@@ -51,7 +52,7 @@ pub enum Kind { | |||
/// The MEVBlocker private mempool. | |||
MEVBlocker { | |||
url: reqwest::Url, | |||
max_additional_tip: eth::U256, | |||
max_additional_tip: primitive_types::U256, |
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.
What same behaviour? Isn't eth::U256
just a re-export of primitive type?
Unfortunately not. There is an open issue for warning on trait implementation on specific types but and the open draft PR doesn't seem like it would get merged any time soon.
|
I think 1 may be complicated to maintain. We would need to rebase the fork every time we want to bump to the latest version of The option 3 is not complicated, it should be a smaller PR than this one I believe. I could take care of that as a follow up issue (I'll create the issue once confirmed). |
1015cc6
to
c2f4b0e
Compare
Long term, I would prefer to forbid using |
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.
Mostly nits.
@@ -6,7 +6,6 @@ use { | |||
}, | |||
chrono::{DateTime, Utc}, | |||
itertools::Itertools, | |||
number::serialization::HexOrDecimalU256, |
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.
nit: can we also make the line below this one primitive_types::H160
, and explicitly use primitive_types::U256
where needed, so if we accidentally add a reference to this type next time, it's more obvious.
@@ -135,7 +135,7 @@ async fn eth_flow_tx_zero_fee(web3: Web3) { | |||
.get_order(ðflow_order.uid(onchain.contracts()).await) | |||
.await | |||
.unwrap(); | |||
order.metadata.executed_surplus_fee > U256::zero() | |||
order.metadata.executed_surplus_fee > U256::zero().into() |
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.
order.metadata.executed_surplus_fee > U256::zero().into() | |
order.metadata.executed_surplus_fee > number::U256::zero() |
@@ -376,10 +376,10 @@ async fn mixed_limit_and_market_orders_test(web3: Web3) { | |||
|
|||
let order_b = OrderCreation { | |||
sell_token: token_b.address(), | |||
sell_amount: to_wei(5), | |||
fee_amount: to_wei(1), | |||
sell_amount: to_wei(5).into(), |
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.
nit: what happens if we change to_wei()
to return number::U256
?
fn try_from(value: crate::U256) -> Result<Self, Self::Error> { | ||
ZeroU256::from(value).try_into() | ||
} | ||
} |
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.
This should be removed as not used.
impl U256 { | ||
pub fn zero() -> Self { | ||
Self(primitive_types::U256::zero()) | ||
} |
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.
nit: Instead, how about implementing an existing trait for this:
impl num::Zero for U256 {
fn zero() -> Self {
Self(primitive_types::U256::zero())
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
impl std::ops::Add for U256 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
Description
Remove the
eth::U256
type and create a new typeU256
incrates/number
. The idea is to enforce specific (de)serialization for for theU256
type as per #2086.Changes
The new type as been converted into
primitive_types::U256
when necessary when needed for the business logic. Ideal solution would be to propagate the new type not only as anAPI
type, but also as an internal type for the business logic operations (but it requires many more changes, it could be potentially done in a followup issue).How to test
#2086