-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 }
- Loading branch information
1 parent
89844fb
commit 8881366
Showing
8 changed files
with
261 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
rust/processor/migrations/2024-05-01-211023_delegator_allowlist/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE IF EXISTS delegated_staking_pool_allowlist; | ||
DROP TABLE IF EXISTS current_delegated_staking_pool_allowlist; | ||
ALTER TABLE delegated_staking_pools DROP COLUMN IF EXISTS allowlist_enabled; |
23 changes: 23 additions & 0 deletions
23
rust/processor/migrations/2024-05-01-211023_delegator_allowlist/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- Your SQL goes here | ||
ALTER TABLE delegated_staking_pools | ||
ADD COLUMN IF NOT EXISTS allowlist_enabled BOOLEAN NOT NULL DEFAULT FALSE; | ||
|
||
CREATE TABLE IF NOT EXISTS current_delegated_staking_pool_allowlist ( | ||
staking_pool_address VARCHAR(66) NOT NULL, | ||
delegator_address VARCHAR(66) NOT NULL, | ||
-- Used for soft delete. On chain, it's a delete operation. | ||
is_allowed BOOLEAN NOT NULL DEFAULT FALSE, | ||
last_transaction_version BIGINT NOT NULL, | ||
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (delegator_address, staking_pool_address) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS delegated_staking_pool_allowlist ( | ||
staking_pool_address VARCHAR(66) NOT NULL, | ||
delegator_address VARCHAR(66) NOT NULL, | ||
-- Used for soft delete. On chain, it's a delete operation. | ||
is_allowed BOOLEAN NOT NULL DEFAULT FALSE, | ||
transaction_version BIGINT NOT NULL, | ||
inserted_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (transaction_version, delegator_address, staking_pool_address) | ||
); |
121 changes: 121 additions & 0 deletions
121
rust/processor/src/db/common/models/stake_models/delegator_allowlist.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This is required because a diesel macro makes clippy sad | ||
#![allow(clippy::extra_unused_lifetimes)] | ||
|
||
use super::stake_utils::StakeEvent; | ||
use crate::{ | ||
schema::{current_delegated_staking_pool_allowlist, delegated_staking_pool_allowlist}, | ||
utils::{counters::PROCESSOR_UNKNOWN_TYPE_COUNT, util::standardize_address}, | ||
}; | ||
use ahash::AHashMap; | ||
use aptos_protos::transaction::v1::{transaction::TxnData, Transaction}; | ||
use field_count::FieldCount; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] | ||
#[diesel(primary_key(delegator_address, staking_pool_address))] | ||
#[diesel(table_name = current_delegated_staking_pool_allowlist)] | ||
pub struct CurrentDelegatedStakingPoolAllowlist { | ||
pub staking_pool_address: String, | ||
pub delegator_address: String, | ||
pub is_allowed: bool, | ||
last_transaction_version: i64, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] | ||
#[diesel(primary_key(transaction_version, delegator_address, staking_pool_address))] | ||
#[diesel(table_name = delegated_staking_pool_allowlist)] | ||
pub struct DelegatedStakingPoolAllowlist { | ||
pub staking_pool_address: String, | ||
pub delegator_address: String, | ||
pub is_allowed: bool, | ||
transaction_version: i64, | ||
} | ||
|
||
impl CurrentDelegatedStakingPoolAllowlist { | ||
pub fn from_transaction( | ||
transaction: &Transaction, | ||
) -> anyhow::Result<AHashMap<(String, String), Self>> { | ||
let mut delegated_staking_pool_allowlist = AHashMap::new(); | ||
let txn_data = match transaction.txn_data.as_ref() { | ||
Some(data) => data, | ||
None => { | ||
PROCESSOR_UNKNOWN_TYPE_COUNT | ||
.with_label_values(&["DelegatedStakingPoolAllowlist"]) | ||
.inc(); | ||
tracing::warn!( | ||
transaction_version = transaction.version, | ||
"Transaction data doesn't exist", | ||
); | ||
return Ok(delegated_staking_pool_allowlist); | ||
}, | ||
}; | ||
let txn_version = transaction.version as i64; | ||
|
||
if let TxnData::User(user_txn) = txn_data { | ||
for event in &user_txn.events { | ||
if let Some(StakeEvent::AllowlistDelegatorEvent(ev)) = | ||
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)? | ||
{ | ||
let current_delegated_staking_pool_allowlist = | ||
CurrentDelegatedStakingPoolAllowlist { | ||
last_transaction_version: txn_version, | ||
staking_pool_address: standardize_address(&ev.pool_address), | ||
delegator_address: standardize_address(&ev.delegator_address), | ||
is_allowed: ev.enabled, | ||
}; | ||
delegated_staking_pool_allowlist.insert( | ||
( | ||
current_delegated_staking_pool_allowlist | ||
.delegator_address | ||
.clone(), | ||
current_delegated_staking_pool_allowlist | ||
.staking_pool_address | ||
.clone(), | ||
), | ||
current_delegated_staking_pool_allowlist, | ||
); | ||
} | ||
} | ||
} | ||
Ok(delegated_staking_pool_allowlist) | ||
} | ||
} | ||
|
||
impl DelegatedStakingPoolAllowlist { | ||
pub fn from_transaction(transaction: &Transaction) -> anyhow::Result<Vec<Self>> { | ||
let mut delegated_staking_pool_allowlist = vec![]; | ||
let txn_data = match transaction.txn_data.as_ref() { | ||
Some(data) => data, | ||
None => { | ||
PROCESSOR_UNKNOWN_TYPE_COUNT | ||
.with_label_values(&["DelegatedStakingPoolAllowlist"]) | ||
.inc(); | ||
tracing::warn!( | ||
transaction_version = transaction.version, | ||
"Transaction data doesn't exist", | ||
); | ||
return Ok(delegated_staking_pool_allowlist); | ||
}, | ||
}; | ||
let txn_version = transaction.version as i64; | ||
|
||
if let TxnData::User(user_txn) = txn_data { | ||
for event in &user_txn.events { | ||
if let Some(StakeEvent::AllowlistDelegatorEvent(ev)) = | ||
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)? | ||
{ | ||
delegated_staking_pool_allowlist.push(Self { | ||
transaction_version: txn_version, | ||
staking_pool_address: standardize_address(&ev.pool_address), | ||
delegator_address: standardize_address(&ev.delegator_address), | ||
is_allowed: ev.enabled, | ||
}); | ||
} | ||
} | ||
} | ||
Ok(delegated_staking_pool_allowlist) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters