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

Chore/lighten wit oracle base #443

Merged
merged 8 commits into from
Oct 9, 2024
8 changes: 4 additions & 4 deletions contracts/core/WitnetDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract WitnetDeployer {
/// @param _salt Arbitrary value to modify resulting address.
/// @return _deployed Just deployed contract address.
function deploy(bytes memory _initCode, bytes32 _salt)
external
virtual public
returns (address _deployed)
{
_deployed = determineAddr(_initCode, _salt);
Expand All @@ -35,7 +35,7 @@ contract WitnetDeployer {
/// @param _salt Arbitrary value to modify resulting address.
/// @return Deterministic contract address.
function determineAddr(bytes memory _initCode, bytes32 _salt)
public view
virtual public view
returns (address)
{
return address(
Expand All @@ -51,14 +51,14 @@ contract WitnetDeployer {
}

function determineProxyAddr(bytes32 _salt)
public view
virtual public view
returns (address)
{
return Create3.determineAddr(_salt);
}

function proxify(bytes32 _proxySalt, address _firstImplementation, bytes memory _initData)
external
virtual external
returns (WitnetProxy)
{
address _proxyAddr = determineProxyAddr(_proxySalt);
Expand Down
31 changes: 7 additions & 24 deletions contracts/core/WitnetDeployerCfxCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,20 @@

pragma solidity >=0.8.0 <0.9.0;

import "./WitnetProxy.sol";
import "./WitnetDeployer.sol";

/// @notice WitnetDeployer contract used both as CREATE2 factory (EIP-1014) for Witnet artifacts,
/// @notice WitnetDeployerCfxCore contract used both as CREATE2 factory (EIP-1014) for Witnet artifacts,
/// @notice and CREATE3 factory (EIP-3171) for Witnet proxies, on the Conflux Core Ecosystem.
/// @author Guillermo Díaz <[email protected]>

contract WitnetDeployerCfxCore {

/// @notice Use given `_initCode` and `_salt` to deploy a contract into a deterministic address.
/// @dev The address of deployed address will be determined by both the `_initCode` and the `_salt`, but not the address
/// @dev nor the nonce of the caller (i.e. see EIP-1014).
/// @param _initCode Creation code, including construction logic and input parameters.
/// @param _salt Arbitrary value to modify resulting address.
/// @return _deployed Just deployed contract address.
function deploy(bytes memory _initCode, bytes32 _salt)
public
returns (address _deployed)
{
_deployed = determineAddr(_initCode, _salt);
if (_deployed.code.length == 0) {
assembly {
_deployed := create2(0, add(_initCode, 0x20), mload(_initCode), _salt)
}
require(_deployed != address(0), "WitnetDeployer: deployment failed");
}
}
contract WitnetDeployerCfxCore is WitnetDeployer {

/// @notice Determine counter-factual address of the contract that would be deployed by the given `_initCode` and a `_salt`.
/// @param _initCode Creation code, including construction logic and input parameters.
/// @param _salt Arbitrary value to modify resulting address.
/// @return Deterministic contract address.
function determineAddr(bytes memory _initCode, bytes32 _salt)
virtual override
public view
returns (address)
{
Expand All @@ -51,14 +33,15 @@ contract WitnetDeployerCfxCore {
}

function determineProxyAddr(bytes32 _salt)
virtual override
public view
returns (address)
{
return determineAddr(type(WitnetProxy).creationCode, _salt);
}

function proxify(bytes32 _proxySalt, address _firstImplementation, bytes memory _initData)
external
virtual override external
returns (WitnetProxy)
{
address _proxyAddr = determineProxyAddr(_proxySalt);
Expand All @@ -78,7 +61,7 @@ contract WitnetDeployerCfxCore {
);
return WitnetProxy(payable(_proxyAddr));
} else {
revert("WitnetDeployer: already proxified");
revert("WitnetDeployerCfxCore: already proxified");
}
}

Expand Down
47 changes: 5 additions & 42 deletions contracts/core/WitnetDeployerMeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,24 @@

pragma solidity >=0.8.0 <0.9.0;

import "./WitnetProxy.sol";
import "./WitnetDeployer.sol";

/// @notice WitnetDeployer contract used both as CREATE2 factory (EIP-1014) for Witnet artifacts,
/// @notice WitnetDeployerMeter contract used both as CREATE2 factory (EIP-1014) for Witnet artifacts,
/// @notice and CREATE3 factory (EIP-3171) for Witnet proxies, on the Meter Ecosystem.
/// @author Guillermo Díaz <[email protected]>

contract WitnetDeployerMeter {

/// @notice Use given `_initCode` and `_salt` to deploy a contract into a deterministic address.
/// @dev The address of deployed address will be determined by both the `_initCode` and the `_salt`, but not the address
/// @dev nor the nonce of the caller (i.e. see EIP-1014).
/// @param _initCode Creation code, including construction logic and input parameters.
/// @param _salt Arbitrary value to modify resulting address.
/// @return _deployed Just deployed contract address.
function deploy(bytes memory _initCode, bytes32 _salt)
public
returns (address _deployed)
{
_deployed = determineAddr(_initCode, _salt);
if (_deployed.code.length == 0) {
assembly {
_deployed := create2(0, add(_initCode, 0x20), mload(_initCode), _salt)
}
require(_deployed != address(0), "WitnetDeployerMeter: deployment failed");
}
}

/// @notice Determine counter-factual address of the contract that would be deployed by the given `_initCode` and a `_salt`.
/// @param _initCode Creation code, including construction logic and input parameters.
/// @param _salt Arbitrary value to modify resulting address.
/// @return Deterministic contract address.
function determineAddr(bytes memory _initCode, bytes32 _salt)
public view
returns (address)
{
return address(
uint160(uint(keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
_salt,
keccak256(_initCode)
)
)))
);
}
contract WitnetDeployerMeter is WitnetDeployer {

function determineProxyAddr(bytes32 _salt)
virtual override
public view
returns (address)
{
return determineAddr(type(WitnetProxy).creationCode, _salt);
}

function proxify(bytes32 _proxySalt, address _firstImplementation, bytes memory _initData)
virtual override
external
returns (WitnetProxy)
{
Expand Down
167 changes: 10 additions & 157 deletions contracts/core/trustable/WitOracleTrustableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ abstract contract WitOracleTrustableBase
using Witnet for Witnet.QueryResponse;
using Witnet for Witnet.RadonSLA;
using Witnet for Witnet.Result;
using WitnetCBOR for WitnetCBOR.CBOR;

WitOracleRequestFactory public immutable override factory;
WitOracleRadonRegistry public immutable override registry;
Expand Down Expand Up @@ -736,8 +735,7 @@ abstract contract WitOracleTrustableBase
{
// validate timestamp
_require(
_resultTimestamp > 0
&& _resultTimestamp <= block.timestamp,
_resultTimestamp > 0,
"bad timestamp"
);
// results cannot be empty
Expand Down Expand Up @@ -767,7 +765,7 @@ abstract contract WitOracleTrustableBase
onlyReporters
returns (uint256 _batchReward)
{
for ( uint _i = 0; _i < _batchResults.length; _i ++) {
for (uint _i = 0; _i < _batchResults.length; _i ++) {
if (
WitOracleDataLib.seekQueryStatus(_batchResults[_i].queryId)
!= Witnet.QueryStatus.Posted
Expand Down Expand Up @@ -889,72 +887,15 @@ abstract contract WitOracleTrustableBase
bytes calldata _resultCborBytes
)
virtual internal
returns (uint256 _evmReward)
returns (uint256)
{
// read requester address and whether a callback was requested:
Witnet.QueryRequest storage __request = WitOracleDataLib.seekQueryRequest(_queryId);

// read query EVM reward:
_evmReward = __request.evmReward;

// set EVM reward right now as to avoid re-entrancy attacks:
__request.evmReward = 0;

// determine whether a callback is required
if (__request.gasCallback > 0) {
(
uint256 _evmCallbackActualGas,
bool _evmCallbackSuccess,
string memory _evmCallbackRevertMessage
) = __reportResultCallback(
_queryId,
_resultTimestamp,
_resultTallyHash,
_resultCborBytes,
__request.requester,
__request.gasCallback
);
if (_evmCallbackSuccess) {
// => the callback run successfully
emit WitOracleQueryReponseDelivered(
_queryId,
_getGasPrice(),
_evmCallbackActualGas
);
} else {
// => the callback reverted
emit WitOracleQueryResponseDeliveryFailed(
_queryId,
_getGasPrice(),
_evmCallbackActualGas,
bytes(_evmCallbackRevertMessage).length > 0
? _evmCallbackRevertMessage
: "WitOracle: callback exceeded gas limit",
_resultCborBytes
);
}
// upon delivery, successfull or not, the audit trail is saved into storage,
// but not the actual result which was intended to be passed over to the requester:
__writeQueryQueryResponse(
_queryId,
_resultTimestamp,
_resultTallyHash,
hex""
);
} else {
// => no callback is involved
emit WitOracleQueryResponse(
_queryId,
_getGasPrice()
);
// write query result and audit trail data into storage
__writeQueryQueryResponse(
_queryId,
_resultTimestamp,
_resultTallyHash,
_resultCborBytes
);
}
return WitOracleDataLib.reportResult(
_getGasPrice(),
_queryId,
_resultTimestamp,
_resultTallyHash,
_resultCborBytes
);
}

function __reportResultAndReward(
Expand All @@ -979,77 +920,6 @@ abstract contract WitOracleTrustableBase
);
}

function __reportResultCallback(
uint256 _queryId,
uint64 _resultTimestamp,
bytes32 _resultTallyHash,
bytes calldata _resultCborBytes,
address _evmRequester,
uint256 _evmCallbackGasLimit
)
virtual internal
returns (
uint256 _evmCallbackActualGas,
bool _evmCallbackSuccess,
string memory _evmCallbackRevertMessage
)
{
_evmCallbackActualGas = gasleft();
if (_resultCborBytes[0] == bytes1(0xd8)) {
WitnetCBOR.CBOR[] memory _errors = WitnetCBOR.fromBytes(_resultCborBytes).readArray();
if (_errors.length < 2) {
// try to report result with unknown error:
try IWitOracleConsumer(_evmRequester).reportWitOracleResultError{gas: _evmCallbackGasLimit}(
_queryId,
_resultTimestamp,
_resultTallyHash,
block.number,
Witnet.ResultErrorCodes.Unknown,
WitnetCBOR.CBOR({
buffer: WitnetBuffer.Buffer({ data: hex"", cursor: 0}),
initialByte: 0,
majorType: 0,
additionalInformation: 0,
len: 0,
tag: 0
})
) {
_evmCallbackSuccess = true;
} catch Error(string memory err) {
_evmCallbackRevertMessage = err;
}
} else {
// try to report result with parsable error:
try IWitOracleConsumer(_evmRequester).reportWitOracleResultError{gas: _evmCallbackGasLimit}(
_queryId,
_resultTimestamp,
_resultTallyHash,
block.number,
Witnet.ResultErrorCodes(_errors[0].readUint()),
_errors[0]
) {
_evmCallbackSuccess = true;
} catch Error(string memory err) {
_evmCallbackRevertMessage = err;
}
}
} else {
// try to report result result with no error :
try IWitOracleConsumer(_evmRequester).reportWitOracleResultValue{gas: _evmCallbackGasLimit}(
_queryId,
_resultTimestamp,
_resultTallyHash,
block.number,
WitnetCBOR.fromBytes(_resultCborBytes)
) {
_evmCallbackSuccess = true;
} catch Error(string memory err) {
_evmCallbackRevertMessage = err;
} catch (bytes memory) {}
}
_evmCallbackActualGas -= gasleft();
}

function __setReporters(address[] memory _reporters)
virtual internal
{
Expand All @@ -1065,21 +935,4 @@ abstract contract WitOracleTrustableBase
return WitOracleDataLib.data();
}

function __writeQueryQueryResponse(
uint256 _queryId,
uint32 _resultTimestamp,
bytes32 _resultTallyHash,
bytes memory _resultCborBytes
)
virtual internal
{
WitOracleDataLib.seekQuery(_queryId).response = Witnet.QueryResponse({
reporter: msg.sender,
finality: uint64(block.number),
resultTimestamp: _resultTimestamp,
resultTallyHash: _resultTallyHash,
resultCborBytes: _resultCborBytes
});
}

}
Loading