Modifying IGP to be a hook (#2638)
- IGP as a standalone hook, implementing postDispatch to call payForGas directly - Setting a DEFAULT_GAS_USAGE if metadata not specified and message.senderAddress() as refund address if not specified. - None Fixes https://github.com/hyperlane-xyz/issues/issues/511 Yes, same interface as the previous IGP but for Mailbox V3 Unit Tests --------- Co-authored-by: Yorke Rhodes <yorke@hyperlane.xyz>pull/2736/head
parent
0e10306d4b
commit
f38660e70a
@ -0,0 +1,60 @@ |
||||
// SPDX-License-Identifier: MIT OR Apache-2.0 |
||||
pragma solidity >=0.8.0; |
||||
|
||||
/*@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@@@@@@@@@@@@@@@@@ |
||||
@@@@@ HYPERLANE @@@@@@@ |
||||
@@@@@@@@@@@@@@@@@@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@*/ |
||||
|
||||
import {Message} from "../libs/Message.sol"; |
||||
import {IPostDispatchHook} from "../interfaces/hooks/IPostDispatchHook.sol"; |
||||
import {DomainRoutingHook} from "./DomainRoutingHook.sol"; |
||||
|
||||
contract ConfigurableDomainRoutingHook is DomainRoutingHook { |
||||
using Message for bytes; |
||||
|
||||
/// @notice mapping of destination domain and recipient to custom hook |
||||
mapping(bytes32 => address) public customHooks; |
||||
|
||||
constructor(address mailbox, address owner) DomainRoutingHook(owner) {} |
||||
|
||||
function postDispatch(bytes calldata metadata, bytes calldata message) |
||||
public |
||||
payable |
||||
override |
||||
{ |
||||
bytes32 hookKey = keccak256( |
||||
abi.encodePacked(message.destination(), message.recipient()) |
||||
); |
||||
|
||||
address customHookPreset = customHooks[hookKey]; |
||||
if (customHookPreset != address(0)) { |
||||
IPostDispatchHook(customHookPreset).postDispatch{value: msg.value}( |
||||
metadata, |
||||
message |
||||
); |
||||
} else { |
||||
super.postDispatch(metadata, message); |
||||
} |
||||
} |
||||
|
||||
// TODO: need to restrict sender |
||||
function configCustomHook( |
||||
uint32 destinationDomain, |
||||
bytes32 recipient, |
||||
address hook |
||||
) external { |
||||
bytes32 hookKey = keccak256( |
||||
abi.encodePacked(destinationDomain, recipient) |
||||
); |
||||
require(customHooks[hookKey] == address(0), "hook already set"); |
||||
customHooks[hookKey] = hook; |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
// SPDX-License-Identifier: MIT OR Apache-2.0 |
||||
pragma solidity >=0.8.0; |
||||
|
||||
/*@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@@@@@@@@@@@@@@@@@ |
||||
@@@@@ HYPERLANE @@@@@@@ |
||||
@@@@@@@@@@@@@@@@@@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@@ |
||||
@@@@@@@@@ @@@@@@@@*/ |
||||
|
||||
/** |
||||
* Format of metadata: |
||||
* |
||||
* [0:32] Gas limit for message |
||||
* [32:52] Refund address for message |
||||
*/ |
||||
library IGPMetadata { |
||||
uint8 private constant GAS_LIMIT_OFFSET = 0; |
||||
uint8 private constant REFUND_ADDRESS_OFFSET = 32; |
||||
|
||||
/** |
||||
* @notice Returns the specified gas limit for the message. |
||||
* @param _metadata ABI encoded IGP hook metadata. |
||||
* @return Gas limit for the message as uint256. |
||||
*/ |
||||
function gasLimit(bytes calldata _metadata) |
||||
internal |
||||
pure |
||||
returns (uint256) |
||||
{ |
||||
return |
||||
uint256(bytes32(_metadata[GAS_LIMIT_OFFSET:GAS_LIMIT_OFFSET + 32])); |
||||
} |
||||
|
||||
/** |
||||
* @notice Returns the specified refund address for the message. |
||||
* @param _metadata ABI encoded IGP hook metadata. |
||||
* @return Refund address for the message as address. |
||||
*/ |
||||
function refundAddress(bytes calldata _metadata) |
||||
internal |
||||
pure |
||||
returns (address) |
||||
{ |
||||
return |
||||
address( |
||||
bytes20( |
||||
_metadata[REFUND_ADDRESS_OFFSET:REFUND_ADDRESS_OFFSET + 20] |
||||
) |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @notice Formats the specified gas limit and refund address into IGP hook metadata. |
||||
* @param _gasLimit Gas limit for the message. |
||||
* @param _refundAddress Refund address for the message. |
||||
* @return ABI encoded IGP hook metadata. |
||||
*/ |
||||
function formatMetadata(uint256 _gasLimit, address _refundAddress) |
||||
internal |
||||
pure |
||||
returns (bytes memory) |
||||
{ |
||||
return abi.encodePacked(bytes32(_gasLimit), bytes20(_refundAddress)); |
||||
} |
||||
} |
Loading…
Reference in new issue