Trace comment on new block inclusion (#100)
This commit is contained in:
@@ -529,6 +529,7 @@ impl ChainNotify for EthSync {
|
||||
}
|
||||
}
|
||||
|
||||
// t_nb 11.4
|
||||
fn new_blocks(&self, new_blocks: NewBlocks) {
|
||||
if new_blocks.has_more_blocks_to_import {
|
||||
return;
|
||||
|
||||
@@ -155,6 +155,7 @@ impl SyncHandler {
|
||||
trace!(target: "sync", "Ignoring new block from unconfirmed peer {}", peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
// t_nb 1.0 decode RLP
|
||||
let block = Unverified::from_rlp(r.at(0)?.as_raw().to_vec())?;
|
||||
let hash = block.header.hash();
|
||||
let number = block.header.number();
|
||||
@@ -166,7 +167,9 @@ impl SyncHandler {
|
||||
let difficulty: U256 = r.val_at(1)?;
|
||||
// Most probably the sent block is being imported by peer right now
|
||||
// Use td and hash, that peer must have for now
|
||||
// t_nb 1.1 check new block diffuculty it can be found as second item in RLP and update peer diffuculty
|
||||
let parent_td = difficulty.checked_sub(*block.header.difficulty());
|
||||
|
||||
if let Some(ref mut peer) = sync.peers.get_mut(&peer_id) {
|
||||
if peer
|
||||
.difficulty
|
||||
@@ -181,6 +184,7 @@ impl SyncHandler {
|
||||
peer.latest_hash = *parent_hash;
|
||||
}
|
||||
|
||||
// t_nb 1.2 if block number is to older then 20 dont process it
|
||||
let last_imported_number = sync.new_blocks.last_imported_block_number();
|
||||
if last_imported_number > number && last_imported_number - number > MAX_NEW_BLOCK_AGE {
|
||||
trace!(target: "sync", "Ignored ancient new block {:?}", hash);
|
||||
|
||||
@@ -1421,7 +1421,7 @@ impl ChainSync {
|
||||
self.check_resume(io);
|
||||
}
|
||||
|
||||
/// called when block is imported to chain - propagates the blocks and updates transactions sent to peers
|
||||
// t_nb 11.4 called when block is imported to chain - propagates the blocks and updates transactions sent to peers
|
||||
pub fn chain_new_blocks(
|
||||
&mut self,
|
||||
io: &mut dyn SyncIo,
|
||||
@@ -1437,7 +1437,9 @@ impl ChainSync {
|
||||
|
||||
if !is_syncing || !sealed.is_empty() || !proposed.is_empty() {
|
||||
trace!(target: "sync", "Propagating blocks, state={:?}", self.state);
|
||||
// t_nb 11.4.1 propagate latest blocks
|
||||
SyncPropagator::propagate_latest_blocks(self, io, sealed);
|
||||
// t_nb 11.4.4 propagate proposed blocks
|
||||
SyncPropagator::propagate_proposed_blocks(self, io, proposed);
|
||||
}
|
||||
if !invalid.is_empty() {
|
||||
@@ -1446,7 +1448,7 @@ impl ChainSync {
|
||||
}
|
||||
|
||||
if !is_syncing && !enacted.is_empty() && !self.peers.is_empty() {
|
||||
// Select random peer to re-broadcast transactions to.
|
||||
// t_nb 11.4.5 Select random peer to re-broadcast transactions to.
|
||||
let peer = random::new().gen_range(0, self.peers.len());
|
||||
trace!(target: "sync", "Re-broadcasting transactions to a random peer.");
|
||||
self.peers.values_mut().nth(peer).map(|peer_info| {
|
||||
|
||||
@@ -39,7 +39,7 @@ use super::{
|
||||
pub struct SyncPropagator;
|
||||
|
||||
impl SyncPropagator {
|
||||
/// propagates latest block to a set of peers
|
||||
// t_nb 11.4.3 propagates latest block to a set of peers
|
||||
pub fn propagate_blocks(
|
||||
sync: &mut ChainSync,
|
||||
chain_info: &BlockChainInfo,
|
||||
@@ -72,7 +72,7 @@ impl SyncPropagator {
|
||||
sent
|
||||
}
|
||||
|
||||
/// propagates new known hashes to all peers
|
||||
// t_nb 11.4.2 propagates new known hashes to all peers
|
||||
pub fn propagate_new_hashes(
|
||||
sync: &mut ChainSync,
|
||||
chain_info: &BlockChainInfo,
|
||||
@@ -279,6 +279,7 @@ impl SyncPropagator {
|
||||
sent_to_peers
|
||||
}
|
||||
|
||||
// t_nb 11.4.1 propagate latest blocks to peers
|
||||
pub fn propagate_latest_blocks(sync: &mut ChainSync, io: &mut dyn SyncIo, sealed: &[H256]) {
|
||||
let chain_info = io.chain().chain_info();
|
||||
if (((chain_info.best_block_number as i64) - (sync.last_sent_block_number as i64)).abs()
|
||||
@@ -287,15 +288,19 @@ impl SyncPropagator {
|
||||
{
|
||||
let peers = sync.get_lagging_peers(&chain_info);
|
||||
if sealed.is_empty() {
|
||||
// t_nb 11.4.2
|
||||
let hashes = SyncPropagator::propagate_new_hashes(sync, &chain_info, io, &peers);
|
||||
let peers = ChainSync::select_random_peers(&peers);
|
||||
// t_nb 11.4.3
|
||||
let blocks =
|
||||
SyncPropagator::propagate_blocks(sync, &chain_info, io, sealed, &peers);
|
||||
if blocks != 0 || hashes != 0 {
|
||||
trace!(target: "sync", "Sent latest {} blocks and {} hashes to peers.", blocks, hashes);
|
||||
}
|
||||
} else {
|
||||
// t_nb 11.4.3
|
||||
SyncPropagator::propagate_blocks(sync, &chain_info, io, sealed, &peers);
|
||||
// t_nb 11.4.2
|
||||
SyncPropagator::propagate_new_hashes(sync, &chain_info, io, &peers);
|
||||
trace!(target: "sync", "Sent sealed block to all peers");
|
||||
};
|
||||
@@ -303,7 +308,7 @@ impl SyncPropagator {
|
||||
sync.last_sent_block_number = chain_info.best_block_number;
|
||||
}
|
||||
|
||||
/// Distribute valid proposed blocks to subset of current peers.
|
||||
// t_nb 11.4.4 Distribute valid proposed blocks to subset of current peers. (if there is any proposed)
|
||||
pub fn propagate_proposed_blocks(
|
||||
sync: &mut ChainSync,
|
||||
io: &mut dyn SyncIo,
|
||||
|
||||
Reference in New Issue
Block a user