Skip to content

Commit

Permalink
feat(primitives, storage): save prune checkpoints in database (#3628)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Jul 11, 2023
1 parent 1763b5e commit 9412963
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub use net::{
SEPOLIA_BOOTNODES,
};
pub use peer::{PeerId, WithPeerId};
pub use prune::{PruneCheckpoint, PruneMode, PruneTargets};
pub use prune::{PruneCheckpoint, PruneMode, PrunePart, PruneTargets};
pub use receipt::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef};
pub use revm_primitives::JumpMap;
pub use serde_helper::JsonU256;
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/prune/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod checkpoint;
mod mode;
mod part;
mod target;

pub use checkpoint::PruneCheckpoint;
pub use mode::PruneMode;
pub use part::PrunePart;
pub use target::PruneTargets;
2 changes: 1 addition & 1 deletion crates/primitives/src/prune/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum PruneMode {
#[cfg(test)]
impl Default for PruneMode {
fn default() -> Self {
Self::Distance(0)
Self::Full
}
}

Expand Down
24 changes: 24 additions & 0 deletions crates/primitives/src/prune/part.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use reth_codecs::{main_codec, Compact};

/// Part of the data that can be pruned.
#[main_codec]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum PrunePart {
/// Prune part responsible for the `TxSenders` table.
SenderRecovery,
/// Prune part responsible for the `TxHashNumber` table.
TransactionLookup,
/// Prune part responsible for the `Receipts` table.
Receipts,
/// Prune part responsible for the `AccountChangeSet` and `AccountHistory` tables.
AccountHistory,
/// Prune part responsible for the `StorageChangeSet` and `StorageHistory` tables.
StorageHistory,
}

#[cfg(test)]
impl Default for PrunePart {
fn default() -> Self {
Self::SenderRecovery
}
}
2 changes: 1 addition & 1 deletion crates/storage/db/src/abstraction/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait Encode: Send + Sync + Sized + Debug {
/// Trait that will transform the data to be read from the DB.
pub trait Decode: Send + Sync + Sized + Debug {
/// Decodes data coming from the database.
fn decode<B: AsRef<[u8]>>(key: B) -> Result<Self, DatabaseError>;
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError>;
}

/// Generic trait that enforces the database key to implement [`Encode`] and [`Decode`].
Expand Down
9 changes: 7 additions & 2 deletions crates/storage/db/src/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use crate::{
use reth_primitives::{
stage::StageCheckpoint,
trie::{BranchNodeCompact, StorageTrieEntry, StoredNibbles, StoredNibblesSubKey},
Account, Address, BlockHash, BlockNumber, Bytecode, Header, IntegerList, Receipt, StorageEntry,
TransactionSignedNoHash, TxHash, TxNumber, H256,
Account, Address, BlockHash, BlockNumber, Bytecode, Header, IntegerList, PruneCheckpoint,
PrunePart, Receipt, StorageEntry, TransactionSignedNoHash, TxHash, TxNumber, H256,
};

/// Enum for the types of tables present in libmdbx.
Expand Down Expand Up @@ -415,6 +415,11 @@ table!(
( SyncStageProgress ) StageId | Vec<u8>
);

table!(
/// Stores the highest pruned block number and prune mode of each prune part.
( PruneParts ) PrunePart | PruneCheckpoint
);

/// Alias Types

/// List with transaction numbers.
Expand Down
19 changes: 18 additions & 1 deletion crates/storage/db/src/tables/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use reth_codecs::Compact;
use reth_primitives::{
trie::{StoredNibbles, StoredNibblesSubKey},
Address, H256,
Address, PrunePart, H256,
};

pub mod accounts;
Expand Down Expand Up @@ -135,3 +135,20 @@ impl Decode for StoredNibblesSubKey {
Ok(Self::from_compact(buf, buf.len()).0)
}
}

impl Encode for PrunePart {
type Encoded = [u8; 1];

fn encode(self) -> Self::Encoded {
let mut buf = [0u8];
self.to_compact(&mut buf.as_mut());
buf
}
}

impl Decode for PrunePart {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
let buf = value.as_ref();
Ok(Self::from_compact(buf, buf.len()).0)
}
}

0 comments on commit 9412963

Please sign in to comment.