Skip to content

Commit 8881366

Browse files
committed
Add indexing support for delegation pool allowlist
Add indexing support for the delegation pool allowlist. EnableDelegatorsAllowlisting { pool_address } DisableDelegatorsAllowlisting { pool_address } AllowlistDelegator { pool_address, delegator_address } RemoveDelegatorFromAllowlist { pool_address, delegator_address }
1 parent 89844fb commit 8881366

File tree

8 files changed

+261
-4
lines changed

8 files changed

+261
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE IF EXISTS delegated_staking_pool_allowlist;
3+
DROP TABLE IF EXISTS current_delegated_staking_pool_allowlist;
4+
ALTER TABLE delegated_staking_pools DROP COLUMN IF EXISTS allowlist_enabled;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Your SQL goes here
2+
ALTER TABLE delegated_staking_pools
3+
ADD COLUMN IF NOT EXISTS allowlist_enabled BOOLEAN NOT NULL DEFAULT FALSE;
4+
5+
CREATE TABLE IF NOT EXISTS current_delegated_staking_pool_allowlist (
6+
staking_pool_address VARCHAR(66) NOT NULL,
7+
delegator_address VARCHAR(66) NOT NULL,
8+
-- Used for soft delete. On chain, it's a delete operation.
9+
is_allowed BOOLEAN NOT NULL DEFAULT FALSE,
10+
last_transaction_version BIGINT NOT NULL,
11+
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(),
12+
PRIMARY KEY (delegator_address, staking_pool_address)
13+
);
14+
15+
CREATE TABLE IF NOT EXISTS delegated_staking_pool_allowlist (
16+
staking_pool_address VARCHAR(66) NOT NULL,
17+
delegator_address VARCHAR(66) NOT NULL,
18+
-- Used for soft delete. On chain, it's a delete operation.
19+
is_allowed BOOLEAN NOT NULL DEFAULT FALSE,
20+
transaction_version BIGINT NOT NULL,
21+
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(),
22+
PRIMARY KEY (transaction_version, delegator_address, staking_pool_address)
23+
);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
7+
use super::stake_utils::StakeEvent;
8+
use crate::{
9+
schema::{current_delegated_staking_pool_allowlist, delegated_staking_pool_allowlist},
10+
utils::{counters::PROCESSOR_UNKNOWN_TYPE_COUNT, util::standardize_address},
11+
};
12+
use ahash::AHashMap;
13+
use aptos_protos::transaction::v1::{transaction::TxnData, Transaction};
14+
use field_count::FieldCount;
15+
use serde::{Deserialize, Serialize};
16+
17+
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)]
18+
#[diesel(primary_key(delegator_address, staking_pool_address))]
19+
#[diesel(table_name = current_delegated_staking_pool_allowlist)]
20+
pub struct CurrentDelegatedStakingPoolAllowlist {
21+
pub staking_pool_address: String,
22+
pub delegator_address: String,
23+
pub is_allowed: bool,
24+
last_transaction_version: i64,
25+
}
26+
27+
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)]
28+
#[diesel(primary_key(transaction_version, delegator_address, staking_pool_address))]
29+
#[diesel(table_name = delegated_staking_pool_allowlist)]
30+
pub struct DelegatedStakingPoolAllowlist {
31+
pub staking_pool_address: String,
32+
pub delegator_address: String,
33+
pub is_allowed: bool,
34+
transaction_version: i64,
35+
}
36+
37+
impl CurrentDelegatedStakingPoolAllowlist {
38+
pub fn from_transaction(
39+
transaction: &Transaction,
40+
) -> anyhow::Result<AHashMap<(String, String), Self>> {
41+
let mut delegated_staking_pool_allowlist = AHashMap::new();
42+
let txn_data = match transaction.txn_data.as_ref() {
43+
Some(data) => data,
44+
None => {
45+
PROCESSOR_UNKNOWN_TYPE_COUNT
46+
.with_label_values(&["DelegatedStakingPoolAllowlist"])
47+
.inc();
48+
tracing::warn!(
49+
transaction_version = transaction.version,
50+
"Transaction data doesn't exist",
51+
);
52+
return Ok(delegated_staking_pool_allowlist);
53+
},
54+
};
55+
let txn_version = transaction.version as i64;
56+
57+
if let TxnData::User(user_txn) = txn_data {
58+
for event in &user_txn.events {
59+
if let Some(StakeEvent::AllowlistDelegatorEvent(ev)) =
60+
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)?
61+
{
62+
let current_delegated_staking_pool_allowlist =
63+
CurrentDelegatedStakingPoolAllowlist {
64+
last_transaction_version: txn_version,
65+
staking_pool_address: standardize_address(&ev.pool_address),
66+
delegator_address: standardize_address(&ev.delegator_address),
67+
is_allowed: ev.enabled,
68+
};
69+
delegated_staking_pool_allowlist.insert(
70+
(
71+
current_delegated_staking_pool_allowlist
72+
.delegator_address
73+
.clone(),
74+
current_delegated_staking_pool_allowlist
75+
.staking_pool_address
76+
.clone(),
77+
),
78+
current_delegated_staking_pool_allowlist,
79+
);
80+
}
81+
}
82+
}
83+
Ok(delegated_staking_pool_allowlist)
84+
}
85+
}
86+
87+
impl DelegatedStakingPoolAllowlist {
88+
pub fn from_transaction(transaction: &Transaction) -> anyhow::Result<Vec<Self>> {
89+
let mut delegated_staking_pool_allowlist = vec![];
90+
let txn_data = match transaction.txn_data.as_ref() {
91+
Some(data) => data,
92+
None => {
93+
PROCESSOR_UNKNOWN_TYPE_COUNT
94+
.with_label_values(&["DelegatedStakingPoolAllowlist"])
95+
.inc();
96+
tracing::warn!(
97+
transaction_version = transaction.version,
98+
"Transaction data doesn't exist",
99+
);
100+
return Ok(delegated_staking_pool_allowlist);
101+
},
102+
};
103+
let txn_version = transaction.version as i64;
104+
105+
if let TxnData::User(user_txn) = txn_data {
106+
for event in &user_txn.events {
107+
if let Some(StakeEvent::AllowlistDelegatorEvent(ev)) =
108+
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)?
109+
{
110+
delegated_staking_pool_allowlist.push(Self {
111+
transaction_version: txn_version,
112+
staking_pool_address: standardize_address(&ev.pool_address),
113+
delegator_address: standardize_address(&ev.delegator_address),
114+
is_allowed: ev.enabled,
115+
});
116+
}
117+
}
118+
}
119+
Ok(delegated_staking_pool_allowlist)
120+
}
121+
}

rust/processor/src/db/common/models/stake_models/delegator_pools.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// This is required because a diesel macro makes clippy sad
55
#![allow(clippy::extra_unused_lifetimes)]
66

7-
use super::stake_utils::{StakeResource, StakeTableItem};
7+
use super::stake_utils::{StakeEvent, StakeResource, StakeTableItem};
88
use crate::{
99
schema::{
1010
current_delegated_staking_pool_balances, delegated_staking_pool_balances,
@@ -30,7 +30,10 @@ pub type DelegatorPoolBalanceMap = AHashMap<StakingPoolAddress, CurrentDelegator
3030
#[diesel(table_name = delegated_staking_pools)]
3131
pub struct DelegatorPool {
3232
pub staking_pool_address: String,
33+
// We should add a new field like `last_transaction_version` to track the last transaction version
34+
// that updated the pool
3335
pub first_transaction_version: i64,
36+
pub allowlist_enabled: bool,
3437
}
3538

3639
// Metadata to fill pool balances and delegator balance
@@ -136,6 +139,41 @@ impl DelegatorPool {
136139
}
137140
}
138141
}
142+
let txn_version = transaction.version as i64;
143+
144+
let events = match txn_data {
145+
TxnData::User(txn) => &txn.events,
146+
TxnData::BlockMetadata(txn) => &txn.events,
147+
_ => {
148+
return Ok((
149+
delegator_pool_map,
150+
delegator_pool_balances,
151+
delegator_pool_balances_map,
152+
))
153+
},
154+
};
155+
156+
for event in events {
157+
if let Some(StakeEvent::AllowlistingEvent(inner)) =
158+
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)?
159+
{
160+
let staking_pool_address = standardize_address(&inner.pool_address);
161+
let enabled = inner.enabled;
162+
if delegator_pool_map.contains_key(&staking_pool_address) {
163+
delegator_pool_map
164+
.get_mut(&staking_pool_address)
165+
.expect("Pool should exist")
166+
.allowlist_enabled = enabled;
167+
} else {
168+
let pool = DelegatorPool {
169+
staking_pool_address: staking_pool_address.clone(),
170+
first_transaction_version: txn_version,
171+
allowlist_enabled: enabled,
172+
};
173+
delegator_pool_map.insert(staking_pool_address.clone(), pool);
174+
}
175+
}
176+
}
139177
}
140178
Ok((
141179
delegator_pool_map,
@@ -211,6 +249,7 @@ impl DelegatorPool {
211249
Self {
212250
staking_pool_address: staking_pool_address.clone(),
213251
first_transaction_version: transaction_version,
252+
allowlist_enabled: false,
214253
},
215254
DelegatorPoolBalance {
216255
transaction_version,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
pub mod current_delegated_voter;
55
pub mod delegator_activities;
6+
pub mod delegator_allowlist;
67
pub mod delegator_balances;
78
pub mod delegator_pools;
89
pub mod proposal_votes;

rust/processor/src/db/common/models/stake_models/stake_utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ pub struct ReactivateStakeEvent {
103103
pub delegator_address: String,
104104
pub pool_address: String,
105105
}
106+
#[derive(Serialize, Deserialize, Debug, Clone)]
107+
pub struct EnableAllowlistingEvent {
108+
pub pool_address: String,
109+
pub enabled: bool,
110+
}
111+
112+
#[derive(Serialize, Deserialize, Debug, Clone)]
113+
pub struct EnableDelegatorAllowlistingEvent {
114+
pub pool_address: String,
115+
pub delegator_address: String,
116+
pub enabled: bool,
117+
}
106118

107119
#[derive(Serialize, Deserialize, Debug, Clone)]
108120
pub enum StakeTableItem {
@@ -195,6 +207,8 @@ pub enum StakeEvent {
195207
UnlockStakeEvent(UnlockStakeEvent),
196208
WithdrawStakeEvent(WithdrawStakeEvent),
197209
ReactivateStakeEvent(ReactivateStakeEvent),
210+
AllowlistingEvent(EnableAllowlistingEvent),
211+
AllowlistDelegatorEvent(EnableDelegatorAllowlistingEvent),
198212
}
199213

200214
impl StakeEvent {
@@ -216,6 +230,13 @@ impl StakeEvent {
216230
},
217231
"0x1::delegation_pool::ReactivateStakeEvent" => serde_json::from_str(data)
218232
.map(|inner| Some(StakeEvent::ReactivateStakeEvent(inner))),
233+
"0x1::delegation_pool::EnableDelegatorsAllowlisting"
234+
| "0x1::delegation_pool::DisableDelegatorsAllowlisting" => {
235+
serde_json::from_str(data).map(|inner| Some(StakeEvent::AllowlistingEvent(inner)))
236+
},
237+
"0x1::delegation_pool::AllowlistDelegator"
238+
| "0x1::delegation_pool::RemoveDelegatorFromAllowlist" => serde_json::from_str(data)
239+
.map(|inner| Some(StakeEvent::AllowlistDelegatorEvent(inner))),
219240
_ => Ok(None),
220241
}
221242
.context(format!(

rust/processor/src/db/postgres/schema.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ diesel::table! {
369369
}
370370
}
371371

372+
diesel::table! {
373+
current_delegated_staking_pool_allowlist (delegator_address, staking_pool_address) {
374+
#[max_length = 66]
375+
staking_pool_address -> Varchar,
376+
#[max_length = 66]
377+
delegator_address -> Varchar,
378+
is_allowed -> Bool,
379+
last_transaction_version -> Int8,
380+
inserted_at -> Timestamp,
381+
}
382+
}
383+
372384
diesel::table! {
373385
current_delegated_staking_pool_balances (staking_pool_address) {
374386
#[max_length = 66]
@@ -694,6 +706,18 @@ diesel::table! {
694706
}
695707
}
696708

709+
diesel::table! {
710+
delegated_staking_pool_allowlist (transaction_version, delegator_address, staking_pool_address) {
711+
#[max_length = 66]
712+
staking_pool_address -> Varchar,
713+
#[max_length = 66]
714+
delegator_address -> Varchar,
715+
is_allowed -> Bool,
716+
transaction_version -> Int8,
717+
inserted_at -> Timestamp,
718+
}
719+
}
720+
697721
diesel::table! {
698722
delegated_staking_pool_balances (transaction_version, staking_pool_address) {
699723
transaction_version -> Int8,
@@ -716,6 +740,7 @@ diesel::table! {
716740
staking_pool_address -> Varchar,
717741
first_transaction_version -> Int8,
718742
inserted_at -> Timestamp,
743+
allowlist_enabled -> Bool,
719744
}
720745
}
721746

@@ -1300,6 +1325,7 @@ diesel::allow_tables_to_appear_in_same_query!(
13001325
current_coin_balances,
13011326
current_collection_datas,
13021327
current_collections_v2,
1328+
current_delegated_staking_pool_allowlist,
13031329
current_delegated_staking_pool_balances,
13041330
current_delegated_voter,
13051331
current_delegator_balances,
@@ -1316,6 +1342,7 @@ diesel::allow_tables_to_appear_in_same_query!(
13161342
current_token_v2_metadata,
13171343
current_unified_fungible_asset_balances_to_be_renamed,
13181344
delegated_staking_activities,
1345+
delegated_staking_pool_allowlist,
13191346
delegated_staking_pool_balances,
13201347
delegated_staking_pools,
13211348
delegator_balances,

rust/processor/src/processors/stake_processor.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use crate::{
66
db::common::models::stake_models::{
77
current_delegated_voter::CurrentDelegatedVoter,
88
delegator_activities::DelegatedStakingActivity,
9+
delegator_allowlist::{
10+
CurrentDelegatedStakingPoolAllowlist, DelegatedStakingPoolAllowlist,
11+
},
912
delegator_balances::{
1013
CurrentDelegatorBalance, CurrentDelegatorBalanceMap, DelegatorBalance,
1114
},
@@ -303,8 +306,9 @@ fn insert_delegator_pools_query(
303306
.on_conflict(staking_pool_address)
304307
.do_update()
305308
.set((
306-
first_transaction_version.eq(excluded(first_transaction_version)),
307-
inserted_at.eq(excluded(inserted_at)),
309+
first_transaction_version.eq(excluded(first_transaction_version)),
310+
allowlist_enabled.eq(excluded(allowlist_enabled)),
311+
inserted_at.eq(excluded(inserted_at)),
308312
)),
309313
Some(
310314
" WHERE delegated_staking_pools.first_transaction_version >= EXCLUDED.first_transaction_version ",
@@ -416,6 +420,9 @@ impl ProcessorTrait for StakeProcessor {
416420
let mut all_current_delegated_voter = AHashMap::new();
417421
let mut all_vote_delegation_handle_to_pool_address = AHashMap::new();
418422

423+
let mut all_delegator_allowlist = vec![];
424+
let mut all_current_delegator_allowlist = AHashMap::new();
425+
419426
for txn in &transactions {
420427
// Add votes data
421428
let current_stake_pool_voter = CurrentStakingPoolVoter::from_transaction(txn).unwrap();
@@ -430,10 +437,24 @@ impl ProcessorTrait for StakeProcessor {
430437
// Add delegator pools
431438
let (delegator_pools, mut delegator_pool_balances, current_delegator_pool_balances) =
432439
DelegatorPool::from_transaction(txn).unwrap();
433-
all_delegator_pools.extend(delegator_pools);
440+
for (pool_address, pool) in delegator_pools.iter() {
441+
// We need to keep the first transaction version for each pool.
442+
if let Some(existing_pool) = all_delegator_pools.get_mut(pool_address) {
443+
existing_pool.allowlist_enabled = pool.allowlist_enabled;
444+
} else {
445+
all_delegator_pools.insert(pool_address.clone(), pool.clone());
446+
}
447+
}
434448
all_delegator_pool_balances.append(&mut delegator_pool_balances);
435449
all_current_delegator_pool_balances.extend(current_delegator_pool_balances);
436450

451+
// Add delegator pool allowlist.
452+
let delegator_allowlist = DelegatedStakingPoolAllowlist::from_transaction(txn).unwrap();
453+
all_delegator_allowlist.extend(delegator_allowlist);
454+
let current_delegator_allowlist =
455+
CurrentDelegatedStakingPoolAllowlist::from_transaction(txn).unwrap();
456+
all_current_delegator_allowlist.extend(current_delegator_allowlist);
457+
437458
// Moving the transaction code here is the new paradigm to avoid redoing a lot of the duplicate work
438459
// Currently only delegator voting follows this paradigm
439460
// TODO: refactor all the other staking code to follow this paradigm

0 commit comments

Comments
 (0)