-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "compat" | ||
version = "0.1.0" | ||
publish = false | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
|
||
[dependencies] | ||
alloy = {workspace = true } | ||
ethereum-types = { workspace = true } |
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,67 @@ | ||
/// A trait to convert between alloy and ethereum types. | ||
pub trait Compat<Out> { | ||
/// Convert the type to another type | ||
fn compat(self) -> Out; | ||
} | ||
|
||
impl Compat<ethereum_types::H160> for alloy::primitives::Address { | ||
fn compat(self) -> ethereum_types::H160 { | ||
let alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) = self; | ||
ethereum_types::H160(arr) | ||
} | ||
} | ||
|
||
impl Compat<ethereum_types::H256> for alloy::primitives::B256 { | ||
fn compat(self) -> ethereum_types::H256 { | ||
let alloy::primitives::FixedBytes(arr) = self; | ||
ethereum_types::H256(arr) | ||
} | ||
} | ||
|
||
impl Compat<[ethereum_types::U256; 8]> for alloy::primitives::Bloom { | ||
fn compat(self) -> [ethereum_types::U256; 8] { | ||
let alloy::primitives::Bloom(alloy::primitives::FixedBytes(src)) = self; | ||
// have u8 * 256 | ||
// want U256 * 8 | ||
// (no unsafe, no unstable) | ||
let mut chunks = src.chunks_exact(32); | ||
let dst = core::array::from_fn(|_ix| { | ||
// This is a bit spicy because we're going from an uninterpeted array of bytes | ||
// to wide integers, but we trust this `From` impl to do the right thing | ||
ethereum_types::U256::from(<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap()) | ||
}); | ||
assert_eq!(chunks.len(), 0); | ||
dst | ||
} | ||
} | ||
|
||
impl Compat<ethereum_types::U256> for alloy::primitives::U256 { | ||
fn compat(self) -> ethereum_types::U256 { | ||
ethereum_types::U256(self.into_limbs()) | ||
} | ||
} | ||
|
||
impl Compat<Vec<Vec<u8>>> for Vec<alloy::primitives::Bytes> { | ||
fn compat(self) -> Vec<Vec<u8>> { | ||
self.into_iter().map(|x| x.to_vec()).collect() | ||
} | ||
} | ||
|
||
impl Compat<alloy::primitives::Address> for ethereum_types::H160 { | ||
fn compat(self) -> alloy::primitives::Address { | ||
let ethereum_types::H160(arr) = self; | ||
alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) | ||
} | ||
} | ||
|
||
impl Compat<alloy::primitives::StorageKey> for ethereum_types::H256 { | ||
fn compat(self) -> alloy::primitives::StorageKey { | ||
let ethereum_types::H256(arr) = self; | ||
alloy::primitives::FixedBytes(arr) | ||
} | ||
} | ||
|
||
#[test] | ||
fn bloom() { | ||
let _did_not_panic = alloy::primitives::Bloom::ZERO.compat(); | ||
} |
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