forked from pipeos-one/dType
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdType.sol
412 lines (350 loc) · 12.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import './dTypeInterface.sol';
import "./lib/ECVerify.sol";
import './dTypeLib.sol';
import './dTypesLib.sol';
contract dType is dTypeInterface {
using dTypeLib for dTypeLib.dType;
using dTypeLib for dTypeLib.dTypeRequired;
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;
mapping(bytes32 => dTypesLib.dTypes[]) public outputs;
dTypeLib.LangChoices constant defaultLang = dTypeLib.LangChoices.Solidity;
struct Type {
dTypeLib.dTypeRequired 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');
checkTypesExist(data.lang, data.types);
typeStruct[typeHash].data.insert(data);
typeStruct[typeHash].index = typeIndex.push(typeHash)-1;
setOptionals(typeHash, data.optionals);
setOutputs(typeHash, data.outputs);
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--;
delete optionals[typeHash];
delete outputs[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];
checkTypesExist(dtype.data.lang, optionalValues);
for (uint256 i = 0 ; i < optionalValues.length; i++) {
optionals[typeHash].push(optionalValues[i]);
}
}
function setOutputs(bytes32 typeHash, dTypesLib.dTypes[] memory outputValues)
typeExists(typeHash)
public
{
Type storage dtype = typeStruct[typeHash];
checkTypesExist(dtype.data.lang, outputValues);
for (uint256 i = 0 ; i < outputValues.length; i++) {
outputs[typeHash].push(outputValues[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(dTypeLib.dType memory dtype) {
return typeStruct[typeHash].data.getFull(
getOptionals(typeHash),
getOutputs(typeHash)
);
}
function get(dTypeLib.LangChoices lang, string memory name)
view
public
returns(dTypeLib.dType 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 getOutputs(bytes32 typeHash) view public returns (dTypesLib.dTypes[] memory outputValues) {
return outputs[typeHash];
}
function getByIndex(uint256 index)
view
public
returns(dTypeLib.dType memory dtype, bytes32 typeHash)
{
require(index <= typeIndex.length, 'Index too big.');
return (getByHash(typeIndex[index]), typeIndex[index]);
}
function checkTypesExist(dTypeLib.LangChoices lang, dTypesLib.dTypes[] memory types)
view
public
{
for (uint256 i = 0 ; i < types.length; i++) {
require(
isType(getTypeHash(lang, types[i].name)),
'A type in the composition does not exists'
);
require(bytes(types[i].name).length > 0, 'Empty type name');
require(bytes(types[i].label).length > 0, 'Empty label name');
}
}
function getTypeSignature(bytes32 typeHash)
view
public
returns(string memory typeSignature)
{
return string(getEncodedTypes(typeStruct[typeHash]));
}
function getEncodedType(dTypeLib.LangChoices lang, string memory name, string[] memory dimensions)
view
public
returns(bytes memory encoded)
{
Type storage dtype = typeStruct[getTypeHash(lang, name)];
if (dtype.data.types.length == 0) {
encoded = abi.encodePacked(dtype.data.name);
return abi.encodePacked(encoded, typeDimensionsToString(dimensions));
}
encoded = getEncodedTypes(dtype);
encoded = abi.encodePacked('(', encoded, ')');
encoded = abi.encodePacked(encoded, typeDimensionsToString(dimensions));
}
function typeDimensionsToString(string[] memory dimensions)
pure
public
returns(bytes memory encoded)
{
for (uint256 i = 0; i < dimensions.length; i++) {
encoded = abi.encodePacked(encoded, '[', dimensions[i], ']');
}
}
function getEncodedTypes(Type storage dtype)
view
internal
returns(bytes memory encoded)
{
uint256 length = dtype.data.types.length;
dTypesLib.dTypes[] storage typeOptionals = optionals[typeIndex[dtype.index]];
uint256 lengthOpt = typeOptionals.length;
for (uint256 i = 0; i < length; i++) {
encoded = abi.encodePacked(
encoded,
getEncodedType(
dtype.data.lang,
dtype.data.types[i].name,
dtype.data.types[i].dimensions
)
);
if (i < length - 1 || lengthOpt > 0) {
encoded = abi.encodePacked(encoded, ',');
}
}
for (uint256 i = 0; i < lengthOpt; i++) {
encoded = abi.encodePacked(
encoded,
getEncodedType(
dtype.data.lang,
typeOptionals[i].name,
typeOptionals[i].dimensions
)
);
if (i < lengthOpt - 1) {
encoded = abi.encodePacked(encoded, ',');
}
}
}
function getSignatureFull(bytes32 typeHash)
view
public
returns (bytes memory signature)
{
Type storage dtype = typeStruct[typeHash];
bytes memory encoded;
uint256 length = dtype.data.types.length;
uint256 lengthOpt = optionals[typeHash].length;
for (uint256 i = 0; i < length; i++) {
// TODO fix this quick fix for encoding multiple function inputs
string memory name = dtype.data.types[i].name;
if (dtype.data.types[i].relation == dTypesLib.dTypeRelation.Bytes) {
name = 'bytes';
}
encoded = abi.encodePacked(
encoded,
getEncodedType(
dtype.data.lang,
name,
dtype.data.types[i].dimensions
)
);
if (i < length - 1 || lengthOpt > 0) {
encoded = abi.encodePacked(encoded, ',');
}
}
encoded = abi.encodePacked(dtype.data.name, '(', encoded, ')');
return encoded;
}
function getSignature(bytes32 typeHash)
view
public
returns (bytes4 signature)
{
return bytes4(keccak256(getSignatureFull(typeHash)));
}
function run(bytes32 funcHash, bytes32[] memory inDataHash, bytes memory freeInputs)
public
returns(bytes32 outDataHash)
{
bytes memory outputData = runView(funcHash, inDataHash, freeInputs);
Type storage dtype = typeStruct[funcHash];
// Inserting the funcHash outputs into the corresponding type storage
bytes32 outputHash = getTypeHash(dtype.data.lang, outputs[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));
}
function run(bytes32 funcHash, bytes32[] memory inDataHash, bytes memory freeInputs, bytes memory signature)
public
returns(bytes32 outDataHash)
{
address senderAddress = recoverAddress(freeInputs, signature);
freeInputs = abi.encodePacked(freeInputs, abi.encode(senderAddress));
return run(funcHash, inDataHash, freeInputs);
}
function pipeView(bytes32[] memory inDataHash, bytes32[] memory funcHash, bytes memory freeInputs)
public
view
returns(bytes memory result)
{
result = getPackedInputs(typeStruct[funcHash[0]], inDataHash, freeInputs);
for (uint256 i = 0; i < funcHash.length; i++) {
Type storage dtype = typeStruct[funcHash[i]];
result = runViewRaw(funcHash[i], dtype, result);
}
return result;
}
function runView(bytes32 funcHash, bytes32[] memory inDataHash, bytes memory freeInputs)
public
view
returns(bytes memory result)
{
Type storage dtype = typeStruct[funcHash];
return runViewRaw(
funcHash,
dtype,
getPackedInputs(dtype, inDataHash, freeInputs)
);
}
function runViewRaw(
bytes32 funcHash,
Type storage dtype,
bytes memory inputs
)
private
view
returns(bytes memory outputData)
{
inputs = abi.encodePacked(getSignature(funcHash), inputs);
// Calling the function determined by funcHash
(bool success, bytes memory result) = dtype.data.contractAddress.staticcall(inputs);
require(success == true, 'Running function failed');
return result;
}
function getPackedInputs(Type storage dtype, bytes32[] memory inDataHash, bytes memory freeInputs) private view returns(bytes memory inputs) {
// require(inDataHash.length == dtype.data.types.length, 'Incorrect number of inputs');
uint256 length = inDataHash.length;
// Retrieve inputs for calling the function at funcHash
for (uint256 i = 0; i < 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.staticcall(
abi.encodeWithSignature('getByHash(bytes32)', inDataHash[i])
);
require(success == true, 'Retrieving input failed');
// TODO - assembly-based abi encoding, merging inputs
inputs = abi.encodePacked(inputs, inputData);
}
// TODO temporary fix
if (freeInputs.length > 0 && dtype.data.types[0].relation == dTypesLib.dTypeRelation.Bytes) {
// this is what should be used
inputs = abi.encode(inputs, freeInputs);
} else {
// we need this for voting, needs fixing
inputs = abi.encodePacked(inputs, freeInputs);
}
}
function recoverAddress(bytes memory data, bytes memory signature) private pure returns(address senderAddress) {
// TODO fix signature security
// chain_id, some identifiers for the purpose of the function
// ideally a nonce (tx.nonce ftw!);
senderAddress = ECVerify.ecverify(keccak256(data), signature);
}
}