mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Migrate from ethereum-types to alloy-primitives (#6078)
* Remove use of ethers_core::RlpStream
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* Remove old code
* Simplify keccak call
* Remove unused package
* Merge branch 'unstable' of https://github.com/ethDreamer/lighthouse into remove_use_of_ethers_core
* Merge branch 'unstable' into remove_use_of_ethers_core
* Run clippy
* Merge branch 'remove_use_of_ethers_core' of https://github.com/dospore/lighthouse into remove_use_of_ethers_core
* Check all cargo fmt
* migrate to alloy primitives init
* fix deps
* integrate alloy-primitives
* resolve dep issues
* more changes based on dep changes
* add TODOs
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* Revert lock
* Add BeaconBlocksByRange v3
* continue migration
* Revert "Add BeaconBlocksByRange v3"
This reverts commit e3ce7fc5ea.
* impl hash256 extended trait
* revert some uneeded diffs
* merge conflict resolved
* fix subnet id rshift calc
* rename to FixedBytesExtended
* debugging
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fix failed test
* fixing more tests
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into remove_use_of_ethers_core
* introduce a shim to convert between the two u256 types
* move alloy to wrokspace
* align alloy versions
* update
* update web3signer test certs
* refactor
* resolve failing tests
* linting
* fix graffiti string test
* fmt
* fix ef test
* resolve merge conflicts
* remove udep and revert cert
* cargo patch
* cyclic dep
* fix build error
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* resolve conflicts, update deps
* merge unstable
* fmt
* fix deps
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* resolve merge conflicts
* resolve conflicts, make necessary changes
* Remove patch
* fmt
* remove file
* merge conflicts
* sneaking in a smol change
* bump versions
* Merge remote-tracking branch 'origin/unstable' into migrate-to-alloy-primitives
* Updates for peerDAS
* Update ethereum_hashing to prevent dupe
* updated alloy-consensus, removed TODOs
* cargo update
* endianess fix
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fmt
* fix merge
* fix test
* fixed_bytes crate
* minor fixes
* convert u256 to i64
* panic free mixin to_low_u64_le
* from_str_radix
* computbe_subnet api and ensuring we use big-endian
* Merge branch 'unstable' of https://github.com/sigp/lighthouse into migrate-to-alloy-primitives
* fix test
* Simplify subnet_id test
* Simplify some more tests
* Add tests to fixed_bytes crate
* Merge branch 'unstable' into migrate-to-alloy-primitives
This commit is contained in:
11
consensus/fixed_bytes/Cargo.toml
Normal file
11
consensus/fixed_bytes/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "fixed_bytes"
|
||||
version = "0.1.0"
|
||||
authors = ["Eitan Seri-Levi <eitan@sigmaprime.io>"]
|
||||
edition = { workspace = true }
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
alloy-primitives = { workspace = true }
|
||||
safe_arith = { workspace = true }
|
||||
160
consensus/fixed_bytes/src/lib.rs
Normal file
160
consensus/fixed_bytes/src/lib.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
use alloy_primitives::FixedBytes;
|
||||
use safe_arith::SafeArith;
|
||||
|
||||
pub type Hash64 = alloy_primitives::B64;
|
||||
pub type Hash256 = alloy_primitives::B256;
|
||||
pub type Uint256 = alloy_primitives::U256;
|
||||
pub type Address = alloy_primitives::Address;
|
||||
|
||||
pub trait UintExtended {
|
||||
fn to_i64(self) -> i64;
|
||||
}
|
||||
|
||||
pub trait FixedBytesExtended {
|
||||
fn from_low_u64_be(value: u64) -> Self;
|
||||
fn from_low_u64_le(value: u64) -> Self;
|
||||
fn to_low_u64_le(&self) -> u64;
|
||||
fn zero() -> Self;
|
||||
}
|
||||
|
||||
impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
|
||||
fn from_low_u64_be(value: u64) -> Self {
|
||||
let value_bytes = value.to_be_bytes();
|
||||
let mut buffer = [0x0; N];
|
||||
let bytes_to_copy = value_bytes.len().min(buffer.len());
|
||||
// Panic-free because bytes_to_copy <= buffer.len()
|
||||
let start_index = buffer
|
||||
.len()
|
||||
.safe_sub(bytes_to_copy)
|
||||
.expect("bytes_to_copy <= buffer.len()");
|
||||
// Panic-free because start_index <= buffer.len()
|
||||
// and bytes_to_copy <= value_bytes.len()
|
||||
buffer
|
||||
.get_mut(start_index..)
|
||||
.expect("start_index <= buffer.len()")
|
||||
.copy_from_slice(
|
||||
value_bytes
|
||||
.get(..bytes_to_copy)
|
||||
.expect("bytes_to_copy <= value_byte.len()"),
|
||||
);
|
||||
Self::from(buffer)
|
||||
}
|
||||
|
||||
fn from_low_u64_le(value: u64) -> Self {
|
||||
let value_bytes = value.to_le_bytes();
|
||||
let mut buffer = [0x0; N];
|
||||
let bytes_to_copy = value_bytes.len().min(buffer.len());
|
||||
// Panic-free because bytes_to_copy <= buffer.len(),
|
||||
// and bytes_to_copy <= value_bytes.len()
|
||||
buffer
|
||||
.get_mut(..bytes_to_copy)
|
||||
.expect("bytes_to_copy <= buffer.len()")
|
||||
.copy_from_slice(
|
||||
value_bytes
|
||||
.get(..bytes_to_copy)
|
||||
.expect("bytes_to_copy <= value_byte.len()"),
|
||||
);
|
||||
Self::from(buffer)
|
||||
}
|
||||
|
||||
fn zero() -> Self {
|
||||
Self::ZERO
|
||||
}
|
||||
|
||||
/// Trims FixedBytes<N> to its first 8 bytes and converts to u64
|
||||
fn to_low_u64_le(&self) -> u64 {
|
||||
let mut result = [0u8; 8];
|
||||
let bytes = self.as_slice();
|
||||
// Panic-free because result.len() == bytes[0..8].len()
|
||||
result.copy_from_slice(&bytes[0..8]);
|
||||
u64::from_le_bytes(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl FixedBytesExtended for alloy_primitives::Address {
|
||||
fn from_low_u64_be(value: u64) -> Self {
|
||||
FixedBytes::<20>::from_low_u64_be(value).into()
|
||||
}
|
||||
|
||||
fn from_low_u64_le(value: u64) -> Self {
|
||||
FixedBytes::<20>::from_low_u64_le(value).into()
|
||||
}
|
||||
|
||||
fn zero() -> Self {
|
||||
FixedBytes::<20>::zero().into()
|
||||
}
|
||||
|
||||
fn to_low_u64_le(&self) -> u64 {
|
||||
FixedBytes::<20>::to_low_u64_le(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl UintExtended for Uint256 {
|
||||
/// Trims the Uint256 to its first 8 bytes and converts to i64
|
||||
fn to_i64(self) -> i64 {
|
||||
let mut result = [0u8; 8];
|
||||
let bytes = self.to_le_bytes::<32>();
|
||||
// Panic-free because result.len() == bytes[0..8].len()
|
||||
result.copy_from_slice(&bytes[0..8]);
|
||||
i64::from_le_bytes(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use alloy_primitives::bytes::Buf;
|
||||
|
||||
#[test]
|
||||
fn from_low_u64_be() {
|
||||
let values = [0, 1, 0xff, 1 << 16, u64::MAX, u64::MAX - 1];
|
||||
for value in values {
|
||||
assert_eq!(
|
||||
(&Hash256::from_low_u64_be(value).as_slice()[24..]).get_u64(),
|
||||
value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_low_u64_le() {
|
||||
let values = [0, 1, 0xff, 1 << 16, u64::MAX, u64::MAX - 1];
|
||||
for value in values {
|
||||
assert_eq!(
|
||||
u64::from_le_bytes(
|
||||
Hash256::from_low_u64_le(value).as_slice()[0..8]
|
||||
.try_into()
|
||||
.unwrap()
|
||||
),
|
||||
value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_low_u64_le() {
|
||||
let values = [0, 1, 0xff, 1 << 16, u64::MAX, u64::MAX - 1];
|
||||
for value in values {
|
||||
assert_eq!(Hash256::from_low_u64_le(value).to_low_u64_le(), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_i64_in_range() {
|
||||
let values = [0, 1, 0xff, 1 << 16, i64::MAX, i64::MAX - 1];
|
||||
for value in values {
|
||||
assert_eq!(Uint256::from(value).to_i64(), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_i64_out_of_range() {
|
||||
let values = [u128::MAX, 1 << 70, 1 << 80, i64::MAX as u128 + 1];
|
||||
for value in values {
|
||||
assert_eq!(
|
||||
Uint256::from(value).to_i64(),
|
||||
i64::from_le_bytes(value.to_le_bytes()[0..8].try_into().unwrap())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user