Skip to content

Commit

Permalink
refactor!: rename Cartesi DApp
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Dec 21, 2023
1 parent c2829bc commit 91fc6f9
Show file tree
Hide file tree
Showing 39 changed files with 495 additions and 482 deletions.
10 changes: 10 additions & 0 deletions onchain/rollups/.changeset/sweet-pianos-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@cartesi/rollups": major
---

The `CartesiDApp` contract was renamed as `Application`.
The `ICartesiDApp` contract was renamed as `IApplication`.
The `CartesiDAppFactory` contract was renamed as `ApplicationFactory`.
The `DAppAddressRelay` contract was renamed as `ApplicationAddressRelay`.
The `encodeDAppAddressRelay` function from the former `DAppAddressRelay` contract was renamed as `encodeApplicationAddressRelay`.
The `OnlyDApp` error from the former `CartesiDApp` contract was renamed as `OnlyApplication`.
10 changes: 5 additions & 5 deletions onchain/rollups/contracts/common/InputEncoding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ library InputEncoding {
);
}

/// @notice Encode a DApp address relay.
/// @param dapp The DApp address
/// @notice Encode an application address relay.
/// @param app The application address
/// @return The encoded input
function encodeDAppAddressRelay(
address dapp
function encodeApplicationAddressRelay(
address app
) internal pure returns (bytes memory) {
return
abi.encodePacked(
dapp // 20B
app // 20B
);
}
}
20 changes: 10 additions & 10 deletions onchain/rollups/contracts/consensus/AbstractConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ pragma solidity ^0.8.8;
import {IConsensus} from "./IConsensus.sol";
import {InputRange} from "../common/InputRange.sol";

/// @notice Stores epoch hashes for several DApps and input ranges.
/// @notice Stores epoch hashes for several applications and input ranges.
/// @dev This contract was designed to be inherited by implementations of the `IConsensus` interface
/// that only need a simple mechanism of storage and retrieval of epoch hashes.
abstract contract AbstractConsensus is IConsensus {
/// @notice Indexes epoch hashes by DApp address, first input index and last input index.
/// @notice Indexes epoch hashes by application address, first input index and last input index.
mapping(address => mapping(uint256 => mapping(uint256 => bytes32)))
private _epochHashes;

/// @notice Get the epoch hash for a certain DApp and input range.
/// @param dapp The DApp contract address
/// @notice Get the epoch hash for a certain application and input range.
/// @param app The application contract address
/// @param r The input range
/// @return epochHash The epoch hash
/// @dev For claimed epochs, returns the epoch hash of the last accepted claim.
/// @dev For unclaimed epochs, returns `bytes32(0)`.
function getEpochHash(
address dapp,
address app,
InputRange calldata r
) public view override returns (bytes32 epochHash) {
epochHash = _epochHashes[dapp][r.firstIndex][r.lastIndex];
epochHash = _epochHashes[app][r.firstIndex][r.lastIndex];
}

/// @notice Accept a claim.
/// @param dapp The DApp contract address
/// @param app The application contract address
/// @param r The input range
/// @param epochHash The epoch hash
/// @dev On successs, emits a `ClaimAcceptance` event.
function _acceptClaim(
address dapp,
address app,
InputRange calldata r,
bytes32 epochHash
) internal {
_epochHashes[dapp][r.firstIndex][r.lastIndex] = epochHash;
emit ClaimAcceptance(dapp, r, epochHash);
_epochHashes[app][r.firstIndex][r.lastIndex] = epochHash;
emit ClaimAcceptance(app, r, epochHash);
}
}
28 changes: 14 additions & 14 deletions onchain/rollups/contracts/consensus/IConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ pragma solidity ^0.8.8;

import {InputRange} from "../common/InputRange.sol";

