Skip to content

Commit

Permalink
include relayer address in callbacks (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege authored Apr 13, 2024
1 parent c122b1d commit 46aac63
Show file tree
Hide file tree
Showing 22 changed files with 507 additions and 579 deletions.
13 changes: 0 additions & 13 deletions evm/abi/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::{
beefy::IntermediateState,
evm_host::EvmHostEvents,
handler::StateMachineUpdatedFilter,
shared_types::{PostRequest, PostResponse, StateMachineHeight},
};
use anyhow::anyhow;
Expand Down Expand Up @@ -251,18 +250,6 @@ impl TryFrom<ismp::consensus::StateMachineHeight> for StateMachineHeight {
}
}

impl From<StateMachineUpdatedFilter> for ismp::events::StateMachineUpdated {
fn from(value: StateMachineUpdatedFilter) -> Self {
ismp::events::StateMachineUpdated {
state_machine_id: ismp::consensus::StateMachineId {
state_id: StateMachine::Kusama(value.state_machine_id.low_u64() as u32),
consensus_state_id: Default::default(),
},
latest_height: value.height.low_u64(),
}
}
}

impl TryFrom<EvmHostEvents> for ismp::events::Event {
type Error = anyhow::Error;
fn try_from(event: EvmHostEvents) -> Result<Self, Self::Error> {
Expand Down
186 changes: 30 additions & 156 deletions evm/abi/src/generated/evm_host.rs

Large diffs are not rendered by default.

59 changes: 1 addition & 58 deletions evm/abi/src/generated/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,35 +429,7 @@ pub mod handler {
],
),
]),
events: ::core::convert::From::from([
(
::std::borrow::ToOwned::to_owned("StateMachineUpdated"),
::std::vec![
::ethers::core::abi::ethabi::Event {
name: ::std::borrow::ToOwned::to_owned(
"StateMachineUpdated",
),
inputs: ::std::vec![
::ethers::core::abi::ethabi::EventParam {
name: ::std::borrow::ToOwned::to_owned("stateMachineId"),
kind: ::ethers::core::abi::ethabi::ParamType::Uint(
256usize,
),
indexed: false,
},
::ethers::core::abi::ethabi::EventParam {
name: ::std::borrow::ToOwned::to_owned("height"),
kind: ::ethers::core::abi::ethabi::ParamType::Uint(
256usize,
),
indexed: false,
},
],
anonymous: false,
},
],
),
]),
events: ::std::collections::BTreeMap::new(),
errors: ::std::collections::BTreeMap::new(),
receive: false,
fallback: false,
Expand Down Expand Up @@ -567,41 +539,12 @@ pub mod handler {
.method_hash([114, 190, 204, 205], (host, response))
.expect("method not found (this should never happen)")
}
///Gets the contract's `StateMachineUpdated` event
pub fn state_machine_updated_filter(
&self,
) -> ::ethers::contract::builders::Event<::std::sync::Arc<M>, M, StateMachineUpdatedFilter>
{
self.0.event()
}
/// Returns an `Event` builder for all the events of this contract.
pub fn events(
&self,
) -> ::ethers::contract::builders::Event<::std::sync::Arc<M>, M, StateMachineUpdatedFilter>
{
self.0.event_with_filter(::core::default::Default::default())
}
}
impl<M: ::ethers::providers::Middleware> From<::ethers::contract::Contract<M>> for Handler<M> {
fn from(contract: ::ethers::contract::Contract<M>) -> Self {
Self::new(contract.address(), contract.client())
}
}
#[derive(
Clone,
::ethers::contract::EthEvent,
::ethers::contract::EthDisplay,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
#[ethevent(name = "StateMachineUpdated", abi = "StateMachineUpdated(uint256,uint256)")]
pub struct StateMachineUpdatedFilter {
pub state_machine_id: ::ethers::core::types::U256,
pub height: ::ethers::core::types::U256,
}
///Container type for all input parameters for the `handleConsensus` function with signature
/// `handleConsensus(address,bytes)` and selector `0xbb1689be`
#[derive(
Expand Down
125 changes: 70 additions & 55 deletions evm/abi/src/generated/host_manager.rs

Large diffs are not rendered by default.

