-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathRpcServer.SmartContract.cs
286 lines (265 loc) · 10.9 KB
/
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.Network.RPC is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project 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 Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Iterators;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Array = System.Array;
namespace Neo.Plugins
{
partial class RpcServer
{
private readonly Dictionary<Guid, Session> sessions = new();
private Timer timer;
private void Initialize_SmartContract()
{
if (settings.SessionEnabled)
timer = new(OnTimer, null, settings.SessionExpirationTime, settings.SessionExpirationTime);
}
private void Dispose_SmartContract()
{
timer?.Dispose();
Session[] toBeDestroyed;
lock (sessions)
{
toBeDestroyed = sessions.Values.ToArray();
sessions.Clear();
}
foreach (Session session in toBeDestroyed)
session.Dispose();
}
private void OnTimer(object state)
{
List<(Guid Id, Session Session)> toBeDestroyed = new();
lock (sessions)
{
foreach (var (id, session) in sessions)
if (DateTime.UtcNow >= session.StartTime + settings.SessionExpirationTime)
toBeDestroyed.Add((id, session));
foreach (var (id, _) in toBeDestroyed)
sessions.Remove(id);
}
foreach (var (_, session) in toBeDestroyed)
session.Dispose();
}
private JObject GetInvokeResult(byte[] script, Signer[] signers = null, Witness[] witnesses = null, bool useDiagnostic = false)
{
JObject json = new();
Session session = new(system, script, signers, witnesses, settings.MaxGasInvoke, useDiagnostic ? new Diagnostic() : null);
try
{
json["script"] = Convert.ToBase64String(script);
json["state"] = session.Engine.State;
json["gasconsumed"] = session.Engine.GasConsumed.ToString();
json["exception"] = GetExceptionMessage(session.Engine.FaultException);
json["notifications"] = new JArray(session.Engine.Notifications.Select(n =>
{
var obj = new JObject();
obj["eventname"] = n.EventName;
obj["contract"] = n.ScriptHash.ToString();
obj["state"] = ToJson(n.State, session);
return obj;
}));
if (useDiagnostic)
{
Diagnostic diagnostic = (Diagnostic)session.Engine.Diagnostic;
json["diagnostics"] = new JObject()
{
["invokedcontracts"] = ToJson(diagnostic.InvocationTree.Root),
["storagechanges"] = ToJson(session.Engine.Snapshot.GetChangeSet())
};
}
try
{
json["stack"] = new JArray(session.Engine.ResultStack.Select(p => ToJson(p, session)));
}
catch (InvalidOperationException)
{
json["stack"] = "error: invalid operation";
}
if (session.Engine.State != VMState.FAULT)
{
ProcessInvokeWithWallet(json, signers);
}
}
catch
{
session.Dispose();
throw;
}
if (session.Iterators.Count == 0 || !settings.SessionEnabled)
{
session.Dispose();
}
else
{
Guid id = Guid.NewGuid();
json["session"] = id.ToString();
lock (sessions)
sessions.Add(id, session);
}
return json;
}
private static JObject ToJson(TreeNode<UInt160> node)
{
JObject json = new();
json["hash"] = node.Item.ToString();
if (node.Children.Any())
{
json["call"] = new JArray(node.Children.Select(ToJson));
}
return json;
}
private static JArray ToJson(IEnumerable<DataCache.Trackable> changes)
{
JArray array = new();
foreach (var entry in changes)
{
array.Add(new JObject
{
["state"] = entry.State.ToString(),
["key"] = Convert.ToBase64String(entry.Key.ToArray()),
["value"] = Convert.ToBase64String(entry.Item.Value.ToArray())
});
}
return array;
}
private static JObject ToJson(StackItem item, Session session)
{
JObject json = item.ToJson();
if (item is InteropInterface interopInterface && interopInterface.GetInterface<object>() is IIterator iterator)
{
Guid id = Guid.NewGuid();
session.Iterators.Add(id, iterator);
json["interface"] = nameof(IIterator);
json["id"] = id.ToString();
}
return json;
}
private static Signer[] SignersFromJson(JArray _params, ProtocolSettings settings)
{
var ret = _params.Select(u => new Signer
{
Account = AddressToScriptHash(u["account"].AsString(), settings.AddressVersion),
Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), u["scopes"]?.AsString()),
AllowedContracts = ((JArray)u["allowedcontracts"])?.Select(p => UInt160.Parse(p.AsString())).ToArray() ?? Array.Empty<UInt160>(),
AllowedGroups = ((JArray)u["allowedgroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray() ?? Array.Empty<ECPoint>(),
Rules = ((JArray)u["rules"])?.Select(r => WitnessRule.FromJson((JObject)r)).ToArray() ?? Array.Empty<WitnessRule>(),
}).ToArray();
// Validate format
_ = IO.Helper.ToByteArray(ret).AsSerializableArray<Signer>();
return ret;
}
private static Witness[] WitnessesFromJson(JArray _params)
{
return _params.Select(u => new
{
Invocation = u["invocation"]?.AsString(),
Verification = u["verification"]?.AsString()
}).Where(x => x.Invocation != null || x.Verification != null).Select(x => new Witness()
{
InvocationScript = Convert.FromBase64String(x.Invocation ?? string.Empty),
VerificationScript = Convert.FromBase64String(x.Verification ?? string.Empty)
}).ToArray();
}
[RpcMethod]
protected virtual JToken InvokeFunction(JArray _params)
{
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
string operation = _params[1].AsString();
ContractParameter[] args = _params.Count >= 3 ? ((JArray)_params[2]).Select(p => ContractParameter.FromJson((JObject)p)).ToArray() : System.Array.Empty<ContractParameter>();
Signer[] signers = _params.Count >= 4 ? SignersFromJson((JArray)_params[3], system.Settings) : null;
Witness[] witnesses = _params.Count >= 4 ? WitnessesFromJson((JArray)_params[3]) : null;
bool useDiagnostic = _params.Count >= 5 && _params[4].GetBoolean();
byte[] script;
using (ScriptBuilder sb = new())
{
script = sb.EmitDynamicCall(script_hash, operation, args).ToArray();
}
return GetInvokeResult(script, signers, witnesses, useDiagnostic);
}
[RpcMethod]
protected virtual JToken InvokeScript(JArray _params)
{
byte[] script = Convert.FromBase64String(_params[0].AsString());
Signer[] signers = _params.Count >= 2 ? SignersFromJson((JArray)_params[1], system.Settings) : null;
Witness[] witnesses = _params.Count >= 2 ? WitnessesFromJson((JArray)_params[1]) : null;
bool useDiagnostic = _params.Count >= 3 && _params[2].GetBoolean();
return GetInvokeResult(script, signers, witnesses, useDiagnostic);
}
[RpcMethod]
protected virtual JToken TraverseIterator(JArray _params)
{
Guid sid = Guid.Parse(_params[0].GetString());
Guid iid = Guid.Parse(_params[1].GetString());
int count = _params[2].GetInt32();
if (count > settings.MaxIteratorResultItems)
throw new ArgumentOutOfRangeException(nameof(count));
Session session;
lock (sessions)
{
session = sessions[sid];
session.ResetExpiration();
}
IIterator iterator = session.Iterators[iid];
JArray json = new();
while (count-- > 0 && iterator.Next())
json.Add(iterator.Value(null).ToJson());
return json;
}
[RpcMethod]
protected virtual JToken TerminateSession(JArray _params)
{
Guid sid = Guid.Parse(_params[0].GetString());
Session session;
bool result;
lock (sessions)
result = sessions.Remove(sid, out session);
if (result) session.Dispose();
return result;
}
[RpcMethod]
protected virtual JToken GetUnclaimedGas(JArray _params)
{
string address = _params[0].AsString();
JObject json = new();
UInt160 script_hash;
try
{
script_hash = AddressToScriptHash(address, system.Settings.AddressVersion);
}
catch
{
script_hash = null;
}
if (script_hash == null)
throw new RpcException(-100, "Invalid address");
var snapshot = system.StoreView;
json["unclaimed"] = NativeContract.NEO.UnclaimedGas(snapshot, script_hash, NativeContract.Ledger.CurrentIndex(snapshot) + 1).ToString();
json["address"] = script_hash.ToAddress(system.Settings.AddressVersion);
return json;
}
static string GetExceptionMessage(Exception exception)
{
return exception?.GetBaseException().Message;
}
}
}