Skip to content

Commit f92e388

Browse files
shekhirinrkrasiuk
authored andcommitted
chore(primitives): derive Compact for StageUnitCheckpoint (#3452)
1 parent bb6caea commit f92e388

File tree

1 file changed

+12
-88
lines changed

1 file changed

+12
-88
lines changed

crates/primitives/src/stage/checkpoints.rs

+12-88
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use crate::{
22
trie::{hash_builder::HashBuilderState, StoredSubNode},
33
Address, BlockNumber, B256,
44
};
5-
use bytes::{Buf, BufMut};
6-
use reth_codecs::{derive_arbitrary, main_codec, Compact};
7-
use serde::{Deserialize, Serialize};
5+
use bytes::Buf;
6+
use reth_codecs::{main_codec, Compact};
87
use std::{
98
fmt::{Display, Formatter},
109
ops::RangeInclusive,
@@ -258,8 +257,8 @@ impl Display for StageCheckpoint {
258257
// TODO(alexey): add a merkle checkpoint. Currently it's hard because [`MerkleCheckpoint`]
259258
// is not a Copy type.
260259
/// Stage-specific checkpoint metrics.
261-
#[derive_arbitrary(compact)]
262-
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
260+
#[main_codec]
261+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
263262
pub enum StageUnitCheckpoint {
264263
/// Saves the progress of AccountHashing stage.
265264
Account(AccountHashingCheckpoint),
@@ -275,42 +274,16 @@ pub enum StageUnitCheckpoint {
275274
IndexHistory(IndexHistoryCheckpoint),
276275
}
277276

278-
/// Generates:
279-
/// 1. [Compact::to_compact] and [Compact::from_compact] implementations for [StageUnitCheckpoint].
280-
/// 2. [StageCheckpoint] getter and builder methods.
277+
#[cfg(test)]
278+
impl Default for StageUnitCheckpoint {
279+
fn default() -> Self {
280+
Self::Account(AccountHashingCheckpoint::default())
281+
}
282+
}
283+
284+
/// Generates [StageCheckpoint] getter and builder methods.
281285
macro_rules! stage_unit_checkpoints {
282286
($(($index:expr,$enum_variant:tt,$checkpoint_ty:ty,#[doc = $fn_get_doc:expr]$fn_get_name:ident,#[doc = $fn_build_doc:expr]$fn_build_name:ident)),+) => {
283-
impl Compact for StageUnitCheckpoint {
284-
fn to_compact<B>(self, buf: &mut B) -> usize
285-
where
286-
B: BufMut + AsMut<[u8]>,
287-
{
288-
match self {
289-
$(
290-
StageUnitCheckpoint::$enum_variant(data) => {
291-
buf.put_u8($index);
292-
1 + data.to_compact(buf)
293-
}
294-
)+
295-
}
296-
}
297-
298-
fn from_compact(buf: &[u8], _len: usize) -> (Self, &[u8])
299-
where
300-
Self: Sized,
301-
{
302-
match buf[0] {
303-
$(
304-
$index => {
305-
let (data, buf) = <$checkpoint_ty>::from_compact(&buf[1..], buf.len() - 1);
306-
(Self::$enum_variant(data), buf)
307-
}
308-
)+
309-
_ => unreachable!("Junk data in database: unknown StageUnitCheckpoint variant"),
310-
}
311-
}
312-
}
313-
314287
impl StageCheckpoint {
315288
$(
316289
#[doc = $fn_get_doc]
@@ -416,53 +389,4 @@ mod tests {
416389
let (decoded, _) = MerkleCheckpoint::from_compact(&buf, encoded);
417390
assert_eq!(decoded, checkpoint);
418391
}
419-
420-
#[test]
421-
fn stage_unit_checkpoint_roundtrip() {
422-
let mut rng = rand::thread_rng();
423-
let checkpoints = vec![
424-
StageUnitCheckpoint::Account(AccountHashingCheckpoint {
425-
address: Some(rng.gen()),
426-
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
427-
progress: EntitiesCheckpoint {
428-
processed: rng.gen::<u32>() as u64,
429-
total: u32::MAX as u64 + rng.gen::<u64>(),
430-
},
431-
}),
432-
StageUnitCheckpoint::Storage(StorageHashingCheckpoint {
433-
address: Some(rng.gen()),
434-
storage: Some(rng.gen()),
435-
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
436-
progress: EntitiesCheckpoint {
437-
processed: rng.gen::<u32>() as u64,
438-
total: u32::MAX as u64 + rng.gen::<u64>(),
439-
},
440-
}),
441-
StageUnitCheckpoint::Entities(EntitiesCheckpoint {
442-
processed: rng.gen::<u32>() as u64,
443-
total: u32::MAX as u64 + rng.gen::<u64>(),
444-
}),
445-
StageUnitCheckpoint::Execution(ExecutionCheckpoint {
446-
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
447-
progress: EntitiesCheckpoint {
448-
processed: rng.gen::<u32>() as u64,
449-
total: u32::MAX as u64 + rng.gen::<u64>(),
450-
},
451-
}),
452-
StageUnitCheckpoint::Headers(HeadersCheckpoint {
453-
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
454-
progress: EntitiesCheckpoint {
455-
processed: rng.gen::<u32>() as u64,
456-
total: u32::MAX as u64 + rng.gen::<u64>(),
457-
},
458-
}),
459-
];
460-
461-
for checkpoint in checkpoints {
462-
let mut buf = Vec::new();
463-
let encoded = checkpoint.to_compact(&mut buf);
464-
let (decoded, _) = StageUnitCheckpoint::from_compact(&buf, encoded);
465-
assert_eq!(decoded, checkpoint);
466-
}
467-
}
468392
}

0 commit comments

Comments
 (0)