-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathContractMethodMetadata.cs
More file actions
170 lines (158 loc) · 8.31 KB
/
ContractMethodMetadata.cs
File metadata and controls
170 lines (158 loc) · 8.31 KB
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
// Copyright (C) 2015-2026 The Neo Project.
//
// ContractMethodMetadata.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 Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.VM.Types;
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;
using Buffer = Neo.VM.Types.Buffer;
namespace Neo.SmartContract.Native
{
internal delegate object? NativeMethodInvoker(NativeContract contract, ApplicationEngine engine, object?[] args);
[DebuggerDisplay("{Name}")]
internal class ContractMethodMetadata : IHardforkActivable
{
public string Name { get; }
public MethodInfo Handler { get; }
public InteropParameterDescriptor[] Parameters { get; }
public bool NeedApplicationEngine { get; }
public bool NeedSnapshot { get; }
public long CpuFee { get; }
public long StorageFee { get; }
public CallFlags RequiredCallFlags { get; }
public ContractMethodDescriptor Descriptor { get; }
public Hardfork? ActiveIn { get; init; } = null;
public Hardfork? DeprecatedIn { get; init; } = null;
internal NativeMethodInvoker Invoker => field ??= CreateInvoker();
public ContractMethodMetadata(MemberInfo member, ContractMethodAttribute attribute)
{
Name = attribute.Name ?? member.Name;
Name = Name.ToLowerInvariant()[0] + Name[1..];
Handler = member switch
{
MethodInfo m => m,
PropertyInfo p => p.GetMethod ?? throw new ArgumentException("Property must have a get method."),
_ => throw new ArgumentException("Member type not supported", nameof(member))
};
ParameterInfo[] parameterInfos = Handler.GetParameters();
if (parameterInfos.Length > 0)
{
NeedApplicationEngine = parameterInfos[0].ParameterType.IsAssignableFrom(typeof(ApplicationEngine));
// snapshot is a DataCache instance, and DataCache implements IReadOnlyStoreView
NeedSnapshot = parameterInfos[0].ParameterType.IsAssignableFrom(typeof(DataCache));
}
if (NeedApplicationEngine || NeedSnapshot)
Parameters = parameterInfos.Skip(1).Select(p => new InteropParameterDescriptor(p)).ToArray();
else
Parameters = parameterInfos.Select(p => new InteropParameterDescriptor(p)).ToArray();
CpuFee = attribute.CpuFee;
StorageFee = attribute.StorageFee;
RequiredCallFlags = attribute.RequiredCallFlags;
ActiveIn = attribute.ActiveIn;
DeprecatedIn = attribute.DeprecatedIn;
Descriptor = new ContractMethodDescriptor
{
Name = Name,
ReturnType = ToParameterType(Handler.ReturnType),
Parameters = Parameters.Select(p => new ContractParameterDefinition { Type = ToParameterType(p.Type), Name = p.Name! }).ToArray(),
Safe = (attribute.RequiredCallFlags & ~CallFlags.ReadOnly) == 0
};
}
internal object? Invoke(NativeContract contract, ApplicationEngine engine, object?[] args)
{
try
{
return Invoker(contract, engine, args);
}
catch (Exception ex) when (ex is not TargetInvocationException)
{
throw new TargetInvocationException(ex);
}
}
private NativeMethodInvoker CreateInvoker()
{
var contract = Expression.Parameter(typeof(NativeContract), "contract");
var engine = Expression.Parameter(typeof(ApplicationEngine), "engine");
var args = Expression.Parameter(typeof(object[]), "args");
var handlerParameters = Handler.GetParameters();
var callParameters = new Expression[handlerParameters.Length];
int publicParameterIndex = 0;
for (int i = 0; i < handlerParameters.Length; i++)
{
if (i == 0 && NeedApplicationEngine)
{
callParameters[i] = Expression.Convert(engine, handlerParameters[i].ParameterType);
continue;
}
if (i == 0 && NeedSnapshot)
{
callParameters[i] = Expression.Convert(
Expression.Property(engine, nameof(ApplicationEngine.SnapshotCache)),
handlerParameters[i].ParameterType);
continue;
}
callParameters[i] = Expression.Convert(
Expression.ArrayIndex(args, Expression.Constant(publicParameterIndex++)),
handlerParameters[i].ParameterType);
}
Expression? instance = Handler.IsStatic ? null : Expression.Convert(contract, Handler.DeclaringType!);
Expression call = Expression.Call(instance, Handler, callParameters);
Expression body = Handler.ReturnType == typeof(void)
? Expression.Block(call, Expression.Constant(null, typeof(object)))
: Expression.Convert(call, typeof(object));
return Expression.Lambda<NativeMethodInvoker>(body, contract, engine, args).Compile();
}
private static ContractParameterType ToParameterType(Type type)
{
if (type.BaseType == typeof(ContractTask)) return ToParameterType(type.GenericTypeArguments[0]);
if (type == typeof(ContractTask)) return ContractParameterType.Void;
if (type == typeof(void)) return ContractParameterType.Void;
if (type == typeof(bool)) return ContractParameterType.Boolean;
if (type == typeof(sbyte)) return ContractParameterType.Integer;
if (type == typeof(byte)) return ContractParameterType.Integer;
if (type == typeof(short)) return ContractParameterType.Integer;
if (type == typeof(ushort)) return ContractParameterType.Integer;
if (type == typeof(int)) return ContractParameterType.Integer;
if (type == typeof(uint)) return ContractParameterType.Integer;
if (type == typeof(long)) return ContractParameterType.Integer;
if (type == typeof(ulong)) return ContractParameterType.Integer;
if (type == typeof(BigInteger)) return ContractParameterType.Integer;
if (type == typeof(byte[])) return ContractParameterType.ByteArray;
if (type == typeof(string)) return ContractParameterType.String;
if (type == typeof(UInt160)) return ContractParameterType.Hash160;
if (type == typeof(UInt256)) return ContractParameterType.Hash256;
if (type == typeof(ECPoint)) return ContractParameterType.PublicKey;
if (type == typeof(Boolean)) return ContractParameterType.Boolean;
if (type == typeof(Integer)) return ContractParameterType.Integer;
if (type == typeof(ByteString)) return ContractParameterType.ByteArray;
if (type == typeof(Buffer)) return ContractParameterType.ByteArray;
if (type == typeof(Array)) return ContractParameterType.Array;
if (type == typeof(Struct)) return ContractParameterType.Array;
if (type == typeof(Map)) return ContractParameterType.Map;
if (type == typeof(StackItem)) return ContractParameterType.Any;
if (type == typeof(object)) return ContractParameterType.Any;
if (typeof(IInteroperable).IsAssignableFrom(type)) return ContractParameterType.Array;
if (typeof(ISerializable).IsAssignableFrom(type)) return ContractParameterType.ByteArray;
if (type.IsArray) return ContractParameterType.Array;
if (type.IsEnum) return ContractParameterType.Integer;
if (type.IsValueType) return ContractParameterType.Array;
return ContractParameterType.InteropInterface;
}
}
}