Skip to content

Commit

Permalink
Merge pull request #6 from neo-project/master
Browse files Browse the repository at this point in the history
Potential Fix for stuck nodes: Don't lose future blocks if multiple f…
  • Loading branch information
vncoelho authored Dec 1, 2018
2 parents 2243d4b + 07896db commit a7ee6ef
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class ImportCompleted { }
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, Block> block_cache_unverified = new Dictionary<uint, Block>();
private readonly Dictionary<uint, LinkedList<Block>> block_cache_unverified = new Dictionary<uint, LinkedList<Block>>();
private readonly MemoryPool mem_pool = new MemoryPool(50_000);
private readonly ConcurrentDictionary<UInt256, Transaction> mem_pool_unverified = new ConcurrentDictionary<UInt256, Transaction>();
internal readonly RelayCache RelayCache = new RelayCache(100);
Expand Down Expand Up @@ -254,6 +254,17 @@ private void OnImport(IEnumerable<Block> blocks)
Sender.Tell(new ImportCompleted());
}

private void AddUnverifiedBlockToCache(Block block)
{
if (!block_cache_unverified.TryGetValue(block.Index, out LinkedList<Block> blocks))
{
blocks = new LinkedList<Block>();
block_cache_unverified.Add(block.Index, blocks);
}

blocks.AddLast(block);
}

private RelayResultReason OnNewBlock(Block block)
{
if (block.Index <= Height)
Expand All @@ -262,7 +273,7 @@ private RelayResultReason OnNewBlock(Block block)
return RelayResultReason.AlreadyExists;
if (block.Index - 1 >= header_index.Count)
{
block_cache_unverified[block.Index] = block;
AddUnverifiedBlockToCache(block);
return RelayResultReason.UnableToVerify;
}
if (block.Index == header_index.Count)
Expand Down Expand Up @@ -301,8 +312,12 @@ private RelayResultReason OnNewBlock(Block block)
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = block });
}
SaveHeaderHashList();
if (block_cache_unverified.TryGetValue(Height + 1, out block_persist))
Self.Tell(block_persist, ActorRefs.NoSender);

if (block_cache_unverified.TryGetValue(Height + 1, out LinkedList<Block> unverifiedBlocks))
{
foreach (var unverifiedBlock in unverifiedBlocks)
Self.Tell(unverifiedBlock, ActorRefs.NoSender);
}
}
else
{
Expand Down

0 comments on commit a7ee6ef

Please sign in to comment.