1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 192× 192× 4970× 63× 63× 3× 3× 38× 1× 1834× 4× 7× 20× 20× 20× 20× 19× 1× 1× 26× 26× 26× 18× 18× 18× 8× 7× 7× 26× 26× 26× 26× 18× 8× 18× | // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (C) 2022 Spanning Labs Inc. pragma solidity ^0.8.2; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "./ISpanningDelegate.sol"; import "./SpanningUtils.sol"; import "./ISpanning.sol"; contract SpanningDelegate is ISpanningDelegate, Ownable, ReentrancyGuard { // This allows us to efficiently unpack data in our Address specification. using SpanningAddress for bytes32; // A unique identifier for the delegate. bytes4 private domain_; // The address of the delegate upon construction. address delegateAddress_ = address(0); // Indicates whether the delegate is deployable. // TODO: This should be an enum with a couple of options (e.g. "kNone", // "kGeneralSettlement", "kValueSettlement", "kGameSettlement", etc) bool private deployable_ = false; /** * @dev Spanning-generated transaction information. * * Currently, this includes: * + msgSenderAddress: Address of the entity that contacted the delegate * + txnSenderAddress: Address of the originator of the transaction * + TODO(ENG-133): functionId: ABI-encoded function header * + TODO(ENG-133): txnValue: Value of the transaction * * This information is determined directly by the protocol, so it is subject * to different trust assumptions than program payloads. */ bool private isValidData_ = false; bytes32 private currentMsgSenderAddress_; bytes32 private currentTxnSenderAddress_; /** * @dev Initializes a Spanning Delegate. * * @param domain - Unique identifier for the delegate */ constructor(bytes4 domain) { domain_ = domain; delegateAddress_ = address(this); } /** * @return bytes4 - Domain of the delegate. */ function getDomain() public view override returns (bytes4) { return domain_; } /** * @dev Sets the deployable status to true. */ function makeDeployable() external override onlyOwner { deployable_ = true; emit Deployable(deployable_); } /** * @dev Sets the deployable status to false. */ function revokeDeployable() external override onlyOwner { deployable_ = false; emit Deployable(deployable_); } /** * @return bool - Deployable status of the delegate. */ function isDeployable() public view override returns (bool) { return deployable_; } /** * @return bool - If the current stack has set Spanning Info correctly */ function isValidData() external view override returns (bool) { return isValidData_; } /** * @return bytes32 - Address of the entity that contacted the delegate. */ function currentSenderAddress() external view override returns (bytes32) { return currentMsgSenderAddress_; } /** * @return bytes32 - Address of the originator of the transaction. */ function currentTxnSenderAddress() external view override returns (bytes32) { return currentTxnSenderAddress_; } /** * @dev Used by authorized middleware to run a transaction on this domain. * * Note: We currently we assume the contract owner == authorized address * * @param programAddress - Address to be called * @param msgSenderAddress - Address of who requested the call * @param txnSenderAddress - Address of the originator of the call * @param payload - ABI-encoding of the desired function call */ function spanningCall( bytes32 programAddress, bytes32 msgSenderAddress, bytes32 txnSenderAddress, bytes calldata payload ) external override onlyOwner nonReentrant { executeSpanningCall( programAddress, msgSenderAddress, txnSenderAddress, payload ); } /** * @dev Allows a user to request a call over authorized middleware nodes. * * Note: This can result in either a local or cross-domain transaction. * Note: Dispatch uses EVM Events as a signal to our middleware. * * @param programAddress - Address to be called * @param payload - ABI-encoding of the desired function call * * TODO(ENG-133): Support "rolled up" method to dispatch transactions */ function makeRequest(bytes32 programAddress, bytes calldata payload) external override nonReentrant { // This will fail if this is being called in the address space of a program // i.e. `delegateCall()` is used instead of `call()` Erequire( address(this) == delegateAddress_, "Contract called as `delegateCall`" ); bytes32 msgSenderAddress = SpanningAddress.create( msg.sender, getDomain() ); bytes32 txnSenderAddress = SpanningAddress.create( tx.origin, getDomain() ); // Request was meant for this domain. Execute the call locally. if (programAddress.getDomain() == getDomain()) { executeSpanningCall( programAddress, msgSenderAddress, txnSenderAddress, payload ); } else { emit SpanningRequest( programAddress, msgSenderAddress, txnSenderAddress, payload ); } } /** * @dev Fallback function for receiving payment */ receive() external payable { Irequire(false, "This contract is not payable."); } /** * @dev Pack Spanning transaction information * * Note: Keep this function in sync with resetSpanningInformation() */ function packSpanningInformation( bytes32 msgSenderAddress, bytes32 txnSenderAddress ) internal { currentMsgSenderAddress_ = msgSenderAddress; currentTxnSenderAddress_ = txnSenderAddress; isValidData_ = true; } /** * @dev Reset cached Spanning transaction information * * Note: Keep this function in sync with packSpanningInformation() */ function resetSpanningInformation() internal { isValidData_ = false; currentMsgSenderAddress_ = bytes32(0); currentTxnSenderAddress_ = bytes32(0); } /** * @dev Helper function to parse revert message * from https://ethereum.stackexchange.com/questions/83528 * * @param _returnData - return data from call */ function _getRevertMsg(bytes memory _returnData) internal view returns (string memory) { // If the _res length is less than 68, then the transaction failed silently (without a revert message) if (_returnData.length < 68) return "Transaction reverted silently"; assembly { // Slice the sighash. _returnData := add(_returnData, 0x04) } return abi.decode(_returnData, (string)); // All that remains is the revert string } /** * @dev Helper function to execute a local transaction * * @param programAddress - Address to be called * @param msgSenderAddress - Address of who requested the call * @param txnSenderAddress - Address of the originator of the transaction * @param payload - ABI-encoding of the desired function call */ function executeSpanningCall( bytes32 programAddress, bytes32 msgSenderAddress, bytes32 txnSenderAddress, bytes calldata payload ) internal { Erequire(isDeployable(), "Chain is currently read-only"); // Pack temporary Spanning txn information for this transaction packSpanningInformation(msgSenderAddress, txnSenderAddress); // Execute (bool success, bytes memory returnData) = programAddress .getAddress() .call(payload); if (success) { emit LocalRequest( programAddress, msgSenderAddress, txnSenderAddress, payload, returnData ); } else { revert(_getRevertMsg(returnData)); } // Reset temporary Spanning txn information resetSpanningInformation(); } } |