From ea8613f52443e425d9ee8403ececbb42a4a57f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Thu, 30 Nov 2023 14:31:44 +0100 Subject: [PATCH] feat: deprecate WitnetV2.RadonSLA.witnessingCollateralRatio --- contracts/apps/UsingWitnetRandomness.sol | 16 ++++---- contracts/apps/WitnetPriceFeeds.sol | 5 +-- contracts/apps/WitnetRandomness.sol | 7 ++-- .../WitnetRequestBoardTrustableBase.sol | 8 ++-- contracts/interfaces/V2/IWitnetConsumer.sol | 8 +--- contracts/libs/Witnet.sol | 3 +- contracts/libs/WitnetEncodingLib.sol | 4 +- contracts/libs/WitnetV2.sol | 41 +++++++------------ 8 files changed, 36 insertions(+), 56 deletions(-) diff --git a/contracts/apps/UsingWitnetRandomness.sol b/contracts/apps/UsingWitnetRandomness.sol index df034d3f..ca7576ee 100644 --- a/contracts/apps/UsingWitnetRandomness.sol +++ b/contracts/apps/UsingWitnetRandomness.sol @@ -18,11 +18,15 @@ abstract contract UsingWitnetRandomness bytes32 internal immutable __witnetRandomnessRadHash; bytes32 private __defaultRandomizePackedSLA; - constructor(WitnetRequestBoard _wrb, uint96 _randomizeCallbackGasLimit) + constructor( + WitnetRequestBoard _wrb, + uint96 _randomizeCallbackGasLimit, + WitnetV2.RadonSLA memory _defaultSLA + ) UsingWitnet(_wrb) WitnetConsumer(_randomizeCallbackGasLimit) { - // Build Witnet randomness request + // On-chain building of the Witnet Randomness Request: { WitnetRequestFactory _factory = witnet().factory(); WitnetBytecodes _registry = witnet().registry(); @@ -48,18 +52,14 @@ abstract contract UsingWitnetRandomness _retrievals, _aggregator, _tally, - 35 // CBOR overhead (3 bytes) + payload (32 bytes) + 32 // 256 bits of pure entropy ;-) )); __witnetRandomnessRadHash = WitnetRequest( _template.buildRequest(new string[][](_retrievals.length)) ).radHash(); } // Settle default randomize SLA: - __defaultRandomizePackedSLA = WitnetV2.RadonSLA({ - witnessingCommitteeSize: 7, - witnessingCollateralRatio: 10, - witnessingWitReward: 10 ** 9 - }).toBytes32(); + __defaultRandomizePackedSLA = _defaultSLA.toBytes32(); } function _defaultRandomizeSLA() internal view returns (WitnetV2.RadonSLA memory) { diff --git a/contracts/apps/WitnetPriceFeeds.sol b/contracts/apps/WitnetPriceFeeds.sol index 2ffcbffa..7c6de97a 100644 --- a/contracts/apps/WitnetPriceFeeds.sol +++ b/contracts/apps/WitnetPriceFeeds.sol @@ -46,9 +46,8 @@ contract WitnetPriceFeeds ); witnet = _wrb; __settleDefaultRadonSLA(WitnetV2.RadonSLA({ - witnessingCommitteeSize: 5, - witnessingCollateralRatio: 10, - witnessingWitReward: 10 ** 9 + witnessingCommitteeSize: 10, + witnessingWitTotalReward: 10 ** 9 })); } diff --git a/contracts/apps/WitnetRandomness.sol b/contracts/apps/WitnetRandomness.sol index cdb357d5..593a2d9b 100644 --- a/contracts/apps/WitnetRandomness.sol +++ b/contracts/apps/WitnetRandomness.sol @@ -70,7 +70,7 @@ contract WitnetRandomness _retrievals, _aggregator, _tally, - 35 // CBOR overhead (3 bytes) + payload (32 bytes) + 32 // 256 bits of pure entropy ;-) )); witnetRandomnessRequest = WitnetRequest(_template.buildRequest(new string[][](_retrievals.length))); __witnetRandomnessRadHash = witnetRandomnessRequest.radHash(); @@ -466,9 +466,8 @@ contract WitnetRandomness function __initializeWitnetRandomnessSLA() virtual internal { __settleWitnetRandomnessSLA(WitnetV2.RadonSLA({ - witnessingCommitteeSize: 5, - witnessingCollateralRatio: 10, - witnessingWitReward: 10 ** 9 + witnessingCommitteeSize: 10, + witnessingWitTotalReward: 10 ** 9 })); } diff --git a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol index 2f46da79..5819d6b2 100644 --- a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol +++ b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol @@ -445,7 +445,7 @@ abstract contract WitnetRequestBoardTrustableBase ) virtual override external payable - checkReward(estimateBaseFee(_getGasPrice(), _queryRAD))//32))// _queryRAD)) + checkReward(estimateBaseFee(_getGasPrice(), _queryRAD)) checkSLA(_querySLA) returns (uint256 _witnetQueryId) { @@ -453,7 +453,7 @@ abstract contract WitnetRequestBoardTrustableBase // Let Web3 observers know that a new request has been posted emit WitnetQuery( _witnetQueryId, - _querySLA.totalWitnessingReward(), + _querySLA.witnessingWitTotalReward, _getMsgValue() ); } @@ -492,7 +492,7 @@ abstract contract WitnetRequestBoardTrustableBase ); emit WitnetQuery( _witnetQueryId, - _querySLA.totalWitnessingReward(), + _querySLA.witnessingWitTotalReward, _getMsgValue() ); } @@ -532,7 +532,7 @@ abstract contract WitnetRequestBoardTrustableBase __seekQueryRequest(_witnetQueryId).bytecode = _queryUnverifiedBytecode; emit WitnetQuery( _witnetQueryId, - _querySLA.totalWitnessingReward(), + _querySLA.witnessingWitTotalReward, _getMsgValue() ); } diff --git a/contracts/interfaces/V2/IWitnetConsumer.sol b/contracts/interfaces/V2/IWitnetConsumer.sol index 8fcc4ce6..6c46d1c1 100644 --- a/contracts/interfaces/V2/IWitnetConsumer.sol +++ b/contracts/interfaces/V2/IWitnetConsumer.sol @@ -8,9 +8,7 @@ interface IWitnetConsumer { /// @notice Method to be called from the WitnetRequestBoard contract as soon as the given Witnet `queryId` /// @notice gets reported, if reported with no errors. /// @dev It should revert if called from any other address different to the WitnetRequestBoard being used - /// @dev by the WitnetConsumer contract. Within the implementation of this method, the WitnetConsumer - /// @dev can call to the WRB as to retrieve the Witnet tracking information (i.e. the `witnetDrTxHash` - /// @dev and `witnetDrCommitTxTimestamp`), or the finality status, of the result being reported. + /// @dev by the WitnetConsumer contract. /// @param witnetQueryId The unique identifier of the Witnet query being reported. /// @param witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. /// @param witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain. @@ -27,9 +25,7 @@ interface IWitnetConsumer { /// @notice Method to be called from the WitnetRequestBoard contract as soon as the given Witnet `queryId` /// @notice gets reported, if reported WITH errors. /// @dev It should revert if called from any other address different to the WitnetRequestBoard being used - /// @dev by the WitnetConsumer contract. Within the implementation of this method, the WitnetConsumer - /// @dev can call to the WRB as to retrieve the Witnet tracking information (i.e. the `witnetDrTxHash` - /// @dev and `witnetDrCommitTxTimestamp`), or the finality status, of the result being reported. + /// @dev by the WitnetConsumer contract. /// @param witnetQueryId The unique identifier of the Witnet query being reported. /// @param witnetQueryId The unique identifier of the Witnet query being reported. /// @param witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin. diff --git a/contracts/libs/Witnet.sol b/contracts/libs/Witnet.sol index 931e41ee..5b2fcfec 100644 --- a/contracts/libs/Witnet.sol +++ b/contracts/libs/Witnet.sol @@ -229,7 +229,6 @@ library Witnet { struct RadonReducer { RadonReducerOpcodes opcode; RadonFilter[] filters; - // bytes script; } /// Reducting functions currently supported on the Witnet blockchain. @@ -696,7 +695,7 @@ library Witnet { // }); // } - // function totalWitnessingReward(Witnet.RadonSLA memory sla) + // function witnessingWitTotalReward(Witnet.RadonSLA memory sla) // internal pure returns (uint64) // { // return sla.witnessReward * sla.numWitnesses; diff --git a/contracts/libs/WitnetEncodingLib.sol b/contracts/libs/WitnetEncodingLib.sol index ba679d55..422d204e 100644 --- a/contracts/libs/WitnetEncodingLib.sol +++ b/contracts/libs/WitnetEncodingLib.sol @@ -372,13 +372,13 @@ library WitnetEncodingLib { maxDataSize ); } - return maxDataSize; + return maxDataSize + 3; // TODO: determine CBOR-encoding length overhead } else if ( dataType == Witnet.RadonDataTypes.Integer || dataType == Witnet.RadonDataTypes.Float || dataType == Witnet.RadonDataTypes.Bool ) { - return 9; // TBD: size(dataType); + return 9; } else { revert UnsupportedRadonDataType( uint8(dataType), diff --git a/contracts/libs/WitnetV2.sol b/contracts/libs/WitnetV2.sol index 0cb3454e..13e0f128 100644 --- a/contracts/libs/WitnetV2.sol +++ b/contracts/libs/WitnetV2.sol @@ -53,12 +53,13 @@ library WitnetV2 { } struct RadonSLA { - /// @dev Number of witnessing nodes that will take part in the resolution of a data request within the Witnet blockchain: + /// @dev Number of witnessing nodes that will take part in the resolution + /// @dev of a data request within the Witnet blockchain: uint8 witnessingCommitteeSize; - /// @dev Collateral-to-reward ratio that witnessing nodes will have to commit with when taking part in a data request resolution. - uint8 witnessingCollateralRatio; - /// @dev Minimum amount of $nanoWIT that all Witnet nodes participating in the resolution of a data request will receive as a reward: - uint64 witnessingWitReward; + + /// @dev Total reward in $nanoWIT that will be equally distributed to all nodes + /// @dev involved in the resolution of a data request in the Witnet blockchain: + uint64 witnessingWitTotalReward; } /// =============================================================================================================== @@ -113,26 +114,22 @@ library WitnetV2 { function equalOrGreaterThan(RadonSLA memory a, RadonSLA memory b) internal pure returns (bool) { - return ( - a.witnessingCommitteeSize * a.witnessingCollateralRatio * a.witnessingWitReward - >= b.witnessingCommitteeSize * b.witnessingCollateralRatio * b.witnessingWitReward - ); + return (a.witnessingCommitteeSize >= b.witnessingCommitteeSize); } function isValid(RadonSLA calldata sla) internal pure returns (bool) { return ( - sla.witnessingWitReward > 0 + sla.witnessingWitTotalReward > 0 && sla.witnessingCommitteeSize > 0 && sla.witnessingCommitteeSize <= 127 - && sla.witnessingCollateralRatio > 0 && sla.witnessingCollateralRatio <= 127 ); } function toBytes32(RadonSLA memory sla) internal pure returns (bytes32) { return bytes32( uint(sla.witnessingCommitteeSize) << 248 - | uint(sla.witnessingCollateralRatio) << 240 + // | uint(sla.witnessingCollateralRatio) << 240 // | uint(sla.witnessingNotBeforeTimestamp) << 64 - | uint(sla.witnessingWitReward) + | uint(sla.witnessingWitTotalReward) ); } @@ -141,24 +138,14 @@ library WitnetV2 { { return RadonSLA({ witnessingCommitteeSize: uint8(uint(_packed) >> 248), - witnessingCollateralRatio: uint8(uint(_packed) >> 240), + // witnessingCollateralRatio: uint8(uint(_packed) >> 240), // witnessingNotBeforeTimestamp: uint64(uint(_packed) >> 64), - witnessingWitReward: uint64(uint(_packed)) + witnessingWitTotalReward: uint64(uint(_packed)) }); } - function totalWitnessingReward(WitnetV2.RadonSLA calldata sla) internal pure returns (uint64) { - return ( - (3 + sla.witnessingCommitteeSize) - * sla.witnessingWitReward - ); - } - - function totalWitnessingReward(bytes32 _packed) internal pure returns (uint64) { - return ( - (3 + (uint8(uint(_packed) << 248))) // 3 + witnessingCommitteSize - * uint64(uint(_packed)) // witnessingWitReward - ); + function witnessingWitTotalReward(bytes32 _packed) internal pure returns (uint64) { + return uint64(uint(_packed)); } uint256 internal constant _WITNET_GENESIS_TIMESTAMP = 1602666045;