Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/floresta-chain/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ mod tests {
impl BlockchainInterface for MockBlockchainInterface {
type Error = MockBlockchainError;

fn size_on_disk(&self) -> Result<u64, Self::Error> {
unimplemented!("MockBlockchainInterface has no on-disk presence")
}

fn get_block_header(&self, hash: &BlockHash) -> Result<Header, Self::Error> {
self.headers
.get(hash)
Expand Down
4 changes: 4 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,10 @@ impl<PersistedState: ChainStore> BlockchainInterface for ChainState<PersistedSta
read_lock!(self).acc.to_owned()
}

fn size_on_disk(&self) -> Result<u64, Self::Error> {
Ok(read_lock!(self).chainstore.size_on_disk()?)
}

fn get_fork_point(&self, block: BlockHash) -> Result<BlockHash, Self::Error> {
let fork_point = self.find_fork_point(&self.get_block_header(&block)?)?;
Ok(fork_point.block_hash())
Expand Down
7 changes: 7 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/chainstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ pub trait ChainStore {
/// If you're using a database that already checks for integrity by itself,
/// this can safely be a no-op.
fn check_integrity(&self) -> Result<(), Self::Error>;

/// Returns the total size on disk, in bytes, of the data this store persists.
///
/// The returned value is the sum of the sizes of each backing file managed by
/// this store. Implementations should not include the size of any enclosing
/// directory or unrelated data.
fn size_on_disk(&self) -> Result<u64, Self::Error>;
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
86 changes: 86 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/flat_chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,16 @@ impl ChainStore for FlatChainStore {
unsafe { self.do_flush() }
}

fn size_on_disk(&self) -> Result<u64, Self::Error> {
let headers_size = self.headers.len() as u64;
let metadata_size = self.metadata.len() as u64;
let block_index_size = self.block_index.index_map.len() as u64;
let fork_headers_size = self.fork_headers.len() as u64;
let accumulator_size = self.accumulator_file.metadata()?.len();

Ok(headers_size + metadata_size + block_index_size + fork_headers_size + accumulator_size)
}

fn save_roots_for_block(&mut self, roots: Vec<u8>, height: u32) -> Result<(), Self::Error> {
let index = Index::new(height)?;

Expand Down Expand Up @@ -1381,6 +1391,7 @@ mod tests {
use super::FlatChainStore;
use super::FlatChainStoreConfig;
use super::FlatChainstoreError;
use super::HashedDiskHeader;
use super::Index;
use super::FLAT_CHAINSTORE_MAGIC;
use super::FLAT_CHAINSTORE_VERSION;
Expand Down Expand Up @@ -1675,6 +1686,81 @@ mod tests {
}
}

fn make_sized_store(
block_index_size: usize,
headers_file_size: usize,
fork_file_size: usize,
) -> FlatChainStore {
let test_id = rand::random::<u64>();
let config = FlatChainStoreConfig {
block_index_size: Some(block_index_size),
headers_file_size: Some(headers_file_size),
fork_file_size: Some(fork_file_size),
cache_size: Some(10),
file_permission: Some(0o660),
path: format!("./tmp-db/{test_id}/").into(),
};
FlatChainStore::new(config).unwrap()
}

#[test]
fn test_size_on_disk() {
// Baseline matches the formula in `size_on_disk`.
let mut store = make_sized_store(32_768, 32_768, 16_384);
let baseline = (32_768 * size_of::<u32>()
+ 32_768 * size_of::<HashedDiskHeader>()
+ 16_384 * size_of::<HashedDiskHeader>()
+ size_of::<Metadata>()) as u64;
assert_eq!(store.size_on_disk().unwrap(), baseline);

// Vary each preallocated field; check the per-field delta so any
// dropped or miscounted summand in `size_on_disk` fails here.
let bigger_index = make_sized_store(65_536, 32_768, 16_384);
assert_eq!(
bigger_index.size_on_disk().unwrap() - baseline,
(32_768 * size_of::<u32>()) as u64,
);

let bigger_headers = make_sized_store(32_768, 65_536, 16_384);
assert_eq!(
bigger_headers.size_on_disk().unwrap() - baseline,
(32_768 * size_of::<HashedDiskHeader>()) as u64,
);

let bigger_fork = make_sized_store(32_768, 32_768, 32_768);
assert_eq!(
bigger_fork.size_on_disk().unwrap() - baseline,
(16_384 * size_of::<HashedDiskHeader>()) as u64,
);

// Accumulator file is the only dynamic component; grows by payload.
let genesis = genesis_block(Network::Regtest);
store
.save_header(&DiskBlockHeader::FullyValid(genesis.header, 0))
.unwrap();
store.update_block_index(0, genesis.block_hash()).unwrap();

let acc = vec![0xab; 64];
store.save_roots_for_block(acc.clone(), 0).unwrap();
assert_eq!(store.size_on_disk().unwrap(), baseline + acc.len() as u64);

// Cumulative append at height 1.
let mut next = genesis.header;
next.prev_blockhash = genesis.block_hash();
store
.save_header(&DiskBlockHeader::FullyValid(next, 1))
.unwrap();
store.update_block_index(1, next.block_hash()).unwrap();

let acc2 = vec![0xcd; 32];
let pre_second = store.size_on_disk().unwrap();
store.save_roots_for_block(acc2.clone(), 1).unwrap();
assert_eq!(
store.size_on_disk().unwrap(),
pre_second + acc2.len() as u64,
);
}

#[test]
fn test_save_height() {
let mut store = get_test_chainstore(None).unwrap();
Expand Down
7 changes: 7 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub trait BlockchainInterface {

/// Returns the amount of [`Work`] associated with a given chain tip
fn get_work(&self, tip: BlockHash) -> Result<Work, Self::Error>;

/// Returns the total size on disk, in bytes, of the chain data persisted by this backend.
fn size_on_disk(&self) -> Result<u64, Self::Error>;
}

/// [UpdatableChainstate] is a contract that a is expected from a chainstate
Expand Down Expand Up @@ -361,6 +364,10 @@ impl<T: BlockchainInterface> BlockchainInterface for Arc<T> {
fn get_fork_point(&self, block: BlockHash) -> Result<BlockHash, Self::Error> {
T::get_fork_point(self, block)
}

fn size_on_disk(&self) -> Result<u64, Self::Error> {
T::size_on_disk(self)
}
}

/// This module defines an [UtxoData] struct, helpful for transaction validation
Expand Down
4 changes: 4 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/partial_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ impl BlockchainInterface for PartialChainState {
self.inner().current_acc.clone()
}

fn size_on_disk(&self) -> Result<u64, Self::Error> {
unimplemented!("partialChainState has no on-disk presence")
}

fn get_height(&self) -> Result<u32, Self::Error> {
Ok(self.inner().current_height)
}
Expand Down
Loading
Loading