forked from Hecate2/neo-fairy-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fairy.Utils.cs
452 lines (422 loc) · 21.3 KB
/
Fairy.Utils.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Iterators;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System.Numerics;
namespace Neo.Plugins
{
public partial class Fairy
{
[RpcMethod]
protected virtual JObject VirtualDeploy(JArray _params)
{
if (defaultFairyWallet == null)
throw new Exception("Please open a wallet before deploying a contract.");
string session = _params[0]!.AsString();
NefFile nef = Convert.FromBase64String(_params[1]!.AsString()).AsSerializable<NefFile>();
ContractManifest manifest = ContractManifest.Parse(_params[2]!.AsString());
ContractParameter? data = null;
Signer[] signers;
var param3 = _params[3]! as JObject;
if (param3 != null) // A contract parameter
{
data = ContractParameter.FromJson(param3);
signers = SignersFromJson((JArray)_params[4]!, system.Settings);
}
else
signers = SignersFromJson((JArray)_params[3]!, system.Settings);
FairySession testSession = GetOrCreateFairySession(session);
DataCache snapshot = testSession.engine.Snapshot;
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
if (data != null)
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString(), data);
else
sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", nef.ToArray(), manifest.ToJson().ToString());
script = sb.ToArray();
}
JObject json = new();
try
{
Block dummyBlock = CreateDummyBlockWithTimestamp(testSession.engine.Snapshot, system.Settings, timestamp: sessionStringToFairySession[session].engine.runtimeArgs.timestamp);
Transaction tx = defaultFairyWallet.MakeTransaction(sessionStringToFairySession[session].engine.Snapshot, script, sender: signers.Length > 0 ? signers[0].Account : defaultFairyWallet.GetAccounts().First().ScriptHash, persistingBlock: dummyBlock);
json["networkfee"] = tx.NetworkFee.ToString();
UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name);
sessionStringToFairySession[session].engine = FairyEngine.Run(script, snapshot.CreateSnapshot(), this, persistingBlock: dummyBlock, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke, oldEngine: sessionStringToFairySession[session].engine);
json["gasconsumed"] = sessionStringToFairySession[session].engine.GasConsumed.ToString();
json[session] = hash.ToString();
}
catch (InvalidOperationException ex)
{
if (ex.InnerException == null)
{
throw;
}
if (ex.InnerException.Message.StartsWith("Contract Already Exists: "))
{
json[session] = ex.InnerException.Message[^42..];
}
else
{
throw ex.InnerException;
}
}
return json;
}
/// <summary>
///
/// </summary>
/// <param name="_params">
/// multiple UInt256 or uint indexes
/// 2 indexes, 1st index <= 2nd index: get all blocks between these indexes
/// other cases: get all blocks defined by each param
/// </param>
/// <returns></returns>
[RpcMethod]
protected virtual JToken GetManyBlocks(JArray _params)
{
using var snapshot = system.GetSnapshot();
JArray result = new();
if (_params.Count == 2 && _params[0] is JNumber && _params[1] is JNumber)
for (uint i = uint.Parse(_params[0]!.AsString()); i <= uint.Parse(_params[1]!.AsString()); ++i)
result.Add(NativeContract.Ledger.GetBlock(snapshot, i).ToJson(system.Settings));
if (result.Count == 0)
foreach (JToken? key in _params)
if (key is JNumber)
result.Add(NativeContract.Ledger.GetBlock(snapshot, uint.Parse(key.AsString())).ToJson(system.Settings));
else
result.Add(NativeContract.Ledger.GetBlock(snapshot, UInt256.Parse(key!.AsString())).ToJson(system.Settings));
return result;
}
[RpcMethod]
protected virtual JToken GetContract(JArray _params)
{
string? session = _params[0]?.AsString();
UInt160 hash = UInt160.Parse(_params[1]!.AsString());
ContractState contractState = NativeContract.ContractManagement.GetContract(
session == null ? system.StoreView : sessionStringToFairySession[session].engine.Snapshot,
hash);
JObject result = contractState.ToJson();
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
contractState.Nef.Serialize(writer);
byte[] nef = ms.ToArray();
// base64 encoded bytes that can be directly saved as .nef file
result["nefFile"] = Convert.ToBase64String(nef)!;
}
return result;
}
[RpcMethod]
protected virtual JToken ListContracts(JArray _params)
{
string? session = _params[0]?.AsString();
bool verbose = _params.Count >= 2 ? _params[1]!.AsBoolean() : false;
IEnumerable<ContractState> contractStates = NativeContract.ContractManagement.ListContracts(
session == null ? system.StoreView : sessionStringToFairySession[session].engine.Snapshot);
JArray json = new();
foreach (ContractState c in contractStates)
json.Add(verbose ? c.ToJson() : new JObject { ["id"] = c.Id, ["hash"] = c.Hash.ToString() });
return json;
}
[RpcMethod]
protected virtual JObject PutStorageWithSession(JArray _params)
{
string session = _params[0]!.AsString();
UInt160 contract = UInt160.Parse(_params[1]!.AsString());
string keyBase64 = _params[2]!.AsString();
byte[] key = Convert.FromBase64String(keyBase64);
string valueBase64 = _params[3]!.AsString();
byte[] value = Convert.FromBase64String(valueBase64);
FairySession testSession = sessionStringToFairySession[session];
ContractState contractState = NativeContract.ContractManagement.GetContract(testSession.engine.Snapshot, contract);
StorageKey storageKey = new StorageKey { Id = contractState.Id, Key = key };
testSession.engine.Snapshot.Delete(storageKey);
if (value.Length > 0)
testSession.engine.Snapshot.Add(new StorageKey { Id = contractState.Id, Key = key }, new StorageItem(value));
JObject json = new();
json[keyBase64] = valueBase64;
return new JObject();
}
[RpcMethod]
protected virtual JObject GetStorageWithSession(JArray _params)
{
string? session = _params[0]?.AsString();
UInt160 contract = UInt160.Parse(_params[1]!.AsString());
string keyBase64 = _params[2]!.AsString();
byte[] key = Convert.FromBase64String(keyBase64);
ContractState contractState;
JObject json = new();
StorageItem item;
if (session == null)
{ // use current actual blockchain state, instead of a fairy session
DataCache storeView = system.StoreView;
contractState = NativeContract.ContractManagement.GetContract(storeView, contract);
item = storeView.TryGet(new StorageKey { Id = contractState.Id, Key = key });
json[keyBase64] = item == null ? null : Convert.ToBase64String(item.Value.ToArray());
return json;
}
FairyEngine oldEngine = sessionStringToFairySession[session].engine;
contractState = NativeContract.ContractManagement.GetContract(oldEngine.Snapshot, contract);
item = oldEngine.Snapshot.TryGet(new StorageKey { Id = contractState.Id, Key = key });
json[keyBase64] = item == null ? null : Convert.ToBase64String(item.Value.ToArray());
return json;
}
[RpcMethod]
protected virtual JObject FindStorageWithSession(JArray _params)
{
string? session = _params[0]?.AsString();
UInt160 contract = UInt160.Parse(_params[1]!.AsString());
string keyBase64 = _params[2]!.AsString();
byte[] prefix = Convert.FromBase64String(keyBase64);
DataCache snapshot;
if (session == null)
{ // use current actual blockchain state, instead of a fairy session
snapshot = system.StoreView;
}
else
{
FairyEngine oldEngine = sessionStringToFairySession[session].engine;
snapshot = oldEngine.Snapshot;
}
ContractState contractState = NativeContract.ContractManagement.GetContract(snapshot, contract);
JObject json = new();
foreach (var (key, value) in snapshot.Find(StorageKey.CreateSearchPrefix(contractState.Id, prefix)))
json[Convert.ToBase64String(key.Key.ToArray())] = Convert.ToBase64String(value.ToArray());
return json;
}
[RpcMethod]
protected virtual JToken Deserialize(JArray _params)
{
JArray json = new();
foreach (JToken? param in _params)
{
string dataBase64 = param!.AsString();
json.Add(BinarySerializer.Deserialize(Convert.FromBase64String(dataBase64), ExecutionEngineLimits.Default).ToJson());
}
return json;
}
[RpcMethod]
protected virtual JObject GetTime(JArray _params)
{
JObject json = new();
if (_params.Count >= 1)
json["time"] = sessionStringToFairySession[_params[0]!.AsString()].engine.GetFairyTime(); // usually you can use GetSnapshotTimeStamp instead of this method
else
json["time"] = FairyEngine.Run(new byte[] { 0x40 }, system.StoreView, this, settings: system.Settings, gas: settings.MaxGasInvoke).GetTime();
return json;
}
[RpcMethod]
protected virtual JObject SetNeoBalance(JArray _params)
{
string session = _params[0]!.AsString();
UInt160 account = UInt160.Parse(_params[1]!.AsString());
ulong balance = ulong.Parse(_params[2]!.AsString());
return SetTokenBalance(session, neoScriptHash, account, balance, Native_Prefix_Account);
}
[RpcMethod]
protected virtual JObject SetGasBalance(JArray _params)
{
string session = _params[0]!.AsString();
UInt160 account = UInt160.Parse(_params[1]!.AsString());
ulong balance = ulong.Parse(_params[2]!.AsString());
return SetTokenBalance(session, gasScriptHash, account, balance, Native_Prefix_Account);
}
[RpcMethod]
protected virtual JObject SetNep17Balance(JArray _params)
{
string session = _params[0]!.AsString();
UInt160 contract = UInt160.Parse(_params[1]!.AsString());
UInt160 account = UInt160.Parse(_params[2]!.AsString());
ulong balance = ulong.Parse(_params[3]!.AsString());
byte prefix = byte.Parse(_params.Count >= 5 ? _params[4]!.AsString() : "1");
return SetTokenBalance(session, contract, account, balance, prefix);
}
protected JObject SetTokenBalance(string session, UInt160 contract, UInt160 account, ulong balance, byte prefixAccount)
{
byte[] balanceBytes = BitConverter.GetBytes(balance);
FairyEngine oldEngine = sessionStringToFairySession[session].engine;
ContractState contractState = NativeContract.ContractManagement.GetContract(oldEngine.Snapshot, contract);
JObject json = new();
if (contract == gasScriptHash)
{
prefixAccount = Native_Prefix_Account;
byte[] key = new byte[] { prefixAccount }.Concat(account.ToArray()).ToArray();
StorageItem storage = oldEngine.Snapshot.GetAndChange(new StorageKey { Id = contractState.Id, Key = key }, () => new StorageItem(new AccountState()));
AccountState state = storage.GetInteroperable<AccountState>();
storage = oldEngine.Snapshot.GetAndChange(new StorageKey { Id = contractState.Id, Key = new byte[] { Native_Prefix_TotalSupply } }, () => new StorageItem(BigInteger.Zero));
storage.Add(balance - state.Balance);
state.Balance = balance;
json[Convert.ToBase64String(key)] = Convert.ToBase64String(balanceBytes);
return json;
}
else if (contract == neoScriptHash)
{
prefixAccount = Native_Prefix_Account;
byte[] key = new byte[] { prefixAccount }.Concat(account.ToArray()).ToArray();
StorageItem storage = oldEngine.Snapshot.GetAndChange(new StorageKey { Id = contractState.Id, Key = key }, () => new StorageItem(new NeoToken.NeoAccountState()));
NeoToken.NeoAccountState state = storage.GetInteroperable<NeoToken.NeoAccountState>();
storage = oldEngine.Snapshot.GetAndChange(new StorageKey { Id = contractState.Id, Key = new byte[] { Native_Prefix_TotalSupply } }, () => new StorageItem(BigInteger.Zero));
storage.Add(balance - state.Balance);
state.Balance = balance;
json[Convert.ToBase64String(key)] = Convert.ToBase64String(balanceBytes);
return json;
}
else
{
byte[] key = new byte[] { prefixAccount }.Concat(account.ToArray()).ToArray();
oldEngine.Snapshot.GetAndChange(new StorageKey { Id = contractState.Id, Key = key }, () => new StorageItem(balanceBytes));
json[Convert.ToBase64String(key)] = Convert.ToBase64String(balanceBytes);
return json;
}
}
[RpcMethod]
protected virtual JToken GetManyUnclaimedGas(JArray _params)
{
string? session = _params[0]?.AsString();
DataCache snapshot = session == null ? system.StoreView : sessionStringToFairySession[session].engine.Snapshot;
uint nextBlockIndex = NativeContract.Ledger.CurrentIndex(snapshot) + 1;
JObject json = new JObject();
for (int i = 1; i < _params.Count; ++i)
{
UInt160 account = UInt160.Parse(_params[i]!.AsString());
json[account.ToString()] = NativeContract.NEO.UnclaimedGas(snapshot, account, nextBlockIndex).ToString();
}
return json;
}
/// <summary>
/// Wait until the transaction is included in blocks
/// </summary>
/// <param name="_params">UInt256String; bool(verbose); waitBlockCount</param>
/// <returns></returns>
/// <exception cref="RpcException"></exception>
[RpcMethod]
protected virtual JToken AwaitConfirmedTransaction(JArray _params)
{
UInt256 hash = UInt256.Parse(_params[0]!.AsString());
bool verbose = _params.Count >= 2 && _params[1]!.AsBoolean();
uint waitBlockCount = _params.Count >= 3 ? uint.Parse(_params[2]!.AsString()) : 2;
JToken? json = GetConfirmedTransaction(hash, verbose);
if (json != null)
return json;
SemaphoreSlim signal = new SemaphoreSlim(0, 1);
uint count = 0;
CommittedHandler getConfirmedTransactionAfterCommitted = delegate (NeoSystem @system, Block @block) { json = GetConfirmedTransaction(hash, verbose); count += 1; signal.Release(); };
Blockchain.Committed += getConfirmedTransactionAfterCommitted;
while (count < waitBlockCount)
{
signal.Wait();
if (json != null)
{
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
return json;
}
}
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
throw new RpcException(RpcError.UnknownTransaction.WithData($"Transaction not found in {waitBlockCount} blocks"));
}
protected JToken? GetConfirmedTransaction(UInt256 hash, bool verbose)
{
// Do not consider anything in MemPool, because they have not been confirmed
//if (system.MemPool.TryGetValue(hash, out Transaction tx) && !verbose)
// return Convert.ToBase64String(tx.ToArray());
Transaction? tx = null;
DataCache snapshot = system.StoreView;
TransactionState state = NativeContract.Ledger.GetTransactionState(snapshot, hash);
tx ??= state?.Transaction;
if (tx is null)
return null;
else
{
if (!verbose) return Convert.ToBase64String(tx.ToArray())!;
JObject json = TransactionToJson(tx, system.Settings);
if (state is not null)
{
TrimmedBlock block = NativeContract.Ledger.GetTrimmedBlock(snapshot, NativeContract.Ledger.GetBlockHash(snapshot, state.BlockIndex));
json["blockhash"] = block.Hash.ToString();
json["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
json["blocktime"] = block.Header.Timestamp;
}
return json;
}
}
protected static JObject BlockToJson(Block block, ProtocolSettings settings)
{
JObject json = block.ToJson(settings);
json["tx"] = block.Transactions.Select(p => TransactionToJson(p, settings)).ToArray();
return json;
}
protected static JObject TransactionToJson(Transaction tx, ProtocolSettings settings)
{
JObject json = tx.ToJson(settings);
json["sysfee"] = tx.SystemFee.ToString();
json["netfee"] = tx.NetworkFee.ToString();
return json;
}
protected static JObject ToJson(StackItem item, int max)
{
JObject json = item.ToJson();
if (item is InteropInterface interopInterface && interopInterface.GetInterface<object>() is IIterator iterator)
{
JArray array = new();
while (max > 0 && iterator.Next())
{
array.Add(iterator.Value(null).ToJson());
max--;
}
json["iterator"] = array;
json["truncated"] = iterator.Next();
}
return json;
}
protected 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(),
AllowedGroups = ((JArray)u["allowedgroups"]!)?.Select(p => ECPoint.Parse(p!.AsString(), ECCurve.Secp256r1)).ToArray(),
Rules = ((JArray)u["rules"]!)?.Select(r => WitnessRule.FromJson((JObject)r!)).ToArray(),
}).ToArray();
// Validate format
_ = IO.Helper.ToByteArray(ret).AsSerializableArray<Signer>();
return ret;
}
protected 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();
}
protected static string? GetExceptionMessage(Exception exception)
{
var baseException = exception?.GetBaseException();
string returned;
if (baseException != null)
{
returned = baseException.StackTrace + "\n" + baseException.Message;
if (returned.Contains("Cannot Call Method "))
returned += "\n!!!Check whether you have written [ContractPermission(\"*\", \"*\")] in your contract!!!";
return returned;
}
else
return null;
}
}
}