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

Relocate transaction verification #1507

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
1d0ef09
Merge pull request #13 from neo-project/master
Qiao-Jin Mar 19, 2020
b14a589
Relocate transaction verifcation
Mar 20, 2020
e81cc6b
Merge pull request #17 from neo-project/master
Qiao-Jin Mar 23, 2020
915794e
Merge pull request #23 from neo-project/master
Qiao-Jin Mar 24, 2020
58b6731
Merge pull request #29 from neo-project/master
Qiao-Jin Mar 24, 2020
df5a99f
Merge branch 'master' of https://github.com/neo-project/neo into neo-…
Mar 25, 2020
28e3f48
Merge branch 'neo-project-master' into relocate_transaction_verification
Mar 25, 2020
9f2f7c6
Code optimization
Mar 25, 2020
241a92d
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Mar 27, 2020
ab4bb26
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Mar 27, 2020
fcaf7e4
Seperate sender fee check from transaction verification
Mar 30, 2020
2cfa56b
Merge branch 'relocate_transaction_verification' of https://github.co…
Mar 30, 2020
f7ba395
Format modification
Mar 30, 2020
eedb714
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Mar 31, 2020
b9f3a46
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 2, 2020
3828ab2
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 3, 2020
e407aad
Function rename and add parallel strategy
Apr 3, 2020
382605e
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 3, 2020
6896500
Code optimization
Apr 6, 2020
b3b8c25
Code optimization
Apr 7, 2020
27ef0af
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 8, 2020
0173cd3
Code optimization
Apr 8, 2020
78b304f
Format optimization
Apr 8, 2020
eb6f41f
Code optimization
Apr 8, 2020
35d7995
Merge branch 'master' of https://github.com/neo-project/neo into neo-…
Apr 10, 2020
08fa2a2
Merge branch 'neo-project-master' into relocate_transaction_verification
Apr 10, 2020
e65623a
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 13, 2020
1e63267
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 20, 2020
a0dcaf0
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 26, 2020
7b605af
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Apr 27, 2020
4dad4d0
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 6, 2020
729d2ce
Fix VerifyWitnesses
May 6, 2020
fcc3c12
Revert "Fix VerifyWitnesses"
May 6, 2020
e578754
Rewrite verify_witness and corresponding logic
May 7, 2020
d228a51
code format
May 7, 2020
c08dc91
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 7, 2020
51e785c
Add statedependent to witness
May 9, 2020
a494053
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 9, 2020
83ee355
Code optimization
May 9, 2020
5c10326
Update src/neo/Network/P2P/Payloads/Witness.cs
Qiao-Jin May 9, 2020
f78503e
Update src/neo/Network/P2P/Payloads/Witness.cs
Qiao-Jin May 9, 2020
64d2a3f
Update src/neo/Network/P2P/Payloads/Witness.cs
Qiao-Jin May 9, 2020
5bea75d
Revert "Update src/neo/Network/P2P/Payloads/Witness.cs"
May 9, 2020
a4b38f0
Revert "Update src/neo/Network/P2P/Payloads/Witness.cs"
May 9, 2020
f119e18
Revert "Update src/neo/Network/P2P/Payloads/Witness.cs"
May 9, 2020
47567ae
optimize Witness
May 9, 2020
e0a5752
optimize witness and IVerifiable.verifyWitness
May 9, 2020
406e1d0
fix tx.Verify
May 9, 2020
116b59a
optimize code
May 9, 2020
fb9853a
optimize tx.GetScriptHashesForVerifying
May 9, 2020
b8c139e
Remove verifyForEachBlock
May 9, 2020
33a70fc
add VerifyResult.InvalidWitness
May 9, 2020
e263a06
Disallow witness.verificationScript to access the snapshot
May 9, 2020
2016da9
Optimize tx.VerifyStateDependent and verifiable.VerifyWitness
May 9, 2020
0bd4479
format
May 9, 2020
8666976
fix VerifyWitness
May 10, 2020
4f832ba
optimize tx.verify
May 10, 2020
5d1a218
optimize verifywitness
May 10, 2020
de81ddd
reset InteropService
May 11, 2020
0638e0b
add UT
May 11, 2020
4d1e1ac
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 12, 2020
9696c6a
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 15, 2020
c32f0fa
Split func OnInventoryReceived to avoid dirty write
May 15, 2020
be04b26
Merge branch 'master' into relocate_transaction_verification
shargon May 15, 2020
f9f2661
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 18, 2020
80ffb08
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 19, 2020
c198d6a
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 26, 2020
f3a5390
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin May 27, 2020
7ab56c3
Renew UT
May 27, 2020
235c9b9
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Jun 3, 2020
689d269
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Jul 10, 2020
a06c26a
Merge branch 'local_master' into relocate_transaction_verification
Aug 7, 2020
9d81c55
Check transaction size before creating task
Aug 7, 2020
13df078
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Aug 7, 2020
151dbac
Add missing note
Aug 7, 2020
1e76ed3
Merge branch 'relocate_transaction_verification' of https://github.co…
Aug 7, 2020
0ab3d71
Fix gas
shargon Aug 7, 2020
c8be765
Re-fix
shargon Aug 7, 2020
8f9bd7e
Merge branch 'master' into relocate_transaction_verification
shargon Aug 7, 2020
868ff31
Redefine WitnessFlag
Aug 7, 2020
a77ea11
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Aug 10, 2020
5288f27
Change multi task to actor pool
Aug 12, 2020
8c6ce68
Format correction
Aug 12, 2020
1320c78
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Aug 12, 2020
16fb3de
Update Transaction.cs
erikzhang Aug 13, 2020
d9c80ec
Rename
erikzhang Aug 13, 2020
90027d9
Update Witness.cs
erikzhang Aug 13, 2020
06e5d19
Merge branch 'master' into relocate_transaction_verification
Qiao-Jin Aug 13, 2020
f3b691a
Public transaction router
Aug 13, 2020
e6880d6
Fix and optimize
erikzhang Aug 14, 2020
c944e86
Move namespace
erikzhang Aug 14, 2020
cd0cd41
Move namespace
erikzhang Aug 14, 2020
3811cf6
Optimize
erikzhang Aug 14, 2020
f079a0f
Use the correct CallFlags
erikzhang Aug 14, 2020
55e3597
Merge branch 'master' into relocate_transaction_verification
erikzhang Aug 14, 2020
ea94018
Merge branch 'master' into relocate_transaction_verification
shargon Aug 16, 2020
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
3 changes: 2 additions & 1 deletion src/neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ internal ConsensusService(IActorRef localNode, IActorRef taskManager, ConsensusC

private bool AddTransaction(Transaction tx, bool verify)
{
if (verify && tx.Verify(context.Snapshot, context.SendersFeeMonitor.GetSenderFee(tx.Sender)) != VerifyResult.Succeed)
if (verify && tx.VerifyStateDependent(context.Snapshot, context.SendersFeeMonitor.GetSenderFee(tx.Sender)) != VerifyResult.Succeed
&& tx.VerifyStateIndependent(context.Snapshot) != VerifyResult.Succeed)
{
Log($"Invalid transaction: {tx.Hash}{Environment.NewLine}{tx.ToArray().ToHexString()}", LogLevel.Warning);
RequestChangeView(ChangeViewReason.TxInvalid);
Expand Down
8 changes: 5 additions & 3 deletions src/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class RelayResult { public IInventory Inventory; public VerifyResult Resu
private readonly Dictionary<UInt256, Block> block_cache = new Dictionary<UInt256, Block>();
private readonly Dictionary<uint, LinkedList<Block>> block_cache_unverified = new Dictionary<uint, LinkedList<Block>>();
internal readonly RelayCache ConsensusRelayCache = new RelayCache(100);
private SnapshotView currentSnapshot;
public SnapshotView currentSnapshot;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

public IStore Store { get; }
public ReadOnlyView View { get; }
Expand Down Expand Up @@ -279,7 +279,9 @@ private void OnFillMemoryPool(IEnumerable<Transaction> transactions)
// First remove the tx if it is unverified in the pool.
MemPool.TryRemoveUnVerified(tx.Hash, out _);
// Verify the the transaction
if (tx.Verify(currentSnapshot, MemPool.SendersFeeMonitor.GetSenderFee(tx.Sender)) != VerifyResult.Succeed)
if (tx.VerifyStateDependent(currentSnapshot, MemPool.SendersFeeMonitor.GetSenderFee(tx.Sender)) != VerifyResult.Succeed)
continue;
if (tx.VerifyStateIndependent(currentSnapshot) != VerifyResult.Succeed)
continue;
// Add to the memory pool
MemPool.TryAdd(tx.Hash, tx);
Expand Down Expand Up @@ -419,7 +421,7 @@ private VerifyResult OnNewTransaction(Transaction transaction)
{
if (ContainsTransaction(transaction.Hash)) return VerifyResult.AlreadyExists;
if (!MemPool.CanTransactionFitInPool(transaction)) return VerifyResult.OutOfMemory;
VerifyResult reason = transaction.Verify(currentSnapshot, MemPool.SendersFeeMonitor.GetSenderFee(transaction.Sender));
VerifyResult reason = transaction.VerifyStateDependent(currentSnapshot, MemPool.SendersFeeMonitor.GetSenderFee(transaction.Sender));
if (reason != VerifyResult.Succeed) return reason;
if (!MemPool.TryAdd(transaction.Hash, transaction)) return VerifyResult.OutOfMemory;
return VerifyResult.Succeed;
Expand Down
3 changes: 2 additions & 1 deletion src/neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ private int ReverifyTransactions(SortedSet<PoolItem> verifiedSortedTxPool,
// Since unverifiedSortedTxPool is ordered in an ascending manner, we take from the end.
foreach (PoolItem item in unverifiedSortedTxPool.Reverse().Take(count))
{
if (item.Tx.VerifyForEachBlock(snapshot, SendersFeeMonitor.GetSenderFee(item.Tx.Sender)) == VerifyResult.Succeed)
if (item.Tx.VerifyStateDependent(snapshot, SendersFeeMonitor.GetSenderFee(item.Tx.Sender)) == VerifyResult.Succeed
&& item.Tx.VerifyForEachBlock(snapshot) == VerifyResult.Succeed)
{
reverifiedItems.Add(item);
SendersFeeMonitor.AddSenderFee(item.Tx);
Expand Down
20 changes: 13 additions & 7 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,25 @@ public static Transaction FromJson(JObject json)

bool IInventory.Verify(StoreView snapshot)
{
return Verify(snapshot, BigInteger.Zero) == VerifyResult.Succeed;
return VerifyStateDependent(snapshot, BigInteger.Zero) == VerifyResult.Succeed
&& VerifyStateIndependent(snapshot) == VerifyResult.Succeed;
}

public virtual VerifyResult VerifyForEachBlock(StoreView snapshot, BigInteger totalSenderFeeFromPool)
public virtual VerifyResult VerifyStateDependent(StoreView snapshot, BigInteger totalSenderFeeFromPool)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, Sender);
BigInteger fee = SystemFee + NetworkFee + totalSenderFeeFromPool;
if (balance < fee) return VerifyResult.InsufficientFunds;
else return VerifyResult.Succeed;
}

public virtual VerifyResult VerifyForEachBlock(StoreView snapshot)
{
if (ValidUntilBlock <= snapshot.Height || ValidUntilBlock > snapshot.Height + MaxValidUntilBlockIncrement)
return VerifyResult.Expired;
UInt160[] hashes = GetScriptHashesForVerifying(snapshot);
if (NativeContract.Policy.GetBlockedAccounts(snapshot).Intersect(hashes).Any())
return VerifyResult.PolicyFail;
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, Sender);
BigInteger fee = SystemFee + NetworkFee + totalSenderFeeFromPool;
if (balance < fee) return VerifyResult.InsufficientFunds;
if (hashes.Length != Witnesses.Length) return VerifyResult.Invalid;
for (int i = 0; i < hashes.Length; i++)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -285,9 +291,9 @@ public virtual VerifyResult VerifyForEachBlock(StoreView snapshot, BigInteger to
return VerifyResult.Succeed;
}

public virtual VerifyResult Verify(StoreView snapshot, BigInteger totalSenderFeeFromPool)
public virtual VerifyResult VerifyStateIndependent(StoreView snapshot)
Qiao-Jin marked this conversation as resolved.
Show resolved Hide resolved
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
VerifyResult result = VerifyForEachBlock(snapshot, totalSenderFeeFromPool);
VerifyResult result = VerifyForEachBlock(snapshot);
if (result != VerifyResult.Succeed) return result;
int size = Size;
if (size > MaxTransactionSize) return VerifyResult.Invalid;
Expand Down
14 changes: 12 additions & 2 deletions src/neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Neo.Network.P2P
{
Expand Down Expand Up @@ -134,8 +135,17 @@ private void OnMessage(Message msg)
OnPongMessageReceived((PingPayload)msg.Payload);
break;
case MessageCommand.Transaction:
if (msg.Payload.Size <= Transaction.MaxTransactionSize)
OnInventoryReceived((Transaction)msg.Payload);
Task.Run(() =>
{
if (msg.Payload.Size <= Transaction.MaxTransactionSize)
{
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
Transaction tx = (Transaction)msg.Payload;
if (tx.VerifyStateIndependent(Blockchain.Singleton.currentSnapshot) == VerifyResult.Succeed)
{
OnInventoryReceived(tx);
}
}
});
break;
case MessageCommand.Verack:
case MessageCommand.Version:
Expand Down
10 changes: 6 additions & 4 deletions tests/neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ private Transaction CreateTransactionWithFee(long fee)
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateDependent(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateIndependent(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = fee;
Expand All @@ -99,8 +100,9 @@ private Transaction CreateTransactionWithFeeAndBalanceVerify(long fee)
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
UInt160 sender = UInt160.Zero;
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns((StoreView snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateDependent(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns((StoreView snapshot, BigInteger amount) => NativeContract.GAS.BalanceOf(snapshot, sender) >= amount + fee ? VerifyResult.Succeed : VerifyResult.InsufficientFunds);
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateIndependent(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = sender;
mock.Object.NetworkFee = fee;
Expand Down
5 changes: 3 additions & 2 deletions tests/neo.UnitTests/Ledger/UT_SendersFeeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.Verify(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateDependent(It.IsAny<StoreView>(), It.IsAny<BigInteger>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyForEachBlock(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateIndependent(It.IsAny<StoreView>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = networkFee;
Expand Down
3 changes: 2 additions & 1 deletion tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ public void Transaction_Reverify_Hashes_Length_Unequal_To_Witnesses_Length()
};
UInt160[] hashes = txSimple.GetScriptHashesForVerifying(snapshot);
Assert.AreEqual(2, hashes.Length);
Assert.AreNotEqual(VerifyResult.Succeed, txSimple.VerifyForEachBlock(snapshot, BigInteger.Zero));
Assert.AreNotEqual(VerifyResult.Succeed, txSimple.VerifyStateDependent(snapshot, BigInteger.Zero));
Assert.AreNotEqual(VerifyResult.Succeed, txSimple.VerifyForEachBlock(snapshot));
}

[TestMethod]
Expand Down