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

Full Output Unification #292

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions .changeset/calm-panthers-retire.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"@cartesi/rollups": major
---

Modified the `OutputValidityProof` struct:
Modified the `OutputValidityProof` struct

- Collapsed the `vouchersEpochRootHash` and `noticesEpochRootHash` fields into a single `outputsEpochRootHash` field
- Added an `inputRange` field
- Removed all fields

- Added an `outputIndex` field

- Added an `outputHashesSiblings` field
2 changes: 0 additions & 2 deletions .changeset/clean-gorillas-occur.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

Modified the `CanonicalMachine` library:

- Collapsed the `VOUCHER_METADATA_LOG2_SIZE` and `NOTICE_METADATA_LOG2_SIZE` constants into a single `OUTPUT_METADATA_LOG2_SIZE` constant (with the same value).
- Collapsed the `EPOCH_VOUCHER_LOG2_SIZE` and `EPOCH_NOTICE_LOG2_SIZE` constants into a single `EPOCH_OUTPUT_LOG2_SIZE` constant (with the same value).
- Updated the value of the `INPUT_MAX_SIZE` constant to reflect a change in the off-chain machine.
11 changes: 0 additions & 11 deletions .changeset/fifty-keys-tickle.md

This file was deleted.

2 changes: 1 addition & 1 deletion .changeset/fuzzy-trainers-tan.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
Modified the `AbstractConsensus` contract:

- Removed the `join` function
- Implemented the `getEpochHash` function
- Implemented the `wasClaimAccepted` function
- Added an internal `_acceptClaim` function
2 changes: 0 additions & 2 deletions .changeset/healthy-wasps-shout.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
Added:

- an `Outputs` interface
- an `InputRange` struct
- a `LibAddress` library
- a `LibInputRange` library
- a `LibError` library
- a `LibMerkle32` library
- a `Quorum` contract (which implements the `IConsensus` interface)
Expand Down
2 changes: 0 additions & 2 deletions .changeset/lazy-gorillas-scream.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ Modified the `IInputRelay` interface:
- Renamed it as `IPortal`

- Moved it to `contracts/portals`

- Made it inherit from `IERC165`
11 changes: 0 additions & 11 deletions .changeset/lemon-rivers-nail.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/orange-poems-fry.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/quiet-guests-greet.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@
Modified the `ICartesiDAppFactory` interface:

- Renamed it as `IApplicationFactory`.

- Added the following parameters to its functions and events:

- `inputBox`
- `portals`
10 changes: 0 additions & 10 deletions .changeset/red-wasps-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ Modified the `CartesiDApp` contract:

- Renamed it as `Application`.

- Added the following parameters to its constructor:

- `inputBox`
- `portals`

- Made it support the following interfaces (as in EIP-165):

- `IApplication`
- `IERC721Receiver`

- Removed the `withdrawEther` function.

- Removed the `OnlyApplication` error.
Expand Down
11 changes: 0 additions & 11 deletions .changeset/rude-scissors-try.md

This file was deleted.

15 changes: 6 additions & 9 deletions .changeset/silly-islands-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Modified the `ICartesiDApp` interface:
- Made it inherit from:

- `IERC721Receiver`.
- `IERC1155Receiver` (which inherits from `IERC165`).
- `IERC1155Receiver`.

- Modified the `executeVoucher` function:

Expand All @@ -27,25 +27,22 @@ Modified the `ICartesiDApp` interface:
- Modified the `VoucherExecuted` event:

- Renamed it as `OutputExecuted`.
- Split the `voucherId` parameter into `inputIndex` and `outputIndexWithinInput` parameters.
- Removed `voucherId` parameters.
- Added an `outputIndex` parameter.
- Added an `output` parameter.

- Modified the `wasVoucherExecuted` function:

- Renamed it as `wasOutputExecuted`.

- Added a `getInputBox` function.

- Added a `getPortals` function.
- Added a `validateOutputHash` function.

- Added an `InputIndexOutOfRange` error.

- Added an `OutputNotExecutable` error.

- Added an `OutputNotReexecutable` error.

- Added an `IncorrectEpochHash` error.

- Added an `IncorrectOutputsEpochRootHash` error.
- Added an `InvalidOutputHashesSiblingsArrayLength` error.

- Added an `IncorrectOutputHashesRootHash` error.
- Added an `ClaimNotAccepted` error.
5 changes: 0 additions & 5 deletions .changeset/smooth-ducks-trade.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ Modified the `InputRelay` contract:
- Renamed it as `Portal`

- Moved it to `contracts/portals`

- Made it support the following interfaces (as in EIP-165):

- `IERC165`
- `IPortal`
2 changes: 1 addition & 1 deletion .changeset/spotty-peas-speak.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Modified the `Authority` contract:
- Removed the `setHistory` function
- Removed the `submitClaim(bytes)` function
- Removed the `withdrawERC20Tokens` function
- Implemented the `submitClaim(address,(uint64,uint64),bytes32)` function
- Implemented the `submitClaim(address,bytes32)` function
2 changes: 1 addition & 1 deletion .changeset/strange-foxes-melt.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Completely modified the `IConsensus` interface:
- Removed the `getClaim` function
- Removed the `ApplicationJoined` event
- Added a `submitClaim` function
- Added a `getEpochHash` function
- Added a `wasClaimAccepted` function
- Added a `ClaimSubmission` event
- Added a `ClaimAcceptance` event
11 changes: 0 additions & 11 deletions .changeset/two-mails-marry.md

This file was deleted.

11 changes: 0 additions & 11 deletions .changeset/wise-owls-push.md

This file was deleted.

7 changes: 2 additions & 5 deletions contracts/common/CanonicalMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ library CanonicalMachine {
/// @notice Maximum input size (2 megabytes).
uint256 constant INPUT_MAX_SIZE = 1 << 21;

/// @notice Log of maximum number of inputs per epoch.
uint256 constant LOG2_MAX_INPUTS_PER_EPOCH = 32;

/// @notice Log of maximum number of outputs per input.
uint256 constant LOG2_MAX_OUTPUTS_PER_INPUT = 16;
/// @notice Log2 of maximum number of outputs.
uint256 constant LOG2_MAX_OUTPUTS = 63;
}
12 changes: 0 additions & 12 deletions contracts/common/InputRange.sol

This file was deleted.

26 changes: 8 additions & 18 deletions contracts/common/OutputValidityProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@

pragma solidity ^0.8.8;

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

/// @param inputRange The range of inputs accepted during the epoch
/// @param inputIndexWithinEpoch Which input, inside the epoch, the output belongs to
/// @param outputIndexWithinInput Index of output emitted by the input
/// @param outputHashesRootHash Merkle root of hashes of outputs emitted by the input
/// @param outputsEpochRootHash Merkle root of all epoch's outputs metadata hashes
/// @param machineStateHash Hash of the machine state claimed this epoch
/// @param outputHashInOutputHashesSiblings Proof that this output metadata is in metadata memory range
/// @param outputHashesInEpochSiblings Proof that this output metadata is in epoch's output memory range
/// @notice Proof of inclusion of an output in the output Merkle tree.
/// @param outputIndex Index of output in the Merkle tree
/// @param outputHashesSiblings Siblings of the output in the Merkle tree
/// @dev From the index and siblings, one can calculate the root of the Merkle tree.
/// @dev The siblings array should have size equal to the log2 of the maximum number of outputs.
/// @dev See the `CanonicalMachine` library for constants.
struct OutputValidityProof {
InputRange inputRange;
uint64 inputIndexWithinEpoch;
uint64 outputIndexWithinInput;
bytes32 outputHashesRootHash;
bytes32 outputsEpochRootHash;
bytes32 machineStateHash;
bytes32[] outputHashInOutputHashesSiblings;
bytes32[] outputHashesInEpochSiblings;
uint64 outputIndex;
guidanoli marked this conversation as resolved.
Show resolved Hide resolved
bytes32[] outputHashesSiblings;
}
41 changes: 16 additions & 25 deletions contracts/consensus/AbstractConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,31 @@
pragma solidity ^0.8.8;

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

