forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ethereum#67 from thanhson1085/master
add randomize contract
- Loading branch information
Showing
5 changed files
with
692 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
pragma solidity ^0.4.21; | ||
|
||
import "./libs/SafeMath.sol"; | ||
|
||
contract TomoRandomize { | ||
using SafeMath for uint256; | ||
uint256 public epochNumber; | ||
uint256 public blockTimeSecret; | ||
uint256 public blockTimeOpening; | ||
|
||
mapping (address=>bytes32[]) randomSecret; | ||
mapping (address=>bytes32[]) randomOpening; | ||
|
||
function TomoRandomize (uint256 _epochNumber, uint256 _blockTimeSecret, uint256 _blockTimeOpening) public { | ||
epochNumber = _epochNumber; | ||
blockTimeOpening = _blockTimeOpening; | ||
blockTimeSecret = _blockTimeSecret; | ||
} | ||
|
||
function setSecret(bytes32[] _secret) public { | ||
require(_secret.length == epochNumber); | ||
|
||
uint256 _blockNum = block.number; | ||
uint256 _epoch = _blockNum.sub(_blockNum.div(epochNumber).mul(epochNumber)); | ||
|
||
require(_epoch <= blockTimeSecret); | ||
|
||
randomSecret[msg.sender] = _secret; | ||
} | ||
|
||
function setOpening(bytes32[] _opening) public { | ||
require(_opening.length == epochNumber); | ||
|
||
uint256 _blockNum = block.number; | ||
uint256 _epoch = _blockNum.sub(_blockNum.div(epochNumber).mul(epochNumber)); | ||
|
||
require(_epoch > blockTimeSecret && _epoch <= blockTimeOpening); | ||
|
||
randomOpening[msg.sender] = _opening; | ||
|
||
} | ||
|
||
function getSecret(address _validator) public view returns(bytes32[]) { | ||
uint256 _blockNum = block.number; | ||
uint256 _epoch = _blockNum.sub(_blockNum.div(epochNumber).mul(epochNumber)); | ||
|
||
require(_epoch > blockTimeSecret); | ||
|
||
return randomSecret[_validator]; | ||
} | ||
|
||
function getOpening(address _validator) public view returns(bytes32[]) { | ||
uint256 _blockNum = block.number; | ||
uint256 _epoch = _blockNum.sub(_blockNum.div(epochNumber).mul(epochNumber)); | ||
|
||
require(_epoch > blockTimeOpening); | ||
|
||
return randomOpening[_validator]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pragma solidity ^0.4.21; | ||
|
||
/** | ||
* @title SafeMath | ||
* @dev Math operations with safety checks that throw on error | ||
*/ | ||
library SafeMath { | ||
|
||
/** | ||
* @dev Multiplies two numbers, throws on overflow. | ||
*/ | ||
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | ||
if (a == 0) { | ||
return 0; | ||
} | ||
uint256 c = a * b; | ||
assert(c / a == b); | ||
return c; | ||
} | ||
|
||
/** | ||
* @dev Integer division of two numbers, truncating the quotient. | ||
*/ | ||
function div(uint256 a, uint256 b) internal pure returns (uint256) { | ||
// assert(b > 0); // Solidity automatically throws when dividing by 0 | ||
// uint256 c = a / b; | ||
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | ||
return a / b; | ||
} | ||
|
||
/** | ||
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). | ||
*/ | ||
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | ||
assert(b <= a); | ||
return a - b; | ||
} | ||
|
||
/** | ||
* @dev Adds two numbers, throws on overflow. | ||
*/ | ||
function add(uint256 a, uint256 b) internal pure returns (uint256) { | ||
uint256 c = a + b; | ||
assert(c >= a); | ||
return c; | ||
} | ||
} |
Oops, something went wrong.