Skip to content

Commit

Permalink
Merge the develop branch to the master branch, preparation to v5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
akolotov committed Jun 16, 2020
2 parents 56c2c39 + 7f89095 commit a90ce6c
Show file tree
Hide file tree
Showing 19 changed files with 1,079 additions and 833 deletions.
1 change: 0 additions & 1 deletion contracts/upgradeable_contracts/BasicAMBMediator.sol
Expand Up @@ -13,7 +13,6 @@ contract BasicAMBMediator is Ownable {
bytes32 internal constant BRIDGE_CONTRACT = 0x811bbb11e8899da471f0e69a3ed55090fc90215227fc5fb1cb0d6e962ea7b74f; // keccak256(abi.encodePacked("bridgeContract"))
bytes32 internal constant MEDIATOR_CONTRACT = 0x98aa806e31e94a687a31c65769cb99670064dd7f5a87526da075c5fb4eab9880; // keccak256(abi.encodePacked("mediatorContract"))
bytes32 internal constant REQUEST_GAS_LIMIT = 0x2dfd6c9f781bb6bbb5369c114e949b69ebb440ef3d4dd6b2836225eb1dc3a2be; // keccak256(abi.encodePacked("requestGasLimit"))
bytes32 internal constant NONCE = 0x7ab1577440dd7bedf920cb6de2f9fc6bf7ba98c78c85a3fa1f8311aac95e1759; // keccak256(abi.encodePacked("nonce"))

/**
* @dev Sets the AMB bridge contract address. Only the owner can call this method.
Expand Down
2 changes: 0 additions & 2 deletions contracts/upgradeable_contracts/TokenBridgeMediator.sol
Expand Up @@ -86,8 +86,6 @@ contract TokenBridgeMediator is BasicAMBMediator, BasicTokenBridge {
/**
* @dev Handles the bridged tokens. Checks that the value is inside the execution limits and invokes the method
* to execute the Mint or Unlock accordingly.
* nonce parameter is a unique message identifier on AMB bridge, it is not actually used by this method,
* it serves as unique reference.
* @param _recipient address that will receive the tokens
* @param _value amount of tokens to be received
*/
Expand Down
Expand Up @@ -75,7 +75,7 @@ contract BasicAMBNativeToErc20 is
* @return patch value of the version
*/
function getBridgeInterfacesVersion() external pure returns (uint64 major, uint64 minor, uint64 patch) {
return (1, 0, 0);
return (1, 0, 1);
}

/**
Expand Down
Expand Up @@ -107,6 +107,7 @@ contract HomeAMBNativeToErc20 is BasicAMBNativeToErc20 {
* @param _value amount of native tokens to be received
*/
function executeActionOnFixedTokens(address _receiver, uint256 _value) internal {
setMediatorBalance(mediatorBalance().sub(_value));
Address.safeSendValue(_receiver, _value);
}

Expand Down Expand Up @@ -156,7 +157,7 @@ contract HomeAMBNativeToErc20 is BasicAMBNativeToErc20 {
require(balance > mediatorBalance());
uint256 diff = balance.sub(mediatorBalance());
setTotalSpentPerDay(getCurrentDay(), totalSpentPerDay(getCurrentDay()).add(diff));
setMediatorBalance(mediatorBalance().add(diff));
setMediatorBalance(balance);
passMessage(_receiver, _receiver, diff);
}
}
34 changes: 17 additions & 17 deletions contracts/upgradeable_contracts/arbitrary_message/BasicAMB.sol
Expand Up @@ -62,6 +62,22 @@ contract BasicAMB is BasicBridge, VersionableAMB {
uintStorage[MAX_GAS_PER_TX] = _maxGasPerTx;
}

/**
* Internal function for retrieving chain id for the source network
* @return chain id for the current network
*/
function sourceChainId() public view returns (uint256) {
return uintStorage[SOURCE_CHAIN_ID];
}

/**
* Internal function for retrieving chain id for the destination network
* @return chain id for the destination network
*/
function destinationChainId() public view returns (uint256) {
return uintStorage[DESTINATION_CHAIN_ID];
}