/// @notice Provides epoch hashes for DApps.
/// @notice Provides epoch hashes for applications.
/// @notice An epoch hash is produced after the machine processes a range of inputs and the epoch is finalized.
/// This hash can be later used to prove that any given output was produced by the machine during the epoch.
/// @notice After an epoch is finalized, a validator may submit a claim containing: the address of the DApp contract,
/// @notice After an epoch is finalized, a validator may submit a claim containing: the address of the application contract,
/// the range of inputs accepted during the epoch, and the epoch hash.
/// @notice Validators may synchronize epoch finalization, but such mechanism is not specified by this interface.
/// @notice A validator should be able to save transaction fees by not submitting a claim if it was...
Expand All @@ -22,48 +22,48 @@ import {InputRange} from "../common/InputRange.sol";
interface IConsensus {
/// @notice A claim was submitted to the consensus.
/// @param submitter The submitter address
/// @param dapp The DApp contract address
/// @param app The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev Overwrites any previous submissions regarding `submitter`, `dapp` and `inputRange`.
/// @dev Overwrites any previous submissions regarding `submitter`, `app` and `inputRange`.
event ClaimSubmission(
address indexed submitter,
address indexed dapp,
address indexed app,
InputRange inputRange,
bytes32 epochHash
);

/// @notice A claim was accepted by the consensus.
/// @param dapp The DApp contract address
/// @param app The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev MUST be triggered after some `ClaimSubmission` event regarding `dapp`, `inputRange` and `epochHash`.
/// @dev Overwrites any previous acceptances regarding `dapp` and `inputRange`.
/// @dev MUST be triggered after some `ClaimSubmission` event regarding `app`, `inputRange` and `epochHash`.
/// @dev Overwrites any previous acceptances regarding `app` and `inputRange`.
event ClaimAcceptance(
address indexed dapp,
address indexed app,
InputRange inputRange,
bytes32 epochHash
);

/// @notice Submit a claim to the consensus.
/// @param dapp The DApp contract address
/// @param app The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev On success, MUST trigger a `ClaimSubmission` event.
function submitClaim(
address dapp,
address app,
InputRange calldata inputRange,
bytes32 epochHash
) external;

/// @notice Get the epoch hash for a certain DApp and input range.
/// @param dapp The DApp contract address
/// @notice Get the epoch hash for a certain application and input range.
/// @param app The application contract address
/// @param inputRange The input range
/// @return epochHash The epoch hash
/// @dev For claimed epochs, must return the epoch hash of the last accepted claim.
/// @dev For unclaimed epochs, MUST either revert or return `bytes32(0)`.
function getEpochHash(
address dapp,
address app,
InputRange calldata inputRange
) external view returns (bytes32 epochHash);
}
8 changes: 4 additions & 4 deletions onchain/rollups/contracts/consensus/authority/Authority.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ contract Authority is AbstractConsensus, Ownable {
constructor(address initialOwner) Ownable(initialOwner) {}

/// @notice Submit a claim.
/// @param dapp The DApp contract address
/// @param app The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev On success, triggers a `ClaimSubmission` event and a `ClaimAcceptance` event.
/// @dev Can only be called by the owner.
function submitClaim(
address dapp,
address app,
InputRange calldata inputRange,
bytes32 epochHash
) external override onlyOwner {
emit ClaimSubmission(msg.sender, dapp, inputRange, epochHash);
_acceptClaim(dapp, inputRange, epochHash);
emit ClaimSubmission(msg.sender, app, inputRange, epochHash);
_acceptClaim(app, inputRange, epochHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pragma solidity ^0.8.8;

import {ICartesiDApp} from "./ICartesiDApp.sol";
import {IApplication} from "./IApplication.sol";
import {IConsensus} from "../consensus/IConsensus.sol";
import {IInputBox} from "../inputs/IInputBox.sol";
import {IInputRelay} from "../inputs/IInputRelay.sol";
Expand All @@ -23,11 +23,9 @@ import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Recei
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";

/// @title Cartesi DApp
///
/// @notice This contract acts as the base layer incarnation of a DApp running on the execution layer.
/// The DApp is hereby able to interact with other smart contracts through the execution of vouchers
/// and the validation of notices. These outputs are generated by the DApp backend on the execution
/// @notice This contract acts as the base layer incarnation of an application running on the execution layer.
/// The application is hereby able to interact with other smart contracts through the execution of vouchers
/// and the validation of notices. These outputs are generated by the application backend on the execution
/// layer and can be proven in the base layer thanks to claims submitted by a consensus contract.
///
/// A voucher is a one-time message call to another contract. It can encode asset transfers, approvals,
Expand All @@ -40,20 +38,20 @@ import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";
/// On their own, they do not trigger any type of contract-to-contract interaction.
/// Rather, they merely serve to attest off-chain results, e.g. which player won a particular chess match.
///
/// Every DApp is subscribed to a consensus contract, and governed by a single address, the owner.
/// Every application is subscribed to a consensus contract, and governed by a single address, the owner.
/// The consensus has the power of submitting claims, which, in turn, are used to validate vouchers and notices.
/// Meanwhile, the owner has complete power over the DApp, as it can replace the consensus at any time.
/// Therefore, the users of a DApp must trust both the consensus and the DApp owner.
/// Meanwhile, the owner has complete power over the application, as it can replace the consensus at any time.
/// Therefore, the users of an application must trust both the consensus and the application owner.
///
/// The DApp developer can choose whichever ownership and consensus models it wants.
/// The application developer can choose whichever ownership and consensus models it wants.
///
/// Examples of DApp ownership models include:
/// Examples of application ownership models include:
///
/// * no owner (address zero)
/// * individual signer (externally-owned account)
/// * multiple signers (multi-sig)
/// * DAO (decentralized autonomous organization)
/// * self-owned DApp (off-chain governance logic)
/// * self-owned application (off-chain governance logic)
///
/// See `IConsensus` for examples of consensus models.
///
Expand All @@ -65,8 +63,8 @@ import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";
/// * `ERC1155Holder`
/// * `ReentrancyGuard`
///
contract CartesiDApp is
ICartesiDApp,
contract Application is
IApplication,
Ownable,
ERC721Holder,
ERC1155Holder,
Expand All @@ -84,8 +82,8 @@ contract CartesiDApp is
/// @notice Raised when the transfer fails.
error EtherTransferFailed();

/// @notice Raised when a mehtod is not called by DApp itself.
error OnlyDApp();
/// @notice Raised when a mehtod is not called by application itself.
error OnlyApplication();

/// @notice The initial machine state hash.
/// @dev See the `getTemplateHash` function.
Expand All @@ -108,11 +106,11 @@ contract CartesiDApp is
/// @dev See the `getInputRelays` function.
IInputRelay[] internal inputRelays;

/// @notice Creates a `CartesiDApp` contract.
/// @notice Creates an `Application` contract.
/// @param _consensus The initial consensus contract
/// @param _inputBox The input box contract
/// @param _inputRelays The input relays
/// @param _initialOwner The initial DApp owner
/// @param _initialOwner The initial application owner
/// @param _templateHash The initial machine state hash
constructor(
IConsensus _consensus,
Expand All @@ -133,7 +131,7 @@ contract CartesiDApp is
bytes4 interfaceId
) public view virtual override(ERC1155Holder, IERC165) returns (bool) {
return
interfaceId == type(ICartesiDApp).interfaceId ||
interfaceId == type(IApplication).interfaceId ||
interfaceId == type(IERC721Receiver).interfaceId ||
super.supportsInterface(interfaceId);
}
Expand Down Expand Up @@ -222,19 +220,19 @@ contract CartesiDApp is
}

/// @notice Accept Ether transfers.
/// @dev If you wish to transfer Ether to a DApp while informing
/// the DApp backend of it, then please do so through the Ether portal contract.
/// @dev If you wish to transfer Ether to an application while informing
/// the backend of it, then please do so through the Ether portal contract.
receive() external payable {}

/// @notice Transfer some amount of Ether to some recipient.
/// @param _receiver The address which will receive the amount of Ether
/// @param _value The amount of Ether to be transferred in Wei
/// @dev This function can only be called by the DApp itself through vouchers.
/// If this method is not called by DApp itself, `OnlyDApp` error is raised.
/// @dev This function can only be called by the application itself through vouchers.
/// If this method is not called by application itself, `OnlyApplication` error is raised.
/// If the transfer fails, `EtherTransferFailed` error is raised.
function withdrawEther(address _receiver, uint256 _value) external {
if (msg.sender != address(this)) {
revert OnlyDApp();
revert OnlyApplication();
}

(bool sent, ) = _receiver.call{value: _value}("");
Expand All @@ -245,7 +243,7 @@ contract CartesiDApp is
}

/// @notice Get the epoch hash regarding the given input range
/// and the DApp from the current consensus.
/// and the application from the current consensus.
/// @param inputRange The input range
/// @return The epoch hash
function getEpochHash(
Expand Down
Loading

0 comments on commit 91fc6f9

Please sign in to comment.