Skip to content

Commit

Permalink
Fine-grained Pending state (polkadot-evm#1575)
Browse files Browse the repository at this point in the history
* Fine-grained Pending state

Signed-off-by: linning <[email protected]>

* Use CountedStorageMap for managing pending transactions

Signed-off-by: linning <[email protected]>

---------

Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P authored Jan 15, 2025
1 parent 802b196 commit 931a708
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ pub mod pallet {
UniqueSaturatedInto::<u32>::unique_saturated_into(to_remove),
));
}
Pending::<T>::kill();
}

fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Expand Down Expand Up @@ -348,10 +347,10 @@ pub mod pallet {
PreLogExists,
}

/// Current building block's transactions and receipts.
/// Mapping from transaction index to transaction in the current building block.
#[pallet::storage]
pub type Pending<T: Config> =
StorageValue<_, Vec<(Transaction, TransactionStatus, Receipt)>, ValueQuery>;
CountedStorageMap<_, Identity, u32, (Transaction, TransactionStatus, Receipt), OptionQuery>;

/// The current Ethereum block.
#[pallet::storage]
Expand Down Expand Up @@ -436,22 +435,25 @@ impl<T: Config> Pallet<T> {
}

fn store_block(post_log: Option<PostLogContent>, block_number: U256) {
let mut transactions = Vec::new();
let mut statuses = Vec::new();
let mut receipts = Vec::new();
let transactions_count = Pending::<T>::count();
let mut transactions = Vec::with_capacity(transactions_count as usize);
let mut statuses = Vec::with_capacity(transactions_count as usize);
let mut receipts = Vec::with_capacity(transactions_count as usize);
let mut logs_bloom = Bloom::default();
let mut cumulative_gas_used = U256::zero();
for (transaction, status, receipt) in Pending::<T>::get() {
transactions.push(transaction);
statuses.push(status);
receipts.push(receipt.clone());
let (logs, used_gas) = match receipt {
Receipt::Legacy(d) | Receipt::EIP2930(d) | Receipt::EIP1559(d) => {
(d.logs.clone(), d.used_gas)
}
};
cumulative_gas_used = used_gas;
Self::logs_bloom(logs, &mut logs_bloom);
for transaction_index in 0..transactions_count {
if let Some((transaction, status, receipt)) = Pending::<T>::take(transaction_index) {
transactions.push(transaction);
statuses.push(status);
receipts.push(receipt.clone());
let (logs, used_gas) = match receipt {
Receipt::Legacy(d) | Receipt::EIP2930(d) | Receipt::EIP1559(d) => {
(d.logs.clone(), d.used_gas)
}
};
cumulative_gas_used = used_gas;
Self::logs_bloom(logs, &mut logs_bloom);
}
}

let ommers = Vec::<ethereum::Header>::new();
Expand Down Expand Up @@ -597,9 +599,8 @@ impl<T: Config> Pallet<T> {
) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo> {
let (to, _, info) = Self::execute(source, &transaction, None)?;

let pending = Pending::<T>::get();
let transaction_hash = transaction.hash();
let transaction_index = pending.len() as u32;
let transaction_index = Pending::<T>::count();

let (reason, status, weight_info, used_gas, dest, extra_data) = match info.clone() {
CallOrCreateInfo::Call(info) => (
Expand Down Expand Up @@ -675,7 +676,9 @@ impl<T: Config> Pallet<T> {
};
let logs_bloom = status.logs_bloom;
let logs = status.clone().logs;
let cumulative_gas_used = if let Some((_, _, receipt)) = pending.last() {
let cumulative_gas_used = if let Some((_, _, receipt)) =
Pending::<T>::get(transaction_index.saturating_sub(1))
{
match receipt {
Receipt::Legacy(d) | Receipt::EIP2930(d) | Receipt::EIP1559(d) => {
d.used_gas.saturating_add(used_gas.effective)
Expand Down Expand Up @@ -706,7 +709,7 @@ impl<T: Config> Pallet<T> {
}
};

Pending::<T>::append((transaction, status, receipt));
Pending::<T>::insert(transaction_index, (transaction, status, receipt));

Self::deposit_event(Event::Executed {
from: source,
Expand Down

0 comments on commit 931a708

Please sign in to comment.