/// @notice Stores epoch hashes for several applications and input ranges.
/// @notice Stores accepted claims for several applications.
/// @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.
/// that only need a simple mechanism of storage and retrieval of accepted claims.
abstract contract AbstractConsensus is IConsensus {
/// @notice Indexes epoch hashes by application contract address, first input index and last input index.
mapping(address => mapping(uint256 => mapping(uint256 => bytes32)))
private _epochHashes;
/// @notice Indexes accepted claims by application contract address.
mapping(address => mapping(bytes32 => bool)) private _acceptedClaims;
guidanoli marked this conversation as resolved.
Show resolved Hide resolved

/// @notice Get the epoch hash for a certain application and input range.
/// @notice Check if an output Merkle root hash was ever accepted by the consensus
/// for a particular application.
/// @param appContract 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(
/// @param claim The output Merkle root hash
function wasClaimAccepted(
address appContract,
InputRange calldata r
) public view override returns (bytes32 epochHash) {
epochHash = _epochHashes[appContract][r.firstIndex][r.lastIndex];
bytes32 claim
) public view override returns (bool) {
return _acceptedClaims[appContract][claim];
}

/// @notice Accept a claim.
/// @param appContract The application contract address
/// @param r The input range
/// @param epochHash The epoch hash
/// @dev On successs, emits a `ClaimAcceptance` event.
function _acceptClaim(
address appContract,
InputRange calldata r,
bytes32 epochHash
) internal {
_epochHashes[appContract][r.firstIndex][r.lastIndex] = epochHash;
emit ClaimAcceptance(appContract, r, epochHash);
/// @param claim The output Merkle root hash
/// @dev Emits a `ClaimAcceptance` event.
function _acceptClaim(address appContract, bytes32 claim) internal {
_acceptedClaims[appContract][claim] = true;
emit ClaimAcceptance(appContract, claim);
}
}
55 changes: 19 additions & 36 deletions contracts/consensus/IConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

pragma solidity ^0.8.8;

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

/// @notice Provides data availability of 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 Provides consensus over the set of valid output Merkle root hashes for applications.
/// @notice The latest output Merkle root hash is available after the machine processes every input.
/// This hash can be later used to prove that any given output was ever produced by the machine.
/// @notice After an epoch is finalized, a validator may submit a claim containing the application contract address,
/// the range of inputs accepted during the epoch, and the epoch hash.
/// and the output Merkle root 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...
/// - already submitted by the validator (see the `ClaimSubmission` event) or;
Expand All @@ -18,53 +16,38 @@ import {InputRange} from "../common/InputRange.sol";
/// For example, a claim may be accepted if it was...
/// - submitted by an authority or;
/// - submitted by the majority of a quorum or;
/// - submitted and not proven wrong after some period of time.
/// - submitted and not proven wrong after some period of time or;
/// - submitted and proven correct through an on-chain tournament.
interface IConsensus {
/// @notice MUST trigger when a claim is submitted.
/// @param submitter The submitter address
/// @param appContract The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev Overwrites any previous submissions regarding `submitter`, `appContract` and `inputRange`.
/// @param claim The output Merkle root hash
event ClaimSubmission(
address indexed submitter,
address indexed appContract,
InputRange inputRange,
bytes32 epochHash
bytes32 claim
);

/// @notice MUST trigger when a claim is accepted.
/// @param appContract The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @dev MUST be triggered after some `ClaimSubmission` event regarding `appContract`, `inputRange` and `epochHash`.
/// @dev Overwrites any previous acceptances regarding `appContract` and `inputRange`.
event ClaimAcceptance(
address indexed appContract,
InputRange inputRange,
bytes32 epochHash
);
/// @param claim The output Merkle root hash
/// @dev MUST be triggered after some `ClaimSubmission` event regarding `appContract`.
guidanoli marked this conversation as resolved.
Show resolved Hide resolved
event ClaimAcceptance(address indexed appContract, bytes32 claim);

/// @notice Submit a claim to the consensus.
/// @param appContract The application contract address
/// @param inputRange The input range
/// @param epochHash The epoch hash
/// @param claim The output Merkle root hash
/// @dev MUST fire a `ClaimSubmission` event.
/// @dev MAY fire a `ClaimAcceptance` event, if the acceptance criteria is met.
function submitClaim(
address appContract,
InputRange calldata inputRange,
bytes32 epochHash
) external;
function submitClaim(address appContract, bytes32 claim) external;

/// @notice Get the epoch hash for a certain application and input range.
/// @notice Check if an output Merkle root hash was ever accepted by the consensus
/// for a particular application.
/// @param appContract 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(
/// @param claim The output Merkle root hash
function wasClaimAccepted(
address appContract,
InputRange calldata inputRange
) external view returns (bytes32 epochHash);
bytes32 claim
) external view returns (bool);
}
Loading