-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Keep track of sender fee to avoid duplicate computation * Code optimization * Optimize * Optimize * Optimize * Code optimization * Correction * Renaming currentFee to totalSenderFeeFromPool * Renaming on Verify as well * Add consideration for null Transactions * Move sender fee recording systems to class SendersMonitor * Code optimization * Capitalize public items * Code optimization * Code optimization * Optimization * Code optimization
- Loading branch information
Showing
9 changed files
with
136 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Neo.Ledger; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.Persistence; | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Neo.UnitTests.Ledger | ||
{ | ||
[TestClass] | ||
public class UT_SendersFeeMonitor | ||
{ | ||
private Transaction CreateTransactionWithFee(long networkFee, long systemFee) | ||
{ | ||
Random random = new Random(); | ||
var randomBytes = new byte[16]; | ||
random.NextBytes(randomBytes); | ||
Mock<Transaction> mock = new Mock<Transaction>(); | ||
mock.Setup(p => p.Reverify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true); | ||
mock.Setup(p => p.Verify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true); | ||
mock.Object.Script = randomBytes; | ||
mock.Object.Sender = UInt160.Zero; | ||
mock.Object.NetworkFee = networkFee; | ||
mock.Object.SystemFee = systemFee; | ||
mock.Object.Attributes = new TransactionAttribute[0]; | ||
mock.Object.Cosigners = new Cosigner[0]; | ||
mock.Object.Witnesses = new[] | ||
{ | ||
new Witness | ||
{ | ||
InvocationScript = new byte[0], | ||
VerificationScript = new byte[0] | ||
} | ||
}; | ||
return mock.Object; | ||
} | ||
|
||
[TestMethod] | ||
public void TestMemPoolSenderFee() | ||
{ | ||
Transaction transaction = CreateTransactionWithFee(1, 2); | ||
SendersFeeMonitor sendersFeeMonitor = new SendersFeeMonitor(); | ||
sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(0); | ||
sendersFeeMonitor.AddSenderFee(transaction); | ||
sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(3); | ||
sendersFeeMonitor.AddSenderFee(transaction); | ||
sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(6); | ||
sendersFeeMonitor.RemoveSenderFee(transaction); | ||
sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(3); | ||
sendersFeeMonitor.RemoveSenderFee(transaction); | ||
sendersFeeMonitor.GetSenderFee(transaction.Sender).Should().Be(0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Neo.Network.P2P.Payloads; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using System.Threading; | ||
|
||
namespace Neo.Ledger | ||
{ | ||
public class SendersFeeMonitor | ||
{ | ||
private readonly ReaderWriterLockSlim _senderFeeRwLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); | ||
|
||
/// <summary> | ||
/// Store all verified unsorted transactions' senders' fee currently in the memory pool. | ||
/// </summary> | ||
private readonly Dictionary<UInt160, BigInteger> _senderFee = new Dictionary<UInt160, BigInteger>(); | ||
|
||
public BigInteger GetSenderFee(UInt160 sender) | ||
{ | ||
_senderFeeRwLock.EnterReadLock(); | ||
if (!_senderFee.TryGetValue(sender, out var value)) | ||
value = BigInteger.Zero; | ||
_senderFeeRwLock.ExitReadLock(); | ||
return value; | ||
} | ||
|
||
public void AddSenderFee(Transaction tx) | ||
{ | ||
_senderFeeRwLock.EnterWriteLock(); | ||
if (_senderFee.TryGetValue(tx.Sender, out var value)) | ||
_senderFee[tx.Sender] = value + tx.SystemFee + tx.NetworkFee; | ||
else | ||
_senderFee.Add(tx.Sender, tx.SystemFee + tx.NetworkFee); | ||
_senderFeeRwLock.ExitWriteLock(); | ||
} | ||
|
||
public void RemoveSenderFee(Transaction tx) | ||
{ | ||
_senderFeeRwLock.EnterWriteLock(); | ||
if ((_senderFee[tx.Sender] -= tx.SystemFee + tx.NetworkFee) == 0) _senderFee.Remove(tx.Sender); | ||
_senderFeeRwLock.ExitWriteLock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters