-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMVDFunctionalitiesManager.sol
340 lines (283 loc) · 15.3 KB
/
MVDFunctionalitiesManager.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
pragma solidity ^0.6.0;
import "./IMVDFunctionalitiesManager.sol";
import "./CommonUtilities.sol";
import "./IMVDProxy.sol";
import "./IMVDFunctionalityProposal.sol";
import "./Functionality.sol";
contract MVDFunctionalitiesManager is IMVDFunctionalitiesManager, CommonUtilities {
address private _proxy;
Functionality[] private _functionalities;
uint256 private _functionalitiesAmount;
mapping(string => uint256) private _indexes;
mapping(address => uint256) private _functionalityCount;
address private _callingContext;
constructor(address sourceLocation,
uint256 getMinimumBlockNumberSourceLocationId, address getMinimumBlockNumberFunctionalityAddress,
uint256 getEmergencyMinimumBlockNumberSourceLocationId, address getEmergencyMinimumBlockNumberFunctionalityAddress,
uint256 getEmergencySurveyStakingSourceLocationId, address getEmergencySurveyStakingFunctionalityAddress,
uint256 checkVoteResultSourceLocationId, address checkVoteResultFunctionalityAddress) public {
if(getMinimumBlockNumberFunctionalityAddress == address(0)) {
return;
}
init(sourceLocation,
getMinimumBlockNumberSourceLocationId, getMinimumBlockNumberFunctionalityAddress,
getEmergencyMinimumBlockNumberSourceLocationId, getEmergencyMinimumBlockNumberFunctionalityAddress,
getEmergencySurveyStakingSourceLocationId, getEmergencySurveyStakingFunctionalityAddress,
checkVoteResultSourceLocationId, checkVoteResultFunctionalityAddress);
}
function init(address sourceLocation,
uint256 getMinimumBlockNumberSourceLocationId, address getMinimumBlockNumberFunctionalityAddress,
uint256 getEmergencyMinimumBlockNumberSourceLocationId, address getEmergencyMinimumBlockNumberFunctionalityAddress,
uint256 getEmergencySurveyStakingSourceLocationId, address getEmergencySurveyStakingFunctionalityAddress,
uint256 checkVoteResultSourceLocationId, address checkVoteResultFunctionalityAddress) public override {
require(_functionalitiesAmount == 0, "Init already called!");
addFunctionality(
"getMinimumBlockNumberForSurvey",
sourceLocation,
getMinimumBlockNumberSourceLocationId,
getMinimumBlockNumberFunctionalityAddress,
false,
"getMinimumBlockNumberForSurvey()",
'["uint256"]',
false,
false
);
addFunctionality(
"getMinimumBlockNumberForEmergencySurvey",
sourceLocation,
getEmergencyMinimumBlockNumberSourceLocationId,
getEmergencyMinimumBlockNumberFunctionalityAddress,
false,
"getMinimumBlockNumberForEmergencySurvey()",
'["uint256"]',
false,
false
);
addFunctionality(
"getEmergencySurveyStaking",
sourceLocation,
getEmergencySurveyStakingSourceLocationId,
getEmergencySurveyStakingFunctionalityAddress,
false,
"getEmergencySurveyStaking()",
'["uint256"]',
false,
false
);
addFunctionality(
"checkSurveyResult",
sourceLocation,
checkVoteResultSourceLocationId,
checkVoteResultFunctionalityAddress,
false,
"checkSurveyResult(address)",
'["bool"]',
false,
false
);
}
function addFunctionality(string memory codeName, address sourceLocation, uint256 sourceLocationId, address location, bool submitable, string memory methodSignature, string memory returnAbiParametersArray, bool isInternal, bool needsSender) public override {
addFunctionality(codeName, sourceLocation, sourceLocationId, location, submitable, methodSignature, returnAbiParametersArray, isInternal, needsSender, _functionalities.length);
}
function addFunctionality(string memory codeName, address sourceLocation, uint256 sourceLocationId, address location, bool submitable, string memory methodSignature, string memory returnAbiParametersArray, bool isInternal, bool needsSender, uint256 position) public override {
require(_proxy == address(0) || _callingContext == msg.sender, "Unauthorized call!");
Functionality memory functionality = Functionality(
codeName,
sourceLocation,
sourceLocationId,
location,
submitable,
methodSignature,
returnAbiParametersArray,
isInternal,
needsSender,
address(0),
true,
position
);
if(position >= _functionalities.length) {
_functionalities.push(functionality);
} else {
removeFunctionality(_functionalities[position].codeName);
_functionalities[position] = functionality;
}
_functionalityCount[location] = _functionalityCount[location] + 1;
_functionalitiesAmount++;
_indexes[codeName] = position;
}
function removeFunctionality(string memory codeName) public override returns(bool removed, uint256 position) {
require(_proxy == address(0) || _callingContext == msg.sender, "Unauthorized call!");
Functionality storage functionality = _functionalities[_indexes[codeName]];
position = functionality.position;
if(compareStrings(codeName, functionality.codeName) && functionality.active) {
functionality.active = false;
_functionalityCount[functionality.location] = _functionalityCount[functionality.location] - 1;
_functionalitiesAmount--;
removed = true;
}
}
function preConditionCheck(string memory codeName, bytes memory data, uint8 submitable, address sender, uint256 value) public override view returns(address location, bytes memory payload) {
Functionality memory functionality = _functionalities[_indexes[codeName]];
require(compareStrings(codeName, functionality.codeName) && functionality.active, "Unauthorized functionality");
require(submitable == (functionality.submitable ? 1 : 0), "Functionality called in the wrong context!");
require(functionality.isInternal ? _functionalityCount[sender] > 0 || _callingContext == sender : true, "Internal functionalities can be called from other functionalities only!");
location = functionality.location;
if(functionality.needsSender) {
require(data.length >= (submitable == 1 ? 64 : 32), "Insufficient space in data payload");
assembly {
mstore(add(data, 0x20), sender)
switch iszero(submitable) case 0 {
mstore(add(data, 0x40), value)
}
}
}
payload = abi.encodePacked(bytes4(keccak256(bytes(functionality.methodSignature))), data);
}
function getFunctionalitiesAmount() public override view returns(uint256) {
return _functionalitiesAmount;
}
function isValidFunctionality(address functionality) public override view returns(bool) {
return _functionalityCount[functionality] > 0;
}
function isAuthorizedFunctionality(address functionality) public override view returns(bool) {
return _callingContext != address(0) && (_functionalityCount[functionality] > 0 || _callingContext == functionality);
}
function getFunctionalityData(string memory codeName) public override view returns(address, uint256, string memory, address, uint256) {
Functionality memory functionality = _functionalities[_indexes[codeName]];
return (compareStrings(codeName, functionality.codeName) && functionality.active ? functionality.location : address(0), functionality.position, functionality.methodSignature, functionality.sourceLocation, functionality.sourceLocationId);
}
function hasFunctionality(string memory codeName) public override view returns(bool) {
Functionality memory functionality = _functionalities[_indexes[codeName]];
return compareStrings(codeName, functionality.codeName) && functionality.active;
}
function functionalitiesToJSON() public override view returns(string memory) {
return functionalitiesToJSON(0, _functionalities.length);
}
function functionalitiesToJSON(uint256 start, uint256 l) public override view returns(string memory functionsJSONArray) {
uint256 length = start + l;
functionsJSONArray = "[";
for(uint256 i = start; i < length; i++) {
functionsJSONArray = !_functionalities[i].active ? functionsJSONArray : string(abi.encodePacked(functionsJSONArray, toJSON(_functionalities[i]), i == length - (_functionalities[i].active ? 1 : 0) ? "" : ","));
length += _functionalities[i].active ? 0 : 1;
length = length > _functionalities.length ? _functionalities.length : length;
}
functionsJSONArray = string(abi.encodePacked(functionsJSONArray, "]"));
}
function functionalityNames() public override view returns(string memory) {
return functionalityNames(0, _functionalities.length);
}
function functionalityNames(uint256 start, uint256 l) public override view returns(string memory functionsJSONArray) {
uint256 length = start + l;
functionsJSONArray = "[";
for(uint256 i = start; i < length; i++) {
functionsJSONArray = !_functionalities[i].active ? functionsJSONArray : string(abi.encodePacked(functionsJSONArray, '"', _functionalities[i].codeName, '"', i == length - (_functionalities[i].active ? 1 : 0) ? "" : ","));
length += _functionalities[i].active ? 0 : 1;
length = length > _functionalities.length ? _functionalities.length : length;
}
functionsJSONArray = string(abi.encodePacked(functionsJSONArray, "]"));
}
function functionalityToJSON(string memory codeName) public override view returns(string memory) {
return string(toJSON(_functionalities[_indexes[codeName]]));
}
function toJSON(Functionality memory func) private pure returns(bytes memory) {
return abi.encodePacked(
'{',
getFirstJSONPart(func.sourceLocation, func.sourceLocationId, func.location),
'","submitable":',
func.submitable ? "true" : "false",
',"isInternal":',
func.isInternal ? "true" : "false",
',"needsSender":',
func.needsSender ? "true" : "false",
',"proposalAddress":"',
toString(func.proposalAddress),
'","codeName":"',
func.codeName,
'","methodSignature":"',
func.methodSignature,
'","returnAbiParametersArray":',
formatReturnAbiParametersArray(func.returnAbiParametersArray),
',"position":',
toString(func.position),
'}'
);
}
function getProxy() public override view returns(address) {
return _proxy;
}
function setProxy() public override {
require(_functionalitiesAmount != 0, "Init not called!");
require(_proxy == address(0) || _proxy == msg.sender, _proxy != address(0) ? "Proxy already set!" : "Only Proxy can toggle itself!");
_proxy = _proxy == address(0) ? msg.sender : address(0);
}
function setupFunctionality(address proposalAddress) public override returns(bool result) {
require(_proxy == msg.sender, "Only Proxy can call This!");
IMVDFunctionalityProposal proposal = IMVDFunctionalityProposal(proposalAddress);
string memory codeName = proposal.getCodeName();
bool hasCodeName = !compareStrings(codeName, "");
string memory replaces = proposal.getReplaces();
bool hasReplaces = !compareStrings(replaces, "");
if(!hasCodeName && !hasReplaces) {
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = proposal.getLocation(), abi.encodeWithSignature("callOneTime(address)", proposalAddress));
_callingContext = address(0);
return result;
}
Functionality memory replacedFunctionality = _functionalities[_indexes[replaces]];
uint256 position = hasReplaces ? replacedFunctionality.position : _functionalities.length;
if(hasReplaces) {
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = replacedFunctionality.location, abi.encodeWithSignature("onStop(address)", proposalAddress));
_callingContext = address(0);
if(!result) {
revert("onStop failed!");
}
}
replacedFunctionality.active = hasReplaces ? false : replacedFunctionality.active;
_functionalitiesAmount -= hasReplaces ? 1 : 0;
_functionalityCount[replacedFunctionality.location] = _functionalityCount[replacedFunctionality.location] - (hasReplaces ? 1 : 0);
if(hasReplaces) {
_functionalities[position] = replacedFunctionality;
}
Functionality memory newFunctionality = Functionality(
codeName,
proposal.getSourceLocation(),
proposal.getSourceLocationId(),
proposal.getLocation(),
proposal.isSubmitable(),
proposal.getMethodSignature(),
proposal.getReturnAbiParametersArray(),
proposal.isInternal(),
proposal.needsSender(),
proposalAddress,
true,
position
);
_functionalitiesAmount += hasCodeName ? 1 : 0;
if(hasCodeName && position == _functionalities.length) {
_functionalities.push(newFunctionality);
} else if(hasCodeName) {
_functionalities[position] = newFunctionality;
}
_indexes[codeName] = hasCodeName ? position : 0;
_functionalityCount[newFunctionality.location] = _functionalityCount[newFunctionality.location] + (hasCodeName ? 1 : 0);
if(hasCodeName) {
(result,) = IMVDProxy(_proxy).callFromManager(_callingContext = newFunctionality.location, abi.encodeWithSignature("onStart(address,address)", proposalAddress, hasReplaces ? replacedFunctionality.location : address(0)));
_callingContext = address(0);
if(!result) {
revert("onStart failed!");
}
}
if(hasCodeName || hasReplaces) {
IMVDProxy(_proxy).emitFromManager(hasCodeName ? codeName : "", proposalAddress, hasReplaces ? replacedFunctionality.codeName : "", hasReplaces ? replacedFunctionality.sourceLocation : address(0), hasReplaces ? replacedFunctionality.sourceLocationId : 0, hasReplaces ? replacedFunctionality.location : address(0), hasReplaces ? replacedFunctionality.submitable : false, hasReplaces ? replacedFunctionality.methodSignature : "", hasReplaces ? replacedFunctionality.isInternal : false, hasReplaces ? replacedFunctionality.needsSender : false, hasReplaces ? replacedFunctionality.proposalAddress : address(0));
}
_callingContext = address(0);
return true;
}
function setCallingContext(address location) public override returns(bool changed) {
require(msg.sender == _proxy, "Unauthorized Access");
_callingContext = (changed = _callingContext == address(0)) ? location : _callingContext;
}
function clearCallingContext() public override {
require(msg.sender == _proxy, "Unauthorized Access");
_callingContext = address(0);
}
}