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: Shift prepay fee calculation into execution, fix failed transactions pipeline #2531

Merged
merged 2 commits into from
Oct 4, 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
30 changes: 12 additions & 18 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub struct EthCallArgs<'a> {
#[derive(Clone, Debug)]
pub struct ValidateTxInfo {
pub signed_tx: SignedTx,
pub prepay_fee: U256,
pub max_prepay_fee: U256,
}

pub struct TransferDomainTxInfo {
Expand Down Expand Up @@ -366,7 +366,7 @@ impl EVMCoreService {

let ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
} = if let Some(validate_info) = self.tx_validation_cache.get_stateless(tx) {
validate_info
} else {
Expand Down Expand Up @@ -407,14 +407,14 @@ impl EVMCoreService {
return Err(format_err!("gas limit higher than max_gas_per_block").into());
}

let prepay_fee = calculate_max_prepay_gas_fee(&signed_tx)?;
debug!("[validate_raw_tx] prepay_fee : {:x?}", prepay_fee);
let max_prepay_fee = calculate_max_prepay_gas_fee(&signed_tx)?;
debug!("[validate_raw_tx] max_prepay_fee : {:x?}", max_prepay_fee);

self.tx_validation_cache.set_stateless(
String::from(tx),
ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
},
)
};
Expand All @@ -424,7 +424,7 @@ impl EVMCoreService {
let mut backend = self.get_backend(state_root)?;
let balance = backend.get_balance(&signed_tx.sender);
debug!("[validate_raw_tx] Account balance : {:x?}", balance);
if balance < prepay_fee {
if balance < max_prepay_fee {
debug!("[validate_raw_tx] insufficient balance to pay fees");
return Err(format_err!("insufficient balance to pay fees").into());
}
Expand Down Expand Up @@ -455,7 +455,7 @@ impl EVMCoreService {
),
ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
},
));
} else {
Expand All @@ -472,13 +472,7 @@ impl EVMCoreService {
// Execute tx and validate total gas usage in queued txs do not exceed block size
let mut executor = AinExecutor::new(&mut backend);
executor.update_total_gas_used(total_current_gas_used);
executor.exec(
&signed_tx,
signed_tx.gas_limit(),
prepay_fee,
block_fee,
false,
)?;
executor.exec(&signed_tx, signed_tx.gas_limit(), block_fee, false)?;
}

Ok(self.tx_validation_cache.set(
Expand All @@ -490,7 +484,7 @@ impl EVMCoreService {
),
ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
},
))
}
Expand Down Expand Up @@ -569,7 +563,7 @@ impl EVMCoreService {

let ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
} = if let Some(validate_info) = self.tx_validation_cache.get_stateless(tx) {
validate_info
} else {
Expand Down Expand Up @@ -757,7 +751,7 @@ impl EVMCoreService {

ValidateTxInfo {
signed_tx,
prepay_fee: U256::zero(),
max_prepay_fee: U256::zero(),
}
};

Expand Down Expand Up @@ -786,7 +780,7 @@ impl EVMCoreService {

Ok(ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
})
}

Expand Down
9 changes: 8 additions & 1 deletion lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,24 @@ impl EVMServices {
queue.transactions.len()
);

