Skip to content

Commit

Permalink
Merge branch 'bat/feat/list-and-offer-snapshots' (#3386)
Browse files Browse the repository at this point in the history
* origin/bat/feat/list-and-offer-snapshots:
  rebasing
  tinies
  Added changelog
  • Loading branch information
brentstone committed Jul 3, 2024
2 parents 8a14909 + f79afd1 commit adf3879
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Addresses the third point and part of the fourth point of Issue
[\#3307](https://github.com/anoma/namada/issues/3307)
* Adds chunking logic to snapshots
* Implements the `ListSnapshots` ABCI call
* Implements the `LoadSnapshotChunk` ABCI call

([\#3386](https://github.com/anoma/namada/pull/3386))
8 changes: 4 additions & 4 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ impl Shell {
Ok(Response::CheckTx(self.mempool_validate(&tx.tx, r#type)))
}
Request::ListSnapshots => {
Ok(Response::ListSnapshots(Default::default()))
self.list_snapshots().map(Response::ListSnapshots)
}
Request::OfferSnapshot(_) => {
Ok(Response::OfferSnapshot(Default::default()))
}
Request::LoadSnapshotChunk(_) => {
Ok(Response::LoadSnapshotChunk(Default::default()))
}
Request::LoadSnapshotChunk(req) => self
.load_snapshot_chunk(req)
.map(Response::LoadSnapshotChunk),
Request::ApplySnapshotChunk(_) => {
Ok(Response::ApplySnapshotChunk(Default::default()))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/node/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod prepare_proposal;
use namada::state::State;
pub mod process_proposal;
pub(super) mod queries;
mod snapshots;
mod stats;
#[cfg(any(test, feature = "testing"))]
#[allow(dead_code)]
Expand Down Expand Up @@ -122,6 +123,8 @@ pub enum Error {
Storage(#[from] namada::state::StorageError),
#[error("Transaction replay attempt: {0}")]
ReplayAttempt(String),
#[error("Error with snapshots: {0}")]
Snapshot(std::io::Error),
}

impl From<Error> for TxResult {
Expand Down
59 changes: 59 additions & 0 deletions crates/node/src/shell/snapshots.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use borsh_ext::BorshSerializeExt;
use namada::hash::{Hash, Sha256Hasher};
use namada::state::BlockHeight;

use super::{Error, Result};
use crate::facade::tendermint::abci::types::Snapshot;
use crate::facade::tendermint::v0_37::abci::{
request as tm_request, response as tm_response,
};
use crate::shell::Shell;
use crate::storage;
use crate::storage::{DbSnapshot, SnapshotMetadata};

impl Shell<storage::PersistentDB, Sha256Hasher> {
/// List the snapshot files held locally. Furthermore, the number
/// of chunks, as hash of each chunk, and a hash of the chunk
/// metadata are provided so that syncing nodes can verify can verify
/// snapshots they receive.
pub fn list_snapshots(&self) -> Result<tm_response::ListSnapshots> {
if self.blocks_between_snapshots.is_none() {
Ok(Default::default())
} else {
let snapshots = DbSnapshot::files(&self.base_dir)
.map_err(Error::Snapshot)?
.into_iter()
.map(|SnapshotMetadata { height, chunks, .. }| {
let hash = Hash::sha256(chunks.serialize_to_vec()).0;
Snapshot {
height: u32::try_from(height.0).unwrap().into(),
format: 0,
#[allow(clippy::cast_possible_truncation)]
chunks: chunks.len() as u32,
hash: hash.into_iter().collect(),
metadata: Default::default(),
}
})
.collect();

Ok(tm_response::ListSnapshots { snapshots })
}
}

/// Load the bytes of a requested chunk and return them
/// to cometbft.
pub fn load_snapshot_chunk(
&self,
req: tm_request::LoadSnapshotChunk,
) -> Result<tm_response::LoadSnapshotChunk> {
let chunk = DbSnapshot::load_chunk(
BlockHeight(req.height.into()),
u64::from(req.chunk),
&self.base_dir,
)
.map_err(Error::Snapshot)?;
Ok(tm_response::LoadSnapshotChunk {
chunk: chunk.into_iter().collect(),
})
}
}
4 changes: 1 addition & 3 deletions crates/node/src/shims/abcipp_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ impl AbcippShim {
.expect("Last block should exists")
.height;
let cfs = db.column_families();
let path = DbSnapshot::path(last_height, base_dir.clone());

snapshot.write_to_file(cfs, &path)?;
snapshot.write_to_file(cfs, base_dir.clone(), last_height)?;
DbSnapshot::cleanup(last_height, &base_dir)
});

Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use arse_merkle_tree::H256;
use blake2b_rs::{Blake2b, Blake2bBuilder};
use namada::state::StorageHasher;
use namada_sdk::state::FullAccessState;
pub use rocksdb::{open, DbSnapshot, RocksDBUpdateVisitor};
pub use rocksdb::{open, DbSnapshot, RocksDBUpdateVisitor, SnapshotMetadata};

#[derive(Default)]
pub struct PersistentStorageHasher(Blake2bHasher);
Expand Down
Loading

0 comments on commit adf3879

Please sign in to comment.