Skip to content

Commit

Permalink
[gql][indexer] index chain identifier into its own table
Browse files Browse the repository at this point in the history
  • Loading branch information
emmazzz committed Jul 26, 2024
1 parent ef8a189 commit 27b7f57
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
14 changes: 14 additions & 0 deletions crates/sui-graphql-e2e-tests/tests/epoch/chain_identifier.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
processed 3 tasks

init:
C: object(0,0)

task 1 'create-checkpoint'. lines 8-8:
Checkpoint created: 1

task 2 'run-graphql'. lines 10-13:
Response: {
"data": {
"chainIdentifier": "8f58ad28"
}
}
12 changes: 12 additions & 0 deletions crates/sui-graphql-e2e-tests/tests/epoch/chain_identifier.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//# init --protocol-version 48 --simulator --accounts C


//# create-checkpoint

//# run-graphql
{
chainIdentifier
}
12 changes: 4 additions & 8 deletions crates/sui-graphql-rpc/src/types/chain_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::{
error::Error,
};
use async_graphql::*;
use diesel::{ExpressionMethods, QueryDsl};
use sui_indexer::schema::checkpoints;
use diesel::QueryDsl;
use sui_indexer::schema::chain_identifier;
use sui_types::{
digests::ChainIdentifier as NativeChainIdentifier, messages_checkpoint::CheckpointDigest,
};
Expand All @@ -17,15 +17,11 @@ pub(crate) struct ChainIdentifier;
impl ChainIdentifier {
/// Query the Chain Identifier from the DB.
pub(crate) async fn query(db: &Db) -> Result<NativeChainIdentifier, Error> {
use checkpoints::dsl;
use chain_identifier::dsl;

let digest_bytes = db
.execute(move |conn| {
conn.first(move || {
dsl::checkpoints
.select(dsl::checkpoint_digest)
.order_by(dsl::sequence_number.asc())
})
conn.first(move || dsl::chain_identifier.select(dsl::checkpoint_digest))
})
.await
.map_err(|e| Error::Internal(format!("Failed to fetch genesis digest: {e}")))?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS chain_identifier;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Your SQL goes here
CREATE TABLE chain_identifier
(
checkpoint_digest BYTEA NOT NULL,
PRIMARY KEY(checkpoint_digest)
);
8 changes: 7 additions & 1 deletion crates/sui-indexer/src/models/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ use sui_types::digests::CheckpointDigest;
use sui_types::gas::GasCostSummary;

use crate::errors::IndexerError;
use crate::schema::checkpoints;
use crate::schema::{chain_identifier, checkpoints};
use crate::types::IndexedCheckpoint;

#[derive(Queryable, Insertable, Selectable, Debug, Clone, Default)]
#[diesel(table_name = chain_identifier)]
pub struct StoredChainIdentifier {
pub checkpoint_digest: Vec<u8>,
}

#[derive(Queryable, Insertable, Selectable, Debug, Clone, Default)]
#[diesel(table_name = checkpoints)]
pub struct StoredCheckpoint {
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-indexer/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod pg;

#[cfg(feature = "postgres-feature")]
mod inner {
pub use crate::schema::pg::chain_identifier;
pub use crate::schema::pg::checkpoints;
pub use crate::schema::pg::display;
pub use crate::schema::pg::epochs;
Expand Down Expand Up @@ -61,6 +62,7 @@ mod inner {
pub use crate::schema::mysql::tx_senders;
}

pub use inner::chain_identifier;
pub use inner::checkpoints;
pub use inner::display;
pub use inner::epochs;
Expand Down
11 changes: 8 additions & 3 deletions crates/sui-indexer/src/schema/pg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
// @generated automatically by Diesel CLI.

diesel::table! {
chain_identifier (checkpoint_digest) {
checkpoint_digest -> Bytea,
}
}

diesel::table! {
checkpoints (sequence_number) {
sequence_number -> Int8,
Expand All @@ -21,7 +25,7 @@ diesel::table! {
validator_signature -> Bytea,
end_of_epoch_data -> Nullable<Bytea>,
min_tx_sequence_number -> Nullable<Int8>,
max_tx_sequence_number -> Nullable<Int8>
max_tx_sequence_number -> Nullable<Int8>,
}
}

Expand Down Expand Up @@ -379,6 +383,7 @@ diesel::table! {
macro_rules! for_all_tables {
($action:path) => {
$action!(
chain_identifier,
checkpoints,
display,
epochs,
Expand Down
30 changes: 25 additions & 5 deletions crates/sui-indexer/src/store/pg_indexer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::errors::{Context, IndexerError};
use crate::handlers::EpochToCommit;
use crate::handlers::TransactionObjectChangesToCommit;
use crate::metrics::IndexerMetrics;
use crate::models::checkpoints::StoredChainIdentifier;
use crate::models::checkpoints::StoredCheckpoint;
use crate::models::display::StoredDisplay;
use crate::models::epoch::StoredEpochInfo;
Expand All @@ -40,11 +41,11 @@ use crate::models::packages::StoredPackage;
use crate::models::transactions::StoredTransaction;
use crate::schema::tx_kinds;
use crate::schema::{
checkpoints, display, epochs, event_emit_module, event_emit_package, event_senders,
event_struct_instantiation, event_struct_module, event_struct_name, event_struct_package,
events, objects, objects_history, objects_snapshot, objects_version, packages, transactions,
tx_calls_fun, tx_calls_mod, tx_calls_pkg, tx_changed_objects, tx_digests, tx_input_objects,
tx_recipients, tx_senders,
chain_identifier, checkpoints, display, epochs, event_emit_module, event_emit_package,
event_senders, event_struct_instantiation, event_struct_module, event_struct_name,
event_struct_package, events, objects, objects_history, objects_snapshot, objects_version,
packages, transactions, tx_calls_fun, tx_calls_mod, tx_calls_pkg, tx_changed_objects,
tx_digests, tx_input_objects, tx_recipients, tx_senders,
};
use crate::types::EventIndex;
use crate::types::{IndexedCheckpoint, IndexedEvent, IndexedPackage, IndexedTransaction, TxIndex};
Expand Down Expand Up @@ -539,6 +540,25 @@ impl<T: R2D2Connection + 'static> PgIndexerStore<T> {
if checkpoints.is_empty() {
return Ok(());
}
// If the first checkpoint has sequence number 0, we need to persist the digest as
// chain identifier.
let first_checkpoint = checkpoints.first().unwrap();
if first_checkpoint.sequence_number == 0 {
transactional_blocking_with_retry!(
&self.blocking_cp,
|conn| {
let checkpoint_digest =
first_checkpoint.checkpoint_digest.into_inner().to_vec();
insert_or_ignore_into!(
chain_identifier::table,
StoredChainIdentifier { checkpoint_digest },
conn
);
Ok::<(), IndexerError>(())
},
PG_DB_COMMIT_SLEEP_DURATION
)?;
}
let guard = self
.metrics
.checkpoint_db_commit_latency_checkpoints
Expand Down

0 comments on commit 27b7f57

Please sign in to comment.