From cd140243d7b7539ef08076d367a8ff04729dcc54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Wed, 29 Nov 2023 11:52:34 +0100 Subject: [PATCH] feat: add mocking contracts --- .../WitnetRequestBoardTrustableBase.sol | 25 ++++++++------ .../defaults/WitnetRequestFactoryDefault.sol | 1 - contracts/data/WitnetRequestBoardDataACLs.sol | 6 ++-- contracts/mocks/WitnetBytecodesMock.sol | 15 ++++++++ contracts/mocks/WitnetPriceFeedsMock.sol | 15 ++++++++ contracts/mocks/WitnetRequestBoardMock.sol | 34 +++++++++++++++++++ contracts/mocks/WitnetRequestFactoryMock.sol | 21 ++++++++++++ 7 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 contracts/mocks/WitnetBytecodesMock.sol create mode 100644 contracts/mocks/WitnetPriceFeedsMock.sol create mode 100644 contracts/mocks/WitnetRequestBoardMock.sol create mode 100644 contracts/mocks/WitnetRequestFactoryMock.sol diff --git a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol index 073e357a..2f46da79 100644 --- a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol +++ b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol @@ -37,9 +37,9 @@ abstract contract WitnetRequestBoardTrustableBase using WitnetV2 for WitnetV2.Response; bytes4 public immutable override specs = type(IWitnetRequestBoard).interfaceId; - - WitnetRequestFactory immutable public override factory; WitnetBytecodes immutable public override registry; + + WitnetRequestFactory immutable private __factory; modifier checkCallbackRecipient(address _addr, uint96 _callbackGasLimit) { require( @@ -76,8 +76,7 @@ abstract contract WitnetRequestBoardTrustableBase "io.witnet.proxiable.board" ) { - assert(address(_factory) != address(0) && address(_registry) != address(0)); - factory = _factory; + __factory = _factory; registry = _registry; } @@ -104,6 +103,10 @@ abstract contract WitnetRequestBoardTrustableBase return bytes4(keccak256(abi.encode(address(this), block.chainid))); } + function factory() virtual override public view returns (WitnetRequestFactory) { + return __factory; + } + // ================================================================================================================ // --- Yet to be implemented virtual methods ---------------------------------------------------------------------- @@ -158,16 +161,16 @@ abstract contract WitnetRequestBoardTrustableBase __storage().base = base(); require( - address(factory).code.length > 0, + address(__factory).code.length > 0, "WitnetRequestBoardTrustableBase: inexistent factory" ); require( - factory.specs() == type(IWitnetRequestFactory).interfaceId, + __factory.specs() == type(IWitnetRequestFactory).interfaceId, "WitnetRequestBoardTrustableBase: uncompliant factory" ); require( - address(factory.witnet()) == address(this) - && address(factory.registry()) == address(registry), + address(__factory.witnet()) == address(this) + && address(__factory.registry()) == address(registry), "WitnetRequestBoardTrustableBase: discordant factory" ); @@ -734,7 +737,7 @@ abstract contract WitnetRequestBoardTrustableBase /// Tells whether given address is included in the active reporters control list. /// @param _reporter The address to be checked. function isReporter(address _reporter) public view override returns (bool) { - return _acls().isReporter_[_reporter]; + return __acls().isReporter_[_reporter]; } /// Adds given addresses to the active reporters control list. @@ -760,7 +763,7 @@ abstract contract WitnetRequestBoardTrustableBase { for (uint ix = 0; ix < _exReporters.length; ix ++) { address _reporter = _exReporters[ix]; - _acls().isReporter_[_reporter] = false; + __acls().isReporter_[_reporter] = false; } emit ReportersUnset(_exReporters); } @@ -983,7 +986,7 @@ abstract contract WitnetRequestBoardTrustableBase { for (uint ix = 0; ix < _reporters.length; ix ++) { address _reporter = _reporters[ix]; - _acls().isReporter_[_reporter] = true; + __acls().isReporter_[_reporter] = true; } emit ReportersSet(_reporters); } diff --git a/contracts/core/defaults/WitnetRequestFactoryDefault.sol b/contracts/core/defaults/WitnetRequestFactoryDefault.sol index a3ef1b11..6bc54b6f 100644 --- a/contracts/core/defaults/WitnetRequestFactoryDefault.sol +++ b/contracts/core/defaults/WitnetRequestFactoryDefault.sol @@ -60,7 +60,6 @@ contract WitnetRequestFactoryDefault "io.witnet.requests.factory" ) { - assert(address(_witnet) != address(0) && address(_registry) != address(0)); witnet = _witnet; registry = _registry; // let logic contract be used as a factory, while avoiding further initializations: diff --git a/contracts/data/WitnetRequestBoardDataACLs.sol b/contracts/data/WitnetRequestBoardDataACLs.sol index 1652d16f..42ca1921 100644 --- a/contracts/data/WitnetRequestBoardDataACLs.sol +++ b/contracts/data/WitnetRequestBoardDataACLs.sol @@ -19,12 +19,12 @@ abstract contract WitnetRequestBoardDataACLs } constructor() { - _acls().isReporter_[msg.sender] = true; + __acls().isReporter_[msg.sender] = true; } modifier onlyReporters { require( - _acls().isReporter_[msg.sender], + __acls().isReporter_[msg.sender], "WitnetRequestBoard: unauthorized reporter" ); _; @@ -33,7 +33,7 @@ abstract contract WitnetRequestBoardDataACLs // ================================================================================================================ // --- Internal functions ----------------------------------------------------------------------------------------- - function _acls() internal pure returns (WitnetBoardACLs storage _struct) { + function __acls() internal pure returns (WitnetBoardACLs storage _struct) { assembly { _struct.slot := _WITNET_BOARD_ACLS_SLOTHASH } diff --git a/contracts/mocks/WitnetBytecodesMock.sol b/contracts/mocks/WitnetBytecodesMock.sol new file mode 100644 index 00000000..6b50c526 --- /dev/null +++ b/contracts/mocks/WitnetBytecodesMock.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "../core/defaults/WitnetBytecodesDefault.sol"; + +contract WitnetBytecodesMock is WitnetBytecodesDefault { + constructor() + WitnetBytecodesDefault( + false, + bytes32("mocked") + ) + {} +} diff --git a/contracts/mocks/WitnetPriceFeedsMock.sol b/contracts/mocks/WitnetPriceFeedsMock.sol new file mode 100644 index 00000000..1656c0bb --- /dev/null +++ b/contracts/mocks/WitnetPriceFeedsMock.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "../apps/WitnetPriceFeeds.sol"; + +contract WitnetPriceFeedsMock is WitnetPriceFeeds { + constructor(WitnetRequestBoard _wrb) + WitnetPriceFeeds( + msg.sender, + _wrb + ) + {} +} diff --git a/contracts/mocks/WitnetRequestBoardMock.sol b/contracts/mocks/WitnetRequestBoardMock.sol new file mode 100644 index 00000000..a1cb8547 --- /dev/null +++ b/contracts/mocks/WitnetRequestBoardMock.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "./WitnetBytecodesMock.sol"; +import "../core/defaults/WitnetRequestBoardTrustableDefault.sol"; + +contract WitnetRequestBoardMock + is + WitnetRequestBoardTrustableDefault +{ + WitnetRequestFactory private __factory; + + constructor(WitnetBytecodesMock _registry) + WitnetRequestBoardTrustableDefault( + WitnetRequestFactory(address(0)), + WitnetBytecodes(address(_registry)), + false, + bytes32("mocked"), + 60000, 65000, 70000, 20000 + ) + { + __acls().isReporter_[msg.sender] = true; + } + + function factory() override public view returns (WitnetRequestFactory) { + return __factory; + } + + function setFactory(WitnetRequestFactory _factory) external onlyOwner { + __factory = _factory; + } +} \ No newline at end of file diff --git a/contracts/mocks/WitnetRequestFactoryMock.sol b/contracts/mocks/WitnetRequestFactoryMock.sol new file mode 100644 index 00000000..41679ed9 --- /dev/null +++ b/contracts/mocks/WitnetRequestFactoryMock.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "./WitnetRequestBoardMock.sol"; +import "../core/defaults/WitnetRequestFactoryDefault.sol"; + +contract WitnetRequestFactoryMock + is + WitnetRequestFactoryDefault +{ + constructor (WitnetRequestBoardMock _wrb) + WitnetRequestFactoryDefault( + WitnetRequestBoard(address(_wrb)), + WitnetBytecodes(_wrb.registry()), + false, + bytes32("mocked") + ) + {} +} \ No newline at end of file