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

Fix/0.7.9 #359

Merged
merged 12 commits into from
Jul 25, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The repository also provides:

- `UsingWitnet`, an inheritable abstract contract that injects methods for conveniently interacting with the WRB.
- `WitnetRequest`, used as a means to encapsulate unmodifiable Witnet Data Requests.
- `WitnetRequestBase`, useful as a base contract to implement your own modifiable Witnet Data Requests.
- `WitnetRequestPrecompiled`, useful as a base contract to implement your own modifiable Witnet Data Requests.


## **WitnetProxy**
Expand Down
15 changes: 15 additions & 0 deletions contracts/UsingWitnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ abstract contract UsingWitnet {
_id = witnet.postRequest{value: _reward}(_radHash, _slaHash);
}

/// @notice Post some data request to be eventually solved by the Witnet decentralized oracle network.
/// @notice The EVM -> Witnet bridge will read the Witnet Data Request bytecode from the WitnetBytecodes
/// @notice registry based on given `_radHash` and `_slaHash` values.
/// @dev Enough ETH needs to be provided as to cover for the implicit fee.
/// @param _radHash Unique hash of some pre-validated Witnet Radon Request.
/// @param _slaParams The SLA params upon which this data request will be solved by Witnet.
/// @return _id The unique identifier of the just posted data request.
/// @return _reward Current reward amount escrowed by the WRB until a result gets reported.
function _witnetPostRequest(bytes32 _radHash, WitnetV2.RadonSLA memory _slaParams)
virtual internal
returns (uint256 _id, uint256 _reward)
{
return _witnetPostRequest(_radHash, witnet.registry().verifyRadonSLA(_slaParams));
}

/// @notice Read the Witnet-provided result to a previously posted request.
/// @dev Reverts if the data request was not yet solved.
/// @param _id The unique identifier of some previously posted data request.
Expand Down
8 changes: 2 additions & 6 deletions contracts/data/WitnetRequestFactoryData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ contract WitnetRequestFactoryData {

struct WitnetRequestSlot {
/// Array of string arguments passed upon initialization.
string[][] args;
/// Curator's address on settled requests.
address curator;
/// Radon RAD hash.
string[][] args;
/// Radon RAD hash.
bytes32 radHash;
/// Radon SLA hash.
bytes32 slaHash;
/// Parent WitnetRequestTemplate contract.
WitnetRequestTemplate template;
}
Expand Down
152 changes: 18 additions & 134 deletions contracts/impls/core/WitnetRequestFactoryDefault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ contract WitnetRequestFactoryDefault
_;
}

modifier securedRequest {
require(
__witnetRequest().slaHash != bytes32(0),
"WitnetRequest: unsecured"
);
_;
}

constructor(
WitnetBytecodes _registry,
bool _upgradable,
Expand Down Expand Up @@ -94,7 +86,7 @@ contract WitnetRequestFactoryDefault
WitnetV2.RadonDataTypes _resultDataType;
require(
_retrievalsIds.length > 0,
"WitnetRequestTemplate: no retrievals ?"
"WitnetRequestTemplate: no retrievals?"
);
// check that all retrievals exist in the registry,
// and they all return the same data type
Expand Down Expand Up @@ -135,32 +127,13 @@ contract WitnetRequestFactoryDefault
)
virtual public
initializer
returns (WitnetRequest)
returns (address)
{
WitnetRequestSlot storage __data = __witnetRequest();
__data.args = _args;
__data.radHash = _radHash;
__data.template = WitnetRequestTemplate(msg.sender);
return WitnetRequest(address(this));
}

function forkWitnetRequest(
address _curator,
bytes32 _slaHash
)
virtual public
initializer
returns (WitnetRequest)
{
WitnetRequestSlot storage __data = __witnetRequest();
WitnetRequest parent = WitnetRequest(msg.sender);
bytes32 _radHash = parent.radHash();
__data.args = parent.args();
__data.curator = _curator;
__data.radHash = _radHash;
__data.slaHash = _slaHash;
__data.template = parent.template();
return WitnetRequest(address(this));
return address(this);
}


Expand Down Expand Up @@ -389,51 +362,14 @@ contract WitnetRequestFactoryDefault


/// ===============================================================================================================
/// --- IWitnetRequest implementation -----------------------------------------------------------------------------
/// --- WitnetRequest implementation ------------------------------------------------------------------------------

