diff --git a/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/down.sql b/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/down.sql new file mode 100644 index 0000000000000..b0868da73b0f2 --- /dev/null +++ b/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS tx_affected_objects; diff --git a/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/up.sql b/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/up.sql new file mode 100644 index 0000000000000..146f78b2f5063 --- /dev/null +++ b/crates/sui-indexer/migrations/pg/2024-09-19-121113_tx_affected_objects/up.sql @@ -0,0 +1,9 @@ +CREATE TABLE tx_affected_objects ( + tx_sequence_number BIGINT NOT NULL, + affected BYTEA NOT NULL, + sender BYTEA NOT NULL, + PRIMARY KEY(affected, tx_sequence_number) +); + +CREATE INDEX tx_affected_objects_tx_sequence_number_index ON tx_affected_objects (tx_sequence_number); +CREATE INDEX tx_affected_objects_sender ON tx_affected_objects (sender, affected, tx_sequence_number); diff --git a/crates/sui-indexer/src/models/tx_indices.rs b/crates/sui-indexer/src/models/tx_indices.rs index de3ccc817826f..89e37c49af88d 100644 --- a/crates/sui-indexer/src/models/tx_indices.rs +++ b/crates/sui-indexer/src/models/tx_indices.rs @@ -3,8 +3,8 @@ use crate::{ schema::{ - tx_affected_addresses, tx_calls_fun, tx_calls_mod, tx_calls_pkg, tx_changed_objects, - tx_digests, tx_input_objects, tx_kinds, tx_recipients, tx_senders, + tx_affected_addresses, tx_affected_objects, tx_calls_fun, tx_calls_mod, tx_calls_pkg, + tx_changed_objects, tx_digests, tx_input_objects, tx_kinds, tx_recipients, tx_senders, }, types::TxIndex, }; @@ -25,7 +25,15 @@ pub struct TxDigest { #[derive(Queryable, Insertable, Selectable, Debug, Clone, Default)] #[diesel(table_name = tx_affected_addresses)] -pub struct StoredTxAffected { +pub struct StoredTxAffectedAddresses { + pub tx_sequence_number: i64, + pub affected: Vec, + pub sender: Vec, +} + +#[derive(Queryable, Insertable, Selectable, Debug, Clone, Default)] +#[diesel(table_name = tx_affected_objects)] +pub struct StoredTxAffectedObjects { pub tx_sequence_number: i64, pub affected: Vec, pub sender: Vec, @@ -108,7 +116,8 @@ impl TxIndex { pub fn split( self: TxIndex, ) -> ( - Vec, + Vec, + Vec, Vec, Vec, Vec, @@ -120,19 +129,32 @@ impl TxIndex { Vec, ) { let tx_sequence_number = self.tx_sequence_number as i64; + let tx_affected_addresses = self .recipients .iter() .chain(self.payers.iter()) .chain(std::iter::once(&self.sender)) .unique() - .map(|a| StoredTxAffected { + .map(|a| StoredTxAffectedAddresses { tx_sequence_number, affected: a.to_vec(), sender: self.sender.to_vec(), }) .collect(); + let tx_affected_objects = self + .input_objects + .iter() + .chain(self.changed_objects.iter()) + .unique() + .map(|o| StoredTxAffectedObjects { + tx_sequence_number, + affected: o.to_vec(), + sender: self.sender.to_vec(), + }) + .collect(); + let tx_sender = StoredTxSenders { tx_sequence_number, sender: self.sender.to_vec(), @@ -224,6 +246,7 @@ impl TxIndex { ( tx_affected_addresses, + tx_affected_objects, vec![tx_sender], tx_recipients, tx_input_objects, diff --git a/crates/sui-indexer/src/schema.rs b/crates/sui-indexer/src/schema.rs index ebbbd4bbd7b1c..b2d3bcfb9edbf 100644 --- a/crates/sui-indexer/src/schema.rs +++ b/crates/sui-indexer/src/schema.rs @@ -288,6 +288,14 @@ diesel::table! { } } +diesel::table! { + tx_affected_objects (affected, tx_sequence_number) { + tx_sequence_number -> Int8, + affected -> Bytea, + sender -> Bytea, + } +} + diesel::table! { tx_calls_fun (package, module, func, tx_sequence_number) { tx_sequence_number -> Int8, @@ -384,6 +392,7 @@ diesel::allow_tables_to_appear_in_same_query!( pruner_cp_watermark, transactions, tx_affected_addresses, + tx_affected_objects, tx_calls_fun, tx_calls_mod, tx_calls_pkg, diff --git a/crates/sui-indexer/src/store/pg_indexer_store.rs b/crates/sui-indexer/src/store/pg_indexer_store.rs index 480e3d34d8adf..079769d9a12bd 100644 --- a/crates/sui-indexer/src/store/pg_indexer_store.rs +++ b/crates/sui-indexer/src/store/pg_indexer_store.rs @@ -50,8 +50,9 @@ use crate::schema::{ event_senders, event_struct_instantiation, event_struct_module, event_struct_name, event_struct_package, events, feature_flags, full_objects_history, objects, objects_history, objects_snapshot, objects_version, packages, protocol_configs, pruner_cp_watermark, - transactions, tx_affected_addresses, tx_calls_fun, tx_calls_mod, tx_calls_pkg, - tx_changed_objects, tx_digests, tx_input_objects, tx_kinds, tx_recipients, tx_senders, + transactions, tx_affected_addresses, tx_affected_objects, tx_calls_fun, tx_calls_mod, + tx_calls_pkg, tx_changed_objects, tx_digests, tx_input_objects, tx_kinds, tx_recipients, + tx_senders, }; use crate::store::transaction_with_retry; use crate::types::EventIndex; @@ -1027,6 +1028,7 @@ impl PgIndexerStore { let len = indices.len(); let ( affected_addresses, + affected_objects, senders, recipients, input_objects, @@ -1048,9 +1050,11 @@ impl PgIndexerStore { Vec::new(), Vec::new(), Vec::new(), + Vec::new(), ), |( mut tx_affected_addresses, + mut tx_affected_objects, mut tx_senders, mut tx_recipients, mut tx_input_objects, @@ -1063,17 +1067,19 @@ impl PgIndexerStore { ), index| { tx_affected_addresses.extend(index.0); - tx_senders.extend(index.1); - tx_recipients.extend(index.2); - tx_input_objects.extend(index.3); - tx_changed_objects.extend(index.4); - tx_pkgs.extend(index.5); - tx_mods.extend(index.6); - tx_funs.extend(index.7); - tx_digests.extend(index.8); - tx_kinds.extend(index.9); + tx_affected_objects.extend(index.1); + tx_senders.extend(index.2); + tx_recipients.extend(index.3); + tx_input_objects.extend(index.4); + tx_changed_objects.extend(index.5); + tx_pkgs.extend(index.6); + tx_mods.extend(index.7); + tx_funs.extend(index.8); + tx_digests.extend(index.9); + tx_kinds.extend(index.10); ( tx_affected_addresses, + tx_affected_objects, tx_senders, tx_recipients, tx_input_objects, @@ -1095,6 +1101,12 @@ impl PgIndexerStore { .execute(conn) .await?; + diesel::insert_into(tx_affected_objects::table) + .values(&affected_objects) + .on_conflict_do_nothing() + .execute(conn) + .await?; + diesel::insert_into(tx_senders::table) .values(&senders) .on_conflict_do_nothing()