Skip to content

Commit

Permalink
fixes after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoyan committed Jan 9, 2025
1 parent e66a4d4 commit 35712fe
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 49 deletions.
8 changes: 5 additions & 3 deletions ampd/src/handlers/starknet_verify_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use itertools::Itertools;
use router_api::ChainName;
use serde::Deserialize;
use starknet_checked_felt::CheckedFelt;
use starknet_core::types::Felt;
use starknet_types::events::contract_call::ContractCallEvent;
use tokio::sync::watch::Receiver;
use tracing::info;
Expand Down Expand Up @@ -132,7 +133,7 @@ where
.collect::<Vec<_>>();

// key is the tx_hash of the tx holding the event
let events: HashMap<CheckedFelt, ContractCallEvent> =
let events: HashMap<Felt, ContractCallEvent> =
try_join_all(unique_msgs.iter().map(|msg| {
self.rpc_client
.get_event_by_hash_contract_call(msg.message_id.tx_hash.clone())
Expand Down Expand Up @@ -172,6 +173,7 @@ mod tests {
use ethers_core::types::H256;
use events::Event;
use mockall::predicate::eq;
use starknet_core::types::Felt;
use tendermint::abci;
use tokio::sync::watch;
use tokio::test as async_test;
Expand All @@ -195,7 +197,7 @@ mod tests {
.expect_get_event_by_hash_contract_call()
.returning(|_| {
Ok(Some((
CheckedFelt::from_str(
Felt::from_str(
"0x035410be6f4bf3f67f7c1bb4a93119d9d410b2f981bfafbf5dbbf5d37ae7439e",
)
.unwrap(),
Expand Down Expand Up @@ -244,7 +246,7 @@ mod tests {
.unwrap()))
.returning(|_| {
Ok(Some((
CheckedFelt::from_str(
Felt::from_str(
"0x045410be6f4bf3f67f7c1bb4a93119d9d410b2f981bfafbf5dbbf5d37ae7439f",
)
.unwrap(),
Expand Down
88 changes: 63 additions & 25 deletions ampd/src/starknet/json_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use async_trait::async_trait;
use error_stack::Report;
use mockall::automock;
use starknet_core::types::{ExecutionResult, FromStrError, TransactionReceipt};
use starknet_checked_felt::CheckedFelt;
use starknet_core::types::{ExecutionResult, Felt, FromStrError, TransactionReceipt};
use starknet_providers::jsonrpc::JsonRpcTransport;
use starknet_providers::{JsonRpcClient, Provider, ProviderError};
use starknet_types::events::contract_call::ContractCallEvent;
Expand Down Expand Up @@ -62,14 +62,14 @@ pub trait StarknetClient {
async fn get_event_by_hash_contract_call(
&self,
tx_hash: CheckedFelt,
) -> Result<Option<(CheckedFelt, ContractCallEvent)>>;
) -> Result<Option<(Felt, ContractCallEvent)>>;

/// Attempts to fetch a SignersRotated event, by a given `tx_hash`.
/// Returns a tuple `(tx_hash, event)` or a `StarknetClientError`.
async fn get_event_by_hash_signers_rotated(
&self,
tx_hash: CheckedFelt,
) -> Result<Option<(CheckedFelt, SignersRotatedEvent)>>;
) -> Result<Option<(Felt, SignersRotatedEvent)>>;
}

#[async_trait]
Expand All @@ -80,7 +80,7 @@ where
async fn get_event_by_hash_contract_call(
&self,
tx_hash: CheckedFelt,
) -> Result<Option<(CheckedFelt, ContractCallEvent)>> {
) -> Result<Option<(Felt, ContractCallEvent)>> {
let receipt_with_block_info = self
.client
.get_transaction_receipt(tx_hash)
Expand All @@ -91,7 +91,7 @@ where
return Err(Report::new(StarknetClientError::UnsuccessfulTx));
}

let event: Option<(CheckedFelt, ContractCallEvent)> = match receipt_with_block_info.receipt {
let event: Option<(Felt, ContractCallEvent)> = match receipt_with_block_info.receipt {
TransactionReceipt::Invoke(tx) => {
// NOTE: There should be only one ContractCall event per gateway tx
tx.events
Expand All @@ -100,7 +100,7 @@ where
// NOTE: Here we ignore the error, because the event might
// not be ContractCall and that by itself is not erroneous behavior
if let Ok(cce) = ContractCallEvent::try_from(e.clone()) {
Some((tx.transaction_hash.into(), cce))
Some((tx.transaction_hash, cce))
} else {
None
}
Expand Down Expand Up @@ -136,7 +136,7 @@ where
async fn get_event_by_hash_signers_rotated(
&self,
tx_hash: CheckedFelt,
) -> Result<Option<(CheckedFelt, SignersRotatedEvent)>> {
) -> Result<Option<(Felt, SignersRotatedEvent)>> {
let receipt_with_block_info = self
.client
.get_transaction_receipt(tx_hash)
Expand All @@ -147,13 +147,13 @@ where
return Err(Report::new(StarknetClientError::UnsuccessfulTx));
}

let event: Option<(CheckedFelt, SignersRotatedEvent)> = match receipt_with_block_info.receipt {
let event: Option<(Felt, SignersRotatedEvent)> = match receipt_with_block_info.receipt {
TransactionReceipt::Invoke(tx) => tx
.events
.iter()
.filter_map(|e| {
if let Ok(sre) = SignersRotatedEvent::try_from(e.clone()) {
Some((tx.transaction_hash.into(), sre))
Some((tx.transaction_hash, sre))
} else {
None
}
Expand All @@ -178,8 +178,8 @@ mod test {
use ethers_core::types::H256;
use serde::de::DeserializeOwned;
use serde::Serialize;
use starknet_core::types::Felt;
use starknet_checked_felt::CheckedFelt;
use starknet_core::types::Felt;
use starknet_providers::jsonrpc::{
HttpTransportError, JsonRpcMethod, JsonRpcResponse, JsonRpcTransport,
};
Expand All @@ -194,7 +194,9 @@ mod test {
let mock_client =
Client::new_with_transport(InvalidSignersRotatedEventMockTransport).unwrap();
let contract_call_event = mock_client
.get_event_by_hash_signers_rotated(Felt::ONE.into())
.get_event_by_hash_signers_rotated(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
Expand All @@ -203,31 +205,47 @@ mod test {
#[tokio::test]
async fn deploy_account_tx_fetch() {
let mock_client = Client::new_with_transport(DeployAccountMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}

#[tokio::test]
async fn deploy_tx_fetch() {
let mock_client = Client::new_with_transport(DeployMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}

#[tokio::test]
async fn l1_handler_tx_fetch() {
let mock_client = Client::new_with_transport(L1HandlerMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}

#[tokio::test]
async fn declare_tx_fetch() {
let mock_client = Client::new_with_transport(DeclareMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}
Expand All @@ -236,23 +254,35 @@ mod test {
async fn invalid_contract_call_event_tx_fetch() {
let mock_client =
Client::new_with_transport(InvalidContractCallEventMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}

#[tokio::test]
async fn no_events_tx_fetch() {
let mock_client = Client::new_with_transport(NoEventsMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.unwrap().is_none());
}

#[tokio::test]
async fn reverted_tx_fetch() {
let mock_client = Client::new_with_transport(RevertedMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event
.unwrap_err()
Expand All @@ -262,23 +292,29 @@ mod test {
#[tokio::test]
async fn failing_tx_fetch() {
let mock_client = Client::new_with_transport(FailingMockTransport).unwrap();
let contract_call_event = mock_client.get_event_by_hash_contract_call(Felt::ONE.into()).await;
let contract_call_event = mock_client
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await;

assert!(contract_call_event.is_err());
}

#[tokio::test]
async fn successful_signers_rotated_tx_fetch() {
let mock_client = Client::new_with_transport(ValidMockTransportSignersRotated).unwrap();
let signers_rotated_event: (CheckedFelt, SignersRotatedEvent) = mock_client
.get_event_by_hash_signers_rotated(Felt::ONE.into())
let signers_rotated_event: (Felt, SignersRotatedEvent) = mock_client
.get_event_by_hash_signers_rotated(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await
.unwrap() // unwrap the result
.unwrap(); // unwrap the option

assert_eq!(
signers_rotated_event.0,
CheckedFelt::from_str("0x0000000000000000000000000000000000000000000000000000000000000001")
Felt::from_str("0x0000000000000000000000000000000000000000000000000000000000000001")
.unwrap()
);

Expand Down Expand Up @@ -310,14 +346,16 @@ mod test {
async fn successful_call_contract_tx_fetch() {
let mock_client = Client::new_with_transport(ValidMockTransportCallContract).unwrap();
let contract_call_event = mock_client
.get_event_by_hash_contract_call(Felt::ONE.into())
.get_event_by_hash_contract_call(
CheckedFelt::try_from(&Felt::ONE.to_bytes_be()).unwrap(),
)
.await
.unwrap() // unwrap the result
.unwrap(); // unwrap the option

assert_eq!(
contract_call_event.0,
CheckedFelt::from_str("0x0000000000000000000000000000000000000000000000000000000000000001")
Felt::from_str("0x0000000000000000000000000000000000000000000000000000000000000001")
.unwrap()
);
assert_eq!(
Expand All @@ -327,7 +365,7 @@ mod test {
"0x0000000000000000000000000000000000000000000000000000000000000002".to_owned(),
destination_address: String::from("hello"),
destination_chain: String::from("destination_chain"),
source_address: CheckedFelt::from_str(
source_address: Felt::from_str(
"0x00b3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca"
)
.unwrap(),
Expand Down
9 changes: 5 additions & 4 deletions ampd/src/starknet/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axelar_wasm_std::voting::Vote;
use cosmwasm_std::HexBinary;
use starknet_core::types::Felt;
use starknet_types::events::contract_call::ContractCallEvent;
use starknet_types::events::signers_rotated::SignersRotatedEvent;

Expand All @@ -26,7 +27,7 @@ pub fn verify_msg(

impl PartialEq<Message> for ContractCallEvent {
fn eq(&self, axl_msg: &Message) -> bool {
axl_msg.source_address == self.source_address.into()
Felt::from(axl_msg.source_address.clone()) == self.source_address
&& axl_msg.destination_chain == self.destination_chain
&& axl_msg.destination_address == self.destination_address
&& axl_msg.payload_hash == self.payload_hash
Expand Down Expand Up @@ -95,8 +96,8 @@ mod tests {
use multisig::msg::Signer;
use multisig::verifier_set::VerifierSet;
use router_api::ChainName;
use starknet_core::types::Felt;
use starknet_checked_felt::CheckedFelt;
use starknet_core::types::Felt;
use starknet_types::events::contract_call::ContractCallEvent;
use starknet_types::events::signers_rotated::{
Signer as StarknetSigner, SignersRotatedEvent, WeightedSigners,
Expand All @@ -118,7 +119,7 @@ mod tests {
),
destination_address: String::from("destination_address"),
destination_chain: String::from("ethereum"),
source_address: CheckedFelt::from_str(
source_address: Felt::from_str(
"0x00b3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca",
)
.unwrap(),
Expand Down Expand Up @@ -205,7 +206,7 @@ mod tests {
assert_eq!(verify_msg(&event, &msg, &source_gw_address), Vote::NotFound);

let mut msg = { mock_valid_message() };
msg.source_address = Felt::THREE.into();
msg.source_address = CheckedFelt::try_from(&Felt::THREE.to_bytes_be()).unwrap();
assert_eq!(verify_msg(&event, &msg, &source_gw_address), Vote::NotFound);

let mut msg = { mock_valid_message() };
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tokio = { version = "1", features = [
"macros",
] }
rand = { workspace = true }
futures = { workspace = true }
# futures = { workspace = true }

[lints]
workspace = true
Loading

0 comments on commit 35712fe

Please sign in to comment.