refactor: Refactor Hyperlane store for Scraper (#4855)
### Description Refactor Hyperlane store for Scraper. Just moved source code into submodules. ### Backward compatibility Yes ### Testing E2E test Ethereum and Sealevel --------- Co-authored-by: Danil Nemirovsky <4614623+ameten@users.noreply.github.com>pull/4862/head
parent
25a927de3a
commit
57346defe3
@ -0,0 +1,6 @@ |
|||||||
|
pub use storage::HyperlaneDbStore; |
||||||
|
|
||||||
|
mod deliveries; |
||||||
|
mod dispatches; |
||||||
|
mod payments; |
||||||
|
mod storage; |
@ -0,0 +1,43 @@ |
|||||||
|
use std::collections::HashMap; |
||||||
|
|
||||||
|
use async_trait::async_trait; |
||||||
|
use eyre::Result; |
||||||
|
|
||||||
|
use hyperlane_core::{Delivery, HyperlaneLogStore, Indexed, LogMeta, H512}; |
||||||
|
|
||||||
|
use crate::db::StorableDelivery; |
||||||
|
use crate::store::storage::{HyperlaneDbStore, TxnWithId}; |
||||||
|
|
||||||
|
#[async_trait] |
||||||
|
impl HyperlaneLogStore<Delivery> for HyperlaneDbStore { |
||||||
|
/// Store delivered message ids from the destination mailbox into the database.
|
||||||
|
/// We store only delivered messages ids from blocks and transaction which we could successfully
|
||||||
|
/// insert into database.
|
||||||
|
async fn store_logs(&self, deliveries: &[(Indexed<Delivery>, LogMeta)]) -> Result<u32> { |
||||||
|
if deliveries.is_empty() { |
||||||
|
return Ok(0); |
||||||
|
} |
||||||
|
let txns: HashMap<H512, TxnWithId> = self |
||||||
|
.ensure_blocks_and_txns(deliveries.iter().map(|r| &r.1)) |
||||||
|
.await? |
||||||
|
.map(|t| (t.hash, t)) |
||||||
|
.collect(); |
||||||
|
let storable = deliveries |
||||||
|
.iter() |
||||||
|
.filter_map(|(message_id, meta)| { |
||||||
|
txns.get(&meta.transaction_id) |
||||||
|
.map(|txn| (*message_id.inner(), meta, txn.id)) |
||||||
|
}) |
||||||
|
.map(|(message_id, meta, txn_id)| StorableDelivery { |
||||||
|
message_id, |
||||||
|
meta, |
||||||
|
txn_id, |
||||||
|
}); |
||||||
|
|
||||||
|
let stored = self |
||||||
|
.db |
||||||
|
.store_deliveries(self.domain.id(), self.mailbox_address, storable) |
||||||
|
.await?; |
||||||
|
Ok(stored as u32) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
use std::collections::HashMap; |
||||||
|
|
||||||
|
use async_trait::async_trait; |
||||||
|
use eyre::Result; |
||||||
|
|
||||||
|
use hyperlane_core::{ |
||||||
|
unwrap_or_none_result, HyperlaneLogStore, HyperlaneMessage, |
||||||
|
HyperlaneSequenceAwareIndexerStoreReader, Indexed, LogMeta, H512, |
||||||
|
}; |
||||||
|
|
||||||
|
use crate::db::StorableMessage; |
||||||
|
use crate::store::storage::{HyperlaneDbStore, TxnWithId}; |
||||||
|
|
||||||
|
#[async_trait] |
||||||
|
impl HyperlaneLogStore<HyperlaneMessage> for HyperlaneDbStore { |
||||||
|
/// Store dispatched messages from the origin mailbox into the database.
|
||||||
|
/// We store only messages from blocks and transaction which we could successfully insert
|
||||||
|
/// into database.
|
||||||
|
async fn store_logs(&self, messages: &[(Indexed<HyperlaneMessage>, LogMeta)]) -> Result<u32> { |
||||||
|
if messages.is_empty() { |
||||||
|
return Ok(0); |
||||||
|
} |
||||||
|
let txns: HashMap<H512, TxnWithId> = self |
||||||
|
.ensure_blocks_and_txns(messages.iter().map(|r| &r.1)) |
||||||
|
.await? |
||||||
|
.map(|t| (t.hash, t)) |
||||||
|
.collect(); |
||||||
|
let storable = messages |
||||||
|
.iter() |
||||||
|
.filter_map(|(message, meta)| { |
||||||
|
txns.get(&meta.transaction_id) |
||||||
|
.map(|t| (message.inner().clone(), meta, t.id)) |
||||||
|
}) |
||||||
|
.map(|(msg, meta, txn_id)| StorableMessage { msg, meta, txn_id }); |
||||||
|
let stored = self |
||||||
|
.db |
||||||
|
.store_dispatched_messages(self.domain.id(), &self.mailbox_address, storable) |
||||||
|
.await?; |
||||||
|
Ok(stored as u32) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[async_trait] |
||||||
|
impl HyperlaneSequenceAwareIndexerStoreReader<HyperlaneMessage> for HyperlaneDbStore { |
||||||
|
/// Gets a message by its nonce.
|
||||||
|
async fn retrieve_by_sequence(&self, sequence: u32) -> Result<Option<HyperlaneMessage>> { |
||||||
|
let message = self |
||||||
|
.db |
||||||
|
.retrieve_message_by_nonce(self.domain.id(), &self.mailbox_address, sequence) |
||||||
|
.await?; |
||||||
|
Ok(message) |
||||||
|
} |
||||||
|
|
||||||
|
/// Gets the block number at which the log occurred.
|
||||||
|
async fn retrieve_log_block_number_by_sequence(&self, sequence: u32) -> Result<Option<u64>> { |
||||||
|
let tx_id = unwrap_or_none_result!( |
||||||
|
self.db |
||||||
|
.retrieve_dispatched_tx_id(self.domain.id(), &self.mailbox_address, sequence) |
||||||
|
.await? |
||||||
|
); |
||||||
|
let block_id = unwrap_or_none_result!(self.db.retrieve_block_id(tx_id).await?); |
||||||
|
Ok(self.db.retrieve_block_number(block_id).await?) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
use std::collections::HashMap; |
||||||
|
|
||||||
|
use async_trait::async_trait; |
||||||
|
use eyre::Result; |
||||||
|
|
||||||
|
use hyperlane_core::{HyperlaneLogStore, Indexed, InterchainGasPayment, LogMeta, H512}; |
||||||
|
|
||||||
|
use crate::db::StorablePayment; |
||||||
|
use crate::store::storage::HyperlaneDbStore; |
||||||
|
|
||||||
|
#[async_trait] |
||||||
|
impl HyperlaneLogStore<InterchainGasPayment> for HyperlaneDbStore { |
||||||
|
/// Store interchain gas payments into the database.
|
||||||
|
/// We store only interchain gas payments from blocks and transaction which we could
|
||||||
|
/// successfully insert into database.
|
||||||
|
async fn store_logs( |
||||||
|
&self, |
||||||
|
payments: &[(Indexed<InterchainGasPayment>, LogMeta)], |
||||||
|
) -> Result<u32> { |
||||||
|
if payments.is_empty() { |
||||||
|
return Ok(0); |
||||||
|
} |
||||||
|
let txns: HashMap<H512, crate::store::storage::TxnWithId> = self |
||||||
|
.ensure_blocks_and_txns(payments.iter().map(|r| &r.1)) |
||||||
|
.await? |
||||||
|
.map(|t| (t.hash, t)) |
||||||
|
.collect(); |
||||||
|
let storable = payments |
||||||
|
.iter() |
||||||
|
.filter_map(|(payment, meta)| { |
||||||
|
txns.get(&meta.transaction_id) |
||||||
|
.map(|txn| (payment.inner(), meta, txn.id)) |
||||||
|
}) |
||||||
|
.map(|(payment, meta, txn_id)| StorablePayment { |
||||||
|
payment, |
||||||
|
meta, |
||||||
|
txn_id, |
||||||
|
}); |
||||||
|
|
||||||
|
let stored = self.db.store_payments(self.domain.id(), storable).await?; |
||||||
|
Ok(stored as u32) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue