Skip to content

Commit

Permalink
Optimize mempool message (#1068)
Browse files Browse the repository at this point in the history
* Optimization

* Prevent relay

* Change comment

* Optimizations
  • Loading branch information
shargon authored and vncoelho committed Aug 29, 2019
1 parent c04ee5d commit 0bd198e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void OnNewHeaders(Header[] headers)
system.TaskManager.Tell(new TaskManager.HeaderTaskCompleted(), Sender);
}

private RelayResultReason OnNewTransaction(Transaction transaction)
private RelayResultReason OnNewTransaction(Transaction transaction, bool relay)
{
if (ContainsTransaction(transaction.Hash))
return RelayResultReason.AlreadyExists;
Expand All @@ -377,8 +377,8 @@ private RelayResultReason OnNewTransaction(Transaction transaction)

if (!MemPool.TryAdd(transaction.Hash, transaction))
return RelayResultReason.OutOfMemory;

system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = transaction });
if (relay)
system.LocalNode.Tell(new LocalNode.RelayDirectly { Inventory = transaction });
return RelayResultReason.Succeed;
}

Expand All @@ -405,8 +405,14 @@ protected override void OnReceive(object message)
case Block block:
Sender.Tell(OnNewBlock(block));
break;
case Transaction[] transactions:
{
// This message comes from a mempool's revalidation, already relayed
foreach (var tx in transactions) OnNewTransaction(tx, false);
break;
}
case Transaction transaction:
Sender.Tell(OnNewTransaction(transaction));
Sender.Tell(OnNewTransaction(transaction, true));
break;
case ConsensusPayload payload:
Sender.Tell(OnNewConsensus(payload));
Expand Down
7 changes: 6 additions & 1 deletion neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,14 @@ internal void UpdatePoolForBlockPersisted(Block block, Snapshot snapshot)

if (policyChanged)
{
var tx = new List<Transaction>();
foreach (PoolItem item in _unverifiedSortedTransactions.Reverse())
if (item.Tx.FeePerByte >= _feePerByte)
_system.Blockchain.Tell(item.Tx, ActorRefs.NoSender);
tx.Add(item.Tx);

if (tx.Count > 0)
_system.Blockchain.Tell(tx.ToArray(), ActorRefs.NoSender);

_unverifiedTransactions.Clear();
_unverifiedSortedTransactions.Clear();
}
Expand Down

0 comments on commit 0bd198e

Please sign in to comment.