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::embedded_prunable_checkpoint;
53pub use crate::indexer::pruner::prune_history_cohort;
54pub use crate::indexer::restore::HISTORY_COHORT;
55pub use crate::indexer::restore::LIVE_COHORT;
56pub use crate::indexer::restore::floor_unrestored_pipelines;
57pub use crate::indexer::restore::history_seed_pending;
58pub use crate::indexer::restore::restore_in_progress;
59pub use crate::indexer::restore::restore_indexes;
60pub use crate::indexer::restore::seed_current_epoch_start;
61pub use crate::indexer::restore::seed_history_cohort;
62pub use crate::reader::RpcStoreReader;
63pub use crate::schema::RpcStoreSchema;
64pub use crate::schema::default_rocksdb_config;
65
66/// Standalone-binary entry point. Opens the database at `path`,
67/// constructs an [`Indexer`] from `ClientArgs`-driven ingestion /
68/// streaming clients, registers every pipeline that is enabled in
69/// `config.pipeline`, and runs the resulting indexer.
70///
71/// The embedded-fullnode path bypasses this helper and constructs
72/// [`Indexer::from_store`] directly with its own
73/// [`IngestionClientTrait`] /
74/// [`CheckpointStreamingClient`] implementations.
75///
76/// [`IngestionClientTrait`]:
77///   sui_indexer_alt_framework::ingestion::ingestion_client::IngestionClientTrait
78/// [`CheckpointStreamingClient`]:
79///   sui_indexer_alt_framework::ingestion::streaming_client::CheckpointStreamingClient
80pub async fn start_indexer(
81    path: impl AsRef<Path>,
82    indexer_args: IndexerArgs,
83    client_args: ClientArgs,
84    db_options: DbOptions,
85    ingestion_config: IngestionConfig,
86    config: ServiceConfig,
87    registry: &Registry,
88) -> anyhow::Result<Service> {
89    let metrics_prefix = Some(METRICS_PREFIX);
90
91    // Build the metrics once; the same Arc threads through the
92    // ingestion client and (via `IngestionClient::metrics`) the
93    // ingestion service, avoiding double registration against
94    // `registry`.
95    let ingestion_metrics = IngestionMetrics::new(metrics_prefix, registry);
96    let ingestion_client = IngestionClient::new(client_args.ingestion, ingestion_metrics)?;
97    let streaming_client: Option<ArcStreamingClient> =
98        client_args.streaming.streaming_url.map(|uri| {
99            Arc::new(GrpcStreamingClient::new(
100                uri,
101                ingestion_config.streaming_connection_timeout(),
102                ingestion_config.streaming_statement_timeout(),
103            )) as ArcStreamingClient
104        });
105
106    let mut indexer = Indexer::new(
107        path,
108        indexer_args,
109        ingestion_client,
110        streaming_client,
111        config.consistency,
112        config.pruner,
113        ingestion_config,
114        db_options,
115        registry,
116    )
117    .await?;
118
119    let committer = config.committer.finish(CommitterConfig::default());
120    indexer.add_pipelines(config.pipeline, committer).await?;
121
122    indexer.run().await
123}