From 2fbd94866d5520864de59507239e00fa218ffd06 Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Wed, 3 Apr 2024 00:06:22 +0800 Subject: [PATCH] feat(protocol): add readonly functions isMessageFailed & isMessageReceived to Bridge (#16608) Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com> --- packages/protocol/contracts/bridge/Bridge.sol | 76 ++++++++++++++++++- .../protocol/contracts/bridge/IBridge.sol | 2 +- .../contracts/signal/ISignalService.sol | 2 +- .../contracts/signal/SignalService.sol | 4 +- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/packages/protocol/contracts/bridge/Bridge.sol b/packages/protocol/contracts/bridge/Bridge.sol index 98f133d8574..cd20c9f7af0 100644 --- a/packages/protocol/contracts/bridge/Bridge.sol +++ b/packages/protocol/contracts/bridge/Bridge.sol @@ -369,7 +369,8 @@ contract Bridge is EssentialContract, IBridge { }); } - /// @notice Checks if a msgHash has failed on its destination chain. + /// @notice Checks if a msgHash has failed on its destination chain and caches cross-chain data + /// if requested. /// @param _message The message. /// @param _proof The merkle inclusion proof. /// @return true if the message has failed, false otherwise. @@ -390,7 +391,8 @@ contract Bridge is EssentialContract, IBridge { ); } - /// @notice Checks if a msgHash has failed on its destination chain. + /// @notice Checks if a msgHash has failed on its destination chain and caches cross-chain data + /// if requested. /// @param _message The message. /// @param _proof The merkle inclusion proof. /// @return true if the message has failed, false otherwise. @@ -407,6 +409,48 @@ contract Bridge is EssentialContract, IBridge { ); } + /// @notice Checks if a msgHash has failed on its destination chain. + /// This is the 'readonly' version of proveMessageFailed. + /// @param _message The message. + /// @param _proof The merkle inclusion proof. + /// @return true if the message has failed, false otherwise. + function isMessageFailed( + Message calldata _message, + bytes calldata _proof + ) + external + view + returns (bool) + { + if (_message.srcChainId != block.chainid) return false; + + return _isSignalReceived( + resolve("signal_service", false), + signalForFailedMessage(hashMessage(_message)), + _message.destChainId, + _proof + ); + } + + /// @notice Checks if a msgHash has failed on its destination chain. + /// This is the 'readonly' version of proveMessageReceived. + /// @param _message The message. + /// @param _proof The merkle inclusion proof. + /// @return true if the message has failed, false otherwise. + function isMessageReceived( + Message calldata _message, + bytes calldata _proof + ) + external + view + returns (bool) + { + if (_message.destChainId != block.chainid) return false; + return _isSignalReceived( + resolve("signal_service", false), hashMessage(_message), _message.srcChainId, _proof + ); + } + /// @notice Checks if the destination chain is enabled. /// @param _chainId The destination chain ID. /// @return enabled_ True if the destination chain is enabled. @@ -575,7 +619,7 @@ contract Bridge is EssentialContract, IBridge { } } - /// @notice Checks if the signal was received. + /// @notice Checks if the signal was received and caches cross-chain data if requested. /// @param _signalService The signal service address. /// @param _signal The signal. /// @param _chainId The ID of the chain the signal is stored on. @@ -598,4 +642,30 @@ contract Bridge is EssentialContract, IBridge { return false; } } + + /// @notice Checks if the signal was received. + /// This is the 'readonly' version of _proveSignalReceived. + /// @param _signalService The signal service address. + /// @param _signal The signal. + /// @param _chainId The ID of the chain the signal is stored on. + /// @param _proof The merkle inclusion proof. + /// @return true if the message was received. + function _isSignalReceived( + address _signalService, + bytes32 _signal, + uint64 _chainId, + bytes calldata _proof + ) + private + view + returns (bool) + { + try ISignalService(_signalService).verifySignalReceived( + _chainId, resolve(_chainId, "bridge", false), _signal, _proof + ) { + return true; + } catch { + return false; + } + } } diff --git a/packages/protocol/contracts/bridge/IBridge.sol b/packages/protocol/contracts/bridge/IBridge.sol index dd2c22ce924..1ff74a032dc 100644 --- a/packages/protocol/contracts/bridge/IBridge.sol +++ b/packages/protocol/contracts/bridge/IBridge.sol @@ -104,7 +104,7 @@ interface IBridge { event AddressBanned(address indexed addr, bool banned); /// @notice Sends a message to the destination chain and takes custody - /// of Ether required in this contract. All extra Ether will be refunded. + /// of Ether required in this contract. /// @param _message The message to be sent. /// @return msgHash_ The hash of the sent message. /// @return message_ The updated message sent. diff --git a/packages/protocol/contracts/signal/ISignalService.sol b/packages/protocol/contracts/signal/ISignalService.sol index 2677fb468c3..b9d94b7e64b 100644 --- a/packages/protocol/contracts/signal/ISignalService.sol +++ b/packages/protocol/contracts/signal/ISignalService.sol @@ -114,7 +114,7 @@ interface ISignalService { /// @param _signal The signal (message) to send. /// @param _proof Merkle proof that the signal was persisted on the /// source chain. - function isSignalReceived( + function verifySignalReceived( uint64 _chainId, address _app, bytes32 _signal, diff --git a/packages/protocol/contracts/signal/SignalService.sol b/packages/protocol/contracts/signal/SignalService.sol index cc7980a9f08..eb7415f3e62 100644 --- a/packages/protocol/contracts/signal/SignalService.sol +++ b/packages/protocol/contracts/signal/SignalService.sol @@ -134,13 +134,13 @@ contract SignalService is EssentialContract, ISignalService { /// @inheritdoc ISignalService /// @dev This function may revert. - function isSignalReceived( + function verifySignalReceived( uint64 _chainId, address _app, bytes32 _signal, bytes calldata _proof ) - public + external view validSender(_app) nonZeroValue(_signal)