Skip to content

Commit

Permalink
(feat) Reimport lock proxy and ccm with byte -> bytes1; this makes un…
Browse files Browse the repository at this point in the history
…pickling work again.

(feat) Now have enough mechanism that we can register an extension (forcibly)
(feat) more docs
(feat) Start of the bridge contract for tokens.
  • Loading branch information
rrw-zilliqa committed Jul 9, 2024
1 parent 71d6c50 commit ed06f0a
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 167 deletions.
36 changes: 20 additions & 16 deletions docs/zilbridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@

Check notice on line 3 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD012)

[new] Multiple consecutive blank lines
## CrossChainManager extensions

The CCM contains state; the current approach to retaining this state
is to replace the ccm in the ccmProxy with a shim contract that
forwards requests to the underlying contract but additionally allows
an owner to register extensions.

This works, and has the advantage that you don't need to change the
address of the ccm baked into the relayers, but it breaks the
invariant that cross-chain events come from the contract referred to
by the ccmProxy. If there is software that reads the address of the
CCM from the ccmProxy and then expects cross-chain events to come from
it, we will need to replace the ccm entirely.

This is not as straightforward as it looks because the deployed CCM on
ethereum is quite old and we would need to test that it still works
with zilBridge 1 when upgraded or updated for a reasonably modern
solidity version.
The currently deployed CCM on Ethereum does not contain functions to register lockProxy extensions.

Check notice on line 6 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length

These can be run remotely from the `counterpartChainId` (in `lockProxy`), currently set to 5 (which is presumably Carbon).

Check notice on line 8 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length

This means we need to either proxy or replace it.

Replacing it is undesirable, because the address of the CCM is baked into the configuration files for the relayers.

Check notice on line 12 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length

My first attempt was to proxy it with a `CCMExtendProxy`, but this doesn't work, because:

Check notice on line 14 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length

* To upgrade you have to call `ccmProxy::upgradeEthCrossChainManager()`

Check notice on line 16 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD007)

[new] Unordered list indentation
* (side-note: this calls the _current_ `eccm.upgradeToNew()` which hands ownership of the CCM data (proxied by the CCM contract) to the CCMExtendProxty, which now needs to hand it back to the old `ccm`)

Check notice on line 17 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD007)

[new] Unordered list indentation

Check notice on line 17 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length
* Subsequent calls through the `CCMExtendProxy` need to use the original `ccm` state, and therefore have the `CCMExtendProxy` as `msg.sender`.

Check notice on line 18 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD007)

[new] Unordered list indentation

Check notice on line 18 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD013)

[new] Line length
* But there is no way to make the `CCMExtendProxy` an owner of the `ccm`.

Check notice on line 19 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD007)

[new] Unordered list indentation

The second attempt is to write a new CCM contract which duplicates the
original CCM and contains the new functions. Sadly, this means that
someone needs to remember what the whitelist parameters were, because
it is a map that does not emit events and we thus can't work out what
is in it.


Check notice on line 27 in docs/zilbridge.md

View workflow job for this annotation

GitHub Actions / Trunk Check

markdownlint(MD012)

[new] Multiple consecutive blank lines
36 changes: 18 additions & 18 deletions smart-contracts/contracts/zilbridge/1/ccmCrossChainManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
*Submitted for verification at Etherscan.io on 2021-10-19
*/

pragma solidity 0.8.20;
//pragma solidity ^0.5.0;
//pragma experimental ABIEncoderV2;
pragma solidity ^0.8.2;

abstract contract Context {
// Empty internal constructor, to prevent people from mistakenly deploying
// an instance of this contract, which should be used via inheritance.
constructor () { }
// solhint-disable-previous-line no-empty-blocks

function _msgSender() internal view returns (address payable) {
Expand Down Expand Up @@ -181,7 +182,7 @@ library ZeroCopySink {
* @param b The byte value
* @return Converted bytes array
*/
function WriteByte(uint8 b) internal pure returns (bytes memory) {
function WriteByte(bytes1 b) internal pure returns (bytes memory) {
return WriteUint8(uint8(b));
}

Expand Down Expand Up @@ -332,7 +333,7 @@ library ZeroCopySource {
function NextBool(bytes memory buff, uint256 offset) internal pure returns(bool, uint256) {
require(offset + 1 <= buff.length && offset < offset + 1, "Offset exceeds limit");
// byte === bytes1
uint8 v;
bytes1 v;
assembly{
v := mload(add(add(buff, 0x20), offset))
}
Expand All @@ -352,9 +353,9 @@ library ZeroCopySource {
* @param offset The position from where we read the byte value
* @return The read byte value and new offset
*/
function NextByte(bytes memory buff, uint256 offset) internal pure returns (uint8, uint256) {
function NextByte(bytes memory buff, uint256 offset) internal pure returns (bytes1, uint256) {
require(offset + 1 <= buff.length && offset < offset + 1, "NextByte, Offset exceeds maximum");
uint8 v;
bytes1 v;
assembly{
v := mload(add(add(buff, 0x20), offset))
}
Expand Down Expand Up @@ -573,7 +574,7 @@ library ZeroCopySource {
}

function NextVarUint(bytes memory buff, uint256 offset) internal pure returns(uint, uint256) {
uint8 v;
bytes1 v;
(v, offset) = NextByte(buff, offset);

uint value;
Expand Down Expand Up @@ -828,7 +829,7 @@ library Utils {
* @return Hashed value in bytes32 format
*/
function hashLeaf(bytes memory _data) internal pure returns (bytes32 result) {
result = sha256(abi.encodePacked(uint8(0x0), _data));
result = sha256(abi.encodePacked(bytes1(0x0), _data));
}

/* @notice Do hash children as the multi-chain does
Expand Down Expand Up @@ -1101,7 +1102,7 @@ library ECCUtils {
bytes32 hash = Utils.hashLeaf(value);
uint size = _auditPath.length.sub(off).div(33);
bytes32 nodeHash;
uint8 pos;
bytes1 pos;
for (uint i = 0; i < size; i++) {
(pos, off) = ZeroCopySource.NextByte(_auditPath, off);
(nodeHash, off) = ZeroCopySource.NextHash(_auditPath, off);
Expand Down Expand Up @@ -1302,26 +1303,25 @@ interface IEthCrossChainData {
}

interface IUpgradableECCM is IOwnable, IPausable {
function upgradeToNew(address) external returns (bool);
function setChainId(uint64 _newChainId) external returns (bool);
function pause() external returns (bool);
function unpause() external returns (bool);
function upgradeToNew(address) external returns (bool);
function setChainId(uint64 _newChainId) external returns (bool);
}


interface IEthCrossChainManager {
function crossChain(uint64 _toChainId, bytes calldata _toContract, bytes calldata _method, bytes calldata _txData) external returns (bool);
}

contract UpgradableECCM is Ownable, Pausable, IUpgradableECCM {
contract UpgradableECCM is IUpgradableECCM, Ownable, Pausable {
address public EthCrossChainDataAddress;
uint64 public chainId;
uint64 public chainId;

constructor (address ethCrossChainDataAddr, uint64 _chainId) Pausable() Ownable() {
EthCrossChainDataAddress = ethCrossChainDataAddr;
chainId = _chainId;
}

function pause() onlyOwner public returns (bool) {
if (!paused()) {
_pause();
Expand All @@ -1332,7 +1332,7 @@ contract UpgradableECCM is Ownable, Pausable, IUpgradableECCM {
}
return true;
}

function unpause() onlyOwner public returns (bool) {
if (paused()) {
_unpause();
Expand All @@ -1359,7 +1359,7 @@ contract UpgradableECCM is Ownable, Pausable, IUpgradableECCM {

contract EthCrossChainManager is IEthCrossChainManager, UpgradableECCM {
using SafeMath for uint256;

address public whiteLister;
mapping(address => bool) public whiteListFromContract;
mapping(address => mapping(bytes => bool)) public whiteListContractMethodMap;
Expand All @@ -1373,7 +1373,7 @@ contract EthCrossChainManager is IEthCrossChainManager, UpgradableECCM {
uint64 _chainId,
address[] memory fromContractWhiteList,
bytes[] memory contractMethodWhiteList
) UpgradableECCM(_eccd,_chainId) {
) UpgradableECCM(_eccd,_chainId) {
whiteLister = msg.sender;
for (uint i=0;i<fromContractWhiteList.length;i++) {
whiteListFromContract[fromContractWhiteList[i]] = true;
Expand Down
25 changes: 7 additions & 18 deletions smart-contracts/contracts/zilbridge/1/lockProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

// SPDX-License-Identifier: MIT

//pragma solidity 0.6.12;
pragma solidity 0.8.20;

/**
Expand All @@ -32,7 +31,7 @@ library ZeroCopySource {
function NextBool(bytes memory buff, uint256 offset) internal pure returns(bool, uint256) {
require(offset + 1 <= buff.length && offset < offset + 1, "Offset exceeds limit");
// byte === bytes1
uint8 v;
bytes1 v;
assembly{
v := mload(add(add(buff, 0x20), offset))
}
Expand All @@ -52,9 +51,9 @@ library ZeroCopySource {
* @param offset The position from where we read the byte value
* @return The read byte value and new offset
*/
function NextByte(bytes memory buff, uint256 offset) internal pure returns (uint8, uint256) {
function NextByte(bytes memory buff, uint256 offset) internal pure returns (bytes1, uint256) {
require(offset + 1 <= buff.length && offset < offset + 1, "NextByte, Offset exceeds maximum");
uint8 v;
bytes1 v;
assembly{
v := mload(add(add(buff, 0x20), offset))
}
Expand Down Expand Up @@ -273,7 +272,7 @@ library ZeroCopySource {
}

function NextVarUint(bytes memory buff, uint256 offset) internal pure returns(uint, uint256) {
uint8 v;
bytes1 v;
(v, offset) = NextByte(buff, offset);

uint value;
Expand Down Expand Up @@ -304,7 +303,6 @@ library ZeroCopySource {
// File: contracts/libs/common/ZeroCopySink.sol



/**
* @dev Wrappers over encoding and serialization operation into bytes from bassic types in Solidity for PolyNetwork cross chain utility.
*
Expand Down Expand Up @@ -347,7 +345,7 @@ library ZeroCopySink {
* @param b The byte value
* @return Converted bytes array
*/
function WriteByte(uint8 b) internal pure returns (bytes memory) {
function WriteByte(bytes1 b) internal pure returns (bytes memory) {
return WriteUint8(uint8(b));
}

Expand Down Expand Up @@ -492,7 +490,6 @@ library ZeroCopySink {
// File: contracts/libs/utils/ReentrancyGuard.sol



/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
Expand Down Expand Up @@ -547,7 +544,6 @@ abstract contract ReentrancyGuard {
// File: contracts/libs/utils/Utils.sol



library Utils {

/* @notice Convert the bytes array to bytes32 type, the bytes array length must be 32
Expand Down Expand Up @@ -632,7 +628,7 @@ library Utils {
* @return Hashed value in bytes32 format
*/
function hashLeaf(bytes memory _data) internal pure returns (bytes32 result) {
result = sha256(abi.encodePacked(uint8(0x0), _data));
result = sha256(abi.encodePacked(bytes1(0x0), _data));
}

/* @notice Do hash children as the multi-chain does
Expand Down Expand Up @@ -1130,13 +1126,6 @@ contract Wallet {
// File: contracts/LockProxy.sol









interface CCM {
function crossChain(uint64 _toChainId, bytes calldata _toContract, bytes calldata _method, bytes calldata _txData) external returns (bool);
}
Expand Down Expand Up @@ -1212,7 +1201,7 @@ contract LockProxy is ReentrancyGuard {
bytes txArgs
);

constructor(address _ccmProxyAddress, uint64 _counterpartChainId) {
constructor(address _ccmProxyAddress, uint64 _counterpartChainId) public {
require(_counterpartChainId > 0, "counterpartChainId cannot be zero");
require(_ccmProxyAddress != address(0), "ccmProxyAddress cannot be empty");
counterpartChainId = _counterpartChainId;
Expand Down
77 changes: 0 additions & 77 deletions smart-contracts/contracts/zilbridge/2/CallingProxy.sol

This file was deleted.

Loading

0 comments on commit ed06f0a

Please sign in to comment.