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

2.1.x/trustlessness #446

Open
wants to merge 24 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4e8d1de
feat: prepare for wit/2.1
guidiaz Oct 7, 2024
0335b08
refactor: WRB.postRequest* -> WRB.postQuery*
guidiaz Oct 7, 2024
7e7017e
chore: lighten WitOracleTrustableBase
guidiaz Oct 7, 2024
2a6734c
feat: WRB.getQuery*StatusTag
guidiaz Oct 7, 2024
3ac2b5c
feat: Escrowable pattern
guidiaz Oct 7, 2024
3ae839b
chore/feat: prepare for trustlessness
guidiaz Oct 8, 2024
753644e
chore: fmt!
guidiaz Oct 8, 2024
e8f8ba4
feat: IWOReporterTrustless.extractQueryRelayData*
guidiaz Oct 9, 2024
776c9f1
refactor: rearrange core into base, trustable, upgradable and trustle…
guidiaz Oct 21, 2024
7554f00
feat(libs): into mem array helper methods
guidiaz Oct 21, 2024
5ed07c6
fix(wsb): way specs() is calculated
guidiaz Oct 21, 2024
92859b4
refactor: appss/WitnetPriceFeedsV21 -> apps/WitnetPriceFeedsUpgradable
guidiaz Oct 21, 2024
9ece990
refactor: WitnetDeployerCfxCore -> WitnetDeployerConfluxCore
guidiaz Oct 21, 2024
e73a905
feat(migrations): defrost WitnetDeployer + WitnetProxy artifacs
guidiaz Oct 21, 2024
51a6991
feat(migrations): agnotisticize core vs apps - trustable vs upgradabl…
guidiaz Oct 21, 2024
0d73169
fix(test): latest refactorings
guidiaz Oct 21, 2024
af672fb
chore: revisit pnpm package deps lock
guidiaz Oct 21, 2024
4d619ea
chore: fmt!
guidiaz Oct 21, 2024
bd1a73a
refactor: Upgradable --> WitOracle*Upgradable*
guidiaz Oct 21, 2024
39cf7dc
chore: move deps compliancy to base constructors
guidiaz Oct 21, 2024
90e1eab
chore: polish migrations console colors
guidiaz Oct 21, 2024
7b40891
chore: vanity2gen: read constructorArgs from file if not specified on…
guidiaz Oct 21, 2024
ade4534
chore: fix tests
guidiaz Oct 21, 2024
72ad7eb
chore: fmt!
guidiaz Oct 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion contracts/WitOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ abstract contract WitOracle
IWitAppliance,
IWitOracle,
IWitOracleEvents
{}
{
function specs() virtual override external pure returns (bytes4) {
return (
type(IWitAppliance).interfaceId
^ type(IWitOracle).interfaceId
);
}
}
9 changes: 8 additions & 1 deletion contracts/WitOracleRadonRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ abstract contract WitOracleRadonRegistry
IWitAppliance,
IWitOracleRadonRegistry,
IWitOracleRadonRegistryEvents
{}
{
function specs() virtual override external pure returns (bytes4) {
return (
type(IWitAppliance).interfaceId
^ type(IWitOracleRadonRegistry).interfaceId
);
}
}
9 changes: 9 additions & 0 deletions contracts/WitPriceFeeds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ abstract contract WitPriceFeeds
"Price-"
)
{}

function specs() virtual override external pure returns (bytes4) {
return (
type(IWitOracleAppliance).interfaceId
^ type(IERC2362).interfaceId
^ type(IWitFeeds).interfaceId
^ type(IWitPriceFeeds).interfaceId
);
}
}
9 changes: 8 additions & 1 deletion contracts/WitRandomness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ abstract contract WitRandomness
IWitOracleEvents,
IWitRandomness,
IWitRandomnessEvents
{}
{
function specs() virtual override external pure returns (bytes4) {
return (
type(IWitOracleAppliance).interfaceId
^ type(IWitRandomness).interfaceId
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import "../patterns/Ownable2Step.sol";
/// @title WitPriceFeeds: Price Feeds live repository reliant on the Witnet Oracle blockchain.
/// @author Guillermo Díaz <[email protected]>

contract WitPriceFeedsV21
contract WitPriceFeedsUpgradable
is
Ownable2Step,
WitPriceFeeds,
Expand All @@ -31,11 +31,10 @@ contract WitPriceFeedsV21
using Witnet for Witnet.RadonSLA;
using Witnet for Witnet.Result;

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

bytes4 immutable public override specs = type(WitPriceFeeds).interfaceId;
WitOracle immutable public override witOracle;
WitOracleRadonRegistry immutable internal __registry;

Expand All @@ -44,8 +43,8 @@ contract WitPriceFeedsV21

constructor(
WitOracle _witOracle,
bool _upgradable,
bytes32 _versionTag
bytes32 _versionTag,
bool _upgradable
)
Ownable(address(msg.sender))
WitnetUpgradableBase(
Expand All @@ -54,6 +53,16 @@ contract WitPriceFeedsV21
"io.witnet.proxiable.feeds.price"
)
{
_require(
address(_witOracle).code.length > 0,
"inexistent oracle"
);
_require(
_witOracle.specs() == (
type(IWitAppliance).interfaceId
^ type(IWitOracle).interfaceId
), "uncompliant oracle"
);
witOracle = _witOracle;
}

Expand Down Expand Up @@ -92,51 +101,28 @@ contract WitPriceFeedsV21
// --- Overrides 'Upgradeable' ------------------------------------------------------------------------------------

/// @notice Re-initialize contract's storage context upon a new upgrade from a proxy.
/// @dev Must fail when trying to upgrade to same logic contract more than once.
function initialize(bytes memory _initData)
public
override
{
address _owner = owner();
if (_owner == address(0)) {
// set owner as specified by first argument in _initData
_owner = abi.decode(_initData, (address));
_transferOwnership(_owner);
// settle default Radon SLA upon first initialization
function __initializeUpgradableData(bytes memory _initData) virtual override internal {
if (__proxiable().codehash == bytes32(0)) {
__defaultRadonSLA = Witnet.RadonSLA({
witNumWitnesses: 10,
witUnitaryReward: 2 * 10 ** 8, // 0.2 $WIT
witUnitaryReward: 2 * 10 ** 8,
maxTallyResultSize: 16
});
// settle default base fee overhead percentage
__baseFeeOverheadPercentage = 10;
} else {
// only the owner can initialize:
_require(
msg.sender == _owner,
"not the owner"
);
// otherwise, store beacon read from _initData, if any
if (_initData.length > 0) {
(uint16 _baseFeeOverheadPercentage, Witnet.RadonSLA memory _defaultRadonSLA) = abi.decode(
_initData, (uint16, Witnet.RadonSLA)
);
__baseFeeOverheadPercentage = _baseFeeOverheadPercentage;
__defaultRadonSLA = _defaultRadonSLA;
}
}

if (
__proxiable().codehash != bytes32(0)
&& __proxiable().codehash == codehash()
) {
_revert("already upgraded");
}
__proxiable().codehash = codehash();

_require(
address(witOracle).code.length > 0,
"inexistent oracle"
);
_require(
witOracle.specs() == type(WitOracle).interfaceId,
"uncompliant oracle"
);
emit Upgraded(_owner, base(), codehash(), version());
}


/// Tells whether provided address could eventually upgrade the contract.
function isUpgradableFrom(address _from) external view override returns (bool) {
address _owner = owner();
Expand Down Expand Up @@ -587,7 +573,7 @@ contract WitPriceFeedsV21
return IWitPriceFeedsSolver.Price({
value: _latestResult.asUint(),
timestamp: _lastValidQueryResponse.resultTimestamp,
tallyHash: _lastValidQueryResponse.resultTallyHash,
drTxHash: _lastValidQueryResponse.resultDrTxHash,
status: latestUpdateQueryResponseStatus(feedId)
});
} else {
Expand All @@ -613,7 +599,7 @@ contract WitPriceFeedsV21
return IWitPriceFeedsSolver.Price({
value: 0,
timestamp: 0,
tallyHash: 0,
drTxHash: 0,
status: latestUpdateQueryResponseStatus(feedId)
});
}
Expand Down Expand Up @@ -773,7 +759,7 @@ contract WitPriceFeedsV21
try witOracle.fetchQueryResponse(_latestId) {} catch {}
}
// Post update request to the WRB:
_latestId = witOracle.postRequest{value: _usedFunds}(
_latestId = witOracle.postQuery{value: _usedFunds}(
__feed.radHash,
querySLA
);
Expand Down
60 changes: 13 additions & 47 deletions contracts/apps/WitRandomnessV21.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,18 @@ contract WitRandomnessV21
Ownable(_operator)
UsingWitOracle(_witOracle)
{
_require(
address(_witOracle) == address(0)
|| _witOracle.specs() == type(WitOracle).interfaceId,
"uncompliant oracle"
);
// Build Witnet-compliant randomness request:
WitOracleRadonRegistry _registry = witOracle().registry();
witOracleQueryRadHash = _registry.verifyRadonRequest(
abi.decode(
abi.encode([
_registry.verifyRadonRetrieval(
Witnet.RadonRetrievalMethods.RNG,
"", // no request url
"", // no request body
new string[2][](0), // no request headers
hex"80" // no request Radon script
)
]), (bytes32[])
),
Witnet.intoMemArray([
_registry.verifyRadonRetrieval(
Witnet.RadonRetrievalMethods.RNG,
"", // no request url
"", // no request body
new string[2][](0), // no request headers
hex"80" // no request Radon script
)
]),
Witnet.RadonReducer({
opcode: Witnet.RadonReduceOpcodes.Mode,
filters: new Witnet.RadonFilter[](0)
Expand Down Expand Up @@ -91,10 +84,6 @@ contract WitRandomnessV21
return type(WitRandomnessV21).name;
}

function specs() virtual override external pure returns (bytes4) {
return type(WitRandomness).interfaceId;
}

function witOracle() override (IWitOracleAppliance, UsingWitOracle)
public view returns (WitOracle)
{
Expand Down Expand Up @@ -184,15 +173,15 @@ contract WitRandomnessV21
/// @param _blockNumber Block number from which the search will start.
/// @return _resultRandomness Random value provided by the Witnet blockchain and used for solving randomness after given block.
/// @return _resultTimestamp Timestamp at which the randomness value was generated by the Witnet blockchain.
/// @return _resultTallyHash Hash of the witnessing commit/reveal act that took place on the Witnet blockchain.
/// @return _resultDrTxHash Hash of the witnessing commit/reveal act that took place on the Witnet blockchain.
/// @return _resultFinalityBlock EVM block number from which the provided randomness can be considered to be final.
function fetchRandomnessAfterProof(uint256 _blockNumber)
virtual override
public view
returns (
bytes32 _resultRandomness,
uint64 _resultTimestamp,
bytes32 _resultTallyHash,
bytes32 _resultDrTxHash,
uint256 _resultFinalityBlock
)
{
Expand All @@ -211,7 +200,7 @@ contract WitRandomnessV21
if (_status == Witnet.QueryResponseStatus.Ready) {
Witnet.QueryResponse memory _queryResponse = __witOracle.getQueryResponse(_queryId);
_resultTimestamp = _queryResponse.resultTimestamp;
_resultTallyHash = _queryResponse.resultTallyHash;
_resultDrTxHash = _queryResponse.resultDrTxHash;
_resultFinalityBlock = _queryResponse.finality;
_resultRandomness = _queryResponse.resultCborBytes.toWitnetResult().asBytes32();

Expand Down Expand Up @@ -475,7 +464,7 @@ contract WitRandomnessV21
_evmUsedFunds = msg.value;

// Post the Witnet Randomness request:
_queryId = __witOracle.postRequest{
_queryId = __witOracle.postQuery{
value: msg.value
}(
witOracleQueryRadHash,
Expand All @@ -502,29 +491,6 @@ contract WitRandomnessV21
emit Randomizing(tx.origin, _msgSender(), _queryId);
}

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

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

/// @dev Recursively searches for the number of the first block after the given one in which a Witnet
/// @dev randomness request was posted. Returns 0 if none found.
function _searchNextBlock(uint256 _target, uint256 _latest) internal view returns (uint256) {
Expand Down
1 change: 1 addition & 0 deletions contracts/core/WitnetDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity >=0.8.0 <0.9.0;

import "./WitnetProxy.sol";

import "../libs/Create3.sol";

/// @notice WitnetDeployer contract used both as CREATE2 (EIP-1014) factory for Witnet artifacts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ pragma solidity >=0.8.0 <0.9.0;

import "./WitnetDeployer.sol";

/// @notice WitnetDeployerCfxCore contract used both as CREATE2 factory (EIP-1014) for Witnet artifacts,
/// @notice WitnetDeployerConfluxCore 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 is WitnetDeployer {
contract WitnetDeployerConfluxCore 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.
Expand Down Expand Up @@ -61,7 +61,7 @@ contract WitnetDeployerCfxCore is WitnetDeployer {
);
return WitnetProxy(payable(_proxyAddr));
} else {
revert("WitnetDeployerCfxCore: already proxified");
revert("WitnetDeployerConfluxCore: already proxified");
}
}

Expand Down
Loading