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

Remove bridge, delete unused tests, update chain ID type in prep for multi-ecosystem bridges #110

Merged
merged 15 commits into from
Jan 27, 2022
Merged
371 changes: 0 additions & 371 deletions contracts/Bridge.sol

This file was deleted.

37 changes: 7 additions & 30 deletions contracts/SignatureBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import "./utils/Pausable.sol";
import "./utils/SafeMath.sol";
import "./utils/SafeCast.sol";
import "./utils/Governable.sol";
import "./utils/ChainIdWithType.sol";
import "./interfaces/IExecutor.sol";
import "hardhat/console.sol";

/**
@title Facilitates deposits, creation and voting of deposit proposals, and deposit executions.
@author ChainSafe Systems & Webb Technologies.
*/
contract SignatureBridge is Pausable, SafeMath, Governable {
contract SignatureBridge is Pausable, SafeMath, Governable, ChainIdWithType {
// destinationChainID => number of deposits
mapping(uint256 => uint64) public _counts;
// resourceID => handler address
Expand All @@ -37,15 +38,14 @@ contract SignatureBridge is Pausable, SafeMath, Governable {
@notice Initializes SignatureBridge with a governor
@param initialGovernor Addresses that should be initially granted the relayer role.
*/
constructor (address initialGovernor) Governable(initialGovernor) {
}
constructor (address initialGovernor) Governable(initialGovernor) {}

/**
@notice Sets a new resource for handler contracts that use the IExecutor interface,
and maps the {handlerAddress} to {resourceID} in {_resourceIDToHandlerAddress}.
@notice Only callable by an address that currently has the admin role.
@param handlerAddress Address of handler resource will be set for.
@param resourceID ResourceID to be used when making deposits.
@param resourceID Secondary resourceID begin mapped to a handler address.
@param executionContextAddress Address of contract to be called when a proposal is ready to execute on it
*/
function adminSetResourceWithSignature(
Expand All @@ -59,23 +59,6 @@ contract SignatureBridge is Pausable, SafeMath, Governable {
handler.setResource(resourceID, executionContextAddress);
}

/**
@notice Migrates the handlers to a new bridge.
@notice Expects the new bridge to have the same resourceID to handler mapping as the old bridge.
@param resourceIDs ResourceID array to migrate all active handlers to the new bridge
@param newBridge New bridge address to migrate handlers to.
*/
function adminMigrateBridgeWithSignature(
bytes32[] calldata resourceIDs,
address newBridge,
bytes memory sig
) external signedByGovernor(keccak256(abi.encodePacked(resourceIDs, newBridge)), sig) {
for (uint i = 0; i < resourceIDs.length; i++) {
IExecutor handler = IExecutor(_resourceIDToHandlerAddress[resourceIDs[i]]);
handler.migrateBridge(newBridge);
}
}

/**
@notice Executes a proposal signed by the governor.
@param data Data meant for execution by execution handlers.
Expand All @@ -87,18 +70,12 @@ contract SignatureBridge is Pausable, SafeMath, Governable {
//Parse resourceID from the data
bytes calldata resourceIDBytes = data[0:32];
bytes32 resourceID = bytes32(resourceIDBytes);
// Parse chain ID from the resource ID
bytes4 executionChainID = bytes4(resourceIDBytes[28:32]);
// Parse chain ID + chain type from the resource ID
uint48 executionChainIdType = uint48(bytes6(resourceIDBytes[26:32]));
// Verify current chain matches chain ID from resource ID
require(uint32(getChainId()) == uint32(executionChainID), "executing on wrong chain");
require(uint256(getChainIdType()) == uint256(executionChainIdType), "executing on wrong chain");
address handler = _resourceIDToHandlerAddress[resourceID];
IExecutor executionHandler = IExecutor(handler);
executionHandler.executeProposal(resourceID, data);
}

function getChainId() public view returns (uint) {
uint chainId;
assembly { chainId := chainid() }
return chainId;
}
}
2 changes: 1 addition & 1 deletion contracts/anchors/FixedDepositAnchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ contract FixedDepositAnchor is AnchorBase, IFixedDepositAnchor {
uint256(encodeInputsData._fee),
uint256(encodeInputsData._refund),
uint256(encodeInputsData._refreshCommitment),
uint256(getChainId()),
uint256(getChainIdType()),
_roots
);

Expand Down
9 changes: 2 additions & 7 deletions contracts/anchors/LinkableTree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ pragma solidity ^0.8.0;

import "../trees/MerkleTreePoseidon.sol";
import "../interfaces/IVerifier.sol";
import "../utils/ChainIdWithType.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "hardhat/console.sol";

abstract contract LinkableTree is MerkleTreePoseidon, ReentrancyGuard {
abstract contract LinkableTree is MerkleTreePoseidon, ReentrancyGuard, ChainIdWithType {
address public handler;

uint8 public immutable maxEdges;
Expand Down Expand Up @@ -154,12 +155,6 @@ abstract contract LinkableTree is MerkleTreePoseidon, ReentrancyGuard {
_;
}

function getChainId() public view returns (uint) {
uint chainId;
assembly { chainId := chainid() }
return chainId;
}

function hasEdge(uint256 _chainID) external view returns (bool) {
return edgeExistsForChain[_chainID];
}
Expand Down
12 changes: 6 additions & 6 deletions contracts/handlers/AnchorHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ contract AnchorHandler is IExecutor, HandlerHelpers {
address newVerifier = address(bytes20(arguments[4:24]));
anchor.setVerifier(newVerifier, nonce);
} else if (functionSig == bytes4(keccak256("updateEdge(uint256,bytes32,uint256)"))) {
uint32 sourceChainId = uint32(bytes4(arguments[4:8]));
uint32 leafIndex = uint32(bytes4(arguments[8:12]));
bytes32 merkleRoot = bytes32(arguments[12:44]);
uint256 sourceChainId = uint48(bytes6(arguments[4:10]));
uint32 leafIndex = uint32(bytes4(arguments[10:14]));
bytes32 merkleRoot = bytes32(arguments[14:46]);
anchor.updateEdge(sourceChainId, merkleRoot, leafIndex);
} else if (functionSig == bytes4(keccak256("configureLimits(uint256,uint256)"))) {
uint256 _minimalWithdrawalAmount = uint256(bytes32(arguments[4:36]));
uint256 _maximumDepositAmount = uint256(bytes32(arguments[36:68]));
anchor.configureLimits(_minimalWithdrawalAmount, _maximumDepositAmount);
uint256 minimalWithdrawalAmount = uint256(bytes32(arguments[4:36]));
uint256 maximumDepositAmount = uint256(bytes32(arguments[36:68]));
anchor.configureLimits(minimalWithdrawalAmount, maximumDepositAmount);
} else {
revert("Invalid function sig");
}
Expand Down
19 changes: 17 additions & 2 deletions contracts/libs/VAnchorEncodeInputs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;

library VAnchorEncodeInputs {
bytes2 public constant EVM_CHAIN_ID_TYPE = 0x0100;

struct Proof {
bytes proof;
bytes roots;
Expand All @@ -19,11 +21,24 @@ library VAnchorEncodeInputs {
return chainId;
}

function getChainIdType() public view returns (uint48) {
// The chain ID and type pair is 6 bytes in length
// The first 2 bytes are reserved for the chain type.
// The last 4 bytes are reserved for a u32 (uint32) chain ID.
bytes4 chainID = bytes4(uint32(getChainId()));
bytes2 chainType = EVM_CHAIN_ID_TYPE;
// We encode the chain ID and type pair into packed bytes which
// should be 6 bytes using the encode packed method. We will
// cast this as a bytes32 in order to encode as a uint256 for zkp verification.
bytes memory chainIdWithType = abi.encodePacked(chainType, chainID);
return uint48(bytes6(chainIdWithType));
}

function _encodeInputs2(
Proof memory _args,
uint8 maxEdges
) public view returns (bytes memory, bytes32[] memory) {
uint256 _chainId = getChainId();
uint256 _chainId = getChainIdType();
bytes32[] memory result = new bytes32[](maxEdges + 1);
bytes memory encodedInput;

Expand Down Expand Up @@ -145,7 +160,7 @@ library VAnchorEncodeInputs {
Proof memory _args,
uint8 maxEdges
) public view returns (bytes memory, bytes32[] memory) {
uint256 _chainId = getChainId();
uint256 _chainId = getChainIdType();
bytes32[] memory result = new bytes32[](maxEdges + 1);
bytes memory encodedInput;

Expand Down
24 changes: 24 additions & 0 deletions contracts/utils/ChainIdWithType.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity ^0.8.5;

abstract contract ChainIdWithType {
bytes2 public constant EVM_CHAIN_ID_TYPE = 0x0100;

function getChainId() public view returns (uint) {
uint chainId;
assembly { chainId := chainid() }
return chainId;
}

function getChainIdType() public view returns (uint48) {
// The chain ID and type pair is 6 bytes in length
// The first 2 bytes are reserved for the chain type.
// The last 4 bytes are reserved for a u32 (uint32) chain ID.
bytes4 chainID = bytes4(uint32(getChainId()));
bytes2 chainType = EVM_CHAIN_ID_TYPE;
// We encode the chain ID and type pair into packed bytes which
// should be 6 bytes using the encode packed method. We will
// cast this as a bytes32 in order to encode as a uint256 for zkp verification.
bytes memory chainIdWithType = abi.encodePacked(chainType, chainID);
return uint48(bytes6(chainIdWithType));
}
}
34 changes: 34 additions & 0 deletions docs/AnchorBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

## Methods

### EVM_CHAIN_ID_TYPE

```solidity
function EVM_CHAIN_ID_TYPE() external view returns (bytes2)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | bytes2 | undefined

### FIELD_SIZE

```solidity
Expand Down Expand Up @@ -229,6 +246,23 @@ function getChainId() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined

### getChainIdType

```solidity
function getChainIdType() external view returns (uint48)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint48 | undefined

### getLastRoot

```solidity
Expand Down
34 changes: 34 additions & 0 deletions docs/FixedDepositAnchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

## Methods

### EVM_CHAIN_ID_TYPE

```solidity
function EVM_CHAIN_ID_TYPE() external view returns (bytes2)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | bytes2 | undefined

### FIELD_SIZE

```solidity
Expand Down Expand Up @@ -262,6 +279,23 @@ function getChainId() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined

### getChainIdType

```solidity
function getChainIdType() external view returns (uint48)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint48 | undefined

### getDenomination

```solidity
Expand Down
34 changes: 34 additions & 0 deletions docs/VAnchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

## Methods

### EVM_CHAIN_ID_TYPE

```solidity
function EVM_CHAIN_ID_TYPE() external view returns (bytes2)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | bytes2 | undefined

### FIELD_SIZE

```solidity
Expand Down Expand Up @@ -303,6 +320,23 @@ function getChainId() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined

### getChainIdType

```solidity
function getChainIdType() external view returns (uint48)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint48 | undefined

### getLastRoot

```solidity
Expand Down
34 changes: 34 additions & 0 deletions docs/VAnchorBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

## Methods

### EVM_CHAIN_ID_TYPE

```solidity
function EVM_CHAIN_ID_TYPE() external view returns (bytes2)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | bytes2 | undefined

### FIELD_SIZE

```solidity
Expand Down Expand Up @@ -303,6 +320,23 @@ function getChainId() external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined

### getChainIdType

```solidity
function getChainIdType() external view returns (uint48)
```






#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint48 | undefined

### getLastRoot

```solidity
Expand Down
Loading