Skip to content

Commit

Permalink
Merge pull request ethereum#67 from thanhson1085/master
Browse files Browse the repository at this point in the history
add randomize contract
  • Loading branch information
ngtuna authored Jun 29, 2018
2 parents 77966cc + c08cc09 commit 33f47f1
Show file tree
Hide file tree
Showing 5 changed files with 692 additions and 0 deletions.
60 changes: 60 additions & 0 deletions contracts/randomize/contract/TomoRandomize.sol
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];
}
}
47 changes: 47 additions & 0 deletions contracts/randomize/contract/libs/SafeMath.sol
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;
}
}
Loading

0 comments on commit 33f47f1

Please sign in to comment.