From dba911bdd15aa1958a614d0f6d9b25063963de0c Mon Sep 17 00:00:00 2001 From: ShawnYun <42930111+ShawnYun@users.noreply.github.com> Date: Mon, 6 Jan 2020 20:27:30 +0800 Subject: [PATCH] Add relay-block-filter (#1380) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add relay-block-filter * modify * modify * modify * modify Co-authored-by: Vitor Nazário Coelho --- src/neo/Network/P2P/LocalNode.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/neo/Network/P2P/LocalNode.cs b/src/neo/Network/P2P/LocalNode.cs index ffd0ac9930..bdc4713bfe 100644 --- a/src/neo/Network/P2P/LocalNode.cs +++ b/src/neo/Network/P2P/LocalNode.cs @@ -155,7 +155,7 @@ protected override void NeedMorePeers(int count) else { // Will call AddPeers with default SeedList set cached on . - // It will try to add those, sequentially, to the list of currently uncconected ones. + // It will try to add those, sequentially, to the list of currently unconnected ones. Random rand = new Random(); AddPeers(SeedList.Where(u => u != null).OrderBy(p => rand.Next()).Take(count)); @@ -197,7 +197,22 @@ private void OnRelay(IInventory inventory) system.Blockchain.Tell(inventory); } - private void OnRelayDirectly(IInventory inventory) => SendToRemoteNodes(new RemoteNode.Relay { Inventory = inventory }); + private void OnRelayDirectly(IInventory inventory) + { + var message = new RemoteNode.Relay { Inventory = inventory }; + // When relaying a block, if the block's index is greater than 'LastBlockIndex' of the RemoteNode, relay the block; + // otherwise, don't relay. + if (inventory is Block block) + { + foreach (KeyValuePair kvp in RemoteNodes) + { + if (block.Index > kvp.Value.LastBlockIndex) + kvp.Key.Tell(message); + } + } + else + SendToRemoteNodes(message); + } private void OnSendDirectly(IInventory inventory) => SendToRemoteNodes(inventory);