125 changes: 70 additions & 55 deletions evm/abi/src/generated/ping_module.rs

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions evm/abi/src/generated/shared_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,52 @@ pub struct GetResponse {
pub request: GetRequest,
pub values: ::std::vec::Vec<StorageValue>,
}
///`IncomingGetResponse(((bytes,bytes,uint64,bytes,uint64,bytes[],uint64),(bytes,bytes)[]),
/// address)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct IncomingGetResponse {
pub response: GetResponse,
pub relayer: ::ethers::core::types::Address,
}
///`IncomingPostRequest((bytes,bytes,uint64,bytes,bytes,uint64,bytes),address)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct IncomingPostRequest {
pub request: PostRequest,
pub relayer: ::ethers::core::types::Address,
}
///`IncomingPostResponse(((bytes,bytes,uint64,bytes,bytes,uint64,bytes),bytes,uint64),address)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct IncomingPostResponse {
pub response: PostResponse,
pub relayer: ::ethers::core::types::Address,
}
///`PostRequest(bytes,bytes,uint64,bytes,bytes,uint64,bytes)`
#[derive(
Clone,
Expand Down
8 changes: 4 additions & 4 deletions evm/examples/CrossChainMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ contract CrossChainMessenger is IIsmpModule {
IDispatcher(host).dispatch(post);
}

function onAccept(PostRequest memory request) external onlyIsmpHost {
emit PostReceived(request.nonce, request.source, string(request.body));
function onAccept(IncomingPostRequest memory incoming) external onlyIsmpHost {
emit PostReceived(incoming.request.nonce, incoming.request.source, string(incoming.request.body));
}

function onPostRequestTimeout(PostRequest memory) external view onlyIsmpHost {
revert("No timeouts for now");
}

function onPostResponse(PostResponse memory) external view onlyIsmpHost {
function onPostResponse(IncomingPostResponse memory) external view onlyIsmpHost {
revert("CrossChainMessenger doesn't emit responses");
}

function onPostResponseTimeout(PostResponse memory) external view onlyIsmpHost {
revert("Token gateway doesn't emit Get Requests");
}

function onGetResponse(GetResponse memory) external view onlyIsmpHost {
function onGetResponse(IncomingGetResponse memory) external view onlyIsmpHost {
revert("CrossChainMessenger doesn't emit Get Requests");
}

Expand Down
8 changes: 4 additions & 4 deletions evm/examples/PingModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ contract PingModule is IIsmpModule {
IDispatcher(_host).dispatch(post);
}

function onAccept(PostRequest memory request) external onlyIsmpHost {
emit PostReceived(string(request.body));
function onAccept(IncomingPostRequest memory incoming) external onlyIsmpHost {
emit PostReceived(string(incoming.request.body));
}

function onPostResponse(PostResponse memory) external onlyIsmpHost {
function onPostResponse(IncomingPostResponse memory) external onlyIsmpHost {
emit PostResponseReceived();
}

function onGetResponse(GetResponse memory) external onlyIsmpHost {
function onGetResponse(IncomingGetResponse memory) external onlyIsmpHost {
emit GetResponseReceived();
}

Expand Down
2 changes: 1 addition & 1 deletion evm/lib/ismp-solidity
1 change: 0 additions & 1 deletion evm/script/DeployIsmp.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ contract DeployScript is Script {
consensusState: new bytes(0),
perByteFee: 3 * 1e15, // $0.003/byte
feeToken: address(feeToken),
latestStateMachineHeight: 0,
stateMachineWhitelist: stateMachineWhitelist
});

Expand Down
45 changes: 16 additions & 29 deletions evm/src/hosts/EvmHost.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Math} from "openzeppelin/utils/math/Math.sol";
import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol";
import {Bytes} from "solidity-merkle-trees/trie/Bytes.sol";

import {IIsmpModule} from "ismp/IIsmpModule.sol";
import {IIsmpModule, IncomingPostRequest, IncomingPostResponse, IncomingGetResponse} from "ismp/IIsmpModule.sol";
import {DispatchPost, DispatchPostResponse, DispatchGet} from "ismp/IDispatcher.sol";
import {IIsmpHost, FeeMetadata, ResponseReceipt} from "ismp/IIsmpHost.sol";
import {StateCommitment, StateMachineHeight} from "ismp/IConsensusClient.sol";
Expand Down Expand Up @@ -38,8 +38,6 @@ struct HostParams {
bytes consensusState;
// timestamp for when the consensus was most recently updated
uint256 consensusUpdateTimestamp;
// latest state machine height
uint256 latestStateMachineHeight;
// whitelisted state machines
uint256[] stateMachineWhitelist;
}
Expand Down Expand Up @@ -187,7 +185,12 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
}

constructor(HostParams memory params) {
initHostParams(params);
_hostParams = params;
uint256 length = params.stateMachineWhitelist.length;
for (uint256 i = 0; i < length; i++) {
// set it to non-zero
_latestStateMachineHeight[params.stateMachineWhitelist[i]] = 1;
}
}

/**
Expand Down Expand Up @@ -235,19 +238,6 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
return _frozen;
}

/**
* @dev initialize the host params. Won't work if it has already been initialized.
*/
function initHostParams(HostParams memory params) public {
require(_hostParams.consensusState.length == 0, "HostParams already set");
_hostParams = params;
uint256 length = params.stateMachineWhitelist.length;
for (uint256 i = 0; i < length; i++) {
// set it to non-zero
_latestStateMachineHeight[params.stateMachineWhitelist[i]] = 1;
}
}

/**
* @return the `HostParams`
*/
Expand Down Expand Up @@ -380,14 +370,6 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
_hostParams.consensusUpdateTimestamp = block.timestamp;
}

/**
* @dev Store the latest state machine height
* @param height State Machine Latest Height
*/
function storeLatestStateMachineHeight(uint256 height) external onlyHandler {
_hostParams.latestStateMachineHeight = height;
}

/**
* @dev Store the state commitment at given state height alongside relevant metadata. Assumes the state commitment is of the latest height.
*/
Expand Down Expand Up @@ -420,7 +402,6 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
if (chainId() == block.chainid) {
require(_hostParams.consensusState.equals(new bytes(0)), "Unauthorized action");
}
_hostParams.latestStateMachineHeight = 0;
_hostParams.consensusState = state;
}

Expand All @@ -439,7 +420,9 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
return;
}

