forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUT_TransactionVerificationContext.cs
131 lines (118 loc) · 6.45 KB
/
UT_TransactionVerificationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_TransactionVerificationContext.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_TransactionVerificationContext
{
[ClassInitialize]
public static void TestSetup(TestContext ctx)
{
_ = TestBlockchain.TheNeoSystem;
}
private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
{
Random random = new();
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new();
mock.Setup(p => p.VerifyStateDependent(It.IsAny<ProtocolSettings>(), It.IsAny<ClonedCache>(), It.IsAny<TransactionVerificationContext>(), It.IsAny<IEnumerable<Transaction>>())).Returns(VerifyResult.Succeed);
mock.Setup(p => p.VerifyStateIndependent(It.IsAny<ProtocolSettings>())).Returns(VerifyResult.Succeed);
mock.Object.Script = randomBytes;
mock.Object.NetworkFee = networkFee;
mock.Object.SystemFee = systemFee;
mock.Object.Signers = new[] { new Signer { Account = UInt160.Zero } };
mock.Object.Attributes = Array.Empty<TransactionAttribute>();
mock.Object.Witnesses = new[]
{
new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = Array.Empty<byte>()
}
};
return mock.Object;
}
[TestMethod]
public async Task TestDuplicateOracle()
{
// Fake balance
var snapshot = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
await NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
_ = NativeContract.GAS.Mint(engine, UInt160.Zero, 8, false);
// Test
TransactionVerificationContext verificationContext = new();
var tx = CreateTransactionWithFee(1, 2);
tx.Attributes = new TransactionAttribute[] { new OracleResponse() { Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = Array.Empty<byte>() } };
var conflicts = new List<Transaction>();
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
tx = CreateTransactionWithFee(2, 1);
tx.Attributes = new TransactionAttribute[] { new OracleResponse() { Code = OracleResponseCode.ConsensusUnreachable, Id = 1, Result = Array.Empty<byte>() } };
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeFalse();
}
[TestMethod]
public async Task TestTransactionSenderFee()
{
var snapshot = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
await NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
_ = NativeContract.GAS.Mint(engine, UInt160.Zero, 8, true);
TransactionVerificationContext verificationContext = new();
var tx = CreateTransactionWithFee(1, 2);
var conflicts = new List<Transaction>();
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeFalse();
verificationContext.RemoveTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeFalse();
}
[TestMethod]
public async Task TestTransactionSenderFeeWithConflicts()
{
var snapshot = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
await NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
_ = NativeContract.GAS.Mint(engine, UInt160.Zero, 3 + 3 + 1, true); // balance is enough for 2 transactions and 1 GAS is left.
TransactionVerificationContext verificationContext = new();
var tx = CreateTransactionWithFee(1, 2);
var conflictingTx = CreateTransactionWithFee(1, 1); // costs 2 GAS
var conflicts = new List<Transaction>();
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue();
verificationContext.AddTransaction(tx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeFalse();
conflicts.Add(conflictingTx);
verificationContext.CheckTransaction(tx, conflicts, snapshot).Should().BeTrue(); // 1 GAS is left on the balance + 2 GAS is free after conflicts removal => enough for one more trasnaction.
}
}
}