forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUT_RpcServer.SmartContract.cs
233 lines (213 loc) · 12.7 KB
/
UT_RpcServer.SmartContract.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_RpcServer.Wallet.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.AspNetCore.Http;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P.Payloads.Conditions;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.UnitTests;
using Neo.UnitTests.Extensions;
using Neo.Wallets;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace Neo.Plugins.RpcServer.Tests;
public partial class UT_RpcServer
{
static readonly string NeoTotalSupplyScript = "wh8MC3RvdGFsU3VwcGx5DBT1Y\u002BpAvCg9TQ4FxI6jBbPyoHNA70FifVtS";
static readonly string NeoTransferScript = "CxEMFPlu76Cuc\u002BbgteStE4ozsOWTNUdrDBQtYNweHko3YcnMFOes3ceblcI/lRTAHwwIdHJhbnNmZXIMFPVj6kC8KD1NDgXEjqMFs/Kgc0DvQWJ9W1I=";
static readonly UInt160 ValidatorScriptHash = Contract
.CreateSignatureRedeemScript(TestProtocolSettings.SoleNode.StandbyCommittee[0])
.ToScriptHash();
static readonly string ValidatorAddress = ValidatorScriptHash.ToAddress(ProtocolSettings.Default.AddressVersion);
static readonly UInt160 MultisigScriptHash = Contract
.CreateMultiSigRedeemScript(1, TestProtocolSettings.SoleNode.StandbyCommittee)
.ToScriptHash();
static readonly string MultisigAddress = MultisigScriptHash.ToAddress(ProtocolSettings.Default.AddressVersion);
static readonly JArray validatorSigner = [new JObject()
{
["account"] = ValidatorScriptHash.ToString(),
["scopes"] = nameof(WitnessScope.CalledByEntry),
["allowedcontracts"] = new JArray([NeoToken.NEO.Hash.ToString(), GasToken.GAS.Hash.ToString()]),
["allowedgroups"] = new JArray([TestProtocolSettings.SoleNode.StandbyCommittee[0].ToString()]),
["rules"] = new JArray([new JObject() { ["action"] = nameof(WitnessRuleAction.Allow), ["condition"] = new JObject { ["type"] = nameof(WitnessConditionType.CalledByEntry) } }]),
}];
static readonly JArray multisigSigner = [new JObject()
{
["account"] = MultisigScriptHash.ToString(),
["scopes"] = nameof(WitnessScope.CalledByEntry),
}];
[TestMethod]
public void TestInvokeFunction()
{
_rpcServer.wallet = _wallet;
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "totalSupply", new JArray([]), validatorSigner, true));
Assert.AreEqual(resp.Count, 8);
Assert.AreEqual(resp["script"], NeoTotalSupplyScript);
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
Assert.IsTrue(((JArray)resp["diagnostics"]["storagechanges"]).Count == 0);
Assert.AreEqual(resp["state"], nameof(VM.VMState.HALT));
Assert.AreEqual(resp["exception"], null);
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Integer));
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
Assert.IsTrue(resp.ContainsProperty("tx"));
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "symbol"));
Assert.AreEqual(resp.Count, 6);
Assert.IsTrue(resp.ContainsProperty("script"));
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.AreEqual(resp["state"], nameof(VM.VMState.HALT));
Assert.AreEqual(resp["exception"], null);
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.ByteString));
Assert.AreEqual(resp["stack"][0]["value"], Convert.ToBase64String(Encoding.UTF8.GetBytes("NEO")));
// This call triggers not only NEO but also unclaimed GAS
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "transfer", new JArray([
new JObject() { ["type"] = nameof(ContractParameterType.Hash160), ["value"] = MultisigScriptHash.ToString() },
new JObject() { ["type"] = nameof(ContractParameterType.Hash160), ["value"] = ValidatorScriptHash.ToString() },
new JObject() { ["type"] = nameof(ContractParameterType.Integer), ["value"] = "1" },
new JObject() { ["type"] = nameof(ContractParameterType.Any) },
]), multisigSigner, true));
Assert.AreEqual(resp.Count, 7);
Assert.AreEqual(resp["script"], NeoTransferScript);
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
Assert.IsTrue(((JArray)resp["diagnostics"]["storagechanges"]).Count == 4);
Assert.AreEqual(resp["state"], nameof(VM.VMState.HALT));
Assert.AreEqual(resp["exception"], $"The smart contract or address {MultisigScriptHash.ToString()} is not found");
JArray notifications = (JArray)resp["notifications"];
Assert.AreEqual(notifications.Count, 2);
Assert.AreEqual(notifications[0]["eventname"].AsString(), "Transfer");
Assert.AreEqual(notifications[0]["contract"].AsString(), NeoToken.NEO.Hash.ToString());
Assert.AreEqual(notifications[0]["state"]["value"][2]["value"], "1");
Assert.AreEqual(notifications[1]["eventname"].AsString(), "Transfer");
Assert.AreEqual(notifications[1]["contract"].AsString(), GasToken.GAS.Hash.ToString());
Assert.AreEqual(notifications[1]["state"]["value"][2]["value"], "50000000");
_rpcServer.wallet = null;
}
[TestMethod]
public void TestInvokeScript()
{
JObject resp = (JObject)_rpcServer.InvokeScript(new JArray(NeoTotalSupplyScript, validatorSigner, true));
Assert.AreEqual(resp.Count, 7);
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], NeoToken.NEO.Hash.ToString());
Assert.AreEqual(resp["state"], nameof(VM.VMState.HALT));
Assert.AreEqual(resp["exception"], null);
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Integer));
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
resp = (JObject)_rpcServer.InvokeScript(new JArray(NeoTransferScript));
Assert.AreEqual(resp.Count, 6);
Assert.AreEqual(resp["stack"][0]["type"], nameof(Neo.VM.Types.Boolean));
Assert.AreEqual(resp["stack"][0]["value"], false);
}
[TestMethod]
public void TestTraverseIterator()
{
// GetAllCandidates that should return 0 candidates
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
string sessionId = resp["session"].AsString();
string iteratorId = resp["stack"][0]["id"].AsString();
JArray respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
Assert.AreEqual(respArray.Count, 0);
_rpcServer.TerminateSession([sessionId]);
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]), "Unknown session");
// register candidate in snapshot
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "registerCandidate",
new JArray([new JObject()
{
["type"] = nameof(ContractParameterType.PublicKey),
["value"] = TestProtocolSettings.SoleNode.StandbyCommittee[0].ToString(),
}]), validatorSigner, true));
Assert.AreEqual(resp["state"], nameof(VM.VMState.HALT));
SnapshotCache snapshot = _neoSystem.GetSnapshotCache();
Transaction? tx = new Transaction
{
Nonce = 233,
ValidUntilBlock = NativeContract.Ledger.CurrentIndex(snapshot) + _neoSystem.Settings.MaxValidUntilBlockIncrement,
Signers = [new Signer() { Account = ValidatorScriptHash, Scopes = WitnessScope.CalledByEntry }],
Attributes = Array.Empty<TransactionAttribute>(),
Script = Convert.FromBase64String(resp["script"].AsString()),
Witnesses = null,
};
ApplicationEngine engine = ApplicationEngine.Run(tx.Script, snapshot, container: tx, settings: _neoSystem.Settings, gas: 1200_0000_0000);
engine.SnapshotCache.Commit();
// GetAllCandidates that should return 1 candidate
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
Assert.AreEqual(respArray.Count, 1);
Assert.AreEqual(respArray[0]["type"], nameof(Neo.VM.Types.Struct));
JArray value = (JArray)respArray[0]["value"];
Assert.AreEqual(value.Count, 2);
Assert.AreEqual(value[0]["type"], nameof(Neo.VM.Types.ByteString));
Assert.AreEqual(value[0]["value"], Convert.ToBase64String(TestProtocolSettings.SoleNode.StandbyCommittee[0].ToArray()));
Assert.AreEqual(value[1]["type"], nameof(Neo.VM.Types.Integer));
Assert.AreEqual(value[1]["value"], "0");
// No result when traversed again
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
Assert.AreEqual(respArray.Count, 0);
// GetAllCandidates again
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
// Insufficient result count limit
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 0]);
Assert.AreEqual(respArray.Count, 0);
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 1]);
Assert.AreEqual(respArray.Count, 1);
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 1]);
Assert.AreEqual(respArray.Count, 0);
// Mocking session timeout
Thread.Sleep((int)_rpcServerSettings.SessionExpirationTime.TotalMilliseconds + 1);
// build another session that did not expire
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
string notExpiredSessionId = resp["session"].AsString();
string notExpiredIteratorId = resp["stack"][0]["id"].AsString();
_rpcServer.OnTimer(new object());
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]), "Unknown session");
// If you want to run the following line without exception,
// DO NOT BREAK IN THE DEBUGGER, because the session expires quickly
respArray = (JArray)_rpcServer.TraverseIterator([notExpiredSessionId, notExpiredIteratorId, 1]);
Assert.AreEqual(respArray.Count, 1);
// Mocking disposal
resp = (JObject)_rpcServer.InvokeFunction(new JArray(NeoToken.NEO.Hash.ToString(), "getAllCandidates", new JArray([]), validatorSigner, true));
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
_rpcServer.Dispose_SmartContract();
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]), "Unknown session");
}
[TestMethod]
public void TestGetUnclaimedGas()
{
JObject resp = (JObject)_rpcServer.GetUnclaimedGas([MultisigAddress]);
Assert.AreEqual(resp["unclaimed"], "50000000");
Assert.AreEqual(resp["address"], MultisigAddress);
resp = (JObject)_rpcServer.GetUnclaimedGas([ValidatorAddress]);
Assert.AreEqual(resp["unclaimed"], "0");
Assert.AreEqual(resp["address"], ValidatorAddress);
}
}