solidity/test/compilationTests/corion/moduleHandler.sol

449 lines
19 KiB
Solidity
Raw Normal View History

2018-10-24 12:52:11 +00:00
pragma solidity >=0.0;
2017-07-05 10:28:15 +00:00
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) {}
2018-07-02 09:14:28 +00:00
function isActive() public view returns (bool success) {}
2017-07-05 10:28:15 +00:00
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 {
2017-07-05 10:28:15 +00:00
struct modules_s {
address payable addr;
2017-07-05 10:28:15 +00:00
bytes32 name;
bool schellingEvent;
bool transferEvent;
}
2017-07-05 10:28:15 +00:00
modules_s[] public modules;
address public foundationAddress;
uint256 debugModeUntil = block.number + 1000000;
2018-07-04 17:20:51 +00:00
2020-06-23 12:14:24 +00:00
constructor(address[] memory newOwners) multiOwner(newOwners) {}
function load(address payable foundation, bool forReplace, address payable Token, address payable Premium, address payable Publisher, address payable Schelling, address payable Provider) public {
2017-07-05 10:28:15 +00:00
/*
Loading modulest to ModuleHandler.
2017-07-05 10:28:15 +00:00
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.
2017-07-05 10:28:15 +00:00
@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, keccak256('Token'), false, false), ! forReplace);
addModule( modules_s(Premium, keccak256('Premium'), false, false), ! forReplace);
addModule( modules_s(Publisher, keccak256('Publisher'), false, true), ! forReplace);
addModule( modules_s(Schelling, keccak256('Schelling'), false, true), ! forReplace);
addModule( modules_s(Provider, keccak256('Provider'), true, true), ! forReplace);
2017-07-05 10:28:15 +00:00
}
function addModule(modules_s memory input, bool call) internal {
2017-07-05 10:28:15 +00:00
/*
Inside function for registration of the modules in the database.
If the call is false, won't happen any direct call.
2017-07-05 10:28:15 +00:00
@input _Structure of module.
@call Is connect to the module or not.
*/
if ( call ) { require( abstractModule(input.addr).connectModule() ); }
(bool success, bool found, uint256 id) = getModuleIDByAddress(input.addr);
2017-07-05 10:28:15 +00:00
require( success && ! found );
(success, found, id) = getModuleIDByHash(input.name);
require( success && ! found );
(success, found, id) = getModuleIDByAddress(address(0x00));
2017-07-05 10:28:15 +00:00
require( success );
if ( ! found ) {
id = modules.length;
modules.push();
2017-07-05 10:28:15 +00:00
}
modules[id] = input;
}
function getModuleAddressByName(string memory name) public view returns( bool success, bool found, address addr ) {
2017-07-05 10:28:15 +00:00
/*
Search by name for module. The result is an Ethereum address.
2017-07-05 10:28:15 +00:00
@name Name of module.
@addr Address of module.
@found Is there any result.
@success Was the transaction successful or not.
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByName(name);
2017-07-05 10:28:15 +00:00
if ( _success && _found ) { return (true, true, modules[_id].addr); }
return (true, false, address(0x00));
2017-07-05 10:28:15 +00:00
}
2018-07-02 09:14:28 +00:00
function getModuleIDByHash(bytes32 hashOfName) public view returns( bool success, bool found, uint256 id ) {
2017-07-05 10:28:15 +00:00
/*
Search by hash of name in the module array. The result is an index array.
2017-07-05 10:28:15 +00:00
@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 memory name) public view returns( bool success, bool found, uint256 id ) {
2017-07-05 10:28:15 +00:00
/*
Search by name for module. The result is an index array.
2017-07-05 10:28:15 +00:00
@name Name of module.
@id Index of module.
@found Was there any result or not.
*/
2018-06-14 10:28:33 +00:00
bytes32 _name = keccak256(bytes(name));
2017-07-05 10:28:15 +00:00
for ( uint256 a=0 ; a<modules.length ; a++ ) {
if ( modules[a].name == _name ) {
return (true, true, a);
}
}
return (true, false, 0);
}
2018-07-02 09:14:28 +00:00
function getModuleIDByAddress(address addr) public view returns( bool success, bool found, uint256 id ) {
2017-07-05 10:28:15 +00:00
/*
Search by ethereum address for module. The result is an index array.
2017-07-05 10:28:15 +00:00
@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 calldata name, address payable addr, bool callCallback) external returns (bool success) {
2017-07-05 10:28:15 +00:00
/*
Module replace, can be called only by the Publisher contract.
2017-07-05 10:28:15 +00:00
@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.
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
require( _success );
if ( ! ( _found && modules[_id].name == keccak256('Publisher') )) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("replaceModule", keccak256(abi.encodePacked(name, addr, callCallback)))) ) {
2017-07-05 10:28:15 +00:00
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;
}
2018-08-07 20:12:52 +00:00
function callReplaceCallback(string calldata moduleName, address newModule) external returns (bool success) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("callReplaceCallback", keccak256(abi.encodePacked(moduleName, newModule)))) ) {
2017-07-05 10:28:15 +00:00
return true;
}
(bool _success, bool _found, uint256 _id) = getModuleIDByName(moduleName);
2017-07-05 10:28:15 +00:00
require( _success);
require( abstractModule(modules[_id].addr).replaceModule(newModule) );
return true;
}
function newModule(string calldata name, address payable addr, bool schellingEvent, bool transferEvent) external returns (bool success) {
2017-07-05 10:28:15 +00:00
/*
Adding new module to the database. Can be called only by the Publisher contract.
2017-07-05 10:28:15 +00:00
@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.
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
require( _success );
if ( ! ( _found && modules[_id].name == keccak256('Publisher') )) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("newModule", keccak256(abi.encodePacked(name, addr, schellingEvent, transferEvent)))) ) {
2017-07-05 10:28:15 +00:00
return true;
}
}
2018-06-14 10:28:33 +00:00
addModule( modules_s(addr, keccak256(bytes(name)), schellingEvent, transferEvent), true);
2017-07-05 10:28:15 +00:00
return true;
}
2018-08-07 20:12:52 +00:00
function dropModule(string calldata name, bool callCallback) external returns (bool success) {
2017-07-05 10:28:15 +00:00
/*
Deleting module from the database. Can be called only by the Publisher contract.
2017-07-05 10:28:15 +00:00
@name Name of module to delete.
@bool Was the function successful?
2017-07-05 10:28:15 +00:00
@callCallback Call the replaceable module to confirm replacement or not.
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
require( _success );
if ( ! ( _found && modules[_id].name == keccak256('Publisher') )) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("replaceModule", keccak256(abi.encodePacked(name, callCallback)))) ) {
2017-07-05 10:28:15 +00:00
return true;
}
}
(_success, _found, _id) = getModuleIDByName(name);
require( _success && _found );
if( callCallback ) {
abstractModule(modules[_id].addr).disableModule(true);
}
delete modules[_id];
return true;
}
2018-08-07 20:12:52 +00:00
function callDisableCallback(string calldata moduleName) external returns (bool success) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("callDisableCallback", keccak256(bytes(moduleName)))) ) {
2017-07-05 10:28:15 +00:00
return true;
}
(bool _success, bool _found, uint256 _id) = getModuleIDByName(moduleName);
2017-07-05 10:28:15 +00:00
require( _success);
require( abstractModule(modules[_id].addr).disableModule(true) );
return true;
}
2017-07-05 10:28:15 +00:00
function broadcastTransfer(address from, address to, uint256 value) external returns (bool success) {
/*
Announcing transactions for the modules.
2017-07-05 10:28:15 +00:00
Can be called only by the token module.
Only the configured modules get notifications.( transferEvent )
2017-07-05 10:28:15 +00:00
@from from who.
@to to who.
@value amount.
@bool Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
require( _success && _found && modules[_id].name == keccak256('Token') );
2017-07-05 10:28:15 +00:00
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 ).
2017-07-05 10:28:15 +00:00
@roundID Number of Schelling round.
@reward Coin emission in this Schelling round.
@bool Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
require( _success && _found && modules[_id].name == keccak256('Schelling') );
2017-07-05 10:28:15 +00:00
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.
2017-07-05 10:28:15 +00:00
Can be called only by the publisher.
Every module will be informed about the ModuleHandler replacement.
2017-07-05 10:28:15 +00:00
@newHandler Address of the new ModuleHandler.
@bool Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
require( _success );
if ( ! ( _found && modules[_id].name == keccak256('Publisher') )) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("replaceModuleHandler", keccak256(abi.encodePacked(newHandler)))) ) {
2017-07-05 10:28:15 +00:00
return true;
}
}
for ( uint256 a=0 ; a<modules.length ; a++ ) {
require( abstractModule(modules[a].addr).replaceModuleHandler(newHandler) );
}
return true;
}
2018-07-02 09:14:28 +00:00
function balanceOf(address owner) public view returns (bool success, uint256 value) {
2017-07-05 10:28:15 +00:00
/*
Query of token balance.
2017-07-05 10:28:15 +00:00
@owner address
@value balance.
@success was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByName('Token');
2017-07-05 10:28:15 +00:00
require( _success && _found );
return (true, token(modules[_id].addr).balanceOf(owner));
}
2018-07-02 09:14:28 +00:00
function totalSupply() public view returns (bool success, uint256 value) {
2017-07-05 10:28:15 +00:00
/*
Query of the whole token amount.
2017-07-05 10:28:15 +00:00
@value amount.
@success was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByName('Token');
2017-07-05 10:28:15 +00:00
require( _success && _found );
return (true, token(modules[_id].addr).totalSupply());
}
2018-07-02 09:14:28 +00:00
function isICO() public view returns (bool success, bool ico) {
2017-07-05 10:28:15 +00:00
/*
Query of ICO state
2017-07-05 10:28:15 +00:00
@ico Is ICO in progress?.
@success was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByName('Token');
2017-07-05 10:28:15 +00:00
require( _success && _found );
return (true, token(modules[_id].addr).isICO());
}
2018-07-02 09:14:28 +00:00
function getCurrentSchellingRoundID() public view returns (bool success, uint256 round) {
2017-07-05 10:28:15 +00:00
/*
Query of number of the actual Schelling round.
2017-07-05 10:28:15 +00:00
@round Schelling round.
@success was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByName('Schelling');
2017-07-05 10:28:15 +00:00
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.
2017-07-05 10:28:15 +00:00
@to Place of new token
@value Token amount
@success Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
require( _success && _found && modules[_id].name == keccak256('Provider') );
2017-07-05 10:28:15 +00:00
(_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.
2017-07-05 10:28:15 +00:00
@from from who.
@to To who.
@value Token amount.
@fee Transaction fee will be charged or not?
@success Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
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.
2017-07-05 10:28:15 +00:00
@from From who.
@value Token amount.
@success Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
require( _success && _found && modules[_id].name == keccak256('Provider') );
2017-07-05 10:28:15 +00:00
(_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.
2017-07-05 10:28:15 +00:00
@from From who.
@value Token amount.
@success Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
require( _success && _found && modules[_id].name == keccak256('Schelling') );
2017-07-05 10:28:15 +00:00
(_success, _found, _id) = getModuleIDByName('Token');
require( _success && _found );
require( token(modules[_id].addr).burn(from, value) );
return true;
}
2018-08-07 20:12:52 +00:00
function configureModule(string calldata moduleName, announcementType aType, uint256 value) external returns (bool success) {
2017-07-05 10:28:15 +00:00
/*
Changing configuration of a module. Can be called only by Publisher or while debug mode by owners.
2017-07-05 10:28:15 +00:00
@moduleName Module name which will be configured
@aType Type of variable (announcementType).
@value New value
@success Was the function successful?
2017-07-05 10:28:15 +00:00
*/
(bool _success, bool _found, uint256 _id) = getModuleIDByAddress(msg.sender);
2017-07-05 10:28:15 +00:00
require( _success );
if ( ! ( _found && modules[_id].name == keccak256('Publisher') )) {
2017-07-05 10:28:15 +00:00
require( block.number < debugModeUntil );
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("configureModule", keccak256(abi.encodePacked(moduleName, aType, value)))) ) {
2017-07-05 10:28:15 +00:00
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.
Freeze can not be recalled!
2017-07-05 10:28:15 +00:00
@forever Is it forever or not?
*/
require( owners[msg.sender] );
if ( forever ) {
2018-06-14 10:28:33 +00:00
if ( ! insertAndCheckDo(calcDoHash("freezing", keccak256(abi.encodePacked(forever)))) ) {
2017-07-05 10:28:15 +00:00
return;
}
2017-07-05 10:28:15 +00:00
}
for ( uint256 a=0 ; a<modules.length ; a++ ) {
require( abstractModule(modules[a].addr).disableModule(forever) );
}
}
}