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 pre-validation evm tx check #2411

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,6 @@ impl EVMCoreService {
return Err(format_err!("value more than money range").into());
}

let balance = self
.get_balance(signed_tx.sender, block_number)
.map_err(|e| format_err!("Error getting balance {e}"))?;
let prepay_fee = calculate_prepay_gas_fee(&signed_tx)?;
debug!("[validate_raw_tx] Account balance : {:x?}", balance);
debug!("[validate_raw_tx] prepay_fee : {:x?}", prepay_fee);

// Validate tx prepay fees with account balance
if balance < prepay_fee {
debug!("[validate_raw_tx] insufficient balance to pay fees");
return Err(format_err!("insufficient balance to pay fees").into());
}

// Validate tx gas limit with intrinsic gas
check_tx_intrinsic_gas(&signed_tx)?;

Expand Down Expand Up @@ -307,8 +294,21 @@ impl EVMCoreService {
u64::default()
};

// Validate total gas usage in queued txs exceeds block size
let prepay_fee = calculate_prepay_gas_fee(&signed_tx)?;
debug!("[validate_raw_tx] prepay_fee : {:x?}", prepay_fee);
if use_queue {
// Validate tx prepay fees with account balance
let balance = self
.get_balance(signed_tx.sender, block_number)
.map_err(|e| format_err!("Error getting balance {e}"))?;
debug!("[validate_raw_tx] Account balance : {:x?}", balance);

if balance < prepay_fee {
debug!("[validate_raw_tx] insufficient balance to pay fees");
return Err(format_err!("insufficient balance to pay fees").into());
}

// Validate total gas usage in queued txs exceeds block size
debug!("[validate_raw_tx] used_gas: {:#?}", used_gas);
let total_current_gas_used = self
.tx_queues
Expand Down
12 changes: 6 additions & 6 deletions test/functional/feature_evm_fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_max_gas_price(self):
# Test insufficient balance due to high gas fees
assert_raises_rpc_error(
-32001,
"evm tx failed to validate insufficient balance to pay fees",
"evm tx failed to validate prepay fee value overflow",
self.nodes[0].eth_sendTransaction,
{
"from": self.ethAddress,
Expand Down Expand Up @@ -254,11 +254,7 @@ def test_fee_deduction_empty_balance(self):
emptyAddress = self.nodes[0].getnewaddress("", "erc55")
balance = self.nodes[0].eth_getBalance(emptyAddress, "latest")
assert_equal(int(balance[2:], 16), 000000000000000000000)

assert_raises_rpc_error(
-32001,
"evm tx failed to validate insufficient balance to pay fees",
self.nodes[0].eth_sendTransaction,
self.nodes[0].eth_sendTransaction(
{
"from": emptyAddress,
"to": self.toAddress,
Expand All @@ -267,6 +263,10 @@ def test_fee_deduction_empty_balance(self):
"gasPrice": "0x2540BE400", # 10_000_000_000
},
)
self.nodes[0].generate(1)
block = self.nodes[0].getblock(self.nodes[0].getbestblockhash())
# Tx should be valid and enter the mempool, but will not be minted into the block
assert_equal(len(block["tx"]), 1)

self.rollback_to(height)

Expand Down
Loading