Skip to content

Commit

Permalink
Improving the use of RelayCache on ProtocolHandler (neo-project#1014
Browse files Browse the repository at this point in the history
)

* Refactoring and simplifing the use of relaycache

* Standarzing name

* simplify
  • Loading branch information
vncoelho authored and Tommo-L committed Jun 22, 2020
1 parent 02c49ec commit e22f2ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class FillCompleted { }
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>>();
internal readonly RelayCache RelayCache = new RelayCache(100);
internal readonly RelayCache ConsensusRelayCache = new RelayCache(100);
private Snapshot currentSnapshot;

public Store Store { get; }
Expand Down Expand Up @@ -328,7 +328,7 @@ private RelayResultReason OnNewConsensus(ConsensusPayload payload)
{
if (!payload.Verify(currentSnapshot)) return RelayResultReason.Invalid;
system.Consensus?.Tell(payload);
RelayCache.Add(payload);
ConsensusRelayCache.Add(payload);
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = payload });
return RelayResultReason.Succeed;
}
Expand Down
19 changes: 8 additions & 11 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,20 @@ private void OnGetDataMessageReceived(InvPayload payload)
UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray();
foreach (UInt256 hash in hashes)
{
Blockchain.Singleton.RelayCache.TryGet(hash, out IInventory inventory);
switch (payload.Type)
{
case InventoryType.TX:
if (inventory == null)
inventory = Blockchain.Singleton.GetTransaction(hash);
if (inventory is Transaction)
Context.Parent.Tell(Message.Create(MessageCommand.Transaction, inventory));
Transaction tx = Blockchain.Singleton.GetTransaction(hash);
if (tx != null)
Context.Parent.Tell(Message.Create(MessageCommand.Transaction, tx));
break;
case InventoryType.Block:
if (inventory == null)
inventory = Blockchain.Singleton.GetBlock(hash);
if (inventory is Block block)
Block block = Blockchain.Singleton.GetBlock(hash);
if (block != null)
{
if (bloom_filter == null)
{
Context.Parent.Tell(Message.Create(MessageCommand.Block, inventory));
Context.Parent.Tell(Message.Create(MessageCommand.Block, block));
}
else
{
Expand All @@ -206,8 +203,8 @@ private void OnGetDataMessageReceived(InvPayload payload)
}
break;
case InventoryType.Consensus:
if (inventory != null)
Context.Parent.Tell(Message.Create(MessageCommand.Consensus, inventory));
if (Blockchain.Singleton.ConsensusRelayCache.TryGet(hash, out IInventory inventoryConsensus))
Context.Parent.Tell(Message.Create(MessageCommand.Consensus, inventoryConsensus));
break;
}
}
Expand Down

0 comments on commit e22f2ee

Please sign in to comment.