Skip to content

Commit

Permalink
make compatible with current version
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Dec 3, 2024
1 parent 1e1f8c6 commit 4126c21
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
9 changes: 9 additions & 0 deletions src/server/p2p/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ pub struct NotifyNewTipBlock {
pub version: u64,
peer_id: PeerId,
pub new_blocks: Vec<P2Block>,
// remove next version
pub total_accumulated_difficulty: u128,
pub timestamp: u64,
}

Expand All @@ -250,11 +252,18 @@ impl_conversions!(NotifyNewTipBlock);

impl NotifyNewTipBlock {
pub fn new(peer_id: PeerId, new_blocks: Vec<P2Block>) -> Self {
let max_block = new_blocks.iter().max_by_key(|x| x.height);
let pow = match max_block {
Some(block) => block.total_pow(),
None => AccumulatedDifficulty::min(),
}
.as_u128();
let timestamp = EpochTime::now().as_u64();
Self {
version: PROTOCOL_VERSION,
peer_id,
new_blocks,
total_accumulated_difficulty: pow,
timestamp,
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,21 +961,22 @@ where S: ShareChain
info!(target: LOG_TARGET, squad; "Received sync response for chain {} from {} with blocks {:?}", algo, peer, blocks.iter().map(|a| format!("{}({:x}{:x}{:x}{:x})",a.height, a.hash[0], a.hash[1], a.hash[2], a.hash[3])).collect::<Vec<String>>());
let tx = self.inner_request_tx.clone();
let peer_store = self.network_peer_store.clone();
let notify_channel = self.client_broadcast_block_tx.clone();
let local_peer_id = *self.swarm.local_peer_id();
// let notify_channel = self.client_broadcast_block_tx.clone();
// let local_peer_id = *self.swarm.local_peer_id();
tokio::spawn(async move {
match share_chain.add_synced_blocks(&blocks).await {
Ok(new_tip) => {
info!(target: LOG_TARGET, squad; "[{:?}] Synced blocks added to share chain: {}",algo, new_tip);
let tip_blocks: Vec<P2Block> = match share_chain.get_tip_and_uncle_blocks().await {
Ok(tip_blocks) => tip_blocks.into_iter().map(|b| (*b).clone()).collect(),
Err(e) => {
error!(target: LOG_TARGET, squad; "Failed to get tip and uncle blocks: {e:?}");
return;
},
};
let notify = NotifyNewTipBlock::new(local_peer_id, tip_blocks);
let _unused = notify_channel.send(notify);
// I think we should do this
// let tip_blocks: Vec<P2Block> = match share_chain.get_tip_and_uncle_blocks().await {
// Ok(tip_blocks) => tip_blocks.into_iter().map(|b| (*b).clone()).collect(),
// Err(e) => {
// error!(target: LOG_TARGET, squad; "Failed to get tip and uncle blocks: {e:?}");
// return;
// },
// };
// let notify = NotifyNewTipBlock::new(local_peer_id, tip_blocks);
// let _unused = notify_channel.send(notify);

let missing_parents = new_tip.into_missing_parents_vec();
if !missing_parents.is_empty() {
Expand Down
38 changes: 21 additions & 17 deletions src/sharechain/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,23 +637,27 @@ impl ShareChain for InMemoryShareChain {
.build()?)
}

async fn get_tip_and_uncle_blocks(&self) -> Result<Vec<Arc<P2Block>>, ShareChainError> {
let p2_chain_read_lock = self.p2_chain.read().await;
let mut result = Vec::new();
let tip_level = match p2_chain_read_lock.get_tip() {
Some(level) => level,
None => return Ok(result),
};
result.push(tip_level.get_block_in_main_chain().ok_or(ShareChainError::BlockNotFound)?);
let uncles = result[0].uncles.clone();
for uncle in uncles {
let block = p2_chain_read_lock
.get_block_at_height(uncle.0, &uncle.1)
.ok_or(ShareChainError::BlockNotFound)?;
result.push(block);
}
Ok(result)
}
// async fn get_tip_and_uncle_blocks(&self) -> Result<Vec<Arc<P2Block>>, ShareChainError> {
// let p2_chain_read_lock = self.p2_chain.read().await;
// let mut result = Vec::new();
// let tip_level = match p2_chain_read_lock.get_tip() {
// Some(level) => level,
// None => return Ok(result),
// };
// result.push(
// tip_level
// .get_block_in_main_chain()
// .ok_or(ShareChainError::BlockNotFound)?,
// );
// let uncles = result[0].uncles.clone();
// for uncle in uncles {
// let block = p2_chain_read_lock
// .get_block_at_height(uncle.0, &uncle.1)
// .ok_or(ShareChainError::BlockNotFound)?;
// result.push(block);
// }
// Ok(result)
// }

async fn get_blocks(&self, requested_blocks: &[(u64, FixedHash)]) -> Vec<Arc<P2Block>> {
let p2_chain_read_lock = self.p2_chain.read().await;
Expand Down
4 changes: 1 addition & 3 deletions src/sharechain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@ pub(crate) trait ShareChain: Send + Sync + 'static {
coinbase_extra: Vec<u8>,
) -> Result<Arc<P2Block>, ShareChainError>;

// /// Return a new block that could be added via `submit_block`.
// async fn new_block(&self, request: &SubmitBlockRequest, squad: Squad) -> Result<P2Block, ShareChainError>;

/// Returns the requested blocks from this chain
async fn get_blocks(&self, requested_blocks: &[(u64, FixedHash)]) -> Vec<Arc<P2Block>>;

/// Returns the requested blocks from this chain
async fn get_tip_and_uncle_blocks(&self) -> Result<Vec<Arc<P2Block>>, ShareChainError>;
// async fn get_tip_and_uncle_blocks(&self) -> Result<Vec<Arc<P2Block>>, ShareChainError>;

async fn request_sync(
&self,
Expand Down

0 comments on commit 4126c21

Please sign in to comment.