From ff41bad7e2363fbae5d85c64a3f0970681247fb7 Mon Sep 17 00:00:00 2001 From: Danil Nemirovsky <4614623+ameten@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:33:39 +0000 Subject: [PATCH] Calculate log index from transaction itself --- .../hyperlane-sealevel/src/interchain_gas.rs | 22 ++++++----- .../src/log_meta_composer.rs | 39 +++++++++++-------- .../chains/hyperlane-sealevel/src/mailbox.rs | 18 +-------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/rust/main/chains/hyperlane-sealevel/src/interchain_gas.rs b/rust/main/chains/hyperlane-sealevel/src/interchain_gas.rs index 9a38fb487..69bef48cf 100644 --- a/rust/main/chains/hyperlane-sealevel/src/interchain_gas.rs +++ b/rust/main/chains/hyperlane-sealevel/src/interchain_gas.rs @@ -12,7 +12,7 @@ use tracing::{info, instrument}; use hyperlane_core::{ config::StrOrIntParseError, ChainCommunicationError, ChainResult, ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain, HyperlaneProvider, Indexed, Indexer, - InterchainGasPaymaster, InterchainGasPayment, LogMeta, SequenceAwareIndexer, H256, U256, + InterchainGasPaymaster, InterchainGasPayment, LogMeta, SequenceAwareIndexer, H256, }; use crate::account::{search_accounts_by_discriminator, search_and_validate_account}; @@ -150,10 +150,19 @@ impl SealevelInterchainGasPaymasterIndexer { self.interchain_payment_account(account) })?; + self.sealevel_gas_payment(sequence_number, &valid_payment_pda_pubkey) + .await + } + + async fn sealevel_gas_payment( + &self, + sequence_number: u64, + payment_pda_pubkey: &Pubkey, + ) -> ChainResult { // Now that we have the valid gas payment PDA pubkey, we can get the full account data. let account = self .rpc_client - .get_account_with_finalized_commitment(&valid_payment_pda_pubkey) + .get_account_with_finalized_commitment(payment_pda_pubkey) .await?; let gas_payment_account = GasPaymentAccount::fetch(&mut account.data.as_ref()) .map_err(ChainCommunicationError::from_other)? @@ -169,11 +178,7 @@ impl SealevelInterchainGasPaymasterIndexer { }; let log_meta = self - .interchain_payment_log_meta( - U256::from(sequence_number), - &valid_payment_pda_pubkey, - &gas_payment_account.slot, - ) + .interchain_payment_log_meta(payment_pda_pubkey, &gas_payment_account.slot) .await?; Ok(SealevelGasPayment::new( @@ -189,14 +194,13 @@ impl SealevelInterchainGasPaymasterIndexer { async fn interchain_payment_log_meta( &self, - log_index: U256, payment_pda_pubkey: &Pubkey, payment_pda_slot: &Slot, ) -> ChainResult { let block = self.rpc_client.get_block(*payment_pda_slot).await?; self.log_meta_composer - .log_meta(block, log_index, payment_pda_pubkey, payment_pda_slot) + .log_meta(block, payment_pda_pubkey, payment_pda_slot) .map_err(Into::::into) } diff --git a/rust/main/chains/hyperlane-sealevel/src/log_meta_composer.rs b/rust/main/chains/hyperlane-sealevel/src/log_meta_composer.rs index f530b04e0..a6bae3e2c 100644 --- a/rust/main/chains/hyperlane-sealevel/src/log_meta_composer.rs +++ b/rust/main/chains/hyperlane-sealevel/src/log_meta_composer.rs @@ -36,7 +36,6 @@ impl LogMetaComposer { pub fn log_meta( &self, block: UiConfirmedBlock, - log_index: U256, pda_pubkey: &Pubkey, pda_slot: &Slot, ) -> Result { @@ -64,15 +63,17 @@ impl LogMetaComposer { )))? } - let (transaction_index, transaction_hash) = - transaction_hashes - .into_iter() - .next() - .ok_or(HyperlaneSealevelError::NoTransactions(format!( + let (transaction_index, transaction_hash, program_index) = transaction_hashes + .into_iter() + .next() + .ok_or(HyperlaneSealevelError::NoTransactions(format!( "block which should contain {} transaction does not contain any after filtering", self.transaction_description, )))?; + // Construct log index which will be increasing relative to block + let log_index = U256::from((transaction_index << 8) + (program_index as usize)); + let log_meta = LogMeta { address: self.program_id.to_bytes().into(), block_number: *pda_slot, @@ -120,7 +121,8 @@ pub fn is_interchain_payment_instruction(instruction_data: &[u8]) -> bool { } /// This function searches for relevant transactions in the vector of provided transactions and -/// returns the relative index and hashes of such transactions. +/// returns the relative index and hashes of such transactions together with index of the relevant +/// instruction. /// /// This function takes a program identifier and the identifier for PDA and searches transactions /// which act upon this program and the PDA. @@ -144,16 +146,19 @@ fn search_transactions( program_id: &Pubkey, pda_pubkey: &Pubkey, is_specified_instruction: fn(&[u8]) -> bool, -) -> Vec<(usize, H512)> { +) -> Vec<(usize, H512, u8)> { transactions .into_iter() .enumerate() - .filter_map(|(index, tx)| filter_by_encoding(tx).map(|(tx, meta)| (index, tx, meta))) - .filter_map(|(index, tx, meta)| { - filter_by_validity(tx, meta) - .map(|(hash, account_keys, instructions)| (index, hash, account_keys, instructions)) + .filter_map(|(txn_index, tx)| { + filter_by_encoding(tx).map(|(tx, meta)| (txn_index, tx, meta)) + }) + .filter_map(|(txn_index, tx, meta)| { + filter_by_validity(tx, meta).map(|(hash, account_keys, instructions)| { + (txn_index, hash, account_keys, instructions) + }) }) - .filter_map(|(index, hash, account_keys, instructions)| { + .filter_map(|(txn_index, hash, account_keys, instructions)| { filter_by_relevancy( program_id, pda_pubkey, @@ -162,9 +167,9 @@ fn search_transactions( instructions, is_specified_instruction, ) - .map(|hash| (index, hash)) + .map(|(hash, program_index)| (txn_index, hash, program_index)) }) - .collect::>() + .collect::>() } fn filter_by_relevancy( @@ -174,7 +179,7 @@ fn filter_by_relevancy( account_keys: Vec, instructions: Vec, is_specified_instruction: fn(&[u8]) -> bool, -) -> Option { +) -> Option<(H512, u8)> { let account_index_map = account_index_map(account_keys); let program_id_str = program_id.to_string(); @@ -213,7 +218,7 @@ fn filter_by_relevancy( return None; } - Some(hash) + Some((hash, program.program_id_index)) } fn filter_by_validity( diff --git a/rust/main/chains/hyperlane-sealevel/src/mailbox.rs b/rust/main/chains/hyperlane-sealevel/src/mailbox.rs index 21a9ff28b..f3fa2ffe6 100644 --- a/rust/main/chains/hyperlane-sealevel/src/mailbox.rs +++ b/rust/main/chains/hyperlane-sealevel/src/mailbox.rs @@ -718,7 +718,6 @@ impl SealevelMailboxIndexer { let log_meta = self .dispatch_message_log_meta( - U256::from(nonce), &valid_message_storage_pda_pubkey, &dispatched_message_account.slot, ) @@ -743,7 +742,6 @@ impl SealevelMailboxIndexer { async fn dispatch_message_log_meta( &self, - log_index: U256, message_storage_pda_pubkey: &Pubkey, message_account_slot: &Slot, ) -> ChainResult { @@ -755,12 +753,7 @@ impl SealevelMailboxIndexer { .await?; self.dispatch_message_log_meta_composer - .log_meta( - block, - log_index, - message_storage_pda_pubkey, - message_account_slot, - ) + .log_meta(block, message_storage_pda_pubkey, message_account_slot) .map_err(Into::::into) } @@ -800,7 +793,6 @@ impl SealevelMailboxIndexer { let log_meta = self .delivered_message_log_meta( - U256::from(nonce), &valid_message_storage_pda_pubkey, &delivered_message_account.slot, ) @@ -823,7 +815,6 @@ impl SealevelMailboxIndexer { async fn delivered_message_log_meta( &self, - log_index: U256, message_storage_pda_pubkey: &Pubkey, message_account_slot: &Slot, ) -> ChainResult { @@ -835,12 +826,7 @@ impl SealevelMailboxIndexer { .await?; self.delivery_message_log_meta_composer - .log_meta( - block, - log_index, - message_storage_pda_pubkey, - message_account_slot, - ) + .log_meta(block, message_storage_pda_pubkey, message_account_slot) .map_err(Into::::into) } }