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

Feat/0.8.1: PoC Decentralized Bridge #2 #352

Open
wants to merge 26 commits into
base: 0.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a59850f
feat: implement Witnet.toString(uint256)
guidiaz May 24, 2023
69b33f2
chore: revisit WitnetBuffer.readFloat16(Buffer)
guidiaz May 24, 2023
53914f2
feat: implement WitnetBuffer.readFloat32(Buffer)
guidiaz May 24, 2023
21c1b1a
feat: implement WitnetBuffer.readFloat64(Buffer)
guidiaz May 24, 2023
887972d
chore: add tests to test/TestWitnetBuffer.sol
guidiaz May 24, 2023
255164d
feat: add support for Float32, Float64 in WitnetCBOR
guidiaz May 24, 2023
bfac84a
feat: read error codes as CBOR array
guidiaz May 24, 2023
1fbf07f
refactor: error messages in WitnetErrorsLib
guidiaz May 24, 2023
a47fd32
refactor: tests in test/TestWitnetErrorsLib.sol
guidiaz May 24, 2023
d9ab953
chore: add tests to test/TestWitnetErrorsLib.sol
guidiaz May 24, 2023
25971be
chore: prepare WRBs requiring an upgrade
guidiaz May 24, 2023
79d1c51
chore: architect IWitnetPriceSolverDeployer
guidiaz May 24, 2023
7e6b716
feat: implement IWitnetPriceSolverDeployer
guidiaz May 24, 2023
610f433
chore: revisit default SLA params in WPF
guidiaz May 24, 2023
9e70b48
chore: prepare WPFs to be upgraded
guidiaz May 24, 2023
6c090d2
refactor(WPF): assume IWitnetPriceSolver implementations may require …
guidiaz May 25, 2023
916f379
feat(WPF): catch reverts upon caption validation
guidiaz May 25, 2023
99e4edb
fix: WitnetErrorLib to comply w/ actual errors
guidiaz May 26, 2023
4be044e
chore: bump package version to 0.7.7
guidiaz May 26, 2023
1917e6d
chore: upgrade WRB to v0.7.7-4be044e on polygon.mumbai moonbeam.moonbase
guidiaz May 31, 2023
de57c01
test: adapt TestWitnetErrorsLib to real error cases
guidiaz May 31, 2023
135faa1
feat: emit WitnetRequestTemplateBuilt even if already built
guidiaz Jun 7, 2023
30a72c2
chore: fmt!
guidiaz Jun 7, 2023
66a31c2
feat: WRBv2 approach #2
guidiaz Jun 8, 2023
a2240c0
feat: add boba.bnb.testnet settings
guidiaz Jun 9, 2023
9433d4e
feat(poc): decentralized bridge approach #2
guidiaz Jun 13, 2023
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
5 changes: 5 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
ARBISCAN_API_KEY=
ELASTOS_API_KEY=
ETHERSCAN_API_KEY=
MOONSCAN_API_KEY=
POLYGONSCAN_API_KEY=
WITNET_EVM_REALM=default
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ yarn-error.log
package-lock.json

