Skip to content

Commit

Permalink
[storage] Refactor MoveStorage to improve readability
Browse files Browse the repository at this point in the history
Closes: #10064
  • Loading branch information
khiemngo authored and bors-libra committed Dec 20, 2021
1 parent dd07efa commit 9ef01ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 56 deletions.
63 changes: 17 additions & 46 deletions storage/storage-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ use diem_types::{
TransactionOutputListWithProof, TransactionToCommit, TransactionWithProof, Version,
},
};
use itertools::Itertools;
use move_core_types::resolver::{ModuleResolver, ResourceResolver};
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
convert::TryFrom,
sync::Arc,
};
use std::{convert::TryFrom, sync::Arc};
use thiserror::Error;

#[cfg(any(feature = "testing", feature = "fuzzing"))]
Expand Down Expand Up @@ -519,50 +514,26 @@ pub trait DbReader: Send + Sync {
}

impl MoveStorage for &dyn DbReader {
fn batch_fetch_resources(&self, access_paths: Vec<AccessPath>) -> Result<Vec<Vec<u8>>> {
self.batch_fetch_resources_by_version(access_paths, self.fetch_synced_version()?)
fn fetch_resource(&self, access_path: AccessPath) -> Result<Vec<u8>> {
self.fetch_resource_by_version(access_path, self.fetch_synced_version()?)
}

fn batch_fetch_resources_by_version(
fn fetch_resource_by_version(
&self,
access_paths: Vec<AccessPath>,
access_path: AccessPath,
version: Version,
) -> Result<Vec<Vec<u8>>> {
let addresses: Vec<AccountAddress> = access_paths
.iter()
.collect::<HashSet<_>>()
.iter()
.map(|path| path.address)
.collect();

let results = addresses
.iter()
.map(|addr| self.get_account_state_with_proof_by_version(*addr, version))
.collect::<Result<Vec<_>>>()?;

// Account address --> AccountState
let account_states = addresses
.iter()
.zip_eq(results)
.map(|(addr, (blob, _proof))| {
let account_state = AccountState::try_from(&blob.ok_or_else(|| {
format_err!("missing blob in account state/account does not exist")
})?)?;
Ok((addr, account_state))
})
.collect::<Result<HashMap<_, AccountState>>>()?;

access_paths
.iter()
.map(|path| {
Ok(account_states
.get(&path.address)
.ok_or_else(|| format_err!("missing account state for queried access path"))?
.get(&path.path)
.ok_or_else(|| format_err!("no value found in account state"))?
.clone())
})
.collect()
) -> Result<Vec<u8>> {
let (account_state_blob, _) =
self.get_account_state_with_proof_by_version(access_path.address, version)?;
let account_state =
AccountState::try_from(&account_state_blob.ok_or_else(|| {
format_err!("missing blob in account state/account does not exist")
})?)?;

Ok(account_state
.get(&access_path.path)
.ok_or_else(|| format_err!("no value found in account state"))?
.clone())
}

fn fetch_config_by_version(&self, config_id: ConfigID, version: Version) -> Result<Vec<u8>> {
Expand Down
18 changes: 8 additions & 10 deletions types/src/move_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ use anyhow::Result;

// TODO combine with ConfigStorage
pub trait MoveStorage {
/// Returns a vector of Move resources as serialized byte array
/// Order of resources returned matches the order of `access_path`
fn batch_fetch_resources(&self, access_paths: Vec<AccessPath>) -> Result<Vec<Vec<u8>>>;
/// Returns a Move resources as a serialized byte array.
fn fetch_resource(&self, access_path: AccessPath) -> Result<Vec<u8>>;

/// Returns a vector of Move resources as serialized byte array from a
/// specified version of the database
/// Order of resources returned matches the order of `access_path`
fn batch_fetch_resources_by_version(
/// Returns a Move resources as serialized byte array from a
/// specified version of the database.
fn fetch_resource_by_version(
&self,
access_paths: Vec<AccessPath>,
access_path: AccessPath,
version: Version,
) -> Result<Vec<Vec<u8>>>;
) -> Result<Vec<u8>>;

/// Returns an on-chain resource as serialized byte array from a
/// Returns an on-chain resource as a serialized byte array from a
/// specified version of the database.
fn fetch_config_by_version(&self, config_id: ConfigID, version: Version) -> Result<Vec<u8>>;

Expand Down

0 comments on commit 9ef01ed

Please sign in to comment.