Skip to content

Commit

Permalink
feat: add getDataAvailability function to IApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Dec 13, 2024
1 parent f9c97d5 commit 846a99b
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-boats-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/rollups": minor
---

Add data availability configuration to application contract
28 changes: 28 additions & 0 deletions contracts/common/DataAvailability.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.8;

import {IInputBox} from "../inputs/IInputBox.sol";

/// @title Data Availability
/// @notice Defines the signatures of data availability solutions.
interface DataAvailability {
/// @notice The application receives inputs only from
/// a contract that implements the `IInputBox` interface.
/// @param inputBox The input box contract address
function InputBox(IInputBox inputBox) external;

/// @notice The application receives inputs from
/// a contract that implements the `IInputBox` interface,
/// and from Espresso, starting from a given block height,
/// and for a given namespace ID.
/// @param inputBox The input box contract address
/// @param fromBlock Height of first Espresso block to consider
/// @param namespaceId The Espresso namespace ID
function InputBoxAndEspresso(
IInputBox inputBox,
uint256 fromBlock,
uint32 namespaceId
) external;
}
17 changes: 16 additions & 1 deletion contracts/dapp/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ 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 @@ -49,10 +53,12 @@ contract Application is
constructor(
IConsensus consensus,
address initialOwner,
bytes32 templateHash
bytes32 templateHash,
bytes memory dataAvailability
) Ownable(initialOwner) {
_templateHash = templateHash;
_consensus = consensus;
_dataAvailability = dataAvailability;
}

/// @notice Accept Ether transfers.
Expand Down Expand Up @@ -136,6 +142,15 @@ contract Application is
return _consensus;
}

function getDataAvailability()
external
view
override
returns (bytes memory)
{
return _dataAvailability;
}

