Skip to content

Commit

Permalink
Clear mempool when policy is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jul 1, 2019
1 parent c097453 commit bcdc435
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class MemoryPool : IReadOnlyCollection<Transaction>
internal int UnverifiedSortedTxCount => _unverifiedSortedTransactions.Count;

private int _maxTxPerBlock;
private long _feePerByte;
private bool _policyChanged;

/// <summary>
/// Total maximum capacity of transactions the pool can hold.
Expand Down Expand Up @@ -102,6 +104,9 @@ public MemoryPool(NeoSystem system, int capacity)
internal void LoadPolicy(Snapshot snapshot)
{
_maxTxPerBlock = (int)NativeContract.Policy.GetMaxTransactionsPerBlock(snapshot);
long newFeePerByte = NativeContract.Policy.GetFeePerByte(snapshot);
_policyChanged = newFeePerByte > _feePerByte;
_feePerByte = newFeePerByte;
}

/// <summary>
Expand Down Expand Up @@ -337,18 +342,30 @@ internal void InvalidateVerifiedTransactions()
// Note: this must only be called from a single thread (the Blockchain actor)
internal void UpdatePoolForBlockPersisted(Block block, Snapshot snapshot)
{
LoadPolicy(snapshot);

_txRwLock.EnterWriteLock();
try
{
// First remove the transactions verified in the block.
foreach (Transaction tx in block.Transactions)
if (_policyChanged)
{
if (TryRemoveVerified(tx.Hash, out _)) continue;
TryRemoveUnVerified(tx.Hash, out _);
_unsortedTransactions.Clear();
_sortedTransactions.Clear();
_unverifiedTransactions.Clear();
_unverifiedSortedTransactions.Clear();
}
else
{
// First remove the transactions verified in the block.
foreach (Transaction tx in block.Transactions)
{
if (TryRemoveVerified(tx.Hash, out _)) continue;
TryRemoveUnVerified(tx.Hash, out _);
}

// Add all the previously verified transactions back to the unverified transactions
InvalidateVerifiedTransactions();
// Add all the previously verified transactions back to the unverified transactions
InvalidateVerifiedTransactions();
}
}
finally
{
Expand All @@ -357,10 +374,9 @@ internal void UpdatePoolForBlockPersisted(Block block, Snapshot 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)
if (block.Index > 0 && block.Index < Blockchain.Singleton.HeaderHeight || _policyChanged)
return;

LoadPolicy(snapshot);
ReverifyTransactions(_sortedTransactions, _unverifiedSortedTransactions,
_maxTxPerBlock, MaxSecondsToReverifyTx, snapshot);
}
Expand Down

0 comments on commit bcdc435

Please sign in to comment.