Skip to content

Commit

Permalink
chore: optimize framework storage footprint for human-readable revert…
Browse files Browse the repository at this point in the history
… messages
  • Loading branch information
guidiaz committed Mar 21, 2024
1 parent 4468d28 commit 4ebd46c
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 51 deletions.
29 changes: 28 additions & 1 deletion contracts/core/WitnetUpgradableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ abstract contract WitnetUpgradableBase

/// @dev Reverts if proxy delegatecalls to unexistent method.
fallback() virtual external {
revert("WitnetUpgradableBase: not implemented");
_revert("not implemented");
}


function class() virtual public view returns (string memory) {
return type(WitnetUpgradableBase).name;
}

// ================================================================================================================
// --- Overrides 'Proxiable' --------------------------------------------------------------------------------------
Expand All @@ -60,6 +64,29 @@ abstract contract WitnetUpgradableBase
// ================================================================================================================
// --- Internal methods -------------------------------------------------------------------------------------------

function _require(
bool _condition,
string memory _message
)
internal view
{
if (!_condition) {
_revert(_message);
}
}

function _revert(string memory _message)
internal view
{
revert(
string(abi.encodePacked(
class(),
": ",
_message
))
);
}

/// Converts bytes32 into string.
function _toString(bytes32 _bytes32)
internal pure
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/customs/WitnetOracleTrustableObscuro.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract WitnetOracleTrustableObscuro
is
WitnetOracleTrustableDefault
{
function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetOracleTrustableObscuro).name;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/customs/WitnetOracleTrustableOvm2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract WitnetOracleTrustableOvm2
{
using WitnetV2 for WitnetV2.RadonSLA;

function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetOracleTrustableOvm2).name;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/customs/WitnetOracleTrustableReef.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract WitnetOracleTrustableReef
is
WitnetOracleTrustableDefault
{
function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetOracleTrustableReef).name;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/customs/WitnetRequestBytecodesNoSha256.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract WitnetRequestBytecodesNoSha256
is
WitnetRequestBytecodesDefault
{
function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetRequestBytecodesNoSha256).name;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/customs/WitnetRequestFactoryCfxCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract WitnetRequestFactoryCfxCore
is
WitnetRequestFactoryDefault
{
function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetRequestFactoryCfxCore).name;
}

Expand Down
83 changes: 47 additions & 36 deletions contracts/core/defaults/WitnetOracleTrustableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,53 @@ abstract contract WitnetOracleTrustableBase
WitnetRequestFactory immutable private __factory;

