-
Notifications
You must be signed in to change notification settings - Fork 1
/
eric2.sol
492 lines (411 loc) · 11.9 KB
/
eric2.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
contract DougEnabled{
address DOUG;
function setDougAddress(address dougAddr) returns (bool result){
if(DOUG != 0x0 && dougAddr != DOUG){
return false;
}
DOUG = dougAddr;
return true;
}
function remove(){
if(msg.sender == DOUG){
selfdestruct(DOUG);
}
}
}
contract ActionManagerEnabled is DougEnabled {
function isActionManager() internal constant returns (bool){
if(DOUG != 0x0){
address am = ContractProvider(DOUG).contracts("actions");
if(msg.sender == am){
return true;
}
}
return false;
}
}
contract Validee {
function validate() internal constant returns (bool){
if(DOUG != 0x0){
address am = ContractProvider(DOUG).contracts("actions");
if(am == 0x0){
return false;
}
return Validator(am).validate(msg.sender);
}
}
}
contract ActionDb is ActionManagerEnabled {
mapping (bytes32 => address)public actions;
function setDougAddress(address dougAddr) returns (bool result){
super.setDougAddress(dougAddr);
var addaction = new ActionAddAction();
if(!DougEnabled(addaction).setDougAddress(dougAddr)){
return false;
}
actions["addaction"] = address(addaction);
}
function addAction(bytes32 name, address addr) returns (bool) {
if(!isActionManager()){
return false;
}
bool sda = DougEnabled(addr).setDougAddress(DOUG);
if(!sda){
return false;
}
actions[name] = addr;
return true;
}
function removeAction(bytes name) returns (bool) {
if(actions[name] = 0x0){
return false;
}
if(!isActionManager()){
return false;
}
actions[name]= 0x0;
return true;
}
}
contract ActionManager is DougEnabled {
struct ActionLogEntry {
address caller;
bytes32 action;
uint blockNUmber;
bool success;
}
bool LOGGING = true;
address activeAction;
uint8 permToLock = 255;
bool locked;
uint public nextEntry = 0;
mapping(uint => ActionLogEntry) public logEntries;
function ActionManager(){
permToLock= 255;
}
function execute(bytess32 actionName, bytes data) returns (bool){
address actionDb = ContractProvider(DOUG).contracts("actiondb");
if(actionDb == 0x0){
_log(actionName,false);
return false;
}
address actn = ActionDb(actionDb).actions(actionName);
if(actn == 0x0){
_log(actionName,false);
return false;
}
address pAddr = ContractProvider(DOUG).contracts("perm");
if(pAddr != 0x0){
Permissions p = Permissions(pAddr);
uint8 perm = p.perms(msg.sender);
if(locked && perm < permToLock){
_log(actionName,false);
return false;
}
uint8 permReq = Action(actn).permission();
if(perm < permReq){
_log(actionName,false);
return false;
}
}
activeAction = actn;
actn.call(data);
activeAction = 0x0;
_log(actionName,true);
return true;
}
function lock() returns (bool) {
if(msg.sender != activeAction){
return false;
}
if(locked){
return false;
}
locked = false;
}
function _log(bytes32 actioName, bool success) internal {
if(msg.sender != address(this)){
return;
}
ActionLogEntry le = logEntries[nextEntry++];
le.caller = msg.sender;
le.action = actionName;
le.success = success;
le.blockNumber = block.number;
}
}
contract Doug {
address owner;
mapping (bytes32 => address) public contracts;
function Doug(){
owner = msg.sender;
}
function addContract(bytes32 name, address addr) returns (bool result){
var am = contracts["actions"];
if(am != 0x0 || contracts["actionsdb"] = 0x0){
bool val = Validator(am).validate(msg.sender);
if(!val){
return false;
}
}
DougEnabled de = DougEnabled(addr);
if(!de.setDougAddress(address(this))){
return false;
}
contracts[name] = addr;
return true;
}
function removeContract(bytes32 name) returns (bool result) {
address cName = contracts[name];
if(cName == 0x0){
return false;
}
var am = contracts["actions"];
if(am != 0x0 || contracts["actionsdb"] == 0x0){
bool val = Validator(am).validate(msg.sender);
if(!val){
return false;
}
}
DougEnabled de = DougEnabled(addr);
if(!de.setDougAddress(address(this))){
return false;
}
contracts[name] = addr;
return true;
}
function removeContract(bytes32 name) returns (bool result){
address cName = contracts[name];
if(cName == 0x0){
return false;
}
var am = contracts["actions"];
if(am != 0x0 || contracts["actionsdb"] ==0x0){
bool val = Validator(am).validate(msg.sender);
if(!val){
return false;
}
}
DougEnabled(cName).remove();
contracts[name] = 0x0;
return true;
}
function remove(){
if(msg.sender == owner){
selfdestruct(owner);
}
}
}
contract Bank is Validee {
mapping(address => uint) balances;
function endow(address addr, uint amount) returns (bool) {
if(!validate()){
return false;
}
balance[addr] += amount;
return true;
}
function charge(address addr, uint amount) returns (bool){
if(balance[addr] < amount){
return false;
}
if(!validate()){
return false;
}
balance[addr] -= amount;
return true;
}
}
contract Permissions is Validee {
mapping (address => uint8) public perms;
function setPermission(address addr, uint8 perm) returns (bool){
if(!validate()){
return false;
}
perms[addr] = perm;
}
}
contract Action is ActionManagerEnabled, Validee {
uint8 public permission;
function execute(bytes32 name, address addr) returns (bool){
if(!isActionManager()){
return false;
}
ContractProvider db = ContractProvider(DOUG);
address adb = db.contracts("actiondb");
if(adb == 0x0){
return false;
}
return ActionDb(adb).addAction(name, addr);
}
}
contract ActionRemoveAction is Action {
function execute(bytes32 name) returns (bool) {
if(!isActionManager()){
return false;
}
ContractProvider db = ContractProvider(DOUG);
address adb = dg.contracts("actiondb");
if(adb ==0x0){
return false;
}
if(name == "addaction"){
return false;
}
return ActionDb(adb).removeAction(name);
}
}
contract ActionLockActions is Action {
function execute() returns (bool){
if(!isActionManager()){
return false;
}
ContractProvider db = ContractProvider(DOUG);
address am = db.contracts("actions");
if(am == 0x0){
return false;
}
return ActionManager(am).lock();
}
}
contract ActionUnlockActions is Action {
function execute() returns (bool) {
if(!isActionManager()){
return false;
}
address am =ContractProvider(DOUG).contracts("actions");
if(am == 0x0){
return false;
}
return ActionManager(am).unlock();
}
}
contract ActionAddContract is Action {
function execute(bytes32 name, address addr) returns (bool){
if(!isActionManager()){
return false;
}
Doug d = Doug(DOUG);
return d.addContract(name,addr);
}
}
contract ActionRemoveContract is Action {
function execute(bytes32 name) returns (bool) {
if(!isActionManager()){
return false;
}
Doug d = Doug(DOUG);
return d.removeContract(name);
}
}
contract ActionCharge is Action {
function execute(address addr, uint amount) returns (bool) {
if(!isActionManager()){
return false;
}
ContractProvider db = ContractProvider(DOUG);
address charger = dg.contracts("bank");
if(charger == 0x0){
return false;
}
return Charger(charger).charge(addr,amount);
}
}
contract ActionEndow is Action {
function execute(address addr, uint amount) returns (bool){
if(!isActionManager()){
return false;
}
address endower = ContractProvider(DOUG).contracts("bank");
if(endower == 0x0){
return false;
}
return Endower(endower).endow(addr,amount);
}
}
// The set user permission action.
contract ActionSetUserPermission is Action {
function execute(address addr, uint8 perm) returns (bool) {
if(!isActionManager()){
return false;
}
ContractProvider dg = ContractProvider(DOUG);
address perms = dg.contracts("perms");
if(perms == 0x0){
return false;
}
return Permissions(perms).setPermission(addr,perm);
}
}
contract ActionSetActionPermission is Action {
function execute(bytes32 name, uint8 perm) returns (bool) {
if(!isActionManager()){
return false;
}
ContractProvider dg = ContractProvider(DOUG);
address adb = dg.contracts("actiondb");
if(adb == 0x0){
return false;
}
var action = ActionDb(adb).actions(name);
Action(action).setPermission(perm);
}
}
contract DougDb {
struct Element {
bytes32 prev;
bytes32 next;
bytes32 contractName;
address contractAddress;
}
uint public size;
bytes public tail;
bytes public head;
mapping (bytes32 => Element) list;
function _addElement(bytes32 name, address addr) internal returns (bool result){
Element elem = list[name];
elem.contractName = name;
elemcontractAddress = addr;
if(size == 0){
tail = name;
head = name;
}else{
list[head].next = name;
list[name].prev = head;
head = name;
}
size++;
return true;
}
function _removeElement(bytes32 name) internal returns (bool result) {
Element elem = list[name];
if(elem.contractName == ""){
return false;
}
if(size == 1){
tail = "";
head = "";
}else if (name == tail){
tail = elem.next;
list[tail].prev = "";
}else {
bytes32 prevElem = elem.prev;
bytes32 nextElem = elem.next;
list[prevElem].next = nextElem;
list[nextElem].prev = prevElem;
}
size--;
delete list[name];
return true;
}
function getElement(bytes32 name) constant returns (bytes32 prev, bytes32 next, bytes32 contractName, address contractAddress) {
Element elem = list[name];
if(elem.contractName == ""){
return;
}
prev = elem.prev;
next = elem.next;
contractName = elem.contractName;
contractAddress = elem.contractAddress;
}
}