Skip to content

Commit c3cff03

Browse files
authored
refactor: read e2store header type as big endian (#1725)
1 parent b07f251 commit c3cff03

File tree

8 files changed

+130
-48
lines changed

8 files changed

+130
-48
lines changed

crates/e2store/src/e2ss.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
//! e2ss := Version | CompressedHeader | account*
1313
//! account := CompressedAccount | CompressedStorage*
1414
//!
15-
//! Version = { type: 0x3265, data: nil }
16-
//! CompressedHeader = { type: 0x03, data: snappyFramed(rlp(header)) }
17-
//! CompressedAccount = { type: 0x08, data: snappyFramed(rlp(Account)) }
18-
//! CompressedStorage = { type: 0x09, data: snappyFramed(rlp(Vec<StorageItem>)) }
15+
//! Version = { type: 0x6532, data: nil }
16+
//! CompressedHeader = { type: 0x0300, data: snappyFramed(rlp(header)) }
17+
//! CompressedAccount = { type: 0x0800, data: snappyFramed(rlp(Account)) }
18+
//! CompressedStorage = { type: 0x0900, data: snappyFramed(rlp(Vec<StorageItem>)) }
1919
//!
2020
//! Account = { address_hash, AccountState, raw_bytecode, storage_entry_count }
2121
//! AccountState = { nonce, balance, storage_root, code_hash }
@@ -44,6 +44,7 @@ use crate::{
4444
stream::{E2StoreStreamReader, E2StoreStreamWriter},
4545
types::{Entry, VersionEntry},
4646
},
47+
entry_types,
4748
types::HeaderEntry,
4849
utils::underlying_io_error_kind,
4950
};
@@ -230,7 +231,7 @@ impl TryFrom<&Entry> for AccountEntry {
230231

231232
fn try_from(entry: &Entry) -> Result<Self, Self::Error> {
232233
ensure!(
233-
entry.header.type_ == 0x08,
234+
entry.header.type_ == entry_types::COMPRESSED_ACCOUNT,
234235
"invalid account entry: incorrect account type"
235236
);
236237
ensure!(
@@ -257,7 +258,7 @@ impl TryFrom<AccountEntry> for Entry {
257258
"FrameEncoder should write whole rlp encoding"
258259
);
259260
let encoded = encoder.into_inner()?;
260-
Ok(Entry::new(0x08, encoded))
261+
Ok(Entry::new(entry_types::COMPRESSED_ACCOUNT, encoded))
261262
}
262263
}
263264

@@ -283,7 +284,7 @@ impl TryFrom<&Entry> for StorageEntry {
283284

284285
fn try_from(entry: &Entry) -> Result<Self, Self::Error> {
285286
ensure!(
286-
entry.header.type_ == 0x09,
287+
entry.header.type_ == entry_types::COMPRESSED_STORAGE,
287288
"invalid storage entry: incorrect storage type"
288289
);
289290
ensure!(
@@ -310,7 +311,7 @@ impl TryFrom<StorageEntry> for Entry {
310311
"FrameEncoder should write whole rlp encoding"
311312
);
312313
let encoded = encoder.into_inner()?;
313-
Ok(Entry::new(0x09, encoded))
314+
Ok(Entry::new(entry_types::COMPRESSED_STORAGE, encoded))
314315
}
315316
}
316317

crates/e2store/src/e2store/memory.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ mod test {
6969
use ethportal_api::utils::bytes::{hex_decode, hex_encode};
7070

7171
use super::*;
72+
use crate::entry_types;
7273

7374
// test cases sourced from: https://github.com/ethereum/go-ethereum/pull/26621/
7475

@@ -88,7 +89,7 @@ mod test {
8889
fn test_entry_beef() {
8990
let expected = "0x2a00020000000000beef";
9091
let entry = Entry::deserialize(&hex_decode(expected).unwrap()).unwrap();
91-
assert_eq!(entry.header.type_, 0x2a); // 42
92+
assert_eq!(entry.header.type_, 0x2a00); // 10752
9293
assert_eq!(entry.header.length, 2);
9394
assert_eq!(entry.header.reserved, 0);
9495
assert_eq!(entry.value, vec![0xbe, 0xef]);
@@ -101,11 +102,14 @@ mod test {
101102
let expected = "0x2a00020000000000beef0900040000000000abcdabcd";
102103
let file = E2StoreMemory::deserialize(&hex_decode(expected).unwrap()).unwrap();
103104
assert_eq!(file.entries.len(), 2);
104-
assert_eq!(file.entries[0].header.type_, 0x2a); // 42
105+
assert_eq!(file.entries[0].header.type_, 0x2a00); // 10752
105106
assert_eq!(file.entries[0].header.length, 2);
106107
assert_eq!(file.entries[0].header.reserved, 0);
107108
assert_eq!(file.entries[0].value, vec![0xbe, 0xef]);
108-
assert_eq!(file.entries[1].header.type_, 0x09); // 9
109+
assert_eq!(
110+
file.entries[1].header.type_,
111+
entry_types::COMPRESSED_STORAGE
112+
); // 2304
109113
assert_eq!(file.entries[1].header.length, 4);
110114
assert_eq!(file.entries[1].header.reserved, 0);
111115
assert_eq!(file.entries[1].value, vec![0xab, 0xcd, 0xab, 0xcd]);

crates/e2store/src/e2store/types.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::{anyhow, ensure};
22
use ssz_derive::{Decode, Encode};
33

4+
use crate::entry_types;
5+
46
/// Represents an e2store `Entry`
57
#[derive(Default, Debug, Eq, PartialEq, Clone)]
68
pub struct Entry {
@@ -83,7 +85,7 @@ impl Header {
8385

8486
/// Write to a byte slice.
8587
fn write(&self, buf: &mut [u8]) {
86-
buf[0..2].copy_from_slice(&self.type_.to_le_bytes());
88+
buf[0..2].copy_from_slice(&self.type_.to_be_bytes());
8789
buf[2..6].copy_from_slice(&self.length.to_le_bytes());
8890
buf[6..8].copy_from_slice(&self.reserved.to_le_bytes());
8991
}
@@ -93,7 +95,7 @@ impl Header {
9395
if bytes.len() != Header::SERIALIZED_SIZE as usize {
9496
return Err(anyhow!("invalid header size: {}", bytes.len()));
9597
}
96-
let type_ = u16::from_le_bytes([bytes[0], bytes[1]]);
98+
let type_ = u16::from_be_bytes([bytes[0], bytes[1]]);
9799
let length = u32::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5]]);
98100
let reserved = u16::from_le_bytes([bytes[6], bytes[7]]);
99101
ensure!(
@@ -116,7 +118,7 @@ pub struct VersionEntry {
116118
impl Default for VersionEntry {
117119
fn default() -> Self {
118120
Self {
119-
version: Entry::new(0x3265, vec![]),
121+
version: Entry::new(entry_types::VERSION, vec![]),
120122
}
121123
}
122124
}
@@ -126,7 +128,7 @@ impl TryFrom<&Entry> for VersionEntry {
126128

127129
fn try_from(entry: &Entry) -> anyhow::Result<Self> {
128130
ensure!(
129-
entry.header.type_ == 0x3265,
131+
entry.header.type_ == entry_types::VERSION,
130132
"invalid version entry: incorrect header type"
131133
);
132134
ensure!(

crates/e2store/src/entry_types.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// Snappy compressed ssz signed beacon block
2+
///
3+
/// data: snappyFramed(ssz(SignedBeaconBlock))
4+
pub const COMPRESSED_SIGNED_BEACON_BLOCK: u16 = 0x0100;
5+
6+
/// Snappy compressed ssz beacon state
7+
///
8+
/// data: snappyFramed(ssz(BeaconState))
9+
pub const COMPRESSED_BEACON_STATE: u16 = 0x0200;
10+
11+
/// Snappy compressed rlp execution header
12+
///
13+
/// data: snappyFramed(rlp(header))
14+
pub const COMPRESSED_HEADER: u16 = 0x0300;
15+
16+
/// Snappy compressed rlp execution body
17+
///
18+
/// data: snappyFramed(rlp(body))
19+
pub const COMPRESSED_BODY: u16 = 0x0400;
20+
21+
/// Snappy compressed rlp execution receipts
22+
///
23+
/// data: snappyFramed(rlp(receipts))
24+
pub const COMPRESSED_RECEIPTS: u16 = 0x0500;
25+
26+
/// Total difficulty of a block
27+
///
28+
/// data: uint256(header.total_difficulty)
29+
pub const TOTAL_DIFFICULTY: u16 = 0x0600;
30+
31+
/// Accumulator
32+
///
33+
/// data: accumulator-root
34+
/// header-record := { block-hash: Bytes32, total-difficulty: Uint256 }
35+
/// accumulator-root := hash_tree_root([]header-record, 8192)
36+
pub const ACCUMULATOR: u16 = 0x0700;
37+
38+
/// CompressedAccount
39+
///
40+
/// data: snappyFramed(rlp(Account))
41+
/// Account = { address_hash, AccountState, raw_bytecode, storage_entry_count }
42+
/// AccountState = { nonce, balance, storage_root, code_hash }
43+
pub const COMPRESSED_ACCOUNT: u16 = 0x0800;
44+
45+
/// CompressedStorage
46+
///
47+
/// data: snappyFramed(rlp(Vec<StorageItem>))
48+
/// StorageItem = { storage_index_hash, value }
49+
pub const COMPRESSED_STORAGE: u16 = 0x0900;
50+
51+
/// BlockIndex
52+
///
53+
/// data: block-index
54+
/// block-index := starting-number | index | index | index ... | count
55+
pub const BLOCK_INDEX: u16 = 0x6232;
56+
57+
/// Version
58+
pub const VERSION: u16 = 0x6532;
59+
60+
/// SlotIndex
61+
///
62+
/// data: starting-slot | index | index | index ... | count
63+
pub const SLOT_INDEX: u16 = 0x6932;

crates/e2store/src/era.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use ethportal_api::consensus::{
99
};
1010
use ssz::Encode;
1111

12-
use crate::e2store::{
13-
memory::E2StoreMemory,
14-
types::{Entry, Header, VersionEntry},
12+
use crate::{
13+
e2store::{
14+
memory::E2StoreMemory,
15+
types::{Entry, Header, VersionEntry},
16+
},
17+
entry_types,
1518
};
1619

1720
pub const SLOTS_PER_HISTORICAL_ROOT: usize = 8192;
@@ -181,7 +184,7 @@ pub struct CompressedSignedBeaconBlock {
181184
impl CompressedSignedBeaconBlock {
182185
pub fn try_from(entry: &Entry, fork: ForkName) -> Result<Self, anyhow::Error> {
183186
ensure!(
184-
entry.header.type_ == 0x01,
187+
entry.header.type_ == entry_types::COMPRESSED_SIGNED_BEACON_BLOCK,
185188
"invalid compressed signed beacon block entry: incorrect header type"
186189
);
187190

@@ -205,8 +208,10 @@ impl TryInto<Entry> for CompressedSignedBeaconBlock {
205208
let _ = snappy_encoder.write(&ssz_encoded)?;
206209
let snappy_encoded = snappy_encoder.into_inner()?;
207210

208-
let header = 0x01;
209-
Ok(Entry::new(header, snappy_encoded))
211+
Ok(Entry::new(
212+
entry_types::COMPRESSED_SIGNED_BEACON_BLOCK,
213+
snappy_encoded,
214+
))
210215
}
211216
}
212217

@@ -218,7 +223,7 @@ pub struct CompressedBeaconState {
218223
impl CompressedBeaconState {
219224
fn try_from(entry: &Entry, fork: ForkName) -> Result<Self, anyhow::Error> {
220225
ensure!(
221-
entry.header.type_ == 0x02,
226+
entry.header.type_ == entry_types::COMPRESSED_BEACON_STATE,
222227
"invalid compressed beacon state entry: incorrect header type"
223228
);
224229

@@ -242,8 +247,10 @@ impl TryInto<Entry> for CompressedBeaconState {
242247
let _ = snappy_encoder.write(&ssz_encoded)?;
243248
let snappy_encoded = snappy_encoder.into_inner()?;
244249

245-
let header = 0x02;
246-
Ok(Entry::new(header, snappy_encoded))
250+
Ok(Entry::new(
251+
entry_types::COMPRESSED_BEACON_STATE,
252+
snappy_encoded,
253+
))
247254
}
248255
}
249256

@@ -263,7 +270,7 @@ impl TryFrom<&Entry> for SlotIndexBlockEntry {
263270

264271
fn try_from(entry: &Entry) -> Result<Self, Self::Error> {
265272
ensure!(
266-
entry.header.type_ == 0x3269,
273+
entry.header.type_ == entry_types::SLOT_INDEX,
267274
"invalid slot index entry: incorrect header type"
268275
);
269276
ensure!(
@@ -296,7 +303,7 @@ impl TryInto<Entry> for SlotIndexBlockEntry {
296303
}
297304
buf.extend_from_slice(&self.slot_index.count.to_le_bytes());
298305

299-
Ok(Entry::new(0x3269, buf))
306+
Ok(Entry::new(entry_types::SLOT_INDEX, buf))
300307
}
301308
}
302309

@@ -348,7 +355,7 @@ impl TryFrom<&Entry> for SlotIndexStateEntry {
348355

349356
fn try_from(entry: &Entry) -> Result<Self, Self::Error> {
350357
ensure!(
351-
entry.header.type_ == 0x3269,
358+
entry.header.type_ == entry_types::SLOT_INDEX,
352359
"invalid slot index entry: incorrect header type"
353360
);
354361
ensure!(
@@ -381,7 +388,7 @@ impl TryInto<Entry> for SlotIndexStateEntry {
381388
}
382389
buf.extend_from_slice(&self.slot_index.count.to_le_bytes());
383390

384-
Ok(Entry::new(0x3269, buf))
391+
Ok(Entry::new(entry_types::SLOT_INDEX, buf))
385392
}
386393
}
387394

0 commit comments

Comments
 (0)