Skip to content

Commit fda0778

Browse files
einar-oplabsmeyer9
authored andcommitted
refactor: replace tuple with BlockNumHash (#254)
Resolves #239 --------- Co-authored-by: Einar Rasmussen <[email protected]>
1 parent b101120 commit fda0778

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

Cargo.lock

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

crates/optimism/trie/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ reth-metrics = { workspace = true, features = ["common"] }
2828

2929
# ethereum
3030
alloy-primitives.workspace = true
31+
alloy-eips.workspace = true
3132

3233
# async
3334
tokio = { workspace = true, features = ["sync"] }
@@ -59,4 +60,5 @@ reth-trie = { workspace = true, features = ["test-utils"] }
5960
serde-bincode-compat = [
6061
"reth-primitives-traits/serde-bincode-compat",
6162
"reth-trie/serde-bincode-compat",
63+
"alloy-eips/serde-bincode-compat",
6264
]

crates/optimism/trie/src/db/models/block.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use alloy_eips::BlockNumHash;
12
use alloy_primitives::B256;
23
use bytes::BufMut;
3-
use derive_more::{Constructor, From, Into};
4+
use derive_more::{From, Into};
45
use reth_db::{
56
table::{Compress, Decompress},
67
DatabaseError,
@@ -10,16 +11,16 @@ use serde::{Deserialize, Serialize};
1011
/// Wrapper for block number and block hash tuple to implement [`Compress`]/[`Decompress`].
1112
///
1213
/// Used for storing block metadata (number + hash).
13-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, From, Into, Constructor)]
14-
pub struct BlockNumberHash(pub u64, pub B256);
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, From, Into)]
15+
pub struct BlockNumberHash(BlockNumHash);
1516

1617
impl Compress for BlockNumberHash {
1718
type Compressed = Vec<u8>;
1819

1920
fn compress_to_buf<B: BufMut + AsMut<[u8]>>(&self, buf: &mut B) {
2021
// Encode block number (8 bytes, big-endian) + hash (32 bytes) = 40 bytes total
21-
buf.put_u64(self.0);
22-
buf.put_slice(self.1.as_slice());
22+
buf.put_u64(self.0.number);
23+
buf.put_slice(self.0.hash.as_slice());
2324
}
2425
}
2526

@@ -29,11 +30,27 @@ impl Decompress for BlockNumberHash {
2930
return Err(DatabaseError::Decode);
3031
}
3132

32-
let block_number =
33-
u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::Decode)?);
33+
let number = u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::Decode)?);
3434
let hash = B256::from_slice(&value[8..40]);
3535

36-
Ok(Self(block_number, hash))
36+
Ok(Self(BlockNumHash { number, hash }))
37+
}
38+
}
39+
40+
impl BlockNumberHash {
41+
/// Create new instance.
42+
pub const fn new(number: u64, hash: B256) -> Self {
43+
Self(BlockNumHash { number, hash })
44+
}
45+
46+
/// Get the block number.
47+
pub const fn number(&self) -> u64 {
48+
self.0.number
49+
}
50+
51+
/// Get the block hash.
52+
pub const fn hash(&self) -> B256 {
53+
self.0.hash
3754
}
3855
}
3956

@@ -45,9 +62,9 @@ mod tests {
4562
#[test]
4663
fn test_block_number_hash_roundtrip() {
4764
let test_cases = vec![
48-
BlockNumberHash(0, B256::ZERO),
49-
BlockNumberHash(42, B256::repeat_byte(0xaa)),
50-
BlockNumberHash(u64::MAX, B256::repeat_byte(0xff)),
65+
BlockNumberHash::new(0, B256::ZERO),
66+
BlockNumberHash::new(42, B256::repeat_byte(0xaa)),
67+
BlockNumberHash::new(u64::MAX, B256::repeat_byte(0xff)),
5168
];
5269

5370
for original in test_cases {

crates/optimism/trie/src/db/store.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl MdbxProofsStorage {
4444
let result = self.env.view(|tx| {
4545
let mut cursor = tx.cursor_read::<ProofWindow>().ok()?;
4646
let value = cursor.seek_exact(key).ok()?;
47-
value.map(|(_, val)| (val.0, val.1))
47+
value.map(|(_, val)| (val.number(), val.hash()))
4848
});
4949
Ok(result?)
5050
}
@@ -56,7 +56,8 @@ impl MdbxProofsStorage {
5656
) -> OpProofsStorageResult<()> {
5757
self.env.update(|tx| {
5858
let mut cursor = tx.new_cursor::<ProofWindow>()?;
59-
cursor.append(ProofWindowKey::EarliestBlock, &BlockNumberHash(block_number, hash))?;
59+
cursor
60+
.append(ProofWindowKey::EarliestBlock, &BlockNumberHash::new(block_number, hash))?;
6061
Ok(())
6162
})?
6263
}

0 commit comments

Comments
 (0)