Skip to content

Commit 1f61c69

Browse files
authored
Merge pull request #107 from poanetwork/develop
Merging the develop branch containing the ERC-TO-NATIVE mode to the master branch
2 parents 64dfc32 + d2d6903 commit 1f61c69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6918
-2535
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
deploy/node_modules
3+
.git
4+
.dockerignore
5+
deploy/.env
6+
docker-compose.yml
7+
Dockerfile
8+
*.log
9+
flats

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:8
2+
3+
WORKDIR /contracts
4+
5+
COPY package.json .
6+
COPY package-lock.json .
7+
RUN npm install
8+
9+
COPY ./deploy/package.json ./deploy/
10+
COPY ./deploy/package-lock.json ./deploy/
11+
RUN cd ./deploy; npm install; cd ..
12+
13+
COPY . .
14+
RUN npm run compile
15+
RUN bash flatten.sh
16+
17+
ENV PATH="/contracts/:${PATH}"

GAS_CONSUMPTION.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
## Gas Consumption
2+
3+
### `NATIVE-TO-ERC` Bridge Mode
4+
5+
#### Deployment
6+
##### Home
7+
Contract | Method | Min | Max | Avg
8+
---- | ---- | ---- | ---- | ----
9+
EternalStorageProxy|deployment|378510|378510|378510
10+
BridgeValidators|deployment|1144207|1144207|1144207
11+
EternalStorageProxy|upgradeTo|35871|30924|30913
12+
BridgeValidators|initialize|187738|280847|253949
13+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
14+
EternalStorageProxy|deployment|378510|378510|378510
15+
HomeBridgeNativeToErc|deployment|3327263|3327263|3327263
16+
EternalStorageProxy|upgradeTo|35871|30924|30913
17+
HomeBridgeNativeToErc|initialize|190051|190947|190755
18+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
19+
Total| |5739327|5823438|5796326
20+
21+
##### Foreign
22+
Contract | Method | Min | Max | Avg
23+
---- | ---- | ---- | ---- | ----
24+
ERC677BridgeToken|deployment|1498202|1499226|1498829
25+
EternalStorageProxy|deployment|378510|378510|378510
26+
BridgeValidators|deployment|1144207|1144207|1144207
27+
EternalStorageProxy|upgradeTo|35871|30924|30913
28+
BridgeValidators|initialize|187738|280847|253949
29+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
30+
EternalStorageProxy|deployment|378510|378510|378510
31+
ForeignBridgeNativeToErc|deployment|2768705|2768705|2768705
32+
EternalStorageProxy|upgradeTo|35871|30924|30913
33+
ForeignBridgeNativeToErc|initialize|213493|213557|213549
34+
ERC677BridgeToken|setBridgeContract|29432|44432|39432
35+
ERC677BridgeToken|transferOwnership|30860|30924|30913
36+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
37+
Total| |6762705|6862072|6829736
38+
39+
#### Usage
40+
41+
##### Validators
42+
43+
Contract | Method | Min | Max | Avg
44+
---- | ---- | ---- | ---- | ----
45+
To sign at the Home (each validator)|
46+
HomeBridgeNativeToErc|submitSignature|159362|275135|220127
47+
To relay signatures from the Home to the Foreign (one validator)|
48+
ForeignBridgeNativeToErc|executeSignatures|89201|146127|120917
49+
To sign and relay from the Foreign to the Home (each validator)|
50+
HomeBridgeNativeToErc|executeAffirmation|64314|107669|83596
51+
52+
##### Users
53+
54+
Contract | Method | Min | Max | Avg
55+
---- | ---- | ---- | ---- | ----
56+
To request transfer from the Home to the Foreign|
57+
HomeBridgeNativeToErc|fallback|46982|46982|46982
58+
To request transfer from the Foreign to the Home|
59+
ERC677BridgeToken|transferAndCall|58370|166206|92399
60+
61+
62+
### `ERC-TO-ERC` Bridge Mode
63+
64+
#### Deployment
65+
##### Home
66+
Contract | Method | Min | Max | Avg
67+
---- | ---- | ---- | ---- | ----
68+
EternalStorageProxy|deployment|378510|378510|378510
69+
BridgeValidators|deployment|1144207|1144207|1144207
70+
EternalStorageProxy|upgradeTo|35871|30924|30913
71+
BridgeValidators|initialize|187738|280847|253949
72+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
73+
EternalStorageProxy|deployment|378510|378510|378510
74+
HomeBridgeErcToErc|deployment|3528509|3528509|3528509
75+
EternalStorageProxy|upgradeTo|35871|30924|30913
76+
ERC677BridgeToken|deployment|1498202|1499226|1498829
77+
ERC677BridgeToken|setBridgeContract|29432|44432|39432
78+
ERC677BridgeToken|transferOwnership|30860|30924|30913
79+
HomeBridgeErcToErc|initialize|212299|213195|213003
80+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
81+
Total| |7521315|7621514|7588994
82+
83+
##### Foreign
84+
Contract | Method | Min | Max | Avg
85+
---- | ---- | ---- | ---- | ----
86+
EternalStorageProxy|deployment|378510|378510|378510
87+
BridgeValidators|deployment|1144207|1144207|1144207
88+
EternalStorageProxy|upgradeTo|35871|30924|30913
89+
BridgeValidators|initialize|187738|280847|253949
90+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
91+
EternalStorageProxy|deployment|378510|378510|378510
92+
ForeignBridgeErcToErc|deployment|2449436|2449436|2449436
93+
EternalStorageProxy|upgradeTo|35871|30924|30913
94+
ForeignBridgeErcToErc|initialize|150614|150614|150614
95+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
96+
Total| |4822063|4905278|4878358
97+
98+
#### Usage
99+
100+
##### Validators
101+
102+
Contract | Method | Min | Max | Avg
103+
---- | ---- | ---- | ---- | ----
104+
To sign at the Home (each validator)|
105+
HomeBridgeErcToErc|submitSignature|159386|275159|220171
106+
To relay signatures from the Home to the Foreign (one validator)|
107+
ForeignBridgeErcToErc|executeSignatures|73779|115769|93027
108+
To sign and relay from the Foreign to the Home (each validator)|
109+
HomeBridgeErcToErc|executeAffirmation|79336|134607|108215
110+
111+
##### Users
112+
113+
Contract | Method | Min | Max | Avg
114+
---- | ---- | ---- | ---- | ----
115+
To request transfer from the Home to the Foreign|
116+
ERC677BridgeToken|transferAndCall|58370|166206|92399
117+
To request transfer from the Foreign to the Home|
118+
ERC677BridgeToken|transfer|37691|86589|55000
119+
120+
121+
### `ERC-TO-NATIVE` Bridge Mode
122+
123+
#### Deployment
124+
##### Home
125+
Contract | Method | Min | Max | Avg
126+
---- | ---- | ---- | ---- | ----
127+
EternalStorageProxy|deployment|378510|378510|378510
128+
BridgeValidators|deployment|1144207|1144207|1144207
129+
EternalStorageProxy|upgradeTo|35871|30924|30913
130+
BridgeValidators|initialize|187738|280847|253949
131+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
132+
EternalStorageProxy|deployment|378510|378510|378510
133+
HomeBridgeErcToNative|deployment|3757420|3757420|3757420
134+
EternalStorageProxy|upgradeTo|35871|30924|30913
135+
HomeBridgeErcToNative|initialize|196910|213930|210795
136+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
137+
Total| |6176343|6276578|6246523
138+
139+
##### Foreign
140+
Contract | Method | Min | Max | Avg
141+
---- | ---- | ---- | ---- | ----
142+
EternalStorageProxy|deployment|378510|378510|378510
143+
BridgeValidators|deployment|1144207|1144207|1144207
144+
EternalStorageProxy|upgradeTo|35871|30924|30913
145+
BridgeValidators|initialize|187738|280847|253949
146+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
147+
EternalStorageProxy|deployment|378510|378510|378510
148+
ForeignBridgeErcToNative|deployment|2449564|2449564|2449564
149+
EternalStorageProxy|upgradeTo|35871|30924|30913
150+
ForeignBridgeErcToNative|initialize|150614|150614|150614
151+
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
152+
Total| |
153+
154+
#### Usage
155+
156+
##### Validators
157+
158+
Contract | Method | Min | Max | Avg
159+
---- | ---- | ---- | ---- | ----
160+
To sign at the Home (each validator)|
161+
HomeBridgeErcToNative|submitSignature|159428|275201|220206
162+
To relay signatures from the Home to the Foreign (one validator)|
163+
ForeignBridgeErcToNative|executeSignatures|73779|115769|92985
164+
To sign and relay from the Foreign to the Home (each validator)|
165+
HomeBridgeErcToNative|executeAffirmation|64380|140744|97562
166+
167+
##### Users
168+
169+
Contract | Method | Min | Max | Avg
170+
---- | ---- | ---- | ---- | ----
171+
To request transfer from the Home to the Foreign|
172+
HomeBridgeErcToNative|fallback|80174|80174|80174
173+
To request transfer from the Foreign to the Home|
174+
ERC677BridgeToken|transfer|37691|86589|55000

