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

Improving the use of RelayCache on ProtocolHandler #1014

Merged
merged 5 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move this call before system.Consensus?.Tell it could be useful for search consensus payloads in the future...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, @shargon. I do not see problems in caching it before processing, since it already passed payload.Verify

Maybe let's do this in another PR, right?

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