Skip to content

Commit

Permalink
avoid EnumIter
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmovses committed May 21, 2024
1 parent efa31e6 commit 023fd36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
24 changes: 15 additions & 9 deletions rust/agents/scraper/src/db/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,31 @@ impl ScraperDb {
origin_domain: u32,
origin_mailbox: &H256,
) -> Result<Option<u32>> {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
#[derive(Copy, Clone, Debug, DeriveColumn)]
enum QueryAs {
Nonce,
}

let last_nonce = message::Entity::find()
.filter(message::Column::Origin.eq(origin_domain))
.filter(message::Column::OriginMailbox.eq(address_to_bytes(origin_mailbox)))
.select_only()
.column_as(message::Column::Nonce.max(), QueryAs::Nonce)
.into_values::<i32, QueryAs>()
.into_tuple::<(Option<i32>,)>()
.one(&self.0)
.await?
.map(|idx| idx as u32);
.map(|t| t.0.map(|idx| idx as u32))
.flatten();

debug!(
?last_nonce,
origin_domain,
?origin_mailbox,
"Queried last message nonce from database"
);

Ok(last_nonce)
}
}

/// Get the dispatched message associated with a nonce.
#[instrument(skip(self))]
Expand All @@ -66,7 +69,7 @@ impl ScraperDb {
origin_mailbox: &H256,
nonce: u32,
) -> Result<Option<HyperlaneMessage>> {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
#[derive(Copy, Clone, Debug, DeriveColumn)]
enum QueryAs {
Nonce,
}
Expand Down Expand Up @@ -100,7 +103,7 @@ impl ScraperDb {
origin_mailbox: &H256,
nonce: u32,
) -> Result<Option<i64>> {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
#[derive(Copy, Clone, Debug, DeriveColumn)]
enum QueryAs {
Nonce,
}
Expand All @@ -112,10 +115,13 @@ impl ScraperDb {
.select_only()
.column_as(message::Column::OriginTxId.max(), QueryAs::Nonce)
.group_by(message::Column::Origin)
.into_values::<i64, QueryAs>()
.into_tuple::<(Option<i64>,)>()
.one(&self.0)
.await?;
Ok(tx_id)

// a bit of a nasty use of flatten but this gets around the issue of
// not being able to derive EnumIter for Query As.
Ok(tx_id.map(|t| t.0).flatten())
}

async fn deliveries_count(&self, domain: u32, destination_mailbox: Vec<u8>) -> Result<u64> {
Expand Down
4 changes: 4 additions & 0 deletions rust/hyperlane-base/src/contract_sync/cursors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Indexable for HyperlaneMessage {
HyperlaneDomainProtocol::Fuel => todo!(),
HyperlaneDomainProtocol::Sealevel => CursorType::SequenceAware,
HyperlaneDomainProtocol::Cosmos => CursorType::SequenceAware,
HyperlaneDomainProtocol::Aptos => CursorType::SequenceAware,
}
}
}
Expand All @@ -35,6 +36,7 @@ impl Indexable for InterchainGasPayment {
HyperlaneDomainProtocol::Fuel => todo!(),
HyperlaneDomainProtocol::Sealevel => CursorType::SequenceAware,
HyperlaneDomainProtocol::Cosmos => CursorType::RateLimited,
HyperlaneDomainProtocol::Aptos => CursorType::RateLimited,
}
}
}
Expand All @@ -46,6 +48,7 @@ impl Indexable for MerkleTreeInsertion {
HyperlaneDomainProtocol::Fuel => todo!(),
HyperlaneDomainProtocol::Sealevel => CursorType::SequenceAware,
HyperlaneDomainProtocol::Cosmos => CursorType::SequenceAware,
HyperlaneDomainProtocol::Aptos => CursorType::SequenceAware,
}
}
}
Expand All @@ -57,6 +60,7 @@ impl Indexable for Delivery {
HyperlaneDomainProtocol::Fuel => todo!(),
HyperlaneDomainProtocol::Sealevel => CursorType::SequenceAware,
HyperlaneDomainProtocol::Cosmos => CursorType::RateLimited,
HyperlaneDomainProtocol::Aptos => CursorType::RateLimited,
}
}
}

0 comments on commit 023fd36

Please sign in to comment.