-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
indexer-alt: index chain identifier (#20148)
## Description Index the chain identifier and initial protocol version in a `kv_genesis` table. This takes the place of chain identifier indexing and will also be used by indexers for protocol configs and feature flags, which need to know the initial protocol version to index and the chain identifier. ## Test plan Run the indexer twice, and note from the tracing messages that the first time, the indexer is bootstrapped, and the second time it reads from the bootstrapped table. ## Stack - #20118 - #20132 - #20147 --- ## Release notes 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: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK: - [ ] REST API:
- Loading branch information
Showing
10 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
crates/sui-indexer-alt/migrations/2024-11-01-182359_kv_genesis/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS kv_genesis; |
12 changes: 12 additions & 0 deletions
12
crates/sui-indexer-alt/migrations/2024-11-01-182359_kv_genesis/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Stores information related to to the genesis checkpoint. | ||
CREATE TABLE IF NOT EXISTS kv_genesis | ||
( | ||
-- The checkpoint digest of the genesis checkpoint | ||
genesis_digest BYTEA PRIMARY KEY, | ||
-- The protocol version from the gensis system state | ||
initial_protocol_version BIGINT NOT NULL | ||
); | ||
|
||
-- Index to ensure there can only be one row in the genesis table. | ||
CREATE UNIQUE INDEX IF NOT EXISTS kv_genesis_unique | ||
ON kv_genesis ((0)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::time::Duration; | ||
|
||
use anyhow::{bail, Context, Result}; | ||
use diesel::{OptionalExtension, QueryDsl, SelectableHelper}; | ||
use diesel_async::RunQueryDsl; | ||
use sui_types::{ | ||
full_checkpoint_content::CheckpointData, | ||
sui_system_state::{get_sui_system_state, SuiSystemStateTrait}, | ||
transaction::{TransactionDataAPI, TransactionKind}, | ||
}; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::info; | ||
|
||
use crate::{ | ||
models::checkpoints::StoredGenesis, schema::kv_genesis, task::graceful_shutdown, Indexer, | ||
}; | ||
|
||
/// Ensures the genesis table has been populated before the rest of the indexer is run, and returns | ||
/// the information stored there. If the database has been bootstrapped before, this function will | ||
/// simply read the previously bootstrapped information. Otherwise, it will wait until the first | ||
/// checkpoint is available and extract the necessary information from there. | ||
/// | ||
/// Can be cancelled via the `cancel` token, or through an interrupt signal (which will also cancel | ||
/// the token). | ||
pub async fn bootstrap( | ||
indexer: &Indexer, | ||
retry_interval: Duration, | ||
cancel: CancellationToken, | ||
) -> Result<StoredGenesis> { | ||
let Ok(mut conn) = indexer.db().connect().await else { | ||
bail!("Bootstrap failed to get connection for DB"); | ||
}; | ||
|
||
// If the row has already been written, return it. | ||
if let Some(genesis) = kv_genesis::table | ||
.select(StoredGenesis::as_select()) | ||
.first(&mut conn) | ||
.await | ||
.optional()? | ||
{ | ||
info!( | ||
chain = genesis.chain()?.as_str(), | ||
protocol = ?genesis.initial_protocol_version(), | ||
"Indexer already bootstrapped", | ||
); | ||
|
||
return Ok(genesis); | ||
} | ||
|
||
// Otherwise, extract the necessary information from the genesis checkpoint: | ||
// | ||
// - Get the Genesis system transaction from the genesis checkpoint. | ||
// - Get the system state object that was written out by the system transaction. | ||
let ingestion_client = indexer.ingestion_client().clone(); | ||
let wait_cancel = cancel.clone(); | ||
let genesis = tokio::spawn(async move { | ||
ingestion_client | ||
.wait_for(0, retry_interval, &wait_cancel) | ||
.await | ||
}); | ||
|
||
let Some(genesis_checkpoint) = graceful_shutdown(vec![genesis], cancel).await.pop() else { | ||
bail!("Bootstrap cancelled"); | ||
}; | ||
|
||
let genesis_checkpoint = genesis_checkpoint.context("Failed to fetch genesis checkpoint")?; | ||
|
||
let CheckpointData { | ||
checkpoint_summary, | ||
transactions, | ||
.. | ||
} = genesis_checkpoint.as_ref(); | ||
|
||
let Some(genesis_transaction) = transactions.iter().find(|tx| { | ||
matches!( | ||
tx.transaction.intent_message().value.kind(), | ||
TransactionKind::Genesis(_) | ||
) | ||
}) else { | ||
bail!("Could not find Genesis transaction"); | ||
}; | ||
|
||
let system_state = get_sui_system_state(&genesis_transaction.output_objects.as_slice()) | ||
.context("Failed to get Genesis SystemState")?; | ||
|
||
let genesis = StoredGenesis { | ||
genesis_digest: checkpoint_summary.digest().inner().to_vec(), | ||
initial_protocol_version: system_state.protocol_version() as i64, | ||
}; | ||
|
||
info!( | ||
chain = genesis.chain()?.as_str(), | ||
protocol = ?genesis.initial_protocol_version(), | ||
"Bootstrapped indexer", | ||
); | ||
|
||
diesel::insert_into(kv_genesis::table) | ||
.values(&genesis) | ||
.on_conflict_do_nothing() | ||
.execute(&mut conn) | ||
.await | ||
.context("Failed to write genesis record")?; | ||
|
||
Ok(genesis) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters