From 5637784a73fb692bf5e87fc470ead20080822710 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Wed, 8 Jan 2025 14:24:09 +0200 Subject: [PATCH 1/2] feat: delete old blocks (#231) Description --- delete old blocks from lmdb fix unit tests fix clippy --- src/server/grpc/p2pool.rs | 3 - src/server/p2p/network.rs | 5 -- src/server/p2p/setup.rs | 1 - src/sharechain/in_memory.rs | 89 ++++++++-------------------- src/sharechain/lmdb_block_storage.rs | 43 ++++++++++++-- src/sharechain/p2chain.rs | 56 +++++++++-------- src/sharechain/p2chain_level.rs | 9 +++ 7 files changed, 101 insertions(+), 105 deletions(-) diff --git a/src/server/grpc/p2pool.rs b/src/server/grpc/p2pool.rs index bf10b65..b34b00e 100644 --- a/src/server/grpc/p2pool.rs +++ b/src/server/grpc/p2pool.rs @@ -235,14 +235,11 @@ where S: ShareChain .await .map_err(|error| Status::internal(format!("failed to get new tip block {error:?}")))?) .clone(); - // dbg!(&new_tip_block.height, &new_tip_block.hash); let shares = share_chain .generate_shares(&new_tip_block, !synced_status) .await .map_err(|error| Status::internal(format!("failed to generate shares {error:?}")))?; - // dbg!(&shares); - let mut response = self .client .write() diff --git a/src/server/p2p/network.rs b/src/server/p2p/network.rs index bef6860..b846695 100644 --- a/src/server/p2p/network.rs +++ b/src/server/p2p/network.rs @@ -24,7 +24,6 @@ use hickory_resolver::{ use itertools::Itertools; use libp2p::{ autonat::{self, NatStatus, OutboundProbeEvent}, - connection_limits::{self}, dcutr, futures::StreamExt, gossipsub::{self, IdentTopic, Message, MessageAcceptance, PublishError}, @@ -1975,10 +1974,8 @@ where S: ShareChain let mut lock = self.relay_store.write().await; // TODO: Do relays expire? // if lock.has_active_relay() { - // // dbg!("Already have an active relay"); // return; // } - // dbg!("No, select a relay"); lock.select_random_relay(); if let Some(relay) = lock.selected_relay_mut() { let mut addresses = relay.addresses.clone(); @@ -2268,7 +2265,6 @@ where S: ShareChain let mut peers_to_dial = vec![]; for record in store_write_lock.best_peers_to_dial(100) { - // dbg!(&record.peer_id); // Only dial seed peers if we have 0 connections if !self.swarm.is_connected(&record.peer_id) && !store_write_lock.is_seed_peer(&record.peer_id) { @@ -2299,7 +2295,6 @@ where S: ShareChain } drop(store_write_lock); // for peer in peers_to_dial { - // dbg!("trying"); // self.initiate_direct_peer_exchange(&peer).await; // } } diff --git a/src/server/p2p/setup.rs b/src/server/p2p/setup.rs index 4b92a89..bc97022 100644 --- a/src/server/p2p/setup.rs +++ b/src/server/p2p/setup.rs @@ -8,7 +8,6 @@ use blake2::Blake2b; use digest::{consts::U32, generic_array::GenericArray, Digest}; use libp2p::{ autonat::{self}, - connection_limits::{self, ConnectionLimits}, dcutr, gossipsub::{self, Message, MessageId}, identify, diff --git a/src/sharechain/in_memory.rs b/src/sharechain/in_memory.rs index d734fcc..167b536 100644 --- a/src/sharechain/in_memory.rs +++ b/src/sharechain/in_memory.rs @@ -89,7 +89,7 @@ impl InMemoryShareChain { info!(target: LOG_TARGET, "Found old block cache file, renaming from {:?} to {:?}", data_path.as_path(), &bkp_file); // First remove the old backup file - let _ = fs::remove_dir_all(bkp_file.as_path()) + let _unused = fs::remove_dir_all(bkp_file.as_path()) .inspect_err(|e| error!(target: LOG_TARGET, "Could not remove old block cache file:{:?}", e)); fs::create_dir_all(bkp_file.parent().unwrap()) .map_err(|e| anyhow::anyhow!("Could not create block cache backup directory:{:?}", e))?; @@ -313,7 +313,6 @@ impl InMemoryShareChain { } } let mut miners_to_shares = HashMap::new(); - let tip_level = match p2_chain.get_tip() { Some(tip_level) => tip_level, None => return Ok(miners_to_shares), @@ -876,16 +875,28 @@ pub mod test { let consensus_manager = ConsensusManager::builder(Network::LocalNet).build().unwrap(); let coinbase_extras = Arc::new(RwLock::new(HashMap::>::new())); let (stats_tx, _) = tokio::sync::broadcast::channel(1000); - let stats_broadcast_client = StatsBroadcastClient::new(stats_tx); - InMemoryShareChain::new( - Config::default(), - PowAlgorithm::Sha3x, - None, + let stat_client = StatsBroadcastClient::new(stats_tx); + let config = Config::default(); + let pow_algo = PowAlgorithm::Sha3x; + + let block_cache = LmdbBlockStorage::new_from_temp_dir(); + let p2chain = P2Chain::new_empty( + pow_algo, + config.share_window * 2, + config.share_window, + config.block_time, + block_cache, + ); + + InMemoryShareChain { + p2_chain: Arc::new(RwLock::new(p2chain)), + pow_algo, + block_validation_params: None, consensus_manager, coinbase_extras, - stats_broadcast_client, - ) - .unwrap() + stat_client, + config, + } } pub fn new_random_address() -> TariAddress { @@ -897,19 +908,7 @@ pub mod test { #[tokio::test] async fn equal_shares() { - let consensus_manager = ConsensusManager::builder(Network::LocalNet).build().unwrap(); - let coinbase_extras = Arc::new(RwLock::new(HashMap::>::new())); - let (stats_tx, _) = tokio::sync::broadcast::channel(1000); - let stats_broadcast_client = StatsBroadcastClient::new(stats_tx); - let share_chain = InMemoryShareChain::new( - Config::default(), - PowAlgorithm::Sha3x, - None, - consensus_manager, - coinbase_extras, - stats_broadcast_client, - ) - .unwrap(); + let share_chain = new_chain(); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -929,7 +928,6 @@ pub mod test { .unwrap(); prev_block = Some(block.clone()); - share_chain.submit_block(block).await.unwrap(); } @@ -946,20 +944,8 @@ pub mod test { #[tokio::test] async fn equal_share_same_participants() { - let consensus_manager = ConsensusManager::builder(Network::LocalNet).build().unwrap(); - let coinbase_extras = Arc::new(RwLock::new(HashMap::>::new())); - let (stats_tx, _) = tokio::sync::broadcast::channel(1000); let static_coinbase_extra = Vec::new(); - let stats_broadcast_client = StatsBroadcastClient::new(stats_tx); - let share_chain = InMemoryShareChain::new( - Config::default(), - PowAlgorithm::Sha3x, - None, - consensus_manager, - coinbase_extras, - stats_broadcast_client, - ) - .unwrap(); + let share_chain = new_chain(); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1000,20 +986,8 @@ pub mod test { #[tokio::test] async fn equal_share_same_participants_with_uncles() { - let consensus_manager = ConsensusManager::builder(Network::LocalNet).build().unwrap(); - let coinbase_extras = Arc::new(RwLock::new(HashMap::>::new())); - let (stats_tx, _) = tokio::sync::broadcast::channel(1000); - let stats_broadcast_client = StatsBroadcastClient::new(stats_tx); let static_coinbase_extra = Vec::new(); - let share_chain = InMemoryShareChain::new( - Config::default(), - PowAlgorithm::Sha3x, - None, - consensus_manager, - coinbase_extras, - stats_broadcast_client, - ) - .unwrap(); + let share_chain = new_chain(); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1160,20 +1134,7 @@ pub mod test { #[tokio::test] async fn chain_start() { - let consensus_manager = ConsensusManager::builder(Network::LocalNet).build().unwrap(); - let coinbase_extras = Arc::new(RwLock::new(HashMap::>::new())); - let (stats_tx, _) = tokio::sync::broadcast::channel(1000); - let stats_broadcast_client = StatsBroadcastClient::new(stats_tx); - let share_chain = InMemoryShareChain::new( - Config::default(), - PowAlgorithm::Sha3x, - None, - consensus_manager, - coinbase_extras, - stats_broadcast_client, - ) - .unwrap(); - + let share_chain = new_chain(); let static_coinbase_extra = Vec::new(); let mut new_tip = share_chain .generate_new_tip_block(&new_random_address(), static_coinbase_extra.clone()) diff --git a/src/sharechain/lmdb_block_storage.rs b/src/sharechain/lmdb_block_storage.rs index 6455341..c12a45e 100644 --- a/src/sharechain/lmdb_block_storage.rs +++ b/src/sharechain/lmdb_block_storage.rs @@ -28,7 +28,7 @@ use std::{ }; use anyhow::{anyhow, Error}; -use log::info; +use log::{error, info}; use rkv::{ backend::{BackendInfo, Lmdb, LmdbEnvironment}, Manager, @@ -50,8 +50,14 @@ pub(crate) struct LmdbBlockStorage { impl LmdbBlockStorage { #[cfg(test)] pub fn new_from_temp_dir() -> Self { + use rand::{distributions::Alphanumeric, Rng}; use tempfile::Builder; - let root = Builder::new().prefix("p2pool").tempdir().unwrap(); + let instance: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(7) + .map(char::from) + .collect(); + let root = Builder::new().prefix("p2pool").suffix(&instance).tempdir().unwrap(); fs::create_dir_all(root.path()).unwrap(); let path = root.path(); let mut manager = Manager::::singleton().write().unwrap(); @@ -93,6 +99,16 @@ impl BlockCache for LmdbBlockStorage { None } + fn delete(&self, hash: &BlockHash) { + let env = self.file_handle.read().expect("reader"); + let store = env.open_single("block_cache", StoreOptions::create()).unwrap(); + let mut writer = env.write().expect("writer"); + store.delete(&mut writer, hash.as_bytes()).unwrap(); + if let Err(e) = writer.commit() { + error!(target: LOG_TARGET, "Error deleting block from lmdb: {:?}", e); + } + } + fn insert(&self, hash: BlockHash, block: Arc) { // Retry if the map is full // This weird pattern of setting a bool is so that the env is closed before resizing, otherwise @@ -106,7 +122,6 @@ impl BlockCache for LmdbBlockStorage { // next_resize = false; } let store = env.open_single("block_cache", StoreOptions::create()).unwrap(); - // dbg!(_retry); let mut writer = env.write().expect("writer"); let block_blob = serialize_message(&block).unwrap(); match store.put(&mut writer, hash.as_bytes(), &rkv::Value::Blob(&block_blob)) { @@ -159,13 +174,13 @@ impl BlockCache for LmdbBlockStorage { fn resize_db(env: &Rkv) { let size = env.info().map(|i| i.map_size()).unwrap_or(0); - // dbg!(size); // let new_size = (size as f64 * 1.2f64).ceil() as usize; let new_size = size * 2; env.set_map_size(new_size).unwrap(); } pub trait BlockCache { fn get(&self, hash: &BlockHash) -> Option>; + fn delete(&self, hash: &BlockHash); fn insert(&self, hash: BlockHash, block: Arc); fn all_blocks(&self) -> Result>, Error>; } @@ -193,12 +208,16 @@ pub mod test { self.blocks.read().unwrap().get(hash).cloned() } + fn delete(&self, hash: &BlockHash) { + self.blocks.write().unwrap().remove(hash); + } + fn insert(&self, hash: BlockHash, block: Arc) { self.blocks.write().unwrap().insert(hash, block); } - fn all_blocks(&self) -> Vec> { - self.blocks.read().unwrap().values().cloned().collect() + fn all_blocks(&self) -> Result>, Error> { + Ok(self.blocks.read().unwrap().values().cloned().collect()) } } @@ -211,4 +230,16 @@ pub mod test { let retrieved_block = cache.get(&hash).unwrap(); assert_eq!(block, retrieved_block); } + + #[test] + fn test_deleting_blocks() { + let cache = LmdbBlockStorage::new_from_temp_dir(); + let block = Arc::new(P2Block::default()); + let hash = block.hash; + cache.insert(hash, block.clone()); + let retrieved_block = cache.get(&hash).unwrap(); + assert_eq!(block, retrieved_block); + cache.delete(&hash); + assert!(cache.get(&hash).is_none()); + } } diff --git a/src/sharechain/p2chain.rs b/src/sharechain/p2chain.rs index 95e5e48..40c66bd 100644 --- a/src/sharechain/p2chain.rs +++ b/src/sharechain/p2chain.rs @@ -172,7 +172,7 @@ impl P2Chain { continue; } info!(target: LOG_TARGET, "Loading block {}({:x}{:x}{:x}{:x}) into chain", block.height, block.hash[0], block.hash[1], block.hash[2], block.hash[3]); - let _ = new_chain.add_block_to_chain(block).inspect_err(|e| { + let _unused = new_chain.add_block_to_chain(block).inspect_err(|e| { error!(target: LOG_TARGET, "Failed to load block into chain: {}", e); }); } @@ -227,7 +227,11 @@ impl P2Chain { // let see if we are the limit for the current chain let mut keys: Vec = self.levels.keys().copied().sorted().collect(); while current_chain_length > self.total_size + SAFETY_MARGIN + MAX_EXTRA_SYNC { - self.levels.remove(&keys[0]); + if let Some(level) = self.levels.remove(&keys[0]) { + for block in level.all_headers() { + self.block_cache.delete(&block.hash); + } + } keys.remove(0); current_chain_length = self.levels.len() as u64; } @@ -730,7 +734,7 @@ mod test { #[test] fn test_only_keeps_size() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); let mut prev_block = None; for i in 0..2100 { @@ -758,7 +762,7 @@ mod test { #[test] fn get_tips() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -786,7 +790,7 @@ mod test { fn test_does_not_set_tip_unless_full_chain() { // we have a window of 5, meaing that we need 5 valid blocks // if we dont start at 0, we need a chain of at least 6 blocks - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -832,7 +836,7 @@ mod test { // to test this properly we need 6 blocks in the chain, and not use 0 as zero will always be valid and counter // as chain start block height 2 will only be valid if it has parents aka block 1, so we need share // window + 1 blocks in chain-- - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -875,7 +879,7 @@ mod test { // to test this properly we need 6 blocks in the chain, and not use 0 as zero will always be valid and counter // as chain start block height 2 will only be valid if it has parents aka block 1, so we need share // window + 1 blocks in chain-- - let mut chain = P2Chain::new_empty(20, 10, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 20, 10, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -916,7 +920,7 @@ mod test { // to test this properly we need 6 blocks in the chain, and not use 0 as zero will always be valid and counter // as chain start block height 2 will only be valid if it has parents aka block 1, so we need share // window + 1 blocks in chain-- - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -983,7 +987,7 @@ mod test { #[test] fn get_parent() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1017,7 +1021,7 @@ mod test { #[test] fn test_dont_set_tip_on_single_high_height() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1098,7 +1102,7 @@ mod test { #[test] fn add_blocks_to_chain_happy_path() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1129,7 +1133,7 @@ mod test { #[test] fn add_blocks_to_chain_small_reorg() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1225,7 +1229,7 @@ mod test { #[test] fn add_blocks_to_chain_super_large_reorg() { // this test will verify that we reorg to a completely new chain - let mut chain = P2Chain::new_empty(10, 5, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1286,7 +1290,7 @@ mod test { #[test] fn add_blocks_missing_block() { // this test will verify that we reorg to a completely new chain - let mut chain = P2Chain::new_empty(50, 25, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 50, 25, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1325,7 +1329,7 @@ mod test { #[test] fn reorg_with_missing_uncle() { // this test will verify that we reorg to a completely new chain - let mut chain = P2Chain::new_empty(50, 25, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 50, 25, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1411,7 +1415,7 @@ mod test { #[test] fn add_blocks_to_chain_super_large_reorg_only_window() { // this test will verify that we reorg to a completely new chain - let mut chain = P2Chain::new_empty(10, 5, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1475,7 +1479,7 @@ mod test { #[test] fn calculate_total_difficulty_correctly() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1504,7 +1508,7 @@ mod test { #[test] fn calculate_total_difficulty_correctly_with_uncles() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1556,7 +1560,7 @@ mod test { #[test] fn calculate_total_difficulty_correctly_with_wrapping_blocks() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1608,7 +1612,7 @@ mod test { #[test] fn reorg_with_uncles() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut timestamp = EpochTime::now(); let mut prev_block = None; @@ -1716,7 +1720,7 @@ mod test { #[test] fn rerog_less_than_share_window() { - let mut chain = P2Chain::new_empty(20, 15, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 20, 15, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1778,7 +1782,7 @@ mod test { #[test] fn rests_levels_after_reorg() { - let mut chain = P2Chain::new_empty(20, 15, 20, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 20, 15, 20, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1836,7 +1840,7 @@ mod test { #[test] fn difficulty_go_up() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1875,7 +1879,7 @@ mod test { } #[test] fn difficulty_go_down() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let mut prev_block = None; let mut tari_block = Block::new(BlockHeader::new(0), AggregateBody::empty()); @@ -1918,7 +1922,7 @@ mod test { // This test adds a block to the tip, and then adds second block, // but has an uncle that is not in the chain. This test checks that // the tip is not set to the new block, because the uncle is missing. - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let prev_block = None; @@ -1951,7 +1955,7 @@ mod test { #[test] fn test_only_reorg_to_chain_if_it_is_verified() { - let mut chain = P2Chain::new_empty(10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); + let mut chain = P2Chain::new_empty(PowAlgorithm::Sha3x, 10, 5, 10, LmdbBlockStorage::new_from_temp_dir()); let prev_block = None; let block = P2BlockBuilder::new(prev_block.as_ref()) diff --git a/src/sharechain/p2chain_level.rs b/src/sharechain/p2chain_level.rs index f7dee17..23a0bbe 100644 --- a/src/sharechain/p2chain_level.rs +++ b/src/sharechain/p2chain_level.rs @@ -175,6 +175,15 @@ impl P2ChainLevel { .filter_map(|hash| self.block_cache.get(hash)) .collect() } + + pub fn all_headers(&self) -> Vec { + self.block_headers + .read() + .expect("could not lock") + .values() + .cloned() + .collect() + } } #[cfg(test)] From 0cece29a0af16c064478d4b374c82f479ebac492 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Thu, 9 Jan 2025 09:54:47 +0200 Subject: [PATCH 2/2] feat!: optimise new tip notify (#224) Description --- Optimize new tip notify message by removing redundant u128 --- src/main.rs | 1 - src/server/grpc/p2pool.rs | 3 +-- src/server/p2p/messages.rs | 14 +++++++++----- src/server/p2p/network.rs | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91d9b0b..7f37862 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use std::{ fs::File, io::Write, panic, - process, time::{SystemTime, UNIX_EPOCH}, }; diff --git a/src/server/grpc/p2pool.rs b/src/server/grpc/p2pool.rs index b34b00e..a458134 100644 --- a/src/server/grpc/p2pool.rs +++ b/src/server/grpc/p2pool.rs @@ -162,8 +162,7 @@ where S: ShareChain .collect(); new_blocks.append(&mut uncles); if new_tip.new_tip.is_some() { - let total_pow = share_chain.get_total_chain_pow().await; - let notify = NotifyNewTipBlock::new(self.local_peer_id, new_blocks, total_pow); + let notify = NotifyNewTipBlock::new(self.local_peer_id, new_blocks); let res = self .p2p_client .broadcast_block(notify) diff --git a/src/server/p2p/messages.rs b/src/server/p2p/messages.rs index 1729de7..c20f5b7 100644 --- a/src/server/p2p/messages.rs +++ b/src/server/p2p/messages.rs @@ -229,7 +229,6 @@ pub struct NotifyNewTipBlock { pub version: u64, peer_id: PeerId, pub new_blocks: Vec, - pub total_accumulated_difficulty: u128, pub timestamp: u64, } @@ -238,7 +237,6 @@ impl Display for NotifyNewTipBlock { writeln!(f, "--------- new tip notify Block -----------------")?; writeln!(f, "version: {}", self.version)?; writeln!(f, "from: {}", self.peer_id.to_base58())?; - writeln!(f, "total_accumulated_difficulty: {}", self.total_accumulated_difficulty)?; writeln!(f, "timestamp: {}", self.timestamp)?; writeln!(f, "--------- p2pblocks -----------------")?; for block in &self.new_blocks { @@ -251,14 +249,20 @@ impl Display for NotifyNewTipBlock { impl_conversions!(NotifyNewTipBlock); impl NotifyNewTipBlock { - pub fn new(peer_id: PeerId, new_blocks: Vec, total_acculumted_difficulty: AccumulatedDifficulty) -> Self { - let total_acculumted_difficulty = total_acculumted_difficulty.as_u128(); + pub fn total_proof_of_work(&self) -> AccumulatedDifficulty { + let max_block = self.new_blocks.iter().max_by_key(|x| x.height); + match max_block { + Some(block) => block.total_pow(), + None => AccumulatedDifficulty::min(), + } + } + + pub fn new(peer_id: PeerId, new_blocks: Vec) -> Self { let timestamp = EpochTime::now().as_u64(); Self { version: PROTOCOL_VERSION, peer_id, new_blocks, - total_accumulated_difficulty: total_acculumted_difficulty, timestamp, } } diff --git a/src/server/p2p/network.rs b/src/server/p2p/network.rs index b846695..a49ef19 100644 --- a/src/server/p2p/network.rs +++ b/src/server/p2p/network.rs @@ -636,7 +636,7 @@ where S: ShareChain let our_tip = share_chain.tip_height().await.unwrap_or(0); let our_pow = share_chain.get_total_chain_pow().await; - if payload.total_accumulated_difficulty < our_pow.as_u128() && + if payload.total_proof_of_work() < our_pow && payload .new_blocks .iter()