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

optimize transaction route #2398

Merged
merged 4 commits into from
Mar 15, 2021
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
14 changes: 3 additions & 11 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ internal class Initialize { }
private readonly static Script onPersistScript, postPersistScript;
private const int MaxTxToReverifyPerIdle = 10;
private readonly NeoSystem system;
private readonly IActorRef txrouter;
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, UnverifiedBlocksList> block_cache_unverified = new Dictionary<uint, UnverifiedBlocksList>();
private ImmutableHashSet<UInt160> extensibleWitnessWhiteList;
Expand All @@ -55,13 +54,6 @@ static Blockchain()
public Blockchain(NeoSystem system)
{
this.system = system;
this.txrouter = Context.ActorOf(TransactionRouter.Props(system));
}

private bool ContainsTransaction(UInt256 hash)
{
if (system.MemPool.ContainsKey(hash)) return true;
return NativeContract.Ledger.ContainsTransaction(system.StoreView, hash);
}

private void OnImport(IEnumerable<Block> blocks, bool verify)
Expand Down Expand Up @@ -247,7 +239,7 @@ private VerifyResult OnNewExtensiblePayload(ExtensiblePayload payload)

private VerifyResult OnNewTransaction(Transaction transaction)
{
if (ContainsTransaction(transaction.Hash)) return VerifyResult.AlreadyExists;
if (system.ContainsTransaction(transaction.Hash)) return VerifyResult.AlreadyExists;
return system.MemPool.TryAdd(transaction, system.StoreView);
}

Expand Down Expand Up @@ -300,10 +292,10 @@ protected override void OnReceive(object message)

private void OnTransaction(Transaction tx)
{
if (ContainsTransaction(tx.Hash))
if (system.ContainsTransaction(tx.Hash))
SendRelayResult(tx, VerifyResult.AlreadyExists);
else
txrouter.Forward(new TransactionRouter.Preverify(tx, true));
system.TxRouter.Forward(new TransactionRouter.Preverify(tx, true));
}

private void Persist(Block block)
Expand Down
9 changes: 9 additions & 0 deletions src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.Persistence;
using Neo.Plugins;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
Expand All @@ -28,6 +29,7 @@ public class NeoSystem : IDisposable
public IActorRef Blockchain { get; }
public IActorRef LocalNode { get; }
public IActorRef TaskManager { get; }
public IActorRef TxRouter;
/// <summary>
/// A readonly view of the store.
/// </summary>
Expand Down Expand Up @@ -63,6 +65,7 @@ public NeoSystem(ProtocolSettings settings, string storageEngine = null, string
this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this));
this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this));
this.TaskManager = ActorSystem.ActorOf(Network.P2P.TaskManager.Props(this));
this.TxRouter = ActorSystem.ActorOf(TransactionRouter.Props(this));
foreach (var plugin in Plugin.Plugins)
plugin.OnSystemLoaded(this);
Blockchain.Ask(new Blockchain.Initialize()).Wait();
Expand Down Expand Up @@ -166,5 +169,11 @@ public SnapshotCache GetSnapshot()
{
return new SnapshotCache(store.GetSnapshot());
}

public bool ContainsTransaction(UInt256 hash)
{
if (MemPool.ContainsKey(hash)) return true;
return NativeContract.Ledger.ContainsTransaction(StoreView, hash);
}
}
}
20 changes: 15 additions & 5 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Akka.Actor;
using Neo.Cryptography;
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down Expand Up @@ -294,15 +295,24 @@ private void OnHeadersMessageReceived(HeadersPayload payload)

private void OnInventoryReceived(IInventory inventory)
{
knownHashes.Add(inventory.Hash);
pendingKnownHashes.Remove(inventory.Hash);
if (inventory is Block block)
switch (inventory)
{
UpdateLastBlockIndex(block.Index);
if (block.Index > NativeContract.Ledger.CurrentIndex(system.StoreView) + InvPayload.MaxHashesCount) return;
case Transaction transaction:
if (!system.ContainsTransaction(transaction.Hash))
system.TxRouter.Tell(new TransactionRouter.Preverify(transaction, true));
break;
case Block block:
UpdateLastBlockIndex(block.Index);
if (block.Index > NativeContract.Ledger.CurrentIndex(system.StoreView) + InvPayload.MaxHashesCount) return;
system.Blockchain.Tell(inventory);
break;
default:
system.Blockchain.Tell(inventory);
break;
}
knownHashes.Add(inventory.Hash);
system.TaskManager.Tell(inventory);
system.Blockchain.Tell(inventory);
}

private void OnInvMessageReceived(InvPayload payload)
Expand Down