From aed54b6b6a68a7b5ac52057f65c3e2db20f54b20 Mon Sep 17 00:00:00 2001 From: Levente Pap Date: Thu, 30 Jan 2025 10:19:22 +0100 Subject: [PATCH] fix: re-add rpc-indexes to support `InputObject` and `ChangedObject` filters (partly revert #3142) (#5074) (#5088) Co-authored-by: muXxer --- crates/iota-core/src/authority.rs | 6 ++ crates/iota-storage/src/indexes.rs | 73 ++++++++++++++++++++ crates/iota-tool/src/db_tool/index_search.rs | 29 ++++++++ 3 files changed, 108 insertions(+) diff --git a/crates/iota-core/src/authority.rs b/crates/iota-core/src/authority.rs index 3c171b3b59d..91a66d1f8a4 100644 --- a/crates/iota-core/src/authority.rs +++ b/crates/iota-core/src/authority.rs @@ -2097,6 +2097,12 @@ impl AuthorityState { indexes .index_tx( cert.data().intent_message().value.sender(), + cert.data() + .intent_message() + .value + .input_objects()? + .iter() + .map(|o| o.object_id()), effects .all_changed_objects() .into_iter() diff --git a/crates/iota-storage/src/indexes.rs b/crates/iota-storage/src/indexes.rs index b4dc2456006..c49bdce8aa3 100644 --- a/crates/iota-storage/src/indexes.rs +++ b/crates/iota-storage/src/indexes.rs @@ -160,6 +160,15 @@ pub struct IndexStoreTables { #[default_options_override_fn = "transactions_to_addr_table_default_config"] transactions_to_addr: DBMap<(IotaAddress, TxSequenceNumber), TransactionDigest>, + /// Index from object id to transactions that used that object id as input. + #[deprecated] + transactions_by_input_object_id: DBMap<(ObjectID, TxSequenceNumber), TransactionDigest>, + + /// Index from object id to transactions that modified/created that object + /// id. + #[deprecated] + transactions_by_mutated_object_id: DBMap<(ObjectID, TxSequenceNumber), TransactionDigest>, + /// Index from package id, module and function identifier to transactions /// that used that moce function call as input. #[default_options_override_fn = "transactions_by_move_function_table_default_config"] @@ -458,6 +467,7 @@ impl IndexStore { pub async fn index_tx( &self, sender: IotaAddress, + active_inputs: impl Iterator, mutated_objects: impl Iterator + Clone, move_functions: impl Iterator + Clone, events: &TransactionEvents, @@ -484,6 +494,21 @@ impl IndexStore { std::iter::once(((sender, sequence), *digest)), )?; + #[allow(deprecated)] + if !self.remove_deprecated_tables { + batch.insert_batch( + &self.tables.transactions_by_input_object_id, + active_inputs.map(|id| ((id, sequence), *digest)), + )?; + + batch.insert_batch( + &self.tables.transactions_by_mutated_object_id, + mutated_objects + .clone() + .map(|(obj_ref, _)| ((obj_ref.0, sequence), *digest)), + )?; + } + batch.insert_batch( &self.tables.transactions_by_move_function, move_functions.map(|(obj_id, module, function)| { @@ -657,6 +682,12 @@ impl IndexStore { }) => Ok(self.get_transactions_by_move_function( package, module, function, cursor, limit, reverse, )?), + Some(TransactionFilter::InputObject(object_id)) => { + Ok(self.get_transactions_by_input_object(object_id, cursor, limit, reverse)?) + } + Some(TransactionFilter::ChangedObject(object_id)) => { + Ok(self.get_transactions_by_mutated_object(object_id, cursor, limit, reverse)?) + } Some(TransactionFilter::FromAddress(address)) => { Ok(self.get_transactions_from_addr(address, cursor, limit, reverse)?) } @@ -734,6 +765,46 @@ impl IndexStore { }) } + pub fn get_transactions_by_input_object( + &self, + input_object: ObjectID, + cursor: Option, + limit: Option, + reverse: bool, + ) -> IotaResult> { + if self.remove_deprecated_tables { + return Ok(vec![]); + } + #[allow(deprecated)] + Self::get_transactions_from_index( + &self.tables.transactions_by_input_object_id, + input_object, + cursor, + limit, + reverse, + ) + } + + pub fn get_transactions_by_mutated_object( + &self, + mutated_object: ObjectID, + cursor: Option, + limit: Option, + reverse: bool, + ) -> IotaResult> { + if self.remove_deprecated_tables { + return Ok(vec![]); + } + #[allow(deprecated)] + Self::get_transactions_from_index( + &self.tables.transactions_by_mutated_object_id, + mutated_object, + cursor, + limit, + reverse, + ) + } + pub fn get_transactions_from_addr( &self, addr: IotaAddress, @@ -1573,6 +1644,7 @@ mod tests { address, vec![].into_iter(), vec![].into_iter(), + vec![].into_iter(), &TransactionEvents { data: vec![] }, object_index_changes, &TransactionDigest::random(), @@ -1616,6 +1688,7 @@ mod tests { address, vec![].into_iter(), vec![].into_iter(), + vec![].into_iter(), &TransactionEvents { data: vec![] }, object_index_changes, &TransactionDigest::random(), diff --git a/crates/iota-tool/src/db_tool/index_search.rs b/crates/iota-tool/src/db_tool/index_search.rs index 694b610467d..70ee8693827 100644 --- a/crates/iota-tool/src/db_tool/index_search.rs +++ b/crates/iota-tool/src/db_tool/index_search.rs @@ -67,6 +67,22 @@ pub fn search_index( termination ) } + "transactions_by_input_object_id" => { + get_db_entries!( + db_read_only_handle.transactions_by_input_object_id, + from_id_seq, + start, + termination + ) + } + "transactions_by_mutated_object_id" => { + get_db_entries!( + db_read_only_handle.transactions_by_mutated_object_id, + from_id_seq, + start, + termination + ) + } "transactions_by_move_function" => { get_db_entries!( db_read_only_handle.transactions_by_move_function, @@ -241,6 +257,19 @@ fn from_addr_seq(s: &str) -> Result<(IotaAddress, TxSequenceNumber), anyhow::Err Ok((address, sequence_number)) } +fn from_id_seq(s: &str) -> Result<(ObjectID, TxSequenceNumber), anyhow::Error> { + // Remove whitespaces + let s = s.trim(); + let tokens = s.split(',').collect::>(); + if tokens.len() != 2 { + return Err(anyhow!("Invalid object id, sequence number pair")); + } + let oid = ObjectID::from_str(tokens[0].trim())?; + let sequence_number = TxSequenceNumber::from_str(tokens[1].trim())?; + + Ok((oid, sequence_number)) +} + fn from_id_module_function_txseq( s: &str, ) -> Result<(ObjectID, String, String, TxSequenceNumber), anyhow::Error> {