/
dType.sol
294 lines (248 loc) · 8.97 KB
/
dType.sol
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
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import './dTypeLib.sol';
import './dTypesLib.sol';
contract dType {
using dTypeLib for dTypeLib.dType;
using dTypeLib for dTypeLib.LangChoices;
using dTypesLib for dTypesLib.dTypes;
address rootAddress;
bytes32[] public typeIndex;
mapping(bytes32 => Type) public typeStruct;
mapping(bytes32 => dTypesLib.dTypes[]) public optionals;
dTypeLib.LangChoices constant defaultLang = dTypeLib.LangChoices.Solidity;
struct Type {
dTypeLib.dType data;
uint256 index;
}
modifier typeExists (bytes32 typeHash) {
require(isType(typeHash), 'No such type inserted');
_;
}
modifier typeNotExists (bytes32 typeHash) {
require(!isType(typeHash), 'Type already exists');
_;
}
event LogNew(bytes32 indexed hash, uint256 indexed index);
event LogUpdate(bytes32 indexed hash, uint256 indexed index);
event LogRemove(bytes32 indexed hash, uint256 indexed index);
function setRootAddress(address root) public {
rootAddress = root;
}
function insert(dTypeLib.dType memory data)
public
returns (bytes32 dataHash)
{
bytes32 typeHash = getTypeHash(data.lang, data.name);
require(!isType(typeHash), 'Type already exists');
for (uint256 i = 0 ; i < data.types.length; i++) {
require(
isType(getTypeHash(data.lang, data.types[i].name)),
'A type in the composition does not exists'
);
require(bytes(data.types[i].name).length > 0, 'Empty type name');
require(bytes(data.types[i].label).length > 0, 'Empty label name');
}
typeStruct[typeHash].data.insert(data);
typeStruct[typeHash].index = typeIndex.push(typeHash)-1;
emit LogNew(typeHash, typeStruct[typeHash].index);
return typeHash;
}
function update(bytes32 typeHash, dTypeLib.dType memory data)
public
returns(bytes32 hash)
{
remove(typeHash);
return insert(data);
}
function remove(bytes32 typeHash)
typeExists(typeHash)
public
returns(uint256 index)
{
uint rowToDelete = typeStruct[typeHash].index;
bytes32 keyToMove = typeIndex[typeIndex.length-1];
typeIndex[rowToDelete] = keyToMove;
typeStruct[keyToMove].index = rowToDelete;
typeIndex.length--;
// TODO remove optionals[typeHash]
emit LogRemove(typeHash, rowToDelete);
emit LogUpdate(keyToMove, rowToDelete);
return rowToDelete;
}
function setOptionals(bytes32 typeHash, dTypesLib.dTypes[] memory optionalValues)
typeExists(typeHash)
public
{
Type storage dtype = typeStruct[typeHash];
for (uint256 i = 0 ; i < optionalValues.length; i++) {
require(
isType(getTypeHash(dtype.data.lang, optionalValues[i].name)),
'A type in the composition does not exists'
);
require(bytes(optionalValues[i].name).length > 0, 'Empty type name');
require(bytes(optionalValues[i].label).length > 0, 'Empty label name');
}
for (uint256 i = 0 ; i < optionalValues.length; i++) {
optionals[typeHash].push(optionalValues[i]);
}
}
function count() public view returns(uint256 counter)
{
return typeIndex.length;
}
function getIndex() public view returns(bytes32[] memory indext) {
return typeIndex;
}
function getTypeHash(dTypeLib.LangChoices lang, string memory name)
pure
public
returns (bytes32 typeHash)
{
return keccak256(abi.encode(lang, name));
}
function getByHash(bytes32 typeHash) view public returns(Type memory dtype) {
return typeStruct[typeHash];
}
function get(dTypeLib.LangChoices lang, string memory name)
view
public
returns(Type memory dtype)
{
bytes32 typeHash = getTypeHash(lang, name);
return getByHash(typeHash);
}
function isType(bytes32 typeHash)
view
public
returns(bool isIndeed)
{
if (typeIndex.length == 0) {
return false;
}
return (typeIndex[typeStruct[typeHash].index] == typeHash);
}
function getOptionals(bytes32 typeHash) view public returns (dTypesLib.dTypes[] memory optionalValues) {
return optionals[typeHash];
}
function getByIndex(uint256 index)
view
public
returns(Type memory dtype, bytes32 typeHash)
{
require(index <= typeIndex.length, 'Index too big.');
return (typeStruct[typeIndex[index]], typeIndex[index]);
}
function getTypeSignature(bytes32 typeHash)
view
public
returns(string memory typeSignature)
{
return string(getEncodedTypes(typeStruct[typeHash]));
}
function getEncodedType(dTypeLib.LangChoices lang, string memory name)
view
internal
returns(bytes memory encoded)
{
Type storage dtype = typeStruct[getTypeHash(lang, name)];
if (dtype.data.types.length > 0) {
return getEncodedTypes(dtype);
}
return abi.encodePacked(dtype.data.name);
}
function typeIsArray(string memory name) pure public returns(bool isArray) {
bytes memory encoded = bytes(name);
uint256 length = encoded.length;
if (
encoded[length-2] == bytes1(0x5b) &&
encoded[length-1] == bytes1(0x5d)
) {
return true;
}
return false;
}
function getEncodedTypes(Type storage dtype)
view
internal
returns(bytes memory encoded)
{
uint256 length = dtype.data.types.length;
if (length > 1) {
for (uint256 i = 0; i < length - 1; i++) {
encoded = abi.encodePacked(
encoded,
getEncodedType(dtype.data.lang, dtype.data.types[i].name),
','
);
}
}
if (length > 0) {
encoded = abi.encodePacked(
encoded,
getEncodedType(dtype.data.lang, dtype.data.types[length - 1].name)
);
}
// If type is an array, we need to append [] instead of enclosing in brackets
if (typeIsArray(dtype.data.name)) {
encoded = abi.encodePacked(encoded, '[]');
} else {
encoded = abi.encodePacked('(', encoded, ')');
}
return encoded;
}
function getSignature(bytes32 typeHash)
view
public
returns (bytes4 signature)
{
Type storage dtype = typeStruct[typeHash];
bytes memory encoded;
uint256 length = dtype.data.types.length;
if (length > 1) {
for (uint256 i = 0; i < length - 1; i++) {
encoded = abi.encodePacked(
encoded,
getEncodedType(dtype.data.lang, dtype.data.types[i].name),
','
);
}
}
if (length > 0) {
encoded = abi.encodePacked(
encoded,
getEncodedType(dtype.data.lang, dtype.data.types[length - 1].name)
);
}
encoded = abi.encodePacked(dtype.data.name, '(', encoded, ')');
return bytes4(keccak256(encoded));
}
function run(bytes32 funcHash, bytes32[] memory inDataHash)
public
returns(bytes32 outDataHash)
{
Type storage dtype = typeStruct[funcHash];
bytes memory encodedInputs = abi.encodePacked(getSignature(funcHash));
require(inDataHash.length == dtype.data.types.length, 'Incorrect number of inputs');
// Retrieve inputs for calling the function at funcHash
for (uint256 i = 0; i < dtype.data.types.length; i++) {
bytes32 typeHash = getTypeHash(dtype.data.lang, dtype.data.types[i].name);
Type storage ttype = typeStruct[typeHash];
(bool success, bytes memory inputData) = ttype.data.contractAddress.call(
abi.encodeWithSignature('getByHash(bytes32)', inDataHash[i])
);
require(success == true, 'Retrieving input failed');
encodedInputs = abi.encodePacked(encodedInputs, inputData);
}
// Calling the function determined by funcHash
(bool success, bytes memory outputData) = dtype.data.contractAddress.call(encodedInputs);
require(success == true, 'Running function failed');
// Inserting the funcHash outputs into the corresponding type storage
bytes32 outputHash = getTypeHash(dtype.data.lang, optionals[funcHash][0].name);
(bool success2, bytes memory result) = typeStruct[outputHash].data.contractAddress.call(
abi.encodeWithSignature('insertBytes(bytes)', outputData)
);
require(success2 == true, 'Inserting output failed');
return abi.decode(result, (bytes32));
}
}