README.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The POA Bridge allows users to transfer assets between two chains in the Ethereu
2525
Currently, the contracts support two types of relay operations:
2626
* Tokenize the native coin in one blockchain network (Home) into an ERC20 token in another network (Foreign).
2727
* Swap a token presented by an existing ERC20 contract in a Foreign network into an ERC20 token in the Home network, where one pair of bridge contracts corresponds to one pair of ERC20 tokens.
28+
* to mint new native coins in Home blockchain network from a token presented by an existing ERC20 contract in a Foreign network.
2829

2930

3031
### Components
@@ -35,6 +36,7 @@ The POA bridge contracts consist of several components:
3536
* Depending on the type of relay operations the following components are also used:
3637
* in `NATIVE-TO-ERC` mode: the ERC20 token (in fact, the ERC677 extension is used) is deployed on the Foreign network;
3738
* in `ERC-TO-ERC` mode: the ERC20 token (in fact, the ERC677 extension is used) is deployed on the Home network;
39+
* in `ERC-TO-NATIVE` mode: The home network nodes must support consensus engine that allows using a smart contract for block reward calculation;
3840
* The **Validators** smart contract is deployed in both the POA.Network and the Ethereum Mainnet.
3941

4042
### Bridge Roles and Responsibilities
@@ -55,26 +57,89 @@ Responsibilities and roles of the bridge:
5557
- **User** role:
5658
- sends assets to Bridge contracts:
5759
- in `NATIVE-TO-ERC` mode: send native coins to the Home Bridge to receive ERC20 tokens from the Foreign Bridge, send ERC20 tokens to the Foreign Bridge to unlock native coins from the Home Bridge;
58-
- in `ERC-TO-ERC` mode: transfer ERC20 tokens to the Foreign Bridge to mint ERC20 tokens on the Home Network, transfer ERC20 tokens to the Home Bridge to unlock ERC20 tokens on Foreign networks.
60+
- in `ERC-TO-ERC` mode: transfer ERC20 tokens to the Foreign Bridge to mint ERC20 tokens on the Home Network, transfer ERC20 tokens to the Home Bridge to unlock ERC20 tokens on Foreign networks;
61+
- in `ERC-TO-NATIVE` mode: send ERC20 tokens to the Foreign Bridge to receive native coins from the Home Bridge, send native coins to the Home Bridge to unlock ERC20 tokens from the Foreign Bridge.
5962