/**
* Updates chain ids of used networks
* @param _sourceChainId chain id for current network
Expand Down Expand Up @@ -115,14 +131,6 @@ contract BasicAMB is BasicBridge, VersionableAMB {
uintStorage[DESTINATION_CHAIN_ID_LENGTH] = destinationChainIdLength;
}

/**
* Internal function for retrieving chain id for the source network
* @return chain id for the current network
*/
function _sourceChainId() internal view returns (uint256) {
return uintStorage[SOURCE_CHAIN_ID];
}

/**
* Internal function for retrieving chain id length for the source network
* @return chain id for the current network
Expand All @@ -131,14 +139,6 @@ contract BasicAMB is BasicBridge, VersionableAMB {
return uintStorage[SOURCE_CHAIN_ID_LENGTH];
}

/**
* Internal function for retrieving chain id for the destination network
* @return chain id for the destination network
*/
function _destinationChainId() internal view returns (uint256) {
return uintStorage[DESTINATION_CHAIN_ID];
}

/**
* Internal function for retrieving chain id length for the destination network
* @return chain id for the destination network
Expand All @@ -161,6 +161,6 @@ contract BasicAMB is BasicBridge, VersionableAMB {
* @param _chainId destination chain id of the received message
*/
function _isDestinationChainIdValid(uint256 _chainId) internal returns (bool res) {
return _chainId == _sourceChainId();
return _chainId == sourceChainId();
}
}
Expand Up @@ -52,30 +52,30 @@ contract MessageDelivery is BasicAMB, MessageProcessor {
* @param _gas gas limit used on the other network for executing a message
*/
function _packHeader(address _contract, uint256 _gas) internal view returns (bytes memory header) {
uint256 sourceChainId = _sourceChainId();
uint256 sourceChainIdLength = _sourceChainIdLength();
uint256 destinationChainId = _destinationChainId();
uint256 destinationChainIdLength = _destinationChainIdLength();
uint256 srcChainId = sourceChainId();
uint256 srcChainIdLength = _sourceChainIdLength();
uint256 dstChainId = destinationChainId();
uint256 dstChainIdLength = _destinationChainIdLength();

bytes32 mVer = MESSAGE_PACKING_VERSION;
uint256 nonce = _nonce();

// Bridge id is recalculated every time again and again, since it is still cheaper than using SLOAD opcode (800 gas)
bytes32 bridgeId = keccak256(abi.encodePacked(sourceChainId, address(this))) &
bytes32 bridgeId = keccak256(abi.encodePacked(srcChainId, address(this))) &
0x00000000ffffffffffffffffffffffffffffffffffffffff0000000000000000;
// 79 = 4 + 20 + 8 + 20 + 20 + 4 + 1 + 1 + 1
header = new bytes(79 + sourceChainIdLength + destinationChainIdLength);
header = new bytes(79 + srcChainIdLength + dstChainIdLength);

// In order to save the gas, the header is packed in the reverse order.
// With such approach, it is possible to store right-aligned values without any additional bit shifts.
assembly {
let ptr := add(header, mload(header)) // points to the last word of header
mstore(ptr, destinationChainId)
mstore(sub(ptr, destinationChainIdLength), sourceChainId)
mstore(ptr, dstChainId)
mstore(sub(ptr, dstChainIdLength), srcChainId)

mstore(add(header, 79), 0x00)
mstore(add(header, 78), destinationChainIdLength)
mstore(add(header, 77), sourceChainIdLength)
mstore(add(header, 78), dstChainIdLength)
mstore(add(header, 77), srcChainIdLength)
mstore(add(header, 76), _gas)
mstore(add(header, 72), _contract)
mstore(add(header, 52), caller)
Expand Down
Expand Up @@ -17,6 +17,6 @@ contract VersionableAMB is VersionableBridge {
* @return (major, minor, patch) version triple
*/
function getBridgeInterfacesVersion() external pure returns (uint64 major, uint64 minor, uint64 patch) {
return (5, 0, 1);
return (5, 0, 2);
}
}

0 comments on commit a90ce6c

Please sign in to comment.