Skip to content

Commit

Permalink
fix blockchain on transaction array logic (neo-project#2146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiao-Jin authored and cloud8little committed Jan 24, 2021
1 parent 05bfbd6 commit 441617d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 40 deletions.
14 changes: 5 additions & 9 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Import { public IEnumerable<Block> Blocks; public bool Verify = tru
public class ImportCompleted { }
public class FillMemoryPool { public IEnumerable<Transaction> Transactions; }
public class FillCompleted { }
internal class PreverifyCompleted { public Transaction Transaction; public VerifyResult Result; public bool Relay; }
internal class PreverifyCompleted { public Transaction Transaction; public VerifyResult Result; }
public class RelayResult { public IInventory Inventory; public VerifyResult Result; }
private class UnverifiedBlocksList { public LinkedList<Block> Blocks = new LinkedList<Block>(); public HashSet<IActorRef> Nodes = new HashSet<IActorRef>(); }

Expand Down Expand Up @@ -338,7 +338,7 @@ private VerifyResult OnNewTransaction(Transaction transaction)
private void OnPreverifyCompleted(PreverifyCompleted task)
{
if (task.Result == VerifyResult.Succeed)
OnInventory(task.Transaction, task.Relay);
OnInventory(task.Transaction, true);
else
SendRelayResult(task.Transaction, task.Result);
}
Expand All @@ -357,11 +357,7 @@ protected override void OnReceive(object message)
OnInventory(block, false);
break;
case Transaction tx:
OnTransaction(tx, true);
break;
case Transaction[] transactions:
// This message comes from a mempool's revalidation, already relayed
foreach (var tx in transactions) OnTransaction(tx, false);
OnTransaction(tx);
break;
case IInventory inventory:
OnInventory(inventory);
Expand All @@ -376,12 +372,12 @@ protected override void OnReceive(object message)
}
}

private void OnTransaction(Transaction tx, bool relay)
private void OnTransaction(Transaction tx)
{
if (ContainsTransaction(tx.Hash))
SendRelayResult(tx, VerifyResult.AlreadyExists);
else
txrouter.Tell(new TransactionRouter.Task { Transaction = tx, Relay = relay }, Sender);
txrouter.Tell(tx, Sender);
}

private void Persist(Block block)
Expand Down
28 changes: 3 additions & 25 deletions src/neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public class MemoryPool : IReadOnlyCollection<Transaction>
internal int UnverifiedSortedTxCount => _unverifiedSortedTransactions.Count;

private int _maxTxPerBlock;
private long _feePerByte;
private uint _execFeeFactor;

/// <summary>
/// Total maximum capacity of transactions the pool can hold.
Expand Down Expand Up @@ -106,15 +104,9 @@ public MemoryPool(NeoSystem system, int capacity)
Capacity = capacity;
}

internal bool LoadPolicy(StoreView snapshot)
internal void LoadPolicy(StoreView snapshot)
{
_maxTxPerBlock = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(snapshot);
long newFeePerByte = NativeContract.Policy.GetFeePerByte(snapshot);
uint newExecFeeFactor = NativeContract.Policy.GetExecFeeFactor(snapshot);
bool policyChanged = newFeePerByte > _feePerByte || newExecFeeFactor > _execFeeFactor;
_feePerByte = newFeePerByte;
_execFeeFactor = newExecFeeFactor;
return policyChanged;
}

/// <summary>
Expand Down Expand Up @@ -359,7 +351,7 @@ internal void InvalidateVerifiedTransactions()
// Note: this must only be called from a single thread (the Blockchain actor)
internal void UpdatePoolForBlockPersisted(Block block, StoreView snapshot)
{
bool policyChanged = LoadPolicy(snapshot);
LoadPolicy(snapshot);

_txRwLock.EnterWriteLock();
try
Expand All @@ -373,20 +365,6 @@ internal void UpdatePoolForBlockPersisted(Block block, StoreView snapshot)

// Add all the previously verified transactions back to the unverified transactions
InvalidateVerifiedTransactions();

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

_unverifiedTransactions.Clear();
_unverifiedSortedTransactions.Clear();

if (tx.Count > 0)
_system.Blockchain.Tell(tx.ToArray(), ActorRefs.NoSender);
}
}
finally
{
Expand All @@ -395,7 +373,7 @@ internal void UpdatePoolForBlockPersisted(Block block, StoreView snapshot)

// If we know about headers of future blocks, no point in verifying transactions from the unverified tx pool
// until we get caught up.
if (block.Index > 0 && block.Index < Blockchain.Singleton.HeaderHeight || policyChanged)
if (block.Index > 0 && block.Index < Blockchain.Singleton.HeaderHeight)
return;

ReverifyTransactions(_sortedTransactions, _unverifiedSortedTransactions,
Expand Down
9 changes: 3 additions & 6 deletions src/neo/Ledger/TransactionRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace Neo.Ledger
{
internal class TransactionRouter : UntypedActor
{
public class Task { public Transaction Transaction; public bool Relay; }

private readonly IActorRef blockchain;

public TransactionRouter(NeoSystem system)
Expand All @@ -18,12 +16,11 @@ public TransactionRouter(NeoSystem system)

protected override void OnReceive(object message)
{
if (!(message is Task task)) return;
if (!(message is Transaction tx)) return;
blockchain.Tell(new Blockchain.PreverifyCompleted
{
Transaction = task.Transaction,
Result = task.Transaction.VerifyStateIndependent(),
Relay = task.Relay
Transaction = tx,
Result = tx.VerifyStateIndependent()
}, Sender);
}

Expand Down

0 comments on commit 441617d

Please sign in to comment.