Skip to content

Commit

Permalink
feat(test-utils): Make MockEthProvider generic over Transaction (#13853)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Dhanraj30 and mattsse authored Jan 22, 2025
1 parent 6937578 commit cc8558f
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ use std::{

/// A mock implementation for Provider interfaces.
#[derive(Debug, Clone)]
pub struct MockEthProvider {
pub struct MockEthProvider<T = TransactionSigned> {
/// Local block store
pub blocks: Arc<Mutex<HashMap<B256, Block>>>,
pub blocks: Arc<Mutex<HashMap<B256, Block<T>>>>,
/// Local header store
pub headers: Arc<Mutex<HashMap<B256, Header>>>,
/// Local account store
Expand All @@ -55,7 +55,52 @@ pub struct MockEthProvider {
pub state_roots: Arc<Mutex<Vec<B256>>>,
}

impl Default for MockEthProvider {
impl<T> MockEthProvider<T> {
/// Add block to local block store
pub fn add_block(&self, hash: B256, block: Block<T>) {
self.add_header(hash, block.header.clone());
self.blocks.lock().insert(hash, block);
}

/// Add multiple blocks to local block store
pub fn extend_blocks(&self, iter: impl IntoIterator<Item = (B256, Block<T>)>) {
for (hash, block) in iter {
self.add_header(hash, block.header.clone());
self.add_block(hash, block)
}
}

/// Add header to local header store
pub fn add_header(&self, hash: B256, header: Header) {
self.headers.lock().insert(hash, header);
}

/// Add multiple headers to local header store
pub fn extend_headers(&self, iter: impl IntoIterator<Item = (B256, Header)>) {
for (hash, header) in iter {
self.add_header(hash, header)
}
}

/// Add account to local account store
pub fn add_account(&self, address: Address, account: ExtendedAccount) {
self.accounts.lock().insert(address, account);
}

/// Add account to local account store
pub fn extend_accounts(&self, iter: impl IntoIterator<Item = (Address, ExtendedAccount)>) {
for (address, account) in iter {
self.add_account(address, account)
}
}

/// Add state root to local state root store
pub fn add_state_root(&self, state_root: B256) {
self.state_roots.lock().push(state_root);
}
}

impl<T> Default for MockEthProvider<T> {
fn default() -> Self {
Self {
blocks: Default::default(),
Expand Down Expand Up @@ -104,51 +149,6 @@ impl ExtendedAccount {
}
}

impl MockEthProvider {
/// Add block to local block store
pub fn add_block(&self, hash: B256, block: Block) {
self.add_header(hash, block.header.clone());
self.blocks.lock().insert(hash, block);
}

/// Add multiple blocks to local block store
pub fn extend_blocks(&self, iter: impl IntoIterator<Item = (B256, Block)>) {
for (hash, block) in iter {
self.add_header(hash, block.header.clone());
self.add_block(hash, block)
}
}

/// Add header to local header store
pub fn add_header(&self, hash: B256, header: Header) {
self.headers.lock().insert(hash, header);
}

/// Add multiple headers to local header store
pub fn extend_headers(&self, iter: impl IntoIterator<Item = (B256, Header)>) {
for (hash, header) in iter {
self.add_header(hash, header)
}
}

/// Add account to local account store
pub fn add_account(&self, address: Address, account: ExtendedAccount) {
self.accounts.lock().insert(address, account);
}

/// Add account to local account store
pub fn extend_accounts(&self, iter: impl IntoIterator<Item = (Address, ExtendedAccount)>) {
for (address, account) in iter {
self.add_account(address, account)
}
}

/// Add state root to local state root store
pub fn add_state_root(&self, state_root: B256) {
self.state_roots.lock().push(state_root);
}
}

/// Mock node.
#[derive(Debug)]
pub struct MockNode;
Expand Down

0 comments on commit cc8558f

Please sign in to comment.