Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: First steps towards WRBv2 #295

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions README.md

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions contracts/WitnetRequestBoard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@ abstract contract WitnetRequestBoard is
IWitnetRequestBoardRequestor,
IWitnetRequestBoardView,
IWitnetRequestParser
{
receive() external payable {
revert("WitnetRequestBoard: no transfers accepted");
}
}
{}
25 changes: 25 additions & 0 deletions contracts/WitnetRequestBoardV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0 <0.9.0;

import "./interfaces/V2/IWitnetBlocks.sol";
import "./interfaces/V2/IWitnetBytecodes.sol";
import "./interfaces/V2/IWitnetDecoder.sol";

// import "./interfaces/V2/IWitnetReporting.sol";
import "./interfaces/V2/IWitnetRequests.sol";
//import "./interfaces/V2/IWitnetTraps.sol";

/// @title Witnet Request Board V2 functionality base contract.
/// @author The Witnet Foundation.
abstract contract WitnetRequestBoardV2 is
IWitnetRequests
//, IWitnetReporting
//, IWitnetTraps
{
function blocks() virtual external view returns (IWitnetBlocks);

function bytecodes() virtual external view returns (IWitnetBytecodes);

function decoder() virtual external view returns (IWitnetDecoder);
}
6 changes: 0 additions & 6 deletions contracts/data/WitnetBoardData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ abstract contract WitnetBoardData {
_;
}

/// Asserts caller corresponds to the current owner.
modifier onlyOwner {
require(msg.sender == _state().owner, "WitnetBoardData: only owner");
_;
}

