Skip to content

Commit 312280b

Browse files
authored
Add parquet_ans_processor (#455)
* Add parquet_ans_processor * add missing model file * add processor file * lint * fix build * rebase and lint * temp * rebase * fix processor name
1 parent fe207f5 commit 312280b

File tree

6 files changed

+448
-1
lines changed

6 files changed

+448
-1
lines changed

rust/processor/src/db/common/models/ans_models/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
pub mod ans_lookup;
55
pub mod ans_lookup_v2;
66
pub mod ans_utils;
7+
8+
// parquet models
9+
pub mod parquet_ans_lookup_v2;
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Copyright © Aptos Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// This is required because a diesel macro makes clippy sad
5+
#![allow(clippy::extra_unused_lifetimes)]
6+
#![allow(clippy::unused_unit)]
7+
8+
use crate::{
9+
bq_analytics::generic_parquet_processor::{GetTimeStamp, HasVersion, NamedTable},
10+
db::common::models::{
11+
ans_models::{
12+
ans_lookup::{AnsPrimaryName, CurrentAnsPrimaryName},
13+
ans_utils::SetReverseLookupEvent,
14+
},
15+
token_v2_models::v2_token_utils::TokenStandard,
16+
},
17+
};
18+
use allocative_derive::Allocative;
19+
use aptos_protos::transaction::v1::Event;
20+
use field_count::FieldCount;
21+
use parquet_derive::ParquetRecordWriter;
22+
use serde::{Deserialize, Serialize};
23+
24+
#[derive(
25+
Allocative, Clone, Default, Debug, Deserialize, FieldCount, ParquetRecordWriter, Serialize,
26+
)]
27+
pub struct AnsPrimaryNameV2 {
28+
pub txn_version: i64,
29+
pub write_set_change_index: i64,
30+
pub registered_address: String,
31+
pub token_standard: String,
32+
pub domain: Option<String>,
33+
pub subdomain: Option<String>,
34+
pub token_name: Option<String>,
35+
pub is_deleted: bool,
36+
#[allocative(skip)]
37+
pub block_timestamp: chrono::NaiveDateTime,
38+
}
39+
40+
impl NamedTable for AnsPrimaryNameV2 {
41+
const TABLE_NAME: &'static str = "ans_primary_name_v2";
42+
}
43+
44+
impl HasVersion for AnsPrimaryNameV2 {
45+
fn version(&self) -> i64 {
46+
self.txn_version
47+
}
48+
}
49+
50+
impl GetTimeStamp for AnsPrimaryNameV2 {
51+
fn get_timestamp(&self) -> chrono::NaiveDateTime {
52+
self.block_timestamp
53+
}
54+
}
55+
56+
#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
57+
pub struct CurrentAnsPrimaryNameV2 {
58+
pub registered_address: String,
59+
pub token_standard: String,
60+
pub domain: Option<String>,
61+
pub subdomain: Option<String>,
62+
pub token_name: Option<String>,
63+
pub is_deleted: bool,
64+
pub last_transaction_version: i64,
65+
}
66+
67+
impl Ord for CurrentAnsPrimaryNameV2 {
68+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
69+
self.registered_address.cmp(&other.registered_address)
70+
}
71+
}
72+
73+
impl PartialOrd for CurrentAnsPrimaryNameV2 {
74+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
75+
Some(self.cmp(other))
76+
}
77+
}
78+
79+
impl CurrentAnsPrimaryNameV2 {
80+
pub fn get_v2_from_v1(
81+
v1_current_primary_name: CurrentAnsPrimaryName,
82+
v1_primary_name: AnsPrimaryName,
83+
block_timestamp: chrono::NaiveDateTime,
84+
) -> (Self, AnsPrimaryNameV2) {
85+
(
86+
Self {
87+
registered_address: v1_current_primary_name.registered_address,
88+
token_standard: TokenStandard::V1.to_string(),
89+
domain: v1_current_primary_name.domain,
90+
subdomain: v1_current_primary_name.subdomain,
91+
token_name: v1_current_primary_name.token_name,
92+
is_deleted: v1_current_primary_name.is_deleted,
93+
last_transaction_version: v1_current_primary_name.last_transaction_version,
94+
},
95+
AnsPrimaryNameV2 {
96+
txn_version: v1_primary_name.transaction_version,
97+
write_set_change_index: v1_primary_name.write_set_change_index,
98+
registered_address: v1_primary_name.registered_address,
99+
token_standard: TokenStandard::V1.to_string(),
100+
domain: v1_primary_name.domain,
101+
subdomain: v1_primary_name.subdomain,
102+
token_name: v1_primary_name.token_name,
103+
is_deleted: v1_primary_name.is_deleted,
104+
block_timestamp,
105+
},
106+
)
107+
}
108+
109+
// Parse v2 primary name record from SetReverseLookupEvent
110+
pub fn parse_v2_primary_name_record_from_event(
111+
event: &Event,
112+
txn_version: i64,
113+
event_index: i64,
114+
ans_v2_contract_address: &str,
115+
block_timestamp: chrono::NaiveDateTime,
116+
) -> anyhow::Result<Option<(Self, AnsPrimaryNameV2)>> {
117+
if let Some(set_reverse_lookup_event) =
118+
SetReverseLookupEvent::from_event(event, ans_v2_contract_address, txn_version).unwrap()
119+
{
120+
if set_reverse_lookup_event.get_curr_domain_trunc().is_empty() {
121+
// Handle case where the address's primary name is unset
122+
return Ok(Some((
123+
Self {
124+
registered_address: set_reverse_lookup_event.get_account_addr().clone(),
125+
token_standard: TokenStandard::V2.to_string(),
126+
domain: None,
127+
subdomain: None,
128+
token_name: None,
129+
last_transaction_version: txn_version,
130+
is_deleted: true,
131+
},
132+
AnsPrimaryNameV2 {
133+
txn_version,
134+
write_set_change_index: -(event_index + 1),
135+
registered_address: set_reverse_lookup_event.get_account_addr().clone(),
136+
token_standard: TokenStandard::V2.to_string(),
137+
domain: None,
138+
subdomain: None,
139+
token_name: None,
140+
is_deleted: true,
141+
block_timestamp,
142+
},
143+
)));
144+
} else {
145+
// Handle case where the address is set to a new primary name
146+
return Ok(Some((
147+
Self {
148+
registered_address: set_reverse_lookup_event.get_account_addr().clone(),
149+
token_standard: TokenStandard::V2.to_string(),
150+
domain: Some(set_reverse_lookup_event.get_curr_domain_trunc()),
151+
subdomain: Some(set_reverse_lookup_event.get_curr_subdomain_trunc()),
152+
token_name: Some(set_reverse_lookup_event.get_curr_token_name()),
153+
last_transaction_version: txn_version,
154+
is_deleted: false,
155+
},
156+
AnsPrimaryNameV2 {
157+
txn_version,
158+
write_set_change_index: -(event_index + 1),
159+
registered_address: set_reverse_lookup_event.get_account_addr().clone(),
160+
token_standard: TokenStandard::V2.to_string(),
161+
domain: Some(set_reverse_lookup_event.get_curr_domain_trunc()),
162+
subdomain: Some(set_reverse_lookup_event.get_curr_subdomain_trunc()),
163+
token_name: Some(set_reverse_lookup_event.get_curr_token_name()),
164+
is_deleted: false,
165+
block_timestamp,
166+
},
167+
)));
168+
}
169+
}
170+
Ok(None)
171+
}
172+
}

rust/processor/src/processors/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::{
3636
db::common::models::processor_status::ProcessorStatus,
3737
gap_detectors::ProcessingResult,
3838
processors::parquet_processors::{
39+
parquet_ans_processor::{ParquetAnsProcessor, ParquetAnsProcessorConfig},
3940
parquet_default_processor::{ParquetDefaultProcessor, ParquetDefaultProcessorConfig},
4041
parquet_fungible_asset_processor::{
4142
ParquetFungibleAssetProcessor, ParquetFungibleAssetProcessorConfig,
@@ -198,6 +199,7 @@ pub enum ProcessorConfig {
198199
ParquetDefaultProcessor(ParquetDefaultProcessorConfig),
199200
ParquetFungibleAssetProcessor(ParquetFungibleAssetProcessorConfig),
200201
ParquetTransactionMetadataProcessor(ParquetTransactionMetadataProcessorConfig),
202+
ParquetAnsProcessor(ParquetAnsProcessorConfig),
201203
}
202204

203205
impl ProcessorConfig {
@@ -213,6 +215,7 @@ impl ProcessorConfig {
213215
ProcessorConfig::ParquetDefaultProcessor(_)
214216
| ProcessorConfig::ParquetFungibleAssetProcessor(_)
215217
| ProcessorConfig::ParquetTransactionMetadataProcessor(_)
218+
| ProcessorConfig::ParquetAnsProcessor(_)
216219
)
217220
}
218221
}
@@ -250,6 +253,7 @@ pub enum Processor {
250253
ParquetDefaultProcessor,
251254
ParquetFungibleAssetProcessor,
252255
ParquetTransactionMetadataProcessor,
256+
ParquetAnsProcessor,
253257
}
254258

255259
#[cfg(test)]

rust/processor/src/processors/parquet_processors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::time::Duration;
22

3+
pub mod parquet_ans_processor;
34
pub mod parquet_default_processor;
4-
55
pub mod parquet_fungible_asset_processor;
66
pub mod parquet_transaction_metadata_processor;
77

0 commit comments

Comments
 (0)