function bytecode()
override
external view
securedRequest
returns (bytes memory)
{
return registry.bytecodeOf(
__witnetRequest().radHash,
__witnetRequest().slaHash
);
}

function hash()
override
external view
securedRequest
returns (bytes32)
{
return sha256(abi.encodePacked(
__witnetRequest().radHash,
__witnetRequest().slaHash
));
}

/// ===============================================================================================================
/// --- WitnetRequest implementation ------------------------------------------------------------------------------

function curator()
override
external view
onlyDelegateCalls
returns (address)
{
return __witnetRequest().curator;
}

function secured()
override
external view
onlyDelegateCalls
returns (bool)
{
return __witnetRequest().slaHash != bytes32(0);
return registry.bytecodeOf(__witnetRequest().radHash);
}

function template()
Expand Down Expand Up @@ -463,62 +399,6 @@ contract WitnetRequestFactoryDefault
return __witnetRequest().radHash;
}

function slaHash()
override
external view
onlyDelegateCalls
returns (bytes32)
{
return __witnetRequest().slaHash;
}

function getRadonSLA()
override
external view
onlyDelegateCalls
returns (WitnetV2.RadonSLA memory)
{
return registry.lookupRadonSLA(
__witnetRequest().slaHash
);
}

function settleRadonSLA(WitnetV2.RadonSLA memory _sla)
virtual override
external
onlyDelegateCalls
returns (WitnetRequest _settled)
{
WitnetRequestSlot storage __data = __witnetRequest();
WitnetRequestTemplate _template = __witnetRequest().template;
require(
address(_template) != address(0),
"WitnetRequestFactoryDefault: not a WitnetRequest"
);
bytes32 _slaHash = registry.verifyRadonSLA(_sla);
if (_slaHash != __data.slaHash) {
if (msg.sender == __witnetRequest().curator) {
__data.slaHash = _slaHash;
_settled = WitnetRequest(address(this));
} else {
(address _address, bytes32 _salt) = _determineAddress(_slaHash);
if (_address.code.length > 0) {
_settled = WitnetRequest(_address);
} else {
_settled = WitnetRequestFactoryDefault(_cloneDeterministic(_salt))
.forkWitnetRequest(
msg.sender,
_slaHash
)
;
}
}
} else {
_settled = WitnetRequest(address(this));
}
emit WitnetRequestSettled(_settled, __data.radHash, _slaHash);
}

