Description:
Proxy contract enabling upgradeable smart contract patterns. Delegates calls to an implementation contract.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"/contracts/data/WitPriceFeedsLegacyDataLib.sol": {
"content": "// SPDX-License-Identifier: MIT\r
\r
pragma solidity >=0.8.0 <0.9.0;\r
\r
import "../WitOracle.sol";\r
\r
import "../interfaces/legacy/IWitPriceFeedsLegacy.sol";\r
import "../interfaces/legacy/IWitPriceFeedsLegacyAdmin.sol";\r
import "../interfaces/legacy/IWitPriceFeedsLegacySolver.sol";\r
import "../libs/Slices.sol";\r
\r
/// @title WitPriceFeedsLegacy data model.\r
/// @author The Witnet Foundation.\r
library WitPriceFeedsLegacyDataLib {\r
\r
using Slices for string;\r
using Slices for Slices.Slice;\r
\r
using Witnet for Witnet.DataResult;\r
using Witnet for Witnet.QueryId;\r
using Witnet for Witnet.ResultStatus;\r
\r
bytes32 private constant _WIT_FEEDS_DATA_SLOTHASH =\r
/* keccak256("io.witnet.feeds.data") */\r
0xe36ea87c48340f2c23c9e1c9f72f5c5165184e75683a4d2a19148e5964c1d1ff;\r
\r
struct Storage {\r
bytes32 reserved;\r
bytes4[] ids;\r
mapping (bytes4 => Record) records;\r
}\r
\r
struct Record {\r
string caption;\r
uint8 decimals;\r
uint256 index;\r
uint256 lastValidQueryId;\r
uint256 latestUpdateQueryId;\r
bytes32 radHash;\r
address solver; // logic contract address for reducing values on routed feeds.\r
int256 solverReductor; // as to reduce resulting number of decimals on routed feeds.\r
bytes32 solverDepsFlag; // as to store ids of up to 8 depending feeds.\r
}\r
\r
\r
// ================================================================================================\r
// --- Internal functions -------------------------------------------------------------------------\r
\r
/// @notice Returns array of feed ids from which given feed's value depends.\r
/// @dev Returns empty array on either unsupported or not-routed feeds.\r
/// @dev The maximum number of dependencies is hard-limited to 8, as to limit number\r
/// @dev of SSTORE operations (`__storage().records[feedId].solverDepsFlag`), \r
/// @dev no matter the actual number of depending feeds involved.\r
function depsOf(bytes4 feedId) internal view returns (bytes4[] memory _deps) {\r
bytes32 _solverDepsFlag = data().records[feedId].solverDepsFlag;\r
_deps = new bytes4[](8);\r
uint _len;\r
for (_len = 0; _len < 8; _len ++) {\r
_deps[_len] = bytes4(_solverDepsFlag);\r
if (_deps[_len] == 0) {\r
break;\r
} else {\r
_solverDepsFlag <<= 32;\r
}\r
}\r
assembly {\r
// reset length to actual number of dependencies:\r
mstore(_deps, _len)\r
}\r
}\r
\r
/// @notice Returns storage pointer to where Storage data is located. \r
function data()\r
internal pure\r
returns (Storage storage _ptr)\r
{\r
assembly {\r
_ptr.slot := _WIT_FEEDS_DATA_SLOTHASH\r
}\r
}\r
\r
function hash(string memory caption) internal pure returns (bytes4) {\r
return bytes4(keccak256(bytes(caption)));\r
}\r
\r
function lastValidQueryId(IWitOracleQueriable witOracle, bytes4 feedId)\r
internal view \r
returns (uint256 _queryId)\r
{\r
_queryId = seekRecord(feedId).latestUpdateQueryId;\r
if (\r
_queryId == 0\r
|| witOracle.getQueryResultStatus(_queryId) != Witnet.ResultStatus.NoErrors\r
) {\r
_queryId = seekRecord(feedId).lastValidQueryId;\r
}\r
}\r
\r
function latestUpdateQueryResultStatus(IWitOracleQueriable witOracle, bytes4 feedId)\r
internal view\r
returns (Witnet.ResultStatus)\r
{\r
uint256 _queryId = seekRecord(feedId).latestUpdateQueryId;\r
if (_queryId != 0) {\r
return witOracle.getQueryResultStatus(_queryId);\r
} else {\r
return Witnet.ResultStatus.NoErrors;\r
}\r
}\r
\r
/// @notice Returns storage pointer to where Record for given feedId is located.\r
function seekRecord(bytes4 feedId) internal view returns (Record storage) {\r
return data().records[feedId];\r
}\r
\r
function seekPriceSolver(bytes4 feedId) internal view returns (\r
address _solverAddress,\r
string[] memory _solverDeps\r
)\r
{\r
_solverAddress = seekRecord(feedId).solver;\r
bytes4[] memory _deps = depsOf(feedId);\r
_solverDeps = new string[](_deps.length);\r
for (uint _ix = 0; _ix < _deps.length; _ix ++) {\r
_solverDeps[_ix] = seekRecord(_deps[_ix]).caption;\r
}\r
}\r
\r
\r
// ================================================================================================\r
// --- Public functions ---------------------------------------------------------------------------\r
\r
function deleteFeed(string calldata caption) public {\r
bytes4 feedId = hash(caption);\r
bytes4[] storage __ids = data().ids;\r
Record storage __record = seekRecord(feedId);\r
uint _index = __record.index;\r
require(_index != 0, "unknown feed");\r
bytes4 _lastFeedId = __ids[__ids.length - 1];\r
__ids[_index - 1] = _lastFeedId;\r
__ids.pop();\r
seekRecord(_lastFeedId).index = _index;\r
delete data().records[feedId];\r
emit IWitPriceFeedsLegacyAdmin.WitFeedDeleted(caption, feedId);\r
}\r
\r
function deleteFeeds() public {\r
bytes4[] storage __ids = data().ids;\r
for (uint _ix = __ids.length; _ix > 0; _ix --) {\r
bytes4 _feedId = __ids[_ix - 1];\r
string memory _caption = data().records[_feedId].caption;\r
delete data().records[_feedId]; __ids.pop();\r
emit IWitPriceFeedsLegacyAdmin.WitFeedDeleted(_caption, _feedId);\r
}\r
}\r
\r
function latestPrice(\r
IWitOracleQueriable witOracle,\r
bytes4 feedId\r
) \r
public view \r
returns (IWitPriceFeedsLegacySolver.Price memory)\r
{\r
uint256 _queryId = lastValidQueryId(witOracle, feedId);\r
if (_queryId != 0) {\r
Witnet.DataResult memory _lastValidResult = witOracle.getQueryResult(_queryId);\r
return IWitPriceFeedsLegacySolver.Price({\r
value: _lastValidResult.fetchUint(),\r
timestamp: _lastValidResult.timestamp,\r
drTxHash: _lastValidResult.drTxHash,\r
latestStatus: _intoLatestUpdateStatus(latestUpdateQueryResultStatus(witOracle, feedId))\r
});\r
\r
} else {\r
address _solver = seekRecord(feedId).solver;\r
if (_solver != address(0)) {\r
// solhint-disable-next-line avoid-low-level-calls\r
(bool _success, bytes memory _result) = address(this).staticcall(abi.encodeWithSelector(\r
IWitPriceFeedsLegacySolver.solve.selector,\r
feedId\r
));\r
if (!_success) {\r
assembly {\r
_result := add(_result, 4)\r
}\r
revert(string(abi.decode(_result, (string))));\r
} else {\r
return abi.decode(_result, (IWitPriceFeedsLegacySolver.Price));\r
}\r
} else {\r
return IWitPriceFeedsLegacySolver.Price({\r
value: 0,\r
timestamp: Witnet.Timestamp.wrap(0),\r
drTxHash: Witnet.TransactionHash.wrap(0),\r
latestStatus: _intoLatestUpdateStatus(latestUpdateQueryResultStatus(witOracle, feedId))\r
});\r
}\r
}\r
}\r
\r
function requestUpdate(\r
IWitOracleQueriable witOracle, \r
bytes4 feedId, \r
Witnet.QuerySLA memory querySLA,\r
uint256 evmUpdateRequestFee\r
)\r
public\r
returns (\r
uint256 _latestQueryId,\r
uint256 _evmUsedFunds\r
)\r
{\r
Record storage __feed = seekRecord(feedId);\r
_latestQueryId = __feed.latestUpdateQueryId;\r
\r
Witnet.ResultStatus _latestStatus = latestUpdateQueryResultStatus(witOracle, feedId); \r
if (_latestStatus.keepWaiting()) {\r
uint72 _evmUpdateRequestCurrentFee = Witnet.QueryEvmReward.unwrap(\r
witOracle.getQueryEvmReward(_latestQueryId)\r
);\r
if (evmUpdateRequestFee > _evmUpdateRequestCurrentFee) {\r
// latest update is still pending, so just increase the reward\r
// accordingly to current tx gasprice:\r
_evmUsedFunds = (evmUpdateRequestFee - _evmUpdateRequestCurrentFee);\r
witOracle.upgradeQueryEvmReward{\r
value: _evmUsedFunds\r
}(\r
_latestQueryId\r
);\r
\r
} else {\r
_evmUsedFunds = 0;\r
}\r
\r
} else {\r
// Check if latest update ended successfully:\r
if (_latestStatus == Witnet.ResultStatus.NoErrors) {\r
// If so, remove previous last valid query from the WRB:\r
if (__feed.lastValidQueryId != 0) {\r
try witOracle.deleteQuery(__feed.lastValidQueryId) returns (Witnet.QueryEvmReward _unusedReward) {\r
evmUpdateRequestFee += Witnet.QueryEvmReward.unwrap(_unusedReward);\r
\r
} catch {}\r
}\r
__feed.lastValidQueryId = _latestQueryId;\r
} else {\r
// Otherwise, try to delete latest query, as it was faulty\r
// and we are about to post a new update request:\r
try witOracle.deleteQuery(_latestQueryId) returns (Witnet.QueryEvmReward _unsedReward) {\r
evmUpdateRequestFee += Witnet.QueryEvmReward.unwrap(_unsedReward);\r
\r
} catch {}\r
}\r
// Post update request to the WRB:\r
_evmUsedFunds = evmUpdateRequestFee;\r
_latestQueryId = witOracle.queryData{\r
value: _evmUsedFunds\r
}(\r
Witnet.RadonHash.wrap(__feed.radHash),\r
querySLA\r
);\r
// Update latest query id:\r
__feed.latestUpdateQueryId = _latestQueryId;\r
}\r
}\r
\r
function settleFeedRequest(\r
string calldata caption,\r
bytes32 radHash\r
)\r
public\r
{\r
bytes4 feedId = hash(caption);\r
Record storage __record = seekRecord(feedId);\r
if (__record.index == 0) {\r
// settle new feed:\r
__record.caption = caption;\r
__record.decimals = validateCaption(caption);\r
__record.index = data().ids.length + 1;\r
__record.radHash = radHash;\r
data().ids.push(feedId);\r
} else if (__record.radHash != radHash) {\r
// update radHash on existing feed:\r
__record.radHash = radHash;\r
__record.solver = address(0);\r
}\r
emit IWitPriceFeedsLegacyAdmin.WitFeedSettled(caption, feedId, radHash);\r
}\r
\r
function settleFeedSolver(\r
string calldata caption, \r
address solver, \r
string[] calldata deps\r
)\r
public\r
{\r
bytes4 feedId = hash(caption); \r
Record storage __record = seekRecord(feedId);\r
if (__record.index == 0) {\r
// settle new feed:\r
__record.caption = caption;\r
__record.decimals = validateCaption(caption);\r
__record.index = data().ids.length + 1;\r
__record.solver = solver;\r
data().ids.push(feedId);\r
\r
} else if (__record.solver != solver) {\r
// update radHash on existing feed:\r
__record.radHash = 0;\r
__record.solver = solver;\r
}\r
// validate solver first-level dependencies\r
{\r
// solhint-disable-next-line avoid-low-level-calls\r
(bool _success, bytes memory _reason) = solver.delegatecall(abi.encodeWithSelector(\r
IWitPriceFeedsLegacySolver.validate.selector,\r
feedId,\r
deps\r
));\r
if (!_success) {\r
assembly {\r
_reason := add(_reason, 4)\r
}\r
revert(string(abi.encodePacked(\r
"solver validation failed: ",\r
string(abi.decode(_reason,(string)))\r
)));\r
}\r
}\r
// smoke-test the solver \r
{ \r
// solhint-disable-next-line avoid-low-level-calls\r
(bool _success, bytes memory _reason) = address(this).staticcall(abi.encodeWithSelector(\r
IWitPriceFeedsLegacySolver.solve.selector,\r
feedId\r
));\r
if (!_success) {\r
assembly {\r
_reason := add(_reason, 4)\r
}\r
revert(string(abi.encodePacked(\r
"smoke-test failed: ",\r
string(abi.decode(_reason,(string)))\r
)));\r
}\r
}\r
emit IWitPriceFeedsLegacyAdmin.WitFeedSolverSettled(caption, feedId, solver);\r
}\r
\r
function supportedFeeds() public view returns (\r
bytes4[] memory _ids,\r
string[] memory _captions,\r
bytes32[] memory _solvers\r
)\r
{\r
_ids = data().ids;\r
_captions = new string[](_ids.length);\r
_solvers = new bytes32[](_ids.length);\r
for (uint _ix = 0; _ix < _ids.length; _ix ++) {\r
Record storage __record = seekRecord(_ids[_ix]);\r
_captions[_ix] = __record.caption;\r
_solvers[_ix] = (\r
address(__record.solver) == address(0) \r
? __record.radHash \r
: bytes32(bytes20(__record.solver))\r
);\r
}\r
}\r
\r
// --- IWitPriceFeedsLegacySolver public functions -------------------------------------------------------------\r
\r
function deployPriceSolver(\r
bytes calldata initcode,\r
bytes calldata constructorParams\r
)\r
public\r
returns (address _solver)\r
{\r
_solver = determinePriceSolverAddress(initcode, constructorParams);\r
if (_solver.code.length == 0) {\r
bytes memory _bytecode = _completeInitCode(initcode, constructorParams);\r
address _createdContract;\r
assembly {\r
_createdContract := create2(\r
0, \r
add(_bytecode, 0x20),\r
mload(_bytecode), \r
0\r
)\r
}\r
// assert(_solver == _createdContract); // fails on TEN chains\r
_solver = _createdContract;\r
require(\r
IWitPriceFeedsLegacySolver(_solver).specs() == type(IWitPriceFeedsLegacySolver).interfaceId,\r
"uncompliant solver implementation"\r
);\r
}\r
}\r
\r
function determinePriceSolverAddress(\r
bytes calldata initcode,\r
bytes calldata constructorParams\r
)\r
public view\r
returns (address)\r
{\r
return address(\r
uint160(uint(keccak256(\r
abi.encodePacked(\r
bytes1(0xff),\r
address(this),\r
bytes32(0),\r
keccak256(_completeInitCode(initcode, constructorParams))\r
)\r
)))\r
);\r
}\r
\r
function validateCaption(string calldata caption) public pure returns (uint8) {\r
Slices.Slice memory _caption = caption.toSlice();\r
Slices.Slice memory _delim = string("-").toSlice();\r
string[] memory _parts = new string[](_caption.count(_delim) + 1);\r
for (uint _ix = 0; _ix < _parts.length; _ix ++) {\r
_parts[_ix] = _caption.split(_delim).toString();\r
}\r
(uint _decimals, bool _success) = Witnet.tryUint(_parts[_parts.length - 1]);\r
require(_success, "bad decimals");\r
return uint8(_decimals);\r
}\r
\r
\r
// ================================================================================================\r
// --- Private functions --------------------------------------------------------------------------\r
\r
function _completeInitCode(bytes calldata initcode, bytes calldata constructorParams)\r
private pure\r
returns (bytes memory)\r
{\r
return abi.encodePacked(\r
initcode,\r
constructorParams\r
);\r
}\r
\r
function _intoLatestUpdateStatus(Witnet.ResultStatus _resultStatus)\r
private pure \r
returns (IWitPriceFeedsLegacySolver.LatestUpdateStatus)\r
{\r
return (_resultStatus.keepWaiting() \r
? IWitPriceFeedsLegacySolver.LatestUpdateStatus.Awaiting\r
: (_resultStatus.hasErrors()\r
? IWitPriceFeedsLegacySolver.LatestUpdateStatus.Error\r
: IWitPriceFeedsLegacySolver.LatestUpdateStatus.Ready\r
)\r
);\r
}\r
\r
}\r
"
},
"/contracts/libs/WitnetCBOR.sol": {
"content": "// SPDX-License-Identifier: MIT\r
\r
pragma solidity >=0.8.0 <0.9.0;\r
\r
import "./WitnetBuffer.sol";\r
\r
/// @title A minimalistic implementation of “RFC 7049 Concise Binary Object Representation”\r
/// @notice This library leverages a buffer-like structure for step-by-step decoding of bytes so as to minimize\r
/// the gas cost of decoding them into a useful native type.\r
/// @dev Most of the logic has been borrowed from Patrick Gansterer’s cbor.js library: https://github.com/paroga/cbor-js\r
/// @author The Witnet Foundation.\r
\r
library WitnetCBOR {\r
\r
using WitnetBuffer for WitnetBuffer.Buffer;\r
using WitnetCBOR for WitnetCBOR.CBOR;\r
\r
/// Data struct following the RFC-7049 standard: Concise Binary Object Representation.\r
struct CBOR {\r
WitnetBuffer.Buffer buffer;\r
uint8 initialByte;\r
uint8 majorType;\r
uint8 additionalInformation;\r
uint64 len;\r
uint64 tag;\r
}\r
\r
uint8 internal constant MAJOR_TYPE_INT = 0;\r
uint8 internal constant MAJOR_TYPE_NEGATIVE_INT = 1;\r
uint8 internal constant MAJOR_TYPE_BYTES = 2;\r
uint8 internal constant MAJOR_TYPE_STRING = 3;\r
uint8 internal constant MAJOR_TYPE_ARRAY = 4;\r
uint8 internal constant MAJOR_TYPE_MAP = 5;\r
uint8 internal constant MAJOR_TYPE_TAG = 6;\r
uint8 internal constant MAJOR_TYPE_CONTENT_FREE = 7;\r
\r
uint32 internal constant UINT32_MAX = type(uint32).max;\r
uint64 internal constant UINT64_MAX = type(uint64).max;\r
\r
error EmptyArray();\r
error InvalidLengthEncoding(uint length);\r
error UnexpectedMajorType(uint read, uint expected);\r
error UnsupportedPrimitive(uint primitive);\r
error UnsupportedMajorType(uint unexpected); \r
\r
modifier isMajorType(\r
WitnetCBOR.CBOR memory cbor,\r
uint8 expected\r
) {\r
if (cbor.majorType != expected) {\r
revert UnexpectedMajorType(cbor.majorType, expected);\r
}\r
_;\r
}\r
\r
modifier notEmpty(WitnetBuffer.Buffer memory buffer) {\r
if (buffer.data.length == 0) {\r
revert WitnetBuffer.EmptyBuffer();\r
}\r
_;\r
}\r
\r
function eof(CBOR memory cbor)\r
internal pure\r
returns (bool)\r
{\r
return cbor.buffer.cursor >= cbor.buffer.data.length;\r
}\r
\r
/// @notice Decode a CBOR structure from raw bytes.\r
/// @dev This is the main factory for CBOR instances, which can be later decoded into native EVM types.\r
/// @param bytecode Raw bytes representing a CBOR-encoded value.\r
/// @return A `CBOR` instance containing a partially decoded value.\r
function fromBytes(bytes memory bytecode)\r
internal pure\r
returns (CBOR memory)\r
{\r
WitnetBuffer.Buffer memory buffer = WitnetBuffer.Buffer(bytecode, 0);\r
return fromBuffer(buffer);\r
}\r
\r
/// @notice Decode a CBOR structure from raw bytes.\r
/// @dev This is an alternate factory for CBOR instances, which can be later decoded into native EVM types.\r
/// @param buffer A Buffer structure representing a CBOR-encoded value.\r
/// @return A `CBOR` instance containing a partially decoded value.\r
function fromBuffer(WitnetBuffer.Buffer memory buffer)\r
internal pure\r
notEmpty(buffer)\r
returns (CBOR memory)\r
{\r
uint8 initialByte;\r
uint8 majorType = 255;\r
uint8 additionalInformation;\r
uint64 tag = UINT64_MAX;\r
uint256 len;\r
bool isTagged = true;\r
while (isTagged) {\r
// Extract basic CBOR properties from input bytes\r
initialByte = buffer.readUint8();\r
len ++;\r
majorType = initialByte >> 5;\r
additionalInformation = initialByte & 0x1f;\r
// Early CBOR tag parsing.\r
if (majorType == MAJOR_TYPE_TAG) {\r
uint _cursor = buffer.cursor;\r
tag = readLength(buffer, additionalInformation);\r
len += buffer.cursor - _cursor;\r
} else {\r
isTagged = false;\r
}\r
}\r
if (majorType > MAJOR_TYPE_CONTENT_FREE) {\r
revert UnsupportedMajorType(majorType);\r
}\r
return CBOR(\r
buffer,\r
initialByte,\r
majorType,\r
additionalInformation,\r
uint64(len),\r
tag\r
);\r
}\r
\r
function fork(WitnetCBOR.CBOR memory self)\r
internal pure\r
returns (WitnetCBOR.CBOR memory)\r
{\r
return CBOR({\r
buffer: self.buffer.fork(),\r
initialByte: self.initialByte,\r
majorType: self.majorType,\r
additionalInformation: self.additionalInformation,\r
len: self.len,\r
tag: self.tag\r
});\r
}\r
\r
function settle(CBOR memory self)\r
internal pure\r
returns (WitnetCBOR.CBOR memory)\r
{\r
if (!self.eof()) {\r
return fromBuffer(self.buffer);\r
} else {\r
return self;\r
}\r
}\r
\r
function skip(CBOR memory self)\r
internal pure\r
returns (WitnetCBOR.CBOR memory)\r
{\r
if (\r
self.majorType == MAJOR_TYPE_INT\r
|| self.majorType == MAJOR_TYPE_NEGATIVE_INT\r
|| (\r
self.majorType == MAJOR_TYPE_CONTENT_FREE \r
&& self.additionalInformation >= 25\r
&& self.additionalInformation <= 27\r
)\r
) {\r
self.buffer.cursor += self.peekLength();\r
} else if (\r
self.majorType == MAJOR_TYPE_STRING\r
|| self.majorType == MAJOR_TYPE_BYTES\r
) {\r
uint64 len = readLength(self.buffer, self.additionalInformation);\r
self.buffer.cursor += len;\r
} else if (\r
self.majorType == MAJOR_TYPE_ARRAY\r
|| self.majorType == MAJOR_TYPE_MAP\r
) { \r
self.len = readLength(self.buffer, self.additionalInformation); \r
} else if (\r
self.majorType != MAJOR_TYPE_CONTENT_FREE\r
|| (\r
self.additionalInformation != 20\r
&& self.additionalInformation != 21\r
)\r
) {\r
revert("WitnetCBOR.skip: unsupported major type");\r
}\r
return self;\r
}\r
\r
function peekLength(CBOR memory self)\r
internal pure\r
returns (uint64)\r
{\r
if (self.additionalInformation < 24) {\r
return 0;\r
} else if (self.additionalInformation < 28) {\r
return uint64(1 << (self.additionalInformation - 24));\r
} else {\r
revert InvalidLengthEncoding(self.additionalInformation);\r
}\r
}\r
\r
function readArray(CBOR memory self)\r
internal pure\r
isMajorType(self, MAJOR_TYPE_ARRAY)\r
returns (CBOR[] memory items)\r
{\r
// read array's length and move self cursor forward to the first array element:\r
uint64 len = readLength(self.buffer, self.additionalInformation);\r
items = new CBOR[](len + 1);\r
for (uint ix = 0; ix < len; ix ++) {\r
// settle next element in the array:\r
self = self.settle();\r
// fork it and added to the list of items to be returned:\r
items[ix] = self.fork();\r
if (self.majorType == MAJOR_TYPE_ARRAY) {\r
CBOR[] memory _subitems = self.readArray();\r
// move forward to the first element after inner array:\r
self = _subitems[_subitems.length - 1];\r
} else if (self.majorType == MAJOR_TYPE_MAP) {\r
CBOR[] memory _subitems = self.readMap();\r
// move forward to the first element after inner map:\r
self = _subitems[_subitems.length - 1];\r
} else {\r
// move forward to the next element:\r
self.skip();\r
}\r
}\r
// return self cursor as extra item at the end of the list,\r
// as to optimize recursion when jumping over nested arrays:\r
items[len] = self;\r
}\r
\r
function readMap(CBOR memory self)\r
internal pure\r
isMajorType(self, MAJOR_TYPE_MAP)\r
returns (CBOR[] memory items)\r
{\r
// read number of items within the map and move self cursor forward to the first inner element:\r
uint64 len = readLength(self.buffer, self.additionalInformation) * 2;\r
items = new CBOR[](len + 1);\r
for (uint ix = 0; ix < len; ix ++) {\r
// settle next element in the array:\r
self = self.settle();\r
// fork it and added to the list of items to be returned:\r
items[ix] = self.fork();\r
if (ix % 2 == 0 && self.majorType != MAJOR_TYPE_STRING) {\r
revert UnexpectedMajorType(self.majorType, MAJOR_TYPE_STRING);\r
} else if (self.majorType == MAJOR_TYPE_ARRAY || self.majorType == MAJOR_TYPE_MAP) {\r
CBOR[] memory _subitems = (self.majorType == MAJOR_TYPE_ARRAY\r
? self.readArray()\r
: self.readMap()\r
);\r
// move forward to the first element after inner array or map:\r
self = _subitems[_subitems.length - 1];\r
} else {\r
// move forward to the next element:\r
self.skip();\r
}\r
}\r
// return self cursor as extra item at the end of the list,\r
// as to optimize recursion when jumping over nested arrays:\r
items[len] = self;\r
}\r
\r
/// Reads the length of the settle CBOR item from a buffer, consuming a different number of bytes depending on the\r
/// value of the `additionalInformation` argument.\r
function readLength(\r
WitnetBuffer.Buffer memory buffer,\r
uint8 additionalInformation\r
) \r
internal pure\r
returns (uint64)\r
{\r
if (additionalInformation < 24) {\r
return additionalInformation;\r
}\r
if (additionalInformation == 24) {\r
return buffer.readUint8();\r
}\r
if (additionalInformation == 25) {\r
return buffer.readUint16();\r
}\r
if (additionalInformation == 26) {\r
return buffer.readUint32();\r
}\r
if (additionalInformation == 27) {\r
return buffer.readUint64();\r
}\r
if (additionalInformation == 31) {\r
return UINT64_MAX;\r
}\r
revert InvalidLengthEncoding(additionalInformation);\r
}\r
\r
/// @notice Read a `CBOR` structure into a native `bool` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as a `bool` value.\r
function readBool(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_CONTENT_FREE)\r
returns (bool)\r
{\r
if (cbor.additionalInformation == 20) {\r
return false;\r
} else if (cbor.additionalInformation == 21) {\r
return true;\r
} else {\r
revert UnsupportedPrimitive(cbor.additionalInformation);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `bytes` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return output The value represented by the input, as a `bytes` value. \r
function readBytes(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_BYTES)\r
returns (bytes memory output)\r
{\r
cbor.len = readLength(\r
cbor.buffer,\r
cbor.additionalInformation\r
);\r
if (cbor.len == UINT32_MAX) {\r
// These checks look repetitive but the equivalent loop would be more expensive.\r
uint32 length = uint32(_readIndefiniteStringLength(\r
cbor.buffer,\r
cbor.majorType\r
));\r
if (length < UINT32_MAX) {\r
output = abi.encodePacked(cbor.buffer.read(length));\r
length = uint32(_readIndefiniteStringLength(\r
cbor.buffer,\r
cbor.majorType\r
));\r
if (length < UINT32_MAX) {\r
output = abi.encodePacked(\r
output,\r
cbor.buffer.read(length)\r
);\r
}\r
}\r
} else {\r
return cbor.buffer.read(uint32(cbor.len));\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a `fixed16` value.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 5 decimal orders so as to get a fixed precision of 5 decimal positions, which should be OK for most `fixed16`\r
/// use cases. In other words, the output of this method is 10,000 times the actual value, encoded into an `int32`.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as an `int128` value.\r
function readFloat16(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_CONTENT_FREE)\r
returns (int32)\r
{\r
if (cbor.additionalInformation == 25) {\r
return cbor.buffer.readFloat16();\r
} else {\r
revert UnsupportedPrimitive(cbor.additionalInformation);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a `fixed32` value.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 9 decimal orders so as to get a fixed precision of 9 decimal positions, which should be OK for most `fixed64`\r
/// use cases. In other words, the output of this method is 10^9 times the actual value, encoded into an `int`.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as an `int` value.\r
function readFloat32(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_CONTENT_FREE)\r
returns (int)\r
{\r
if (cbor.additionalInformation == 26) {\r
return cbor.buffer.readFloat32();\r
} else {\r
revert UnsupportedPrimitive(cbor.additionalInformation);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a `fixed64` value.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 15 decimal orders so as to get a fixed precision of 15 decimal positions, which should be OK for most `fixed64`\r
/// use cases. In other words, the output of this method is 10^15 times the actual value, encoded into an `int`.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as an `int` value.\r
function readFloat64(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_CONTENT_FREE)\r
returns (int)\r
{\r
if (cbor.additionalInformation == 27) {\r
return cbor.buffer.readFloat64();\r
} else {\r
revert UnsupportedPrimitive(cbor.additionalInformation);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `int128[]` value whose inner values follow the same convention \r
/// @notice as explained in `decodeFixed16`.\r
/// @param cbor An instance of `CBOR`.\r
function readFloat16Array(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_ARRAY)\r
returns (int32[] memory values)\r
{\r
uint64 length = readLength(cbor.buffer, cbor.additionalInformation);\r
if (length < UINT64_MAX) {\r
values = new int32[](length);\r
for (uint64 i = 0; i < length; ) {\r
CBOR memory item = fromBuffer(cbor.buffer);\r
values[i] = readFloat16(item);\r
unchecked {\r
i ++;\r
}\r
}\r
} else {\r
revert InvalidLengthEncoding(length);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `int128` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as an `int128` value.\r
function readInt(CBOR memory cbor)\r
internal pure\r
returns (int64)\r
{\r
if (cbor.majorType == 1) {\r
uint64 _value = readLength(\r
cbor.buffer,\r
cbor.additionalInformation\r
);\r
return int64(-1) - int64(uint64(_value));\r
} else if (cbor.majorType == 0) {\r
// Any `uint64` can be safely casted to `int128`, so this method supports majorType 1 as well so as to have offer\r
// a uniform API for positive and negative numbers\r
return int64(readUint(cbor));\r
}\r
else {\r
revert UnexpectedMajorType(cbor.majorType, 1);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `int[]` value.\r
/// @param cbor instance of `CBOR`.\r
/// @return array The value represented by the input, as an `int[]` value.\r
function readIntArray(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_ARRAY)\r
returns (int64[] memory array)\r
{\r
uint64 length = readLength(cbor.buffer, cbor.additionalInformation);\r
if (length < UINT64_MAX) {\r
array = new int64[](length);\r
for (uint i = 0; i < length; ) {\r
CBOR memory item = fromBuffer(cbor.buffer);\r
array[i] = readInt(item);\r
unchecked {\r
i ++;\r
}\r
}\r
} else {\r
revert InvalidLengthEncoding(length);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `string` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return text The value represented by the input, as a `string` value.\r
function readString(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_STRING)\r
returns (string memory text)\r
{\r
cbor.len = readLength(cbor.buffer, cbor.additionalInformation);\r
if (cbor.len == UINT64_MAX) {\r
bool _done;\r
while (!_done) {\r
uint64 length = _readIndefiniteStringLength(\r
cbor.buffer,\r
cbor.majorType\r
);\r
if (length < UINT64_MAX) {\r
text = string(abi.encodePacked(\r
text,\r
cbor.buffer.readText(length / 4)\r
));\r
} else {\r
_done = true;\r
}\r
}\r
} else {\r
return string(cbor.buffer.readText(cbor.len));\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `string[]` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return strings The value represented by the input, as an `string[]` value.\r
function readStringArray(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_ARRAY)\r
returns (string[] memory strings)\r
{\r
uint length = readLength(cbor.buffer, cbor.additionalInformation);\r
if (length < UINT64_MAX) {\r
strings = new string[](length);\r
for (uint i = 0; i < length; ) {\r
CBOR memory item = fromBuffer(cbor.buffer);\r
strings[i] = readString(item);\r
unchecked {\r
i ++;\r
}\r
}\r
} else {\r
revert InvalidLengthEncoding(length);\r
}\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `uint64` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return The value represented by the input, as an `uint64` value.\r
function readUint(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_INT)\r
returns (uint64)\r
{\r
return readLength(\r
cbor.buffer,\r
cbor.additionalInformation\r
);\r
}\r
\r
/// @notice Decode a `CBOR` structure into a native `uint64[]` value.\r
/// @param cbor An instance of `CBOR`.\r
/// @return values The value represented by the input, as an `uint64[]` value.\r
function readUintArray(CBOR memory cbor)\r
internal pure\r
isMajorType(cbor, MAJOR_TYPE_ARRAY)\r
returns (uint64[] memory values)\r
{\r
uint64 length = readLength(cbor.buffer, cbor.additionalInformation);\r
if (length < UINT64_MAX) {\r
values = new uint64[](length);\r
for (uint ix = 0; ix < length; ) {\r
CBOR memory item = fromBuffer(cbor.buffer);\r
values[ix] = readUint(item);\r
unchecked {\r
ix ++;\r
}\r
}\r
} else {\r
revert InvalidLengthEncoding(length);\r
}\r
} \r
\r
/// Read the length of a CBOR indifinite-length item (arrays, maps, byte strings and text) from a buffer, consuming\r
/// as many bytes as specified by the first byte.\r
function _readIndefiniteStringLength(\r
WitnetBuffer.Buffer memory buffer,\r
uint8 majorType\r
)\r
private pure\r
returns (uint64 len)\r
{\r
uint8 initialByte = buffer.readUint8();\r
if (initialByte == 0xff) {\r
return UINT64_MAX;\r
}\r
len = readLength(\r
buffer,\r
initialByte & 0x1f\r
);\r
if (len >= UINT64_MAX) {\r
revert InvalidLengthEncoding(len);\r
} else if (majorType != (initialByte >> 5)) {\r
revert UnexpectedMajorType((initialByte >> 5), majorType);\r
}\r
}\r
\r
}"
},
"/contracts/libs/WitnetBuffer.sol": {
"content": "// SPDX-License-Identifier: MIT\r
\r
pragma solidity >=0.8.0 <0.9.0;\r
\r
/// @title A convenient wrapper around the `bytes memory` type that exposes a buffer-like interface\r
/// @notice The buffer has an inner cursor that tracks the final offset of every read, i.e. any subsequent read will\r
/// start with the byte that goes right after the last one in the previous read.\r
/// @dev `uint32` is used here for `cursor` because `uint16` would only enable seeking up to 8KB, which could in some\r
/// theoretical use cases be exceeded. Conversely, `uint32` supports up to 512MB, which cannot credibly be exceeded.\r
/// @author The Witnet Foundation.\r
library WitnetBuffer {\r
\r
error EmptyBuffer();\r
error IndexOutOfBounds(uint index, uint range);\r
error MissingArgs(uint expected, uint given);\r
\r
/// Iterable bytes buffer.\r
struct Buffer {\r
bytes data;\r
uint cursor;\r
}\r
\r
// Ensures we access an existing index in an array\r
modifier withinRange(uint index, uint _range) {\r
if (index > _range) {\r
revert IndexOutOfBounds(index, _range);\r
}\r
_;\r
}\r
\r
/// @notice Concatenate undefinite number of bytes chunks.\r
/// @dev Faster than looping on `abi.encodePacked(output, _buffs[ix])`.\r
function concat(bytes[] memory _buffs)\r
internal pure\r
returns (bytes memory output)\r
{\r
unchecked {\r
uint destinationPointer;\r
uint destinationLength;\r
assembly {\r
// get safe scratch location\r
output := mload(0x40)\r
// set starting destination pointer\r
destinationPointer := add(output, 32)\r
} \r
for (uint ix = 1; ix <= _buffs.length; ix ++) { \r
uint source;\r
uint sourceLength;\r
uint sourcePointer; \r
assembly {\r
// load source length pointer\r
source := mload(add(_buffs, mul(ix, 32)))\r
// load source length\r
sourceLength := mload(source)\r
// sets source memory pointer\r
sourcePointer := add(source, 32)\r
}\r
memcpy(\r
destinationPointer,\r
sourcePointer,\r
sourceLength\r
);\r
assembly { \r
// increase total destination length\r
destinationLength := add(destinationLength, sourceLength)\r
// sets destination memory pointer\r
destinationPointer := add(destinationPointer, sourceLength)\r
}\r
}\r
assembly {\r
// protect output bytes\r
mstore(output, destinationLength)\r
// set final output length\r
mstore(0x40, add(mload(0x40), add(destinationLength, 32)))\r
}\r
}\r
}\r
\r
function fork(WitnetBuffer.Buffer memory buffer)\r
internal pure\r
returns (WitnetBuffer.Buffer memory)\r
{\r
return Buffer(\r
buffer.data,\r
buffer.cursor\r
);\r
}\r
\r
function mutate(\r
WitnetBuffer.Buffer memory buffer,\r
uint length,\r
bytes memory pokes\r
)\r
internal pure\r
withinRange(length, buffer.data.length - buffer.cursor + 1)\r
{\r
bytes[] memory parts = new bytes[](3);\r
parts[0] = peek(\r
buffer,\r
0,\r
buffer.cursor\r
);\r
parts[1] = pokes;\r
parts[2] = peek(\r
buffer,\r
buffer.cursor + length,\r
buffer.data.length - buffer.cursor - length\r
);\r
buffer.data = concat(parts);\r
}\r
\r
/// @notice Read and consume the next byte from the buffer.\r
/// @param buffer An instance of `Buffer`.\r
/// @return The next byte in the buffer counting from the cursor position.\r
function next(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor, buffer.data.length)\r
returns (bytes1)\r
{\r
// Return the byte at the position marked by the cursor and advance the cursor all at once\r
return buffer.data[buffer.cursor ++];\r
}\r
\r
function peek(\r
WitnetBuffer.Buffer memory buffer,\r
uint offset,\r
uint length\r
)\r
internal pure\r
withinRange(offset + length, buffer.data.length)\r
returns (bytes memory)\r
{\r
bytes memory data = buffer.data;\r
bytes memory peeks = new bytes(length);\r
uint destinationPointer;\r
uint sourcePointer;\r
assembly {\r
destinationPointer := add(peeks, 32)\r
sourcePointer := add(add(data, 32), offset)\r
}\r
memcpy(\r
destinationPointer,\r
sourcePointer,\r
length\r
);\r
return peeks;\r
}\r
\r
// @notice Extract bytes array from buffer starting from current cursor.\r
/// @param buffer An instance of `Buffer`.\r
/// @param length How many bytes to peek from the Buffer.\r
// solium-disable-next-line security/no-assign-params\r
function peek(\r
WitnetBuffer.Buffer memory buffer,\r
uint length\r
)\r
internal pure\r
withinRange(length, buffer.data.length - buffer.cursor)\r
returns (bytes memory)\r
{\r
return peek(\r
buffer,\r
buffer.cursor,\r
length\r
);\r
}\r
\r
/// @notice Read and consume a certain amount of bytes from the buffer.\r
/// @param buffer An instance of `Buffer`.\r
/// @param length How many bytes to read and consume from the buffer.\r
/// @return output A `bytes memory` containing the first `length` bytes from the buffer, counting from the cursor position.\r
function read(Buffer memory buffer, uint length)\r
internal pure\r
withinRange(buffer.cursor + length, buffer.data.length)\r
returns (bytes memory output)\r
{\r
// Create a new `bytes memory destination` value\r
output = new bytes(length);\r
// Early return in case that bytes length is 0\r
if (length > 0) {\r
bytes memory input = buffer.data;\r
uint offset = buffer.cursor;\r
// Get raw pointers for source and destination\r
uint sourcePointer;\r
uint destinationPointer;\r
assembly {\r
sourcePointer := add(add(input, 32), offset)\r
destinationPointer := add(output, 32)\r
}\r
// Copy `length` bytes from source to destination\r
memcpy(\r
destinationPointer,\r
sourcePointer,\r
length\r
);\r
// Move the cursor forward by `length` bytes\r
seek(\r
buffer,\r
length,\r
true\r
);\r
}\r
}\r
\r
/// @notice Read and consume the next 2 bytes from the buffer as an IEEE 754-2008 floating point number enclosed in an\r
/// `int32`.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 5 decimal orders so as to get a fixed precision of 5 decimal positions, which should be OK for most `float16`\r
/// use cases. In other words, the integer output of this method is 10,000 times the actual value. The input bytes are\r
/// expected to follow the 16-bit base-2 format (a.k.a. `binary16`) in the IEEE 754-2008 standard.\r
/// @param buffer An instance of `Buffer`.\r
/// @return result The `int32` value of the next 4 bytes in the buffer counting from the cursor position.\r
function readFloat16(Buffer memory buffer)\r
internal pure\r
returns (int32 result)\r
{\r
uint32 value = readUint16(buffer);\r
// Get bit at position 0\r
uint32 sign = value & 0x8000;\r
// Get bits 1 to 5, then normalize to the [-15, 16] range so as to counterweight the IEEE 754 exponent bias\r
int32 exponent = (int32(value & 0x7c00) >> 10) - 15;\r
// Get bits 6 to 15\r
int32 fraction = int32(value & 0x03ff);\r
// Add 2^10 to the fraction if exponent is not -15\r
if (exponent != -15) {\r
fraction |= 0x400;\r
} else if (exponent == 16) {\r
revert(\r
string(abi.encodePacked(\r
"WitnetBuffer.readFloat16: ",\r
sign != 0 ? "negative" : hex"",\r
" infinity"\r
))\r
);\r
}\r
// Compute `2 ^ exponent · (1 + fraction / 1024)`\r
if (exponent >= 0) {\r
result = int32(int(\r
int(1 << uint256(int256(exponent)))\r
* 10000\r
* fraction\r
) >> 10);\r
} else {\r
result = int32(int(\r
int(fraction)\r
* 10000\r
/ int(1 << uint(int(- exponent)))\r
) >> 10);\r
}\r
// Make the result negative if the sign bit is not 0\r
if (sign != 0) {\r
result *= -1;\r
}\r
}\r
\r
/// @notice Consume the next 4 bytes from the buffer as an IEEE 754-2008 floating point number enclosed into an `int`.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 9 decimal orders so as to get a fixed precision of 9 decimal positions, which should be OK for most `float32`\r
/// use cases. In other words, the integer output of this method is 10^9 times the actual value. The input bytes are\r
/// expected to follow the 64-bit base-2 format (a.k.a. `binary32`) in the IEEE 754-2008 standard.\r
/// @param buffer An instance of `Buffer`.\r
/// @return result The `int` value of the next 8 bytes in the buffer counting from the cursor position.\r
function readFloat32(Buffer memory buffer)\r
internal pure\r
returns (int result)\r
{\r
uint value = readUint32(buffer);\r
// Get bit at position 0\r
uint sign = value & 0x80000000;\r
// Get bits 1 to 8, then normalize to the [-127, 128] range so as to counterweight the IEEE 754 exponent bias\r
int exponent = (int(value & 0x7f800000) >> 23) - 127;\r
// Get bits 9 to 31\r
int fraction = int(value & 0x007fffff);\r
// Add 2^23 to the fraction if exponent is not -127\r
if (exponent != -127) {\r
fraction |= 0x800000;\r
} else if (exponent == 128) {\r
revert(\r
string(abi.encodePacked(\r
"WitnetBuffer.readFloat32: ",\r
sign != 0 ? "negative" : hex"",\r
" infinity"\r
))\r
);\r
}\r
// Compute `2 ^ exponent · (1 + fraction / 2^23)`\r
if (exponent >= 0) {\r
result = (\r
int(1 << uint(exponent))\r
* (10 ** 9)\r
* fraction\r
) >> 23;\r
} else {\r
result = (\r
fraction \r
* (10 ** 9)\r
/ int(1 << uint(-exponent)) \r
) >> 23;\r
}\r
// Make the result negative if the sign bit is not 0\r
if (sign != 0) {\r
result *= -1;\r
}\r
}\r
\r
/// @notice Consume the next 8 bytes from the buffer as an IEEE 754-2008 floating point number enclosed into an `int`.\r
/// @dev Due to the lack of support for floating or fixed point arithmetic in the EVM, this method offsets all values\r
/// by 15 decimal orders so as to get a fixed precision of 15 decimal positions, which should be OK for most `float64`\r
/// use cases. In other words, the integer output of this method is 10^15 times the actual value. The input bytes are\r
/// expected to follow the 64-bit base-2 format (a.k.a. `binary64`) in the IEEE 754-2008 standard.\r
/// @param buffer An instance of `Buffer`.\r
/// @return result The `int` value of the next 8 bytes in the buffer counting from the cursor position.\r
function readFloat64(Buffer memory buffer)\r
internal pure\r
returns (int result)\r
{\r
uint value = readUint64(buffer);\r
// Get bit at position 0\r
uint sign = value & 0x8000000000000000;\r
// Get bits 1 to 12, then normalize to the [-1023, 1024] range so as to counterweight the IEEE 754 exponent bias\r
int exponent = (int(value & 0x7ff0000000000000) >> 52) - 1023;\r
// Get bits 6 to 15\r
int fraction = int(value & 0x000fffffffffffff);\r
// Add 2^52 to the fraction if exponent is not -1023\r
if (exponent != -1023) {\r
fraction |= 0x10000000000000;\r
} else if (exponent == 1024) {\r
revert(\r
string(abi.encodePacked(\r
"WitnetBuffer.readFloat64: ",\r
sign != 0 ? "negative" : hex"",\r
" infinity"\r
))\r
);\r
}\r
// Compute `2 ^ exponent · (1 + fraction / 1024)`\r
if (exponent >= 0) {\r
result = (\r
int(1 << uint(exponent))\r
* (10 ** 15)\r
* fraction\r
) >> 52;\r
} else {\r
result = (\r
fraction \r
* (10 ** 15)\r
/ int(1 << uint(-exponent)) \r
) >> 52;\r
}\r
// Make the result negative if the sign bit is not 0\r
if (sign != 0) {\r
result *= -1;\r
}\r
}\r
\r
// Read a text string of a given length from a buffer. Returns a `bytes memory` value for the sake of genericness,\r
/// but it can be easily casted into a string with `string(result)`.\r
// solium-disable-next-line security/no-assign-params\r
function readText(\r
WitnetBuffer.Buffer memory buffer,\r
uint64 length\r
)\r
internal pure\r
returns (bytes memory text)\r
{\r
text = new bytes(length);\r
unchecked {\r
for (uint64 index = 0; index < length; index ++) {\r
uint8 char = readUint8(buffer);\r
if (char & 0x80 != 0) {\r
if (char < 0xe0) {\r
char = (char & 0x1f) << 6\r
| (readUint8(buffer) & 0x3f);\r
length -= 1;\r
} else if (char < 0xf0) {\r
char = (char & 0x0f) << 12\r
| (readUint8(buffer) & 0x3f) << 6\r
| (readUint8(buffer) & 0x3f);\r
length -= 2;\r
} else {\r
char = (char & 0x0f) << 18\r
| (readUint8(buffer) & 0x3f) << 12\r
| (readUint8(buffer) & 0x3f) << 6 \r
| (readUint8(buffer) & 0x3f);\r
length -= 3;\r
}\r
}\r
text[index] = bytes1(char);\r
}\r
// Adjust text to actual length:\r
assembly {\r
mstore(text, length)\r
}\r
}\r
}\r
\r
/// @notice Read and consume the next byte from the buffer as an `uint8`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint8` value of the next byte in the buffer counting from the cursor position.\r
function readUint8(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor, buffer.data.length)\r
returns (uint8 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 1), offset))\r
}\r
buffer.cursor ++;\r
}\r
\r
/// @notice Read and consume the next 2 bytes from the buffer as an `uint16`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint16` value of the next 2 bytes in the buffer counting from the cursor position.\r
function readUint16(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor + 2, buffer.data.length)\r
returns (uint16 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 2), offset))\r
}\r
buffer.cursor += 2;\r
}\r
\r
/// @notice Read and consume the next 4 bytes from the buffer as an `uint32`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint32` value of the next 4 bytes in the buffer counting from the cursor position.\r
function readUint32(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor + 4, buffer.data.length)\r
returns (uint32 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 4), offset))\r
}\r
buffer.cursor += 4;\r
}\r
\r
/// @notice Read and consume the next 8 bytes from the buffer as an `uint64`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint64` value of the next 8 bytes in the buffer counting from the cursor position.\r
function readUint64(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor + 8, buffer.data.length)\r
returns (uint64 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 8), offset))\r
}\r
buffer.cursor += 8;\r
}\r
\r
/// @notice Read and consume the next 16 bytes from the buffer as an `uint128`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint128` value of the next 16 bytes in the buffer counting from the cursor position.\r
function readUint128(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor + 16, buffer.data.length)\r
returns (uint128 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 16), offset))\r
}\r
buffer.cursor += 16;\r
}\r
\r
/// @notice Read and consume the next 32 bytes from the buffer as an `uint256`.\r
/// @param buffer An instance of `Buffer`.\r
/// @return value The `uint256` value of the next 32 bytes in the buffer counting from the cursor position.\r
function readUint256(Buffer memory buffer)\r
internal pure\r
withinRange(buffer.cursor + 32, buffer.data.length)\r
returns (uint256 value)\r
{\r
bytes memory data = buffer.data;\r
uint offset = buffer.cursor;\r
assembly {\r
value := mload(add(add(data, 32), offset))\r
}\r
buffer.cursor += 32;\r
}\r
\r
/// @notice Count number of required parameters for given bytes arrays\r
/// @dev Wildcard format: "\#\", with # in ["0".."9"].\r
/// @param input Bytes array containing strings.\r
/// @param count Highest wildcard index found, plus 1.\r
function argsCountOf(bytes memory input)\r
internal pure\r
returns (uint8 count)\r
{\r
if (input.length < 3) {\r
return 0;\r
}\r
unchecked {\r
uint ix = 0; \r
uint length = input.length - 2;\r
for (; ix < length; ) {\r
if (\r
input[ix] == bytes1("\\")\r
&& input[ix + 2] == bytes1("\\")\r
&& input[ix + 1] >= bytes1("0")\r
&& input[ix + 1] <= bytes1("9")\r
) {\r
uint8 ax = uint8(uint8(input[ix + 1]) - uint8(bytes1("0")) + 1);\r
if (ax > count) {\r
count = ax;\r
}\r
ix += 3;\r
} else {\r
ix ++;\r
}\r
}\r
}\r
}\r
\r
/// @notice Replace indexed bytes-wildcards by correspondent substrings.\r
/// @dev Wildcard format: "\#\", with # in ["0".."9"].\r
/// @param input Bytes array containing strings.\r
/// @param args Array of substring values for replacing indexed wildcards.\r
/// @return output Resulting bytes array after replacing all wildcards.\r
/// @return hits Total number of replaced wildcards.\r
function replace(bytes memory input, string[] memory args)\r
internal pure\r
returns (bytes memory output, uint hits)\r
{\r
uint ix = 0; uint lix = 0;\r
uint inputLength;\r
uint inputPointer;\r
uint outputLength;\r
uint outputPointer; \r
uint source;\r
uint sourceLength;\r
uint sourcePointer;\r
\r
if (input.length < 3) {\r
return (input, 0);\r
}\r
\r
assembly {\r
// set starting input pointer\r
inputPointer := add(input, 32)\r
// get safe output location\r
output := mload(0x40)\r
// set starting output pointer\r
outputPointer := add(output, 32)\r
} \r
\r
unchecked {\r
uint length = input.length - 2;\r
for (; ix < length; ) {\r
if (\r
input[ix] == bytes1("\\")\r
&& input[ix + 2] == bytes1("\\")\r
&& input[ix + 1] >= bytes1("0")\r
&& input[ix + 1] <= bytes1("9")\r
) {\r
inputLength = (ix - lix);\r
if (ix > lix) {\r
memcpy(\r
outputPointer,\r
inputPointer,\r
inputLength\r
);\r
inputPointer += inputLength + 3;\r
outputPointer += inputLength;\r
} else {\r
inputPointer += 3;\r
}\r
uint ax = uint(uint8(input[ix + 1]) - uint8(bytes1("0")));\r
if (ax >= args.length) {\r
revert MissingArgs(ax + 1, args.length);\r
}\r
assembly {\r
source := mload(add(args, mul(32, add(ax, 1))))\r
sourceLength := mload(source)\r
sourcePointer := add(source, 32) \r
} \r
memcpy(\r
outputPointer,\r
sourcePointer,\r
sourceLength\r
);\r
outputLength += inputLength + sourceLength;\r
outputPointer += sourceLength;\r
ix += 3;\r
lix = ix;\r
hits ++;\r
} else {\r
ix ++;\r
}\r
}\r
ix = input.length; \r
}\r
if (outputLength > 0) {\r
if (ix > lix ) {\r
memcpy(\r
outputPointer,\r
inputPointer,\r
ix - lix\r
);\r
outputLength += (ix - lix);\r
}\r
assembly {\r
// set final output length\r
mstore(output, outputLength)\r
// protect output bytes\r
mstore(0x40, add(mload(0x40), add(outputLength, 32)))\r
}\r
}\r
else {\r
return (input, 0);\r
}\r
}\r
\r
/// @notice Replace indexed bytes-wildcard by given substring.\r
/// @dev Wildcard format: "\#\", with # in ["0".."9"].\r
/// @param input Bytes array containing strings.\r
/// @param argIndex Index of the wildcard to be replaced.\r
/// @param argValue Replacing substring to be used.\r
/// @return output Resulting bytes array after replacing all wildcards.\r
/// @return hits Total number of replaced wildcards.\r
function replace(bytes memory input, uint8 argIndex, string memory argValue)\r
internal pure\r
returns (bytes memory output, uint hits)\r
{\r
uint ix = 0; uint lix = 0;\r
uint inputLength;\r
uint inputPointer;\r
uint outputLength;\r
uint outputPointer; \r
uint argValueLength;\r
uint argValuePointer;\r
\r
if (input.length < 3) {\r
return (input, 0);\r
}\r
\r
assembly {\r
// set starting input pointer\r
inputPointer := add(input, 32)\r
// get safe output location\r
output := mload(0x40)\r
// set starting output pointer\r
outputPointer := add(output, 32)\r
// set pointer to arg value substring\r
argValuePointer := add(argValue, 32)\r
// set arg value substring length\r
argValueLength := mload(argValue)\r
} \r
\r
unchecked {\r
uint length = input.length - 2;\r
for (; ix < length; ) {\r
if (\r
input[ix] == bytes1("\\")\r
&& input[ix + 2] == bytes1("\\")\r
&& input[ix + 1] >= bytes1("0")\r
&& input[ix + 1] <= bytes1("9")\r
&& uint8(input[ix + 1]) - uint8(bytes1("0")) == argIndex\r
) {\r
inputLength = (ix - lix);\r
if (ix > lix) {\r
memcpy(\r
outputPointer,\r
inputPointer,\r
inputLength\r
);\r
inputPointer += inputLength + 3;\r
outputPointer += inputLength;\r
} else {\r
inputPointer += 3;\r
}\r
memcpy(\r
outputPointer,\r
argValuePointer,\r
argValueLength\r
);\r
outputLength += inputLength + argValueLength;\r
outputPointer += argValueLength;\r
ix += 3;\r
lix = ix;\r
hits ++;\r
} else {\r
ix ++;\r
}\r
}\r
ix = input.length; \r
}\r
if (outputLength > 0) {\r
if (ix > lix ) {\r
memcpy(\r
outputPointer,\r
inputPointer,\r
ix - lix\r
);\r
outputLength += (ix - lix);\r
}\r
assembly {\r
// set final output length\r
mstore(output, outputLength)\r
// protect output bytes\r
mstore(0x40, add(mload(0x40), add(outputLength, 32)))\r
}\r
}\r
else {\r
return (input, 0);\r
}\r
}\r
\r
/// @notice Replace indexed string wildcards by correspondent substrings.\r
/// @dev Wildcard format: "\#\", with # in ["0".."9"].\r
/// @param input String potentially containing wildcards.\r
/// @param args Array of substring values for replacing indexed wildcards.\r
/// @return output Resulting string after replacing all wildcards.\r
function replace(string memory input, string[] memory args)\r
internal pure\r
returns (string memory)\r
{\r
(bytes memory _outputBytes, ) = replace(bytes(input), args);\r
return string(_outputBytes);\r
}\r
\r
/// @notice Replace last indexed wildcard by given substring.\r
/// @dev Wildcard format: "\#\", with # in ["0".."9"].\r
/// @param input String potentially containing wildcards.\r
/// @param argIndex Index of the wildcard to be replaced.\r
/// @param argValue Replacing string to be used.\r
/// @return output Resulting string after replacing all wildcards.\r
function replace(string memory input, uint8 argIndex, string memory argValue)\r
internal pure\r
returns (string memory)\r
{\r
(bytes memory _outputBytes, ) = replace(bytes(input), argIndex, argValue);\r
return string(_outputBytes);\r
}\r
\r
/// @notice Move the inner cursor of the buffer to a rela
Submitted on: 2025-10-14 13:44:22
Comments
Log in to comment.
No comments yet.