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 2 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
56 changes: 47 additions & 9 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ 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 int Size; }

private const int MaxUnverifiedBlockSize = 100 * 1024 * 1024;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
public static readonly uint MillisecondsPerBlock = ProtocolSettings.Default.MillisecondsPerBlock;
public static readonly TimeSpan TimePerBlock = TimeSpan.FromMilliseconds(MillisecondsPerBlock);
public static readonly ECPoint[] StandbyCommittee = ProtocolSettings.Default.StandbyCommittee.Select(p => ECPoint.DecodePoint(p.HexToBytes(), ECCurve.Secp256r1)).ToArray();
Expand Down Expand Up @@ -61,7 +63,8 @@ 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>();
private int block_cache_unverified_size = 0;
internal readonly RelayCache RelayCache = new RelayCache(100);
private SnapshotView currentSnapshot;

Expand Down Expand Up @@ -263,22 +266,57 @@ private void OnImport(IEnumerable<Block> blocks, bool verify)
Sender.Tell(new ImportCompleted());
}

private void RemoveUnverifiedBlockToCache(uint index, bool removeKnownHashes)
{
if (block_cache_unverified.Remove(index, out var entry))
{
block_cache_unverified_size -= entry.Size;

if (removeKnownHashes)
{
var hashes = entry.Blocks.Select(u => u.Hash).ToArray();

system.LocalNode.Tell(new TaskManager.RemoveKnownHashes { Hashes = hashes });
system.TaskManager.Tell(new TaskManager.RemoveKnownHashes { Hashes = hashes });
}
}
}

private void AddUnverifiedBlockToCache(Block block)
{
if (block_cache_unverified_size > MaxUnverifiedBlockSize && block.Index > Height + 10)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
// Drop last entry if it's higher

if (block_cache_unverified.Count > 0)
{
var max = block_cache_unverified.Keys.Max();
if (max > Height + 10)
{
RemoveUnverifiedBlockToCache(max, true);
}
}

// Drop block, we can't save it safely in memory
return;
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

// 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)
foreach (var unverifiedBlock in blocks.Blocks)
{
if (block.Hash == unverifiedBlock.Hash)
return;
}
blocks.AddLast(block);
blocks.Blocks.AddLast(block);
blocks.Size += block.Size;
block_cache_unverified_size += block.Size;
}

private void OnFillMemoryPool(IEnumerable<Transaction> transactions)
Expand Down Expand Up @@ -328,16 +366,16 @@ private VerifyResult OnNewBlock(Block block)
if (!block.Verify(currentSnapshot))
return VerifyResult.Invalid;
block_cache.TryAdd(block.Hash, block);
block_cache_unverified.Remove(block.Index);
RemoveUnverifiedBlockToCache(block.Index, false);
// We can store the new block in block_cache and tell the new height to other nodes before Persist().
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);
RemoveUnverifiedBlockToCache(Height + 1, false);
}
}
return VerifyResult.Succeed;
Expand Down
4 changes: 4 additions & 0 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ protected override void OnReceive(object message)
case SendDirectly send:
OnSendDirectly(send.Inventory);
break;
case TaskManager.RemoveKnownHashes remove:
OnRemoveKnownHashes(remove);
break;
}
}

Expand All @@ -216,6 +219,7 @@ private void OnRelayDirectly(IInventory inventory)
SendToRemoteNodes(message);
}

private void OnRemoveKnownHashes(TaskManager.RemoveKnownHashes remove) => SendToRemoteNodes(remove);
private void OnSendDirectly(IInventory inventory) => SendToRemoteNodes(inventory);

protected override void OnTcpConnected(IActorRef connection)
Expand Down
5 changes: 5 additions & 0 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ protected override UInt256 GetKeyForItem((UInt256, DateTime) item)

private readonly ICancelable timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimerInterval, TimerInterval, Context.Self, new Timer(), ActorRefs.NoSender);

private void OnRemoveKnownHashes(UInt256[] hashes)
{
knownHashes.ExceptWith(hashes);
}

private void OnMessage(Message msg)
{
foreach (IP2PPlugin plugin in Plugin.P2PPlugins)
Expand Down
3 changes: 3 additions & 0 deletions src/neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ protected override void OnReceive(object message)
case Timer _:
RefreshPendingKnownHashes();
break;
case TaskManager.RemoveKnownHashes remove:
OnRemoveKnownHashes(remove.Hashes);
break;
case Message msg:
EnqueueMessage(msg);
break;
Expand Down
9 changes: 9 additions & 0 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Update { public uint LastBlockIndex; public bool RequestTasks; }
public class NewTasks { public InvPayload Payload; }
public class RestartTasks { public InvPayload Payload; }
private class Timer { }
internal class RemoveKnownHashes { public UInt256[] Hashes; }

private static readonly TimeSpan TimerInterval = TimeSpan.FromSeconds(30);
private static readonly TimeSpan TaskTimeout = TimeSpan.FromMinutes(1);
Expand Down Expand Up @@ -121,10 +122,18 @@ private void OnPersistCompleted(Block block)
receivedBlockIndex.Remove(block.Index);
}

private void OnRemoveKnownHashes(UInt256[] hashes)
{
knownHashes.ExceptWith(hashes);
}

protected override void OnReceive(object message)
{
switch (message)
{
case RemoveKnownHashes remove:
OnRemoveKnownHashes(remove.Hashes);
break;
case Register register:
OnRegister(register.Version);
break;
Expand Down