From a65b129b96d661beaab42d92dacddfc95917f12c Mon Sep 17 00:00:00 2001 From: George Danezis Date: Thu, 13 Jan 2022 16:29:45 +0000 Subject: [PATCH] Added a parent iterator by object_id or (objectid,version) (#166) Co-authored-by: George Danezis --- fastpay_core/src/authority.rs | 12 +++++++++ fastpay_core/src/authority/authority_store.rs | 26 +++++++++++++++++++ .../src/unit_tests/authority_tests.rs | 11 +++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/fastpay_core/src/authority.rs b/fastpay_core/src/authority.rs index 5d1f77f02453b..26b505d0fe1b3 100644 --- a/fastpay_core/src/authority.rs +++ b/fastpay_core/src/authority.rs @@ -497,4 +497,16 @@ impl AuthorityState { ) -> Result>, FastPayError> { self._database.get_objects(_objects) } + + /// Returns all parents (object_ref and transaction digests) that match an object_id, at + /// any object version, or optionally at a specific version. + pub async fn get_parent_iterator( + &self, + object_id: ObjectID, + seq: Option, + ) -> Result, FastPayError> { + { + self._database.get_parent_iterator(object_id, seq) + } + } } diff --git a/fastpay_core/src/authority/authority_store.rs b/fastpay_core/src/authority/authority_store.rs index b3fee4e847792..0829cd08a1ce8 100644 --- a/fastpay_core/src/authority/authority_store.rs +++ b/fastpay_core/src/authority/authority_store.rs @@ -142,6 +142,32 @@ impl AuthorityStore { .map_err(|_| FastPayError::StorageError) } + /// Returns all parents (object_ref and transaction digests) that match an object_id, at + /// any object version, or optionally at a specific version. + pub fn get_parent_iterator( + &self, + object_id: ObjectID, + seq: Option, + ) -> Result, FastPayError> { + let seq_inner = seq.unwrap_or_else(|| SequenceNumber::from(0)); + let obj_dig_inner = ObjectDigest::new([0; 32]); + + Ok(self + .parent_sync + .iter() + // The object id [0; 16] is the smallest possible + .skip_to(&(object_id, seq_inner, obj_dig_inner)) + .map_err(|_| FastPayError::StorageError)? + .take_while(|((id, iseq, _digest), _txd)| { + let mut flag = id == &object_id; + if seq.is_some() { + flag &= seq_inner == *iseq; + } + flag + }) + .collect()) + } + // Methods to mutate the store /// Insert an object diff --git a/fastpay_core/src/unit_tests/authority_tests.rs b/fastpay_core/src/unit_tests/authority_tests.rs index 19ee5a3a88dd1..84cc0ee821593 100644 --- a/fastpay_core/src/unit_tests/authority_tests.rs +++ b/fastpay_core/src/unit_tests/authority_tests.rs @@ -767,7 +767,7 @@ async fn test_handle_confirmation_order_ok() { .unwrap(); authority_state.read_certificate(&refx).await.unwrap() }, - Some(certified_transfer_order) + Some(certified_transfer_order.clone()) ); // Check locks are set and archived correctly @@ -780,6 +780,15 @@ async fn test_handle_confirmation_order_ok() { .await .expect("Exists") .is_none()); + + // Check that all the parents are returned. + assert!( + authority_state.get_parent_iterator(object_id, None).await + == Ok(vec![( + (object_id, 1.into(), new_account.digest()), + certified_transfer_order.order.digest() + )]) + ); } #[tokio::test]