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

Add block hash system contract #229

Merged
merged 10 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "module-system/module-implementations/sov-evm/src/evm/system_contracts/lib/forge-std"]
path = module-system/module-implementations/sov-evm/src/evm/system_contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
eyusufatik marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

abstract contract Ownable {
address public owner;
address public pendingOwner;

event OwnershipTransferred(address previousOwner, address newOwner);
event OwnershipTransferRequested(address previousOwner, address newOwner);

modifier onlyOwner() {
require(msg.sender == owner, "Caller is not owner");
_;
}

modifier onlyPendingOwner() {
require(msg.sender == pendingOwner, "Caller is not pending owner");
_;
}

constructor() {
owner = msg.sender;
}

function renounceOwnership() public onlyOwner {
owner = address(0);
emit OwnershipTransferred(owner, address(0));
}

function transferOwnership(address newOwner) public onlyOwner {
pendingOwner = newOwner;
emit OwnershipTransferRequested(owner, newOwner);
}

function acceptOwnership() public onlyPendingOwner {
address old_owner = owner;
owner = pendingOwner;
pendingOwner = address(0);
emit OwnershipTransferred(old_owner, pendingOwner);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "../lib/Ownable.sol";
import "./interfaces/IL1BlockHashList.sol";

contract L1BlockHashList is Ownable, IL1BlockHashList {
mapping(uint256 => bytes32) public blockHashes;
mapping(bytes32 => bytes32) public merkleRoots;
uint256 public blockNumber;

event BlockInfoAdded(uint256 blockNumber, bytes32 blockHash, bytes32 merkleRoot);
constructor() Ownable(){ }

function setBlockInfo(bytes32 _blockHash, bytes32 _merkleRoot) public onlyOwner {
blockHashes[blockNumber++] = _blockHash;
merkleRoots[_blockHash] = _merkleRoot;
emit BlockInfoAdded(blockNumber, _blockHash, _merkleRoot);
eyusufatik marked this conversation as resolved.
Show resolved Hide resolved
}

function getBlockHash(uint256 _blockNumber) public view returns (bytes32) {
return blockHashes[_blockNumber];
}

function getMerkleRootFromBlockHash(bytes32 _blockHash) public view returns (bytes32) {
return merkleRoots[_blockHash];
}

function getMerkleRootFromBlockNumber(uint256 _blockNumber) public view returns (bytes32) {
return merkleRoots[blockHashes[_blockNumber]];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

interface IL1BlockHashList {
function setBlockInfo(bytes32, bytes32) external;
function getBlockHash(uint256) external view returns (bytes32);
function getMerkleRootFromBlockHash(bytes32) external view returns (bytes32);
function getMerkleRootFromBlockNumber(uint256) external view returns (bytes32);
eyusufatik marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/L1BlockHashList.sol";

contract L1BlockHashListTest is Test {
L1BlockHashList l1BlockHashList;
bytes32 randomBlockHash = bytes32(keccak256("CITREA_TEST"));
bytes32 randomMerkleRoot = bytes32(keccak256("CITREA"));

function setUp() public {
l1BlockHashList = new L1BlockHashList();
}

function testSetBlockInfo() public {
l1BlockHashList.setBlockInfo(randomBlockHash, randomMerkleRoot);
assertEq(l1BlockHashList.getBlockHash(0), randomBlockHash);
assertEq(l1BlockHashList.getMerkleRootFromBlockHash(randomBlockHash), randomMerkleRoot);
assertEq(l1BlockHashList.getMerkleRootFromBlockNumber(0), randomMerkleRoot);
}

function testNonOwnerCannotSetBlockInfo() public {
vm.startPrank(address(0x1));
vm.expectRevert("Caller is not owner");
l1BlockHashList.setBlockInfo(randomBlockHash, randomMerkleRoot);
}

function testBlockInfoAvailableAfterManyWrites() public {
for (uint256 i = 0; i < 100; i++) {
bytes32 blockHash = keccak256(abi.encodePacked(i));
bytes32 root = keccak256(abi.encodePacked(blockHash));
l1BlockHashList.setBlockInfo(blockHash, root);
assertEq(l1BlockHashList.getBlockHash(i), blockHash);
assertEq(l1BlockHashList.getMerkleRootFromBlockHash(blockHash), root);
assertEq(l1BlockHashList.getMerkleRootFromBlockNumber(i), root);
}

bytes32 zeroth_hash = keccak256(abi.encodePacked(uint(0)));
bytes32 zeroth_root = keccak256(abi.encodePacked(zeroth_hash));
assertEq(l1BlockHashList.getBlockHash(0), zeroth_hash);
assertEq(l1BlockHashList.getMerkleRootFromBlockHash(zeroth_hash), zeroth_root);
assertEq(l1BlockHashList.getMerkleRootFromBlockNumber(0), zeroth_root);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "../lib/Ownable.sol";
import "forge-std/Test.sol";

contract OwnableHarness is Ownable {
constructor () Ownable() { }

function privilegedFunction() public onlyOwner {
}
}

contract OwnableTest is Test {
OwnableHarness ownable;

function setUp() public {
ownable = new OwnableHarness();
}

function testOnlyOwner() public {
ownable.privilegedFunction();
address non_owner = address(0x1);
vm.startPrank(non_owner);
vm.expectRevert("Caller is not owner");
ownable.privilegedFunction();
}

function testTransferOwnership() public {
ownable.transferOwnership(address(0x1));
assertEq(ownable.pendingOwner(), address(0x1));
}

function testAcceptOwnership() public {
address new_owner = address(0x1);
ownable.transferOwnership(new_owner);
vm.startPrank(new_owner);
ownable.acceptOwnership();
assertEq(ownable.owner(), new_owner);
}

function testRenounceOwnership() public {
ownable.renounceOwnership();
assertEq(ownable.owner(), address(0));
}
}
Loading