Skip to content

Commit

Permalink
feat: change gossip of new blocks (#165)
Browse files Browse the repository at this point in the history
Description
---
Only gossip a new block if you mined it
Change gossip message to not contain the hash + height, but rather the
full header + uncles.
  • Loading branch information
SWvheerden authored Nov 19, 2024
1 parent 1a2f225 commit 84e7567
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 268 deletions.
82 changes: 41 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ libp2p = {git = "https://github.com/tari-project/rust-libp2p.git", rev = "3d918c
libp2p-peersync = {git = "https://github.com/tari-project/tari-dan.git", branch = "development"}
log = {version = "0.4.21", features = ["kv"]}
log4rs = "1.3.0"
minotari_app_grpc = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
minotari_node_grpc_client = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
minotari_app_grpc = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
minotari_node_grpc_client = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
num = {version = "0.4.3", features = ["default", "num-bigint", "serde"]}
rand = "0.8.0"
serde = "1.0.203"
serde_cbor = "0.11.2"
serde_json = "1.0.122"
tari_common = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_common_types = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_core = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_common = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
tari_common_types = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
tari_core = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
tari_crypto = "0.21.0"
tari_script = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_shutdown = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_script = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
tari_shutdown = {git = "https://github.com/tari-project/tari.git", rev = "d58c67030c3cff20eea6a17883c2a7908dc75ea6"}
tari_utilities = {version = "0.8", features = ["borsh"]}
thiserror = "1.0"
tokio = {version = "1.41.0", features = ["full"]}
Expand Down
2 changes: 1 addition & 1 deletion src/server/grpc/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ macro_rules! proxy_simple_result {
let res = match $self.client.write().await.$call($request.into_inner()).await {
Ok(resp) => Ok(resp),
Err(error) => {
error!("Error while calling {:?} on base node: {:?}", stringify!($call), error);
error!("ShareChainError while calling {:?} on base node: {:?}", stringify!($call), error);
Err(error)
},
};
Expand Down
40 changes: 22 additions & 18 deletions src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,25 @@ where S: ShareChain
info!(target: LOG_TARGET, "We are not synced yet, not submitting block atm");
return Ok(());
}
let pow_algo = block.original_block.header.pow.pow_algo;
let pow_algo = block.original_header.pow.pow_algo;
let share_chain = match pow_algo {
PowAlgorithm::RandomX => self.share_chain_random_x.clone(),
PowAlgorithm::Sha3x => self.share_chain_sha3x.clone(),
};
match share_chain.submit_block(block.clone()).await {
Ok(new_tip) => {
let _ = self.stats_broadcast.send_miner_block_accepted(pow_algo);
let mut new_blocks = vec![(block.height, block.hash)];
for uncle in block.uncles.iter() {
new_blocks.push(*uncle);
}
let mut new_blocks = vec![Arc::<P2Block>::unwrap_or_clone(block.clone())];
let mut uncles = share_chain
.get_blocks(&block.uncles)
.await
.into_iter()
.map(|block| Arc::<P2Block>::unwrap_or_clone(block))
.collect();
new_blocks.append(&mut uncles);
if new_tip {
let total_pow = share_chain.get_total_chain_pow().await;
let notify = NotifyNewTipBlock::new(self.local_peer_id, pow_algo, new_blocks, total_pow);
let notify = NotifyNewTipBlock::new(self.local_peer_id, new_blocks, total_pow);
let res = self
.p2p_client
.broadcast_block(notify)
Expand Down Expand Up @@ -246,8 +250,8 @@ where S: ShareChain
.map_err(|e| Status::internal(format!("Could not convert gprc block to tari block: {}", e)))?;
// we set the nonce to 0 in order to find the template again.
tari_block.header.nonce = 0;
new_tip_block.original_block = tari_block;
let tari_hash = new_tip_block.original_block.hash();
new_tip_block.populate_tari_data(tari_block);
let tari_hash = new_tip_block.original_header.hash();

let height = grpc_block
.header
Expand Down Expand Up @@ -402,17 +406,17 @@ where S: ShareChain
};


p2pool_block.original_block = tari_block;
let mined_tari_hash = p2pool_block.original_block.hash();
p2pool_block.original_header= tari_block.header;
let mined_tari_hash = p2pool_block.original_header.hash();

debug!(target: LOG_TARGET, "Trace - getting block difficulty: {}", timer.elapsed().as_millis());
// Check block's difficulty compared to the latest network one to increase the probability
// to get the block accepted (and also a block with lower difficulty than latest one is invalid anyway).
let request_block_difficulty = match p2pool_block.original_block.header.pow.pow_algo {
PowAlgorithm::Sha3x => sha3x_difficulty(&p2pool_block.original_block.header)
let request_block_difficulty = match p2pool_block.original_header.pow.pow_algo {
PowAlgorithm::Sha3x => sha3x_difficulty(&p2pool_block.original_header)
.map_err(|error| Status::internal(error.to_string()))?,
PowAlgorithm::RandomX => randomx_difficulty(
&p2pool_block.original_block.header,
&p2pool_block.original_header,
self.block_validation_params.random_x_factory(),
self.block_validation_params.genesis_block_hash(),
self.block_validation_params.consensus_manager(),
Expand All @@ -422,23 +426,23 @@ where S: ShareChain
info!(
target: LOG_TARGET,
"Submitted {} block difficulty: {}",
p2pool_block.original_block.header.pow.pow_algo, request_block_difficulty
p2pool_block.original_header.pow.pow_algo, request_block_difficulty
);

debug!(target: LOG_TARGET, "Trace - getting network difficulty: {}", timer.elapsed().as_millis());
let network_difficulty = match p2pool_block.original_block.header.pow.pow_algo {
let network_difficulty = match p2pool_block.original_header.pow.pow_algo {
PowAlgorithm::Sha3x => self
.sha3_block_height_difficulty_cache
.read()
.await
.get(&(p2pool_block.original_block.header.height))
.get(&(p2pool_block.original_header.height))
.copied()
.unwrap_or_else(Difficulty::min),
PowAlgorithm::RandomX => self
.randomx_block_height_difficulty_cache
.read()
.await
.get(&(p2pool_block.original_block.header.height))
.get(&(p2pool_block.original_header.height))
.copied()
.unwrap_or_else(Difficulty::min),
};
Expand Down Expand Up @@ -490,7 +494,7 @@ where S: ShareChain
// Don't error if we can't submit it.
match self.submit_share_chain_block(p2pool_block.clone()).await {
Ok(_) => {
let pow_type = p2pool_block.original_block.header.pow.pow_algo.to_string();
let pow_type = p2pool_block.original_header.pow.pow_algo.to_string();
info!(target: LOG_TARGET, "🔗 Block submitted to {} share chain!", pow_type);
},
Err(error) => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/http/stats/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub(crate) async fn handle_chain(
miner_wallet_address: block.miner_wallet_address.to_base58(),
sent_to_main_chain: block.sent_to_main_chain,
target_difficulty: block.target_difficulty.as_u64(),
candidate_block_height: block.original_block.header.height,
candidate_block_height: block.original_header.height,
algo: pow_algo.to_string().to_lowercase(),
});
}
Expand Down
Loading

0 comments on commit 84e7567

Please sign in to comment.