Skip to content

Commit

Permalink
Merge branch 'develop' into arbitrary-message-bridging-#73
Browse files Browse the repository at this point in the history
# Conflicts:
#	contracts/upgradeable_contracts/erc20_to_erc20/HomeBridgeErcToErc.sol
#	contracts/upgradeable_contracts/erc20_to_native/HomeBridgeErcToNative.sol
#	contracts/upgradeable_contracts/native_to_erc20/ForeignBridgeNativeToErc.sol
#	contracts/upgradeable_contracts/native_to_erc20/HomeBridgeNativeToErc.sol
#	deploy/src/loadEnv.js
#	flatten.sh
  • Loading branch information
patitonar committed May 22, 2019
2 parents 799d711 + dcef430 commit 6dfb7a9
Show file tree
Hide file tree
Showing 83 changed files with 12,977 additions and 4,072 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
deploy
27 changes: 27 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": [
"plugin:node/recommended",
"airbnb-base",
"plugin:prettier/recommended"
],
"plugins": ["node"],
"env": {
"node" : true,
"mocha" : true
},
"globals" : {
"artifacts": false,
"contract": false,
"assert": false,
"web3": false
},
"rules": {
"no-plusplus": "off",
"no-await-in-loop": "off",
"no-shadow": "off",
"prefer-destructuring": "off",
"no-use-before-define": ["error", { "functions": false }],
"no-restricted-syntax": "off",
"node/no-unpublished-require": "off"
}
}
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120
}
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sudo: required
group: beta
language: node_js
node_js:
- "9"
- "10"
cache:
yarn: true
env:
Expand All @@ -14,4 +14,5 @@ matrix:
allow_failures:
- env: SOLIDITY_COVERAGE=true
script:
- yarn lint:js
- yarn test
179 changes: 6 additions & 173 deletions GAS_CONSUMPTION.md
Original file line number Diff line number Diff line change
@@ -1,174 +1,7 @@
## Gas Consumption
## Gas Consumption by Bridge Mode

### `NATIVE-TO-ERC` Bridge Mode

#### Deployment
##### Home
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
HomeBridgeNativeToErc|deployment|3327263|3327263|3327263
EternalStorageProxy|upgradeTo|35871|30924|30913
HomeBridgeNativeToErc|initialize|190051|190947|190755
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |5739327|5823438|5796326

##### Foreign
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
ERC677BridgeToken|deployment|1498202|1499226|1498829
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
ForeignBridgeNativeToErc|deployment|2768705|2768705|2768705
EternalStorageProxy|upgradeTo|35871|30924|30913
ForeignBridgeNativeToErc|initialize|213493|213557|213549
ERC677BridgeToken|setBridgeContract|29432|44432|39432
ERC677BridgeToken|transferOwnership|30860|30924|30913
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |6762705|6862072|6829736

#### Usage

##### Validators

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To sign at the Home (each validator)|
HomeBridgeNativeToErc|submitSignature|159362|275135|220127
To relay signatures from the Home to the Foreign (one validator)|
ForeignBridgeNativeToErc|executeSignatures|89201|146127|120917
To sign and relay from the Foreign to the Home (each validator)|
HomeBridgeNativeToErc|executeAffirmation|64314|107669|83596

##### Users

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To request transfer from the Home to the Foreign|
HomeBridgeNativeToErc|fallback|46982|46982|46982
To request transfer from the Foreign to the Home|
ERC677BridgeToken|transferAndCall|58370|166206|92399


### `ERC-TO-ERC` Bridge Mode

#### Deployment
##### Home
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
HomeBridgeErcToErc|deployment|3528509|3528509|3528509
EternalStorageProxy|upgradeTo|35871|30924|30913
ERC677BridgeToken|deployment|1498202|1499226|1498829
ERC677BridgeToken|setBridgeContract|29432|44432|39432
ERC677BridgeToken|transferOwnership|30860|30924|30913
HomeBridgeErcToErc|initialize|212299|213195|213003
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |7521315|7621514|7588994

##### Foreign
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
ForeignBridgeErcToErc|deployment|2449436|2449436|2449436
EternalStorageProxy|upgradeTo|35871|30924|30913
ForeignBridgeErcToErc|initialize|150614|150614|150614
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |4822063|4905278|4878358

#### Usage

##### Validators

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To sign at the Home (each validator)|
HomeBridgeErcToErc|submitSignature|159386|275159|220171
To relay signatures from the Home to the Foreign (one validator)|
ForeignBridgeErcToErc|executeSignatures|73779|115769|93027
To sign and relay from the Foreign to the Home (each validator)|
HomeBridgeErcToErc|executeAffirmation|79336|134607|108215

##### Users

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To request transfer from the Home to the Foreign|
ERC677BridgeToken|transferAndCall|58370|166206|92399
To request transfer from the Foreign to the Home|
ERC677BridgeToken|transfer|37691|86589|55000


### `ERC-TO-NATIVE` Bridge Mode

#### Deployment
##### Home
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
HomeBridgeErcToNative|deployment|3757420|3757420|3757420
EternalStorageProxy|upgradeTo|35871|30924|30913
HomeBridgeErcToNative|initialize|196910|213930|210795
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |6176343|6276578|6246523

##### Foreign
Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
EternalStorageProxy|deployment|378510|378510|378510
BridgeValidators|deployment|1144207|1144207|1144207
EternalStorageProxy|upgradeTo|35871|30924|30913
BridgeValidators|initialize|187738|280847|253949
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
EternalStorageProxy|deployment|378510|378510|378510
ForeignBridgeErcToNative|deployment|2449564|2449564|2449564
EternalStorageProxy|upgradeTo|35871|30924|30913
ForeignBridgeErcToNative|initialize|150614|150614|150614
EternalStorageProxy|transferProxyOwnership|30653|30653|30653
Total| |

#### Usage

##### Validators

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To sign at the Home (each validator)|
HomeBridgeErcToNative|submitSignature|159428|275201|220206
To relay signatures from the Home to the Foreign (one validator)|
ForeignBridgeErcToNative|executeSignatures|73779|115769|92985
To sign and relay from the Foreign to the Home (each validator)|
HomeBridgeErcToNative|executeAffirmation|64380|140744|97562

##### Users

Contract | Method | Min | Max | Avg
---- | ---- | ---- | ---- | ----
To request transfer from the Home to the Foreign|
HomeBridgeErcToNative|fallback|80174|80174|80174
To request transfer from the Foreign to the Home|
ERC677BridgeToken|transfer|37691|86589|55000
- [NATIVE-TO-ERC](docs/NATIVE-TO-ERC.md)
- [NATIVE-TO-ERC-WITH-REWARD](docs/NATIVE-TO-ERC-WITH-REWARD.md)
- [ERC-TO-ERC](docs/ERC-TO-ERC.md)
- [ERC-TO-NATIVE](docs/ERC-TO-NATIVE.md)
- [ERC-TO-NATIVE-WITH-REWARD](docs/ERC-TO-NATIVE-WITH-REWARD.md)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ docker-compose down
### Gas Consumption
The [GAS_CONSUMPTION](GAS_CONSUMPTION.md) file includes Min, Max, and Avg gas consumption figures for contracts associated with each bridge mode.

### Reward Management
The [REWARD_MANAGEMENT](REWARD_MANAGEMENT.md) file includes information on how rewards are distributed among the validators on each bridge mode.

### Testing environment
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.
This can be done by running the following command:
Expand Down
53 changes: 53 additions & 0 deletions REWARD_MANAGEMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Reward Management

## NATIVE-TO-ERC
Configuration:
```
HOME_REWARDABLE=ONE_DIRECTION
FOREIGN_REWARDABLE=ONE_DIRECTION
```
### Home to Foreign transfer
Fees are calculated and distributed on Foreign network. Validators will receive ERC20 tokens.
![native-erc-hometoforeign](https://user-images.githubusercontent.com/4614574/51607402-4bda6180-1ef3-11e9-91e3-50fe5d35d296.png)

### Foreign to Home transfer
Fees are calculated and distributed on Home network. Validators will receive native coins.
![native-erc-foreigntohome](https://user-images.githubusercontent.com/4614574/51607428-5d236e00-1ef3-11e9-8083-3669899c7252.png)

## NATIVE-TO-ERC - Fees collected on Home network only
Configuration:
```
HOME_REWARDABLE=BOTH_DIRECTIONS
FOREIGN_REWARDABLE=false
```
### Home to Foreign transfer
Fees are calculated and distributed on Home network. Validators will receive native coins.
![native-erc-homefee-hometoforeign](https://user-images.githubusercontent.com/4614574/53118155-43456d00-352b-11e9-80db-53e31494e09b.png)

### Foreign to Home transfer
Fees are calculated and distributed on Home network. Validators will receive native coins.
![native-erc-homefee-foreigntohome](https://user-images.githubusercontent.com/4614574/53118176-4b9da800-352b-11e9-8118-123f30e37d61.png)

## ERC-TO-NATIVE
Configuration:
```
HOME_REWARDABLE=BOTH_DIRECTIONS
FOREIGN_REWARDABLE=false
```
### Foreign to Home transfer
Fees are calculated and distributed on Home network. Validators will receive native coins.
![erc-native-foreigntohome](https://user-images.githubusercontent.com/4614574/51607498-9065fd00-1ef3-11e9-8212-fc1ba16ae91a.png)

### Home to Foreign transfer
Fees are calculated and distributed on Home network. Validators will receive native coins.
![erc-native-hometoforeign](https://user-images.githubusercontent.com/4614574/51607508-96f47480-1ef3-11e9-93a1-0f1111793f2a.png)

## ERC-TO-ERC

### Foreign to Home transfer
Fees are calculated and distributed on Home network. Validators will receive ERC20 tokens.
![ERC-ERC-ForeignToHome (1)](https://user-images.githubusercontent.com/4614574/56502412-98c8d680-64e8-11e9-8eea-5bcd545d74d9.png)

### Home to Foreign transfer
Fees are calculated and distributed on Home network. Validators will receive ERC20 tokens.
![ERC-ERC-HomeToForeign (1)](https://user-images.githubusercontent.com/4614574/56502454-b8f89580-64e8-11e9-84ae-d9a1c229e0c4.png)
2 changes: 2 additions & 0 deletions contracts/IBlockReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ interface IBlockReward {
function mintedTotally() public view returns (uint256);
function mintedTotallyByBridge(address _bridge) public view returns(uint256);
function bridgesAllowedLength() external view returns(uint256);
function addBridgeTokenFeeReceivers(uint256 _amount) external;
function addBridgeNativeFeeReceivers(uint256 _amount) external;
}
12 changes: 12 additions & 0 deletions contracts/IRewardableValidators.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity 0.4.24;


interface IRewardableValidators {
function isValidator(address _validator) public view returns(bool);
function requiredSignatures() public view returns(uint256);
function owner() public view returns(address);
function validatorList() public view returns (address[]);
function getValidatorRewardAddress(address _validator) public view returns(address);
function validatorCount() public view returns (uint256);
function getNextValidator(address _address) public view returns (address);
}
58 changes: 58 additions & 0 deletions contracts/test/BlockReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import "../libraries/SafeMath.sol";
contract BlockReward is IBlockReward {
using SafeMath for uint256;

address[] public validatorList;
uint256 public mintedCoins = 0;
uint256 public feeAmount = 0;
mapping(bytes32 => uint256) internal uintStorage;
bytes32 internal constant MINTED_TOTALLY_BY_BRIDGE = "mintedTotallyByBridge";
address public token;

function () external payable {
}
Expand Down Expand Up @@ -40,4 +43,59 @@ contract BlockReward is IBlockReward {
bytes32 hash = keccak256(abi.encode(MINTED_TOTALLY_BY_BRIDGE, _bridge));
uintStorage[hash] = uintStorage[hash].add(_amount);
}

function setValidatorsRewards(address[] _initialValidators) external {
validatorList = _initialValidators;
}

function setToken(address _token) external {
token = _token;
}

function addBridgeNativeFeeReceivers(uint256 _amount) external {
feeAmount = _amount;
uint256 feePerValidator = _amount.div(validatorList.length);

uint256 randomValidatorIndex;
uint256 diff = _amount.sub(feePerValidator.mul(validatorList.length));
if (diff > 0) {
randomValidatorIndex = random(validatorList.length);
}

for (uint256 i = 0; i < validatorList.length; i++) {
uint256 feeToDistribute = feePerValidator;
if (diff > 0 && randomValidatorIndex == i) {
feeToDistribute = feeToDistribute.add(diff);
}
this.addExtraReceiver(feeToDistribute, validatorList[i]);
}
}

function addBridgeTokenFeeReceivers(uint256 _amount) external {
address[] memory receivers = new address[](validatorList.length);
uint256[] memory rewards = new uint256[](validatorList.length);
feeAmount = _amount;
uint256 feePerValidator = _amount.div(validatorList.length);

uint256 randomValidatorIndex;
uint256 diff = _amount.sub(feePerValidator.mul(validatorList.length));
if (diff > 0) {
randomValidatorIndex = random(validatorList.length);
}

for (uint256 i = 0; i < validatorList.length; i++) {
uint256 feeToDistribute = feePerValidator;
if (diff > 0 && randomValidatorIndex == i) {
feeToDistribute = feeToDistribute.add(diff);
}
receivers[i] = validatorList[i];
rewards[i] = feeToDistribute;
}

require(token.call(abi.encodeWithSignature("mintReward(address[],uint256[])", receivers, rewards)));
}

function random(uint256 _count) public view returns(uint256) {
return uint256(blockhash(block.number.sub(1))) % _count;
}
}
Loading

0 comments on commit 6dfb7a9

Please sign in to comment.