aboutsummaryrefslogtreecommitdiffstats
path: root/test/compilationTests/corion/moduleHandler.sol
blob: 682f81dd4352ed1c69afdc29ce6b3e4165a9ad04 (plain) (blame)
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
pragma solidity ^0.4.11;

import "./module.sol";
import "./announcementTypes.sol";
import "./multiOwner.sol";

import "./publisher.sol";
import "./token.sol";
import "./provider.sol";
import "./schelling.sol";
import "./premium.sol";
import "./ico.sol";

contract abstractModule {
    function connectModule() external returns (bool success) {}
    function disconnectModule() external returns (bool success) {}
    function replaceModule(address addr) external returns (bool success) {}
    function disableModule(bool forever) external returns (bool success) {}
    function isActive() public constant returns (bool success) {}
    function replaceModuleHandler(address newHandler) external returns (bool success) {}
    function transferEvent(address from, address to, uint256 value) external returns (bool success) {}
    function newSchellingRoundEvent(uint256 roundID, uint256 reward) external returns (bool success) {}
}

contract moduleHandler is multiOwner, announcementTypes {
    
    struct modules_s {
        address addr;
        bytes32 name;
        bool schellingEvent;
        bool transferEvent;
    }
    
    modules_s[] public modules;
    address public foundationAddress;
    uint256 debugModeUntil = block.number + 1000000;
    
    function moduleHandler(address[] newOwners) multiOwner(newOwners) {}
    
    function load(address foundation, bool forReplace, address Token, address Premium, address Publisher, address Schelling, address Provider) {
        /*
            Loading modulest to ModuleHandler.
            
            This module can be called only once and only by the owner, if every single module and its database are already put on the blockchain.
            If forReaplace is true, than the ModuleHandler will be replaced. Before the publishing of its replace, the new contract must be already on the blockchain.
            
            @foundation     Address of foundation.
            @forReplace     Is it for replace or not. If not, it will be connected to the module.
            @Token          address of token.
            @Publisher      address of publisher.
            @Schelling      address of Schelling.
            @Provider       address of provider
        */
        require( owners[msg.sender] );
        require( modules.length == 0 );
        foundationAddress = foundation;
        addModule( modules_s(Token,      sha3('Token'),      false, false),  ! forReplace);
        addModule( modules_s(Premium,    sha3('Premium'),    false, false),  ! forReplace);
        addModule( modules_s(Publisher,  sha3('Publisher'),  false, true),   ! forReplace);
        addModule( modules_s(Schelling,  sha3('Schelling'),  false, true),   ! forReplace);
        addModule( modules_s(Provider,   sha3('Provider'),   true, true),    ! forReplace);
    }
    function addModule(modules_s input, bool call) internal {
        /*
            Inside function for registration of the modules in the database.
            If the call is false, wont happen any direct call.
            
            @input  _Structure of module.
            @call   Is connect to the module or not.
        */
        if ( call ) { require( abstractModule(input.addr).connectModule() ); }
        var (success, found, id) = getModuleIDByAddress(input.addr);
        require( success && ! found );
        (success, found, id) = getModuleIDByHash(input.name);
        require( success && ! found );
        (success, found, id) = getModuleIDByAddress(0x00);
        require( success );
        if ( ! found ) {
            id = modules.length;
            modules.length++;
        }
        modules[id] = input;
    }
    function getModuleAddressByName(string name) public constant returns( bool success, bool found, address addr ) {
        /*
            Search by name for module. The result is an Ethereum address.
            
            @name       Name of module.
            @addr       Address of module.
            @found      Is there any result.
            @success    Was the transaction succesfull or not.
        */
        var (_success, _found, _id) = getModuleIDByName(name);
        if ( _success && _found ) { return (true, true, modules[_id].addr); }
        return (true, false, 0x00);
    }
    function getModuleIDByHash(bytes32 hashOfName) public constant returns( bool success, bool found, uint256 id ) {
        /*
            Search by hash of name in the module array. The result is an index array.
            
            @name       Name of module.
            @id         Index of module.
            @found      Was there any result or not.
        */
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            if ( modules[a].name == hashOfName ) {
                return (true, true, a);
            }
        }
        return (true, false, 0);
    }
    function getModuleIDByName(string name) public constant returns( bool success, bool found, uint256 id ) {
        /*
            Search by name for module. The result is an index array.
            
            @name       Name of module.
            @id         Index of module.
            @found      Was there any result or not.
        */
        bytes32 _name = sha3(name);
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            if ( modules[a].name == _name ) {
                return (true, true, a);
            }
        }
        return (true, false, 0);
    }
    function getModuleIDByAddress(address addr) public constant returns( bool success, bool found, uint256 id ) {
        /*
            Search by ethereum address for module. The result is an index array.
            
            @name       Name of module.
            @id         Index of module.
            @found      Was there any result or not.
        */
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            if ( modules[a].addr == addr ) {
                return (true, true, a);
            }
        }
        return (true, false, 0);
    }
    function replaceModule(string name, address addr, bool callCallback) external returns (bool success) {
        /*
            Module replace, can be called only by the Publisher contract.
            
            @name           Name of module.
            @addr           Address of module.
            @bool           Was there any result or not.
            @callCallback   Call the replaceable module to confirm replacement or not.
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success );
        if ( ! ( _found && modules[_id].name == sha3('Publisher') )) {
            require( block.number < debugModeUntil );
            if ( ! insertAndCheckDo(calcDoHash("replaceModule", sha3(name, addr, callCallback))) ) {
                return true;
            }
        }
        (_success, _found, _id) = getModuleIDByName(name);
        require( _success && _found );
        if ( callCallback ) {
            require( abstractModule(modules[_id].addr).replaceModule(addr) );
        }
        require( abstractModule(addr).connectModule() );
        modules[_id].addr = addr;
        return true;
    }
    
    function callReplaceCallback(string moduleName, address newModule) external returns (bool success) {
        require( block.number < debugModeUntil );
        if ( ! insertAndCheckDo(calcDoHash("callReplaceCallback", sha3(moduleName, newModule))) ) {
            return true;
        }
        var (_success, _found, _id) = getModuleIDByName(moduleName);
        require( _success);
        require( abstractModule(modules[_id].addr).replaceModule(newModule) );
        return true;
    }
    
    function newModule(string name, address addr, bool schellingEvent, bool transferEvent) external returns (bool success) {
        /*
            Adding new module to the database. Can be called only by the Publisher contract.
            
            @name               Name of module.
            @addr               Address of module.
            @schellingEvent     Gets it new Schelling round notification?
            @transferEvent      Gets it new transaction notification?
            @bool               Was there any result or not.
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success );
        if ( ! ( _found && modules[_id].name == sha3('Publisher') )) {
            require( block.number < debugModeUntil );
            if ( ! insertAndCheckDo(calcDoHash("newModule", sha3(name, addr, schellingEvent, transferEvent))) ) {
                return true;
            }
        }
        addModule( modules_s(addr, sha3(name), schellingEvent, transferEvent), true);
        return true;
    }
    function dropModule(string name, bool callCallback) external returns (bool success) {
        /*
            Deleting module from the database. Can be called only by the Publisher contract.
            
            @name           Name of module to delete.
            @bool           Was the function successfull?
            @callCallback   Call the replaceable module to confirm replacement or not.
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success );
        if ( ! ( _found && modules[_id].name == sha3('Publisher') )) {
            require( block.number < debugModeUntil );
            if ( ! insertAndCheckDo(calcDoHash("replaceModule", sha3(name, callCallback))) ) {
                return true;
            }
        }
        (_success, _found, _id) = getModuleIDByName(name);
        require( _success && _found );
        if( callCallback ) {
            abstractModule(modules[_id].addr).disableModule(true);
        }
        delete modules[_id];
        return true;
    }
    
    function callDisableCallback(string moduleName) external returns (bool success) {
        require( block.number < debugModeUntil );
        if ( ! insertAndCheckDo(calcDoHash("callDisableCallback", sha3(moduleName))) ) {
            return true;
        }
        var (_success, _found, _id) = getModuleIDByName(moduleName);
        require( _success);
        require( abstractModule(modules[_id].addr).disableModule(true) );
        return true;
    }
    
    function broadcastTransfer(address from, address to, uint256 value) external returns (bool success) {
        /*
            Announcing transactions for the modules.
            
            Can be called only by the token module.
            Only the configured modules get notifications.( transferEvent )
            
            @from       from who.
            @to         to who.
            @value      amount.
            @bool       Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found && modules[_id].name == sha3('Token') );
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            if ( modules[a].transferEvent && abstractModule(modules[a].addr).isActive() ) {
                require( abstractModule(modules[a].addr).transferEvent(from, to, value) );
            }
        }
        return true;
    }
    function broadcastSchellingRound(uint256 roundID, uint256 reward) external returns (bool success) {
        /*
            Announcing new Schelling round for the modules.
            Can be called only by the Schelling module.
            Only the configured modules get notifications( schellingEvent ).
            
            @roundID        Number of Schelling round.
            @reward         Coin emission in this Schelling round.
            @bool           Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found && modules[_id].name == sha3('Schelling') );
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            if ( modules[a].schellingEvent && abstractModule(modules[a].addr).isActive() ) {
                require( abstractModule(modules[a].addr).newSchellingRoundEvent(roundID, reward) );
            }
        }
        return true;
    }
    function replaceModuleHandler(address newHandler) external returns (bool success) {
        /*
            Replacing ModuleHandler.
            
            Can be called only by the publisher.
            Every module will be informed about the ModuleHandler replacement.
            
            @newHandler     Address of the new ModuleHandler.
            @bool           Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success );
        if ( ! ( _found && modules[_id].name == sha3('Publisher') )) {
            require( block.number < debugModeUntil );
            if ( ! insertAndCheckDo(calcDoHash("replaceModuleHandler", sha3(newHandler))) ) {
                return true;
            }
        }
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            require( abstractModule(modules[a].addr).replaceModuleHandler(newHandler) );
        }
        return true;
    }
    function balanceOf(address owner) public constant returns (bool success, uint256 value) {
        /*
            Query of token balance.
            
            @owner     address
            @value      balance.
            @success    was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        return (true, token(modules[_id].addr).balanceOf(owner));
    }
    function totalSupply() public constant returns (bool success, uint256 value) {
        /*
            Query of the whole token amount.
            
            @value      amount.
            @success    was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        return (true, token(modules[_id].addr).totalSupply());
    }
    function isICO() public constant returns (bool success, bool ico) {
        /*
            Query of ICO state
            
            @ico        Is ICO in progress?.
            @success    was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        return (true, token(modules[_id].addr).isICO());
    }
    function getCurrentSchellingRoundID() public constant returns (bool success, uint256 round) {
        /*
            Query of number of the actual Schelling round.
            
            @round      Schelling round.
            @success    was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByName('Schelling');
        require( _success && _found );
        return (true, schelling(modules[_id].addr).getCurrentSchellingRoundID());
    }
    function mint(address to, uint256 value) external returns (bool success) {
        /*
            Token emission request. Can be called only by the provider.
            
            @to         Place of new token
            @value      Token amount
            
            @success    Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found && modules[_id].name == sha3('Provider') );
        (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        require( token(modules[_id].addr).mint(to, value) );
        return true;
    }
    function transfer(address from, address to, uint256 value, bool fee) external returns (bool success) {
        /*
            Token transaction request. Can be called only by a module.
            
            @from       from who.
            @to         To who.
            @value      Token amount.
            @fee        Transaction fee will be charged or not?
            @success    Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found );
        (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        require( token(modules[_id].addr).transferFromByModule(from, to, value, fee) );
        return true;
    }
    function processTransactionFee(address from, uint256 value) external returns (bool success) {
        /*
            Token transaction fee. Can be called only by the provider.
            
            @from       From who.
            @value      Token amount.
            @success    Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found && modules[_id].name == sha3('Provider') );
        (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        require( token(modules[_id].addr).processTransactionFee(from, value) );
        return true;
    }
    function burn(address from, uint256 value) external returns (bool success) {
        /*
            Token burn. Can be called only by Schelling.
            
            @from       From who.
            @value      Token amount.
            @success    Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success && _found && modules[_id].name == sha3('Schelling') );
        (_success, _found, _id) = getModuleIDByName('Token');
        require( _success && _found );
        require( token(modules[_id].addr).burn(from, value) );
        return true;
    }
    function configureModule(string moduleName, announcementType aType, uint256 value) external returns (bool success) {
        /*
            Changing configuration of a module. Can be called only by Publisher or while debug mode by owners.
            
            @moduleName Module name which will be configured
            @aType      Type of variable (announcementType).
            @value      New value
            @success    Was the function successfull?
        */
        var (_success, _found, _id) = getModuleIDByAddress(msg.sender);
        require( _success );
        if ( ! ( _found && modules[_id].name == sha3('Publisher') )) {
            require( block.number < debugModeUntil );
            if ( ! insertAndCheckDo(calcDoHash("configureModule", sha3(moduleName, aType, value))) ) {
                return true;
            }
        }
        (_success, _found, _id) = getModuleIDByName(moduleName);
        require( _success && _found );
        require( schelling(modules[_id].addr).configure(aType, value) );
        return true;
    }
    function freezing(bool forever) external {
        /*
            Freezing CORION Platform. Can be called only by the owner.
            Freez can not be recalled!
            
            @forever    Is it forever or not?
        */
        require( owners[msg.sender] );
        if ( forever ) {
            if ( ! insertAndCheckDo(calcDoHash("freezing", sha3(forever))) ) {
                return;
            }            
        }
        for ( uint256 a=0 ; a<modules.length ; a++ ) {
            require( abstractModule(modules[a].addr).disableModule(forever) );
        }
    }
}