From b37820bd407b220db541f0729fbea8b279169cef Mon Sep 17 00:00:00 2001 From: AntiochP <30642645+antiochp@users.noreply.github.com> Date: Mon, 18 Dec 2017 16:18:36 -0500 Subject: [PATCH] broadcast all accepted blocks, main chain or fork (#515) (#517) tweak logging to be more consistent --- chain/src/chain.rs | 22 ++++++++++++++++++++-- chain/src/pipe.rs | 15 +++++++++------ p2p/src/peers.rs | 12 ++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index ec1ace81f..cf4c1a428 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -157,8 +157,26 @@ impl Chain { adapter.block_accepted(&b); } self.check_orphans(); - } - Ok(None) => {} + }, + Ok(None) => { + // block got accepted but we did not extend the head + // so its on a fork (or is the start of a new fork) + // broadcast the block out so everyone knows about the fork + // + // TODO - This opens us to an amplification attack on blocks + // mined at a low difficulty. We should suppress really old blocks + // or less relevant blocks somehow. + // We should also probably consider banning nodes that send us really old blocks. + // + if !opts.intersects(SYNC) { + // broadcast the block + let adapter = self.adapter.clone(); + adapter.block_accepted(&b); + } + // We accepted a block here so there is a chance we can now accept + // one or more orphans. + self.check_orphans(); + }, Err(Error::Orphan) => { // TODO - Do we want to check that orphan height is > current height? // TODO - Just check heights here? Or should we be checking total_difficulty as well? diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 8188d957d..da217dd28 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -50,7 +50,7 @@ pub struct BlockContext { /// chain head if updated. pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Error> { // TODO should just take a promise for a block with a full header so we don't - // spend resources reading the full block when its header is invalid + // spend resources reading the full block when its header is invalid debug!( LOGGER, @@ -88,7 +88,7 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er .map_err(|e| Error::StoreErr(e, "pipe reload head".to_owned()))?; // start a chain extension unit of work dependent on the success of the - // internal validation and saving operations + // internal validation and saving operations sumtree::extending(&mut sumtrees, |mut extension| { validate_block(b, &mut ctx, &mut extension)?; debug!( @@ -371,8 +371,9 @@ fn update_head(b: &Block, ctx: &mut BlockContext) -> Result, Error> ctx.head = tip.clone(); debug!( LOGGER, - "Updated head to {} at {}.", + "pipe: update_head: {}, {} at {}", b.hash(), + b.header.total_difficulty, b.header.height ); if b.header.height % 500 == 0 { @@ -394,8 +395,9 @@ fn update_sync_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result Result Result, Error> { let tip = Tip::from_block(bh); - debug!(LOGGER, "pipe: update_header_head {},{}", tip.total_difficulty, ctx.head.total_difficulty); + debug!(LOGGER, "pipe: update_header_head: {}, {}", tip.total_difficulty, ctx.head.total_difficulty); if tip.total_difficulty > ctx.head.total_difficulty { ctx.store .save_header_head(&tip) @@ -414,8 +416,9 @@ fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result