Skip to content

Commit 4e2701a

Browse files
authored
cuprated: broadcast newly added alt blocks to peers (#577)
broadcast newly added alt blocks to peers
1 parent d36c975 commit 4e2701a

File tree

1 file changed

+16
-8
lines changed
  • binaries/cuprated/src/blockchain/manager

1 file changed

+16
-8
lines changed

binaries/cuprated/src/blockchain/manager/handler.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,19 @@ impl super::BlockchainManager {
122122
let block_hash = block.hash();
123123
let res = self.handle_incoming_alt_block(block, prepared_txs).await?;
124124

125-
if matches!(res, AddAltBlock::Cached(true)) {
125+
if let AddAltBlock::NewlyCached(block_blob) = res {
126126
info!(
127127
alt_block = true,
128128
hash = hex::encode(block_hash),
129129
"Successfully added block"
130130
);
131+
132+
let chain_height = self
133+
.blockchain_context_service
134+
.blockchain_context()
135+
.chain_height;
136+
137+
self.broadcast_block(block_blob, chain_height).await;
131138
}
132139

133140
return Ok(IncomingBlockOk::AddedToAltChain);
@@ -324,7 +331,7 @@ impl super::BlockchainManager {
324331
return;
325332
}
326333
// continue adding alt blocks.
327-
Ok(AddAltBlock::Cached(_)) => (),
334+
Ok(AddAltBlock::NewlyCached(_) | AddAltBlock::AlreadyCached) => (),
328335
}
329336
}
330337

@@ -366,7 +373,7 @@ impl super::BlockchainManager {
366373
};
367374

368375
match chain {
369-
Some((Chain::Alt(_), _)) => return Ok(AddAltBlock::Cached(false)),
376+
Some((Chain::Alt(_), _)) => return Ok(AddAltBlock::AlreadyCached),
370377
Some((Chain::Main, _)) => anyhow::bail!("Alt block already in main chain"),
371378
None => (),
372379
}
@@ -386,14 +393,15 @@ impl super::BlockchainManager {
386393
return Ok(AddAltBlock::Reorged);
387394
}
388395

396+
let block_blob = Bytes::copy_from_slice(&alt_block_info.block_blob);
389397
self.blockchain_write_handle
390398
.ready()
391399
.await
392400
.expect(PANIC_CRITICAL_SERVICE_ERROR)
393401
.call(BlockchainWriteRequest::WriteAltBlock(alt_block_info))
394402
.await?;
395403

396-
Ok(AddAltBlock::Cached(true))
404+
Ok(AddAltBlock::NewlyCached(block_blob))
397405
}
398406

399407
/// Attempt a re-org with the given top block of the alt-chain.
@@ -693,10 +701,10 @@ impl super::BlockchainManager {
693701

694702
/// The result from successfully adding an alt-block.
695703
enum AddAltBlock {
696-
/// The alt-block was cached.
697-
///
698-
/// The inner `bool` is for if the block was cached before [`false`] or was cached during the call [`true`].
699-
Cached(bool),
704+
/// We already had this alt-block cached.
705+
AlreadyCached,
706+
/// The alt-block was newly cached. Contains the block blob.
707+
NewlyCached(Bytes),
700708
/// The chain was reorged.
701709
Reorged,
702710
}

0 commit comments

Comments
 (0)