-
Notifications
You must be signed in to change notification settings - Fork 809
chore: refactor validate_against_state_and_deduct_caller #2991
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
Open
rakita
wants to merge
9
commits into
main
Choose a base branch
from
rakita/refactor-deduct-caller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−239
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca70bba
chore: refactor validate_against_state_and_deduct_caller
rakita 26c3e61
inline
rakita d4dc43d
Merge remote-tracking branch 'origin/main' into modul-valid
rakita 786d83c
add box
rakita 2470e11
more functions and op handler
rakita 8dcaa86
simplify erc20 example
rakita 9967d46
Merge remote-tracking branch 'origin/main' into refac
rakita bd7507a
Merge remote-tracking branch 'origin/main' into refac
rakita ed1ad3b
nits
rakita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,7 @@ use context_interface::{ | |
use core::cmp::Ordering; | ||
use primitives::StorageKey; | ||
use primitives::{eip7702, hardfork::SpecId, KECCAK_EMPTY, U256}; | ||
use state::AccountInfo; | ||
use std::boxed::Box; | ||
use state::{Account, AccountInfo}; | ||
|
||
/// Loads and warms accounts for execution, including precompiles and access list. | ||
pub fn load_accounts< | ||
|
@@ -66,6 +65,26 @@ pub fn load_accounts< | |
Ok(()) | ||
} | ||
|
||
/// Validates caller account nonce and code according to EIP-3607. | ||
/// | ||
/// Internally calls [`validate_account_nonce_and_code`] with the components from the configuration. | ||
#[inline] | ||
pub fn validate_account_nonce_and_code_with_components( | ||
caller_info: &mut AccountInfo, | ||
tx: impl Transaction, | ||
cfg: impl Cfg, | ||
) -> Result<(), InvalidTransaction> { | ||
let is_eip3607_disabled = cfg.is_eip3607_disabled(); | ||
let is_nonce_check_disabled = cfg.is_nonce_check_disabled(); | ||
|
||
validate_account_nonce_and_code( | ||
caller_info, | ||
tx.nonce(), | ||
is_eip3607_disabled, | ||
is_nonce_check_disabled, | ||
) | ||
} | ||
|
||
/// Validates caller account nonce and code according to EIP-3607. | ||
#[inline] | ||
pub fn validate_account_nonce_and_code( | ||
|
@@ -106,73 +125,84 @@ pub fn validate_account_nonce_and_code( | |
Ok(()) | ||
} | ||
|
||
/// Validates caller state and deducts transaction costs from the caller's balance. | ||
/// Check maximum possible fee and deduct the effective fee. Returns new balance. | ||
#[inline] | ||
pub fn validate_against_state_and_deduct_caller< | ||
CTX: ContextTr, | ||
ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>, | ||
>( | ||
context: &mut CTX, | ||
) -> Result<(), ERROR> { | ||
let basefee = context.block().basefee() as u128; | ||
let blob_price = context.block().blob_gasprice().unwrap_or_default(); | ||
let is_balance_check_disabled = context.cfg().is_balance_check_disabled(); | ||
let is_eip3607_disabled = context.cfg().is_eip3607_disabled(); | ||
let is_nonce_check_disabled = context.cfg().is_nonce_check_disabled(); | ||
|
||
let (tx, journal) = context.tx_journal_mut(); | ||
|
||
// Load caller's account. | ||
let caller_account = journal.load_account_code(tx.caller())?.data; | ||
|
||
validate_account_nonce_and_code( | ||
&mut caller_account.info, | ||
tx.nonce(), | ||
is_eip3607_disabled, | ||
is_nonce_check_disabled, | ||
)?; | ||
|
||
let max_balance_spending = tx.max_balance_spending()?; | ||
|
||
// Check if account has enough balance for `gas_limit * max_fee`` and value transfer. | ||
// Transfer will be done inside `*_inner` functions. | ||
if max_balance_spending > caller_account.info.balance && !is_balance_check_disabled { | ||
return Err(InvalidTransaction::LackOfFundForMaxFee { | ||
fee: Box::new(max_balance_spending), | ||
balance: Box::new(caller_account.info.balance), | ||
} | ||
.into()); | ||
pub fn deduct_caller_balance_with_components( | ||
balance: U256, | ||
tx: impl Transaction, | ||
block: impl Block, | ||
cfg: impl Cfg, | ||
) -> Result<U256, InvalidTransaction> { | ||
let basefee = block.basefee() as u128; | ||
let blob_price = block.blob_gasprice().unwrap_or_default(); | ||
let is_balance_check_disabled = cfg.is_balance_check_disabled(); | ||
|
||
if !is_balance_check_disabled { | ||
tx.has_enough_balance(balance)?; | ||
} | ||
|
||
let effective_balance_spending = tx | ||
.effective_balance_spending(basefee, blob_price) | ||
.expect("effective balance is always smaller than max balance so it can't overflow"); | ||
|
||
// subtracting max balance spending with value that is going to be deducted later in the call. | ||
let gas_balance_spending = effective_balance_spending - tx.value(); | ||
|
||
let mut new_balance = caller_account | ||
.info | ||
.balance | ||
.saturating_sub(gas_balance_spending); | ||
// new balance | ||
let mut new_balance = balance.saturating_sub(gas_balance_spending); | ||
|
||
if is_balance_check_disabled { | ||
// Make sure the caller's balance is at least the value of the transaction. | ||
new_balance = new_balance.max(tx.value()); | ||
} | ||
|
||
let old_balance = caller_account.info.balance; | ||
Ok(new_balance) | ||
} | ||
|
||
/// Make changes to the caller account. | ||
/// | ||
/// Returns the old balance. | ||
#[inline] | ||
pub fn caller_touch_and_change( | ||
caller_account: &mut Account, | ||
new_balance: U256, | ||
is_call: bool, | ||
) -> U256 { | ||
// Touch account so we know it is changed. | ||
caller_account.mark_touch(); | ||
caller_account.info.balance = new_balance; | ||
|
||
let old_balance = core::mem::replace(&mut caller_account.info.balance, new_balance); | ||
// Bump the nonce for calls. Nonce for CREATE will be bumped in `make_create_frame`. | ||
if tx.kind().is_call() { | ||
if is_call { | ||
// Nonce is already checked | ||
caller_account.info.nonce = caller_account.info.nonce.saturating_add(1); | ||
} | ||
old_balance | ||
} | ||
|
||
/// Validates caller state and deducts transaction costs from the caller's balance. | ||
#[inline] | ||
pub fn validate_against_state_and_deduct_caller< | ||
CTX: ContextTr, | ||
ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>, | ||
>( | ||
context: &mut CTX, | ||
) -> Result<(), ERROR> { | ||
let (tx, block, cfg, journal) = context.tx_block_cfg_journal_mut(); | ||
|
||
// Load caller's account. | ||
let caller_account = journal.load_account_code(tx.caller())?.data; | ||
|
||
// validate account nonce and code | ||
validate_account_nonce_and_code_with_components(&mut caller_account.info, tx, cfg)?; | ||
|
||
let new_balance = | ||
deduct_caller_balance_with_components(caller_account.info.balance, tx, block, cfg)?; | ||
|
||
// make changes to the account | ||
let old_balance = caller_touch_and_change(caller_account, new_balance, tx.kind().is_call()); | ||
|
||
// journal the change | ||
journal.caller_accounting_journal_entry(tx.caller(), old_balance, tx.kind().is_call()); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Ok(()) | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.