From ca863ad4f1cbeaec881be14082e84fae54f144bb Mon Sep 17 00:00:00 2001 From: Xun Li Date: Fri, 4 Feb 2022 14:30:52 -0800 Subject: [PATCH] [Client Refactoring 4] Rename download_own_object_ids (#362) --- fastpay/src/client.rs | 2 +- fastpay_core/src/client.rs | 22 ++++++------ fastpay_core/src/unit_tests/client_tests.rs | 38 +++++---------------- 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/fastpay/src/client.rs b/fastpay/src/client.rs index efc7f8d2101c4..9fb93e2f4d8f5 100644 --- a/fastpay/src/client.rs +++ b/fastpay/src/client.rs @@ -84,7 +84,7 @@ async fn make_client_state_and_try_sync( ); // Force a sync - let _ = c.sync_client_state_with_random_authority(); + let _ = c.sync_client_state(); c } diff --git a/fastpay_core/src/client.rs b/fastpay_core/src/client.rs index c8d3c75b2c6a2..adcc76adda31c 100644 --- a/fastpay_core/src/client.rs +++ b/fastpay_core/src/client.rs @@ -82,9 +82,7 @@ pub trait Client { /// Synchronise client state with a random authorities, updates all object_ids and certificates, request only goes out to one authority. /// this method doesn't guarantee data correctness, client will have to handle potential byzantine authority - async fn sync_client_state_with_random_authority( - &mut self, - ) -> Result; + async fn sync_client_state(&mut self) -> Result; /// Call move functions in the module in the given package, with args supplied async fn move_call( @@ -1077,12 +1075,14 @@ where Ok(new_sent_certificate) } - async fn download_own_object_ids( + // TODO: This is incomplete at the moment. + // A complete algorithm is being introduced in + // https://github.com/MystenLabs/fastnft/pull/336. + async fn download_own_object_ids_from_random_authority( &self, + address: FastPayAddress, ) -> Result<(AuthorityName, Vec), FastPayError> { - let request = AccountInfoRequest { - account: self.address, - }; + let request = AccountInfoRequest { account: address }; // Sequentially try each authority in random order. let mut authorities: Vec<&AuthorityName> = self.authority_clients.keys().collect(); // TODO: implement sampling according to stake distribution and using secure RNG. https://github.com/MystenLabs/fastnft/issues/128 @@ -1458,9 +1458,7 @@ where Ok(()) } - async fn sync_client_state_with_random_authority( - &mut self, - ) -> Result { + async fn sync_client_state(&mut self) -> Result { if !self.store.pending_orders.is_empty()? { // Finish executing the previous orders self.try_complete_pending_orders().await?; @@ -1469,7 +1467,9 @@ where self.store.object_sequence_numbers.clear()?; self.store.object_refs.clear()?; - let (authority_name, object_refs) = self.download_own_object_ids().await?; + let (authority_name, object_refs) = self + .download_own_object_ids_from_random_authority(self.address) + .await?; for object_ref in object_refs { let (object_id, sequence_number, _) = object_ref; self.store diff --git a/fastpay_core/src/unit_tests/client_tests.rs b/fastpay_core/src/unit_tests/client_tests.rs index 88276a9a8827e..b0ebe5f2a10a7 100644 --- a/fastpay_core/src/unit_tests/client_tests.rs +++ b/fastpay_core/src/unit_tests/client_tests.rs @@ -641,8 +641,7 @@ fn test_client_state_sync() { assert!(rt.block_on(sender.get_owned_objects()).is_empty()); // Sync client state - rt.block_on(sender.sync_client_state_with_random_authority()) - .unwrap(); + rt.block_on(sender.sync_client_state()).unwrap(); // Confirm data are the same after sync assert!(!rt.block_on(sender.get_owned_objects()).is_empty()); @@ -698,10 +697,7 @@ async fn test_client_state_sync_with_transferred_object() { assert!(&client2.store.certificates.is_empty().unwrap()); // Sync client state - client2 - .sync_client_state_with_random_authority() - .await - .unwrap(); + client2.sync_client_state().await.unwrap(); // Confirm client 2 received the new object id and cert assert_eq!(1, client2.get_owned_objects().await.len()); @@ -781,10 +777,7 @@ async fn test_client_certificate_state() { client1.next_sequence_number(&gas_object_id_1) ); - client2 - .sync_client_state_with_random_authority() - .await - .unwrap(); + client2.sync_client_state().await.unwrap(); // Client 2 should retrieve 2 certificates for the 2 transactions after sync assert_eq!(2, client2.store.certificates.iter().count()); @@ -870,10 +863,7 @@ async fn test_client_certificate_state() { .len() ); - client1 - .sync_client_state_with_random_authority() - .await - .unwrap(); + client1.sync_client_state().await.unwrap(); assert_eq!(3, client1.store.certificates.iter().count()); assert!(client1 @@ -1934,10 +1924,7 @@ async fn test_move_calls_certs() { ); // Sync client 2 - client2 - .sync_client_state_with_random_authority() - .await - .unwrap(); + client2.sync_client_state().await.unwrap(); // Client 2 should have 2 certificate, one new object, with two associated certificate. assert_eq!(2, client2.store.certificates.iter().count()); @@ -2484,10 +2471,7 @@ async fn test_object_store() { // Run a few syncs to retrieve objects ids for _ in 0..4 { - let _ = client1 - .sync_client_state_with_random_authority() - .await - .unwrap(); + let _ = client1.sync_client_state().await.unwrap(); } // Try to download objects which are not already in storage client1 @@ -2567,14 +2551,8 @@ async fn test_object_store_transfer() { // Run a few syncs to populate object ids for _ in 0..4 { - let _ = client1 - .sync_client_state_with_random_authority() - .await - .unwrap(); - let _ = client2 - .sync_client_state_with_random_authority() - .await - .unwrap(); + let _ = client1.sync_client_state().await.unwrap(); + let _ = client2.sync_client_state().await.unwrap(); } // Try to download objects which are not already in storage