Skip to content

Commit

Permalink
refactor(protocol): replace finalize with verify (#298)
Browse files Browse the repository at this point in the history
* chore(ci): disable checking coverage against the base of the PR

* chore: update configs

* chore: add flag_management

* chore: try

* chore: try2

* feat(protocol): enable whitelisting provers (disabled for now) (#287)

* Finalize -> Verify

Co-authored-by: David <david@taikocha.in>
  • Loading branch information
dantaik and davidtaikocha committed Nov 22, 2022
1 parent 151415e commit 5c4d370
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 67 deletions.
14 changes: 7 additions & 7 deletions packages/protocol/contracts/L1/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ library LibData {
mapping(bytes32 => uint256) commits;
mapping(address => bool) provers; // Whitelisted provers
uint64 genesisHeight;
uint64 latestFinalizedHeight;
uint64 latestFinalizedId;
uint64 latestVerifiedHeight;
uint64 latestVerifiedId;
uint64 nextBlockId;
}

Expand All @@ -69,7 +69,7 @@ library LibData {
State storage s,
uint256 number
) internal view returns (bytes32) {
require(number <= s.latestFinalizedHeight, "L1:id");
require(number <= s.latestVerifiedHeight, "L1:id");
return s.l2Hashes[number];
}

Expand All @@ -80,14 +80,14 @@ library LibData {
view
returns (
uint64 genesisHeight,
uint64 latestFinalizedHeight,
uint64 latestFinalizedId,
uint64 latestVerifiedHeight,
uint64 latestVerifiedId,
uint64 nextBlockId
)
{
genesisHeight = s.genesisHeight;
latestFinalizedHeight = s.latestFinalizedHeight;
latestFinalizedId = s.latestFinalizedId;
latestVerifiedHeight = s.latestVerifiedHeight;
latestVerifiedId = s.latestVerifiedId;
nextBlockId = s.nextBlockId;
}

Expand Down
33 changes: 18 additions & 15 deletions packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
*/
function proposeBlock(bytes[] calldata inputs) external nonReentrant {
V1Proposing.proposeBlock(state, inputs);
V1Finalizing.finalizeBlocks(
V1Finalizing.verifyBlocks(
state,
LibConstants.TAIKO_MAX_FINALIZATIONS_PER_TX
LibConstants.TAIKO_MAX_VERIFICATIONS_PER_TX
);
}

Expand All @@ -97,9 +97,9 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
bytes[] calldata inputs
) external nonReentrant {
V1Proving.proveBlock(state, AddressResolver(this), blockIndex, inputs);
V1Finalizing.finalizeBlocks(
V1Finalizing.verifyBlocks(
state,
LibConstants.TAIKO_MAX_FINALIZATIONS_PER_TX
LibConstants.TAIKO_MAX_VERIFICATIONS_PER_TX
);
}

Expand Down Expand Up @@ -128,9 +128,9 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
blockIndex,
inputs
);
V1Finalizing.finalizeBlocks(
V1Finalizing.verifyBlocks(
state,
LibConstants.TAIKO_MAX_FINALIZATIONS_PER_TX
LibConstants.TAIKO_MAX_VERIFICATIONS_PER_TX
);
}

Expand All @@ -157,11 +157,14 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
return V1Proving.isProverWhitelisted(state, prover);
}

/// @notice Finalize up to N blocks.
/// @param maxBlocks Max number of blocks to finalize.
function finalizeBlocks(uint256 maxBlocks) external nonReentrant {

/**
* Verify up to N blocks.
* @param maxBlocks Max number of blocks to verify.
*/
function verifyBlocks(uint256 maxBlocks) external nonReentrant {
require(maxBlocks > 0, "L1:maxBlocks");
V1Finalizing.finalizeBlocks(state, maxBlocks);
V1Finalizing.verifyBlocks(state, maxBlocks);
}

function isCommitValid(bytes32 hash) public view returns (bool) {
Expand All @@ -185,16 +188,16 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
}

function getLatestSyncedHeader() public view override returns (bytes32) {
return state.getL2BlockHash(state.latestFinalizedHeight);
return state.getL2BlockHash(state.latestVerifiedHeight);
}

function getStateVariables()
public
view
returns (
uint64 /*genesisHeight*/,
uint64 /*latestFinalizedHeight*/,
uint64 /*latestFinalizedId*/,
uint64 /*latestVerifiedHeight*/,
uint64 /*latestVerifiedId*/,
uint64 /*nextBlockId*/
)
{
Expand All @@ -214,7 +217,7 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
returns (
uint256, // TAIKO_CHAIN_ID
uint256, // TAIKO_MAX_PROPOSED_BLOCKS
uint256, // TAIKO_MAX_FINALIZATIONS_PER_TX
uint256, // TAIKO_MAX_VERIFICATIONS_PER_TX
uint256, // TAIKO_COMMIT_DELAY_CONFIRMATIONS
uint256, // TAIKO_MAX_PROOFS_PER_FORK_CHOICE
uint256, // TAIKO_BLOCK_MAX_GAS_LIMIT
Expand All @@ -230,7 +233,7 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
return (
LibConstants.TAIKO_CHAIN_ID,
LibConstants.TAIKO_MAX_PROPOSED_BLOCKS,
LibConstants.TAIKO_MAX_FINALIZATIONS_PER_TX,
LibConstants.TAIKO_MAX_VERIFICATIONS_PER_TX,
LibConstants.TAIKO_COMMIT_DELAY_CONFIRMATIONS,
LibConstants.TAIKO_MAX_PROOFS_PER_FORK_CHOICE,
LibConstants.TAIKO_BLOCK_MAX_GAS_LIMIT,
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/v1/V1Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "../LibData.sol";
/// @author david <david@taiko.xyz>
abstract contract V1Events {
// The following events must match the definitions in other V1 libraries.
event BlockFinalized(uint256 indexed id, bytes32 blockHash);
event BlockVerified(uint256 indexed id, bytes32 blockHash);

event BlockCommitted(bytes32 hash, uint256 validSince);

Expand Down
20 changes: 10 additions & 10 deletions packages/protocol/contracts/L1/v1/V1Finalizing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../LibData.sol";

/// @author dantaik <dan@taiko.xyz>
library V1Finalizing {
event BlockFinalized(uint256 indexed id, bytes32 blockHash);
event BlockVerified(uint256 indexed id, bytes32 blockHash);

event HeaderSynced(
uint256 indexed height,
Expand All @@ -27,39 +27,39 @@ library V1Finalizing {
s.nextBlockId = 1;
s.genesisHeight = uint64(block.number);

emit BlockFinalized(0, _genesisBlockHash);
emit BlockVerified(0, _genesisBlockHash);
emit HeaderSynced(block.number, 0, _genesisBlockHash);
}

function finalizeBlocks(LibData.State storage s, uint256 maxBlocks) public {
uint64 latestL2Height = s.latestFinalizedHeight;
function verifyBlocks(LibData.State storage s, uint256 maxBlocks) public {
uint64 latestL2Height = s.latestVerifiedHeight;
bytes32 latestL2Hash = s.l2Hashes[latestL2Height];
uint64 processed = 0;

for (
uint256 i = s.latestFinalizedId + 1;
uint256 i = s.latestVerifiedId + 1;
i < s.nextBlockId && processed <= maxBlocks;
i++
) {
LibData.ForkChoice storage fc = s.forkChoices[i][latestL2Hash];

if (fc.blockHash == LibConstants.TAIKO_BLOCK_DEADEND_HASH) {
emit BlockFinalized(i, 0);
emit BlockVerified(i, 0);
} else if (fc.blockHash != 0) {
latestL2Height += 1;
latestL2Hash = fc.blockHash;
emit BlockFinalized(i, latestL2Hash);
emit BlockVerified(i, latestL2Hash);
} else {
break;
}
processed += 1;
}

if (processed > 0) {
s.latestFinalizedId += processed;
s.latestVerifiedId += processed;

if (latestL2Height > s.latestFinalizedHeight) {
s.latestFinalizedHeight = latestL2Height;
if (latestL2Height > s.latestVerifiedHeight) {
s.latestVerifiedHeight = latestL2Height;
s.l2Hashes[latestL2Height] = latestL2Hash;
emit HeaderSynced(block.number, latestL2Height, latestL2Hash);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/v1/V1Proposing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ library V1Proposing {
);
require(
s.nextBlockId <=
s.latestFinalizedId + LibConstants.TAIKO_MAX_PROPOSED_BLOCKS,
s.latestVerifiedId + LibConstants.TAIKO_MAX_PROPOSED_BLOCKS,
"L1:tooMany"
);

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/v1/V1Proving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ library V1Proving {
LibData.BlockMetadata memory meta
) private view {
require(
meta.id > s.latestFinalizedId && meta.id < s.nextBlockId,
meta.id > s.latestVerifiedId && meta.id < s.nextBlockId,
"L1:meta:id"
);
require(
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L2/V1TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ contract V1TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {
returns (
uint256, // TAIKO_CHAIN_ID
uint256, // TAIKO_MAX_PROPOSED_BLOCKS
uint256, // TAIKO_MAX_FINALIZATIONS_PER_TX
uint256, // TAIKO_MAX_VERIFICATIONS_PER_TX
uint256, // TAIKO_COMMIT_DELAY_CONFIRMATIONS
uint256, // TAIKO_MAX_PROOFS_PER_FORK_CHOICE
uint256, // TAIKO_BLOCK_MAX_GAS_LIMIT
Expand All @@ -156,7 +156,7 @@ contract V1TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {
return (
LibConstants.TAIKO_CHAIN_ID,
LibConstants.TAIKO_MAX_PROPOSED_BLOCKS,
LibConstants.TAIKO_MAX_FINALIZATIONS_PER_TX,
LibConstants.TAIKO_MAX_VERIFICATIONS_PER_TX,
LibConstants.TAIKO_COMMIT_DELAY_CONFIRMATIONS,
LibConstants.TAIKO_MAX_PROOFS_PER_FORK_CHOICE,
LibConstants.TAIKO_BLOCK_MAX_GAS_LIMIT,
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/libs/LibConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ library LibConstants {
// https://github.com/ethereum-lists/chains/pull/1611
uint256 public constant TAIKO_CHAIN_ID = 167;
uint256 public constant TAIKO_MAX_PROPOSED_BLOCKS = 2048;
uint256 public constant TAIKO_MAX_FINALIZATIONS_PER_TX = 20;
uint256 public constant TAIKO_MAX_VERIFICATIONS_PER_TX = 20;
uint256 public constant TAIKO_COMMIT_DELAY_CONFIRMATIONS = 4;
uint256 public constant TAIKO_MAX_PROOFS_PER_FORK_CHOICE = 5;
uint256 public constant TAIKO_BLOCK_MAX_GAS_LIMIT = 5000000; // TODO
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/docs/L1/LibData.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ struct State {
mapping(uint256 => mapping(bytes32 => struct LibData.ForkChoice)) forkChoices;
mapping(bytes32 => uint256) commits;
uint64 genesisHeight;
uint64 latestFinalizedHeight;
uint64 latestFinalizedId;
uint64 latestVerifiedHeight;
uint64 latestVerifiedId;
uint64 nextBlockId;
}
```
Expand All @@ -74,7 +74,7 @@ function getL2BlockHash(struct LibData.State s, uint256 number) internal view re
### getStateVariables

```solidity
function getStateVariables(struct LibData.State s) internal view returns (uint64 genesisHeight, uint64 latestFinalizedHeight, uint64 latestFinalizedId, uint64 nextBlockId)
function getStateVariables(struct LibData.State s) internal view returns (uint64 genesisHeight, uint64 latestVerifiedHeight, uint64 latestVerifiedId, uint64 nextBlockId)
```

### hashMetadata
Expand Down
12 changes: 6 additions & 6 deletions packages/protocol/docs/L1/TaikoL1.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ merkel proof.
| blockIndex | uint256 | The index of the block to prove. This is also used to select the right implementation version. |
| inputs | bytes[] | A list of data input: - inputs[0] An Evidence object with various information regarding the block to be proven and the actual proofs. - inputs[1] The target block to be proven invalid. - inputs[2] The receipt for the `invalidBlock` transaction on L2. Note that the `invalidBlock` transaction is supposed to be the only transaction in the L2 block. |

### finalizeBlocks
### verifyBlocks

```solidity
function finalizeBlocks(uint256 maxBlocks) external
function verifyBlocks(uint256 maxBlocks) external
```

Finalize up to N blocks.
Verify up to N blocks.

#### Parameters

| Name | Type | Description |
| --------- | ------- | --------------------------------- |
| maxBlocks | uint256 | Max number of blocks to finalize. |
| Name | Type | Description |
| --------- | ------- | ------------------------------- |
| maxBlocks | uint256 | Max number of blocks to verify. |

### isCommitValid

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/docs/L1/v1/V1Events.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## V1Events

### BlockFinalized
### BlockVerified

```solidity
event BlockFinalized(uint256 id, bytes32 blockHash)
event BlockVerified(uint256 id, bytes32 blockHash)
```

### BlockCommitted
Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/docs/L1/v1/V1Finalizing.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## V1Finalizing

### BlockFinalized
### BlockVerified

```solidity
event BlockFinalized(uint256 id, bytes32 blockHash)
event BlockVerified(uint256 id, bytes32 blockHash)
```

### HeaderSynced
Expand All @@ -18,8 +18,8 @@ event HeaderSynced(uint256 height, uint256 srcHeight, bytes32 srcHash)
function init(struct LibData.State s, bytes32 _genesisBlockHash) public
```

### finalizeBlocks
### verifyBlocks

```solidity
function finalizeBlocks(struct LibData.State s, uint256 maxBlocks) public
function verifyBlocks(struct LibData.State s, uint256 maxBlocks) public
```
4 changes: 2 additions & 2 deletions packages/protocol/docs/libs/LibConstants.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ uint256 TAIKO_CHAIN_ID
uint256 TAIKO_MAX_PROPOSED_BLOCKS
```

### TAIKO_MAX_FINALIZATIONS_PER_TX
### TAIKO_MAX_VERIFICATIONS_PER_TX

```solidity
uint256 TAIKO_MAX_FINALIZATIONS_PER_TX
uint256 TAIKO_MAX_VERIFICATIONS_PER_TX
```

### TAIKO_COMMIT_DELAY_CONFIRMATIONS
Expand Down
Loading

0 comments on commit 5c4d370

Please sign in to comment.