Skip to content

Commit

Permalink
feat: deprecate WitnetV2.RadonSLA.witnessingCollateralRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Nov 30, 2023
1 parent 1ba0496 commit ea8613f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 56 deletions.
16 changes: 8 additions & 8 deletions contracts/apps/UsingWitnetRandomness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions contracts/apps/WitnetPriceFeeds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ contract WitnetPriceFeeds
);
witnet = _wrb;
__settleDefaultRadonSLA(WitnetV2.RadonSLA({
witnessingCommitteeSize: 5,
witnessingCollateralRatio: 10,
witnessingWitReward: 10 ** 9
witnessingCommitteeSize: 10,
witnessingWitTotalReward: 10 ** 9
}));
}

Expand Down
7 changes: 3 additions & 4 deletions contracts/apps/WitnetRandomness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
}));
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/core/defaults/WitnetRequestBoardTrustableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@ abstract contract WitnetRequestBoardTrustableBase
)
virtual override
external payable
checkReward(estimateBaseFee(_getGasPrice(), _queryRAD))//32))// _queryRAD))
checkReward(estimateBaseFee(_getGasPrice(), _queryRAD))
checkSLA(_querySLA)
returns (uint256 _witnetQueryId)
{
_witnetQueryId = __postRequest(_queryRAD, _querySLA.toBytes32(), 0);
// Let Web3 observers know that a new request has been posted
emit WitnetQuery(
_witnetQueryId,
_querySLA.totalWitnessingReward(),
_querySLA.witnessingWitTotalReward,
_getMsgValue()
);
}
Expand Down Expand Up @@ -492,7 +492,7 @@ abstract contract WitnetRequestBoardTrustableBase
);
emit WitnetQuery(
_witnetQueryId,
_querySLA.totalWitnessingReward(),
_querySLA.witnessingWitTotalReward,
_getMsgValue()
);
}
Expand Down Expand Up @@ -532,7 +532,7 @@ abstract contract WitnetRequestBoardTrustableBase
__seekQueryRequest(_witnetQueryId).bytecode = _queryUnverifiedBytecode;
emit WitnetQuery(
_witnetQueryId,
_querySLA.totalWitnessingReward(),
_querySLA.witnessingWitTotalReward,
_getMsgValue()
);
}
Expand Down
8 changes: 2 additions & 6 deletions contracts/interfaces/V2/IWitnetConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions contracts/libs/Witnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ library Witnet {
struct RadonReducer {
RadonReducerOpcodes opcode;
RadonFilter[] filters;
// bytes script;
}

/// Reducting functions currently supported on the Witnet blockchain.
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions contracts/libs/WitnetEncodingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
41 changes: 14 additions & 27 deletions contracts/libs/WitnetV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// ===============================================================================================================
Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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;
Expand Down

0 comments on commit ea8613f

Please sign in to comment.