Skip to content

Commit

Permalink
Rewrite ans processor with SDK (#570)
Browse files Browse the repository at this point in the history
* Rewrite ans processor with SDK

* Add ans specific config

* Formatting

* Fix serde flatten

* Formatting and remove writes to ans primary name v2 as it is now deprecated
  • Loading branch information
dermanyang authored Oct 29, 2024
1 parent cc19acd commit 9d44544
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 16 deletions.
18 changes: 9 additions & 9 deletions rust/processor/src/processors/ans_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async fn insert_to_db(
Ok(())
}

fn insert_current_ans_lookups_query(
pub fn insert_current_ans_lookups_query(
item_to_insert: Vec<CurrentAnsLookup>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -211,7 +211,7 @@ fn insert_current_ans_lookups_query(
)
}

fn insert_ans_lookups_query(
pub fn insert_ans_lookups_query(
item_to_insert: Vec<AnsLookup>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -228,7 +228,7 @@ fn insert_ans_lookups_query(
)
}

fn insert_current_ans_primary_names_query(
pub fn insert_current_ans_primary_names_query(
item_to_insert: Vec<CurrentAnsPrimaryName>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -253,7 +253,7 @@ fn insert_current_ans_primary_names_query(
)
}

fn insert_ans_primary_names_query(
pub fn insert_ans_primary_names_query(
item_to_insert: Vec<AnsPrimaryName>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -270,7 +270,7 @@ fn insert_ans_primary_names_query(
)
}

fn insert_current_ans_lookups_v2_query(
pub fn insert_current_ans_lookups_v2_query(
item_to_insert: Vec<CurrentAnsLookupV2>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -296,7 +296,7 @@ fn insert_current_ans_lookups_v2_query(
)
}

fn insert_ans_lookups_v2_query(
pub fn insert_ans_lookups_v2_query(
item_to_insert: Vec<AnsLookupV2>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -317,7 +317,7 @@ fn insert_ans_lookups_v2_query(
)
}

fn insert_current_ans_primary_names_v2_query(
pub fn insert_current_ans_primary_names_v2_query(
item_to_insert: Vec<CurrentAnsPrimaryNameV2>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand All @@ -342,7 +342,7 @@ fn insert_current_ans_primary_names_v2_query(
)
}

fn insert_ans_primary_names_v2_query(
pub fn insert_ans_primary_names_v2_query(
items_to_insert: Vec<AnsPrimaryNameV2>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Expand Down Expand Up @@ -470,7 +470,7 @@ impl ProcessorTrait for AnsProcessor {
}
}

fn parse_ans(
pub fn parse_ans(
transactions: &[Transaction],
ans_v1_primary_names_table_handle: String,
ans_v1_name_records_table_handle: String,
Expand Down
14 changes: 9 additions & 5 deletions rust/sdk-processor/src/config/indexer_processor_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::{db_config::DbConfig, processor_config::ProcessorConfig};
use crate::processors::{
account_transactions_processor::AccountTransactionsProcessor,
account_transactions_processor::AccountTransactionsProcessor, ans_processor::AnsProcessor,
default_processor::DefaultProcessor, events_processor::EventsProcessor,
fungible_asset_processor::FungibleAssetProcessor, token_v2_processor::TokenV2Processor,
};
Expand Down Expand Up @@ -35,6 +35,14 @@ impl RunnableConfig for IndexerProcessorConfig {
let acc_txns_processor = AccountTransactionsProcessor::new(self.clone()).await?;
acc_txns_processor.run_processor().await
},
ProcessorConfig::AnsProcessor(_) => {
let ans_processor = AnsProcessor::new(self.clone()).await?;
ans_processor.run_processor().await
},
ProcessorConfig::DefaultProcessor(_) => {
let default_processor = DefaultProcessor::new(self.clone()).await?;
default_processor.run_processor().await
},
ProcessorConfig::EventsProcessor(_) => {
let events_processor = EventsProcessor::new(self.clone()).await?;
events_processor.run_processor().await
Expand All @@ -43,10 +51,6 @@ impl RunnableConfig for IndexerProcessorConfig {
let fungible_asset_processor = FungibleAssetProcessor::new(self.clone()).await?;
fungible_asset_processor.run_processor().await
},
ProcessorConfig::DefaultProcessor(_) => {
let default_processor = DefaultProcessor::new(self.clone()).await?;
default_processor.run_processor().await
},
ProcessorConfig::TokenV2Processor(_) => {
let token_v2_processor = TokenV2Processor::new(self.clone()).await?;
token_v2_processor.run_processor().await
Expand Down
6 changes: 4 additions & 2 deletions rust/sdk-processor/src/config/processor_config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::processors::token_v2_processor::TokenV2ProcessorConfig;
use crate::processors::{
ans_processor::AnsProcessorConfig, token_v2_processor::TokenV2ProcessorConfig,
};
use ahash::AHashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// This enum captures the configs for all the different processors that are defined.
///
/// The configs for each processor should only contain configuration specific to that
Expand Down Expand Up @@ -37,6 +38,7 @@ use std::collections::HashSet;
)]
pub enum ProcessorConfig {
AccountTransactionsProcessor(DefaultProcessorConfig),
AnsProcessor(AnsProcessorConfig),
DefaultProcessor(DefaultProcessorConfig),
EventsProcessor(DefaultProcessorConfig),
FungibleAssetProcessor(DefaultProcessorConfig),
Expand Down
149 changes: 149 additions & 0 deletions rust/sdk-processor/src/processors/ans_processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use crate::{
config::{
db_config::DbConfig,
indexer_processor_config::IndexerProcessorConfig,
processor_config::{DefaultProcessorConfig, ProcessorConfig},
},
steps::{
ans_processor::{AnsExtractor, AnsStorer},
common::get_processor_status_saver,
},
utils::{
chain_id::check_or_update_chain_id,
database::{new_db_pool, run_migrations, ArcDbPool},
starting_version::get_starting_version,
},
};
use anyhow::Result;
use aptos_indexer_processor_sdk::{
aptos_indexer_transaction_stream::{TransactionStream, TransactionStreamConfig},
builder::ProcessorBuilder,
common_steps::{
TransactionStreamStep, VersionTrackerStep, DEFAULT_UPDATE_PROCESSOR_STATUS_SECS,
},
traits::{processor_trait::ProcessorTrait, IntoRunnableStep},
};
use processor::worker::TableFlags;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AnsProcessorConfig {
#[serde(flatten)]
pub default: DefaultProcessorConfig,
pub ans_v1_primary_names_table_handle: String,
pub ans_v1_name_records_table_handle: String,
pub ans_v2_contract_address: String,
}

pub struct AnsProcessor {
pub config: IndexerProcessorConfig,
pub db_pool: ArcDbPool,
}

impl AnsProcessor {
pub async fn new(config: IndexerProcessorConfig) -> Result<Self> {
match config.db_config {
DbConfig::PostgresConfig(ref postgres_config) => {
let conn_pool = new_db_pool(
&postgres_config.connection_string,
Some(postgres_config.db_pool_size),
)
.await
.map_err(|e| {
anyhow::anyhow!(
"Failed to create connection pool for PostgresConfig: {:?}",
e
)
})?;

Ok(Self {
config,
db_pool: conn_pool,
})
},
}
}
}

#[async_trait::async_trait]
impl ProcessorTrait for AnsProcessor {
fn name(&self) -> &'static str {
self.config.processor_config.name()
}

async fn run_processor(&self) -> Result<()> {
// Run migrations
match self.config.db_config {
DbConfig::PostgresConfig(ref postgres_config) => {
run_migrations(
postgres_config.connection_string.clone(),
self.db_pool.clone(),
)
.await;
},
}

// Merge the starting version from config and the latest processed version from the DB.
let starting_version = get_starting_version(&self.config, self.db_pool.clone()).await?;

// Check and update the ledger chain id to ensure we're indexing the correct chain.
let grpc_chain_id = TransactionStream::new(self.config.transaction_stream_config.clone())
.await?
.get_chain_id()
.await?;
check_or_update_chain_id(grpc_chain_id as i64, self.db_pool.clone()).await?;

let processor_config = match self.config.processor_config.clone() {
ProcessorConfig::AnsProcessor(processor_config) => processor_config,
_ => {
return Err(anyhow::anyhow!(
"Invalid processor config for ANS Processor: {:?}",
self.config.processor_config
))
},
};
let channel_size = processor_config.default.channel_size;
let deprecated_table_flags =
TableFlags::from_set(&processor_config.default.deprecated_tables);

// Define processor steps.
let transaction_stream = TransactionStreamStep::new(TransactionStreamConfig {
starting_version: Some(starting_version),
..self.config.transaction_stream_config.clone()
})
.await?;
let acc_txns_extractor =
AnsExtractor::new(deprecated_table_flags, self.config.processor_config.clone());
let acc_txns_storer = AnsStorer::new(self.db_pool.clone(), processor_config);
let version_tracker = VersionTrackerStep::new(
get_processor_status_saver(self.db_pool.clone(), self.config.clone()),
DEFAULT_UPDATE_PROCESSOR_STATUS_SECS,
);

// Connect processor steps together.
let (_, buffer_receiver) = ProcessorBuilder::new_with_inputless_first_step(
transaction_stream.into_runnable_step(),
)
.connect_to(acc_txns_extractor?.into_runnable_step(), channel_size)
.connect_to(acc_txns_storer.into_runnable_step(), channel_size)
.connect_to(version_tracker.into_runnable_step(), channel_size)
.end_and_return_output_receiver(channel_size);

loop {
match buffer_receiver.recv().await {
Ok(txn_context) => {
debug!(
"Finished processing transactions from versions [{:?}, {:?}]",
txn_context.metadata.start_version, txn_context.metadata.end_version,
);
},
Err(e) => {
info!("No more transactions in channel: {:?}", e);
break Ok(());
},
}
}
}
}
1 change: 1 addition & 0 deletions rust/sdk-processor/src/processors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod account_transactions_processor;
pub mod ans_processor;
pub mod default_processor;
pub mod events_processor;
pub mod fungible_asset_processor;
Expand Down
Loading

0 comments on commit 9d44544

Please sign in to comment.