(bool success,) = address(destination).call(abi.encodeWithSelector(IIsmpModule.onAccept.selector, request));
(bool success,) = address(destination).call(
abi.encodeWithSelector(IIsmpModule.onAccept.selector, IncomingPostRequest(request, relayer))
);

if (success) {
bytes32 commitment = request.hash();
Expand All @@ -455,7 +438,9 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
*/
function dispatchIncoming(PostResponse memory response, address relayer) external onlyHandler {
address origin = _bytesToAddress(response.request.from);
(bool success,) = address(origin).call(abi.encodeWithSelector(IIsmpModule.onPostResponse.selector, response));
(bool success,) = address(origin).call(
abi.encodeWithSelector(IIsmpModule.onPostResponse.selector, IncomingPostResponse(response, relayer))
);

if (success) {
bytes32 commitment = response.request.hash();
Expand All @@ -471,7 +456,9 @@ abstract contract EvmHost is IIsmpHost, IHostManager, Context {
*/
function dispatchIncoming(GetResponse memory response, address relayer) external onlyHandler {
address origin = _bytesToAddress(response.request.from);
(bool success,) = address(origin).call(abi.encodeWithSelector(IIsmpModule.onGetResponse.selector, response));
(bool success,) = address(origin).call(
abi.encodeWithSelector(IIsmpModule.onGetResponse.selector, IncomingGetResponse(response, relayer))
);

if (success) {
bytes32 commitment = response.request.hash();
Expand Down
2 changes: 0 additions & 2 deletions evm/src/modules/HandlerV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ contract HandlerV1 is IHandler, Context {
_;
}

event StateMachineUpdated(uint256 stateMachineId, uint256 height);

/**
* @dev Handle incoming consensus messages. These message are accompanied with some cryptographic proof.
* If the Host's internal consensus client verifies this proof successfully,
Expand Down
5 changes: 3 additions & 2 deletions evm/src/modules/HostManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {PostRequest, PostResponse, GetRequest, GetResponse, PostTimeout} from "i
import {StateMachine} from "ismp/StateMachine.sol";

import {HostParams, IHostManager, WithdrawParams, EvmHost} from "../hosts/EvmHost.sol";
import {BaseIsmpModule} from "ismp/IIsmpModule.sol";
import {BaseIsmpModule, IncomingPostRequest} from "ismp/IIsmpModule.sol";

/// Host manager params
struct HostManagerParams {
Expand Down Expand Up @@ -56,7 +56,8 @@ contract HostManager is BaseIsmpModule {
_params.admin = address(0);
}

function onAccept(PostRequest calldata request) external override onlyIsmpHost {
function onAccept(IncomingPostRequest calldata incoming) external override onlyIsmpHost {
PostRequest calldata request = incoming.request;
// Only Hyperbridge governor parachain can send requests to this module.
require(request.source.equals(StateMachine.polkadot(_params.governorStateMachineId)), "Unauthorized request");

Expand Down
Loading

0 comments on commit 46aac63

Please sign in to comment.