Skip to content

Commit

Permalink
[cmd]add block id for account show. (#1715)
Browse files Browse the repository at this point in the history
* [cmd]add block id for account show.

* Refactor RemoteStateReader.

* fix clippy.
  • Loading branch information
tiangong3624749 authored Nov 27, 2020
1 parent 31200fb commit 59d1390
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/faucet/src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Faucet {
receiver: AccountAddress,
public_key: Vec<u8>,
) -> Result<Result<(), anyhow::Error>> {
let chain_state_reader = RemoteStateReader::new(&self.client);
let chain_state_reader = RemoteStateReader::new(&self.client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader
.get_account_resource(self.faucet_account.address())?
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/account/accept_token_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl CommandAction for AcceptTokenCommand {
let client = ctx.state().client();
let node_info = client.node_info()?;
let sender = ctx.state().get_account_or_default(opt.sender)?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&sender.address)?;

Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/account/execute_builtin_script_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl CommandAction for ExecuteBuildInCommand {
let node_info = client.node_info()?;

let sender = ctx.state().get_account_or_default(opt.sender)?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&sender.address)?;

Expand Down
12 changes: 10 additions & 2 deletions cmd/starcoin/src/account/show_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::view::AccountWithStateView;
use crate::StarcoinOpt;
use anyhow::{format_err, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::ValidCryptoMaterialStringExt;
use starcoin_crypto::{HashValue, ValidCryptoMaterialStringExt};
use starcoin_rpc_client::RemoteStateReader;
use starcoin_state_api::AccountStateReader;
use starcoin_vm_types::account_address::AccountAddress;
Expand All @@ -18,6 +18,9 @@ use structopt::StructOpt;
pub struct ShowOpt {
#[structopt(name = "account_address")]
account_address: Option<AccountAddress>,

#[structopt(name = "block_id", short = "b")]
block_id: Option<HashValue>,
}

pub struct ShowCommand;
Expand Down Expand Up @@ -46,7 +49,12 @@ impl CommandAction for ShowCommand {
.account_get(account_address)?
.ok_or_else(|| format_err!("Account with address {} not exist.", account_address))?;

let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = if let Some(block_id) = opt.block_id {
let block = client.chain_get_block_by_hash(block_id)?;
RemoteStateReader::new_with_root(client, block.header.state_root)
} else {
RemoteStateReader::new(client)?
};
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let sequence_number = account_state_reader
.get_account_resource(account.address())?
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/account/transfer_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl CommandAction for TransferCommand {
};
let receiver = opt.receiver;

let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let receiver_exist_on_chain = account_state_reader
.get_account_resource(&receiver)?
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/dev/deploy_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl CommandAction for DeployCommand {
let module_address = *compiled_module.address();
let client = ctx.state().client();
let node_info = client.node_info()?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&module_address)?;

Expand Down
4 changes: 2 additions & 2 deletions cmd/starcoin/src/dev/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl CommandAction for ExecuteCommand {

let client = ctx.state().client();
let node_info = client.node_info()?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&sender)?;

Expand Down Expand Up @@ -210,7 +210,7 @@ impl CommandAction for ExecuteCommand {
let signed_txn = client.account_sign_txn(script_txn)?;
let txn_hash = signed_txn.crypto_hash();
let (vm_status, output) = if opt.local_mode {
let state_view = RemoteStateReader::new(client);
let state_view = RemoteStateReader::new(client)?;
playground::dry_run(
&state_view,
Transaction::UserTransaction(signed_txn.clone()),
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/dev/generate_multisig_txn_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl CommandAction for GenerateMultisigTxnCommand {

let client = ctx.state().client();
let node_info = client.node_info()?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&sender)?;

Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/dev/get_coin_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl CommandAction for GetCoinCommand {
})?;

let association_address = account_config::association_address();
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader
.get_account_resource(&association_address)?
Expand Down
6 changes: 3 additions & 3 deletions cmd/starcoin/src/dev/sign_txn_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn sign_txn_by_rpc_client(
let account = cli_state.get_account_or_default(account_address)?;
let client = cli_state.client();
let node_info = client.node_info()?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader
.get_account_resource(account.address())?
Expand All @@ -101,7 +101,7 @@ fn sign_txn_by_rpc_client(

pub fn get_dao_config(cli_state: &CliState) -> Result<DaoConfig> {
let client = cli_state.client();
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
Ok(account_state_reader
.get_on_chain_config::<DaoConfig>()?
Expand Down Expand Up @@ -137,7 +137,7 @@ mod tests {
cli_state: &CliState,
addr: AccountAddress,
) -> Result<(AccountResource, u128)> {
let chain_state_reader = RemoteStateReader::new(cli_state.client());
let chain_state_reader = RemoteStateReader::new(cli_state.client())?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader
.get_account_resource(&addr)?
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/state/get_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CommandAction for GetCommand {
let state = client
.state_get(AccessPath::new(account_addr, struct_tag.access_vector()))?
.ok_or_else(|| format_err!("Account with address {} state not exist.", account_addr))?;
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let viewer = MoveValueAnnotator::new(&chain_state_reader);
let annotated_resource = viewer.view_struct(struct_tag, state.as_slice())?;
Ok(annotated_resource)
Expand Down
16 changes: 8 additions & 8 deletions cmd/tx-factory/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn get_account_or_default(
}

let addr = default_account.clone().unwrap().address;
let state_reader = RemoteStateReader::new(&client);
let state_reader = RemoteStateReader::new(&client)?;
let account_state_reader = AccountStateReader::new(&state_reader);
let mut balance = account_state_reader.get_balance(&addr)?;
// balance resource has not been created
Expand Down Expand Up @@ -231,7 +231,7 @@ impl TxnMocker {
account_password: String,
unlock_duration: Duration,
) -> Result<Self> {
let state_reader = RemoteStateReader::new(&client);
let state_reader = RemoteStateReader::new(&client)?;
let account_state_reader = AccountStateReader::new(&state_reader);

let account_resource = account_state_reader.get_account_resource(&account_address)?;
Expand Down Expand Up @@ -275,7 +275,7 @@ impl TxnMocker {
self.next_sequence_number = match seq_number_in_pool {
Some(n) => n,
None => {
let state_reader = RemoteStateReader::new(&self.client);
let state_reader = RemoteStateReader::new(&self.client)?;
let account_state_reader = AccountStateReader::new(&state_reader);

let account_resource =
Expand All @@ -292,14 +292,14 @@ impl TxnMocker {
Ok(())
}

fn is_account_exist(&mut self, account: &AccountAddress) -> bool {
let state_reader = RemoteStateReader::new(&self.client);
fn is_account_exist(&mut self, account: &AccountAddress) -> Result<bool> {
let state_reader = RemoteStateReader::new(&self.client)?;
let account_state_reader = AccountStateReader::new(&state_reader);

let account_resource = account_state_reader
.get_account_resource(account)
.unwrap_or(None);
account_resource.is_some()
Ok(account_resource.is_some())
}

fn gen_and_submit_txn(&mut self, blocking: bool) -> Result<()> {
Expand Down Expand Up @@ -456,7 +456,7 @@ impl TxnMocker {
account_list.push(account);
i += 1;
} else {
if self.is_account_exist(&account.address) {
if self.is_account_exist(&account.address)? {
account_list.push(account);
i += 1;
info!("watch timeout.")
Expand Down Expand Up @@ -511,7 +511,7 @@ impl TxnMocker {
let result = match seq_number_in_pool {
Some(n) => Some(n),
None => {
let state_reader = RemoteStateReader::new(&self.client);
let state_reader = RemoteStateReader::new(&self.client)?;
let account_state_reader = AccountStateReader::new(&state_reader);

let account_resource = account_state_reader.get_account_resource(&address)?;
Expand Down
7 changes: 7 additions & 0 deletions rpc/api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ pub trait StateApi {

#[rpc(name = "state.get_state_root")]
fn get_state_root(&self) -> FutureResult<HashValue>;

#[rpc(name = "state.get_with_proof_by_root")]
fn get_with_proof_by_root(
&self,
access_path: AccessPath,
state_root: HashValue,
) -> FutureResult<StateWithProof>;
}
15 changes: 15 additions & 0 deletions rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,21 @@ impl RpcClient {
.map_err(map_err)
}

pub fn state_get_with_proof_by_root(
&self,
access_path: AccessPath,
state_root: HashValue,
) -> anyhow::Result<StateWithProof> {
self.call_rpc_blocking(|inner| async move {
inner
.state_client
.get_with_proof_by_root(access_path, state_root)
.compat()
.await
})
.map_err(map_err)
}

pub fn state_get_state_root(&self) -> anyhow::Result<HashValue> {
self.call_rpc_blocking(
|inner| async move { inner.state_client.get_state_root().compat().await },
Expand Down
20 changes: 14 additions & 6 deletions rpc/client/src/remote_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ use starcoin_types::state_set::ChainStateSet;
pub struct RemoteStateReader<'a> {
//TODO add cache.
client: &'a RpcClient,
state_root: HashValue,
}

impl<'a> RemoteStateReader<'a> {
pub fn new(client: &'a RpcClient) -> Self {
Self { client }
pub fn new(client: &'a RpcClient) -> Result<Self> {
let state_root = client.state_get_state_root()?;
Ok(Self::new_with_root(client, state_root))
}

pub fn new_with_root(client: &'a RpcClient, state_root: HashValue) -> Self {
Self { client, state_root }
}
}

Expand All @@ -32,9 +38,7 @@ impl<'a> ChainStateReader for RemoteStateReader<'a> {

fn state_root(&self) -> HashValue {
//TODO change trait api to return Result<HashValue>
self.client
.state_get_state_root()
.expect("unexpected error.")
self.state_root
}

fn dump(&self) -> Result<ChainStateSet> {
Expand All @@ -44,7 +48,11 @@ impl<'a> ChainStateReader for RemoteStateReader<'a> {

impl<'a> StateView for RemoteStateReader<'a> {
fn get(&self, access_path: &AccessPath) -> Result<Option<Vec<u8>>> {
self.client.state_get(access_path.clone())
Ok(self
.client
.state_get_with_proof_by_root(access_path.clone(), self.state_root())?
.get_state()
.clone())
}

fn multi_get(&self, _access_paths: &[AccessPath]) -> Result<Vec<Option<Vec<u8>>>> {
Expand Down
13 changes: 13 additions & 0 deletions rpc/server/src/module/state_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,17 @@ where
let fut = self.service.clone().state_root().map_err(map_err);
Box::new(fut.compat())
}

fn get_with_proof_by_root(
&self,
access_path: AccessPath,
state_root: HashValue,
) -> FutureResult<StateWithProof> {
let fut = self
.service
.clone()
.get_with_proof_by_root(access_path, state_root)
.map_err(map_err);
Box::new(fut.compat())
}
}
4 changes: 4 additions & 0 deletions state/api/src/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl StateWithProof {
pub fn new(state: Option<Vec<u8>>, proof: StateProof) -> Self {
Self { state, proof }
}

pub fn get_state(&self) -> &Option<Vec<u8>> {
&self.state
}
}

pub trait ChainStateReader: StateView {
Expand Down
4 changes: 2 additions & 2 deletions testsuite/tests/steps/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn steps() -> Steps<MyWorld> {
let result = transfer_txn(client, to, pre_mine_address, None);
assert!(result.is_ok());
std::thread::sleep(Duration::from_millis(3000));
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client).unwrap();
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let balances = account_state_reader.get_balance(to.address());
assert!(balances.is_ok());
Expand All @@ -50,7 +50,7 @@ fn transfer_txn(
from: AccountAddress,
amount: Option<u128>,
) -> Result<(), Error> {
let chain_state_reader = RemoteStateReader::new(client);
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader
.get_account_resource(&from)
Expand Down

0 comments on commit 59d1390

Please sign in to comment.