modifier checkCallbackRecipient(address _addr, uint24 _callbackGasLimit) {
require(
_require(
_addr.code.length > 0 && IWitnetConsumer(_addr).reportableFrom(address(this)) && _callbackGasLimit > 0,
"WitnetOracle: invalid callback"
"invalid callback"
); _;
}

modifier checkReward(uint256 _baseFee) {
require(
_require(
_getMsgValue() >= _baseFee,
"WitnetOracle: insufficient reward"
"insufficient reward"
);
require(
_require(
_getMsgValue() <= _baseFee * 10,
"WitnetOracle: too much reward"
"too much reward"
);
_;
}

modifier checkSLA(WitnetV2.RadonSLA calldata sla) {
require(
_require(
WitnetV2.isValid(sla),
"WitnetOracle: invalid SLA"
"invalid SLA"
); _;
}

/// Asserts the given query is currently in the given status.
modifier inStatus(uint256 _queryId, WitnetV2.QueryStatus _status) {
if (WitnetOracleDataLib.seekQueryStatus(_queryId) != _status) {
revert(WitnetOracleDataLib.notInStatusRevertMessage(_status));
_revert(WitnetOracleDataLib.notInStatusRevertMessage(_status));
} else {
_;
}
}

/// Asserts the caller actually posted the referred query.
modifier onlyRequester(uint256 _queryId) {
require(
_require(
msg.sender == WitnetOracleDataLib.seekQueryRequest(_queryId).requester,
"WitnetOracle: not the requester"
"not the requester"
); _;
}

/// Asserts the caller is authorized as a reporter
modifier onlyReporters {
require(
_require(
__storage().reporters[msg.sender],
"WitnetOracle: unauthorized reporter"
"unauthorized reporter"
);
_;
}
Expand All @@ -110,7 +110,7 @@ abstract contract WitnetOracleTrustableBase
}

receive() external payable {
revert("WitnetOracle: no transfers accepted");
_revert("no transfers accepted");
}

/// @dev Provide backwards compatibility for dapps bound to versions <= 0.6.1
Expand All @@ -119,8 +119,8 @@ abstract contract WitnetOracleTrustableBase
/* solhint-disable payable-fallback */
/* solhint-disable no-complex-fallback */
fallback() override external {
revert(string(abi.encodePacked(
"WitnetOracle: not implemented: 0x",
_revert(string(abi.encodePacked(
"not implemented: 0x",
Witnet.toHexString(uint8(bytes1(msg.sig))),
Witnet.toHexString(uint8(bytes1(msg.sig << 8))),
Witnet.toHexString(uint8(bytes1(msg.sig << 16))),
Expand All @@ -132,6 +132,14 @@ abstract contract WitnetOracleTrustableBase
return bytes4(keccak256(abi.encode(address(this), block.chainid)));
}

function class()
public view
virtual override(WitnetOracle, WitnetUpgradableBase)
returns (string memory)
{
return type(WitnetOracleTrustableBase).name;
}

function factory() virtual override public view returns (WitnetRequestFactory) {
return __factory;
}
Expand Down Expand Up @@ -172,9 +180,9 @@ abstract contract WitnetOracleTrustableBase
_newReporters = abi.decode(_newReportersRaw, (address[]));
} else {
// only owner can initialize:
require(
_require(
msg.sender == _owner,
"WitnetOracle: not the owner"
"not the owner"
);
// get reporters from _initData
_newReporters = abi.decode(_initData, (address[]));
Expand All @@ -184,22 +192,22 @@ abstract contract WitnetOracleTrustableBase
__proxiable().codehash != bytes32(0)
&& __proxiable().codehash == codehash()
) {
revert("WitnetOracle: already upgraded");
_revert("already upgraded");
}
__proxiable().codehash = codehash();

require(
_require(
address(__factory).code.length > 0,
"WitnetOracle: inexistent factory"
"inexistent factory"
);
require(
_require(
__factory.specs() == type(IWitnetRequestFactory).interfaceId,
"WitnetOracle: uncompliant factory"
"uncompliant factory"
);
require(
_require(
address(__factory.witnet()) == address(this)
&& address(__factory.registry()) == address(registry),
"WitnetOracle: discordant factory"
"discordant factory"
);

// Set reporters, if any
Expand Down Expand Up @@ -231,9 +239,9 @@ abstract contract WitnetOracleTrustableBase
returns (uint256)
{
uint16 _resultMaxSize = registry.lookupRadonRequestResultMaxSize(radHash);
require(
_require(
_resultMaxSize > 0,
"WitnetOracleTrustableDefault: invalid RAD"
"invalid RAD"
);
return estimateBaseFee(
gasPrice,
Expand Down Expand Up @@ -290,7 +298,7 @@ abstract contract WitnetOracleTrustableBase
function getQueryResponse(uint256 _witnetQueryId)
public view
virtual override
returns (WitnetV2.Response memory _response)
returns (WitnetV2.Response memory)
{
return WitnetOracleDataLib.seekQueryResponse(_witnetQueryId);
}
Expand Down Expand Up @@ -562,9 +570,9 @@ abstract contract WitnetOracleTrustableBase
returns (uint256)
{
// results cannot be empty:
require(
_require(
_witnetQueryResultCborBytes.length != 0,
"WitnetOracleTrustableDefault: result cannot be empty"
"result cannot be empty"
);
// do actual report and return reward transfered to the reproter:
// solhint-disable not-rely-on-time
Expand Down Expand Up @@ -599,15 +607,15 @@ abstract contract WitnetOracleTrustableBase
returns (uint256)
{
// validate timestamp
require(
_require(
_witnetQueryResultTimestamp > 0
&& _witnetQueryResultTimestamp <= block.timestamp,
"WitnetOracleTrustableDefault: bad timestamp"
"bad timestamp"
);
// results cannot be empty
require(
_require(
_witnetQueryResultCborBytes.length != 0,
"WitnetOracleTrustableDefault: result cannot be empty"
"result cannot be empty"
);
// do actual report and return reward transfered to the reproter:
return __reportResultAndReward(
Expand Down Expand Up @@ -647,7 +655,10 @@ abstract contract WitnetOracleTrustableBase
) {
emit BatchReportError(
_batchResults[_i].queryId,
"WitnetOracle: invalid report data"
string(abi.encodePacked(
class(),
"invalid report data"
))
);
} else {
_batchReward += __reportResult(
Expand Down Expand Up @@ -728,7 +739,7 @@ abstract contract WitnetOracleTrustableBase
{
_witnetQueryId = ++ __storage().nonce; //__newQueryId(_radHash, _packedSLA);
WitnetV2.Request storage __request = WitnetOracleDataLib.seekQueryRequest(_witnetQueryId);
require(__request.requester == address(0), "WitnetOracle: already posted");
_require(__request.requester == address(0), "already posted");
{
__request.requester = msg.sender;
__request.gasCallback = _callbackGasLimit;
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/defaults/WitnetOracleTrustableDefault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract WitnetOracleTrustableDefault
is
WitnetOracleTrustableBase
{
function class() virtual override external view returns (string memory) {
function class() virtual override public view returns (string memory) {
return type(WitnetOracleTrustableDefault).name;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/defaults/WitnetPriceFeedsDefault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract WitnetPriceFeedsDefault
using WitnetV2 for WitnetV2.Response;
using WitnetV2 for WitnetV2.RadonSLA;

function class() virtual override external view returns (string memory) {
function class() virtual override(WitnetFeeds, WitnetUpgradableBase) public view returns (string memory) {
return type(WitnetPriceFeedsDefault).name;
}

Expand Down
6 changes: 5 additions & 1 deletion contracts/core/defaults/WitnetRequestBytecodesDefault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ contract WitnetRequestBytecodesDefault
using WitnetEncodingLib for Witnet.RadonSLA;
using WitnetEncodingLib for Witnet.RadonDataTypes;

function class() virtual override external view returns (string memory) {
function class()
public view
virtual override(WitnetRequestBytecodes, WitnetUpgradableBase)
returns (string memory)
{
return type(WitnetRequestBytecodesDefault).name;
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/core/defaults/WitnetRequestFactoryDefault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ contract WitnetRequestFactoryDefault
}

function class()
virtual override(WitnetRequestFactory, WitnetRequestTemplate)
external view
virtual override(WitnetRequestFactory, WitnetRequestTemplate, WitnetUpgradableBase)
public view
returns (string memory)
{
if (
Expand Down
8 changes: 4 additions & 4 deletions contracts/data/WitnetOracleDataLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ library WitnetOracleDataLib {

function notInStatusRevertMessage(WitnetV2.QueryStatus self) public pure returns (string memory) {
if (self == WitnetV2.QueryStatus.Posted) {
return "WitnetOracle: query not in Posted status";
return "query not in Posted status";
} else if (self == WitnetV2.QueryStatus.Reported) {
return "WitnetOracle: query not in Reported status";
return "query not in Reported status";
} else if (self == WitnetV2.QueryStatus.Finalized) {
return "WitnetOracle: query not in Finalized status";
return "query not in Finalized status";
} else {
return "WitnetOracle: bad mood";
return "bad mood";
}
}
}

0 comments on commit 4ebd46c

Please sign in to comment.