Skip to content

Commit

Permalink
Merge pull request #38 from maxkudla/update-random-engine
Browse files Browse the repository at this point in the history
update random engine logic, add test
  • Loading branch information
Koroqe committed Jun 27, 2018
2 parents 52ebe66 + e1db4ab commit fed92c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions contracts/pandora/lottery/RandomEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ pragma solidity ^0.4.23;
import "./ILotteryEngine.sol";

contract RandomEngine is ILotteryEngine {
bytes32 baseHash;
uint256 lastBlockNumber;

constructor()
public {
lastBlockNumber = block.number;
}

// Current implementation able to return one unique value per block
//todo add request count dependency
function getRandom(uint256 _max)
public
returns (uint256 o_result) {
o_result = (uint(blockhash(block.number)) + uint(blockhash(block.number - 10))) % _max;
if (block.number > lastBlockNumber) {
lastBlockNumber = block.number;
baseHash = keccak256((uint256(blockhash(block.number)) + uint256(blockhash(block.number - 10))));
} else {
baseHash = keccak256(baseHash);
}
o_result = uint256(baseHash) % _max;
}
}
19 changes: 19 additions & 0 deletions test/test_pandora_lottery_random_engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const RandomEngine = artifacts.require('RandomEngine');

contract('RandomEngine', accounts => {

let max = 9999;
let engine;

before('setup test random engine', async () => {
engine = await RandomEngine.new();
});

it('should return different numbers each call', async () => {
const [r1, r2] = await Promise.all([
engine.getRandom.call(max),
engine.getRandom.call(max),
]);
assert.isOk(r1 !== r2, 'random is the same as previous');
});
});

0 comments on commit fed92c6

Please sign in to comment.