From 6751a12ec03b877e0d193c619456e0f74274402a Mon Sep 17 00:00:00 2001 From: Luca Joss <43531661+ljoss17@users.noreply.github.com> Date: Tue, 4 Mar 2025 01:17:16 +0100 Subject: [PATCH] Update `test_packet_clearing` (#328) * Use auto_bi_relay in test_packet_clearing * Assert acks have been relayed on Cosmos side * Fix test_packet_clearing following merge * use unshashed packet_ack * add comment * Fix test_packet_clearing using query packet commitment * starknet-devnet with full state archive capacity forgot in #332 --------- Co-authored-by: Ranadeep Biswas Co-authored-by: Rano | Ranadeep --- .../src/impls/queries/ack_commitment.rs | 13 +- .../src/tests/clear_packets.rs | 119 +++++++++++++++--- .../src/impls/bootstrap/start_chain.rs | 2 + 3 files changed, 114 insertions(+), 20 deletions(-) diff --git a/relayer/crates/starknet-chain-components/src/impls/queries/ack_commitment.rs b/relayer/crates/starknet-chain-components/src/impls/queries/ack_commitment.rs index eaaa9031..bcb16da3 100644 --- a/relayer/crates/starknet-chain-components/src/impls/queries/ack_commitment.rs +++ b/relayer/crates/starknet-chain-components/src/impls/queries/ack_commitment.rs @@ -35,6 +35,9 @@ use crate::types::membership_proof_signer::MembershipVerifierContainer; use crate::types::messages::ibc::channel::PortId as CairoPortId; use crate::types::messages::ibc::packet::Sequence; use crate::types::status::StarknetChainStatus; + +const SUCCESS_ACK: &[u8] = br#"{"result":"AQ=="}"#; + pub struct QueryStarknetAckCommitment; #[cgp_provider(PacketAcknowledgementQuerierComponent)] @@ -117,6 +120,14 @@ where proof_bytes: signed_bytes, }; - Ok((ack_bytes, dummy_proof)) + // assert sha256 hash of SUCCESS_ACK is stored ack_bytes + // asset_eq!(ack_bytes, SUCCESS_ACK); + + // FIXME: we can't send `ack_bytes` as it is stored after hashing. + // We should query block event to get the original ack_bytes. + // + // For now, we just return the SUCCESS_ACK as ack_bytes. + + Ok((SUCCESS_ACK.to_vec(), dummy_proof)) } } diff --git a/relayer/crates/starknet-integration-tests/src/tests/clear_packets.rs b/relayer/crates/starknet-integration-tests/src/tests/clear_packets.rs index efea9863..2a4d8876 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/clear_packets.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/clear_packets.rs @@ -16,10 +16,12 @@ use flate2::Compression; use hermes_chain_components::traits::packet::fields::{ HasPacketTimeoutHeight, HasPacketTimeoutTimestamp, }; -use hermes_chain_components::traits::queries::chain_status::CanQueryChainStatus; +use hermes_chain_components::traits::queries::chain_status::{ + CanQueryChainHeight, CanQueryChainStatus, +}; use hermes_chain_components::traits::queries::client_state::CanQueryClientStateWithLatestHeight; +use hermes_chain_components::traits::queries::packet_commitment::CanQueryPacketCommitment; use hermes_chain_components::traits::types::chain_id::HasChainId; -use hermes_chain_components::traits::types::timestamp::HasTimeoutType; use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions; use hermes_cosmos_chain_components::types::config::gas::dynamic_gas_config::DynamicGasConfig; use hermes_cosmos_chain_components::types::config::gas::eip_type::EipQueryType; @@ -84,7 +86,7 @@ use crate::contexts::bootstrap::StarknetBootstrap; use crate::contexts::osmosis_bootstrap::OsmosisBootstrap; #[test] -fn test_query_unreceived_packets() -> Result<(), Error> { +fn test_packet_clearing() -> Result<(), Error> { // ### SETUP START ### let runtime = init_test_runtime(); @@ -415,6 +417,7 @@ fn test_query_unreceived_packets() -> Result<(), Error> { let wallet_starknet_b = &starknet_chain_driver.user_wallet_b; let address_starknet_b = &wallet_starknet_b.account_address; let transfer_quantity = 1_000u128; + let transfer_back_quantity = 310u128; let denom_cosmos = &cosmos_chain_driver.genesis_config.transfer_denom; let starknet_account_b = SingleOwnerAccount::new( @@ -511,6 +514,16 @@ fn test_query_unreceived_packets() -> Result<(), Error> { cairo_encoding.decode(&output)? }; + let birelay = StarknetCosmosBiRelay { + runtime: runtime.clone(), + relay_a_to_b: starknet_to_cosmos_relay, + relay_b_to_a: cosmos_to_starknet_relay.clone(), + }; + + birelay + .auto_bi_relay(Some(Duration::from_secs(10)), Some(Duration::from_secs(0))) + .await?; + info!("ics20 token address: {:?}", ics20_token_address); let balance_cosmos_a_step_0 = cosmos_chain @@ -560,7 +573,7 @@ fn test_query_unreceived_packets() -> Result<(), Error> { { let call_data = cairo_encoding.encode(&product![ ics20_contract_address, - U256::from(transfer_quantity) + U256::from(transfer_back_quantity) ])?; let call = Call { @@ -596,7 +609,7 @@ fn test_query_unreceived_packets() -> Result<(), Error> { port_id_on_a: ics20_port.clone(), chan_id_on_a: starknet_channel_id.clone(), denom, - amount: transfer_quantity.into(), + amount: transfer_back_quantity.into(), receiver: address_cosmos_a.clone(), memo: String::new(), timeout_height_on_b: Height { @@ -643,17 +656,88 @@ fn test_query_unreceived_packets() -> Result<(), Error> { ) .await?; - // assert_eq!(pending_packets_starknet, vec![Sequence::from(3)]); - // TODO: Currently, querying for unreceived packets using commitment sequences that were never sent - // will always return them as unreceived. This assertion must be updated once the correct commitment- - // querying logic has been implemented. - // assert_eq!(pending_packets_cosmos, vec![Sequence::from(1), Sequence::from(2), Sequence::from(3)]); - // assert_eq!(pending_acks_starknet, vec![Sequence::from(1)]); - // assert_eq!(pending_acks_cosmos, vec![Sequence::from(2), Sequence::from(3)]); + let eventual_cosmos_amount = &Amount::new( + balance_cosmos_a_step_0.quantity - (transfer_quantity * 2) + transfer_back_quantity, + denom_cosmos.clone(), + ); + + birelay + .auto_bi_relay(Some(Duration::from_secs(10)), Some(Duration::from_secs(0))) + .await?; - // TODO: Call packet clearing - // TODO: Assert there are no pending packets or pending acks - // TODO: Assert tokens have been correctly transferred + cosmos_chain + .assert_eventual_amount(address_cosmos_a, eventual_cosmos_amount) + .await?; + + let balance_starknet_b_step_2 = starknet_chain + .query_token_balance(&ics20_token_address, address_starknet_b) + .await?; + + assert_eq!( + balance_starknet_b_step_0.quantity + (transfer_quantity * 2).into() + - transfer_back_quantity.into(), + balance_starknet_b_step_2.quantity + ); + + let cosmos_latest_height = cosmos_chain.query_chain_height().await?; + let starknet_latest_height = starknet_chain.query_chain_height().await?; + + let (cosmos_commitment1, _) = + >::query_packet_commitment( + cosmos_chain, + &cosmos_channel_id, + &IbcPortId::transfer(), + &Sequence::from(1), + &cosmos_latest_height, + ) + .await?; + + let (cosmos_commitment2, _) = + >::query_packet_commitment( + cosmos_chain, + &cosmos_channel_id, + &IbcPortId::transfer(), + &Sequence::from(2), + &cosmos_latest_height, + ) + .await?; + + let (cosmos_commitment3, _) = + >::query_packet_commitment( + cosmos_chain, + &cosmos_channel_id, + &IbcPortId::transfer(), + &Sequence::from(3), + &cosmos_latest_height, + ) + .await?; + + let (starknet_commitment1, _) = + >::query_packet_commitment( + &starknet_chain, + &starknet_channel_id, + &IbcPortId::transfer(), + &Sequence::from(1), + &starknet_latest_height, + ) + .await?; + + assert!( + cosmos_commitment1.is_none(), + "cosmos_commitment1 expected to be None" + ); + assert!( + cosmos_commitment2.is_none(), + "cosmos_commitment2 expected to be None" + ); + assert!( + cosmos_commitment3.is_none(), + "cosmos_commitment3 expected to be None" + ); + assert!( + starknet_commitment1.is_none(), + "starknet_commitment1 expected to be None" + ); Ok(()) }) @@ -1274,10 +1358,7 @@ where SrcChain: CanQueryChainStatus + HasPacketTimeoutHeight + HasPacketTimeoutTimestamp, - DstChain: CanQueryChainStatus - //+ HasChannelIdType - //+ HasPortIdType - + HasTimeoutType, + DstChain: CanQueryChainStatus, { let src_chain = relay.src_chain(); let dst_chain = relay.dst_chain(); diff --git a/relayer/crates/starknet-test-components/src/impls/bootstrap/start_chain.rs b/relayer/crates/starknet-test-components/src/impls/bootstrap/start_chain.rs index 2b4536af..608b9aeb 100644 --- a/relayer/crates/starknet-test-components/src/impls/bootstrap/start_chain.rs +++ b/relayer/crates/starknet-test-components/src/impls/bootstrap/start_chain.rs @@ -44,6 +44,8 @@ where &chain_node_config.rpc_port.to_string(), "--block-generation-on", "1", + "--state-archive-capacity", + "full", "--dump-on", "block", "--dump-path",