|
| 1 | +use crate::{ |
| 2 | + config::{ |
| 3 | + db_config::DbConfig, indexer_processor_config::IndexerProcessorConfig, |
| 4 | + processor_config::ProcessorConfig, |
| 5 | + }, |
| 6 | + steps::{ |
| 7 | + common::latest_processed_version_tracker::LatestVersionProcessedTracker, |
| 8 | + events_processor::{EventsExtractor, EventsStorer}, |
| 9 | + }, |
| 10 | + utils::{ |
| 11 | + chain_id::check_or_update_chain_id, |
| 12 | + database::{new_db_pool, run_migrations, ArcDbPool}, |
| 13 | + starting_version::get_starting_version, |
| 14 | + }, |
| 15 | +}; |
| 16 | +use ahash::AHashMap; |
| 17 | +use anyhow::Result; |
| 18 | +use aptos_indexer_processor_sdk::{ |
| 19 | + aptos_indexer_transaction_stream::{TransactionStream, TransactionStreamConfig}, |
| 20 | + builder::ProcessorBuilder, |
| 21 | + common_steps::TransactionStreamStep, |
| 22 | + traits::IntoRunnableStep, |
| 23 | +}; |
| 24 | +use serde::{Deserialize, Serialize}; |
| 25 | +use tracing::{debug, info}; |
| 26 | + |
| 27 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 28 | +#[serde(deny_unknown_fields)] |
| 29 | +pub struct FungibleAssetProcessorConfig { |
| 30 | + // Number of rows to insert, per chunk, for each DB table. Default per table is ~32,768 (2**16/2) |
| 31 | + #[serde(default = "AHashMap::new")] |
| 32 | + pub per_table_chunk_sizes: AHashMap<String, usize>, |
| 33 | + // Size of channel between steps |
| 34 | + #[serde(default = "FungibleAssetProcessorConfig::default_channel_size")] |
| 35 | + pub channel_size: usize, |
| 36 | +} |
| 37 | + |
| 38 | +impl FungibleAssetProcessorConfig { |
| 39 | + pub const fn default_channel_size() -> usize { |
| 40 | + 10 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +pub struct FungibleAssetProcessor { |
| 45 | + pub config: IndexerProcessorConfig, |
| 46 | + pub db_pool: ArcDbPool, |
| 47 | +} |
| 48 | + |
| 49 | +impl FungibleAssetProcessor { |
| 50 | + pub async fn new(config: IndexerProcessorConfig) -> Result<Self> { |
| 51 | + match config.db_config { |
| 52 | + DbConfig::PostgresConfig(ref postgres_config) => { |
| 53 | + let conn_pool = new_db_pool( |
| 54 | + &postgres_config.connection_string, |
| 55 | + Some(postgres_config.db_pool_size), |
| 56 | + ) |
| 57 | + .await |
| 58 | + .map_err(|e| { |
| 59 | + anyhow::anyhow!( |
| 60 | + "Failed to create connection pool for PostgresConfig: {:?}", |
| 61 | + e |
| 62 | + ) |
| 63 | + })?; |
| 64 | + |
| 65 | + Ok(Self { |
| 66 | + config, |
| 67 | + db_pool: conn_pool, |
| 68 | + }) |
| 69 | + }, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub async fn run_processor(self) -> Result<()> { |
| 74 | + let processor_name = self.config.processor_config.name(); |
| 75 | + |
| 76 | + // (Optional) Run migrations |
| 77 | + match self.config.db_config { |
| 78 | + DbConfig::PostgresConfig(ref postgres_config) => { |
| 79 | + run_migrations( |
| 80 | + postgres_config.connection_string.clone(), |
| 81 | + self.db_pool.clone(), |
| 82 | + ) |
| 83 | + .await; |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + // (Optional) Merge the starting version from config and the latest processed version from the DB |
| 88 | + let starting_version = get_starting_version(&self.config, self.db_pool.clone()).await?; |
| 89 | + |
| 90 | + // (Optional) Check and update the ledger chain id to ensure we're indexing the correct chain |
| 91 | + let grpc_chain_id = TransactionStream::new(self.config.transaction_stream_config.clone()) |
| 92 | + .await? |
| 93 | + .get_chain_id() |
| 94 | + .await?; |
| 95 | + check_or_update_chain_id(grpc_chain_id as i64, self.db_pool.clone()).await?; |
| 96 | + |
| 97 | + let fa_config = match self.config.processor_config { |
| 98 | + ProcessorConfig::FungibleAssetProcessor(fa_config) => fa_config, |
| 99 | + _ => return Err(anyhow::anyhow!("Processor config is wrong type")), |
| 100 | + }; |
| 101 | + let channel_size = fa_config.channel_size; |
| 102 | + |
| 103 | + // Define processor steps |
| 104 | + let transaction_stream = TransactionStreamStep::new(TransactionStreamConfig { |
| 105 | + starting_version: Some(starting_version), |
| 106 | + ..self.config.transaction_stream_config |
| 107 | + }) |
| 108 | + .await?; |
| 109 | + // let events_extractor = EventsExtractor {}; |
| 110 | + // let events_storer = EventsStorer::new(self.db_pool.clone(), events_processor_config); |
| 111 | + let version_tracker = LatestVersionProcessedTracker::new( |
| 112 | + self.db_pool.clone(), |
| 113 | + starting_version, |
| 114 | + processor_name.to_string(), |
| 115 | + ); |
| 116 | + |
| 117 | + // Connect processor steps together |
| 118 | + let (_, buffer_receiver) = ProcessorBuilder::new_with_inputless_first_step( |
| 119 | + transaction_stream.into_runnable_step(), |
| 120 | + ) |
| 121 | + // .connect_to(events_extractor.into_runnable_step(), channel_size) |
| 122 | + // .connect_to(events_storer.into_runnable_step(), channel_size) |
| 123 | + .connect_to(version_tracker.into_runnable_step(), channel_size) |
| 124 | + .end_and_return_output_receiver(channel_size); |
| 125 | + |
| 126 | + // (Optional) Parse the results |
| 127 | + loop { |
| 128 | + match buffer_receiver.recv().await { |
| 129 | + Ok(txn_context) => { |
| 130 | + if txn_context.data.is_empty() { |
| 131 | + continue; |
| 132 | + } |
| 133 | + debug!( |
| 134 | + "Finished processing versions [{:?}, {:?}]", |
| 135 | + txn_context.start_version, txn_context.end_version, |
| 136 | + ); |
| 137 | + }, |
| 138 | + Err(e) => { |
| 139 | + info!("No more transactions in channel: {:?}", e); |
| 140 | + break Ok(()); |
| 141 | + }, |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments