Skip to content

Commit

Permalink
Fail invalid nonce on validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Sep 18, 2023
1 parent 27bb62c commit 07a50cf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 33 deletions.
31 changes: 6 additions & 25 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cmp::Ordering;
use std::{path::PathBuf, sync::Arc};

use crate::fee::calculate_prepay_gas_fee;
Expand Down Expand Up @@ -47,7 +46,6 @@ pub struct ValidateTxInfo {
pub signed_tx: SignedTx,
pub prepay_fee: U256,
pub higher_nonce: bool,
pub lower_nonce: bool,
}

fn init_vsdb(path: PathBuf) {
Expand Down Expand Up @@ -279,36 +277,20 @@ impl EVMCoreService {
signed_tx.nonce()
)
.into());
} else if nonce < signed_tx.nonce() {
return Ok(ValidateTxInfo {
signed_tx,
prepay_fee,
higher_nonce: true,
});
}

return Ok(ValidateTxInfo {
signed_tx,
prepay_fee,
higher_nonce: false,
lower_nonce: false,
});
} else {
// Should be queued for now and don't go through VM validation
match signed_tx.nonce().cmp(&nonce) {
Ordering::Greater => {
return Ok(ValidateTxInfo {
signed_tx,
prepay_fee,
higher_nonce: true,
lower_nonce: false,
});
}
Ordering::Less => {
return Ok(ValidateTxInfo {
signed_tx,
prepay_fee,
higher_nonce: false,
lower_nonce: true,
});
}
_ => {}
}

// Validate tx prepay fees with account balance
let balance = backend.get_balance(&signed_tx.sender);
debug!("[validate_raw_tx] Account balance : {:x?}", balance);
Expand Down Expand Up @@ -341,7 +323,6 @@ impl EVMCoreService {
signed_tx,
prepay_fee,
higher_nonce: false,
lower_nonce: false,
})
}

Expand Down
4 changes: 0 additions & 4 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ pub fn evm_unsafe_try_prevalidate_raw_tx_in_q(
signed_tx,
prepay_fee,
higher_nonce,
lower_nonce,
}) => {
let Ok(nonce) = u64::try_from(signed_tx.nonce()) else {
return cross_boundary_error_return(result, "nonce value overflow");
Expand All @@ -492,7 +491,6 @@ pub fn evm_unsafe_try_prevalidate_raw_tx_in_q(
tx_hash: format!("{:?}", signed_tx.hash()),
prepay_fee,
higher_nonce,
lower_nonce,
},
)
}
Expand Down Expand Up @@ -548,7 +546,6 @@ pub fn evm_unsafe_try_validate_raw_tx_in_q(
signed_tx,
prepay_fee,
higher_nonce,
lower_nonce,
}) => {
let Ok(nonce) = u64::try_from(signed_tx.nonce()) else {
return cross_boundary_error_return(result, "nonce value overflow");
Expand All @@ -566,7 +563,6 @@ pub fn evm_unsafe_try_validate_raw_tx_in_q(
tx_hash: format!("{:?}", signed_tx.hash()),
prepay_fee,
higher_nonce,
lower_nonce,
},
)
}
Expand Down
1 change: 0 additions & 1 deletion lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ pub mod ffi {
pub tx_hash: String,
pub prepay_fee: u64,
pub higher_nonce: bool,
pub lower_nonce: bool,
}

extern "Rust" {
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/consensus/xvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,16 @@ Res CXVMConsensus::operator()(const CEvmTxMessage &obj) const {
return Res::Err("evm tx size too large");

CrossBoundaryResult result;
ValidateTxCompletion validateResults;
if (evmPreValidate) {
validateResults = evm_unsafe_try_prevalidate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx));
evm_unsafe_try_prevalidate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx));
if (!result.ok) {
LogPrintf("[evm_try_prevalidate_raw_tx] failed, reason : %s\n", result.reason);
return Res::Err("evm tx failed to pre-validate %s", result.reason);
}
return Res::Ok();
}

ValidateTxCompletion validateResults;
validateResults = evm_unsafe_try_validate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx));
if (validateResults.higher_nonce) {
return Res::Ok();
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ bool BlockAssembler::EvmTxPreapply(EvmTxPreApplyContext& ctx)
const auto obj = std::get<CEvmTxMessage>(txMessage);

ValidateTxCompletion txResult;
txResult = evm_unsafe_try_validate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx));
txResult = evm_unsafe_try_prevalidate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx));
if (!result.ok) {
return false;
}
Expand Down

0 comments on commit 07a50cf

Please sign in to comment.