-
Notifications
You must be signed in to change notification settings - Fork 4
/
Fairy.Tester.cs
243 lines (227 loc) · 13 KB
/
Fairy.Tester.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
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System.Collections.Concurrent;
using System.Numerics;
using System.Text;
namespace Neo.Plugins
{
public partial class Fairy
{
readonly ConcurrentQueue<LogEventArgs> logs = new();
public UInt160 neoScriptHash = UInt160.Parse("0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5");
public UInt160 gasScriptHash = UInt160.Parse("0xd2a4cff31913016155e38e474a2c06d08be276cf");
const byte Native_Prefix_Account = 20;
const byte Native_Prefix_TotalSupply = 11;
[RpcMethod]
protected virtual JObject InvokeFunctionWithSession(JArray _params)
{
string session = _params[0]!.AsString();
bool writeSnapshot = _params[1]!.AsBoolean();
UInt160 script_hash = UInt160.Parse(_params[2]!.AsString());
string operation = _params[3]!.AsString();
ContractParameter[] args = _params.Count >= 5 ? ((JArray)_params[4]!).Select(p => ContractParameter.FromJson((JObject)p!)).ToArray() : System.Array.Empty<ContractParameter>();
Signer[]? signers = _params.Count >= 6 ? SignersFromJson((JArray)_params[5]!, system.Settings) : null;
Witness[]? witnesses = _params.Count >= 7 ? WitnessesFromJson((JArray)_params[6]!) : null;
byte[] script;
using (ScriptBuilder sb = new())
{
script = sb.EmitDynamicCall(script_hash, operation, args).ToArray();
}
return GetInvokeResultWithSession(session, writeSnapshot, script, signers, witnesses);
}
[RpcMethod]
protected virtual JObject InvokeManyWithSession(JArray _params)
{
string session = _params[0]!.AsString();
bool writeSnapshot = _params[1]!.AsBoolean();
Signer[]? signers = _params.Count >= 4 ? SignersFromJson((JArray)_params[3]!, system.Settings) : null;
Witness[]? witnesses = _params.Count >= 5 ? WitnessesFromJson((JArray)_params[4]!) : null;
byte[] script = Array.Empty<byte>();
using (ScriptBuilder sb = new())
{
foreach (JArray? invokeParams in (JArray)_params[2]!)
{
UInt160 script_hash = UInt160.Parse(invokeParams![0]!.AsString());
string operation = invokeParams[1]!.AsString();
ContractParameter[] args = invokeParams.Count >= 3 ? ((JArray)invokeParams[2]!).Select(p => ContractParameter.FromJson((JObject)p!)).ToArray() : System.Array.Empty<ContractParameter>();
sb.EmitDynamicCall(script_hash, operation, args);
}
script = sb.ToArray();
}
return GetInvokeResultWithSession(session, writeSnapshot, script, signers, witnesses);
}
[RpcMethod]
protected virtual JObject InvokeScriptWithSession(JArray _params)
{
string session = _params[0]!.AsString();
bool writeSnapshot = _params[1]!.AsBoolean();
byte[] script = Convert.FromBase64String(_params[2]!.AsString());
Signer[]? signers = _params.Count >= 4 ? SignersFromJson((JArray)_params[3]!, system.Settings) : null;
Witness[]? witnesses = _params.Count >= 5 ? WitnessesFromJson((JArray)_params[4]!) : null;
return GetInvokeResultWithSession(session, writeSnapshot, script, signers, witnesses);
}
private void CacheLog(object sender, LogEventArgs logEventArgs)
{
logs.Enqueue(logEventArgs);
}
private JObject GetInvokeResultWithSession(string session, bool writeSnapshot, byte[] script, Signer[]? signers = null, Witness[]? witnesses = null)
{
FairySession testSession = GetOrCreateFairySession(session);
FairyEngine oldEngine = testSession.engine, newEngine;
logs.Clear();
Random random = new();
Transaction? tx = signers == null ? null : new Transaction
{
Nonce = (uint)random.Next(),
ValidUntilBlock = NativeContract.Ledger.CurrentIndex(testSession.engine.Snapshot) + system.Settings.MaxValidUntilBlockIncrement,
Signers = signers,
Attributes = Array.Empty<TransactionAttribute>(),
Script = script,
Witnesses = witnesses
};
FairyEngine.Log += CacheLog!;
newEngine = FairyEngine.Run(script, testSession.engine.Snapshot.CreateSnapshot(), this, container: tx, settings: system.Settings, gas: settings.MaxGasInvoke, oldEngine: oldEngine);
FairyEngine.Log -= CacheLog!;
if (writeSnapshot && newEngine.State == VMState.HALT)
sessionStringToFairySession[session].engine = newEngine;
JObject json = new();
JArray notifications = new();
for (int i = newEngine.Notifications.Count - 1; i >= 0; i--)
{
NotifyEventArgs notification = newEngine.Notifications[i];
JObject notificationJson = new();
notificationJson["tx"] = notification.ScriptContainer.Hash.ToString();
notificationJson["scripthash"] = notification.ScriptHash.ToString();
notificationJson["contractname"] = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, notification.ScriptHash)?.Manifest.Name;
notificationJson["eventname"] = notification.EventName;
notificationJson["eventargs"] = notification.State.ToJson();
notifications.Add(notificationJson);
if (newEngine.Notifications[i].EventName == "OracleRequest")
{
int oracleContractId = NativeContract.Oracle.Id;
ulong requestId = (ulong)(new BigInteger(newEngine.Snapshot.TryGet(new StorageKey { Id = oracleContractId, Key = new byte[] { 9 } }).Value.ToArray()) - 1);
OracleRequest oracleRequest = newEngine.Snapshot.TryGet(new KeyBuilder(oracleContractId, 7).AddBigEndian(requestId)).GetInteroperable<OracleRequest>();
//if (!Uri.TryCreate(oracleRequest.Url, UriKind.Absolute, out var uri))
// break;
//if (uri.Scheme != "https")
//{
// ConsoleHelper.Info($"WARNING: uri scheme {uri.Scheme} not supported by fairy.");
// break;
//}
JArray oracleRequests;
if (!json.ContainsProperty("oraclerequests"))
{
oracleRequests = new JArray();
json["oraclerequests"] = oracleRequests;
}
else
{
oracleRequests = (JArray)json["oraclerequests"]!;
}
oracleRequests.Add(oracleRequest.ToStackItem(new ReferenceCounter()).ToJson());
}
}
if (notifications.Count > 0) json["notifications"] = notifications;
json["script"] = Convert.ToBase64String(script);
json["state"] = newEngine.State;
json["gasconsumed"] = newEngine.GasConsumed.ToString();
json["exception"] = GetExceptionMessage(newEngine.FaultException);
if (json["exception"] != null)
{
StringBuilder traceback = new();
try { if (newEngine.CallingScriptHash != null) traceback.Append($"CallingScriptHash={newEngine.CallingScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CallingScriptHash)?.Manifest.Name}]\r\n"); } catch { }
try { traceback.Append($"CurrentScriptHash={newEngine.CurrentScriptHash}[{NativeContract.ContractManagement.GetContract(newEngine.Snapshot, newEngine.CurrentScriptHash)?.Manifest.Name}]\r\n"); } catch { }
try { traceback.Append($"EntryScriptHash={newEngine.EntryScriptHash}\r\n"); } catch { }
traceback.Append(newEngine.FaultException.StackTrace);
foreach (Neo.VM.ExecutionContext context in newEngine.InvocationStack.Reverse())
{
UInt160 contextScriptHash = context.GetScriptHash();
string? contextContractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, contextScriptHash)?.Manifest.Name;
//try
{
if (contractScriptHashToAllInstructionPointerToSourceLineNum.ContainsKey(contextScriptHash) && contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash].ContainsKey((uint)context.InstructionPointer))
{
string sourceCodeTraceback = "";
SourceFilenameAndLineNum sourceCode = contractScriptHashToAllInstructionPointerToSourceLineNum[contextScriptHash][(uint)context.InstructionPointer];
sourceCodeTraceback += $"\r\nFile {sourceCode.sourceFilename}, line {sourceCode.lineNum}: {sourceCode.sourceContent}";
traceback.Append(sourceCodeTraceback);
}
}
//catch (Exception _) {; }
traceback.Append($"\r\n\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]");
}
traceback.Append($"\r\n{json["exception"]!.GetString()}");
if (!logs.IsEmpty)
traceback.Append($"\r\n-------Logs-------({logs.Count})");
foreach (LogEventArgs log in logs)
{
string? contractName = NativeContract.ContractManagement.GetContract(newEngine.Snapshot, log.ScriptHash)?.Manifest.Name;
traceback.Append($"\r\n[{log.ScriptHash}] {contractName}: {log.Message}");
}
json["traceback"] = traceback.ToString();
}
//try
//{
json["stack"] = new JArray(newEngine.ResultStack.Select(p => ToJson(p, settings.MaxIteratorResultItems)));
//}
//catch (InvalidOperationException)
//{
// json["stack"] = "error: invalid operation";
//}
if (newEngine.State != VMState.FAULT)
{
if (witnesses == null)
ProcessInvokeWithWalletAndSnapshot(oldEngine, script, json, signers, block: CreateDummyBlockWithTimestamp(testSession.engine.Snapshot, system.Settings, timestamp: testSession.timestamp));
else
{
Wallet signatureWallet = oldEngine.runtimeArgs.fairyWallet == null ? defaultFairyWallet : oldEngine.runtimeArgs.fairyWallet;
json["tx"] = Convert.ToBase64String(tx.ToArray());
json["networkfee"] = tx.CalculateNetworkFee(oldEngine.Snapshot, system.Settings, (a) => signatureWallet.GetAccount(a)?.Contract?.Script).ToString();
}
}
return json;
}
private void ProcessInvokeWithWalletAndSnapshot(FairyEngine engine, byte[] script, JObject result, Signer[]? signers = null, Block? block = null)
{
Wallet signatureWallet = engine.runtimeArgs.fairyWallet == null ? defaultFairyWallet : engine.runtimeArgs.fairyWallet;
if (signatureWallet == null || signers == null) return;
Signer[] witnessSigners = signers;
if (witnessSigners.Length <= 0) return;
UInt160? sender = signers.Length > 0 ? signers[0].Account : null;
Transaction tx;
try
{
tx = signatureWallet.MakeTransaction(engine.Snapshot.CreateSnapshot(), script, sender, witnessSigners, maxGas: settings.MaxGasInvoke, persistingBlock: block);
}
catch //(Exception e)
{
// result["exception"] = GetExceptionMessage(e);
return;
}
DataCache snapshotForSignature = engine.Snapshot.CreateSnapshot();
ContractParametersContext context = new(snapshotForSignature, tx, system.Settings.Network);
signatureWallet.Sign(context);
try
{
tx.Witnesses = context.GetWitnesses();
}
catch
{
// When no valid signature is given, we can only try to simulate a transaction with a single witness
tx.Witnesses = defaultWitness;
}
result["tx"] = Convert.ToBase64String(tx.ToArray());
result["networkfee"] = tx.CalculateNetworkFee(snapshotForSignature, system.Settings, (a) => signatureWallet.GetAccount(a)?.Contract?.Script).ToString();
if (!context.Completed)
{
result["pendingsignature"] = context.ToJson();
}
}
}
}