-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: hoist trait test utilities (#708)
- Loading branch information
Showing
9 changed files
with
76 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! An implementation of the [BlobProvider] trait for tests. | ||
use alloc::{boxed::Box, vec::Vec}; | ||
use alloy_eips::eip4844::Blob; | ||
use alloy_primitives::{map::HashMap, B256}; | ||
use async_trait::async_trait; | ||
use op_alloy_protocol::BlockInfo; | ||
|
||
use crate::{errors::BlobProviderError, sources::IndexedBlobHash, traits::BlobProvider}; | ||
|
||
/// A mock blob provider for testing. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct TestBlobProvider { | ||
/// Maps block hashes to blob data. | ||
pub blobs: HashMap<B256, Blob>, | ||
/// whether the blob provider should return an error. | ||
pub should_error: bool, | ||
} | ||
|
||
impl TestBlobProvider { | ||
/// Insert a blob into the mock blob provider. | ||
pub fn insert_blob(&mut self, hash: B256, blob: Blob) { | ||
self.blobs.insert(hash, blob); | ||
} | ||
|
||
/// Clears blobs from the mock blob provider. | ||
pub fn clear(&mut self) { | ||
self.blobs.clear(); | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl BlobProvider for TestBlobProvider { | ||
type Error = BlobProviderError; | ||
|
||
async fn get_blobs( | ||
&mut self, | ||
_block_ref: &BlockInfo, | ||
blob_hashes: &[IndexedBlobHash], | ||
) -> Result<Vec<Box<Blob>>, Self::Error> { | ||
if self.should_error { | ||
return Err(BlobProviderError::SlotDerivation); | ||
} | ||
let mut blobs = Vec::new(); | ||
for blob_hash in blob_hashes { | ||
if let Some(data) = self.blobs.get(&blob_hash.hash) { | ||
blobs.push(Box::new(*data)); | ||
} | ||
} | ||
Ok(blobs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters