Skip to content

Commit

Permalink
indexer: make block finality configurable (#12685)
Browse files Browse the repository at this point in the history
Tested this out by using Finality::None in mpc node indexers. Observed
reduction of latency.
  • Loading branch information
saketh-are authored Jan 4, 2025
1 parent 77b4ab2 commit 88ec6c8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion chain/indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::mpsc;

use near_chain_configs::GenesisValidationMode;
pub use near_primitives;
use near_primitives::types::Gas;
use near_primitives::types::{Finality, Gas};
pub use nearcore::{get_default_home, init_configs, NearConfig};

pub use near_indexer_primitives::{
Expand Down Expand Up @@ -83,6 +83,8 @@ pub struct IndexerConfig {
pub sync_mode: SyncModeEnum,
/// Whether await for node to be synced or not
pub await_for_node_synced: AwaitForNodeSyncedEnum,
/// Finality level at which blocks are streamed
pub finality: Finality,
/// Tells whether to validate the genesis file before starting
pub validate_genesis: bool,
}
Expand Down
3 changes: 2 additions & 1 deletion chain/indexer/src/streamer/fetchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ pub(crate) async fn fetch_status(
/// entire block or we already fetched this block.
pub(crate) async fn fetch_latest_block(
client: &Addr<near_client::ViewClientActor>,
finality: &near_primitives::types::Finality,
) -> Result<views::BlockView, FailedToFetchData> {
tracing::debug!(target: INDEXER, "Fetching latest block");
client
.send(
near_client::GetBlock(near_primitives::types::BlockReference::Finality(
near_primitives::types::Finality::Final,
finality.clone(),
))
.with_span_context(),
)
Expand Down
13 changes: 7 additions & 6 deletions chain/indexer/src/streamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static DELAYED_LOCAL_RECEIPTS_CACHE: std::sync::LazyLock<
Arc<RwLock<HashMap<CryptoHash, views::ReceiptView>>>,
> = std::sync::LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));

const INTERVAL: Duration = Duration::from_millis(500);
const INTERVAL: Duration = Duration::from_millis(250);

/// Blocks #47317863 and #47317864 with restored receipts.
const PROBLEMATIC_BLOCKS: [CryptoHash; 2] = [
Expand Down Expand Up @@ -413,11 +413,12 @@ pub(crate) async fn start(
AwaitForNodeSyncedEnum::StreamWhileSyncing => {}
};

let block = if let Ok(block) = fetch_latest_block(&view_client).await {
block
} else {
continue;
};
let block =
if let Ok(block) = fetch_latest_block(&view_client, &indexer_config.finality).await {
block
} else {
continue;
};

let latest_block_height = block.header.height;
let start_syncing_block_height = if let Some(last_synced_block_height) =
Expand Down
1 change: 1 addition & 0 deletions tools/indexer/example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tracing.workspace = true
near-config-utils.workspace = true
near-indexer.workspace = true
near-o11y.workspace = true
near-primitives.workspace = true
1 change: 1 addition & 0 deletions tools/indexer/example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ fn main() -> Result<()> {
home_dir,
sync_mode: near_indexer::SyncModeEnum::FromInterruption,
await_for_node_synced: near_indexer::AwaitForNodeSyncedEnum::WaitForFullSync,
finality: near_primitives::types::Finality::Final,
validate_genesis: true,
};
let system = actix::System::new();
Expand Down
1 change: 1 addition & 0 deletions tools/mirror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ impl<T: ChainAccess> TxMirror<T> {
home_dir,
sync_mode: near_indexer::SyncModeEnum::FromInterruption,
await_for_node_synced: near_indexer::AwaitForNodeSyncedEnum::StreamWhileSyncing,
finality: Finality::Final,
validate_genesis: false,
})
.context("failed to start target chain indexer")?;
Expand Down

0 comments on commit 88ec6c8

Please sign in to comment.