Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block receive height limit #1870

Merged
merged 20 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Akka.Actor;
using Akka.Configuration;
using Akka.IO;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.IO.Actors;
Expand Down Expand Up @@ -28,6 +29,7 @@ public class FillMemoryPool { public IEnumerable<Transaction> Transactions; }
public class FillCompleted { }
internal class PreverifyCompleted { public Transaction Transaction; public VerifyResult Result; public bool Relay; }
public class RelayResult { public IInventory Inventory; public VerifyResult Result; }
private class UnverifiedBlocks { public LinkedList<Block> Blocks = new LinkedList<Block>(); public HashSet<IActorRef> Nodes = new HashSet<IActorRef>(); }

public static readonly uint MillisecondsPerBlock = ProtocolSettings.Default.MillisecondsPerBlock;
public static readonly TimeSpan TimePerBlock = TimeSpan.FromMilliseconds(MillisecondsPerBlock);
Expand Down Expand Up @@ -61,7 +63,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu
private readonly List<UInt256> header_index = new List<UInt256>();
private uint stored_header_count = 0;
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, LinkedList<Block>> block_cache_unverified = new Dictionary<uint, LinkedList<Block>>();
private readonly Dictionary<uint, UnverifiedBlocks> block_cache_unverified = new Dictionary<uint, UnverifiedBlocks>();
internal readonly RelayCache RelayCache = new RelayCache(100);
private SnapshotView currentSnapshot;

Expand Down Expand Up @@ -266,19 +268,30 @@ private void OnImport(IEnumerable<Block> blocks, bool verify)
private void AddUnverifiedBlockToCache(Block block)
{
// Check if any block proposal for height `block.Index` exists
if (!block_cache_unverified.TryGetValue(block.Index, out LinkedList<Block> blocks))
if (!block_cache_unverified.TryGetValue(block.Index, out var blocks))
{
// There are no blocks, a new LinkedList is created and, consequently, the current block is added to the list
blocks = new LinkedList<Block>();
blocks = new UnverifiedBlocks();
block_cache_unverified.Add(block.Index, blocks);
}
// Check if any block with the hash being added already exists on possible candidates to be processed
foreach (var unverifiedBlock in blocks)
else
{
if (block.Hash == unverifiedBlock.Hash)
// Check if any block with the hash being added already exists on possible candidates to be processed
foreach (var unverifiedBlock in blocks.Blocks)
{
if (block.Hash == unverifiedBlock.Hash)
return;
}

if (!blocks.Nodes.Add(Sender))
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
// Same index with different hash
Sender.Tell(Tcp.Abort.Instance);
return;
}
}
blocks.AddLast(block);

blocks.Blocks.AddLast(block);
}

private void OnFillMemoryPool(IEnumerable<Transaction> transactions)
Expand Down Expand Up @@ -333,9 +346,9 @@ private VerifyResult OnNewBlock(Block block)
system.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(Singleton.Height + 1)));
Persist(block);
SaveHeaderHashList();
if (block_cache_unverified.TryGetValue(Height + 1, out LinkedList<Block> unverifiedBlocks))
if (block_cache_unverified.TryGetValue(Height + 1, out var unverifiedBlocks))
{
foreach (var unverifiedBlock in unverifiedBlocks)
foreach (var unverifiedBlock in unverifiedBlocks.Blocks)
Self.Tell(unverifiedBlock, ActorRefs.NoSender);
block_cache_unverified.Remove(Height + 1);
}
Expand Down
8 changes: 5 additions & 3 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,20 @@ private void OnGetHeadersMessageReceived(GetBlockByIndexPayload payload)
private void OnInventoryReceived(IInventory inventory)
{
pendingKnownHashes.Remove(inventory.Hash);
knownHashes.Add(inventory.Hash);
system.TaskManager.Tell(inventory);
system.Blockchain.Tell(inventory, ActorRefs.NoSender);
switch (inventory)
{
case Transaction transaction:
knownHashes.Add(inventory.Hash);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
system.Consensus?.Tell(transaction);
break;
case Block block:
if (block.Index > Blockchain.Singleton.Height + 500) return;
knownHashes.Add(inventory.Hash);
UpdateLastBlockIndex(block.Index, false);
break;
}
system.TaskManager.Tell(inventory);
system.Blockchain.Tell(inventory, ActorRefs.NoSender);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnInvMessageReceived(InvPayload payload)
Expand Down