/// Asserts the give query was actually posted before calling this method.
modifier wasPosted(uint256 _queryId) {
require(_queryId > 0 && _queryId <= _state().numQueries, "WitnetBoardData: not yet posted");
Expand Down
102 changes: 102 additions & 0 deletions contracts/data/WitnetReporting1Data.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0 <0.9.0;

import "../interfaces/V2/IWitnetReporting1.sol";
import "../interfaces/V2/IWitnetReporting1Admin.sol";

/// @title Witnet Request Board base data model.
/// @author The Witnet Foundation.
abstract contract WitnetReporting1Data
is
IWitnetReporting1,
IWitnetReporting1Admin
{

bytes32 internal constant _WITNET_REPORTING_1_DATA_SLOTHASH =
/* keccak256("io.witnet.boards.data.v2.reporting.1") */
0x32ecea6ea7fbc6d7e8c8041c5ecf898bf8d40bd92da1207bebee19461a94c7bd;

struct Escrow {
uint256 index;
uint256 weiSignUpFee;
uint256 lastSignUpBlock;
uint256 lastSignOutBlock;
uint256 lastSlashBlock;
}

struct Reporting {
uint256 totalReporters;
IWitnetReporting1.SignUpConfig settings;
address[] reporters;
mapping (address => Escrow) escrows;
}

// --- Internal view functions



// ================================================================================================================
// --- Internal state-modifying functions -------------------------------------------------------------------------

function __deleteReporterAddressByIndex(uint _index)
internal
returns (uint256 _totalReporters)
{
_totalReporters = __reporting().totalReporters;
if (_index >= _totalReporters) {
revert WitnetV2.IndexOutOfBounds(_index, _totalReporters);
}
else if (_totalReporters > 1 && _index < _totalReporters - 1) {
address _latestReporterAddress = __reporting().reporters[_totalReporters - 1];
Escrow storage __latestReporterEscrow = __reporting().escrows[_latestReporterAddress];
__latestReporterEscrow.index = _index;
__reporting().reporters[_index] = _latestReporterAddress;
}
__reporting().reporters.pop();
__reporting().totalReporters = -- _totalReporters;
}

function __pushReporterAddress(address _reporterAddr)
internal
returns (uint256 _totalReporters)
{
__reporting().reporters.push(_reporterAddr);
return ++ __reporting().totalReporters;
}

/// @dev Returns storage pointer to contents of 'Board' struct.
function __reporting()
internal pure
returns (Reporting storage _ptr)
{
assembly {
_ptr.slot := _WITNET_REPORTING_1_DATA_SLOTHASH
}
}

/// @dev Slash given reporter, after checking slashing conditions for sender are met.
function __slashSignedUpReporter(address _reporter)
internal
virtual
returns (uint256 _weiValue)
{
WitnetReporting1Data.Escrow storage __escrow = __reporting().escrows[_reporter];
if (__escrow.weiSignUpFee > 0) {
if (
__escrow.lastSignOutBlock < __escrow.lastSignUpBlock
|| block.number < __escrow.lastSignUpBlock + __reporting().settings.banningBlocks
) {
_weiValue = __escrow.weiSignUpFee;
__escrow.weiSignUpFee = 0;
__escrow.lastSlashBlock = block.number;
emit Slashed(
_reporter,
_weiValue,
__deleteReporterAddressByIndex(__escrow.index)
);
}
}
}

}
185 changes: 185 additions & 0 deletions contracts/data/WitnetRequestBoardV2Data.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0 <0.9.0;

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

import "../interfaces/V2/IWitnetBlocks.sol";
import "../interfaces/V2/IWitnetBytecodes.sol";
import "../interfaces/V2/IWitnetDecoder.sol";
import "../interfaces/V2/IWitnetRequests.sol";
import "../interfaces/V2/IWitnetRequestsAdmin.sol";
import "../patterns/Payable.sol";

/// @title Witnet Request Board base data model.
/// @author The Witnet Foundation.
abstract contract WitnetRequestBoardV2Data
is
ERC165,
Payable,
IWitnetRequestsAdmin
{
using Strings for address;
using Strings for uint256;

bytes32 internal constant _WITNET_REQUEST_BOARD_DATA_SLOTHASH =
/* keccak256("io.witnet.boards.data.v2") */
0xfeed002ff8a708dcba69bac2a8e829fd61fee551b9e9fc0317707d989cb0fe53;

struct Board {
address base;
address owner;
address pendingOwner;

IWitnetBlocks blocks;
IWitnetBytecodes bytecodes;
IWitnetDecoder decoder;

IWitnetRequests.Stats serviceStats;
bytes4 serviceTag;

mapping (bytes32 => WitnetV2.DrPost) posts;
}

constructor() {
__board().owner = msg.sender;
}

/// Asserts the given query is currently in the given status.
modifier drPostInStatus(bytes32 _drHash, WitnetV2.DrPostStatus _requiredStatus) {
WitnetV2.DrPostStatus _currentStatus = _getDrPostStatus(_drHash);
if (_currentStatus != _requiredStatus) {
revert IWitnetRequests.DrPostNotInStatus(
_drHash,
_currentStatus,
_requiredStatus
);
}
_;
}

/// Asserts the given query was previously posted and that it was not yet deleted.
modifier drPostNotDeleted(bytes32 _drHash) {
WitnetV2.DrPostStatus _currentStatus = __drPost(_drHash).status;
if (uint(_currentStatus) <= uint(WitnetV2.DrPostStatus.Deleted)) {
revert IWitnetRequests.DrPostBadMood(_drHash, _currentStatus);
}
_;
}


// ================================================================================================================
// --- Internal functions -----------------------------------------------------------------------------------------

/// Returns storage pointer to contents of 'Board' struct.
function __board()
internal pure
returns (Board storage _ptr)
{
assembly {
_ptr.slot := _WITNET_REQUEST_BOARD_DATA_SLOTHASH
}
}

function _canDrPostBeDeletedFrom(bytes32 _drHash, address _from)
internal view
virtual
returns (bool _itCanBeDeleted)
{
WitnetV2.DrPostStatus _currentStatus = _getDrPostStatus(_drHash);
if (_from == __drPostRequest(_drHash).requester) {
_itCanBeDeleted = (
_currentStatus == WitnetV2.DrPostStatus.Finalized
|| _currentStatus == WitnetV2.DrPostStatus.Expired
);
}
}

function __deleteDrPost(bytes32 _drHash)
internal
virtual
{
WitnetV2.DrPostRequest storage __request = __drPostRequest(_drHash);
uint _value = __request.weiReward;
address _to = __request.requester;
delete __board().posts[_drHash];
if (address(this).balance < _value) {
revert WitnetV2.InsufficientBalance(address(this).balance, _value);
}
_safeTransferTo(payable(_to), _value);
}

function __deleteDrPostRequest(bytes32 _drHash)
internal
virtual
{
delete __drPost(_drHash).request;
}

function _getDrPostBlock(bytes32 _drHash)
internal view
virtual
returns (uint256)
{
return __drPost(_drHash).block;
}

/// Gets current status of given query.
function _getDrPostStatus(bytes32 _drHash)
internal view
virtual
returns (WitnetV2.DrPostStatus _temporaryStatus)
{
uint256 _drPostBlock = _getDrPostBlock(_drHash);
_temporaryStatus = __drPost(_drHash).status;
if (
_temporaryStatus == WitnetV2.DrPostStatus.Reported
|| _temporaryStatus == WitnetV2.DrPostStatus.Disputed
|| _temporaryStatus == WitnetV2.DrPostStatus.Accepted
) {
if (block.number > _drPostBlock + 256 /* TODO: __drPostExpirationBlocks */) {
_temporaryStatus = WitnetV2.DrPostStatus.Expired;
}
}
}

// /// Gets from of a given query.
function __drPost(bytes32 _drHash)
internal view
returns (WitnetV2.DrPost storage)
{
return __board().posts[_drHash];
}

/// Gets the WitnetV2.DrPostRequest part of a given post.
function __drPostRequest(bytes32 _drHash)
internal view
returns (WitnetV2.DrPostRequest storage)
{
return __board().posts[_drHash].request;
}

/// Gets the Witnet.Result part of a given post.
function __drPostResponse(bytes32 _drHash)
internal view
returns (WitnetV2.DrPostResponse storage)
{
return __board().posts[_drHash].response;
}

function __setServiceTag()
internal
virtual
returns (bytes4 _serviceTag)
{
_serviceTag = bytes4(keccak256(abi.encodePacked(
"evm::",
block.chainid.toString(),
"::",
address(this).toHexString()
)));
__board().serviceTag = _serviceTag;
}

}
Loading