-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathSmartContract.cs
200 lines (169 loc) · 7.02 KB
/
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
using Neo.SmartContract.Testing.Extensions;
using Neo.SmartContract.Testing.Storage;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Neo.SmartContract.Testing
{
public class SmartContract : IDisposable
{
internal readonly TestEngine Engine;
private readonly Type _contractType;
private readonly Dictionary<string, FieldInfo?> _notifyCache = new();
public delegate void OnRuntimeLogDelegate(string message);
public event OnRuntimeLogDelegate? OnRuntimeLog;
/// <summary>
/// Contract hash
/// </summary>
public UInt160 Hash { get; }
/// <summary>
/// Storage for this contract
/// </summary>
public SmartContractStorage Storage { get; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="initialize">Initialize object</param>
protected SmartContract(SmartContractInitialize initialize)
{
Engine = initialize.Engine;
Hash = initialize.Hash;
Storage = new SmartContractStorage(this, initialize.ContractId);
_contractType = GetType().BaseType ?? GetType(); // Mock
}
/// <summary>
/// Invoke to NeoVM
/// </summary>
/// <param name="methodName">Method name</param>
/// <param name="args">Arguments</param>
/// <returns>Object</returns>
internal StackItem Invoke(string methodName, params object[] args)
{
// Compose script
TestingSyscall? dynArgument = null;
using ScriptBuilder script = new();
ConvertArgs(script, args, ref dynArgument);
script.EmitPush(Engine.CallFlags);
script.EmitPush(methodName);
script.EmitPush(Hash);
script.EmitSysCall(ApplicationEngine.System_Contract_Call);
// Execute
return Engine.Execute(script.ToArray(), 0, dynArgument is null ? null : engine => ConfigureEngine(engine, dynArgument));
}
private void ConfigureEngine(ApplicationEngine engine, TestingSyscall testingSyscall)
{
if (engine is not TestingApplicationEngine testEngine) throw new InvalidOperationException();
testEngine.TestingSyscall = testingSyscall;
}
private void ConvertArgs(ScriptBuilder script, object[] args, ref TestingSyscall? testingSyscall)
{
if (args is null || args.Length == 0)
script.Emit(OpCode.NEWARRAY0);
else
{
for (int i = args.Length - 1; i >= 0; i--)
{
var arg = args[i];
if (arg is object[] arg2)
{
ConvertArgs(script, arg2, ref testingSyscall);
continue;
}
else if (arg is IEnumerable<object> argEnumerable)
{
ConvertArgs(script, argEnumerable.ToArray(), ref testingSyscall);
continue;
}
if (ReferenceEquals(arg, InvalidTypes.InvalidUInt160.InvalidLength) ||
ReferenceEquals(arg, InvalidTypes.InvalidUInt256.InvalidLength) ||
ReferenceEquals(arg, InvalidTypes.InvalidECPoint.InvalidLength))
{
arg = System.Array.Empty<byte>();
}
else if (ReferenceEquals(arg, InvalidTypes.InvalidUInt160.InvalidType) ||
ReferenceEquals(arg, InvalidTypes.InvalidUInt256.InvalidType) ||
ReferenceEquals(arg, InvalidTypes.InvalidECPoint.InvalidType))
{
arg = BigInteger.Zero;
}
else if (arg is InteropInterface interop)
{
// We can't send the interopInterface by an script
// We create a syscall in order to detect it and push the item
testingSyscall ??= new TestingSyscall();
script.EmitSysCall(TestingSyscall.Hash, testingSyscall.Add((e) => e.Push(interop)));
continue;
}
else if (arg is Action<ApplicationEngine> onItem)
{
// We create a syscall in order to detect it and push the item
testingSyscall ??= new TestingSyscall();
script.EmitSysCall(TestingSyscall.Hash, testingSyscall.Add((e) => onItem(e)));
continue;
}
script.EmitPush(arg);
}
script.EmitPush(args.Length);
script.Emit(OpCode.PACK);
}
}
/// <summary>
/// Invoke OnRuntimeLog
/// </summary>
/// <param name="message">Message</param>
internal void InvokeOnRuntimeLog(string message)
{
OnRuntimeLog?.Invoke(message);
}
/// <summary>
/// Invoke on notify
/// </summary>
/// <param name="eventName">Event name</param>
/// <param name="state">State</param>
internal void InvokeOnNotify(string eventName, VM.Types.Array state)
{
if (!_notifyCache.TryGetValue(eventName, out var evField))
{
var ev = _contractType.GetEvent(eventName);
if (ev is null)
{
ev = _contractType.GetEvents()
.FirstOrDefault(u => u.Name == eventName || u.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName == eventName);
if (ev is null)
{
_notifyCache[eventName] = null;
return;
}
}
_notifyCache[eventName] = evField = _contractType.GetField(ev.Name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
}
// Not found
if (evField is null) return;
if (evField.GetValue(this) is not Delegate del) return;
// Avoid parse if is not needed
var invocations = del.GetInvocationList();
if (invocations.Length == 0) return;
// Invoke
var args = state.ConvertTo(del.Method.GetParameters());
foreach (var handler in invocations)
{
handler.Method.Invoke(handler.Target, args);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt160(SmartContract value) => value.Hash;
/// <summary>
/// Release mock
/// </summary>
public void Dispose()
{
Engine.ReleaseMock(this);
}
}
}