Skip to content

Commit

Permalink
feat(relayer): introduce StarknetMessage type containing counterpar…
Browse files Browse the repository at this point in the history
…ty Cosmos height (#165)

* Add StarknetMessage which contains the counterparty height and use it instead of Call for Messages

* helper methods

* rm redundant counterparty height

* rename var

* happy clippy

---------

Co-authored-by: Ranadeep Biswas <[email protected]>
  • Loading branch information
ljoss17 and rnbguy authored Jan 6, 2025
1 parent 1fc51d4 commit a151c39
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hermes_test_components::chain::traits::types::address::HasAddressType;
use starknet::accounts::Call;
use starknet::core::types::Felt;

use crate::impls::types::message::StarknetMessage;
use crate::traits::contract::message::InvokeContractMessageBuilder;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasSelectorType;
Expand All @@ -14,18 +15,20 @@ where
Chain: HasAddressType<Address = Felt>
+ HasSelectorType<Selector = Felt>
+ HasBlobType<Blob = Vec<Felt>>
+ HasMessageType<Message = Call>,
+ HasMessageType<Message = StarknetMessage>,
{
fn build_invoke_contract_message(
_chain: &Chain,
contract_address: &Felt,
entry_point_selector: &Felt,
calldata: &Vec<Felt>,
) -> Call {
Call {
) -> StarknetMessage {
let call = Call {
to: *contract_address,
selector: *entry_point_selector,
calldata: calldata.clone(),
}
};

StarknetMessage::new(call)
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use hermes_chain_components::traits::types::height::HasHeightType;
use hermes_chain_components::traits::types::ibc::CounterpartyMessageHeightGetter;
use hermes_chain_components::traits::types::message::HasMessageType;
use ibc::core::client::types::Height;

use crate::impls::types::message::StarknetMessage;

pub struct GetCounterpartyCosmosHeightFromStarknetMessage;

impl<Chain, Counterparty> CounterpartyMessageHeightGetter<Chain, Counterparty>
for GetCounterpartyCosmosHeightFromStarknetMessage
where
Chain: HasMessageType,
Counterparty: HasHeightType,
Chain: HasMessageType<Message = StarknetMessage>,
Counterparty: HasHeightType<Height = Height>,
{
fn counterparty_message_height_for_update_client(
_message: &Chain::Message,
message: &Chain::Message,
) -> Option<Counterparty::Height> {
// TODO: Define a `StarknetMessage` type that wraps around `Call`
// and provide counterparty height
None
message.counterparty_height
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use starknet::accounts::Call;
use starknet::core::types::Felt;
use starknet::macros::selector;

use crate::impls::types::message::StarknetMessage;
use crate::traits::queries::address::CanQueryContractAddress;
use crate::types::channel_id::ChannelId as StarknetChannelId;
use crate::types::connection_id::ConnectionId as StarknetConnectionId;
Expand All @@ -44,7 +45,7 @@ pub struct BuildStarknetChannelHandshakeMessages;
impl<Chain, Counterparty, Encoding> ChannelOpenInitMessageBuilder<Chain, Counterparty>
for BuildStarknetChannelHandshakeMessages
where
Chain: HasMessageType<Message = Call>
Chain: HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
Expand Down Expand Up @@ -106,20 +107,22 @@ where
.encode(&chan_open_init_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("chan_open_init"),
calldata,
};

let message = StarknetMessage::new(call);

Ok(message)
}
}

impl<Chain, Counterparty, Encoding> ChannelOpenTryMessageBuilder<Chain, Counterparty>
for BuildStarknetChannelHandshakeMessages
where
Chain: HasMessageType<Message = Call>
Chain: HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
Expand Down Expand Up @@ -205,12 +208,15 @@ where
.encode(&chan_open_try_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("chan_open_try"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Expand All @@ -219,7 +225,7 @@ impl<Chain, Counterparty, Encoding> ChannelOpenAckMessageBuilder<Chain, Counterp
for BuildStarknetChannelHandshakeMessages
where
Chain: HasChannelIdType<Counterparty, ChannelId = StarknetChannelId>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
Expand Down Expand Up @@ -279,12 +285,15 @@ where
.encode(&chan_open_ack_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("chan_open_ack"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Expand All @@ -294,7 +303,7 @@ impl<Chain, Counterparty, Encoding> ChannelOpenConfirmMessageBuilder<Chain, Coun
where
Chain: HasPortIdType<Counterparty, PortId = IbcPortId>
+ HasChannelIdType<Counterparty, ChannelId = StarknetChannelId>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
Expand Down Expand Up @@ -340,12 +349,15 @@ where
.encode(&chan_open_confirm_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("chan_open_confirm"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use starknet::accounts::Call;
use starknet::core::types::Felt;
use starknet::macros::selector;

use crate::impls::types::message::StarknetMessage;
use crate::traits::queries::address::CanQueryContractAddress;
use crate::types::client_id::ClientId as StarknetClientId;
use crate::types::connection_id::ConnectionId as StarknetConnectionId;
Expand All @@ -58,7 +59,7 @@ where
InitConnectionOptions = CosmosInitConnectionOptions,
> + HasClientIdType<Counterparty, ClientId = StarknetClientId>
+ HasAddressType<Address = Felt>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
+ CanRaiseError<&'static str>
Expand Down Expand Up @@ -150,12 +151,14 @@ where
.encode(&conn_open_init_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("conn_open_init"),
calldata,
};

let message = StarknetMessage::new(call);

Ok(message)
}
}
Expand All @@ -164,7 +167,7 @@ impl<Chain, Counterparty, Encoding> ConnectionOpenTryMessageBuilder<Chain, Count
for BuildStarknetConnectionHandshakeMessages
where
Chain: HasHeightType
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasClientIdType<Counterparty, ClientId = StarknetClientId>
+ HasClientStateType<Counterparty>
+ HasAddressType<Address = Felt>
Expand All @@ -191,7 +194,7 @@ where
client_id: &StarknetClientId,
counterparty_client_id: &CosmosClientId,
counterparty_connection_id: &CosmosConnectionId,
payload: ConnectionOpenTryPayload<Counterparty, Chain>,
counterparty_payload: ConnectionOpenTryPayload<Counterparty, Chain>,
) -> Result<Chain::Message, Chain::Error> {
// FIXME: Cairo IBC should accept counterparty client ID as string value
let cosmos_client_id_as_cairo = {
Expand All @@ -208,7 +211,7 @@ where

// FIXME: Cairo IBC should use bytes for commitment prefix
let commitment_prefix =
from_utf8(&payload.commitment_prefix).map_err(Chain::raise_error)?;
from_utf8(&counterparty_payload.commitment_prefix).map_err(Chain::raise_error)?;

// FIXME: Use the connection version from the payload
let connection_version = ConnectionVersion {
Expand All @@ -222,8 +225,8 @@ where
};

let proof_height = CairoHeight {
revision_number: payload.update_height.revision_number(),
revision_height: payload.update_height.revision_height(),
revision_number: counterparty_payload.update_height.revision_number(),
revision_height: counterparty_payload.update_height.revision_height(),
};

let conn_open_init_msg: MsgConnOpenTry = MsgConnOpenTry {
Expand All @@ -248,12 +251,15 @@ where
.encode(&conn_open_init_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("conn_open_init"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Expand All @@ -265,7 +271,7 @@ where
+ HasClientStateType<Counterparty>
+ HasConnectionIdType<Counterparty, ConnectionId = StarknetConnectionId>
+ HasAddressType<Address = Felt>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
+ CanRaiseError<Encoding::Error>,
Expand Down Expand Up @@ -322,12 +328,15 @@ where
.encode(&conn_open_ack_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("conn_open_ack"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Expand All @@ -337,7 +346,7 @@ impl<Chain, Counterparty, Encoding> ConnectionOpenConfirmMessageBuilder<Chain, C
where
Chain: HasConnectionIdType<Counterparty, ConnectionId = StarknetConnectionId>
+ HasAddressType<Address = Felt>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
+ CanRaiseError<Encoding::Error>,
Expand Down Expand Up @@ -379,12 +388,15 @@ where
.encode(&conn_open_confirm_msg)
.map_err(Chain::raise_error)?;

let message = Call {
let call = Call {
to: ibc_core_address,
selector: selector!("conn_open_confirm"),
calldata,
};

let message =
StarknetMessage::new(call).with_counterparty_height(counterparty_payload.update_height);

Ok(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use starknet::accounts::Call;
use starknet::core::types::Felt;
use starknet::macros::{selector, short_string};

use crate::impls::types::message::StarknetMessage;
use crate::traits::queries::address::CanQueryContractAddress;
use crate::types::cosmos::client_state::{ClientStatus, CometClientState};
use crate::types::cosmos::consensus_state::CometConsensusState;
Expand All @@ -28,7 +29,7 @@ impl<Chain, Counterparty, Encoding> CreateClientMessageBuilder<Chain, Counterpar
for BuildCreateCometClientMessage
where
Chain: HasCreateClientMessageOptionsType<Counterparty>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasEncoding<AsFelt, Encoding = Encoding>
+ CanQueryContractAddress<symbol!("ibc_core_contract_address")>
Expand Down Expand Up @@ -88,6 +89,8 @@ where
calldata,
};

Ok(call)
let message = StarknetMessage::new(call);

Ok(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use starknet::accounts::Call;
use starknet::core::types::Felt;
use starknet::macros::selector;

use crate::impls::types::message::StarknetMessage;
use crate::traits::queries::address::CanQueryContractAddress;
use crate::types::client_id::ClientId;
use crate::types::cosmos::update::CometUpdateHeader;
Expand All @@ -26,7 +27,7 @@ impl<Chain, Counterparty, Encoding> UpdateClientMessageBuilder<Chain, Counterpar
for BuildUpdateCometClientMessage
where
Chain: HasCreateClientMessageOptionsType<Counterparty>
+ HasMessageType<Message = Call>
+ HasMessageType<Message = StarknetMessage>
+ HasAddressType<Address = Felt>
+ HasClientIdType<Counterparty, ClientId = ClientId>
+ HasEncoding<AsFelt, Encoding = Encoding>
Expand Down Expand Up @@ -60,6 +61,8 @@ where
calldata,
};

Ok(vec![call])
let message = StarknetMessage::new(call);

Ok(vec![message])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use starknet::core::types::{
ExecuteInvocation, FunctionInvocation, RevertedInvocation, TransactionTrace,
};

use crate::impls::types::message::StarknetMessage;
use crate::types::event::StarknetEvent;
use crate::types::message_response::StarknetMessageResponse;
use crate::types::tx_response::TxResponse;
Expand All @@ -24,7 +25,7 @@ pub struct UnexpectedTransactionTraceType {

impl<Chain> MessageSender<Chain> for SendCallMessages
where
Chain: HasMessageType<Message = Call>
Chain: HasMessageType<Message = StarknetMessage>
+ CanSubmitTx<Transaction = Vec<Call>>
+ HasTxResponseType<TxResponse = TxResponse>
+ HasMessageResponseType<MessageResponse = StarknetMessageResponse>
Expand All @@ -34,9 +35,13 @@ where
{
async fn send_messages(
chain: &Chain,
messages: Vec<Call>,
messages: Vec<StarknetMessage>,
) -> Result<Vec<StarknetMessageResponse>, Chain::Error> {
let tx_hash = chain.submit_tx(&messages).await?;
let calls: Vec<Call> = messages
.iter()
.map(|message| message.call.clone())
.collect();
let tx_hash = chain.submit_tx(&calls).await?;

let tx_response = chain.poll_tx_response(&tx_hash).await?;

Expand Down
Loading

0 comments on commit a151c39

Please sign in to comment.