|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.0; |
| 4 | + |
| 5 | +import "../arbitration/IArbitrable.sol"; |
| 6 | +import "../bridge/IL1Bridge.sol"; |
| 7 | + |
| 8 | +import "./interfaces/IHomeGateway.sol"; |
| 9 | +import "./interfaces/IForeignGateway.sol"; |
| 10 | + |
| 11 | +abstract contract BaseForeignGateway is IL1Bridge, IForeignGateway { |
| 12 | + // @dev Note the disputeID needs to start from one as |
| 13 | + // the KlerosV1 proxy governor depends on this implementation. |
| 14 | + uint256 internal localDisputeID = 1; |
| 15 | + |
| 16 | + // For now this is just a constant, but we'd probably need to |
| 17 | + // implement the same arbitrationCost calculation code we'll have |
| 18 | + // in the V2 court. |
| 19 | + uint256 internal internalArbitrationCost; |
| 20 | + |
| 21 | + struct DisputeData { |
| 22 | + uint256 id; |
| 23 | + address arbitrable; |
| 24 | + } |
| 25 | + mapping(uint256 => bytes32) public disputeIDtoHash; |
| 26 | + mapping(bytes32 => DisputeData) public disputeHashtoDisputeData; |
| 27 | + |
| 28 | + IHomeGateway public homeGateway; |
| 29 | + uint256 public chainID; |
| 30 | + |
| 31 | + modifier onlyFromL2() { |
| 32 | + this.onlyAuthorized(); |
| 33 | + _; |
| 34 | + } |
| 35 | + |
| 36 | + constructor(uint256 _arbitrationCost, IHomeGateway _homeGateway) { |
| 37 | + internalArbitrationCost = _arbitrationCost; |
| 38 | + homeGateway = _homeGateway; |
| 39 | + |
| 40 | + uint256 id; |
| 41 | + assembly { |
| 42 | + id := chainid() |
| 43 | + } |
| 44 | + chainID = id; |
| 45 | + } |
| 46 | + |
| 47 | + function createDispute(uint256 _choices, bytes calldata _extraData) external payable returns (uint256 disputeID) { |
| 48 | + require(msg.value >= arbitrationCost(_extraData), "Not paid enough for arbitration"); |
| 49 | + |
| 50 | + disputeID = localDisputeID++; |
| 51 | + bytes32 disputeHash = keccak256( |
| 52 | + abi.encodePacked( |
| 53 | + chainID, |
| 54 | + blockhash(block.number - 1), |
| 55 | + "createDispute", |
| 56 | + disputeID, |
| 57 | + arbitrationCost(_extraData), |
| 58 | + _extraData, |
| 59 | + msg.sender |
| 60 | + ) |
| 61 | + ); |
| 62 | + disputeIDtoHash[disputeID] = disputeHash; |
| 63 | + disputeHashtoDisputeData[disputeHash] = DisputeData({id: disputeID, arbitrable: msg.sender}); |
| 64 | + |
| 65 | + bytes4 methodSelector = IHomeGateway.relayCreateDispute.selector; |
| 66 | + bytes memory data = abi.encodeWithSelector(methodSelector, disputeHash, _choices, _extraData); |
| 67 | + |
| 68 | + uint256 bridgeCost = this.getSubmissionPrice(data.length); |
| 69 | + // We only pay for the submissionPrice gas cost |
| 70 | + // which is minimum gas cost required for submitting a |
| 71 | + // arbitrum retryable ticket to the retry buffer for |
| 72 | + // bridge to L2. |
| 73 | + // For immediate inclusion a user/bot needs to pay (GasPrice x MaxGas) |
| 74 | + // with the associated ticketId that is emitted by this function |
| 75 | + // after the ticket is successfully submitted. |
| 76 | + // For more details, see: |
| 77 | + // https://developer.offchainlabs.com/docs/l1_l2_messages#retryable-tickets-contract-api |
| 78 | + // |
| 79 | + // We do NOT forward the arbitrationCost ETH funds to the HomeGateway yet, |
| 80 | + // only the calldata. |
| 81 | + this.sendCrossDomainMessage{value: bridgeCost}(data, 0, 0); |
| 82 | + |
| 83 | + emit DisputeCreation(disputeID, IArbitrable(msg.sender)); |
| 84 | + } |
| 85 | + |
| 86 | + function arbitrationCost(bytes calldata _extraData) public view returns (uint256 cost) { |
| 87 | + // Calculate the size of calldata that will be passed to the L2 bridge |
| 88 | + // as that is a factor for the bridging cost. |
| 89 | + // Calldata size of relayCreateDispute: |
| 90 | + // relayCreateDispute methodId + |
| 91 | + // (createDispute methodId + bytes32 disputeHash + uint256 _choices + bytes _extraData) |
| 92 | + // 4 + 4 + 32 + 32 + dynamic |
| 93 | + uint256 calldatasize = 82 + _extraData.length; |
| 94 | + |
| 95 | + uint256 bridgeCost = this.getSubmissionPrice(calldatasize); |
| 96 | + return bridgeCost + internalArbitrationCost; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Relay the rule call from the home gateway to the arbitrable. |
| 101 | + */ |
| 102 | + function relayRule(bytes32 _disputeHash, uint256 _ruling) external onlyFromL2 { |
| 103 | + DisputeData memory dispute = disputeHashtoDisputeData[_disputeHash]; |
| 104 | + |
| 105 | + IArbitrable arbitrable = IArbitrable(dispute.arbitrable); |
| 106 | + arbitrable.rule(dispute.id, _ruling); |
| 107 | + } |
| 108 | + |
| 109 | + function foreignDisputeHashToID(bytes32 _disputeHash) external view returns (uint256) { |
| 110 | + return disputeHashtoDisputeData[_disputeHash].id; |
| 111 | + } |
| 112 | + |
| 113 | + function disputeID(uint256 _foreignDisputeID) external view returns (uint256) { |
| 114 | + bytes32 disputeHash = disputeIDtoHash[_foreignDisputeID]; |
| 115 | + require(disputeHash != 0, "Dispute does not exist"); |
| 116 | + |
| 117 | + return homeGateway.homeDisputeHashToID(disputeHash); |
| 118 | + } |
| 119 | + |
| 120 | + function homeChainID(uint256 _disputeID) external view returns (uint256) { |
| 121 | + return homeGateway.chainID(); |
| 122 | + } |
| 123 | + |
| 124 | + function homeBridge(uint256 _disputeID) external view returns (address) { |
| 125 | + return address(homeGateway); |
| 126 | + } |
| 127 | +} |
0 commit comments