Skip to content

Commit

Permalink
[GraphQL/Data] TransactionBlock Point Queries (MystenLabs#15628)
Browse files Browse the repository at this point in the history
## Description

Port queries for TransactionBlock by digest into the new data loading
framework.

## Test Plan

```
sui-graphql-rpc$ cargo nextest run
sui-graphql-e2e-tests$ cargo nextest run -j 1 --features pg_nte
```

## Stack
- MystenLabs#15467 
- MystenLabs#15470 
- MystenLabs#15471 
- MystenLabs#15472 
- MystenLabs#15473
- MystenLabs#15474 
- MystenLabs#15475 
- MystenLabs#15484 
- MystenLabs#15485
- MystenLabs#15519
- MystenLabs#15520
- MystenLabs#15521
- MystenLabs#15522
- MystenLabs#15523
- MystenLabs#15524
- MystenLabs#15525
- MystenLabs#15526
- MystenLabs#15527
- MystenLabs#15625 
- MystenLabs#15528
- MystenLabs#15529
  • Loading branch information
amnn committed Jan 12, 2024
1 parent a9f26c0 commit f4b18af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 30 deletions.
1 change: 0 additions & 1 deletion crates/sui-graphql-rpc/src/context_data/db_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub(crate) type BalanceQuery<'a, DB> = BoxedSelectStatement<
>;

pub(crate) trait GenericQueryBuilder<DB: Backend> {
fn get_tx_by_digest(digest: Vec<u8>) -> transactions::BoxedQuery<'static, DB>;
fn get_obj(address: Vec<u8>, version: Option<i64>) -> objects::BoxedQuery<'static, DB>;
fn get_obj_by_type(object_type: String) -> objects::BoxedQuery<'static, DB>;
fn get_display_by_obj_type(object_type: String) -> display::BoxedQuery<'static, DB>;
Expand Down
18 changes: 0 additions & 18 deletions crates/sui-graphql-rpc/src/context_data/db_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ impl PgManager {

/// Implement methods to query db and return StoredData
impl PgManager {
async fn get_tx(&self, digest: Vec<u8>) -> Result<Option<StoredTransaction>, Error> {
self.run_query_async_with_cost(
move || Ok(QueryBuilder::get_tx_by_digest(digest.clone())),
|query| move |conn| query.get_result::<StoredTransaction>(conn).optional(),
)
.await
}

async fn get_obj(
&self,
address: Vec<u8>,
Expand Down Expand Up @@ -482,16 +474,6 @@ impl PgManager {
}
}

pub(crate) async fn fetch_tx(
&self,
digest: &Digest,
) -> Result<Option<TransactionBlock>, Error> {
self.get_tx(digest.to_vec())
.await?
.map(TransactionBlock::try_from)
.transpose()
}

/// Retrieve the validator APYs
pub(crate) async fn fetch_validator_apys(
&self,
Expand Down
5 changes: 0 additions & 5 deletions crates/sui-graphql-rpc/src/context_data/pg_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ pub(crate) const EXPLAIN_COSTING_LOG_TARGET: &str = "gql-explain-costing";
pub(crate) struct PgQueryBuilder;

impl GenericQueryBuilder<Pg> for PgQueryBuilder {
fn get_tx_by_digest(digest: Vec<u8>) -> transactions::BoxedQuery<'static, Pg> {
transactions::dsl::transactions
.filter(transactions::dsl::transaction_digest.eq(digest))
.into_boxed()
}
fn get_obj(address: Vec<u8>, version: Option<i64>) -> objects::BoxedQuery<'static, Pg> {
let mut query = objects::dsl::objects.into_boxed();
query = query.filter(objects::dsl::object_id.eq(address));
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-graphql-rpc/src/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ impl Object {
ctx: &Context<'_>,
) -> Result<Option<TransactionBlock>> {
let digest = self.native.previous_transaction;
ctx.data_unchecked::<PgManager>()
.fetch_tx(&digest.into())
TransactionBlock::query(ctx.data_unchecked(), digest.into())
.await
.extend()
}
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-graphql-rpc/src/types/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ impl Query {
ctx: &Context<'_>,
digest: Digest,
) -> Result<Option<TransactionBlock>> {
ctx.data_unchecked::<PgManager>()
.fetch_tx(&digest)
TransactionBlock::query(ctx.data_unchecked(), digest)
.await
.extend()
}
Expand Down
24 changes: 22 additions & 2 deletions crates/sui-graphql-rpc/src/types/transaction_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ use async_graphql::{
connection::{Connection, CursorType, Edge},
*,
};
use diesel::{ExpressionMethods, QueryDsl};
use fastcrypto::encoding::{Base58, Encoding};
use sui_indexer::models_v2::transactions::StoredTransaction;
use sui_indexer::{models_v2::transactions::StoredTransaction, schema_v2::transactions};
use sui_types::{
base_types::SuiAddress as NativeSuiAddress,
transaction::{
SenderSignedData as NativeSenderSignedData, TransactionDataAPI, TransactionExpiration,
},
};

use crate::error::Error;
use crate::{
data::{Db, QueryExecutor},
error::Error,
};

use super::{
address::Address,
Expand Down Expand Up @@ -164,6 +168,22 @@ impl TransactionBlock {
}
}

impl TransactionBlock {
/// Look up a `TransactionBlock` in the database, by its transaction digest.
pub(crate) async fn query(db: &Db, digest: Digest) -> Result<Option<Self>, Error> {
use transactions::dsl;
db.optional::<_, _, _, _, StoredTransaction>(move || {
dsl::transactions
.filter(dsl::transaction_digest.eq(digest.to_vec()))
.into_boxed()
})
.await
.map_err(|e| Error::Internal(format!("Failed to fetch transaction: {e}")))?
.map(TransactionBlock::try_from)
.transpose()
}
}

impl TryFrom<StoredTransaction> for TransactionBlock {
type Error = Error;

Expand Down

0 comments on commit f4b18af

Please sign in to comment.