Skip to content

Commit

Permalink
[Client Refactoring 4] Rename download_own_object_ids (MystenLabs#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored Feb 4, 2022
1 parent 4992273 commit ca863ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
2 changes: 1 addition & 1 deletion fastpay/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 11 additions & 11 deletions fastpay_core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthorityName, anyhow::Error>;
async fn sync_client_state(&mut self) -> Result<AuthorityName, anyhow::Error>;

/// Call move functions in the module in the given package, with args supplied
async fn move_call(
Expand Down Expand Up @@ -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<ObjectRef>), 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
Expand Down Expand Up @@ -1458,9 +1458,7 @@ where
Ok(())
}

async fn sync_client_state_with_random_authority(
&mut self,
) -> Result<AuthorityName, anyhow::Error> {
async fn sync_client_state(&mut self) -> Result<AuthorityName, anyhow::Error> {
if !self.store.pending_orders.is_empty()? {
// Finish executing the previous orders
self.try_complete_pending_orders().await?;
Expand All @@ -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
Expand Down
38 changes: 8 additions & 30 deletions fastpay_core/src/unit_tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca863ad

Please sign in to comment.