*.tmp
.old
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ Please, have a look at the [`witnet/truffle-box`](https://github.com/witnet/truf
·······································|···························|·············|·············|·············|··············|··············
| WitnetProxy · upgradeTo · - · - · 129653 · 1 · - │
·······································|···························|·············|·············|·············|··············|··············
| WitnetRandomness · clone · - · - · 247765 · 7 · - │
| WitnetRandomnessProxiable · clone · - · - · 247765 · 7 · - │
·······································|···························|·············|·············|·············|··············|··············
| WitnetRandomness · upgradeRandomizeFee · - · - · 28446 · 1 · - │
| WitnetRandomnessProxiable · upgradeRandomizeFee · - · - · 28446 · 1 · - │
·······································|···························|·············|·············|·············|··············|··············
| WitnetRequestBoardTrustableBoba · deleteQuery · - · - · 59706 · 3 · - │
·······································|···························|·············|·············|·············|··············|··············
Expand Down
95 changes: 95 additions & 0 deletions contracts/UsingWitnetV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

import "./WitnetRequestBoardV2.sol";

/// @title The UsingWitnetV2 contract
/// @dev Witnet-aware contracts can inherit from this contract in order to interact with Witnet.
/// @author The Witnet Foundation.
abstract contract UsingWitnetV2 {

/// @dev Immutable address to the WitnetRequestBoardV2 contract.
WitnetRequestBoardV2 public immutable witnet;

/// @dev Include an address to specify the WitnetRequestBoard entry point address.
/// @param _wrb The WitnetRequestBoard entry point address.
constructor(WitnetRequestBoardV2 _wrb)
{
require(
_wrb.class() == type(WitnetRequestBoardV2).interfaceId,
"UsingWitnetV2: uncompliant request board"
);
witnet = _wrb;
}

/// @dev Provides a convenient way for client contracts extending this to block the execution of the main logic of the
/// @dev contract until a particular data query has been successfully solved and reported by Witnet,
/// @dev either with an error or successfully.
modifier witnetQueryInStatus(bytes32 _queryHash, WitnetV2.QueryStatus _queryStatus) {
require(
witnet.checkQueryStatus(_queryHash) == _queryStatus,
"UsingWitnetV2: unexpected query status");
_;
}

/// @notice Returns EVM gas price within the context of current transaction.
function _getTxGasPrice() virtual internal view returns (uint256) {
return tx.gasprice;
}

/// @notice Estimate the minimum reward in EVM/wei required for posting the described Witnet data request.
/// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`).
/// @param _slaParams The query's SLA parameters.
/// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards.
/// @param _maxEvmGasPrice The maximum EVM gas price willing to pay upon result reporting.
function _witnetEstimateQueryReward(
bytes32 _radHash,
WitnetV2.RadonSLAv2 memory _slaParams,
uint256 _witEvmPrice,
uint256 _maxEvmGasPrice
)
internal view
returns (uint256)
{
return witnet.estimateQueryReward(_radHash, _slaParams, _witEvmPrice, _maxEvmGasPrice, 0);
}

/// @notice Post some data request to be eventually solved by the Witnet decentralized oracle network.
/// @dev Enough EVM coins need to be provided as to cover for the implicit cost and bridge rewarding.
/// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`).
/// @param _slaParams The query's SLA parameters.
/// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards.
/// @param _maxEvmGasPrice The maximum EVM gas price willing to pay upon result reporting.
/// @return _queryHash The unique identifier of the new data query.
/// @return _queryReward The actual amount escrowed into the WRB as query reward.
function _witnetPostQuery(
bytes32 _radHash,
WitnetV2.RadonSLAv2 memory _slaParams,
uint256 _witEvmPrice,
uint256 _maxEvmGasPrice
)
virtual internal
returns (bytes32 _queryHash, uint256 _queryReward)
{
_queryReward = _witnetEstimateQueryReward(_radHash, _slaParams, _witEvmPrice, _maxEvmGasPrice);
require(_queryReward <= msg.value, "UsingWitnetV2: EVM reward too low");
_queryHash = witnet.postQuery{value: _queryReward}(_radHash, _slaParams);
}

/// @notice Post some data request to be eventually solved by the Witnet decentralized oracle network.
/// @dev Enough EVM coins need to be provided as to cover for the implicit cost and bridge rewarding.
/// @dev Implicitly sets `tx.gasprice` as the maximum EVM gas price expected to be paid upon result reporting.
/// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`).
/// @param _slaParams The query's SLA parameters.
/// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards.
/// @return _queryHash The unique identifier of the new data query.
/// @return _queryReward The actual amount escrowed into the WRB as query reward.
function _witnetPostQuery(bytes32 _radHash, WitnetV2.RadonSLAv2 memory _slaParams, uint256 _witEvmPrice)
virtual internal
returns (bytes32 _queryHash, uint256 _queryReward)
{
return _witnetPostQuery(_radHash, _slaParams, _witEvmPrice, _getTxGasPrice());
}
}
11 changes: 11 additions & 0 deletions contracts/WitnetBlocks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

import "./interfaces/V2/IWitnetBlocks.sol";

abstract contract WitnetBlocks
is
IWitnetBlocks
{}
2 changes: 2 additions & 0 deletions contracts/WitnetPriceFeeds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pragma solidity >=0.8.0 <0.9.0;

import "ado-contracts/contracts/interfaces/IERC2362.sol";
import "./interfaces/V2/IWitnetPriceFeeds.sol";
import "./interfaces/V2/IWitnetPriceSolverDeployer.sol";
import "./WitnetFeeds.sol";

abstract contract WitnetPriceFeeds
is
IERC2362,
IWitnetPriceFeeds,
IWitnetPriceSolverDeployer,
WitnetFeeds
{
constructor(WitnetRequestBoard _wrb)
Expand Down
36 changes: 36 additions & 0 deletions contracts/WitnetRequestBoardV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

import "./WitnetBlocks.sol";
import "./WitnetBytecodes.sol";
import "./WitnetRequestFactory.sol";

/// @title Witnet Request Board V2 functionality base contract.
/// @author The Witnet Foundation.
abstract contract WitnetRequestBoardV2
is
IWitnetRequestBoardV2
{
WitnetBlocks immutable public blocks;
WitnetRequestFactory immutable public factory;
WitnetBytecodes immutable public registry;
constructor (
WitnetBlocks _blocks,
WitnetRequestFactory _factory
)
{
require(
_blocks.class() == type(WitnetBlocks).interfaceId,
"WitnetRequestBoardV2: uncompliant blocks"
);
require(
_factory.class() == type(WitnetRequestFactory).interfaceId,
"WitnetRequestBoardV2: uncompliant factory"
);
blocks = _blocks;
factory = _factory;
registry = _factory.registry();
}
}
47 changes: 47 additions & 0 deletions contracts/data/WitnetBlocksData.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0 <0.9.0;

import "../libs/WitnetV2.sol";

/// @title WitnetBlocks data model.
/// @author The Witnet Foundation.
abstract contract WitnetBlocksData {

bytes32 private constant _WITNET_BLOCKS_DATA_SLOTHASH =
/* keccak256("io.witnet.blocks.data") */
0x28b1d7e478138a94698f82768889fd6edf6b777bb6815c200552870d3e78ffb5;

struct Storage {
uint256 lastBeaconIndex;
uint256 nextBeaconIndex;
uint256 latestBeaconIndex;
mapping (/* beacon index */ uint256 => WitnetV2.Beacon) beacons;
mapping (/* beacon index */ uint256 => uint256) beaconSuccessorOf;
mapping (/* beacon index */ uint256 => BeaconTracks) beaconTracks;
}

struct BeaconTracks {
mapping (bytes32 => bool) disputed;
bytes32[] queries;
uint256 offset;
}

// ================================================================================================
// --- Internal functions -------------------------------------------------------------------------

/// @notice Returns storage pointer to where Storage data is located.
function __blocks()
internal pure
returns (Storage storage _ptr)
{
assembly {
_ptr.slot := _WITNET_BLOCKS_DATA_SLOTHASH
}
}

function __tracks_(uint256 beaconIndex) internal view returns (BeaconTracks storage) {
return __blocks().beaconTracks[beaconIndex];
}

}
63 changes: 63 additions & 0 deletions contracts/data/WitnetRequestBoardV2Data.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

import "../libs/WitnetV2.sol";
import "../interfaces/V2/IWitnetBlocks.sol";

/// @title Witnet Request Board base data model.
/// @author The Witnet Foundation.
abstract contract WitnetRequestBoardV2Data {

bytes32 internal constant _WITNET_BOARD_DATA_SLOTHASH =
/* keccak256("io.witnet.boards.data.v2") */
0xfeed002ff8a708dcba69bac2a8e829fd61fee551b9e9fc0317707d989cb0fe53;

struct Storage {
mapping (address => Escrow) escrows;
mapping (/* queryHash */ bytes32 => WitnetV2.Query) queries;
mapping (/* tallyHash */ bytes32 => Suitor) suitors;
}

struct Suitor {
uint256 index;
bytes32 queryHash;
}

struct Escrow {
uint256 atStake;
uint256 balance;
}

/// Asserts the given query was previously posted but not yet deleted.
modifier queryExists(bytes32 queryHash) {
require(__query_(queryHash).from != address(0), "WitnetRequestBoardV2Data: empty query");
_;
}


// ================================================================================================================
// --- Internal functions -----------------------------------------------------------------------------------------

/// Gets query storage by query id.
function __query_(bytes32 queryHash) internal view returns (WitnetV2.Query storage) {
return __storage().queries[queryHash];
}

/// Gets the Witnet.Request part of a given query.
function __request_(bytes32 queryHash) internal view returns (WitnetV2.QueryRequest storage) {
return __query_(queryHash).request;
}

/// Gets the Witnet.Result part of a given query.
function __report_(bytes32 queryHash) internal view returns (WitnetV2.QueryReport storage) {
return __query_(queryHash).report;
}

/// Returns storage pointer to contents of 'WitnetBoardState' struct.
function __storage() internal pure returns (Storage storage _ptr) {
assembly {
_ptr.slot := _WITNET_BOARD_DATA_SLOTHASH
}
}
}
2 changes: 2 additions & 0 deletions contracts/impls/WitnetUpgradableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ abstract contract WitnetUpgradableBase
revert("WitnetUpgradableBase: not implemented");
}

function class() virtual external view returns (bytes4);


// ================================================================================================================
// --- Overrides IERC165 interface --------------------------------------------------------------------------------
Expand Down
Loading