Skip to content

Commit

Permalink
Merge branch 'arbitrary-message-bridging-#73' into amb-erc677-erc677
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Sep 2, 2019
2 parents ffcb920 + 0dafaf7 commit e8be477
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 1,031 deletions.

This file was deleted.

43 changes: 0 additions & 43 deletions contracts/upgradeable_contracts/arbitrary_message/BasicAMB.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
pragma solidity 0.4.24;

import "../../upgradeability/EternalStorage.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/AddressUtils.sol";
import "../Validatable.sol";
import "../BasicBridge.sol";

contract BasicAMB is BasicBridge {
event HomeToForeignModeChanged(uint256 mode);
event ForeignToHomeModeChanged(uint256 mode);

uint256 internal constant SUBSIDIZED_MODE = 0;
uint256 internal constant DEFRAYAL_MODE = 1;
bytes32 internal constant HOME_TO_FOREIGN_MODE = keccak256(abi.encodePacked("homeToForeignMode"));
bytes32 internal constant FOREIGN_TO_HOME_MODE = keccak256(abi.encodePacked("foreignToHomeMode"));
bytes32 internal constant MAX_GAS_PER_TX = keccak256(abi.encodePacked("maxGasPerTx"));

function initialize(
Expand All @@ -33,15 +22,11 @@ contract BasicAMB is BasicBridge {
uintStorage[MAX_GAS_PER_TX] = _maxGasPerTx;
uintStorage[GAS_PRICE] = _gasPrice;
uintStorage[REQUIRED_BLOCK_CONFIRMATIONS] = _requiredBlockConfirmations;
uintStorage[HOME_TO_FOREIGN_MODE] = SUBSIDIZED_MODE;
uintStorage[FOREIGN_TO_HOME_MODE] = SUBSIDIZED_MODE;
setOwner(_owner);
setInitialize();

emit RequiredBlockConfirmationChanged(_requiredBlockConfirmations);
emit GasPriceChanged(_gasPrice);
emit HomeToForeignModeChanged(SUBSIDIZED_MODE);
emit ForeignToHomeModeChanged(SUBSIDIZED_MODE);

return isInitialized();
}
Expand All @@ -50,34 +35,6 @@ contract BasicAMB is BasicBridge {
return bytes4(keccak256(abi.encodePacked("arbitrary-message-bridge-core")));
}

function setSubsidizedModeForHomeToForeign() external onlyOwner {
uintStorage[HOME_TO_FOREIGN_MODE] = SUBSIDIZED_MODE;
emit HomeToForeignModeChanged(SUBSIDIZED_MODE);
}

function setDefrayalModeForHomeToForeign() external onlyOwner {
uintStorage[HOME_TO_FOREIGN_MODE] = DEFRAYAL_MODE;
emit HomeToForeignModeChanged(DEFRAYAL_MODE);
}

function setSubsidizedModeForForeignToHome() external onlyOwner {
uintStorage[FOREIGN_TO_HOME_MODE] = SUBSIDIZED_MODE;
emit ForeignToHomeModeChanged(SUBSIDIZED_MODE);
}

function setDefrayalModeForForeignToHome() external onlyOwner {
uintStorage[FOREIGN_TO_HOME_MODE] = DEFRAYAL_MODE;
emit ForeignToHomeModeChanged(DEFRAYAL_MODE);
}

function homeToForeignMode() public view returns (uint256) {
return uintStorage[HOME_TO_FOREIGN_MODE];
}

function foreignToHomeMode() public view returns (uint256) {
return uintStorage[FOREIGN_TO_HOME_MODE];
}

function maxGasPerTx() public view returns (uint256) {
return uintStorage[MAX_GAS_PER_TX];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ contract ForeignAMB is BasicForeignAMB {
event UserRequestForAffirmation(bytes encodedData);
event RelayedMessage(address sender, address executor, bytes32 transactionHash, bool status);

function isMessageDeliverySubsidizedMode() internal returns (bool) {
return foreignToHomeMode() == SUBSIDIZED_MODE;
}

function isMessageProcessorSubsidizedMode() internal returns (bool) {
return homeToForeignMode() == SUBSIDIZED_MODE;
}

function emitEventOnMessageRequest(bytes encodedData) internal {
emit UserRequestForAffirmation(encodedData);
}
Expand Down
8 changes: 0 additions & 8 deletions contracts/upgradeable_contracts/arbitrary_message/HomeAMB.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ contract HomeAMB is BasicHomeAMB {
event UserRequestForSignature(bytes encodedData);
event AffirmationCompleted(address sender, address executor, bytes32 transactionHash, bool status);

function isMessageDeliverySubsidizedMode() internal returns (bool) {
return homeToForeignMode() == SUBSIDIZED_MODE;
}

function emitEventOnMessageRequest(bytes encodedData) internal {
emit UserRequestForSignature(encodedData);
}

function isMessageProcessorSubsidizedMode() internal returns (bool) {
return foreignToHomeMode() == SUBSIDIZED_MODE;
}

function emitEventOnMessageProcessed(address sender, address executor, bytes32 txHash, bool status) internal {
emit AffirmationCompleted(sender, executor, txHash, status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,16 @@ contract MessageDelivery is BasicAMB {
using SafeMath for uint256;

function requireToPassMessage(address _contract, bytes _data, uint256 _gas) public {
require(isMessageDeliverySubsidizedMode());
require(_gas >= getMinimumGasUsage(_data) && _gas <= maxGasPerTx());
emitEventOnMessageRequest(abi.encodePacked(msg.sender, _contract, _gas, uint8(0x00), _data));
}

function requireToPassMessage(address _contract, bytes _data, uint256 _gas, uint256 _gasPrice) public {
if (isMessageDeliverySubsidizedMode()) requireToPassMessage(_contract, _data, _gas);
else {
require(_gas >= getMinimumGasUsage(_data) && _gas <= maxGasPerTx());
emitEventOnMessageRequest(abi.encodePacked(msg.sender, _contract, _gas, uint8(0x01), _gasPrice, _data));
}
}

function requireToPassMessage(address _contract, bytes _data, uint256 _gas, bytes1 _oracleGasPriceSpeed) public {
if (isMessageDeliverySubsidizedMode()) requireToPassMessage(_contract, _data, _gas);
else {
require(_gas >= getMinimumGasUsage(_data) && _gas <= maxGasPerTx());
emitEventOnMessageRequest(
abi.encodePacked(msg.sender, _contract, _gas, uint8(0x02), _oracleGasPriceSpeed, _data)
);
}
}

function getMinimumGasUsage(bytes _data) public pure returns (uint256 gas) {
//From Ethereum Yellow Paper
// 68 gas is paid for every non-zero byte of data or code for a transaction
return _data.length.mul(68);
}

function isMessageDeliverySubsidizedMode() internal returns (bool);

/* solcov ignore next */
function emitEventOnMessageRequest(bytes encodedData) internal;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pragma solidity 0.4.24;

import "./BalanceHandler.sol";
import "../../upgradeability/EternalStorage.sol";

contract MessageProcessor is BalanceHandler {
uint256 internal constant PASS_MESSAGE_GAS = 100000;
contract MessageProcessor is EternalStorage {
bytes32 internal constant MESSAGE_SENDER = keccak256(abi.encodePacked("messageSender"));
bytes32 internal constant TRANSACTION_HASH = keccak256(abi.encodePacked("transactionHash"));

Expand Down Expand Up @@ -34,56 +33,19 @@ contract MessageProcessor is BalanceHandler {
address executor,
bytes32 txHash,
uint256 gasLimit,
bytes1 dataType,
uint256 gasPrice,
bytes1, /* dataType */
uint256, /* gasPrice */
bytes memory data
) internal {
bool status;

if (dataType == 0x00) {
require(isMessageProcessorSubsidizedMode());
status = _passMessage(sender, executor, data, gasLimit, txHash);
} else if (dataType == 0x01) {
require(!isMessageProcessorSubsidizedMode());
require(gasPrice == tx.gasprice);
status = _defrayAndPassMessage(sender, executor, data, gasLimit, txHash);
} else if (dataType == 0x02) {
require(!isMessageProcessorSubsidizedMode());
status = _defrayAndPassMessage(sender, executor, data, gasLimit, txHash);
} else {
status = false;
}
bool status = _passMessage(sender, executor, data, gasLimit, txHash);

emitEventOnMessageProcessed(sender, executor, txHash, status);
}

function _defrayAndPassMessage(address _sender, address _contract, bytes _data, uint256 _gas, bytes32 _txHash)
internal
returns (bool)
{
uint256 fee = PASS_MESSAGE_GAS.add(_gas).mul(tx.gasprice);
require(balanceOf(_sender) >= fee);
require(address(this).balance >= fee);

setBalanceOf(_sender, balanceOf(_sender).sub(fee));

bool status = _passMessage(_sender, _contract, _data, _gas, _txHash);

msg.sender.transfer(fee);

return status;
}

function _passMessage(address _sender, address _contract, bytes _data, uint256 _gas, bytes32 _txHash)
internal
returns (bool)
{
if (_contract == address(this)) {
//Special check to handle invocation of withdrawFromDeposit
if (isWithdrawFromDepositSelector(_data)) {
setAccountForAction(_sender);
}
}
setMessageSender(_sender);
setTransactionHash(_txHash);
bool status = _contract.call.gas(_gas)(_data);
Expand All @@ -92,7 +54,6 @@ contract MessageProcessor is BalanceHandler {
return status;
}

function isMessageProcessorSubsidizedMode() internal returns (bool);

/* solcov ignore next */
function emitEventOnMessageProcessed(address sender, address executor, bytes32 txHash, bool status) internal;
}
5 changes: 1 addition & 4 deletions deploy/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ HOME_MAX_AMOUNT_PER_TX=1500000000000000000000000
HOME_MIN_AMOUNT_PER_TX=500000000000000000
HOME_REQUIRED_BLOCK_CONFIRMATIONS=1
HOME_GAS_PRICE=1000000000
# only for ARBITRARY_MESSAGE mode
HOME_AMB_SUBSIDIZED_MODE=false

BLOCK_REWARD_ADDRESS=

Expand All @@ -40,8 +38,7 @@ FOREIGN_MAX_AMOUNT_PER_TX=750000000000000000000000
FOREIGN_MIN_AMOUNT_PER_TX=500000000000000000
FOREIGN_REQUIRED_BLOCK_CONFIRMATIONS=8
FOREIGN_GAS_PRICE=10000000000
# only for ARBITRARY_MESSAGE mode
FOREIGN_AMB_SUBSIDIZED_MODE=false

#for bridge erc_to_erc and erc_to_native mode
ERC20_TOKEN_ADDRESS=
# Only for for erc_to_erc mode
Expand Down
6 changes: 0 additions & 6 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,6 @@ HOME_REQUIRED_BLOCK_CONFIRMATIONS=1
# transactions for deposit or withdrawal confirmations. This price is used if
# the Gas price oracle is unreachable.
HOME_GAS_PRICE=1000000000
# The mode of economic compensation for Home bridge operations.
# If set to true, messages bridged from Foreign Network to Home Network will not pay fees.
HOME_AMB_SUBSIDIZED_MODE=false

# The RPC channel to a Foreign node able to handle deployment/configuration
# transactions.
Expand All @@ -501,9 +498,6 @@ FOREIGN_REQUIRED_BLOCK_CONFIRMATIONS=8
# finalizing asset deposits. This price is used if the Gas price oracle is
# unreachable.
FOREIGN_GAS_PRICE=10000000000
# The mode of economic compensation for Foreign bridge operations.
# If set to true, messages bridged from Home Network to Foreign Network will not pay fees.
FOREIGN_AMB_SUBSIDIZED_MODE=false

# The minimum number of validators required to send their signatures confirming
# the relay of assets. The same number of validators is expected on both sides
Expand Down
58 changes: 1 addition & 57 deletions deploy/src/arbitrary_message/foreign.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const {
FOREIGN_VALIDATORS_OWNER,
FOREIGN_UPGRADEABLE_ADMIN,
FOREIGN_MAX_AMOUNT_PER_TX,
FOREIGN_REQUIRED_BLOCK_CONFIRMATIONS,
HOME_AMB_SUBSIDIZED_MODE,
FOREIGN_AMB_SUBSIDIZED_MODE
FOREIGN_REQUIRED_BLOCK_CONFIRMATIONS
} = env

const DEPLOYMENT_ACCOUNT_ADDRESS = privateKeyToAddress(DEPLOYMENT_ACCOUNT_PRIVATE_KEY)
Expand Down Expand Up @@ -66,60 +64,6 @@ async function initializeBridge({ validatorsBridge, bridge, initialNonce }) {
}
nonce++

if (!HOME_AMB_SUBSIDIZED_MODE) {
console.log('setting defrayal mode for home side')
const homeBridgeDefrayalModeData = await bridge.methods
.setDefrayalModeForForeignToHome()
.encodeABI()
const txHomeBridgeDefrayalModeData = await sendRawTxForeign({
data: homeBridgeDefrayalModeData,
nonce,
to: bridge.options.address,
privateKey: deploymentPrivateKey,
url: FOREIGN_RPC_URL
})
if (txHomeBridgeDefrayalModeData.status) {
assert.strictEqual(
Web3Utils.hexToNumber(txHomeBridgeDefrayalModeData.status),
1,
'Transaction Failed'
)
} else {
await assertStateWithRetry(
bridge.methods.foreignToHomeMode().call,
'1'
)
}
nonce++
}

if (!FOREIGN_AMB_SUBSIDIZED_MODE) {
console.log('setting defrayal mode for foreign side')
const foreignBridgeDefrayalModeData = await bridge.methods
.setDefrayalModeForHomeToForeign()
.encodeABI()
const txForeignBridgeDefrayalModeData = await sendRawTxForeign({
data: foreignBridgeDefrayalModeData,
nonce,
to: bridge.options.address,
privateKey: deploymentPrivateKey,
url: FOREIGN_RPC_URL
})
if (txForeignBridgeDefrayalModeData.status) {
assert.strictEqual(
Web3Utils.hexToNumber(txForeignBridgeDefrayalModeData.status),
1,
'Transaction Failed'
)
} else {
await assertStateWithRetry(
bridge.methods.homeToForeignMode().call,
'1'
)
}
nonce++
}

return nonce
}

Expand Down
Loading

0 comments on commit e8be477

Please sign in to comment.