solidity/test/compilationTests/corion/multiOwner.sol

84 lines
2.3 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 "./safeMath.sol";
contract multiOwner is safeMath {
2017-07-05 10:28:15 +00:00
mapping(address => bool) public owners;
uint256 public ownerCount;
mapping(bytes32 => address[]) public doDB;
2017-07-05 10:28:15 +00:00
/*
Constructor
*/
2020-06-23 12:14:24 +00:00
constructor(address[] memory newOwners) {
2017-07-05 10:28:15 +00:00
for ( uint256 a=0 ; a<newOwners.length ; a++ ) {
_addOwner(newOwners[a]);
}
}
/*
Externals
*/
function insertOwner(address addr) external {
2018-06-14 10:28:33 +00:00
if ( insertAndCheckDo(calcDoHash("insertOwner", keccak256(abi.encodePacked(addr)))) ) {
2017-07-05 10:28:15 +00:00
_addOwner(addr);
}
}
function dropOwner(address addr) external {
2018-06-14 10:28:33 +00:00
if ( insertAndCheckDo(calcDoHash("dropOwner", keccak256(abi.encodePacked(addr)))) ) {
2017-07-05 10:28:15 +00:00
_delOwner(addr);
}
}
function cancelDo(bytes32 doHash) external {
if ( insertAndCheckDo(calcDoHash("cancelDo", doHash)) ) {
delete doDB[doHash];
}
}
/*
Constants
*/
2018-07-02 09:14:28 +00:00
function ownersForChange() public view returns (uint256 owners) {
2017-07-05 10:28:15 +00:00
return ownerCount * 75 / 100;
}
function calcDoHash(string memory job, bytes32 data) public pure returns (bytes32 hash) {
2018-06-14 10:28:33 +00:00
return keccak256(abi.encodePacked(job, data));
2017-07-05 10:28:15 +00:00
}
2018-07-02 09:14:28 +00:00
function validDoHash(bytes32 doHash) public view returns (bool valid) {
2017-07-05 10:28:15 +00:00
return doDB[doHash].length > 0;
}
/*
Internals
*/
function insertAndCheckDo(bytes32 doHash) internal returns (bool success) {
require( owners[msg.sender] );
if (doDB[doHash].length >= ownersForChange()) {
delete doDB[doHash];
return true;
}
for ( uint256 a=0 ; a<doDB[doHash].length ; a++ ) {
require( doDB[doHash][a] != msg.sender );
}
if ( doDB[doHash].length+1 >= ownersForChange() ) {
delete doDB[doHash];
return true;
} else {
doDB[doHash].push(msg.sender);
return false;
}
}
/*
Privates
*/
function _addOwner(address addr) private {
if ( owners[addr] ) { return; }
owners[addr] = true;
ownerCount = safeAdd(ownerCount, 1);
}
function _delOwner(address addr) private {
if ( ! owners[addr] ) { return; }
delete owners[addr];
ownerCount = safeSub(ownerCount, 1);
}
}