Skip to content

Commit

Permalink
fix: do not recreate path in get_managed_address
Browse files Browse the repository at this point in the history
Clean up get_or_create_state_mut as a byproduct
  • Loading branch information
huitseeker committed Feb 20, 2022
1 parent 68612db commit 2db39b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
33 changes: 18 additions & 15 deletions sui_core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,27 @@ impl<A> ClientAddressManager<A> {
committee: Committee,
authority_clients: BTreeMap<AuthorityName, A>,
) -> Result<&mut ClientState<A>, SuiError> {
if let std::collections::btree_map::Entry::Vacant(e) = self.address_states.entry(address) {
#[allow(clippy::map_entry)]
// the fallible store creation complicates the use of the entry API
if !self.address_states.contains_key(&address) {
// Load the records if available
let single_store = if self.store.is_managed_address(address)? {
// Unwrap is okay since we checked cond
self.store.get_managed_address(address)
} else {
self.store.manage_new_address(address)
}?;
e.insert(ClientState::new_for_manager(
let single_store = match self.store.get_managed_address(address)? {
Some(store) => store,
None => self.store.manage_new_address(address)?,
};
self.address_states.insert(
address,
secret,
committee,
authority_clients,
single_store,
));
ClientState::new_for_manager(
address,
secret,
committee,
authority_clients,
single_store,
),
);
}

return Ok(self.address_states.get_mut(&address).unwrap());
// unwrap-safe as we just populated the entry
Ok(self.address_states.get_mut(&address).unwrap())
}

/// Get all the states
Expand Down
11 changes: 5 additions & 6 deletions sui_core/src/client/client_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ impl ClientAddressManagerStore {
pub fn get_managed_address(
&self,
address: SuiAddress,
) -> Result<client_store::ClientSingleAddressStore, typed_store::rocks::TypedStoreError> {
// Create an a path for this address
let path = self.make_db_path_for_address(address);

self.managed_address_paths.get(&address)?;
Ok(ClientSingleAddressStore::new(path))
) -> Result<Option<client_store::ClientSingleAddressStore>, typed_store::rocks::TypedStoreError>
{
self.managed_address_paths
.get(&address)
.map(|opt_path| opt_path.map(ClientSingleAddressStore::new))
}

/// Check if an address is managed
Expand Down

0 comments on commit 2db39b6

Please sign in to comment.