Skip to content

Commit

Permalink
refactor: type data availability as IERC165
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Dec 19, 2024
1 parent 8292d60 commit 90d475f
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 41 deletions.
15 changes: 8 additions & 7 deletions contracts/dapp/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";

Expand All @@ -33,6 +34,10 @@ contract Application is
/// @dev See the `getTemplateHash` function.
bytes32 internal immutable _templateHash;

/// @notice The data availability solution.
/// @dev See the `getDataAvailability` function.
IERC165 internal immutable _dataAvailability;

/// @notice Keeps track of which outputs have been executed.
/// @dev See the `wasOutputExecuted` function.
BitMaps.BitMap internal _executed;
Expand All @@ -41,10 +46,6 @@ contract Application is
/// @dev See the `getConsensus` and `migrateToConsensus` functions.
IConsensus internal _consensus;

/// @notice The data availability solution.
/// @dev See the `getDataAvailability` function.
bytes internal _dataAvailability;

/// @notice Creates an `Application` contract.
/// @param consensus The initial consensus contract
/// @param initialOwner The initial application owner
Expand All @@ -54,11 +55,11 @@ contract Application is
IConsensus consensus,
address initialOwner,
bytes32 templateHash,
bytes memory dataAvailability
IERC165 dataAvailability
) Ownable(initialOwner) {
_templateHash = templateHash;
_consensus = consensus;
_dataAvailability = dataAvailability;
_consensus = consensus;
}

/// @notice Accept Ether transfers.
Expand Down Expand Up @@ -146,7 +147,7 @@ contract Application is
external
view
override
returns (bytes memory)
returns (IERC165)
{
return _dataAvailability;
}
Expand Down
7 changes: 4 additions & 3 deletions contracts/dapp/ApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pragma solidity ^0.8.8;

