-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[indexer-alt] add transaction indices pipelines (#19953)
## Description This PR adds schema and pipeline for various transaction indices including - tx_affected_addresses (replacing tx_senders and tx_recipients) - tx_kinds - tx_digests - tx_calls_fun (collapsing into one) ## Test plan cargo run -- \ --database-url "postgres://postgres:postgrespw@localhost:5432/sui_indexer_alt" \ --remote-store-url https://checkpoints.mainnet.sui.io \ --last-checkpoint 10000 --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK: - [ ] REST API: --------- Co-authored-by: Ashok Menon <[email protected]>
- Loading branch information
Showing
14 changed files
with
459 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 2 additions & 1 deletion
3
crates/sui-indexer-alt/migrations/2024-10-16-002409_tx_affected_objects/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
4 changes: 4 additions & 0 deletions
4
crates/sui-indexer-alt/migrations/2024-10-21-003426_tx_indices/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 @@ | ||
DROP TABLE IF EXISTS tx_affected_addresses; | ||
DROP TABLE IF EXISTS tx_calls_fun; | ||
DROP TABLE IF EXISTS tx_digests; | ||
DROP TABLE IF EXISTS tx_kinds; |
57 changes: 57 additions & 0 deletions
57
crates/sui-indexer-alt/migrations/2024-10-21-003426_tx_indices/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,57 @@ | ||
CREATE TABLE IF NOT EXISTS tx_affected_addresses | ||
( | ||
affected BYTEA NOT NULL, | ||
tx_sequence_number BIGINT NOT NULL, | ||
sender BYTEA NOT NULL, | ||
PRIMARY KEY (affected, tx_sequence_number) | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_affected_addresses_tx_sequence_number | ||
ON tx_affected_addresses (tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_affected_addresses_sender | ||
ON tx_affected_addresses (sender, affected, tx_sequence_number); | ||
|
||
CREATE TABLE IF NOT EXISTS tx_digests | ||
( | ||
tx_sequence_number BIGINT PRIMARY KEY, | ||
tx_digest BYTEA NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS tx_kinds | ||
( | ||
tx_kind SMALLINT NOT NULL, | ||
tx_sequence_number BIGINT NOT NULL, | ||
PRIMARY KEY (tx_kind, tx_sequence_number) | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_kinds_tx_sequence_number | ||
ON tx_kinds (tx_sequence_number); | ||
|
||
CREATE TABLE IF NOT EXISTS tx_calls | ||
( | ||
package BYTEA NOT NULL, | ||
module TEXT NOT NULL, | ||
function TEXT NOT NULL, | ||
tx_sequence_number BIGINT NOT NULL, | ||
sender BYTEA NOT NULL, | ||
PRIMARY KEY (package, module, function, tx_sequence_number) | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_tx_sequence_number | ||
ON tx_calls (tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_fun_sender | ||
ON tx_calls (sender, package, module, function, tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_mod | ||
ON tx_calls (package, module, tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_mod_sender | ||
ON tx_calls (sender, package, module, tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_pkg | ||
ON tx_calls (package, tx_sequence_number); | ||
|
||
CREATE INDEX IF NOT EXISTS tx_calls_pkg_sender | ||
ON tx_calls (sender, package, tx_sequence_number); |
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
73 changes: 73 additions & 0 deletions
73
crates/sui-indexer-alt/src/handlers/tx_affected_addresses.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,73 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use diesel_async::RunQueryDsl; | ||
use itertools::Itertools; | ||
use sui_types::{full_checkpoint_content::CheckpointData, object::Owner}; | ||
|
||
use crate::{ | ||
db, models::transactions::StoredTxAffectedAddress, pipeline::concurrent::Handler, | ||
pipeline::Processor, schema::tx_affected_addresses, | ||
}; | ||
|
||
pub struct TxAffectedAddress; | ||
|
||
impl Processor for TxAffectedAddress { | ||
const NAME: &'static str = "tx_affected_addresses"; | ||
|
||
type Value = StoredTxAffectedAddress; | ||
|
||
fn process(checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let CheckpointData { | ||
transactions, | ||
checkpoint_summary, | ||
.. | ||
} = checkpoint.as_ref(); | ||
|
||
let mut values = Vec::new(); | ||
let first_tx = checkpoint_summary.network_total_transactions as usize - transactions.len(); | ||
|
||
for (i, tx) in transactions.iter().enumerate() { | ||
let tx_sequence_number = (first_tx + i) as i64; | ||
let sender = tx.transaction.sender_address(); | ||
let payer = tx.transaction.gas_owner(); | ||
let recipients = tx.effects.all_changed_objects().into_iter().filter_map( | ||
|(_object_ref, owner, _write_kind)| match owner { | ||
Owner::AddressOwner(address) => Some(address), | ||
_ => None, | ||
}, | ||
); | ||
|
||
let affected_addresses: Vec<StoredTxAffectedAddress> = recipients | ||
.chain(vec![sender, payer]) | ||
.unique() | ||
.map(|a| StoredTxAffectedAddress { | ||
tx_sequence_number, | ||
affected: a.to_vec(), | ||
sender: sender.to_vec(), | ||
}) | ||
.collect(); | ||
values.extend(affected_addresses); | ||
} | ||
|
||
Ok(values) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for TxAffectedAddress { | ||
const MIN_EAGER_ROWS: usize = 100; | ||
const MAX_CHUNK_ROWS: usize = 1000; | ||
const MAX_PENDING_ROWS: usize = 10000; | ||
|
||
async fn commit(values: &[Self::Value], conn: &mut db::Connection<'_>) -> Result<usize> { | ||
Ok(diesel::insert_into(tx_affected_addresses::table) | ||
.values(values) | ||
.on_conflict_do_nothing() | ||
.execute(conn) | ||
.await?) | ||
} | ||
} |
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,68 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::Arc; | ||
|
||
use anyhow::{Ok, Result}; | ||
use diesel_async::RunQueryDsl; | ||
use sui_types::full_checkpoint_content::CheckpointData; | ||
use sui_types::transaction::TransactionDataAPI; | ||
|
||
use crate::{ | ||
db, models::transactions::StoredTxCalls, pipeline::concurrent::Handler, pipeline::Processor, | ||
schema::tx_calls, | ||
}; | ||
|
||
pub struct TxCallsFun; | ||
|
||
impl Processor for TxCallsFun { | ||
const NAME: &'static str = "tx_calls_fun"; | ||
|
||
type Value = StoredTxCalls; | ||
|
||
fn process(checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let CheckpointData { | ||
transactions, | ||
checkpoint_summary, | ||
.. | ||
} = checkpoint.as_ref(); | ||
|
||
let first_tx = checkpoint_summary.network_total_transactions as usize - transactions.len(); | ||
|
||
Ok(transactions | ||
.iter() | ||
.enumerate() | ||
.flat_map(|(i, tx)| { | ||
let tx_sequence_number = (first_tx + i) as i64; | ||
let sender = tx.transaction.sender_address().to_vec(); | ||
let calls = tx.transaction.data().transaction_data().move_calls(); | ||
|
||
calls | ||
.iter() | ||
.map(|(package, module, function)| StoredTxCalls { | ||
tx_sequence_number, | ||
package: package.to_vec(), | ||
module: module.to_string(), | ||
function: function.to_string(), | ||
sender: sender.clone(), | ||
}) | ||
.collect::<Vec<_>>() | ||
}) | ||
.collect()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for TxCallsFun { | ||
const MIN_EAGER_ROWS: usize = 100; | ||
const MAX_CHUNK_ROWS: usize = 1000; | ||
const MAX_PENDING_ROWS: usize = 10000; | ||
|
||
async fn commit(values: &[Self::Value], conn: &mut db::Connection<'_>) -> Result<usize> { | ||
Ok(diesel::insert_into(tx_calls::table) | ||
.values(values) | ||
.on_conflict_do_nothing() | ||
.execute(conn) | ||
.await?) | ||
} | ||
} |
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,55 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use diesel_async::RunQueryDsl; | ||
use sui_types::full_checkpoint_content::CheckpointData; | ||
|
||
use crate::{ | ||
db, models::transactions::StoredTxDigest, pipeline::concurrent::Handler, pipeline::Processor, | ||
schema::tx_digests, | ||
}; | ||
|
||
pub struct TxDigests; | ||
|
||
impl Processor for TxDigests { | ||
const NAME: &'static str = "tx_digests"; | ||
|
||
type Value = StoredTxDigest; | ||
|
||
fn process(checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let CheckpointData { | ||
transactions, | ||
checkpoint_summary, | ||
.. | ||
} = checkpoint.as_ref(); | ||
|
||
let first_tx = checkpoint_summary.network_total_transactions as usize - transactions.len(); | ||
|
||
Ok(transactions | ||
.iter() | ||
.enumerate() | ||
.map(|(i, tx)| StoredTxDigest { | ||
tx_sequence_number: (first_tx + i) as i64, | ||
tx_digest: tx.transaction.digest().inner().to_vec(), | ||
}) | ||
.collect()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for TxDigests { | ||
const MIN_EAGER_ROWS: usize = 100; | ||
const MAX_CHUNK_ROWS: usize = 1000; | ||
const MAX_PENDING_ROWS: usize = 10000; | ||
|
||
async fn commit(values: &[Self::Value], conn: &mut db::Connection<'_>) -> Result<usize> { | ||
Ok(diesel::insert_into(tx_digests::table) | ||
.values(values) | ||
.on_conflict_do_nothing() | ||
.execute(conn) | ||
.await?) | ||
} | ||
} |
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,65 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use diesel_async::RunQueryDsl; | ||
use sui_types::full_checkpoint_content::CheckpointData; | ||
|
||
use crate::{ | ||
db, | ||
models::transactions::{StoredKind, StoredTxKind}, | ||
pipeline::{concurrent::Handler, Processor}, | ||
schema::tx_kinds, | ||
}; | ||
|
||
pub struct TxKinds; | ||
|
||
impl Processor for TxKinds { | ||
const NAME: &'static str = "tx_kinds"; | ||
|
||
type Value = StoredTxKind; | ||
|
||
fn process(checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let CheckpointData { | ||
transactions, | ||
checkpoint_summary, | ||
.. | ||
} = checkpoint.as_ref(); | ||
|
||
let mut values = Vec::new(); | ||
let first_tx = checkpoint_summary.network_total_transactions as usize - transactions.len(); | ||
|
||
for (i, tx) in transactions.iter().enumerate() { | ||
let tx_sequence_number = (first_tx + i) as i64; | ||
let tx_kind = if tx.transaction.is_system_tx() { | ||
StoredKind::SystemTransaction | ||
} else { | ||
StoredKind::ProgrammableTransaction | ||
}; | ||
|
||
values.push(StoredTxKind { | ||
tx_sequence_number, | ||
tx_kind, | ||
}); | ||
} | ||
|
||
Ok(values) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for TxKinds { | ||
const MIN_EAGER_ROWS: usize = 100; | ||
const MAX_CHUNK_ROWS: usize = 1000; | ||
const MAX_PENDING_ROWS: usize = 10000; | ||
|
||
async fn commit(values: &[Self::Value], conn: &mut db::Connection<'_>) -> Result<usize> { | ||
Ok(diesel::insert_into(tx_kinds::table) | ||
.values(values) | ||
.on_conflict_do_nothing() | ||
.execute(conn) | ||
.await?) | ||
} | ||
} |
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
Oops, something went wrong.