6063
## Usage
6164

62-
### Install Dependencies
65+
There are two ways to deploy contracts:
66+
* install and use NodeJS
67+
* use Docker to deploy
68+
69+
### Deployment with NodeJS
70+
71+
#### Install Dependencies
6372
```bash
6473
npm install
6574
```
66-
### Deploy
75+
#### Deploy
6776
Please the [README.md](deploy/README.md) in the `deploy` folder for instructions and .env file configuration
6877

69-
### Test
78+
#### Test
7079
```bash
7180
npm test
7281
```
7382

74-
### Flatten
83+
#### Flatten
84+
Fattened contracts can be used to verify the contract code in a block explorer like BlockScout or Etherscan.
85+
The following command will prepare flattened version of the contracts:
86+
7587
```bash
7688
npm run flatten
7789
```
90+
The flattened contracts can be found in the `flats` directory.
91+
92+
### Deployment in the Docker environment
93+
[Docker](https://www.docker.com/community-edition) and [Docker Compose](https://docs.docker.com/compose/install/) can be used to deploy contracts without NodeJS installed on the system.
94+
If you are on Linux, we recommend you [create a docker group and add your user to it](https://docs.docker.com/install/linux/linux-postinstall/), so that you can use the CLI without `sudo`.
95+
96+
#### Prepare the docker container
97+
```bash
98+
docker-compose up --build
99+
```
100+
_Note: The container must be rebuilt every time the code in a contract or deployment script is changed._
101+
102+
#### Deploy the contracts
103+
1. Create the `.env` file in the `deploy` directory as described in the deployment [README.md](deploy/README.md).
104+
2. Run deployment process:
105+
```bash
106+
docker-compose run bridge-contracts deploy.sh
107+
```
108+
or with Linux:
109+
```bash
110+
./deploy.sh
111+
```
112+
113+
#### Copy flatten sources (if needed)
114+
1. Discover the container name:
115+
```bash
116+
docker-compose images bridge-contracts
117+
```
118+
2. In the following command, use the container name to copy the flattened contracts code to the current working directory. The contracts will be located in the `flats` directory.
119+
```bash
120+
docker cp name-of-your-container:/contracts/flats ./
121+
```
122+
123+
#### Shutdown the container
124+
If the container is no longer needed, it can be shutdown:
125+
```bash
126+
docker-compose down
127+
```
128+
129+
### Gas Consumption
130+
The [GAS_CONSUMPTION](GAS_CONSUMPTION.md) file includes Min, Max, and Avg gas consumption figures for contracts associated with each bridge mode.
131+
132+
### Testing environment
133+
To test the bridge scripts in ERC20-to-ERC20 mode on a testnet like Sokol or Kovan, you must deploy an ERC20 token to the foreign network.
134+
This can be done by running the following command:
135+
```bash
136+
cd deploy
137+
node testenv-deploy.js token
138+
```
139+
or with Docker:
140+
```bash
141+
./deploy.sh token
142+
```
78143

79144
## Contributing
80145

contracts/ERC677BridgeToken.sol

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "openzeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
55
import "openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
66
import "./IBurnableMintableERC677Token.sol";
77
import "./ERC677Receiver.sol";
8-
import "./libraries/Version.sol";
98

109

1110
contract ERC677BridgeToken is
@@ -14,14 +13,21 @@ contract ERC677BridgeToken is
1413
BurnableToken,
1514
MintableToken {
1615

17-
Version.Version public getTokenInterfacesVersion = Version.Version(2, 0, 0);
16+
address public bridgeContract;
17+
18+
event ContractFallbackCallFailed(address from, address to, uint value);
1819

1920
constructor(
2021
string _name,
2122
string _symbol,
2223
uint8 _decimals)
2324
public DetailedERC20(_name, _symbol, _decimals) {}
2425

26+
function setBridgeContract(address _bridgeContract) onlyOwner public {
27+
require(_bridgeContract != address(0) && isContract(_bridgeContract));
28+
bridgeContract = _bridgeContract;
29+
}
30+
2531
modifier validRecipient(address _recipient) {
2632
require(_recipient != address(0) && _recipient != address(this));
2733
_;
@@ -39,6 +45,10 @@ contract ERC677BridgeToken is
3945
return true;
4046
}
4147

48+
function getTokenInterfacesVersion() public pure returns(uint64 major, uint64 minor, uint64 patch) {
49+
return (2, 0, 0);
50+
}
51+
4252
function superTransfer(address _to, uint256 _value) internal returns(bool)
4353
{
4454
return super.transfer(_to, _value);
@@ -47,8 +57,12 @@ contract ERC677BridgeToken is
4757
function transfer(address _to, uint256 _value) public returns (bool)
4858
{
4959
require(superTransfer(_to, _value));
50-
if (isContract(_to)) {
51-
contractFallback(_to, _value, new bytes(0));
60+
if (isContract(_to) && !contractFallback(_to, _value, new bytes(0))) {
61+
if (_to == bridgeContract) {
62+
revert();
63+
} else {
64+
emit ContractFallbackCallFailed(msg.sender, _to, _value);
65+
}
5266
}
5367
return true;
5468
}

contracts/IBlockReward.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma solidity 0.4.24;
2+
3+
4+
interface IBlockReward {
5+
function addExtraReceiver(uint256 _amount, address _receiver) external;
6+
function mintedTotally() public view returns (uint256);
7+
function mintedTotallyByBridge(address _bridge) public view returns(uint256);
8+
function bridgesAllowedLength() external view returns(uint256);
9+
}

contracts/libraries/Version.sol

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)