// SPDX-License-Identifier: MITpragmasolidity 0.6.12;pragmaexperimentalABIEncoderV2;import"@openzeppelin/contracts/math/SafeMath.sol";
import"@openzeppelin/contracts/utils/ReentrancyGuard.sol";
/**
* @dev Accounting is an abstract contract that encapsulates the most critical logic in the Hop contracts.
* The accounting system works by using two balances that can only increase `_credit` and `_debit`.
* A bonder's available balance is the total credit minus the total debit. The contract exposes
* two external functions that allows a bonder to stake and unstake and exposes two internal
* functions to its child contracts that allow the child contract to add to the credit
* and debit balance. In addition, child contracts can override `_additionalDebit` to account
* for any additional debit balance in an alternative way. Lastly, it exposes a modifier,
* `requirePositiveBalance`, that can be used by child contracts to ensure the bonder does not
* use more than its available stake.
*/abstractcontractAccountingisReentrancyGuard{
usingSafeMathforuint256;
mapping(address=>bool) private _isBonder;
mapping(address=>uint256) private _credit;
mapping(address=>uint256) private _debit;
eventStake (addressindexed account,
uint256 amount
);
eventUnstake (addressindexed account,
uint256 amount
);
eventBonderAdded (addressindexed newBonder
);
eventBonderRemoved (addressindexed previousBonder
);
/* ========== Modifiers ========== */modifieronlyBonder{
require(_isBonder[msg.sender], "ACT: Caller is not bonder");
_;
}
modifieronlyGovernance{
_requireIsGovernance();
_;
}
/// @dev Used by parent contract to ensure that the Bonder is solvent at the end of the transaction.modifierrequirePositiveBalance{
_;
require(getCredit(msg.sender) >= getDebitAndAdditionalDebit(msg.sender), "ACT: Not enough available credit");
}
/// @dev Sets the Bonder addressesconstructor(address[] memory bonders) public{
for (uint256 i =0; i < bonders.length; i++) {
require(_isBonder[bonders[i]] ==false, "ACT: Cannot add duplicate bonder");
_isBonder[bonders[i]] =true;
emit BonderAdded(bonders[i]);
}
}
/* ========== Virtual functions ========== *//**
* @dev The following functions are overridden in L1_Bridge and L2_Bridge
*/function_transferFromBridge(address recipient, uint256 amount) internalvirtual;
function_transferToBridge(addressfrom, uint256 amount) internalvirtual;
function_requireIsGovernance() internalvirtual;
/**
* @dev This function can be optionally overridden by a parent contract to track any additional
* debit balance in an alternative way.
*/function_additionalDebit(address/*bonder*/) internalviewvirtualreturns (uint256) {
this; // Silence state mutability warning without generating any additional byte codereturn0;
}
/* ========== Public/external getters ========== *//**
* @dev Check if address is a Bonder
* @param maybeBonder The address being checked
* @return true if address is a Bonder
*/functiongetIsBonder(address maybeBonder) publicviewreturns (bool) {
return _isBonder[maybeBonder];
}
/**
* @dev Get the Bonder's credit balance
* @param bonder The owner of the credit balance being checked
* @return The credit balance for the Bonder
*/functiongetCredit(address bonder) publicviewreturns (uint256) {
return _credit[bonder];
}
/**
* @dev Gets the debit balance tracked by `_debit` and does not include `_additionalDebit()`
* @param bonder The owner of the debit balance being checked
* @return The debit amount for the Bonder
*/functiongetRawDebit(address bonder) externalviewreturns (uint256) {
return _debit[bonder];
}
/**
* @dev Get the Bonder's total debit
* @param bonder The owner of the debit balance being checked
* @return The Bonder's total debit balance
*/functiongetDebitAndAdditionalDebit(address bonder) publicviewreturns (uint256) {
return _debit[bonder].add(_additionalDebit(bonder));
}
/* ========== Bonder external functions ========== *//**
* @dev Allows the Bonder to deposit tokens and increase its credit balance
* @param bonder The address being staked on
* @param amount The amount being staked
*/functionstake(address bonder, uint256 amount) externalpayablenonReentrant{
require(_isBonder[bonder] ==true, "ACT: Address is not bonder");
_transferToBridge(msg.sender, amount);
_addCredit(bonder, amount);
emit Stake(bonder, amount);
}
/**
* @dev Allows the caller to withdraw any available balance and add to their debit balance
* @param amount The amount being unstaked
*/functionunstake(uint256 amount) externalrequirePositiveBalancenonReentrant{
_addDebit(msg.sender, amount);
_transferFromBridge(msg.sender, amount);
emit Unstake(msg.sender, amount);
}
/**
* @dev Add Bonder to allowlist
* @param bonder The address being added as a Bonder
*/functionaddBonder(address bonder) externalonlyGovernance{
require(_isBonder[bonder] ==false, "ACT: Address is already bonder");
_isBonder[bonder] =true;
emit BonderAdded(bonder);
}
/**
* @dev Remove Bonder from allowlist
* @param bonder The address being removed as a Bonder
*/functionremoveBonder(address bonder) externalonlyGovernance{
require(_isBonder[bonder] ==true, "ACT: Address is not bonder");
_isBonder[bonder] =false;
emit BonderRemoved(bonder);
}
/* ========== Internal functions ========== */function_addCredit(address bonder, uint256 amount) internal{
_credit[bonder] = _credit[bonder].add(amount);
}
function_addDebit(address bonder, uint256 amount) internal{
_debit[bonder] = _debit[bonder].add(amount);
}
}
Contract Source Code
File 2 of 8: Bridge.sol
// SPDX-License-Identifier: MITpragmasolidity 0.6.12;pragmaexperimentalABIEncoderV2;import"./Accounting.sol";
import"../libraries/Lib_MerkleTree.sol";
/**
* @dev Bridge extends the accounting system and encapsulates the logic that is shared by both the
* L1 and L2 Bridges. It allows to TransferRoots to be set by parent contracts and for those
* TransferRoots to be withdrawn against. It also allows the bonder to bond and withdraw Transfers
* directly through `bondWithdrawal` and then settle those bonds against their TransferRoot once it
* has been set.
*/abstractcontractBridgeisAccounting{
usingLib_MerkleTreeforbytes32;
structTransferRoot {
uint256 total;
uint256 amountWithdrawn;
uint256 createdAt;
}
/* ========== Events ========== */eventWithdrew(bytes32indexed transferId,
addressindexed recipient,
uint256 amount,
bytes32 transferNonce
);
eventWithdrawalBonded(bytes32indexed transferId,
uint256 amount
);
eventWithdrawalBondSettled(addressindexed bonder,
bytes32indexed transferId,
bytes32indexed rootHash
);
eventMultipleWithdrawalsSettled(addressindexed bonder,
bytes32indexed rootHash,
uint256 totalBondsSettled
);
eventTransferRootSet(bytes32indexed rootHash,
uint256 totalAmount
);
/* ========== State ========== */mapping(bytes32=> TransferRoot) private _transferRoots;
mapping(bytes32=>bool) private _spentTransferIds;
mapping(address=>mapping(bytes32=>uint256)) private _bondedWithdrawalAmounts;
uint256constant RESCUE_DELAY =8weeks;
constructor(address[] memory bonders) publicAccounting(bonders) {}
/* ========== Public Getters ========== *//**
* @dev Get the hash that represents an individual Transfer.
* @param chainId The id of the destination chain
* @param recipient The address receiving the Transfer
* @param amount The amount being transferred including the `_bonderFee`
* @param transferNonce Used to avoid transferId collisions
* @param bonderFee The amount paid to the address that withdraws the Transfer
* @param amountOutMin The minimum amount received after attempting to swap in the destination
* AMM market. 0 if no swap is intended.
* @param deadline The deadline for swapping in the destination AMM market. 0 if no
* swap is intended.
*/functiongetTransferId(uint256 chainId,
address recipient,
uint256 amount,
bytes32 transferNonce,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline
)
publicpurereturns (bytes32)
{
returnkeccak256(abi.encode(
chainId,
recipient,
amount,
transferNonce,
bonderFee,
amountOutMin,
deadline
));
}
/**
* @notice getChainId can be overridden by subclasses if needed for compatibility or testing purposes.
* @dev Get the current chainId
* @return chainId The current chainId
*/functiongetChainId() publicvirtualviewreturns (uint256 chainId) {
this; // Silence state mutability warning without generating any additional byte codeassembly {
chainId :=chainid()
}
}
/**
* @dev Get the TransferRoot id for a given rootHash and totalAmount
* @param rootHash The Merkle root of the TransferRoot
* @param totalAmount The total of all Transfers in the TransferRoot
* @return The calculated transferRootId
*/functiongetTransferRootId(bytes32 rootHash, uint256 totalAmount) publicpurereturns (bytes32) {
returnkeccak256(abi.encodePacked(rootHash, totalAmount));
}
/**
* @dev Get the TransferRoot for a given rootHash and totalAmount
* @param rootHash The Merkle root of the TransferRoot
* @param totalAmount The total of all Transfers in the TransferRoot
* @return The TransferRoot with the calculated transferRootId
*/functiongetTransferRoot(bytes32 rootHash, uint256 totalAmount) publicviewreturns (TransferRoot memory) {
return _transferRoots[getTransferRootId(rootHash, totalAmount)];
}
/**
* @dev Get the amount bonded for the withdrawal of a transfer
* @param bonder The Bonder of the withdrawal
* @param transferId The Transfer's unique identifier
* @return The amount bonded for a Transfer withdrawal
*/functiongetBondedWithdrawalAmount(address bonder, bytes32 transferId) externalviewreturns (uint256) {
return _bondedWithdrawalAmounts[bonder][transferId];
}
/**
* @dev Get the spent status of a transfer ID
* @param transferId The transfer's unique identifier
* @return True if the transferId has been spent
*/functionisTransferIdSpent(bytes32 transferId) externalviewreturns (bool) {
return _spentTransferIds[transferId];
}
/* ========== User/Relayer External Functions ========== *//**
* @notice Can be called by anyone (recipient or relayer)
* @dev Withdraw a Transfer from its destination bridge
* @param recipient The address receiving the Transfer
* @param amount The amount being transferred including the `_bonderFee`
* @param transferNonce Used to avoid transferId collisions
* @param bonderFee The amount paid to the address that withdraws the Transfer
* @param amountOutMin The minimum amount received after attempting to swap in the destination
* AMM market. 0 if no swap is intended. (only used to calculate `transferId` in this function)
* @param deadline The deadline for swapping in the destination AMM market. 0 if no
* swap is intended. (only used to calculate `transferId` in this function)
* @param rootHash The Merkle root of the TransferRoot
* @param transferRootTotalAmount The total amount being transferred in a TransferRoot
* @param transferIdTreeIndex The index of the transferId in the Merkle tree
* @param siblings The siblings of the transferId in the Merkle tree
* @param totalLeaves The total number of leaves in the Merkle tree
*/functionwithdraw(address recipient,
uint256 amount,
bytes32 transferNonce,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
bytes32 rootHash,
uint256 transferRootTotalAmount,
uint256 transferIdTreeIndex,
bytes32[] calldata siblings,
uint256 totalLeaves
)
externalnonReentrant{
bytes32 transferId = getTransferId(
getChainId(),
recipient,
amount,
transferNonce,
bonderFee,
amountOutMin,
deadline
);
require(
rootHash.verify(
transferId,
transferIdTreeIndex,
siblings,
totalLeaves
)
, "BRG: Invalid transfer proof");
bytes32 transferRootId = getTransferRootId(rootHash, transferRootTotalAmount);
_addToAmountWithdrawn(transferRootId, amount);
_fulfillWithdraw(transferId, recipient, amount, uint256(0));
emit Withdrew(transferId, recipient, amount, transferNonce);
}
/**
* @dev Allows the bonder to bond individual withdrawals before their TransferRoot has been committed.
* @param recipient The address receiving the Transfer
* @param amount The amount being transferred including the `_bonderFee`
* @param transferNonce Used to avoid transferId collisions
* @param bonderFee The amount paid to the address that withdraws the Transfer
*/functionbondWithdrawal(address recipient,
uint256 amount,
bytes32 transferNonce,
uint256 bonderFee
)
externalonlyBonderrequirePositiveBalancenonReentrant{
bytes32 transferId = getTransferId(
getChainId(),
recipient,
amount,
transferNonce,
bonderFee,
0,
0
);
_bondWithdrawal(transferId, amount);
_fulfillWithdraw(transferId, recipient, amount, bonderFee);
}
/**
* @dev Refunds the Bonder's stake from a bonded withdrawal and counts that withdrawal against
* its TransferRoot.
* @param bonder The Bonder of the withdrawal
* @param transferId The Transfer's unique identifier
* @param rootHash The Merkle root of the TransferRoot
* @param transferRootTotalAmount The total amount being transferred in a TransferRoot
* @param transferIdTreeIndex The index of the transferId in the Merkle tree
* @param siblings The siblings of the transferId in the Merkle tree
* @param totalLeaves The total number of leaves in the Merkle tree
*/functionsettleBondedWithdrawal(address bonder,
bytes32 transferId,
bytes32 rootHash,
uint256 transferRootTotalAmount,
uint256 transferIdTreeIndex,
bytes32[] calldata siblings,
uint256 totalLeaves
)
external{
require(
rootHash.verify(
transferId,
transferIdTreeIndex,
siblings,
totalLeaves
)
, "BRG: Invalid transfer proof");
bytes32 transferRootId = getTransferRootId(rootHash, transferRootTotalAmount);
uint256 amount = _bondedWithdrawalAmounts[bonder][transferId];
require(amount >0, "L2_BRG: transferId has no bond");
_bondedWithdrawalAmounts[bonder][transferId] =0;
_addToAmountWithdrawn(transferRootId, amount);
_addCredit(bonder, amount);
emit WithdrawalBondSettled(bonder, transferId, rootHash);
}
/**
* @dev Refunds the Bonder for all withdrawals that they bonded in a TransferRoot.
* @param bonder The address of the Bonder being refunded
* @param transferIds All transferIds in the TransferRoot in order
* @param totalAmount The totalAmount of the TransferRoot
*/functionsettleBondedWithdrawals(address bonder,
// transferIds _must_ be calldata or it will be mutated by Lib_MerkleTree.getMerkleRootbytes32[] calldata transferIds,
uint256 totalAmount
)
external{
bytes32 rootHash = Lib_MerkleTree.getMerkleRoot(transferIds);
bytes32 transferRootId = getTransferRootId(rootHash, totalAmount);
uint256 totalBondsSettled =0;
for(uint256 i =0; i < transferIds.length; i++) {
uint256 transferBondAmount = _bondedWithdrawalAmounts[bonder][transferIds[i]];
if (transferBondAmount >0) {
totalBondsSettled = totalBondsSettled.add(transferBondAmount);
_bondedWithdrawalAmounts[bonder][transferIds[i]] =0;
}
}
_addToAmountWithdrawn(transferRootId, totalBondsSettled);
_addCredit(bonder, totalBondsSettled);
emit MultipleWithdrawalsSettled(bonder, rootHash, totalBondsSettled);
}
/* ========== External TransferRoot Rescue ========== *//**
* @dev Allows governance to withdraw the remaining amount from a TransferRoot after the rescue delay has passed.
* @param rootHash the Merkle root of the TransferRoot
* @param originalAmount The TransferRoot's recorded total
* @param recipient The address receiving the remaining balance
*/functionrescueTransferRoot(bytes32 rootHash, uint256 originalAmount, address recipient) externalonlyGovernance{
bytes32 transferRootId = getTransferRootId(rootHash, originalAmount);
TransferRoot memory transferRoot = getTransferRoot(rootHash, originalAmount);
require(transferRoot.createdAt !=0, "BRG: TransferRoot not found");
assert(transferRoot.total == originalAmount);
uint256 rescueDelayEnd = transferRoot.createdAt.add(RESCUE_DELAY);
require(block.timestamp>= rescueDelayEnd, "BRG: TransferRoot cannot be rescued before the Rescue Delay");
uint256 remainingAmount = transferRoot.total.sub(transferRoot.amountWithdrawn);
_addToAmountWithdrawn(transferRootId, remainingAmount);
_transferFromBridge(recipient, remainingAmount);
}
/* ========== Internal Functions ========== */function_markTransferSpent(bytes32 transferId) internal{
require(!_spentTransferIds[transferId], "BRG: The transfer has already been withdrawn");
_spentTransferIds[transferId] =true;
}
function_addToAmountWithdrawn(bytes32 transferRootId, uint256 amount) internal{
TransferRoot storage transferRoot = _transferRoots[transferRootId];
require(transferRoot.total >0, "BRG: Transfer root not found");
uint256 newAmountWithdrawn = transferRoot.amountWithdrawn.add(amount);
require(newAmountWithdrawn <= transferRoot.total, "BRG: Withdrawal exceeds TransferRoot total");
transferRoot.amountWithdrawn = newAmountWithdrawn;
}
function_setTransferRoot(bytes32 rootHash, uint256 totalAmount) internal{
bytes32 transferRootId = getTransferRootId(rootHash, totalAmount);
require(_transferRoots[transferRootId].total ==0, "BRG: Transfer root already set");
require(totalAmount >0, "BRG: Cannot set TransferRoot totalAmount of 0");
_transferRoots[transferRootId] = TransferRoot(totalAmount, 0, block.timestamp);
emit TransferRootSet(rootHash, totalAmount);
}
function_bondWithdrawal(bytes32 transferId, uint256 amount) internal{
require(_bondedWithdrawalAmounts[msg.sender][transferId] ==0, "BRG: Withdrawal has already been bonded");
_addDebit(msg.sender, amount);
_bondedWithdrawalAmounts[msg.sender][transferId] = amount;
emit WithdrawalBonded(transferId, amount);
}
/* ========== Private Functions ========== *//// @dev Completes the Transfer, distributes the Bonder fee and marks the Transfer as spent.function_fulfillWithdraw(bytes32 transferId,
address recipient,
uint256 amount,
uint256 bonderFee
) private{
_markTransferSpent(transferId);
_transferFromBridge(recipient, amount.sub(bonderFee));
if (bonderFee >0) {
_transferFromBridge(msg.sender, bonderFee);
}
}
}
// SPDX-License-Identifier: MITpragmasolidity 0.6.12;pragmaexperimentalABIEncoderV2;import"./Bridge.sol";
import"../interfaces/IMessengerWrapper.sol";
/**
* @dev L1_Bridge is responsible for the bonding and challenging of TransferRoots. All TransferRoots
* originate in the L1_Bridge through `bondTransferRoot` and are propagated up to destination L2s.
*/abstractcontractL1_BridgeisBridge{
structTransferBond {
address bonder;
uint256 createdAt;
uint256 totalAmount;
uint256 challengeStartTime;
address challenger;
bool challengeResolved;
}
/* ========== State ========== */mapping(uint256=>mapping(bytes32=>uint256)) public transferRootCommittedAt;
mapping(bytes32=> TransferBond) public transferBonds;
mapping(uint256=>mapping(address=>uint256)) public timeSlotToAmountBonded;
mapping(uint256=>uint256) public chainBalance;
/* ========== Config State ========== */addresspublic governance;
mapping(uint256=> IMessengerWrapper) public crossDomainMessengerWrappers;
mapping(uint256=>bool) public isChainIdPaused;
uint256public challengePeriod =1days;
uint256public challengeResolutionPeriod =10days;
uint256public minTransferRootBondDelay =15minutes;
uint256publicconstant CHALLENGE_AMOUNT_DIVISOR =10;
uint256publicconstant TIME_SLOT_SIZE =4hours;
/* ========== Events ========== */eventTransferSentToL2(uint256indexed chainId,
addressindexed recipient,
uint256 amount,
uint256 amountOutMin,
uint256 deadline,
addressindexed relayer,
uint256 relayerFee
);
eventTransferRootBonded (bytes32indexed root,
uint256 amount
);
eventTransferRootConfirmed(uint256indexed originChainId,
uint256indexed destinationChainId,
bytes32indexed rootHash,
uint256 totalAmount
);
eventTransferBondChallenged(bytes32indexed transferRootId,
bytes32indexed rootHash,
uint256 originalAmount
);
eventChallengeResolved(bytes32indexed transferRootId,
bytes32indexed rootHash,
uint256 originalAmount
);
/* ========== Modifiers ========== */modifieronlyL2Bridge(uint256 chainId) {
IMessengerWrapper messengerWrapper = crossDomainMessengerWrappers[chainId];
messengerWrapper.verifySender(msg.sender, msg.data);
_;
}
constructor (address[] memory bonders, address _governance) publicBridge(bonders) {
governance = _governance;
}
/* ========== Send Functions ========== *//**
* @notice `amountOutMin` and `deadline` should be 0 when no swap is intended at the destination.
* @notice `amount` is the total amount the user wants to send including the relayer fee
* @dev Send tokens to a supported layer-2 to mint hToken and optionally swap the hToken in the
* AMM at the destination.
* @param chainId The chainId of the destination chain
* @param recipient The address receiving funds at the destination
* @param amount The amount being sent
* @param amountOutMin The minimum amount received after attempting to swap in the destination
* AMM market. 0 if no swap is intended.
* @param deadline The deadline for swapping in the destination AMM market. 0 if no
* swap is intended.
* @param relayer The address of the relayer at the destination.
* @param relayerFee The amount distributed to the relayer at the destination. This is subtracted from the `amount`.
*/functionsendToL2(uint256 chainId,
address recipient,
uint256 amount,
uint256 amountOutMin,
uint256 deadline,
address relayer,
uint256 relayerFee
)
externalpayable{
IMessengerWrapper messengerWrapper = crossDomainMessengerWrappers[chainId];
require(messengerWrapper != IMessengerWrapper(0), "L1_BRG: chainId not supported");
require(isChainIdPaused[chainId] ==false, "L1_BRG: Sends to this chainId are paused");
require(amount >0, "L1_BRG: Must transfer a non-zero amount");
require(amount >= relayerFee, "L1_BRG: Relayer fee cannot exceed amount");
_transferToBridge(msg.sender, amount);
bytesmemory message =abi.encodeWithSignature(
"distribute(address,uint256,uint256,uint256,address,uint256)",
recipient,
amount,
amountOutMin,
deadline,
relayer,
relayerFee
);
chainBalance[chainId] = chainBalance[chainId].add(amount);
messengerWrapper.sendCrossDomainMessage(message);
emit TransferSentToL2(
chainId,
recipient,
amount,
amountOutMin,
deadline,
relayer,
relayerFee
);
}
/* ========== TransferRoot Functions ========== *//**
* @dev Setting a TransferRoot is a two step process.
* @dev 1. The TransferRoot is bonded with `bondTransferRoot`. Withdrawals can now begin on L1
* @dev and recipient L2's
* @dev 2. The TransferRoot is confirmed after `confirmTransferRoot` is called by the l2 bridge
* @dev where the TransferRoot originated.
*//**
* @dev Used by the Bonder to bond a TransferRoot and propagate it up to destination L2s
* @param rootHash The Merkle root of the TransferRoot Merkle tree
* @param destinationChainId The id of the destination chain
* @param totalAmount The amount destined for the destination chain
*/functionbondTransferRoot(bytes32 rootHash,
uint256 destinationChainId,
uint256 totalAmount
)
externalonlyBonderrequirePositiveBalance{
bytes32 transferRootId = getTransferRootId(rootHash, totalAmount);
require(transferRootCommittedAt[destinationChainId][transferRootId] ==0, "L1_BRG: TransferRoot has already been confirmed");
require(transferBonds[transferRootId].createdAt ==0, "L1_BRG: TransferRoot has already been bonded");
uint256 currentTimeSlot = getTimeSlot(block.timestamp);
uint256 bondAmount = getBondForTransferAmount(totalAmount);
timeSlotToAmountBonded[currentTimeSlot][msg.sender] = timeSlotToAmountBonded[currentTimeSlot][msg.sender].add(bondAmount);
transferBonds[transferRootId] = TransferBond(
msg.sender,
block.timestamp,
totalAmount,
uint256(0),
address(0),
false
);
_distributeTransferRoot(rootHash, destinationChainId, totalAmount);
emit TransferRootBonded(rootHash, totalAmount);
}
/**
* @dev Used by an L2 bridge to confirm a TransferRoot via cross-domain message. Once a TransferRoot
* has been confirmed, any challenge against that TransferRoot can be resolved as unsuccessful.
* @param originChainId The id of the origin chain
* @param rootHash The Merkle root of the TransferRoot Merkle tree
* @param destinationChainId The id of the destination chain
* @param totalAmount The amount destined for each destination chain
* @param rootCommittedAt The block timestamp when the TransferRoot was committed on its origin chain
*/functionconfirmTransferRoot(uint256 originChainId,
bytes32 rootHash,
uint256 destinationChainId,
uint256 totalAmount,
uint256 rootCommittedAt
)
externalonlyL2Bridge(originChainId)
{
bytes32 transferRootId = getTransferRootId(rootHash, totalAmount);
require(transferRootCommittedAt[destinationChainId][transferRootId] ==0, "L1_BRG: TransferRoot already confirmed");
require(rootCommittedAt >0, "L1_BRG: rootCommittedAt must be greater than 0");
transferRootCommittedAt[destinationChainId][transferRootId] = rootCommittedAt;
chainBalance[originChainId] = chainBalance[originChainId].sub(totalAmount, "L1_BRG: Amount exceeds chainBalance. This indicates a layer-2 failure.");
// If the TransferRoot was never bonded, distribute the TransferRoot.
TransferBond storage transferBond = transferBonds[transferRootId];
if (transferBond.createdAt ==0) {
_distributeTransferRoot(rootHash, destinationChainId, totalAmount);
}
emit TransferRootConfirmed(originChainId, destinationChainId, rootHash, totalAmount);
}
function_distributeTransferRoot(bytes32 rootHash,
uint256 chainId,
uint256 totalAmount
)
internal{
// Set TransferRoot on recipient Bridgeif (chainId == getChainId()) {
// Set L1 TransferRoot
_setTransferRoot(rootHash, totalAmount);
} else {
chainBalance[chainId] = chainBalance[chainId].add(totalAmount);
IMessengerWrapper messengerWrapper = crossDomainMessengerWrappers[chainId];
require(messengerWrapper != IMessengerWrapper(0), "L1_BRG: chainId not supported");
// Set L2 TransferRootbytesmemory setTransferRootMessage =abi.encodeWithSignature(
"setTransferRoot(bytes32,uint256)",
rootHash,
totalAmount
);
messengerWrapper.sendCrossDomainMessage(setTransferRootMessage);
}
}
/* ========== External TransferRoot Challenges ========== *//**
* @dev Challenge a TransferRoot believed to be fraudulent
* @param rootHash The Merkle root of the TransferRoot Merkle tree
* @param originalAmount The total amount bonded for this TransferRoot
* @param destinationChainId The id of the destination chain
*/functionchallengeTransferBond(bytes32 rootHash, uint256 originalAmount, uint256 destinationChainId) externalpayable{
bytes32 transferRootId = getTransferRootId(rootHash, originalAmount);
TransferBond storage transferBond = transferBonds[transferRootId];
require(transferRootCommittedAt[destinationChainId][transferRootId] ==0, "L1_BRG: TransferRoot has already been confirmed");
require(transferBond.createdAt !=0, "L1_BRG: TransferRoot has not been bonded");
uint256 challengePeriodEnd = transferBond.createdAt.add(challengePeriod);
require(challengePeriodEnd >=block.timestamp, "L1_BRG: TransferRoot cannot be challenged after challenge period");
require(transferBond.challengeStartTime ==0, "L1_BRG: TransferRoot already challenged");
transferBond.challengeStartTime =block.timestamp;
transferBond.challenger =msg.sender;
// Move amount from timeSlotToAmountBonded to debituint256 timeSlot = getTimeSlot(transferBond.createdAt);
uint256 bondAmount = getBondForTransferAmount(originalAmount);
address bonder = transferBond.bonder;
timeSlotToAmountBonded[timeSlot][bonder] = timeSlotToAmountBonded[timeSlot][bonder].sub(bondAmount);
_addDebit(transferBond.bonder, bondAmount);
// Get stake for challengeuint256 challengeStakeAmount = getChallengeAmountForTransferAmount(originalAmount);
_transferToBridge(msg.sender, challengeStakeAmount);
emit TransferBondChallenged(transferRootId, rootHash, originalAmount);
}
/**
* @dev Resolve a challenge after the `challengeResolutionPeriod` has passed
* @param rootHash The Merkle root of the TransferRoot Merkle tree
* @param originalAmount The total amount originally bonded for this TransferRoot
* @param destinationChainId The id of the destination chain
*/functionresolveChallenge(bytes32 rootHash, uint256 originalAmount, uint256 destinationChainId) external{
bytes32 transferRootId = getTransferRootId(rootHash, originalAmount);
TransferBond storage transferBond = transferBonds[transferRootId];
require(transferBond.challengeStartTime !=0, "L1_BRG: TransferRoot has not been challenged");
require(block.timestamp> transferBond.challengeStartTime.add(challengeResolutionPeriod), "L1_BRG: Challenge period has not ended");
require(transferBond.challengeResolved ==false, "L1_BRG: TransferRoot already resolved");
transferBond.challengeResolved =true;
uint256 challengeStakeAmount = getChallengeAmountForTransferAmount(originalAmount);
if (transferRootCommittedAt[destinationChainId][transferRootId] >0) {
// Invalid challengeif (transferBond.createdAt > transferRootCommittedAt[destinationChainId][transferRootId].add(minTransferRootBondDelay)) {
// Credit the bonder back with the bond amount plus the challenger's stake
_addCredit(transferBond.bonder, getBondForTransferAmount(originalAmount).add(challengeStakeAmount));
} else {
// If the TransferRoot was bonded before it was committed, the challenger and Bonder// get their stake back. This discourages Bonders from tricking challengers into// challenging a valid TransferRoots that haven't yet been committed. It also ensures// that Bonders are not punished if a TransferRoot is bonded too soon in error.// Return the challenger's stake
_addCredit(transferBond.challenger, challengeStakeAmount);
// Credit the bonder back with the bond amount
_addCredit(transferBond.bonder, getBondForTransferAmount(originalAmount));
}
} else {
// Valid challenge// Burn 25% of the challengers stake
_transferFromBridge(address(0xdead), challengeStakeAmount.mul(1).div(4));
// Reward challenger with the remaining 75% of their stake plus 100% of the Bonder's stake
_addCredit(transferBond.challenger, challengeStakeAmount.mul(7).div(4));
}
emit ChallengeResolved(transferRootId, rootHash, originalAmount);
}
/* ========== Override Functions ========== */function_additionalDebit(address bonder) internalviewoverridereturns (uint256) {
uint256 currentTimeSlot = getTimeSlot(block.timestamp);
uint256 bonded =0;
uint256 numTimeSlots = challengePeriod / TIME_SLOT_SIZE;
for (uint256 i =0; i < numTimeSlots; i++) {
bonded = bonded.add(timeSlotToAmountBonded[currentTimeSlot - i][bonder]);
}
return bonded;
}
function_requireIsGovernance() internaloverride{
require(governance ==msg.sender, "L1_BRG: Caller is not the owner");
}
/* ========== External Config Management Setters ========== */functionsetGovernance(address _newGovernance) externalonlyGovernance{
require(_newGovernance !=address(0), "L1_BRG: _newGovernance cannot be address(0)");
governance = _newGovernance;
}
functionsetCrossDomainMessengerWrapper(uint256 chainId, IMessengerWrapper _crossDomainMessengerWrapper) externalonlyGovernance{
crossDomainMessengerWrappers[chainId] = _crossDomainMessengerWrapper;
}
functionsetChainIdDepositsPaused(uint256 chainId, bool isPaused) externalonlyGovernance{
isChainIdPaused[chainId] = isPaused;
}
functionsetChallengePeriod(uint256 _challengePeriod) externalonlyGovernance{
require(_challengePeriod % TIME_SLOT_SIZE ==0, "L1_BRG: challengePeriod must be divisible by TIME_SLOT_SIZE");
challengePeriod = _challengePeriod;
}
functionsetChallengeResolutionPeriod(uint256 _challengeResolutionPeriod) externalonlyGovernance{
challengeResolutionPeriod = _challengeResolutionPeriod;
}
functionsetMinTransferRootBondDelay(uint256 _minTransferRootBondDelay) externalonlyGovernance{
minTransferRootBondDelay = _minTransferRootBondDelay;
}
/* ========== Public Getters ========== */functiongetBondForTransferAmount(uint256 amount) publicpurereturns (uint256) {
// Bond covers amount plus a bounty to pay a potential challengerreturn amount.add(getChallengeAmountForTransferAmount(amount));
}
functiongetChallengeAmountForTransferAmount(uint256 amount) publicpurereturns (uint256) {
// Bond covers amount plus a bounty to pay a potential challengerreturn amount.div(CHALLENGE_AMOUNT_DIVISOR);
}
functiongetTimeSlot(uint256 time) publicpurereturns (uint256) {
return time / TIME_SLOT_SIZE;
}
}
Contract Source Code
File 5 of 8: L1_ETH_Bridge.sol
// SPDX-License-Identifier: MITpragmasolidity 0.6.12;pragmaexperimentalABIEncoderV2;import"./L1_Bridge.sol";
/**
* @dev A L1_Bridge that uses an ETH as the canonical token
*/contractL1_ETH_BridgeisL1_Bridge{
constructor (address[] memory bonders, address _governance) publicL1_Bridge(bonders, _governance) {}
/* ========== Override Functions ========== */function_transferFromBridge(address recipient, uint256 amount) internaloverride{
(bool success, ) = recipient.call{value: amount}(newbytes(0));
require(success, 'L1_ETH_BRG: ETH transfer failed');
}
function_transferToBridge(address/*from*/, uint256 amount) internaloverride{
require(msg.value== amount, "L1_ETH_BRG: Value does not match amount");
}
}
Contract Source Code
File 6 of 8: Lib_MerkleTree.sol
// SPDX-License-Identifier: MITpragmasolidity >0.5.0 <0.8.0;/**
* @title Lib_MerkleTree
* @author River Keefer
*/libraryLib_MerkleTree{
/**********************
* Internal Functions *
**********************//**
* Calculates a merkle root for a list of 32-byte leaf hashes. WARNING: If the number
* of leaves passed in is not a power of two, it pads out the tree with zero hashes.
* If you do not know the original length of elements for the tree you are verifying,
* then this may allow empty leaves past _elements.length to pass a verification check down the line.
* Note that the _elements argument is modified, therefore it must not be used again afterwards
* @param _elements Array of hashes from which to generate a merkle root.
* @return Merkle root of the leaves, with zero hashes for non-powers-of-two (see above).
*/functiongetMerkleRoot(bytes32[] memory _elements
)
internalpurereturns (bytes32)
{
require(
_elements.length>0,
"Lib_MerkleTree: Must provide at least one leaf hash."
);
if (_elements.length==1) {
return _elements[0];
}
uint256[16] memory defaults = [
0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563,
0x633dc4d7da7256660a892f8f1604a44b5432649cc8ec5cb3ced4c4e6ac94dd1d,
0x890740a8eb06ce9be422cb8da5cdafc2b58c0a5e24036c578de2a433c828ff7d,
0x3b8ec09e026fdc305365dfc94e189a81b38c7597b3d941c279f042e8206e0bd8,
0xecd50eee38e386bd62be9bedb990706951b65fe053bd9d8a521af753d139e2da,
0xdefff6d330bb5403f63b14f33b578274160de3a50df4efecf0e0db73bcdd3da5,
0x617bdd11f7c0a11f49db22f629387a12da7596f9d1704d7465177c63d88ec7d7,
0x292c23a9aa1d8bea7e2435e555a4a60e379a5a35f3f452bae60121073fb6eead,
0xe1cea92ed99acdcb045a6726b2f87107e8a61620a232cf4d7d5b5766b3952e10,
0x7ad66c0a68c72cb89e4fb4303841966e4062a76ab97451e3b9fb526a5ceb7f82,
0xe026cc5a4aed3c22a58cbd3d2ac754c9352c5436f638042dca99034e83636516,
0x3d04cffd8b46a874edf5cfae63077de85f849a660426697b06a829c70dd1409c,
0xad676aa337a485e4728a0b240d92b3ef7b3c372d06d189322bfd5f61f1e7203e,
0xa2fca4a49658f9fab7aa63289c91b7c7b6c832a6d0e69334ff5b0a3483d09dab,
0x4ebfd9cd7bca2505f7bef59cc1c12ecc708fff26ae4af19abe852afe9e20c862,
0x2def10d13dd169f550f578bda343d9717a138562e0093b380a1120789d53cf10
];
// Reserve memory space for our hashes.bytesmemory buf =newbytes(64);
// We'll need to keep track of left and right siblings.bytes32 leftSibling;
bytes32 rightSibling;
// Number of non-empty nodes at the current depth.uint256 rowSize = _elements.length;
// Current depth, counting from 0 at the leavesuint256 depth =0;
// Common sub-expressionsuint256 halfRowSize; // rowSize / 2bool rowSizeIsOdd; // rowSize % 2 == 1while (rowSize >1) {
halfRowSize = rowSize /2;
rowSizeIsOdd = rowSize %2==1;
for (uint256 i =0; i < halfRowSize; i++) {
leftSibling = _elements[(2* i) ];
rightSibling = _elements[(2* i) +1];
assembly {
mstore(add(buf, 32), leftSibling )
mstore(add(buf, 64), rightSibling)
}
_elements[i] =keccak256(buf);
}
if (rowSizeIsOdd) {
leftSibling = _elements[rowSize -1];
rightSibling =bytes32(defaults[depth]);
assembly {
mstore(add(buf, 32), leftSibling)
mstore(add(buf, 64), rightSibling)
}
_elements[halfRowSize] =keccak256(buf);
}
rowSize = halfRowSize + (rowSizeIsOdd ? 1 : 0);
depth++;
}
return _elements[0];
}
/**
* Verifies a merkle branch for the given leaf hash. Assumes the original length
* of leaves generated is a known, correct input, and does not return true for indices
* extending past that index (even if _siblings would be otherwise valid.)
* @param _root The Merkle root to verify against.
* @param _leaf The leaf hash to verify inclusion of.
* @param _index The index in the tree of this leaf.
* @param _siblings Array of sibline nodes in the inclusion proof, starting from depth 0 (bottom of the tree).
* @param _totalLeaves The total number of leaves originally passed into.
* @return Whether or not the merkle branch and leaf passes verification.
*/functionverify(bytes32 _root,
bytes32 _leaf,
uint256 _index,
bytes32[] memory _siblings,
uint256 _totalLeaves
)
internalpurereturns (bool)
{
require(
_totalLeaves >0,
"Lib_MerkleTree: Total leaves must be greater than zero."
);
require(
_index < _totalLeaves,
"Lib_MerkleTree: Index out of bounds."
);
require(
_siblings.length== _ceilLog2(_totalLeaves),
"Lib_MerkleTree: Total siblings does not correctly correspond to total leaves."
);
bytes32 computedRoot = _leaf;
for (uint256 i =0; i < _siblings.length; i++) {
if ((_index &1) ==1) {
computedRoot =keccak256(
abi.encodePacked(
_siblings[i],
computedRoot
)
);
} else {
computedRoot =keccak256(
abi.encodePacked(
computedRoot,
_siblings[i]
)
);
}
_index >>=1;
}
return _root == computedRoot;
}
/*********************
* Private Functions *
*********************//**
* Calculates the integer ceiling of the log base 2 of an input.
* @param _in Unsigned input to calculate the log.
* @return ceil(log_base_2(_in))
*/function_ceilLog2(uint256 _in
)
privatepurereturns (uint256)
{
require(
_in >0,
"Lib_MerkleTree: Cannot compute ceil(log_2) of 0."
);
if (_in ==1) {
return0;
}
// Find the highest set bit (will be floor(log_2)).// Borrowed with <3 from https://github.com/ethereum/solidity-examplesuint256 val = _in;
uint256 highest =0;
for (uint256 i =128; i >=1; i >>=1) {
if (val & (uint(1) << i) -1<< i !=0) {
highest += i;
val >>= i;
}
}
// Increment by one if this is not a perfect logarithm.if ((uint(1) << highest) != _in) {
highest +=1;
}
return highest;
}
}
Contract Source Code
File 7 of 8: ReentrancyGuard.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/abstractcontractReentrancyGuard{
// Booleans are more expensive than uint256 or any type that takes up a full// word because each write operation emits an extra SLOAD to first read the// slot's contents, replace the bits taken up by the boolean, and then write// back. This is the compiler's defense against contract upgrades and// pointer aliasing, and it cannot be disabled.// The values being non-zero value makes deployment a bit more expensive,// but in exchange the refund on every call to nonReentrant will be lower in// amount. Since refunds are capped to a percentage of the total// transaction's gas, it is best to keep them low in cases like this one, to// increase the likelihood of the full refund coming into effect.uint256privateconstant _NOT_ENTERED =1;
uint256privateconstant _ENTERED =2;
uint256private _status;
constructor () internal{
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/modifiernonReentrant() {
// On the first call to nonReentrant, _notEntered will be truerequire(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
Contract Source Code
File 8 of 8: SafeMath.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/librarySafeMath{
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryAdd(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontrySub(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryMul(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryDiv(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b ==0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryMod(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
if (b ==0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/functionadd(uint256 a, uint256 b) internalpurereturns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/functionmul(uint256 a, uint256 b) internalpurereturns (uint256) {
if (a ==0) return0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b >0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b >0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b >0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b, stringmemory errorMessage) internalpurereturns (uint256) {
require(b >0, errorMessage);
return a % b;
}
}