-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractDiscovery.cs
More file actions
181 lines (154 loc) · 6.22 KB
/
ContractDiscovery.cs
File metadata and controls
181 lines (154 loc) · 6.22 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
171
172
173
174
175
176
177
178
179
180
181
using System.Reflection;
using Qubic.Core.Contracts;
namespace Qubic.ScTester;
public class ContractInfo
{
public string Name { get; init; } = "";
public int ContractIndex { get; init; }
public List<FunctionInfo> Functions { get; init; } = [];
}
public class FunctionInfo
{
public string Name { get; init; } = "";
public uint InputTypeId { get; init; }
public Type InputType { get; init; } = null!;
public Type OutputType { get; init; } = null!;
public bool IsEmptyInput { get; init; }
public List<InputPropertyInfo> InputProperties { get; init; } = [];
}
public class InputPropertyInfo
{
public string Name { get; init; } = "";
public Type PropertyType { get; init; } = null!;
public bool IsRequired { get; init; }
public PropertyInfo ReflectionProperty { get; init; } = null!;
}
public class ContractDiscovery
{
public List<ContractInfo> Contracts { get; }
public ContractDiscovery()
{
Contracts = Discover();
}
private static List<ContractInfo> Discover()
{
var assembly = typeof(ISmartContractInput).Assembly;
var contracts = new List<ContractInfo>();
// Find all static classes named *Contract in Qubic.Core.Contracts.* namespaces
var contractTypes = assembly.GetTypes()
.Where(t => t.IsClass && t.IsSealed && t.IsAbstract // static class
&& t.Namespace?.StartsWith("Qubic.Core.Contracts.") == true
&& t.Name.EndsWith("Contract"))
.ToList();
foreach (var contractType in contractTypes)
{
var indexField = contractType.GetField("ContractIndex",
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (indexField == null) continue;
var contractIndex = (int)indexField.GetValue(null)!;
var contractName = contractType.Name.Replace("Contract", "");
var ns = contractType.Namespace!;
var functions = new List<FunctionInfo>();
// Discover functions from nested Functions class
var functionsClass = contractType.GetNestedType("Functions",
BindingFlags.Public | BindingFlags.Static);
if (functionsClass != null)
{
foreach (var field in functionsClass.GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (field.FieldType != typeof(uint)) continue;
var inputTypeId = (uint)field.GetValue(null)!;
var funcName = field.Name;
var inputType = assembly.GetType($"{ns}.{funcName}Input");
var outputType = assembly.GetType($"{ns}.{funcName}Output");
if (inputType == null || outputType == null) continue;
var isEmptyInput = GetSerializedSize(inputType) == 0;
var props = GetInputProperties(inputType);
functions.Add(new FunctionInfo
{
Name = funcName,
InputTypeId = inputTypeId,
InputType = inputType,
OutputType = outputType,
IsEmptyInput = isEmptyInput,
InputProperties = props
});
}
}
contracts.Add(new ContractInfo
{
Name = contractName,
ContractIndex = contractIndex,
Functions = functions.OrderBy(f => f.InputTypeId).ToList()
});
}
return contracts.OrderBy(c => c.ContractIndex).ToList();
}
private static int GetSerializedSize(Type inputType)
{
try
{
var instance = Activator.CreateInstance(inputType);
var prop = inputType.GetProperty("SerializedSize");
if (prop != null)
return (int)prop.GetValue(instance)!;
}
catch { }
return -1;
}
private static List<InputPropertyInfo> GetInputProperties(Type inputType)
{
var result = new List<InputPropertyInfo>();
foreach (var prop in inputType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (prop.Name is "SerializedSize") continue;
var isRequired = prop.GetCustomAttributes()
.Any(a => a.GetType().Name == "RequiredMemberAttribute");
result.Add(new InputPropertyInfo
{
Name = prop.Name,
PropertyType = prop.PropertyType,
IsRequired = isRequired,
ReflectionProperty = prop
});
}
return result;
}
public static object? CallFromBytes(Type outputType, byte[] data)
{
// ReadOnlySpan<byte> is a ref struct and can't be boxed, so we can't use
// MethodInfo.Invoke directly. Instead, call through a generic helper that
// uses the ISmartContractOutput<TSelf> static abstract interface.
var helper = typeof(ContractDiscovery)
.GetMethod(nameof(CallFromBytesGeneric), BindingFlags.NonPublic | BindingFlags.Static)!
.MakeGenericMethod(outputType);
return helper.Invoke(null, [data]);
}
private static object CallFromBytesGeneric<T>(byte[] data)
where T : ISmartContractOutput<T>
{
return T.FromBytes(data)!;
}
public static byte[] CreateInputBytes(Type inputType, Dictionary<string, object?> values)
{
var instance = Activator.CreateInstance(inputType)!;
foreach (var (name, value) in values)
{
var prop = inputType.GetProperty(name);
if (prop != null && value != null)
prop.SetValue(instance, value);
}
var toBytes = inputType.GetMethod("ToBytes")!;
return (byte[])toBytes.Invoke(instance, null)!;
}
public static Dictionary<string, object?> ReadOutputProperties(object output)
{
var result = new Dictionary<string, object?>();
var type = output.GetType();
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
result[prop.Name] = prop.GetValue(output);
}
return result;
}
}