Skip to main content

sui_rpc_store/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Storage backend for `sui-rpc-api`.
5//!
6//! Built on top of [`sui_consistent_store`], this crate hosts the
7//! column families that back every read the RPC service performs:
8//!
9//! - Raw chain data — objects, transactions, effects, events,
10//!   checkpoints, committees — previously served by the validator's
11//!   perpetual / checkpoint / committee stores.
12//! - Indexes — owner, dynamic-field, coin, balance, package version,
13//!   epoch info, ledger history — previously served by
14//!   `sui-core::rpc_index` and `sui-indexer-alt-consistent-store`.
15//!
16//! Values are encoded with bespoke protobuf messages defined under
17//! `proto/sui/rpc_store/`, mirroring the build setup in
18//! `sui-consistent-store`.
19
20pub mod config;
21pub mod indexer;
22pub mod proto;
23pub mod reader;
24pub mod schema;
25
26use std::path::Path;
27use std::sync::Arc;
28
29use prometheus::Registry;
30use sui_consistent_store::DbOptions;
31use sui_indexer_alt_framework::IndexerArgs;
32use sui_indexer_alt_framework::ingestion::ArcStreamingClient;
33use sui_indexer_alt_framework::ingestion::ClientArgs;
34use sui_indexer_alt_framework::ingestion::IngestionConfig;
35use sui_indexer_alt_framework::ingestion::ingestion_client::IngestionClient;
36use sui_indexer_alt_framework::ingestion::streaming_client::GrpcStreamingClient;
37use sui_indexer_alt_framework::metrics::IngestionMetrics;
38use sui_indexer_alt_framework::pipeline::CommitterConfig;
39use sui_indexer_alt_framework::service::Service;
40
41pub use crate::config::CommitterLayer;
42pub use crate::config::ConsistencyConfig;
43pub use crate::config::PipelineLayer;
44pub use crate::config::PrunerConfig;
45pub use crate::config::RestoreLayer;
46pub use crate::config::ServiceConfig;
47pub use crate::indexer::Indexer;
48pub use crate::indexer::METRICS_PREFIX;
49pub use crate::indexer::Store;
50pub use crate::indexer::checkpoint_broadcast::CheckpointBroadcast;
51pub use crate::indexer::checkpoint_broadcast::seed_watermark_to_tip as seed_checkpoint_broadcast_watermark;
52pub use crate::indexer::pruner::DEFAULT_RETRACTION_CURSORS_CAPACITY;
53pub use crate::indexer::pruner::RetractionCursors;
54pub use crate::indexer::pruner::embedded_prunable_checkpoint;
55pub use crate::indexer::pruner::prune_history_cohort;
56pub use crate::indexer::restore::HISTORY_COHORT;
57pub use crate::indexer::restore::LIVE_COHORT;
58pub use crate::indexer::restore::floor_unrestored_pipelines;
59pub use crate::indexer::restore::history_seed_pending;
60pub use crate::indexer::restore::restore_in_progress;
61pub use crate::indexer::restore::restore_indexes;
62pub use crate::indexer::restore::seed_current_epoch_start;
63pub use crate::indexer::restore::seed_history_cohort;
64pub use crate::reader::RpcStoreReader;
65pub use crate::schema::RpcStoreSchema;
66pub use crate::schema::default_rocksdb_config;
67
68/// Standalone-binary entry point. Opens the database at `path`,
69/// constructs an [`Indexer`] from `ClientArgs`-driven ingestion /
70/// streaming clients, registers every pipeline that is enabled in
71/// `config.pipeline`, and runs the resulting indexer.
72///
73/// The embedded-fullnode path bypasses this helper and constructs
74/// [`Indexer::from_store`] directly with its own
75/// [`IngestionClientTrait`] /
76/// [`CheckpointStreamingClient`] implementations.
77///
78/// [`IngestionClientTrait`]:
79///   sui_indexer_alt_framework::ingestion::ingestion_client::IngestionClientTrait
80/// [`CheckpointStreamingClient`]:
81///   sui_indexer_alt_framework::ingestion::streaming_client::CheckpointStreamingClient
82pub async fn start_indexer(
83    path: impl AsRef<Path>,
84    indexer_args: IndexerArgs,
85    client_args: ClientArgs,
86    db_options: DbOptions,
87    ingestion_config: IngestionConfig,
88    config: ServiceConfig,
89    registry: &Registry,
90) -> anyhow::Result<Service> {
91    let metrics_prefix = Some(METRICS_PREFIX);
92
93    // Build the metrics once; the same Arc threads through the
94    // ingestion client and (via `IngestionClient::metrics`) the
95    // ingestion service, avoiding double registration against
96    // `registry`.
97    let ingestion_metrics = IngestionMetrics::new(metrics_prefix, registry);
98    let ingestion_client = IngestionClient::new(client_args.ingestion, ingestion_metrics)?;
99    let streaming_client: Option<ArcStreamingClient> =
100        client_args.streaming.streaming_url.map(|uri| {
101            Arc::new(GrpcStreamingClient::new(
102                uri,
103                ingestion_config.streaming_connection_timeout(),
104                ingestion_config.streaming_statement_timeout(),
105            )) as ArcStreamingClient
106        });
107
108    let mut indexer = Indexer::new(
109        path,
110        indexer_args,
111        ingestion_client,
112        streaming_client,
113        config.consistency,
114        config.pruner,
115        ingestion_config,
116        db_options,
117        registry,
118    )
119    .await?;
120
121    let committer = config.committer.finish(CommitterConfig::default());
122    indexer.add_pipelines(config.pipeline, committer).await?;
123
124    indexer.run().await
125}