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

evm: Fix divide by zero error in eth_estimateGas #2697

Merged
merged 10 commits into from
Nov 20, 2023

Conversation

shohamc1
Copy link
Contributor

No description provided.

@shohamc1 shohamc1 requested a review from sieniven November 17, 2023 05:44
@shohamc1 shohamc1 marked this pull request as ready for review November 17, 2023 08:13
@@ -39,6 +39,25 @@ pub struct CallRequest {
}

impl CallRequest {
pub fn guess_tx_type(&mut self) -> Result<(), Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the context here ? Why do we want to re-assign to transaction_type ?

Copy link
Contributor

@sieniven sieniven Nov 17, 2023

Choose a reason for hiding this comment

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

@shohamc1 I guess the tradeoff in abstracting this out of th effective_gas_price calculation is that we cannot keep CallRequest immutable. Not sure if that is the best way although i think the idea is nice to always set a tx type on every call context. We could abstract this into our custom deserialize implementation actually which may keep it cleaner. Alternatively maybe keeping it inside effective_gas_price calculation is still the best way, since the tx type in the call context is really only relevant for gas price calculations, may be safer.

Copy link
Contributor

@sieniven sieniven Nov 17, 2023

Choose a reason for hiding this comment

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

Note that most libraries underlying implementation when sending estimate gas dont actually set a tx type - so we will find ourselves going into this method pretty often. Also, that will mean that CallRequest is required to guess_tx_type, which should be set on deserialization, since get_effective_gas_price will now expect an explicit tx type or result in an error. Might be better to keep the field optional and let effective_gas_price method handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made guess_tx_type not mutate CallRequest in 506043e as a temporary fix to unblock RPCs. We can merge this and then explore what the best way to handle transaction_type will be.

sieniven
sieniven previously approved these changes Nov 17, 2023
@sieniven sieniven self-requested a review November 17, 2023 10:32
}

let allowance = available
.checked_div(fee_cap)
.ok_or(RPCError::ValueOverflow)?;
.ok_or(RPCError::DivideByZero)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't validate the fee_cap is not zero before get divided for error DividedByZero??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is what checked_div does.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm.. i mean that should have 2 validations:
i. fee_cap is not allowed to zero
ii. check_div for underoverflow checking

Copy link
Member

Choose a reason for hiding this comment

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

Agree. And in relation to the other point, should be more generic.

fee_cap check, ideal to have it checked, but probably not necessary if it isn't used directly later, and can just rely on checked_div

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can use checked_div for both for the moment. get_effective_gas_price already prevents fee_cap from being 0.

@prasannavl
Copy link
Member

Going ahead and merging this to unblock a patch release today, but let's fix the error type in another PR to cleanup. DivideByZero is not the most accurate error to be returned.

@@ -39,6 +39,27 @@ pub struct CallRequest {
}

impl CallRequest {
pub fn guess_tx_type(&self) -> Result<Self, Error> {
Copy link
Member

Choose a reason for hiding this comment

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

Let's rename this. The response does co-relate well with the guess name.

Perhaps clone_with_guessed_tx_type

Copy link
Member

@prasannavl prasannavl Nov 20, 2023

Choose a reason for hiding this comment

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

Or better yet - even better is guess_tx_type simply just returns the guess.

And then just do a clone internally in the method, and use the clone going forward?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to clone_with_guessed_tx_type for now. We can decide on how to properly handle the mutation after we have unblocked the RPC users.

@@ -39,6 +39,27 @@ pub struct CallRequest {
}

impl CallRequest {
pub fn clone_with_guessed_tx_type(&self) -> Result<Self, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only used by get_effective_gas_price where we can switch on the tx type itself. We shouldn't need to clone nor re-assign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

transaction_type is optional and not included by certain clients (notably Metamask), which is why we need this function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure but this can be directly called by get_effective_gas_price and only return the tx_type itself

@Bushstar Bushstar merged commit c10105e into master Nov 20, 2023
12 of 14 checks passed
@Bushstar Bushstar deleted the evm/fix-estimategas-zero branch November 20, 2023 10:57
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.

6 participants