Skip to content

Commit 5162316

Browse files
committed
fixed_bytes crate
1 parent 4cd0268 commit 5162316

File tree

13 files changed

+48
-118
lines changed

13 files changed

+48
-118
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ eth2_keystore = { path = "crypto/eth2_keystore" }
204204
eth2_network_config = { path = "common/eth2_network_config" }
205205
eth2_wallet = { path = "crypto/eth2_wallet" }
206206
execution_layer = { path = "beacon_node/execution_layer" }
207+
fixed_bytes = { path = "consensus/fixed_bytes" }
207208
filesystem = { path = "common/filesystem" }
208209
fork_choice = { path = "consensus/fork_choice" }
209210
genesis = { path = "beacon_node/genesis" }

consensus/fixed_bytes/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "fixed_bytes"
3+
version = "0.1.0"
4+
authors = ["Eitan Seri-Levi <[email protected]>"]
5+
edition = { workspace = true }
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
alloy-primitives = { workspace = true }
11+
safe_arith = { workspace = true }

consensus/types/src/fixed_bytes.rs renamed to consensus/fixed_bytes/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
use alloy_primitives::{Address, FixedBytes};
1+
use alloy_primitives::FixedBytes;
22
use safe_arith::SafeArith;
33

4+
pub type Hash64 = alloy_primitives::B64;
5+
pub type Hash256 = alloy_primitives::B256;
6+
pub type Uint256 = alloy_primitives::U256;
7+
pub type Address = alloy_primitives::Address;
8+
49
pub trait FixedBytesExtended {
510
fn from_low_u64_be(value: u64) -> Self;
611
fn from_low_u64_le(value: u64) -> Self;
@@ -16,7 +21,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
1621
let start_index = buffer
1722
.len()
1823
.safe_sub(bytes_to_copy)
19-
.expect("byte_to_copy <= buffer.len()");
24+
.expect("bytes_to_copy <= buffer.len()");
2025
// Panic-free because start_index <= buffer.len()
2126
// and bytes_to_copy <= value_bytes.len()
2227
buffer
@@ -34,7 +39,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
3439
let value_bytes = value.to_le_bytes();
3540
let mut buffer = [0x0; N];
3641
let bytes_to_copy = value_bytes.len().min(buffer.len());
37-
// Panic-free because bytes_to_copy <= buffer.len()
42+
// Panic-free because bytes_to_copy <= buffer.len(),
3843
// and bytes_to_copy <= value_bytes.len()
3944
buffer
4045
.get_mut(..bytes_to_copy)
@@ -52,7 +57,7 @@ impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
5257
}
5358
}
5459

55-
impl FixedBytesExtended for Address {
60+
impl FixedBytesExtended for alloy_primitives::Address {
5661
fn from_low_u64_be(value: u64) -> Self {
5762
FixedBytes::<20>::from_low_u64_be(value).into()
5863
}

consensus/merkle_proof/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = { workspace = true }
88
alloy-primitives = { workspace = true }
99
ethereum_hashing = { workspace = true }
1010
safe_arith = { workspace = true }
11+
fixed_bytes = { workspace = true }
1112

1213
[dev-dependencies]
1314
quickcheck = { workspace = true }

consensus/merkle_proof/src/lib.rs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,9 @@
1-
use alloy_primitives::FixedBytes;
21
use ethereum_hashing::{hash, hash32_concat, ZERO_HASHES};
3-
use safe_arith::{ArithError, SafeArith};
2+
use safe_arith::ArithError;
43
use std::sync::LazyLock;
54

6-
type H256 = alloy_primitives::B256;
7-
8-
pub trait FixedBytesExtended {
9-
fn from_low_u64_be(value: u64) -> Self;
10-
fn from_low_u64_le(value: u64) -> Self;
11-
fn zero() -> Self;
12-
}
13-
14-
impl<const N: usize> FixedBytesExtended for FixedBytes<N> {
15-
fn from_low_u64_be(value: u64) -> Self {
16-
let value_bytes = value.to_be_bytes();
17-
let mut buffer = [0x0; N];
18-
let bytes_to_copy = value_bytes.len().min(buffer.len());
19-
// Panic-free because bytes_to_copy <= buffer.len()
20-
let start_index = buffer
21-
.len()
22-
.safe_sub(bytes_to_copy)
23-
.expect("bytes_to_copy <= buffer.len()");
24-
// Panic-free because start_index <= buffer.len()
25-
// and bytes_to_copy <= value_bytes.len()
26-
buffer
27-
.get_mut(start_index..)
28-
.expect("start_index <= buffer.len()")
29-
.copy_from_slice(
30-
value_bytes
31-
.get(..bytes_to_copy)
32-
.expect("bytes_to_copy <= value_byte.len()"),
33-
);
34-
Self::from(buffer)
35-
}
36-
37-
fn from_low_u64_le(value: u64) -> Self {
38-
let value_bytes = value.to_le_bytes();
39-
let mut buffer = [0x0; N];
40-
let bytes_to_copy = value_bytes.len().min(buffer.len());
41-
// Panic-free because bytes_to_copy <= buffer.len()
42-
// and bytes_to_copy <= value_bytes.len()
43-
buffer
44-
.get_mut(..bytes_to_copy)
45-
.expect("bytes_to_copy <= buffer.len()")
46-
.copy_from_slice(
47-
value_bytes
48-
.get(..bytes_to_copy)
49-
.expect("bytes_to_copy <= value_byte.len()"),
50-
);
51-
Self::from(buffer)
52-
}
53-
54-
fn zero() -> Self {
55-
Self::ZERO
56-
}
57-
}
5+
type H256 = fixed_bytes::Hash256;
6+
pub use fixed_bytes::FixedBytesExtended;
587

598
const MAX_TREE_DEPTH: usize = 32;
609
const EMPTY_SLICE: &[H256] = &[];

consensus/swap_or_not_shuffle/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ criterion = { workspace = true }
1414
[dependencies]
1515
alloy-primitives = { workspace = true }
1616
ethereum_hashing = { workspace = true }
17+
fixed_bytes = { workspace = true }
1718

1819
[features]
1920
arbitrary = ["alloy-primitives/arbitrary"]

consensus/swap_or_not_shuffle/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ mod shuffle_list;
2020
pub use compute_shuffled_index::compute_shuffled_index;
2121
pub use shuffle_list::shuffle_list;
2222

23-
type Hash256 = alloy_primitives::B256;
23+
type Hash256 = fixed_bytes::Hash256;

consensus/types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ maplit = { workspace = true }
5252
alloy-rlp = { version = "0.3.4", features = ["derive"] }
5353
milhouse = { workspace = true }
5454
rpds = { workspace = true }
55+
fixed_bytes = { workspace = true }
5556

5657
[dev-dependencies]
5758
criterion = { workspace = true }

consensus/types/src/beacon_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use self::committee_cache::get_active_validator_indices;
2-
use crate::fixed_bytes::FixedBytesExtended;
32
use crate::historical_summary::HistoricalSummary;
43
use crate::test_utils::TestRandom;
4+
use crate::FixedBytesExtended;
55
use crate::*;
66
use compare_fields::CompareFields;
77
use compare_fields_derive::CompareFields;

0 commit comments

Comments
 (0)