Skip to content

Commit

Permalink
feat: Store origin, destination, interchain_gas_paymaster and sequenc…
Browse files Browse the repository at this point in the history
…e on gas payment (#4933)

### Description

Store origin, destination, interchain_gas_paymaster and sequence on gas
payment

### Related issues

- Contributes into
#4271

### Backward compatibility

Yes (after the fields are added to database schema)

### Testing

E2E Ethereum and Sealevel tests (for no regressions).

Co-authored-by: Danil Nemirovsky <[email protected]>
  • Loading branch information
ameten and ameten authored Dec 3, 2024
1 parent 170a0fc commit ae2c3a8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::BorrowMut as _;

use sea_orm::ConnectionTrait;
use sea_orm_migration::prelude::*;

Expand Down Expand Up @@ -39,6 +41,13 @@ impl MigrationTrait for Migration {
.big_unsigned()
.not_null(),
)
.col(ColumnDef::new(GasPayment::Origin).unsigned())
.col(ColumnDef::new(GasPayment::Destination).unsigned())
.col(
ColumnDef::new_with_type(GasPayment::InterchainGasPaymaster, Address)
.borrow_mut(),
)
.col(ColumnDef::new(GasPayment::Sequence).big_integer())
.foreign_key(
ForeignKey::create()
.from_col(GasPayment::TxId)
Expand All @@ -49,6 +58,16 @@ impl MigrationTrait for Migration {
.from_col(GasPayment::Domain)
.to(Domain::Table, Domain::Id),
)
.foreign_key(
ForeignKey::create()
.from_col(GasPayment::Origin)
.to(Domain::Table, Domain::Id),
)
.foreign_key(
ForeignKey::create()
.from_col(GasPayment::Destination)
.to(Domain::Table, Domain::Id),
)
.index(
Index::create()
// don't need domain because TxId includes it
Expand Down Expand Up @@ -136,6 +155,16 @@ pub enum GasPayment {
/// Used to disambiguate duplicate payments from multiple payments made in
/// same transaction.
LogIndex,
/// Domain ID of the chain the payment was made on; technically duplicating
/// field Domain, but Domain becomes ambiguous as we add Destination domain as well.
Origin,
/// Domain ID of the chain the payment was made for.
Destination,
/// Interchain Gas Paymaster contract address
InterchainGasPaymaster,
/// Sequence of this payment for indexing by agent. It can be null if agent
/// does not use sequence-aware indexing.
Sequence,
}

#[derive(Iden)]
Expand Down
3 changes: 2 additions & 1 deletion rust/main/agents/scraper/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ impl BaseAgent for Scraper {
let chain_setup = settings.chain_setup(domain).expect("Missing chain config");
let store = HyperlaneDbStore::new(
db.clone(),
chain_setup.addresses.mailbox,
domain.clone(),
chain_setup.addresses.mailbox,
chain_setup.addresses.interchain_gas_paymaster,
settings
.build_provider(domain, &metrics.clone())
.await?
Expand Down
26 changes: 26 additions & 0 deletions rust/main/agents/scraper/src/db/generated/gas_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct Model {
pub gas_amount: BigDecimal,
pub tx_id: i64,
pub log_index: i64,
pub origin: Option<i32>,
pub destination: Option<i32>,
pub interchain_gas_paymaster: Option<Vec<u8>>,
pub sequence: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
Expand All @@ -33,6 +37,10 @@ pub enum Column {
GasAmount,
TxId,
LogIndex,
Origin,
Destination,
InterchainGasPaymaster,
Sequence,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
Expand All @@ -49,7 +57,9 @@ impl PrimaryKeyTrait for PrimaryKey {

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Destination,
Domain,
Origin,
Transaction,
}

Expand All @@ -65,17 +75,33 @@ impl ColumnTrait for Column {
Self::GasAmount => ColumnType::Decimal(Some((78u32, 0u32))).def(),
Self::TxId => ColumnType::BigInteger.def(),
Self::LogIndex => ColumnType::BigInteger.def(),
Self::Origin => ColumnType::Integer.def().null(),
Self::Destination => ColumnType::Integer.def().null(),
Self::InterchainGasPaymaster => {
ColumnType::Binary(sea_orm::sea_query::BlobSize::Blob(None))
.def()
.null()
}
Self::Sequence => ColumnType::BigInteger.def().null(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Destination => Entity::belongs_to(super::domain::Entity)
.from(Column::Destination)
.to(super::domain::Column::Id)
.into(),
Self::Domain => Entity::belongs_to(super::domain::Entity)
.from(Column::Domain)
.to(super::domain::Column::Id)
.into(),
Self::Origin => Entity::belongs_to(super::domain::Entity)
.from(Column::Origin)
.to(super::domain::Column::Id)
.into(),
Self::Transaction => Entity::belongs_to(super::transaction::Entity)
.from(Column::TxId)
.to(super::transaction::Column::Id)
Expand Down
13 changes: 12 additions & 1 deletion rust/main/agents/scraper/src/db/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use itertools::Itertools;
use sea_orm::{prelude::*, ActiveValue::*, Insert, QuerySelect};
use tracing::{debug, instrument, trace};

use hyperlane_core::{h256_to_bytes, InterchainGasPayment, LogMeta};
use hyperlane_core::{address_to_bytes, h256_to_bytes, InterchainGasPayment, LogMeta, H256};
use migration::OnConflict;

use crate::conversions::u256_to_decimal;
Expand All @@ -14,6 +14,7 @@ use super::generated::gas_payment;

pub struct StorablePayment<'a> {
pub payment: &'a InterchainGasPayment,
pub sequence: Option<i64>,
pub meta: &'a LogMeta,
/// The database id of the transaction the payment was made in
pub txn_id: i64,
Expand All @@ -24,9 +25,11 @@ impl ScraperDb {
pub async fn store_payments(
&self,
domain: u32,
interchain_gas_paymaster: &H256,
payments: impl Iterator<Item = StorablePayment<'_>>,
) -> Result<u64> {
let latest_id_before = self.latest_payment_id(domain).await?;
let interchain_gas_paymaster = address_to_bytes(interchain_gas_paymaster);

// we have a race condition where a message may not have been scraped yet even
let models = payments
Expand All @@ -39,6 +42,10 @@ impl ScraperDb {
gas_amount: Set(u256_to_decimal(storable.payment.gas_amount)),
tx_id: Unchanged(storable.txn_id),
log_index: Unchanged(storable.meta.log_index.as_u64() as i64),
origin: Set(Some(domain as i32)),
destination: Set(Some(storable.payment.destination as i32)),
interchain_gas_paymaster: Set(Some(interchain_gas_paymaster.clone())),
sequence: Set(storable.sequence),
})
.collect_vec();

Expand All @@ -61,6 +68,10 @@ impl ScraperDb {
gas_payment::Column::TimeCreated,
gas_payment::Column::Payment,
gas_payment::Column::GasAmount,
gas_payment::Column::Origin,
gas_payment::Column::Destination,
gas_payment::Column::InterchainGasPaymaster,
gas_payment::Column::Sequence,
])
.to_owned(),
)
Expand Down
22 changes: 18 additions & 4 deletions rust/main/agents/scraper/src/store/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,30 @@ impl HyperlaneLogStore<InterchainGasPayment> for HyperlaneDbStore {
let storable = payments
.iter()
.filter_map(|(payment, meta)| {
txns.get(&meta.transaction_id)
.map(|txn| (payment.inner(), meta, txn.id))
txns.get(&meta.transaction_id).map(|txn| {
(
payment.inner(),
payment.sequence.map(|v| v as i64),
meta,
txn.id,
)
})
})
.map(|(payment, meta, txn_id)| StorablePayment {
.map(|(payment, sequence, meta, txn_id)| StorablePayment {
payment,
sequence,
meta,
txn_id,
});

let stored = self.db.store_payments(self.domain.id(), storable).await?;
let stored = self
.db
.store_payments(
self.domain.id(),
&self.interchain_gas_paymaster_address,
storable,
)
.await?;
Ok(stored as u32)
}
}
11 changes: 7 additions & 4 deletions rust/main/agents/scraper/src/store/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const CHUNK_SIZE: usize = 50;
/// connections needed to scrape the contracts on a single blockchain.
#[derive(Clone, Debug)]
pub struct HyperlaneDbStore {
pub(crate) mailbox_address: H256,
pub(crate) domain: HyperlaneDomain,
pub(crate) db: ScraperDb,
pub(crate) domain: HyperlaneDomain,
pub(crate) mailbox_address: H256,
pub(crate) interchain_gas_paymaster_address: H256,
provider: Arc<dyn HyperlaneProvider>,
cursor: Arc<BlockCursor>,
}
Expand All @@ -39,8 +40,9 @@ pub struct HyperlaneDbStore {
impl HyperlaneDbStore {
pub async fn new(
db: ScraperDb,
mailbox_address: H256,
domain: HyperlaneDomain,
mailbox_address: H256,
interchain_gas_paymaster_address: H256,
provider: Arc<dyn HyperlaneProvider>,
index_settings: &IndexSettings,
) -> Result<Self> {
Expand All @@ -51,8 +53,9 @@ impl HyperlaneDbStore {
Ok(Self {
db,
domain,
provider,
mailbox_address,
interchain_gas_paymaster_address,
provider,
cursor,
})
}
Expand Down

0 comments on commit ae2c3a8

Please sign in to comment.