let mut exceed_block_limit = false;
for queue_item in queue.transactions.clone() {
executor.update_total_gas_used(U256::from(total_gas_used));
let apply_result = match executor.apply_queue_tx(queue_item.tx, base_fee) {
Ok(result) => result,
Err(EVMError::BlockSizeLimit(message)) => {
debug!("[construct_block] {}", message);
failed_transactions.push(queue_item.tx_hash);
exceed_block_limit = true;
continue;
}
Err(e) => {
return Err(e);
if exceed_block_limit {
failed_transactions.push(queue_item.tx_hash);
continue;
} else {
return Err(e);
}
}
};

Expand Down
30 changes: 13 additions & 17 deletions lib/ain-evm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ impl<'backend> AinExecutor<'backend> {
&mut self,
signed_tx: &SignedTx,
gas_limit: U256,
prepay_fee: U256,
base_fee: U256,
system_tx: bool,
) -> Result<(TxResponse, ReceiptV3)> {
Expand All @@ -151,7 +150,12 @@ impl<'backend> AinExecutor<'backend> {
access_list: signed_tx.access_list(),
};

if !system_tx && prepay_fee != U256::zero() {
let prepay_fee = if system_tx {
U256::zero()
} else {
calculate_current_prepay_gas_fee(signed_tx, base_fee)?
};
if !system_tx {
self.backend
.deduct_prepay_gas_fee(signed_tx.sender, prepay_fee)?;
}
Expand Down Expand Up @@ -188,10 +192,8 @@ impl<'backend> AinExecutor<'backend> {
let total_gas_used = self.backend.vicinity.total_gas_used;
let block_gas_limit = self.backend.vicinity.block_gas_limit;
if !system_tx && total_gas_used + U256::from(used_gas) > block_gas_limit {
if !prepay_fee != U256::zero() {
self.backend
.refund_unused_gas_fee(signed_tx, U256::zero(), base_fee)?;
}
self.backend
.refund_unused_gas_fee(signed_tx, U256::zero(), base_fee)?;
return Err(EVMError::BlockSizeLimit(
"Block size limit exceeded, tx cannot make it into the block".to_string(),
));
Expand All @@ -202,7 +204,7 @@ impl<'backend> AinExecutor<'backend> {
ApplyBackend::apply(self.backend, values, logs.clone(), true);
self.backend.commit();

if !system_tx && prepay_fee != U256::zero() {
if !system_tx {
self.backend
.refund_unused_gas_fee(signed_tx, U256::from(used_gas), base_fee)?;
}
Expand Down Expand Up @@ -243,14 +245,8 @@ impl<'backend> AinExecutor<'backend> {
.into());
}

let prepay_fee = calculate_current_prepay_gas_fee(&signed_tx, base_fee)?;
let (tx_response, receipt) = self.exec(
&signed_tx,
signed_tx.gas_limit(),
prepay_fee,
base_fee,
false,
)?;
let (tx_response, receipt) =
self.exec(&signed_tx, signed_tx.gas_limit(), base_fee, false)?;

debug!(
"[apply_queue_tx]receipt : {:?}, exit_reason {:#?} for signed_tx : {:#x}",
Expand Down Expand Up @@ -316,7 +312,7 @@ impl<'backend> AinExecutor<'backend> {
}

let (tx_response, receipt) =
self.exec(&signed_tx, U256::MAX, U256::zero(), U256::zero(), true)?;
self.exec(&signed_tx, U256::MAX, U256::zero(), true)?;
if !tx_response.exit_reason.is_succeed() {
return Err(format_err!(
"[apply_queue_tx] Transfer domain failed VM execution {:?}",
Expand Down Expand Up @@ -373,7 +369,7 @@ impl<'backend> AinExecutor<'backend> {
self.commit();

let (tx_response, receipt) =
self.exec(&signed_tx, U256::MAX, U256::zero(), U256::zero(), true)?;
self.exec(&signed_tx, U256::MAX, U256::zero(), true)?;
if !tx_response.exit_reason.is_succeed() {
debug!(
"[apply_queue_tx] DST20 bridge failed VM execution {:?}, data {}",
Expand Down
12 changes: 6 additions & 6 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,20 +353,20 @@ fn unsafe_prevalidate_raw_tx_in_q(
unsafe {
let ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
} = SERVICES
.evm
.core
.validate_raw_tx(raw_tx, queue_id, true, U256::zero())?;

let nonce = u64::try_from(signed_tx.nonce())?;
let prepay_fee = u64::try_from(prepay_fee)?;
let max_prepay_fee = u64::try_from(max_prepay_fee)?;

Ok(ffi::ValidateTxCompletion {
nonce,
sender: format!("{:?}", signed_tx.sender),
tx_hash: format!("{:?}", signed_tx.hash()),
prepay_fee,
max_prepay_fee,
})
}
}
Expand Down Expand Up @@ -403,21 +403,21 @@ fn unsafe_validate_raw_tx_in_q(queue_id: u64, raw_tx: &str) -> Result<ffi::Valid
unsafe {
let ValidateTxInfo {
signed_tx,
prepay_fee,
max_prepay_fee,
} = SERVICES
.evm
.core
.validate_raw_tx(raw_tx, queue_id, false, block_fee)?;

let nonce = u64::try_from(signed_tx.nonce())?;

let prepay_fee = u64::try_from(prepay_fee)?;
let max_prepay_fee = u64::try_from(max_prepay_fee)?;

Ok(ffi::ValidateTxCompletion {
nonce,
sender: format!("{:?}", signed_tx.sender),
tx_hash: format!("{:?}", signed_tx.hash()),
prepay_fee,
max_prepay_fee,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub mod ffi {
pub nonce: u64,
pub sender: String,
pub tx_hash: String,
pub prepay_fee: u64,
pub max_prepay_fee: u64,
}

extern "Rust" {
Expand Down