function owner() public view override(IOwnable, Ownable) returns (address) {
return super.owner();
}
Expand Down
34 changes: 28 additions & 6 deletions contracts/dapp/ApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ contract ApplicationFactory is IApplicationFactory {
function newApplication(
IConsensus consensus,
address appOwner,
bytes32 templateHash
bytes32 templateHash,
bytes calldata dataAvailability
) external override returns (IApplication) {
IApplication appContract = new Application(
consensus,
appOwner,
templateHash
templateHash,
dataAvailability
);

emit ApplicationCreated(consensus, appOwner, templateHash, appContract);
emit ApplicationCreated(
consensus,
appOwner,
templateHash,
dataAvailability,
appContract
);

return appContract;
}
Expand All @@ -33,15 +41,23 @@ contract ApplicationFactory is IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external override returns (IApplication) {
IApplication appContract = new Application{salt: salt}(
consensus,
appOwner,
templateHash
templateHash,
dataAvailability
);

emit ApplicationCreated(consensus, appOwner, templateHash, appContract);
emit ApplicationCreated(
consensus,
appOwner,
templateHash,
dataAvailability,
appContract
);

return appContract;
}
Expand All @@ -50,6 +66,7 @@ contract ApplicationFactory is IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external view override returns (address) {
return
Expand All @@ -58,7 +75,12 @@ contract ApplicationFactory is IApplicationFactory {
keccak256(
abi.encodePacked(
type(Application).creationCode,
abi.encode(consensus, appOwner, templateHash)
abi.encode(
consensus,
appOwner,
templateHash,
dataAvailability
)
)
)
);
Expand Down
5 changes: 5 additions & 0 deletions contracts/dapp/IApplication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ interface IApplication is IOwnable {
/// @notice Get the current consensus.
/// @return The current consensus
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);
}
6 changes: 5 additions & 1 deletion contracts/dapp/IApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface IApplicationFactory {
IConsensus indexed consensus,
address appOwner,
bytes32 templateHash,
bytes dataAvailability,
IApplication appContract
);

Expand All @@ -35,7 +36,8 @@ interface IApplicationFactory {
function newApplication(
IConsensus consensus,
address appOwner,
bytes32 templateHash
bytes32 templateHash,
bytes calldata dataAvailability
) external returns (IApplication);

/// @notice Deploy a new application deterministically.
Expand All @@ -50,6 +52,7 @@ interface IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external returns (IApplication);

Expand All @@ -65,6 +68,7 @@ interface IApplicationFactory {
IConsensus consensus,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external view returns (address);
}
2 changes: 2 additions & 0 deletions contracts/dapp/ISelfHostedApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external returns (IApplication, IAuthority);

Expand All @@ -54,6 +55,7 @@ interface ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external view returns (address, address);
}
4 changes: 4 additions & 0 deletions contracts/dapp/SelfHostedApplicationFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external returns (IApplication application, IAuthority authority) {
authority = _authorityFactory.newAuthority(
Expand All @@ -62,6 +63,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
authority,
appOwner,
templateHash,
dataAvailability,
salt
);
}
Expand All @@ -71,6 +73,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
uint256 epochLength,
address appOwner,
bytes32 templateHash,
bytes calldata dataAvailability,
bytes32 salt
) external view returns (address application, address authority) {
authority = _authorityFactory.calculateAuthorityAddress(
Expand All @@ -83,6 +86,7 @@ contract SelfHostedApplicationFactory is ISelfHostedApplicationFactory {
IConsensus(authority),
appOwner,
templateHash,
dataAvailability,
salt
);
}
Expand Down
26 changes: 22 additions & 4 deletions test/dapp/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {Outputs} from "contracts/common/Outputs.sol";
import {SafeERC20Transfer} from "contracts/delegatecall/SafeERC20Transfer.sol";
import {IOwnable} from "contracts/access/IOwnable.sol";
import {LibAddress} from "contracts/library/LibAddress.sol";
import {InputBox} from "contracts/inputs/InputBox.sol";
import {IInputBox} from "contracts/inputs/IInputBox.sol";
import {DataAvailability} from "contracts/common/DataAvailability.sol";

import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
Expand Down Expand Up @@ -45,12 +48,14 @@ contract ApplicationTest is TestBase, OwnableTest {
IERC1155 _erc1155SingleToken;
IERC1155 _erc1155BatchToken;
SafeERC20Transfer _safeERC20Transfer;
IInputBox _inputBox;

LibEmulator.State _emulator;
address _appOwner;
address _authorityOwner;
address _recipient;
address _tokenOwner;
bytes _dataAvailability;
string[] _outputNames;
bytes4[] _interfaceIds;
uint256[] _tokenIds;
Expand Down Expand Up @@ -90,13 +95,14 @@ contract ApplicationTest is TestBase, OwnableTest {
address(0)
)
);
new Application(_consensus, address(0), _templateHash);
new Application(_consensus, address(0), _templateHash, new bytes(0));
}

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

Expand All @@ -106,12 +112,14 @@ contract ApplicationTest is TestBase, OwnableTest {
IApplication appContract = new Application(
consensus,
owner,
templateHash
templateHash,
dataAvailability
);

assertEq(address(appContract.getConsensus()), address(consensus));
assertEq(appContract.owner(), owner);
assertEq(appContract.getTemplateHash(), templateHash);
assertEq(appContract.getDataAvailability(), dataAvailability);
}

// -------------------
Expand Down Expand Up @@ -338,8 +346,18 @@ contract ApplicationTest is TestBase, OwnableTest {
_tokenIds,
_initialSupplies
);
_inputBox = new InputBox();
_consensus = new Authority(_authorityOwner, _epochLength);
_appContract = new Application(_consensus, _appOwner, _templateHash);
_dataAvailability = abi.encodeCall(
DataAvailability.InputBox,
(_inputBox)
);
_appContract = new Application(
_consensus,
_appOwner,
_templateHash,
_dataAvailability
);
_safeERC20Transfer = new SafeERC20Transfer();
}

Expand Down
Loading

0 comments on commit 846a99b

Please sign in to comment.