-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement AccessTuple conversion methods in firehose-protos
- Loading branch information
1 parent
4450967
commit 8c97ebe
Showing
7 changed files
with
98 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,86 @@ | ||
use crate::error::ProtosError; | ||
|
||
use super::AccessTuple; | ||
|
||
use alloy_eip2930::AccessListItem; | ||
use alloy_primitives::{hex, Address, B256}; | ||
|
||
impl TryFrom<&AccessTuple> for AccessListItem { | ||
type Error = ProtosError; | ||
|
||
fn try_from(tuple: &AccessTuple) -> Result<Self, Self::Error> { | ||
let address = Address::from_slice(tuple.address.as_slice()); | ||
|
||
let storage_keys = tuple | ||
.storage_keys | ||
.iter() | ||
.map(convert_to_b256) | ||
.collect::<Result<Vec<B256>, ProtosError>>()?; | ||
|
||
Ok(AccessListItem { | ||
address, | ||
storage_keys, | ||
}) | ||
} | ||
} | ||
|
||
fn convert_to_b256(key: &Vec<u8>) -> Result<B256, ProtosError> { | ||
let key_bytes: [u8; 32] = key | ||
.as_slice() | ||
.try_into() | ||
.map_err(|_| ProtosError::InvalidAccessTupleStorageKey(hex::encode(key.clone())))?; | ||
Ok(B256::from(key_bytes)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn create_mock_access_tuple() -> AccessTuple { | ||
AccessTuple { | ||
address: vec![0x11; 20], | ||
storage_keys: vec![vec![0xaa; 32], vec![0xbb; 32]], | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_access_tuple_to_access_list_item_conversion() { | ||
let mock_tuple = create_mock_access_tuple(); | ||
|
||
let access_list_item = AccessListItem::try_from(&mock_tuple).expect("Conversion failed"); | ||
|
||
assert_eq!(access_list_item.address, Address::from([0x11; 20])); | ||
assert_eq!( | ||
access_list_item.storage_keys.len(), | ||
mock_tuple.storage_keys.len() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_access_tuple_with_empty_storage_keys() { | ||
let mock_tuple = AccessTuple { | ||
address: vec![0x11; 20], | ||
storage_keys: vec![], | ||
}; | ||
|
||
let access_list_item = AccessListItem::try_from(&mock_tuple).expect("Conversion failed"); | ||
|
||
assert_eq!(access_list_item.address, Address::from([0x11; 20])); | ||
assert!(access_list_item.storage_keys.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_access_tuple_storage_key_invalid_length() { | ||
let mock_tuple = AccessTuple { | ||
address: vec![0x11; 20], | ||
storage_keys: vec![vec![0xaa; 31]], | ||
}; | ||
|
||
let error = AccessListItem::try_from(&mock_tuple).unwrap_err(); | ||
|
||
assert!(matches!( | ||
error, | ||
ProtosError::InvalidAccessTupleStorageKey(_) | ||
)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,37 +1,14 @@ | ||
use crate::transactions::error::TransactionError; | ||
use alloy_eip2930::{AccessList, AccessListItem}; | ||
use alloy_primitives::{Address, B256}; | ||
use firehose_protos::ethereum_v2::AccessTuple; | ||
use revm_primitives::hex; | ||
|
||
pub(crate) fn compute_access_list( | ||
access_list: &[AccessTuple], | ||
) -> Result<AccessList, TransactionError> { | ||
let access_list_items: Vec<AccessListItem> = access_list | ||
let access_list_items = access_list | ||
.iter() | ||
.map(atuple_to_alist_item) | ||
.collect::<Result<Vec<AccessListItem>, TransactionError>>( | ||
)?; | ||
.map(AccessListItem::try_from) | ||
.collect::<Result<Vec<AccessListItem>, _>>()?; | ||
|
||
Ok(AccessList(access_list_items)) | ||
} | ||
|
||
fn atuple_to_alist_item(tuple: &AccessTuple) -> Result<AccessListItem, TransactionError> { | ||
let address: Address = Address::from_slice(tuple.address.as_slice()); | ||
let storage_keys = tuple | ||
.storage_keys | ||
.iter() | ||
.map(|key| { | ||
let key_bytes: [u8; 32] = key | ||
.as_slice() | ||
.try_into() | ||
.map_err(|_| TransactionError::InvalidStorageKey(hex::encode(key.clone())))?; | ||
Ok(B256::from(key_bytes)) | ||
}) | ||
.collect::<Result<Vec<B256>, TransactionError>>()?; | ||
|
||
Ok(AccessListItem { | ||
address, | ||
storage_keys, | ||
}) | ||
} |
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