-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create message transformer on/off ramps that inherit from canonical 1…
….6 on/off ramps
- Loading branch information
1 parent
f84bc28
commit 5d5e85d
Showing
13 changed files
with
278 additions
and
136 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
contracts/src/v0.8/ccip/interfaces/IMessageTransformer.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import {Internal} from "../libraries/Internal.sol"; | ||
|
||
/// @notice Interface for plug-in message hook contracts that transform OffRamp & OnRamp messages. | ||
/// The transformer functions are expected to revert on transform failures. | ||
interface IMessageTransformer { | ||
/// @notice Common error that can be thrown on transform failures and used by consumers | ||
/// @param errorReason abi encoded revert reason | ||
error MessageTransformError(bytes errorReason); | ||
|
||
/// @notice Transforms the given OffRamp message. Reverts on transform failure | ||
/// @param message to transform | ||
/// @return transformed message | ||
function transformInboundMessage( | ||
Internal.Any2EVMRampMessage memory message | ||
) external returns (Internal.Any2EVMRampMessage memory); | ||
|
||
/// @notice Transforms the given OnRamp message. Reverts on transform failure | ||
/// @param message to transform | ||
/// @return transformed message | ||
function transformOutboundMessage( | ||
Internal.EVM2AnyRampMessage memory message | ||
) external returns (Internal.EVM2AnyRampMessage memory); | ||
} |
39 changes: 39 additions & 0 deletions
39
contracts/src/v0.8/ccip/offRamp/MessageTransformerOffRamp.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.24; | ||
|
||
import {OffRamp} from "./OffRamp.sol"; | ||
import {IMessageTransformer} from "../interfaces/IMessageTransformer.sol"; | ||
import {Internal} from "../libraries/Internal.sol"; | ||
|
||
contract MessageTransformerOffRamp is OffRamp { | ||
|
||
address internal s_messageTransformer; | ||
|
||
constructor( | ||
StaticConfig memory staticConfig, | ||
DynamicConfig memory dynamicConfig, | ||
SourceChainConfigArgs[] memory sourceChainConfigs, | ||
address messageTransformerAddr | ||
) OffRamp(staticConfig, dynamicConfig, sourceChainConfigs) { | ||
if (address(messageTransformerAddr) == address(0)) { | ||
revert ZeroAddressNotAllowed(); | ||
} | ||
s_messageTransformer = messageTransformerAddr; | ||
} | ||
|
||
function getMessageTransformerAddress() external view returns (address) { | ||
return s_messageTransformer; | ||
} | ||
|
||
function _beforeExecuteSingleMessage( | ||
Internal.Any2EVMRampMessage memory message | ||
) internal override returns (Internal.Any2EVMRampMessage memory transformedMessage) { | ||
try IMessageTransformer(s_messageTransformer).transformInboundMessage(message) returns ( | ||
Internal.Any2EVMRampMessage memory m | ||
) { | ||
transformedMessage = m; | ||
} catch (bytes memory err) { | ||
revert IMessageTransformer.MessageTransformError(err); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
contracts/src/v0.8/ccip/onRamp/MessageTransformerOnRamp.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.24; | ||
|
||
import {OnRamp} from "./OnRamp.sol"; | ||
import {IMessageTransformer} from "../interfaces/IMessageTransformer.sol"; | ||
import {Internal} from "../libraries/Internal.sol"; | ||
|
||
contract MessageTransformerOnRamp is OnRamp { | ||
|
||
address internal s_messageTransformer; | ||
|
||
error ZeroAddressNotAllowed(); | ||
|
||
constructor( | ||
StaticConfig memory staticConfig, | ||
DynamicConfig memory dynamicConfig, | ||
DestChainConfigArgs[] memory destChainConfigs, | ||
address messageTransformerAddr | ||
) OnRamp(staticConfig, dynamicConfig, destChainConfigs) { | ||
if (address(messageTransformerAddr) == address(0)) { | ||
revert ZeroAddressNotAllowed(); | ||
} | ||
s_messageTransformer = messageTransformerAddr; | ||
} | ||
|
||
function getMessageTransformerAddress() external view returns (address) { | ||
return s_messageTransformer; | ||
} | ||
|
||
function _postProcessMessage( | ||
Internal.EVM2AnyRampMessage memory message | ||
) internal override returns (Internal.EVM2AnyRampMessage memory transformedMessage) { | ||
try IMessageTransformer(s_messageTransformer).transformOutboundMessage( | ||
message | ||
) returns (Internal.EVM2AnyRampMessage memory m) { | ||
transformedMessage = m; | ||
} catch (bytes memory err) { | ||
revert IMessageTransformer.MessageTransformError(err); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...s/src/v0.8/ccip/test/offRamp/OffRamp/MessageTransformerOffRamp.executeSingleMessage.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.24; | ||
|
||
import {OffRamp} from "../../../offRamp/OffRamp.sol"; | ||
import {MessageTransformerOffRamp} from "../../../offRamp/MessageTransformerOffRamp.sol"; | ||
import {OffRampSetup} from "./OffRampSetup.t.sol"; | ||
import {Internal} from "../../../libraries/Internal.sol"; | ||
import {IMessageTransformer} from "../../../interfaces/IMessageTransformer.sol"; | ||
import {MessageTransformerHelper} from "../../helpers/MessageTransformerHelper.sol"; | ||
import {MultiOCR3Base} from "../../../ocr/MultiOCR3Base.sol"; | ||
import {Router} from "../../../Router.sol"; | ||
|
||
contract MessageTransformerOffRamp_executeSingleMessage is OffRampSetup { | ||
|
||
MessageTransformerOffRamp internal s_messageTransformerOffRamp; | ||
|
||
|
||
function setUp() public virtual override { | ||
super.setUp(); | ||
s_messageTransformerOffRamp = new MessageTransformerOffRamp( | ||
s_offRamp.getStaticConfig(), | ||
s_offRamp.getDynamicConfig(), | ||
new OffRamp.SourceChainConfigArgs[](0), | ||
address(s_inboundMessageTransformer) | ||
); | ||
|
||
OffRamp.SourceChainConfigArgs[] memory sourceChainConfigs = new OffRamp.SourceChainConfigArgs[](1); | ||
sourceChainConfigs[0] = OffRamp.SourceChainConfigArgs({ | ||
router: s_destRouter, | ||
sourceChainSelector: SOURCE_CHAIN_SELECTOR_1, | ||
onRamp: ON_RAMP_ADDRESS_1, | ||
isEnabled: true | ||
}); | ||
s_messageTransformerOffRamp.applySourceChainConfigUpdates(sourceChainConfigs); | ||
|
||
Router.OnRamp[] memory onRampUpdates = new Router.OnRamp[](0); | ||
Router.OffRamp[] memory offRampUpdates = new Router.OffRamp[](2 * sourceChainConfigs.length); | ||
|
||
for (uint256 i = 0; i < sourceChainConfigs.length; ++i) { | ||
uint64 sourceChainSelector = sourceChainConfigs[i].sourceChainSelector; | ||
|
||
offRampUpdates[2 * i] = Router.OffRamp({sourceChainSelector: sourceChainSelector, offRamp: address(s_messageTransformerOffRamp)}); | ||
offRampUpdates[2 * i + 1] = Router.OffRamp({ | ||
sourceChainSelector: sourceChainSelector, | ||
offRamp: s_inboundNonceManager.getPreviousRamps(sourceChainSelector).prevOffRamp | ||
}); | ||
} | ||
|
||
s_destRouter.applyRampUpdates(onRampUpdates, new Router.OffRamp[](0), offRampUpdates); | ||
} | ||
|
||
function test_executeSingleMessage_WithMessageTransformer() public { | ||
vm.stopPrank(); | ||
vm.startPrank(address(s_messageTransformerOffRamp)); | ||
Internal.Any2EVMRampMessage memory message = _generateAny2EVMMessageNoTokens(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1, 1); | ||
s_messageTransformerOffRamp.executeSingleMessage(message, new bytes[](message.tokenAmounts.length), new uint32[](0)); | ||
} | ||
|
||
function test_executeSingleMessage_WithMessageTransformer_RevertWhen_UnknownChain() public { | ||
vm.stopPrank(); | ||
vm.startPrank(address(s_messageTransformerOffRamp)); | ||
Internal.Any2EVMRampMessage memory message = _generateAny2EVMMessageNoTokens(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1, 1); | ||
// Fail with any error (UnknownChain in this case) to check if OffRamp wraps the error with MessageTransformError during the revert | ||
s_inboundMessageTransformer.setShouldRevert(true); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
IMessageTransformer.MessageTransformError.selector, | ||
abi.encodeWithSelector(MessageTransformerHelper.UnknownChain.selector) | ||
) | ||
); | ||
s_messageTransformerOffRamp.executeSingleMessage(message, new bytes[](message.tokenAmounts.length), new uint32[](0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.