Skip to content

Commit

Permalink
[gql][indexer] index chain identifier into its own table (MystenLabs#…
Browse files Browse the repository at this point in the history
…18825)

This PR puts chain identifier into its own table so that queries of
chain identifier does not depend on the existence of checkpoint 0 in the
db, which may be pruned away.

Tested against devnet locally and added a gql e2e test.

---

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol:
- [ ] Nodes (Validators and Full nodes):
- [ ] Indexer:
- [ ] JSON-RPC:
- [x] GraphQL: Added a way to always have `chainIdentifier` query return
the correct chain ID even when pruning is enabled.
- [ ] CLI:
- [ ] Rust SDK:
- [ ] REST API:

---------

Co-authored-by: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com>
Co-authored-by: Ashok Menon <ashok@mystenlabs.com>
  • Loading branch information
3 people committed Jul 30, 2024
1 parent dde6f14 commit bdfa49e
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 13 deletions.
16 changes: 16 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,16 @@
processed 3 tasks

init:
C: object(0,0)

task 1, line 7:
//# create-checkpoint
Checkpoint created: 1

task 2, lines 9-12:
//# run-graphql
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, pruner_cp_watermark};
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
7 changes: 7 additions & 0 deletions crates/sui-indexer/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// 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 Down Expand Up @@ -276,6 +282,7 @@ diesel::table! {
macro_rules! for_all_tables {
($action:path) => {
$action!(
chain_identifier,
checkpoints,
epochs,
events,
Expand Down
80 changes: 80 additions & 0 deletions crates/sui-indexer/src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::all)]

#[cfg(feature = "mysql-feature")]
#[cfg(not(feature = "postgres-feature"))]
mod mysql;

#[cfg(feature = "postgres-feature")]
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;
pub use crate::schema::pg::events;
pub use crate::schema::pg::objects;
pub use crate::schema::pg::objects_history;
pub use crate::schema::pg::objects_snapshot;
pub use crate::schema::pg::packages;
pub use crate::schema::pg::pruner_cp_watermark;
pub use crate::schema::pg::transactions;
pub use crate::schema::pg::tx_calls;
pub use crate::schema::pg::tx_changed_objects;
pub use crate::schema::pg::tx_digests;
pub use crate::schema::pg::tx_input_objects;
pub use crate::schema::pg::tx_recipients;
pub use crate::schema::pg::tx_senders;
}

#[cfg(feature = "mysql-feature")]
#[cfg(not(feature = "postgres-feature"))]
mod inner {
pub use crate::schema::mysql::chain_identifier;
pub use crate::schema::mysql::checkpoints;
pub use crate::schema::mysql::display;
pub use crate::schema::mysql::epochs;
pub use crate::schema::mysql::events;
pub use crate::schema::mysql::objects;
pub use crate::schema::mysql::objects_history;
pub use crate::schema::mysql::objects_snapshot;
pub use crate::schema::mysql::packages;
pub use crate::schema::mysql::pruner_cp_watermark;
pub use crate::schema::mysql::transactions;
pub use crate::schema::mysql::tx_calls;
pub use crate::schema::mysql::tx_changed_objects;
pub use crate::schema::mysql::tx_digests;
pub use crate::schema::mysql::tx_input_objects;
pub use crate::schema::mysql::tx_recipients;
pub use crate::schema::mysql::tx_senders;
}

pub use inner::chain_identifier;
pub use inner::checkpoints;
pub use inner::display;
pub use inner::epochs;
pub use inner::events;
pub use inner::objects;
pub use inner::objects_history;
pub use inner::objects_snapshot;
pub use inner::packages;
pub use inner::pruner_cp_watermark;
pub use inner::transactions;
pub use inner::tx_calls;
pub use inner::tx_changed_objects;
pub use inner::tx_digests;
pub use inner::tx_input_objects;
pub use inner::tx_recipients;
pub use inner::tx_senders;

// Postgres only tables
#[cfg(feature = "postgres-feature")]
pub use crate::schema::pg::events_partition_0;
#[cfg(feature = "postgres-feature")]
pub use crate::schema::pg::objects_history_partition_0;
#[cfg(feature = "postgres-feature")]
pub use crate::schema::pg::transactions_partition_0;
Loading

0 comments on commit bdfa49e

Please sign in to comment.