Skip to content

Commit

Permalink
[Storage] Unify AccountState and AccountView
Browse files Browse the repository at this point in the history
  • Loading branch information
sitalkedia authored and aptos-bot committed May 4, 2022
1 parent 9636dd0 commit ba399a7
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 70 deletions.
2 changes: 1 addition & 1 deletion api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use aptos_types::{
};

use anyhow::Result;
use aptos_types::account_state_blob::AccountStateBlob;
use aptos_types::{account_state_blob::AccountStateBlob, account_view::AccountView};
use move_core_types::{
identifier::Identifier, language_storage::StructTag, move_resource::MoveStructType,
value::MoveValue,
Expand Down
1 change: 1 addition & 0 deletions aptos-move/aptos-validator-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use aptos_types::{
account_address::AccountAddress,
account_config,
account_state::AccountState,
account_view::AccountView,
contract_event::EventWithProof,
event::EventKey,
on_chain_config::ValidatorSet,
Expand Down
1 change: 1 addition & 0 deletions aptos-move/transaction-replay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use aptos_types::{
account_address::AccountAddress,
account_config::aptos_root_address,
account_state::AccountState,
account_view::AccountView,
contract_event::{ContractEvent, EventWithProof},
event::EventKey,
transaction::{ChangeSet, Transaction, TransactionOutput, Version, WriteSetPayload},
Expand Down
2 changes: 1 addition & 1 deletion config/management/operational/src/rest_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aptos_management::error::Error;
use aptos_rest_client::Client;
use aptos_types::{
account_address::AccountAddress, account_config, account_config::AccountResource,
account_state::AccountState, account_state_blob::AccountStateBlob,
account_state::AccountState, account_state_blob::AccountStateBlob, account_view::AccountView,
transaction::SignedTransaction, validator_config::ValidatorConfig,
validator_info::ValidatorInfo,
};
Expand Down
5 changes: 3 additions & 2 deletions config/seed-peer-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use aptos_logger::prelude::*;
use aptos_rest_client::Client;
use aptos_types::{
account_config::aptos_root_address, account_state::AccountState,
account_state_blob::AccountStateBlob, network_address::NetworkAddress,
on_chain_config::ValidatorSet, validator_info::ValidatorInfo, PeerId,
account_state_blob::AccountStateBlob, account_view::AccountView,
network_address::NetworkAddress, on_chain_config::ValidatorSet, validator_info::ValidatorInfo,
PeerId,
};

/// Retrieve the Fullnode seed peers from JSON-RPC
Expand Down
4 changes: 2 additions & 2 deletions storage/state-view/src/account_with_state_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<'a> AccountView for AccountWithStateCache<'a> {
.and_then(|x| x.maybe_bytes.clone()))
}

fn get_account_address(&self) -> &AccountAddress {
self.account_address
fn get_account_address(&self) -> anyhow::Result<Option<AccountAddress>> {
Ok(Some(*self.account_address))
}
}

Expand Down
4 changes: 2 additions & 2 deletions storage/state-view/src/account_with_state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl<'a> AccountView for AccountWithStateView<'a> {
self.state_view.get_state_value(state_key)
}

fn get_account_address(&self) -> &AccountAddress {
self.account_address
fn get_account_address(&self) -> anyhow::Result<Option<AccountAddress>> {
Ok(Some(*self.account_address))
}
}

Expand Down
1 change: 1 addition & 0 deletions testsuite/smoke-test/src/operational_tooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use aptos_types::{
account_address::{from_identity_public_key, AccountAddress},
account_state::AccountState,
account_state_blob::AccountStateBlob,
account_view::AccountView,
block_info::BlockInfo,
ledger_info::LedgerInfo,
network_address::NetworkAddress,
Expand Down
71 changes: 23 additions & 48 deletions types/src/account_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use crate::{
account_address::AccountAddress,
account_config::{AccountResource, BalanceResource},
account_state_blob::AccountStateBlob,
on_chain_config::{access_path_for_config, OnChainConfig, ValidatorSet},
account_view::AccountView,
state_store::{state_key::StateKey, state_value::StateValue},
validator_config::{ValidatorConfig, ValidatorOperatorConfigResource},
};
use anyhow::{anyhow, Error, Result};
use move_core_types::{language_storage::StructTag, move_resource::MoveResource};
Expand All @@ -23,48 +22,10 @@ use std::{
pub struct AccountState(BTreeMap<Vec<u8>, Vec<u8>>);

impl AccountState {
// By design and do not remove
pub fn get_account_address(&self) -> Result<Option<AccountAddress>> {
self.get_account_resource()
.map(|opt_ar| opt_ar.map(|ar| ar.address()))
}

// Return the `AccountResource` for this blob. If the blob doesn't have an `AccountResource`
// then it must have a `AptosAccountResource` in which case we convert that to an
// `AccountResource`.
pub fn get_account_resource(&self) -> Result<Option<AccountResource>> {
match self.get_resource::<AccountResource>()? {
x @ Some(_) => Ok(x),
None => Ok(None),
}
}

pub fn get_validator_config_resource(&self) -> Result<Option<ValidatorConfig>> {
self.get_resource::<ValidatorConfig>()
}

pub fn get_validator_operator_config_resource(
&self,
) -> Result<Option<ValidatorOperatorConfigResource>> {
self.get_resource::<ValidatorOperatorConfigResource>()
}

pub fn get_validator_set(&self) -> Result<Option<ValidatorSet>> {
self.get_config::<ValidatorSet>()
}

pub fn get(&self, key: &[u8]) -> Option<&Vec<u8>> {
self.0.get(key)
}

pub fn get_resource_impl<T: DeserializeOwned>(&self, key: &[u8]) -> Result<Option<T>> {
self.0
.get(key)
.map(|bytes| bcs::from_bytes(bytes))
.transpose()
.map_err(Into::into)
}

pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>> {
self.0.insert(key, value)
}
Expand All @@ -77,14 +38,6 @@ impl AccountState {
self.0.iter()
}

pub fn get_config<T: OnChainConfig>(&self) -> Result<Option<T>> {
self.get_resource_impl(&access_path_for_config(T::CONFIG_ID).path)
}

pub fn get_resource<T: MoveResource>(&self) -> Result<Option<T>> {
self.get_resource_impl(&T::struct_tag().access_vector())
}

/// Return an iterator over the module values stored under this account
pub fn get_modules(&self) -> impl Iterator<Item = &Vec<u8>> {
self.0.iter().filter_map(
Expand Down Expand Up @@ -144,6 +97,28 @@ impl fmt::Debug for AccountState {
}
}

impl AccountView for AccountState {
fn get_state_value(&self, _: &StateKey) -> Result<Option<Vec<u8>>> {
unimplemented!()
}

fn get_account_address(&self) -> anyhow::Result<Option<AccountAddress>> {
match self.get_resource::<AccountResource>()? {
x @ Some(_) => Ok(x),
None => Ok(None),
}
.map(|opt_ar| opt_ar.map(|ar| ar.address()))
}

fn get_resource_impl<T: DeserializeOwned>(&self, path: Vec<u8>) -> Result<Option<T>> {
self.0
.get(&path)
.map(|bytes| bcs::from_bytes(bytes))
.transpose()
.map_err(Into::into)
}
}

impl TryFrom<&StateValue> for AccountState {
type Error = Error;

Expand Down
1 change: 1 addition & 0 deletions types/src/account_state_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{
account_config::{AccountResource, BalanceResource},
account_state::AccountState,
account_view::AccountView,
state_store::state_value::StateValue,
};
use anyhow::{anyhow, format_err, Error, Result};
Expand Down
37 changes: 23 additions & 14 deletions types/src/account_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ use crate::{
access_path_for_config, ConfigurationResource, OnChainConfig, ValidatorSet, Version,
},
state_store::state_key::StateKey,
validator_config::ValidatorConfig,
validator_config::{ValidatorConfig, ValidatorOperatorConfigResource},
};
use anyhow::anyhow;
use move_core_types::{account_address::AccountAddress, move_resource::MoveResource};
use serde::de::DeserializeOwned;

pub trait AccountView {
fn get_state_value(&self, state_key: &StateKey) -> anyhow::Result<Option<Vec<u8>>>;

fn get_account_address(&self) -> &AccountAddress;
fn get_account_address(&self) -> anyhow::Result<Option<AccountAddress>>;

fn get_validator_set(&self) -> anyhow::Result<Option<ValidatorSet>> {
self.get_on_chain_config::<ValidatorSet>()
Expand All @@ -27,25 +28,29 @@ pub trait AccountView {
}

fn get_move_resource<T: MoveResource>(&self) -> anyhow::Result<Option<T>> {
let state_key = self.get_state_key_for_path(T::struct_tag().access_vector());
self.get_resource_impl(&state_key)
self.get_resource_impl(T::struct_tag().access_vector())
}

fn get_validator_config_resource(&self) -> anyhow::Result<Option<ValidatorConfig>> {
self.get_resource::<ValidatorConfig>()
}

fn get_validator_operator_config_resource(
&self,
) -> anyhow::Result<Option<ValidatorOperatorConfigResource>> {
self.get_resource::<ValidatorOperatorConfigResource>()
}

fn get_on_chain_config<T: OnChainConfig>(&self) -> anyhow::Result<Option<T>> {
let state_key = self.get_state_key_for_path(access_path_for_config(T::CONFIG_ID).path);
self.get_resource_impl(&state_key)
self.get_resource_impl(access_path_for_config(T::CONFIG_ID).path)
}

fn get_version(&self) -> anyhow::Result<Option<Version>> {
self.get_on_chain_config::<Version>()
}

fn get_resource<T: MoveResource>(&self) -> anyhow::Result<Option<T>> {
self.get_resource_impl(&self.get_state_key_for_path(T::struct_tag().access_vector()))
self.get_resource_impl(T::struct_tag().access_vector())
}

fn get_chain_id_resource(&self) -> anyhow::Result<Option<ChainIdResource>> {
Expand All @@ -60,19 +65,23 @@ pub trait AccountView {
self.get_resource::<BalanceResource>()
}

fn get_state_key_for_path(&self, path: Vec<u8>) -> StateKey {
StateKey::AccessPath(AccessPath::new(*self.get_account_address(), path))
fn get_state_key_for_path(&self, path: Vec<u8>) -> anyhow::Result<StateKey> {
let account_address = self
.get_account_address()?
.ok_or_else(|| anyhow!("Could not fetch account address"))?;
Ok(StateKey::AccessPath(AccessPath::new(account_address, path)))
}

fn get_account_resource(&self) -> anyhow::Result<Option<AccountResource>> {
self.get_resource::<AccountResource>()
}

fn get_resource_impl<T: DeserializeOwned>(
&self,
state_key: &StateKey,
) -> anyhow::Result<Option<T>> {
self.get_state_value(state_key)?
fn get_config<T: OnChainConfig>(&self) -> anyhow::Result<Option<T>> {
self.get_resource_impl(access_path_for_config(T::CONFIG_ID).path)
}

fn get_resource_impl<T: DeserializeOwned>(&self, path: Vec<u8>) -> anyhow::Result<Option<T>> {
self.get_state_value(&self.get_state_key_for_path(path)?)?
.map(|bytes| bcs::from_bytes(&bytes))
.transpose()
.map_err(Into::into)
Expand Down

0 comments on commit ba399a7

Please sign in to comment.