Skip to content

Commit

Permalink
fix starcoin-network-rpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Oct 17, 2024
1 parent a0cd743 commit abe7cd8
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 41 deletions.
8 changes: 1 addition & 7 deletions cmd/faucet/src/faucet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{format_err, Result};
use anyhow::Result;
use starcoin_account_api::AccountInfo;
use starcoin_crypto::HashValue;
use starcoin_logger::prelude::*;
Expand Down Expand Up @@ -63,12 +63,6 @@ impl Faucet {
let chain_state_reader = self.client.state_reader(StateRootOption::Latest)?;
chain_state_reader
.get_account_resource(*self.faucet_account.address())?
.ok_or_else(|| {
format_err!(
"Can not find account on chain by address:{}",
self.faucet_account.address()
)
})?
.sequence_number()
}
};
Expand Down
14 changes: 7 additions & 7 deletions cmd/tx-factory/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ fn get_account_or_default(

let addr = default_account.clone().unwrap().address;
let state_reader = client.state_reader(StateRootOption::Latest)?;
let mut balance = state_reader.get_balance(addr)?;
let mut balance = state_reader.get_balance(addr);
// balance resource has not been created
while balance.is_none() {
while balance.is_err() {
std::thread::sleep(Duration::from_millis(1000));
balance = state_reader.get_balance(addr)?;
balance = state_reader.get_balance(addr);
info!("account balance is null.");
}
default_account.unwrap()
Expand Down Expand Up @@ -228,8 +228,8 @@ impl TxnMocker {
) -> Result<Self> {
let state_reader = client.state_reader(StateRootOption::Latest)?;

let account_resource = state_reader.get_account_resource(account_address)?;
if account_resource.is_none() {
let account_resource = state_reader.get_account_resource(account_address);
if account_resource.is_err() {
bail!("account {} not exists, please faucet it", account_address);
}
let account_resource = account_resource.unwrap();
Expand Down Expand Up @@ -277,8 +277,8 @@ impl TxnMocker {
None => {
let state_reader = self.client.state_reader(StateRootOption::Latest)?;

let account_resource = state_reader.get_account_resource(self.account_address)?;
if account_resource.is_none() {
let account_resource = state_reader.get_account_resource(self.account_address);
if account_resource.is_err() {
bail!(
"account {} not exists, please faucet it",
&self.account_address
Expand Down
4 changes: 2 additions & 2 deletions network-rpc/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub trait NetworkRpc: Sized + Send + Sync + 'static {
&self,
peer_id: PeerId,
req: GetAccountState,
) -> BoxFuture<Result<Option<AccountState>>>;
) -> BoxFuture<Result<AccountState>>;

fn get_block_ids(&self, peer_id: PeerId, req: GetBlockIds)
-> BoxFuture<Result<Vec<HashValue>>>;
Expand All @@ -292,7 +292,7 @@ pub trait NetworkRpc: Sized + Send + Sync + 'static {
&self,
peer_id: PeerId,
request: GetTableInfo,
) -> BoxFuture<Result<Option<TableInfo>>>;
) -> BoxFuture<Result<TableInfo>>;

fn get_dag_block_children(
&self,
Expand Down
7 changes: 3 additions & 4 deletions network-rpc/api/src/remote_chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl ChainStateReader for RemoteChainStateReader {
Ok(state_proof)
}

fn get_account_state(&self, account_address: &AccountAddress) -> Result<Option<AccountState>> {
fn get_account_state(&self, account_address: &AccountAddress) -> Result<AccountState> {
//TODO: How to verify it
let peer_id = self
.peer_id
Expand Down Expand Up @@ -135,9 +135,8 @@ impl ChainStateReader for RemoteChainStateReader {
.ok_or_else(|| anyhow!("peer id not set"))?;
let req = GetTableInfo(address);
let client = self.client.clone();
let table_info: Option<TableInfo> =
futures::executor::block_on(client.get_state_table_info(peer_id, req))?;
Ok(table_info)
let table_info = futures::executor::block_on(client.get_state_table_info(peer_id, req))?;
Ok(Some(table_info))
}
}

Expand Down
11 changes: 7 additions & 4 deletions network-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,18 @@ impl gen_server::NetworkRpc for NetworkRpcImpl {
fn get_state_with_proof(
&self,
_peer_id: PeerId,
req: GetStateWithProof,
_req: GetStateWithProof,
) -> BoxFuture<Result<StateWithProof>> {
// XXX FIXME YSG
/*
let state_service = self.state_service.clone();
let fut = async move {
state_service
.get_with_proof_by_root(req.access_path, req.state_root)
.await
};
Box::pin(fut)
Box::pin(fut) */
unimplemented!("")
}

fn get_state_with_table_item_proof(
Expand All @@ -255,7 +258,7 @@ impl gen_server::NetworkRpc for NetworkRpcImpl {
&self,
_peer_id: PeerId,
request: GetTableInfo,
) -> BoxFuture<Result<Option<TableInfo>>> {
) -> BoxFuture<Result<TableInfo>> {
let state_service = self.state_service.clone();
let fut = async move { state_service.get_table_info(request.0).await };
Box::pin(fut)
Expand All @@ -265,7 +268,7 @@ impl gen_server::NetworkRpc for NetworkRpcImpl {
&self,
_peer_id: PeerId,
req: GetAccountState,
) -> BoxFuture<Result<Option<AccountState>>> {
) -> BoxFuture<Result<AccountState>> {
let state_service = self.state_service.clone();
let fut = async move {
state_service
Expand Down
2 changes: 1 addition & 1 deletion rpc/client/src/remote_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> ChainStateReader for RemoteStateReader<'a> {
.map(Into::into)
}

fn get_account_state(&self, _address: &AccountAddress) -> Result<Option<AccountState>> {
fn get_account_state(&self, _address: &AccountAddress) -> Result<AccountState> {
unimplemented!()
//TODO implement get_account_state by root
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/server/src/module/chain_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use starcoin_rpc_api::types::{
TransactionInfoWithProofView, TransactionView,
};
use starcoin_rpc_api::FutureResult;
use starcoin_state_api::StateView;
use starcoin_vm_types::StateView;
use starcoin_statedb::ChainStateDB;
use starcoin_storage::Storage;
use starcoin_types::access_path::AccessPath;
Expand Down
2 changes: 1 addition & 1 deletion rpc/server/src/module/contract_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use starcoin_types::language_storage::{ModuleId, StructTag};
use starcoin_types::transaction::{DryRunTransaction, RawUserTransaction, TransactionPayload};
use starcoin_vm_types::access_path::AccessPath;
use starcoin_vm_types::file_format::CompiledModule;
use starcoin_vm_types::state_view::StateView;
use starcoin_vm_types::state_store::StateView;
use starcoin_vm_types::transaction::authenticator::AccountPublicKey;
use std::str::FromStr;
use std::sync::Arc;
Expand Down
4 changes: 2 additions & 2 deletions state/api/src/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ pub trait ChainStateReader: StateView {
fn get_with_proof(&self, access_path: &AccessPath) -> Result<StateWithProof>;

/// Gets account state
fn get_account_state(&self, address: &AccountAddress) -> Result<Option<AccountState>>;
fn get_account_state(&self, address: &AccountAddress) -> Result<AccountState>;

/// get whole state data of some account address.
fn get_account_state_set(&self, address: &AccountAddress) -> Result<Option<AccountStateSet>>;

fn exist_account(&self, address: &AccountAddress) -> Result<bool> {
self.get_account_state(address).map(|state| state.is_some())
Ok(self.get_account_state(address).is_ok())
}

/// Gets current state root.
Expand Down
10 changes: 5 additions & 5 deletions state/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl ServiceHandler<Self, StateRequest> for ChainStateService {
StateResponse::StateWithProof(Box::new(self.service.get_with_proof(access_path)?))
}
StateRequest::GetAccountState(address) => {
StateResponse::AccountState(self.service.get_account_state(&address)?)
StateResponse::AccountState(Option::from(self.service.get_account_state(&address)?))
}
StateRequest::StateRoot() => StateResponse::StateRoot(self.service.state_root()),
StateRequest::GetWithProofByRoot(state_key, state_root) => {
Expand All @@ -113,10 +113,10 @@ impl ServiceHandler<Self, StateRequest> for ChainStateService {
))
}
StateRequest::GetAccountStateByRoot(account, state_root) => {
StateResponse::AccountState(
StateResponse::AccountState(Some(
self.service
.get_account_state_by_root(account, state_root)?,
)
))
}
StateRequest::GetAccountStateSet {
address,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl Inner {
&self,
account: AccountAddress,
state_root: HashValue,
) -> Result<Option<AccountState>> {
) -> Result<AccountState> {
let reader = self.state_db.fork_at(state_root);
reader.get_account_state(&account)
}
Expand All @@ -234,7 +234,7 @@ impl ChainStateReader for Inner {
self.state_db.get_with_proof(access_path)
}

fn get_account_state(&self, address: &AccountAddress) -> Result<Option<AccountState>> {
fn get_account_state(&self, address: &AccountAddress) -> Result<AccountState> {
self.state_db.get_account_state(address)
}
fn get_account_state_set(&self, address: &AccountAddress) -> Result<Option<AccountStateSet>> {
Expand Down
10 changes: 6 additions & 4 deletions state/statedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,12 @@ impl ChainStateReader for ChainStateDB {
Ok(state_with_proof)
}

fn get_account_state(&self, address: &AccountAddress) -> Result<Option<AccountState>> {
Ok(self
.get_account_state_object_option(address)?
.map(|state_object| state_object.to_state()))
fn get_account_state(&self, address: &AccountAddress) -> Result<AccountState> {
let account_state = self.get_account_state_object_option(address)?;
match account_state {
Some(account_state) => Ok(account_state.to_state()),
None => Err(AccountNotExist(*address).into()),
}
}

fn get_account_state_set(&self, address: &AccountAddress) -> Result<Option<AccountStateSet>> {
Expand Down
4 changes: 1 addition & 3 deletions txpool/src/pool_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ impl CachedSeqNumberClient {
);
0
}
Ok(account_resource) => account_resource
.map(|res| res.sequence_number())
.unwrap_or_default(),
Ok(account_resource) => account_resource.sequence_number(),
}
}
}
Expand Down

0 comments on commit abe7cd8

Please sign in to comment.