-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge the develop branch to the master branch, preparation to v5.0.0
- Loading branch information
Showing
90 changed files
with
7,837 additions
and
900 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
node_modules | ||
deploy/node_modules | ||
.git | ||
.gitignore | ||
.dockerignore | ||
deploy/.env | ||
deploy/*.config | ||
deploy/*.env | ||
docker-compose.yml | ||
Dockerfile | ||
*.log | ||
flats | ||
contracts.sublime-project | ||
contracts.sublime-workspace | ||
upgrade/.env* | ||
!upgrade/.env.example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma solidity 0.4.24; | ||
|
||
interface IMediatorFeeManager { | ||
function calculateFee(uint256) external view returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
pragma solidity 0.4.24; | ||
|
||
import "../upgradeable_contracts/Sacrifice.sol"; | ||
|
||
/** | ||
* @title Address | ||
* @dev Helper methods for Address type. | ||
*/ | ||
library Address { | ||
/** | ||
* @dev Try to send native tokens to the address. If it fails, it will force the transfer by creating a selfdestruct contract | ||
* @param _receiver address that will receive the native tokens | ||
* @param _value the amount of native tokens to send | ||
*/ | ||
function safeSendValue(address _receiver, uint256 _value) internal { | ||
if (!_receiver.send(_value)) { | ||
(new Sacrifice).value(_value)(_receiver); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,112 @@ | ||
pragma solidity 0.4.24; | ||
|
||
import "../interfaces/IBridgeValidators.sol"; | ||
import "./Message.sol"; | ||
|
||
library ArbitraryMessage { | ||
// layout of message :: bytes: | ||
// offset 0: 32 bytes :: uint256 - message length | ||
// offset 32: 32 bytes :: bytes32 txHash | ||
// offset 52: 20 bytes :: address - sender address | ||
// offset 72: 20 bytes :: address - executor contract | ||
// offset 104: 32 bytes :: uint256 - gasLimit | ||
// offset 136: 1 bytes :: bytes1 - dataType | ||
// (optional) 137: 32 bytes :: uint256 - gasPrice | ||
// (optional) 137: 1 bytes :: bytes1 - gasPriceSpeed | ||
|
||
// bytes 1 to 32 are 0 because message length is stored as little endian. | ||
// mload always reads 32 bytes. | ||
// so we can and have to start reading recipient at offset 20 instead of 32. | ||
// if we were to read at 32 the address would contain part of value and be corrupted. | ||
// when reading from offset 20 mload will read 12 zero bytes followed | ||
// by the 20 recipient address bytes and correctly convert it into an address. | ||
// this saves some storage/gas over the alternative solution | ||
// which is padding address to 32 bytes and reading recipient at offset 32. | ||
// for more details see discussion in: | ||
// https://github.com/paritytech/parity-bridge/issues/61 | ||
|
||
function unpackData(bytes _data, bool applyDataOffset) | ||
/** | ||
* @dev Unpacks data fields from AMB message | ||
* layout of message :: bytes: | ||
* offset 0 : 32 bytes :: uint256 - message length | ||
* offset 32 : 32 bytes :: bytes32 - messageId | ||
* offset 64 : 20 bytes :: address - sender address | ||
* offset 84 : 20 bytes :: address - executor contract | ||
* offset 104 : 4 bytes :: uint32 - gasLimit | ||
* offset 108 : 1 bytes :: uint8 - source chain id length (X) | ||
* offset 109 : 1 bytes :: uint8 - destination chain id length (Y) | ||
* offset 110 : 1 bytes :: bytes1 - dataType | ||
* (optional) 111 : 32 bytes :: uint256 - gasPrice | ||
* (optional) 111 : 1 bytes :: bytes1 - gasPriceSpeed | ||
* offset 111/143/112 : X bytes :: bytes - source chain id | ||
* offset 111/143/112 + X : Y bytes :: bytes - destination chain id | ||
* NOTE: when message structure is changed, make sure that MESSAGE_PACKING_VERSION from VersionableAMB is updated as well | ||
* NOTE: assembly code uses calldatacopy, make sure that message is passed as the first argument in the calldata | ||
* @param _data encoded message | ||
*/ | ||
function unpackData(bytes _data) | ||
internal | ||
pure | ||
returns ( | ||
bytes32 messageId, | ||
address sender, | ||
address executor, | ||
bytes32 txHash, | ||
uint256 gasLimit, | ||
uint32 gasLimit, | ||
bytes1 dataType, | ||
uint256[2] chainIds, | ||
uint256 gasPrice, | ||
bytes memory data | ||
) | ||
{ | ||
uint256 dataOffset = 0; | ||
// 32 (message id) + 20 (sender) + 20 (executor) + 4 (gasLimit) + 1 (source chain id length) + 1 (destination chain id length) + 1 (dataType) | ||
uint256 srcdataptr = 32 + 20 + 20 + 4 + 1 + 1 + 1; | ||
uint256 datasize; | ||
// 32 (tx hash) + 20 (sender) + 20 (executor) + 32 (gasLimit) + 1 (dataType) | ||
uint256 srcdataptr = 32 + 20 + 20 + 32 + 1; | ||
|
||
assembly { | ||
txHash := mload(add(_data, 32)) | ||
sender := mload(add(_data, 52)) | ||
executor := mload(add(_data, 72)) | ||
gasLimit := mload(add(_data, 104)) | ||
dataType := and(mload(add(_data, 136)), 0xFF00000000000000000000000000000000000000000000000000000000000000) | ||
messageId := mload(add(_data, 32)) // 32 bytes | ||
sender := and(mload(add(_data, 52)), 0xffffffffffffffffffffffffffffffffffffffff) // 20 bytes | ||
|
||
// executor (20 bytes) + gasLimit (4 bytes) + srcChainIdLength (1 byte) + dstChainIdLength (1 bytes) + dataType (1 byte) + remainder (5 bytes) | ||
let blob := mload(add(_data, 84)) | ||
|
||
// after bit shift left 12 bytes are zeros automatically | ||
executor := shr(96, blob) | ||
gasLimit := and(shr(64, blob), 0xffffffff) | ||
|
||
// load source chain id length | ||
let chainIdLength := byte(24, blob) | ||
|
||
dataType := and(shl(208, blob), 0xFF00000000000000000000000000000000000000000000000000000000000000) | ||
switch dataType | ||
case 0x0000000000000000000000000000000000000000000000000000000000000000 { | ||
gasPrice := 0 | ||
} | ||
case 0x0100000000000000000000000000000000000000000000000000000000000000 { | ||
gasPrice := mload(add(_data, 137)) // 32 | ||
srcdataptr := add(srcdataptr, 0x20) | ||
gasPrice := mload(add(_data, 111)) // 32 | ||
srcdataptr := add(srcdataptr, 32) | ||
} | ||
case 0x0200000000000000000000000000000000000000000000000000000000000000 { | ||
gasPrice := 0 | ||
srcdataptr := add(srcdataptr, 0x01) | ||
srcdataptr := add(srcdataptr, 1) | ||
} | ||
|
||
// at this moment srcdataptr points to sourceChainId | ||
|
||
// mask for sourceChainId | ||
// e.g. length X -> (1 << (X * 8)) - 1 | ||
let mask := sub(shl(shl(3, chainIdLength), 1), 1) | ||
|
||
// increase payload offset by length of source chain id | ||
srcdataptr := add(srcdataptr, chainIdLength) | ||
|
||
// write sourceChainId | ||
mstore(chainIds, and(mload(add(_data, srcdataptr)), mask)) | ||
|
||
// at this moment srcdataptr points to destinationChainId | ||
|
||
// load destination chain id length | ||
chainIdLength := byte(25, blob) | ||
|
||
// mask for destinationChainId | ||
// e.g. length X -> (1 << (X * 8)) - 1 | ||
mask := sub(shl(shl(3, chainIdLength), 1), 1) | ||
|
||
// increase payload offset by length of destination chain id | ||
srcdataptr := add(srcdataptr, chainIdLength) | ||
|
||
// write destinationChainId | ||
mstore(add(chainIds, 32), and(mload(add(_data, srcdataptr)), mask)) | ||
|
||
// at this moment srcdataptr points to payload | ||
|
||
// datasize = message length - payload offset | ||
datasize := sub(mload(_data), srcdataptr) | ||
} | ||
|
||
data = new bytes(datasize); | ||
assembly { | ||
// BYTES_HEADER_SIZE | ||
let dataptr := add(data, 32) | ||
if eq(applyDataOffset, 1) { | ||
dataOffset := 32 | ||
} | ||
// 68 = 4 (selector) + 32 (bytes header) + 32 (bytes length) | ||
calldatacopy(dataptr, add(add(68, srcdataptr), dataOffset), datasize) | ||
// 36 = 4 (selector) + 32 (bytes length header) | ||
srcdataptr := add(srcdataptr, 36) | ||
|
||
// calldataload(4) - offset of first bytes argument in the calldata | ||
calldatacopy(add(data, 32), add(calldataload(4), srcdataptr), datasize) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.