Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #68 from bodhiproject/trim_code
Browse files Browse the repository at this point in the history
bettingStartBlock and resultSettingStartBlock + fix tests
  • Loading branch information
dwalintukan authored Dec 28, 2017
2 parents 55bffa4 + 4aade5e commit 9f34e59
Show file tree
Hide file tree
Showing 18 changed files with 1,107 additions and 1,453 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addons:
- g++-4.8

install:
- npm install -g truffle@^4.0.1
- npm install -g truffle@4.0.4
- npm install

script:
Expand Down
Binary file modified architecture_flowchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 24 additions & 10 deletions contracts/events/EventFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract EventFactory {

// Events
event TopicCreated(address indexed _topicAddress, address indexed _creator, address indexed _oracle,
bytes32[10] _name, bytes32[10] _resultNames, uint256 _bettingEndBlock, uint256 _resultSettingEndBlock);
bytes32[10] _name, bytes32[10] _resultNames);

function EventFactory(address _addressManager) public {
require(_addressManager != address(0));
Expand All @@ -21,41 +21,55 @@ contract EventFactory {
address _oracle,
bytes32[10] _name,
bytes32[10] _resultNames,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock)
public
returns (TopicEvent topicEvent)
{
bytes32 topicHash = getTopicHash(_name, _resultNames, _bettingEndBlock, _resultSettingEndBlock);
bytes32 topicHash = getTopicHash(_name, _resultNames, _bettingStartBlock, _bettingEndBlock,
_resultSettingStartBlock, _resultSettingEndBlock);
// Topic should not exist yet
require(address(topics[topicHash]) == 0);

TopicEvent topic = new TopicEvent(msg.sender, _oracle, _name, _resultNames, _bettingEndBlock,
_resultSettingEndBlock, addressManager);
TopicEvent topic = new TopicEvent(msg.sender, _oracle, _name, _resultNames, _bettingStartBlock, _bettingEndBlock,
_resultSettingStartBlock, _resultSettingEndBlock, addressManager);
topics[topicHash] = topic;

TopicCreated(address(topic), msg.sender, _oracle, _name, _resultNames, _bettingEndBlock,
_resultSettingEndBlock);
TopicCreated(address(topic), msg.sender, _oracle, _name, _resultNames);

return topic;
}

function doesTopicExist(bytes32[10] _name, bytes32[10] _resultNames, uint256 _bettingEndBlock,
function doesTopicExist(
bytes32[10] _name,
bytes32[10] _resultNames,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock)
public
constant
returns (bool)
{
bytes32 topicHash = getTopicHash(_name, _resultNames, _bettingEndBlock, _resultSettingEndBlock);
bytes32 topicHash = getTopicHash(_name, _resultNames, _bettingStartBlock, _bettingEndBlock,
_resultSettingStartBlock, _resultSettingEndBlock);
return address(topics[topicHash]) != 0;
}

function getTopicHash(bytes32[10] _name, bytes32[10] _resultNames, uint256 _bettingEndBlock,
function getTopicHash(
bytes32[10] _name,
bytes32[10] _resultNames,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock)
internal
pure
returns (bytes32)
{
return keccak256(_name, _resultNames, _bettingEndBlock, _resultSettingEndBlock);
return keccak256(_name, _resultNames, _bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock,
_resultSettingEndBlock);
}
}
20 changes: 14 additions & 6 deletions contracts/events/TopicEvent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ contract TopicEvent is ITopicEvent, Ownable {
* @param _centralizedOracle The address of the CentralizedOracle that will decide the result.
* @param _name The question or statement prediction broken down by multiple bytes32.
* @param _resultNames The possible results.
* @param _bettingStartBlock The block when betting will start.
* @param _bettingEndBlock The block when betting will end.
* @param _resultSettingEndBlock The last block the Centralized Oracle can set the result.
* @param _resultSettingStartBlock The first block the CentralizedOracle can set the result.
* @param _resultSettingEndBlock The last block the CentralizedOracle can set the result.
* @param _addressManager The address of the AddressManager.
*/
function TopicEvent(
address _owner,
address _centralizedOracle,
bytes32[10] _name,
bytes32[10] _resultNames,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
address _addressManager)
Ownable(_owner)
Expand All @@ -103,8 +107,9 @@ contract TopicEvent is ITopicEvent, Ownable {
require(!_name[0].isEmpty());
require(!_resultNames[0].isEmpty());
require(!_resultNames[1].isEmpty());
require(_bettingEndBlock > block.number);
require(_resultSettingEndBlock > _bettingEndBlock);
require(_bettingEndBlock > _bettingStartBlock);
require(_resultSettingStartBlock >= _bettingEndBlock);
require(_resultSettingEndBlock > _resultSettingStartBlock);

owner = _owner;
name = _name;
Expand All @@ -125,7 +130,8 @@ contract TopicEvent is ITopicEvent, Ownable {
addressManager = IAddressManager(_addressManager);
token = ERC20(addressManager.bodhiTokenAddress());

createCentralizedOracle(_centralizedOracle, _bettingEndBlock, _resultSettingEndBlock);
createCentralizedOracle(_centralizedOracle, _bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock,
_resultSettingEndBlock);
}

/// @notice Fallback function that rejects any amount sent to the contract.
Expand Down Expand Up @@ -417,15 +423,17 @@ contract TopicEvent is ITopicEvent, Ownable {

function createCentralizedOracle(
address _centralizedOracle,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock)
private
{
uint16 index = addressManager.getLastOracleFactoryIndex();
address oracleFactory = addressManager.getOracleFactoryAddress(index);
address newOracle = IOracleFactory(oracleFactory).createCentralizedOracle(_centralizedOracle, address(this),
name, resultNames, numOfResults, _bettingEndBlock, _resultSettingEndBlock,
addressManager.startingOracleThreshold());
name, resultNames, numOfResults, _bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock,
_resultSettingEndBlock, addressManager.startingOracleThreshold());

assert(newOracle != address(0));
oracles.push(Oracle({
Expand Down
18 changes: 14 additions & 4 deletions contracts/oracles/CentralizedOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import "./Oracle.sol";

contract CentralizedOracle is Oracle {
address public oracle;
uint256 public bettingStartBlock;
uint256 public bettingEndBlock;
uint256 public resultSettingStartBlock;
uint256 public resultSettingEndBlock;

/*
Expand All @@ -15,8 +17,10 @@ contract CentralizedOracle is Oracle {
* @param _eventName The name of the Event.
* @param _eventResultNames The result options of the Event.
* @param _numOfResults The number of result options.
* @param _bettingStartBlock The block when betting will start.
* @param _bettingEndBlock The block when betting will end.
* @param _resultSettingEndBlock The last block the Centralized Oracle can set the result.
* @param _resultSettingStartBlock The first block the CentralizedOracle can set the result.
* @param _resultSettingEndBlock The last block the CentralizedOracle can set the result.
* @param _consensusThreshold The BOT amount that needs to be paid by the Oracle for their result to be valid.
*/
function CentralizedOracle(
Expand All @@ -26,7 +30,9 @@ contract CentralizedOracle is Oracle {
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold)
Ownable(_owner)
Expand All @@ -38,16 +44,19 @@ contract CentralizedOracle is Oracle {
require(!_eventResultNames[0].isEmpty());
require(!_eventResultNames[1].isEmpty());
require(_numOfResults > 0);
require(_bettingEndBlock > block.number);
require(_resultSettingEndBlock > _bettingEndBlock);
require(_bettingEndBlock > _bettingStartBlock);
require(_resultSettingStartBlock >= _bettingEndBlock);
require(_resultSettingEndBlock > _resultSettingStartBlock);
require(_consensusThreshold > 0);

oracle = _oracle;
eventAddress = _eventAddress;
eventName = _eventName;
eventResultNames = _eventResultNames;
numOfResults = _numOfResults;
bettingStartBlock = _bettingStartBlock;
bettingEndBlock = _bettingEndBlock;
resultSettingStartBlock = _resultSettingStartBlock;
resultSettingEndBlock = _resultSettingEndBlock;
consensusThreshold = _consensusThreshold;
}
Expand All @@ -67,6 +76,7 @@ contract CentralizedOracle is Oracle {
validResultIndex(_resultIndex)
isNotFinished()
{
require(block.number >= bettingStartBlock);
require(block.number < bettingEndBlock);
require(msg.value > 0);

Expand All @@ -87,7 +97,7 @@ contract CentralizedOracle is Oracle {
validResultIndex(_resultIndex)
isNotFinished()
{
require(block.number >= bettingEndBlock);
require(block.number >= resultSettingStartBlock);
if (block.number < resultSettingEndBlock) {
require(msg.sender == oracle);
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/oracles/IOracleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ contract IOracleFactory {
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold)
public returns (address);
Expand Down
53 changes: 13 additions & 40 deletions contracts/oracles/OracleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ contract OracleFactory is IOracleFactory {
bytes32[10] _name,
bytes32[10] _resultNames,
uint8 _numOfResults,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold);
event DecentralizedOracleCreated(
Expand Down Expand Up @@ -45,23 +47,27 @@ contract OracleFactory is IOracleFactory {
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold)
public
returns (address)
{
bytes32 oracleHash = getCentralizedOracleHash(_oracle, _eventAddress, _eventName, _eventResultNames,
_numOfResults, _bettingEndBlock, _resultSettingEndBlock, _consensusThreshold);
_numOfResults, _bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock, _resultSettingEndBlock,
_consensusThreshold);
// CentralizedOracle should not exist yet
require(oracles[oracleHash] == address(0));

CentralizedOracle oracle = new CentralizedOracle(msg.sender, _oracle, _eventAddress, _eventName,
_eventResultNames, _numOfResults, _bettingEndBlock, _resultSettingEndBlock, _consensusThreshold);
_eventResultNames, _numOfResults, _bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock,
_resultSettingEndBlock, _consensusThreshold);
oracles[oracleHash] = address(oracle);

CentralizedOracleCreated(address(oracle), _oracle, _eventAddress, _eventName, _eventResultNames, _numOfResults,
_bettingEndBlock, _resultSettingEndBlock, _consensusThreshold);
_bettingStartBlock, _bettingEndBlock, _resultSettingStartBlock, _resultSettingEndBlock, _consensusThreshold);

return address(oracle);
}
Expand Down Expand Up @@ -92,56 +98,23 @@ contract OracleFactory is IOracleFactory {
return address(oracle);
}

function doesCentralizedOracleExist(
address _oracle,
address _eventAddress,
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint256 _bettingEndBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold)
public
view
returns (bool)
{
bytes32 oracleHash = getCentralizedOracleHash(_oracle, _eventAddress, _eventName, _eventResultNames,
_numOfResults, _bettingEndBlock, _resultSettingEndBlock, _consensusThreshold);
return oracles[oracleHash] != address(0);
}

function doesDecentralizedOracleExist(
address _eventAddress,
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint8 _lastResultIndex,
uint256 _arbitrationEndBlock,
uint256 _consensusThreshold)
public
view
returns (bool)
{
bytes32 oracleHash = getDecentralizedOracleHash(_eventAddress, _eventName, _eventResultNames, _numOfResults,
_lastResultIndex, _arbitrationEndBlock, _consensusThreshold);
return oracles[oracleHash] != address(0);
}

function getCentralizedOracleHash(
address _oracle,
address _eventAddress,
bytes32[10] _eventName,
bytes32[10] _eventResultNames,
uint8 _numOfResults,
uint256 _bettingStartBlock,
uint256 _bettingEndBlock,
uint256 _resultSettingStartBlock,
uint256 _resultSettingEndBlock,
uint256 _consensusThreshold)
private
pure
returns (bytes32)
{
return keccak256(_oracle, _eventAddress, _eventName, _eventResultNames, _numOfResults, _bettingEndBlock,
_resultSettingEndBlock, _consensusThreshold);
return keccak256(_oracle, _eventAddress, _eventName, _eventResultNames, _numOfResults, _bettingStartBlock,
_bettingEndBlock, _resultSettingStartBlock, _resultSettingEndBlock, _consensusThreshold);
}

function getDecentralizedOracleHash(
Expand Down
Loading

0 comments on commit 9f34e59

Please sign in to comment.