function version()
virtual override(WitnetRequestTemplate, WitnetUpgradableBase)
public view
Expand Down Expand Up @@ -700,7 +580,7 @@ contract WitnetRequestFactoryDefault
virtual override
public
onlyDelegateCalls
returns (WitnetRequest _request)
returns (address _request)
{
// if called on a WitnetRequest instance:
if (address(__witnetRequest().template) != address(0)) {
Expand All @@ -715,12 +595,11 @@ contract WitnetRequestFactoryDefault
__data.resultDataMaxSize,
_args
);
// address of unsecured requests (i.e. no slaHash, no curator) built out of a template,
// will be determined by the template's address and the request's radHash:
(address _address, bytes32 _salt) = _determineAddress(_radHash);
if (_address.code.length > 0) {
_request = WitnetRequest(_address);
} else {
// the request address will be determined by the template's address,
// the request's radHash and the factory's implementation version:
bytes32 _salt;
(_request, _salt) = _determineRequestAddressAndSalt(_radHash);
if (_request.code.length == 0) {
_request = WitnetRequestFactoryDefault(_cloneDeterministic(_salt))
.initializeWitnetRequest(
_radHash,
Expand Down Expand Up @@ -751,11 +630,16 @@ contract WitnetRequestFactoryDefault
);
}

function _determineAddress(bytes32 _hash)
function _determineRequestAddressAndSalt(bytes32 _radHash)
internal view
returns (address, bytes32)
{
bytes32 _salt = keccak256(abi.encodePacked(_hash, bytes4(_WITNET_UPGRADABLE_VERSION)));
bytes32 _salt = keccak256(
abi.encodePacked(
_radHash,
bytes4(_WITNET_UPGRADABLE_VERSION)
)
);
return (
address(uint160(uint256(keccak256(
abi.encodePacked(
Expand Down
27 changes: 23 additions & 4 deletions contracts/impls/core/customs/WitnetRequestBoardTrustableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,9 @@ abstract contract WitnetRequestBoardTrustableBase
/// @dev Fails if:
/// @dev - provided reward is too low.
/// @dev - provided address is zero.
/// @param _requestAddr The address of a IWitnetRequest contract, containing the actual Data Request seralized bytecode.
/// @param _requestInterface The address of a IWitnetRequest contract, containing the actual Data Request seralized bytecode.
/// @return _queryId An unique query identifier.
function postRequest(IWitnetRequest _requestAddr)
function postRequest(IWitnetRequest _requestInterface)
virtual override
public payable
returns (uint256 _queryId)
Expand All @@ -434,13 +434,13 @@ abstract contract WitnetRequestBoardTrustableBase
require(_value >= _baseReward, "WitnetRequestBoardTrustableBase: reward too low");

// Validates provided script:
require(address(_requestAddr) != address(0), "WitnetRequestBoardTrustableBase: no request");
require(_requestInterface.hash() != bytes32(0), "WitnetRequestBoardTrustableBase: no precompiled request");

_queryId = ++ __storage().numQueries;
__storage().queries[_queryId].from = msg.sender;

Witnet.Request storage _request = __request(_queryId);
_request.addr = address(_requestAddr);
_request.addr = address(_requestInterface);
_request.gasprice = _gasPrice;
_request.reward = _value;

Expand Down Expand Up @@ -482,6 +482,25 @@ abstract contract WitnetRequestBoardTrustableBase
// Let observers know that a new request has been posted
emit PostedRequest(_queryId, msg.sender);
}

/// Requests the execution of the given Witnet Data Request in expectation that it will be relayed and solved by the Witnet DON.
/// A reward amount is escrowed by the Witnet Request Board that will be transferred to the reporter who relays back the Witnet-provided
/// result to this request.
/// @dev Fails if:
/// @dev - provided reward is too low.
/// @param _radHash The RAD hash of the data tequest to be solved by Witnet.
/// @param _slaParams The SLA param of the data request to be solved by Witnet.
function postRequest(bytes32 _radHash, WitnetV2.RadonSLA calldata _slaParams)
virtual override
public payable
returns (uint256 _queryId)
{
return postRequest(
_radHash,
registry.verifyRadonSLA(_slaParams)
);
}


/// Increments the reward of a previously posted request by adding the transaction value to it.
/// @dev Updates request `gasPrice` in case this method is called with a higher
Expand Down
17 changes: 13 additions & 4 deletions contracts/interfaces/IWitnetRequestBoardRequestor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

import "../libs/Witnet.sol";
import "../libs/WitnetV2.sol";

/// @title Witnet Requestor Interface
/// @notice It defines how to interact with the Witnet Request Board in order to:
Expand Down Expand Up @@ -47,11 +47,20 @@ interface IWitnetRequestBoardRequestor {
/// @notice A reward amount is escrowed by the Witnet Request Board that will be transferred to the reporter who relays back the Witnet-provided
/// @notice result to this request.
/// @dev Fails if, provided reward is too low.
/// @param radHash The radHash of the Witnet Data Request.
/// @param slaHash The slaHash of the Witnet Data Request.
/// @param radHash The RAD hash of the data request to be solved by Witnet.
/// @param slaHash The SLA hash of the data request to be solved by Witnet.
/// @return _queryId Unique query identifier.
function postRequest(bytes32 radHash, bytes32 slaHash) external payable returns (uint256 _queryId);


/// @notice Requests the execution of the given Witnet Data Request in expectation that it will be relayed and solved by the Witnet DON.
/// @notice A reward amount is escrowed by the Witnet Request Board that will be transferred to the reporter who relays back the Witnet-provided
/// @notice result to this request.
/// @dev Fails if, provided reward is too low.
/// @param radHash The RAD hash of the data request to be solved by Witnet.
/// @param slaParams The SLA params of the data request to be solved by Witnet.
/// @return _queryId Unique query identifier.
function postRequest(bytes32 radHash, WitnetV2.RadonSLA calldata slaParams) external payable returns (uint256 _queryId);

/// @notice Increments the reward of a previously posted request by adding the transaction value to it.
/// @dev Updates request `gasPrice` in case this method is called with a higher
/// @dev gas price value than the one used in previous calls to `postRequest` or
Expand Down
Loading
Loading