Skip to content

Commit

Permalink
Guard TransactionQueue data with single mutex (#2225)
Browse files Browse the repository at this point in the history
* Guard transaction queue data in with single mutex

* Rename context to queue_id

* Rename context

* Trigger actions
  • Loading branch information
Jouzo authored Jul 26, 2023
1 parent 4037785 commit 3919458
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 222 deletions.
42 changes: 21 additions & 21 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl EVMCoreService {
pub fn validate_raw_tx(
&self,
tx: &str,
context: u64,
queue_id: u64,
use_context: bool,
) -> Result<ValidateTxInfo, Box<dyn Error>> {
debug!("[validate_raw_tx] raw transaction : {:#?}", tx);
Expand Down Expand Up @@ -258,7 +258,7 @@ impl EVMCoreService {
debug!("[validate_raw_tx] used_gas: {:#?}", used_gas);
let total_current_gas_used = self
.tx_queues
.get_total_gas_used(context)
.get_total_gas_used(queue_id)
.unwrap_or_default();

if U256::from(total_current_gas_used + used_gas) > MAX_GAS_PER_BLOCK {
Expand Down Expand Up @@ -287,20 +287,20 @@ impl EVMCoreService {
impl EVMCoreService {
pub fn add_balance(
&self,
context: u64,
queue_id: u64,
address: H160,
amount: U256,
hash: NativeTxHash,
) -> Result<(), EVMError> {
let queue_tx = QueueTx::BridgeTx(BridgeTx::EvmIn(BalanceUpdate { address, amount }));
self.tx_queues
.queue_tx(context, queue_tx, hash, 0u64, U256::zero())?;
.queue_tx(queue_id, queue_tx, hash, 0u64, U256::zero())?;
Ok(())
}

pub fn sub_balance(
&self,
context: u64,
queue_id: u64,
address: H160,
amount: U256,
hash: NativeTxHash,
Expand All @@ -320,34 +320,34 @@ impl EVMCoreService {
} else {
let queue_tx = QueueTx::BridgeTx(BridgeTx::EvmOut(BalanceUpdate { address, amount }));
self.tx_queues
.queue_tx(context, queue_tx, hash, 0u64, U256::zero())?;
.queue_tx(queue_id, queue_tx, hash, 0u64, U256::zero())?;
Ok(())
}
}

pub fn get_context(&self) -> u64 {
self.tx_queues.get_context()
pub fn get_queue_id(&self) -> u64 {
self.tx_queues.get_queue_id()
}

pub fn clear(&self, context: u64) -> Result<(), EVMError> {
self.tx_queues.clear(context)?;
pub fn clear(&self, queue_id: u64) -> Result<(), EVMError> {
self.tx_queues.clear(queue_id)?;
Ok(())
}

pub fn remove(&self, context: u64) {
self.tx_queues.remove(context);
pub fn remove(&self, queue_id: u64) {
self.tx_queues.remove(queue_id);
}

pub fn remove_txs_by_sender(&self, context: u64, address: H160) -> Result<(), EVMError> {
self.tx_queues.remove_txs_by_sender(context, address)?;
pub fn remove_txs_by_sender(&self, queue_id: u64, address: H160) -> Result<(), EVMError> {
self.tx_queues.remove_txs_by_sender(queue_id, address)?;
Ok(())
}

/// Retrieves the next valid nonce for the specified account within a particular context.
/// Retrieves the next valid nonce for the specified account within a particular queue.
///
/// The method first attempts to retrieve the next valid nonce from the transaction queue associated with the
/// provided context. If no nonce is found in the transaction queue, that means that no transactions have been
/// queued for this account in this context. It falls back to retrieving the nonce from the storage at the latest
/// provided queue_id. If no nonce is found in the transaction queue, that means that no transactions have been
/// queued for this account in this queue_id. It falls back to retrieving the nonce from the storage at the latest
/// block. If no nonce is found in the storage (i.e., no transactions for this account have been committed yet),
/// the nonce is defaulted to zero.
///
Expand All @@ -356,16 +356,16 @@ impl EVMCoreService {
///
/// # Arguments
///
/// * `context` - The context queue number.
/// * `queue_id` - The queue_id queue number.
/// * `address` - The EVM address of the account whose nonce we want to retrieve.
///
/// # Returns
///
/// Returns the next valid nonce as a `U256`. Defaults to U256::zero()
pub fn get_next_valid_nonce_in_context(&self, context: u64, address: H160) -> U256 {
pub fn get_next_valid_nonce_in_queue(&self, queue_id: u64, address: H160) -> U256 {
let nonce = self
.tx_queues
.get_next_valid_nonce(context, address)
.get_next_valid_nonce(queue_id, address)
.unwrap_or_else(|| {
let latest_block = self
.storage
Expand All @@ -378,7 +378,7 @@ impl EVMCoreService {
});

debug!(
"Account {:x?} nonce {:x?} in context {context}",
"Account {:x?} nonce {:x?} in queue_id {queue_id}",
address, nonce
);
nonce
Expand Down
28 changes: 14 additions & 14 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ impl EVMServices {

pub fn finalize_block(
&self,
context: u64,
queue_id: u64,
update_state: bool,
difficulty: u32,
beneficiary: H160,
timestamp: u64,
) -> Result<FinalizedBlockInfo, Box<dyn Error>> {
let mut all_transactions = Vec::with_capacity(self.core.tx_queues.len(context));
let mut failed_transactions = Vec::with_capacity(self.core.tx_queues.len(context));
let mut receipts_v3: Vec<ReceiptV3> = Vec::with_capacity(self.core.tx_queues.len(context));
let mut all_transactions = Vec::with_capacity(self.core.tx_queues.len(queue_id));
let mut failed_transactions = Vec::with_capacity(self.core.tx_queues.len(queue_id));
let mut receipts_v3: Vec<ReceiptV3> = Vec::with_capacity(self.core.tx_queues.len(queue_id));
let mut total_gas_used = 0u64;
let mut total_gas_fees = U256::zero();
let mut logs_bloom: Bloom = Bloom::default();
Expand Down Expand Up @@ -145,7 +145,7 @@ impl EVMServices {

let mut executor = AinExecutor::new(&mut backend);

for (queue_tx, hash) in self.core.tx_queues.get_cloned_vec(context) {
for (queue_tx, hash) in self.core.tx_queues.get_cloned_vec(queue_id) {
match queue_tx {
QueueTx::SignedTx(signed_tx) => {
if ain_cpp_imports::past_changi_intermediate_height_4_height() {
Expand Down Expand Up @@ -185,8 +185,8 @@ impl EVMServices {
}
QueueTx::BridgeTx(BridgeTx::EvmIn(BalanceUpdate { address, amount })) => {
debug!(
"[finalize_block] EvmIn for address {:x?}, amount: {}, context {}",
address, amount, context
"[finalize_block] EvmIn for address {:x?}, amount: {}, queue_id {}",
address, amount, queue_id
);
if let Err(e) = executor.add_balance(address, amount) {
debug!("[finalize_block] EvmIn failed with {e}");
Expand Down Expand Up @@ -268,23 +268,23 @@ impl EVMServices {
total_priority_fees
);

match self.core.tx_queues.get_total_fees(context) {
match self.core.tx_queues.get_total_fees(queue_id) {
Some(total_fees) => {
if (total_burnt_fees + total_priority_fees) != U256::from(total_fees) {
return Err(anyhow!("EVM block rejected because block total fees != (burnt fees + priority fees). Burnt fees: {}, priority fees: {}", total_burnt_fees, total_priority_fees).into());
}
}
None => {
return Err(anyhow!(
"EVM block rejected because failed to get total fees from context: {}",
context
"EVM block rejected because failed to get total fees from queue_id: {}",
queue_id
)
.into())
}
}

if update_state {
self.core.tx_queues.remove(context);
self.core.tx_queues.remove(queue_id);
}

Ok(FinalizedBlockInfo {
Expand All @@ -295,7 +295,7 @@ impl EVMServices {
})
} else {
if update_state {
self.core.tx_queues.remove(context);
self.core.tx_queues.remove(queue_id);
}

Ok(FinalizedBlockInfo {
Expand Down Expand Up @@ -333,7 +333,7 @@ impl EVMServices {

pub fn queue_tx(
&self,
context: u64,
queue_id: u64,
tx: QueueTx,
hash: NativeTxHash,
gas_used: u64,
Expand All @@ -347,7 +347,7 @@ impl EVMServices {

self.core
.tx_queues
.queue_tx(context, tx.clone(), hash, gas_used, base_fee)?;
.queue_tx(queue_id, tx.clone(), hash, gas_used, base_fee)?;

if let QueueTx::SignedTx(signed_tx) = tx {
self.filters.add_tx_to_filters(signed_tx.transaction.hash())
Expand Down
Loading

0 comments on commit 3919458

Please sign in to comment.