import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IApplicationFactory} from "./IApplicationFactory.sol";
import {IConsensus} from "../consensus/IConsensus.sol";
Expand All @@ -17,7 +18,7 @@ contract ApplicationFactory is IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability
IERC165 dataAvailability
) external override returns (IApplication) {
IApplication appContract = new Application(
consensus,
Expand All @@ -41,7 +42,7 @@ contract ApplicationFactory is IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external override returns (IApplication) {
IApplication appContract = new Application{salt: salt}(
Expand All @@ -66,7 +67,7 @@ contract ApplicationFactory is IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external view override returns (address) {
return
Expand Down
7 changes: 4 additions & 3 deletions contracts/dapp/IApplication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pragma solidity ^0.8.8;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IOwnable} from "../access/IOwnable.sol";
import {IConsensus} from "../consensus/IConsensus.sol";
import {OutputValidityProof} from "../common/OutputValidityProof.sol";
Expand Down Expand Up @@ -117,7 +119,6 @@ interface IApplication is IOwnable {
function getConsensus() external view returns (IConsensus);

/// @notice Get the data availability solution used by application.
/// @return Solidity ABI-encoded function call that describes
/// the source of inputs that should be fed to the application.
function getDataAvailability() external view returns (bytes memory);
/// @return ERC-165 contract that describes the data availability solution
function getDataAvailability() external view returns (IERC165);
}
14 changes: 10 additions & 4 deletions contracts/dapp/IApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pragma solidity ^0.8.8;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IApplication} from "./IApplication.sol";
import {IConsensus} from "../consensus/IConsensus.sol";

Expand All @@ -14,13 +16,14 @@ interface IApplicationFactory {
/// @param consensus The initial consensus contract
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @param appContract The application contract
/// @dev MUST be triggered on a successful call to `newApplication`.
event ApplicationCreated(
IConsensus indexed consensus,
address appOwner,
bytes32 templateHash,
bytes dataAvailability,
IERC165 dataAvailability,
IApplication appContract
);

Expand All @@ -30,20 +33,22 @@ interface IApplicationFactory {
/// @param consensus The initial consensus contract
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @return The application
/// @dev On success, MUST emit an `ApplicationCreated` event.
/// @dev Reverts if the application owner address is zero.
function newApplication(
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability
IERC165 dataAvailability
) external returns (IApplication);

/// @notice Deploy a new application deterministically.
/// @param consensus The initial consensus contract
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @param salt The salt used to deterministically generate the application contract address
/// @return The application
/// @dev On success, MUST emit an `ApplicationCreated` event.
Expand All @@ -52,14 +57,15 @@ interface IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external returns (IApplication);

/// @notice Calculate the address of an application contract to be deployed deterministically.
/// @param consensus The initial consensus contract
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @param salt The salt used to deterministically generate the application contract address
/// @return The deterministic application contract address
/// @dev Beware that only the `newApplication` function with the `salt` parameter
Expand All @@ -68,7 +74,7 @@ interface IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external view returns (address);
}
8 changes: 6 additions & 2 deletions contracts/dapp/ISelfHostedApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pragma solidity ^0.8.8;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IAuthority} from "../consensus/authority/IAuthority.sol";
import {IAuthorityFactory} from "../consensus/authority/IAuthorityFactory.sol";
import {IApplication} from "./IApplication.sol";
Expand All @@ -26,6 +28,7 @@ interface ISelfHostedApplicationFactory {
/// @param epochLength The epoch length
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @param salt The salt used to deterministically generate the addresses
/// @return The application contract
/// @return The authority contract
Expand All @@ -37,7 +40,7 @@ interface ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external returns (IApplication, IAuthority);

Expand All @@ -47,6 +50,7 @@ interface ISelfHostedApplicationFactory {
/// @param epochLength The epoch length
/// @param appOwner The initial application owner
/// @param templateHash The initial machine state hash
/// @param dataAvailability The data availability solution
/// @param salt The salt used to deterministically generate the addresses
/// @return The application address
/// @return The authority address
Expand All @@ -55,7 +59,7 @@ interface ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external view returns (address, address);
}
6 changes: 4 additions & 2 deletions contracts/dapp/SelfHostedApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pragma solidity ^0.8.8;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IConsensus} from "../consensus/IConsensus.sol";
import {IAuthority} from "../consensus/authority/IAuthority.sol";
import {IAuthorityFactory} from "../consensus/authority/IAuthorityFactory.sol";
Expand Down Expand Up @@ -50,7 +52,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external returns (IApplication application, IAuthority authority) {
authority = _authorityFactory.newAuthority(
Expand All @@ -73,7 +75,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) external view returns (address application, address authority) {
authority = _authorityFactory.calculateAuthorityAddress(
Expand Down
11 changes: 6 additions & 5 deletions test/dapp/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Re
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {IERC20Errors, IERC721Errors, IERC1155Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
Expand Down Expand Up @@ -54,7 +55,7 @@ contract ApplicationTest is TestBase, OwnableTest {
address _authorityOwner;
address _recipient;
address _tokenOwner;
bytes _dataAvailability;
IERC165 _dataAvailability;
string[] _outputNames;
bytes4[] _interfaceIds;
uint256[] _tokenIds;
Expand Down Expand Up @@ -94,14 +95,14 @@ contract ApplicationTest is TestBase, OwnableTest {
address(0)
)
);
new Application(_consensus, address(0), _templateHash, new bytes(0));
new Application(_consensus, address(0), _templateHash, IERC165(address(0)));
}

function testConstructor(
IConsensus consensus,
address owner,
bytes32 templateHash,
bytes calldata dataAvailability
IERC165 dataAvailability
) external {
vm.assume(owner != address(0));

Expand All @@ -118,7 +119,7 @@ contract ApplicationTest is TestBase, OwnableTest {
assertEq(address(appContract.getConsensus()), address(consensus));
assertEq(appContract.owner(), owner);
assertEq(appContract.getTemplateHash(), templateHash);
assertEq(appContract.getDataAvailability(), dataAvailability);
assertEq(address(appContract.getDataAvailability()), address(dataAvailability));
}

// -------------------
Expand Down Expand Up @@ -347,7 +348,7 @@ contract ApplicationTest is TestBase, OwnableTest {
);
_inputBox = new InputBox();
_consensus = new Authority(_authorityOwner, _epochLength);
_dataAvailability = new bytes(0);
_dataAvailability = IERC165(address(0));
_appContract = new Application(
_consensus,
_appOwner,
Expand Down
22 changes: 12 additions & 10 deletions test/dapp/ApplicationFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/// @title Application Factory Test
pragma solidity ^0.8.22;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {TestBase} from "../util/TestBase.sol";
import {ApplicationFactory, IApplicationFactory} from "contracts/dapp/ApplicationFactory.sol";
import {IApplication} from "contracts/dapp/IApplication.sol";
Expand All @@ -21,7 +23,7 @@ contract ApplicationFactoryTest is TestBase {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability
IERC165 dataAvailability
) public {
vm.assume(appOwner != address(0));

Expand All @@ -35,14 +37,14 @@ contract ApplicationFactoryTest is TestBase {
assertEq(address(appContract.getConsensus()), address(consensus));
assertEq(appContract.owner(), appOwner);
assertEq(appContract.getTemplateHash(), templateHash);
assertEq(appContract.getDataAvailability(), dataAvailability);
assertEq(address(appContract.getDataAvailability()), address(dataAvailability));
}

function testNewApplicationDeterministic(
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) public {
vm.assume(appOwner != address(0));
Expand All @@ -69,7 +71,7 @@ contract ApplicationFactoryTest is TestBase {
assertEq(address(appContract.getConsensus()), address(consensus));
assertEq(appContract.owner(), appOwner);
assertEq(appContract.getTemplateHash(), templateHash);
assertEq(appContract.getDataAvailability(), dataAvailability);
assertEq(address(appContract.getDataAvailability()), address(dataAvailability));

precalculatedAddress = _factory.calculateApplicationAddress(
consensus,
Expand Down Expand Up @@ -97,7 +99,7 @@ contract ApplicationFactoryTest is TestBase {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability
IERC165 dataAvailability
) public {
vm.assume(appOwner != address(0));

Expand All @@ -123,7 +125,7 @@ contract ApplicationFactoryTest is TestBase {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
bytes32 salt
) public {
vm.assume(appOwner != address(0));
Expand Down Expand Up @@ -151,7 +153,7 @@ contract ApplicationFactoryTest is TestBase {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
IERC165 dataAvailability,
IApplication appContract
) internal {
Vm.Log[] memory entries = vm.getRecordedLogs();
Expand All @@ -176,16 +178,16 @@ contract ApplicationFactoryTest is TestBase {
(
address appOwner_,
bytes32 templateHash_,
bytes memory dataAvailability_,
IERC165 dataAvailability_,
IApplication app_
) = abi.decode(
entry.data,
(address, bytes32, bytes, IApplication)
(address, bytes32, IERC165, IApplication)
);

assertEq(appOwner, appOwner_);
assertEq(templateHash, templateHash_);
assertEq(dataAvailability, dataAvailability_);
assertEq(address(dataAvailability), address(dataAvailability_));
assertEq(address(appContract), address(app_));
}
}
Expand Down
Loading

0 comments on commit 90d475f

Please sign in to comment.