Skip to content

Commit

Permalink
feat(contracts): commit DA for multiple blocks (scroll-tech#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Scroll Dev <[email protected]>
Co-authored-by: HAOYUatHZ <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2022
1 parent 0892813 commit a648073
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 191 deletions.
115 changes: 72 additions & 43 deletions contracts/docs/apis/ZKRollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,38 @@ Append a cross chain message to message queue.
|---|---|---|
| _0 | uint256 | undefined |

### batches

```solidity
function batches(bytes32) external view returns (bytes32 batchHash, bytes32 parentHash, uint64 batchIndex, bool verified)
```

Mapping from batch id to batch struct.



#### Parameters

| Name | Type | Description |
|---|---|---|
| _0 | bytes32 | undefined |

#### Returns

| Name | Type | Description |
|---|---|---|
| batchHash | bytes32 | undefined |
| parentHash | bytes32 | undefined |
| batchIndex | uint64 | undefined |
| verified | bool | undefined |

### blocks

```solidity
function blocks(bytes32) external view returns (struct IZKRollup.BlockHeader header, bytes32 transactionRoot, bool verified)
function blocks(bytes32) external view returns (bytes32 parentHash, bytes32 transactionRoot, uint64 blockHeight, uint64 batchIndex)
```

Mapping from block hash to block index.
Mapping from block hash to block struct.



Expand All @@ -58,14 +83,15 @@ Mapping from block hash to block index.

| Name | Type | Description |
|---|---|---|
| header | IZKRollup.BlockHeader | undefined |
| parentHash | bytes32 | undefined |
| transactionRoot | bytes32 | undefined |
| verified | bool | undefined |
| blockHeight | uint64 | undefined |
| batchIndex | uint64 | undefined |

### commitBlock
### commitBatch

```solidity
function commitBlock(IZKRollup.BlockHeader _header, IZKRollup.Layer2Transaction[] _txn) external nonpayable
function commitBatch(IZKRollup.Layer2Batch _batch) external nonpayable
```


Expand All @@ -76,34 +102,33 @@ function commitBlock(IZKRollup.BlockHeader _header, IZKRollup.Layer2Transaction[

| Name | Type | Description |
|---|---|---|
| _header | IZKRollup.BlockHeader | undefined |
| _txn | IZKRollup.Layer2Transaction[] | undefined |
| _batch | IZKRollup.Layer2Batch | undefined |

### finalizeBlockWithProof
### finalizeBatchWithProof

```solidity
function finalizeBlockWithProof(bytes32 _blockHash, uint256[] _proof, uint256[] _instances) external nonpayable
function finalizeBatchWithProof(bytes32 _batchId, uint256[] _proof, uint256[] _instances) external nonpayable
```

finalize commited block in layer 1
finalize commited batch in layer 1

*will add more parameters if needed.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| _blockHash | bytes32 | The block hash of the commited block. |
| _proof | uint256[] | The corresponding proof of the commited block. |
| _instances | uint256[] | Instance used to verify, generated from block. |
| _batchId | bytes32 | The identification of the commited batch. |
| _proof | uint256[] | The corresponding proof of the commited batch. |
| _instances | uint256[] | Instance used to verify, generated from batch. |

### finalizedBlocks
### finalizedBatches

```solidity
function finalizedBlocks(uint256) external view returns (bytes32)
function finalizedBatches(uint256) external view returns (bytes32)
```

Mapping from block height to finalized block hash.
Mapping from batch index to finalized batch id.



Expand Down Expand Up @@ -178,7 +203,7 @@ Return the total number of appended message.
### importGenesisBlock

```solidity
function importGenesisBlock(IZKRollup.BlockHeader _genesis) external nonpayable
function importGenesisBlock(IZKRollup.Layer2BlockHeader _genesis) external nonpayable
```


Expand All @@ -189,7 +214,7 @@ function importGenesisBlock(IZKRollup.BlockHeader _genesis) external nonpayable

| Name | Type | Description |
|---|---|---|
| _genesis | IZKRollup.BlockHeader | undefined |
| _genesis | IZKRollup.Layer2BlockHeader | undefined |

### initialize

Expand All @@ -207,13 +232,13 @@ function initialize(uint256 _chainId) external nonpayable
|---|---|---|
| _chainId | uint256 | undefined |

### lastFinalizedBlockHash
### lastFinalizedBatchID

```solidity
function lastFinalizedBlockHash() external view returns (bytes32)
function lastFinalizedBatchID() external view returns (bytes32)
```

The hash of the latest finalized block.
The latest finalized batch id.



Expand Down Expand Up @@ -325,21 +350,21 @@ function renounceOwnership() external nonpayable
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*


### revertBlock
### revertBatch

```solidity
function revertBlock(bytes32 _blockHash) external nonpayable
function revertBatch(bytes32 _batchId) external nonpayable
```

revert a pending block.
revert a pending batch.

*one can only revert unfinalized blocks.*
*one can only revert unfinalized batches.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| _blockHash | bytes32 | The block hash of the block. |
| _batchId | bytes32 | The identification of the batch. |

### transferOwnership

Expand Down Expand Up @@ -392,7 +417,7 @@ Update the address of operator.
### verifyMessageStateProof

```solidity
function verifyMessageStateProof(uint256 _blockNumber) external view returns (bool)
function verifyMessageStateProof(uint256 _batchIndex, uint256 _blockHeight) external view returns (bool)
```

Verify a state proof for message relay.
Expand All @@ -403,7 +428,8 @@ Verify a state proof for message relay.

| Name | Type | Description |
|---|---|---|
| _blockNumber | uint256 | undefined |
| _batchIndex | uint256 | undefined |
| _blockHeight | uint256 | undefined |

#### Returns

Expand All @@ -415,40 +441,43 @@ Verify a state proof for message relay.

## Events

### CommitBlock
### CommitBatch

```solidity
event CommitBlock(bytes32 indexed _blockHash, uint64 indexed _blockHeight, bytes32 _parentHash)
event CommitBatch(bytes32 indexed _batchId, bytes32 _batchHash, uint256 _batchIndex, bytes32 _parentHash)
```


Emitted when a new batch is commited.



#### Parameters

| Name | Type | Description |
|---|---|---|
| _blockHash `indexed` | bytes32 | undefined |
| _blockHeight `indexed` | uint64 | undefined |
| _batchId `indexed` | bytes32 | undefined |
| _batchHash | bytes32 | undefined |
| _batchIndex | uint256 | undefined |
| _parentHash | bytes32 | undefined |

### FinalizeBlock
### FinalizeBatch

```solidity
event FinalizeBlock(bytes32 indexed _blockHash, uint64 indexed _blockHeight)
event FinalizeBatch(bytes32 indexed _batchId, bytes32 _batchHash, uint256 _batchIndex, bytes32 _parentHash)
```


Emitted when a batch is finalized.



#### Parameters

| Name | Type | Description |
|---|---|---|
| _blockHash `indexed` | bytes32 | undefined |
| _blockHeight `indexed` | uint64 | undefined |
| _batchId `indexed` | bytes32 | undefined |
| _batchHash | bytes32 | undefined |
| _batchIndex | uint256 | undefined |
| _parentHash | bytes32 | undefined |

### OwnershipTransferred

Expand All @@ -467,21 +496,21 @@ event OwnershipTransferred(address indexed previousOwner, address indexed newOwn
| previousOwner `indexed` | address | undefined |
| newOwner `indexed` | address | undefined |

### RevertBlock
### RevertBatch

```solidity
event RevertBlock(bytes32 indexed _blockHash)
event RevertBatch(bytes32 indexed _batchId)
```


Emitted when a batch is reverted.



#### Parameters

| Name | Type | Description |
|---|---|---|
| _blockHash `indexed` | bytes32 | undefined |
| _batchId `indexed` | bytes32 | undefined |

### UpdateMesssenger

Expand Down
9 changes: 5 additions & 4 deletions contracts/integration-test/ERC20Gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe("ERC20Gateway", async () => {
gasUsed: 0,
timestamp: 0,
extraData: "0x",
txs: [],
});

// deploy L1ScrollMessenger in layer 1
Expand Down Expand Up @@ -425,7 +426,7 @@ describe("ERC20Gateway", async () => {
deadline,
nonce,
messageData,
{ blockNumber: 0, merkleProof: "0x" }
{ batchIndex: 0, blockHeight: 0, merkleProof: "0x" }
);
await relayTx.wait();
// should emit RelayedMessage
Expand Down Expand Up @@ -477,7 +478,7 @@ describe("ERC20Gateway", async () => {
deadline,
nonce,
messageData,
{ blockNumber: 0, merkleProof: "0x" }
{ batchIndex: 0, blockHeight: 0, merkleProof: "0x" }
);
await relayTx.wait();
// should emit RelayedMessage
Expand Down Expand Up @@ -744,7 +745,7 @@ describe("ERC20Gateway", async () => {
deadline,
nonce,
messageData,
{ blockNumber: 0, merkleProof: "0x" }
{ batchIndex: 0, blockHeight: 0, merkleProof: "0x" }
);
await relayTx.wait();
const afterBalanceLayer1 = await l1WETH.balanceOf(recipient.address);
Expand Down Expand Up @@ -801,7 +802,7 @@ describe("ERC20Gateway", async () => {
deadline,
nonce,
messageData,
{ blockNumber: 0, merkleProof: "0x" }
{ batchIndex: 0, blockHeight: 0, merkleProof: "0x" }
);
await relayTx.wait();
const afterBalanceLayer1 = await l1WETH.balanceOf(recipient.address);
Expand Down
3 changes: 2 additions & 1 deletion contracts/integration-test/GatewayRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe("GatewayRouter", async () => {
gasUsed: 0,
timestamp: 0,
extraData: "0x",
txs: []
});

// deploy L1ScrollMessenger in layer 1
Expand Down Expand Up @@ -195,7 +196,7 @@ describe("GatewayRouter", async () => {
deadline,
nonce,
messageData,
{ blockNumber: 0, merkleProof: "0x" }
{ batchIndex: 0, blockHeight: 0, merkleProof: "0x" }
);
await relayTx.wait();
const afterBalanceLayer1 = await ethers.provider.getBalance(recipient.address);
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/L1/IL1ScrollMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { IScrollMessenger } from "../libraries/IScrollMessenger.sol";
interface IL1ScrollMessenger is IScrollMessenger {
struct L2MessageProof {
// @todo add more fields
uint256 blockNumber;
uint256 batchIndex;
uint256 blockHeight;
bytes merkleProof;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/L1/L1ScrollMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ contract L1ScrollMessenger is OwnableUpgradeable, PausableUpgradeable, ScrollMes
require(!isMessageExecuted[_msghash], "Message successfully executed");

// @todo check proof
require(IZKRollup(rollup).verifyMessageStateProof(_proof.blockNumber), "invalid state proof");
require(IZKRollup(rollup).verifyMessageStateProof(_proof.batchIndex, _proof.blockHeight), "invalid state proof");
require(ZkTrieVerifier.verifyMerkleProof(_proof.merkleProof), "invalid proof");

// @todo check `_to` address to avoid attack.
Expand Down
Loading

0 comments on commit a648073

Please sign in to comment.