Skip to content

Commit

Permalink
add an command line argument for getStates command
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Sep 22, 2024
1 parent 3dbc581 commit d39956e
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 65 deletions.
3 changes: 2 additions & 1 deletion crates/rooch-executor/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use moveos_types::moveos_std::event::{AnnotatedEvent, Event, EventID};
use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::state::{AnnotatedState, FieldKey, ObjectState, StateChangeSetExt};
use moveos_types::state_resolver::{AnnotatedStateKV, StateKV};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::TransactionOutput;
use moveos_types::transaction::VerifiedMoveOSTransaction;
Expand Down Expand Up @@ -99,7 +100,7 @@ impl Message for ExecuteViewFunctionMessage {

#[derive(Debug, Serialize, Deserialize)]
pub struct StatesMessage {
pub state_root: Option<String>,
pub state_root: StateRootHash,
pub access_path: AccessPath,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-executor/src/actor/reader_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ impl Handler<StatesMessage> for ReaderExecutorActor {
msg: StatesMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<ObjectState>>, anyhow::Error> {
let resolver = if let Some(state_root_str) = msg.state_root {
let hex_bytes = hex::decode(state_root_str).expect("decode root state failed");
let resolver = if !msg.state_root.is_empty() {
let hex_bytes = hex::decode(msg.state_root.0).expect("decode root state failed");
let state_root = H256::from_slice(hex_bytes.as_slice());
let root_object_meta = ObjectMeta::root_metadata(state_root, 55);
RootObjectResolver::new(root_object_meta, &self.moveos_store)
Expand Down
39 changes: 23 additions & 16 deletions crates/rooch-executor/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state::{FieldKey, StateChangeSetExt};
use moveos_types::state_resolver::{AnnotatedStateKV, StateKV};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::transaction::FunctionCall;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::TransactionOutput;
Expand Down Expand Up @@ -118,8 +119,8 @@ impl ExecutorProxy {

pub async fn get_states(
&self,
state_root: Option<String>,
access_path: AccessPath,
state_root: StateRootHash,
) -> Result<Vec<Option<ObjectState>>> {
self.reader_actor
.send(StatesMessage {
Expand Down Expand Up @@ -268,31 +269,37 @@ impl ExecutorProxy {
}

pub async fn chain_id(&self) -> Result<ChainID> {
self.get_states(None, AccessPath::object(ChainID::chain_id_object_id()))
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("chain id not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("chain id not found")))
.and_then(|state| Ok(state.into_object::<ChainID>()?.value))
self.get_states(
AccessPath::object(ChainID::chain_id_object_id()),
StateRootHash::empty(),
)
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("chain id not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("chain id not found")))
.and_then(|state| Ok(state.into_object::<ChainID>()?.value))
}

pub async fn bitcoin_network(&self) -> Result<BitcoinNetwork> {
self.get_states(None, AccessPath::object(BitcoinNetwork::object_id()))
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("bitcoin network not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("bitcoin network not found")))
.and_then(|state| Ok(state.into_object::<BitcoinNetwork>()?.value))
self.get_states(
AccessPath::object(BitcoinNetwork::object_id()),
StateRootHash::empty(),
)
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("bitcoin network not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("bitcoin network not found")))
.and_then(|state| Ok(state.into_object::<BitcoinNetwork>()?.value))
}

//TODO provide a trait to abstract the async state reader, elemiate the duplicated code bwteen RpcService and Client
pub async fn get_sequence_number(&self, address: AccountAddress) -> Result<u64> {
Ok(self
.get_states(
None,
AccessPath::object(Account::account_object_id(address)),
StateRootHash::empty(),
)
.await?
.pop()
Expand Down
14 changes: 10 additions & 4 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use crate::jsonrpc_types::{
FieldKeyView, FunctionCallView, H256View, IndexerEventPageView, IndexerObjectStatePageView,
IndexerStateIDView, ModuleABIView, ObjectIDVecView, ObjectIDView, ObjectStateFilterView,
ObjectStateView, QueryOptions, RoochAddressView, StateChangeSetPageView, StateOptions,
StatePageView, StrView, StructTagView, SyncStateFilterView, TransactionWithInfoPageView,
TxOptions,
StatePageView, StateRootHashView, StrView, StructTagView, SyncStateFilterView,
TransactionWithInfoPageView, TxOptions,
};
use crate::RpcResult;
use jsonrpsee::proc_macros::rpc;
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::{access_path::AccessPath, state::FieldKey};
use rooch_open_rpc_macros::open_rpc;

Expand Down Expand Up @@ -57,8 +58,8 @@ pub trait RoochAPI {
#[method(name = "getStates")]
async fn get_states(
&self,
state_root: Option<String>,
access_path: AccessPathView,
state_root: StateRootHashView,
state_option: Option<StateOptions>,
) -> RpcResult<Vec<Option<ObjectStateView>>>;

Expand Down Expand Up @@ -92,7 +93,12 @@ pub trait RoochAPI {
let key_states = field_key.into_iter().map(FieldKey::from).collect();
let access_path_view =
AccessPathView::from(AccessPath::fields(object_id.into(), key_states));
self.get_states(None, access_path_view, state_option).await
self.get_states(
access_path_view,
StrView::from(StateRootHash::empty()),
state_option,
)
.await
}

/// List Object Fields via ObjectID.
Expand Down
17 changes: 17 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/str_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use move_core_types::account_address::AccountAddress;
use moveos_types::move_std::string::MoveString;
use moveos_types::state_root_hash::StateRootHash;
use schemars::gen::SchemaGenerator;
use schemars::schema::{InstanceType, Schema, SchemaObject};
use schemars::JsonSchema;
Expand Down Expand Up @@ -325,3 +326,19 @@ where
self.0.to_human_readable_string(verbose, indent)
}
}

pub type StateRootHashView = StrView<StateRootHash>;

impl FromStr for StateRootHashView {
type Err = anyhow::Error;

fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
Ok(Self(StateRootHash(s.to_string())))
}
}

impl std::fmt::Display for StateRootHashView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
11 changes: 9 additions & 2 deletions crates/rooch-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use moveos_types::moveos_std::move_module::MoveModule;
use moveos_types::moveos_std::object::{ObjectID, ObjectMeta, RawField};
use moveos_types::state::{FieldKey, MoveType, ObjectState};
use moveos_types::state_resolver::{StateKV, StateResolver, StatelessResolver};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::{
function_return_value::FunctionResult, module_binding::MoveFunctionCaller,
moveos_std::tx_context::TxContext, transaction::FunctionCall,
Expand Down Expand Up @@ -114,7 +115,10 @@ impl ModuleResolver for &Client {
fn get_module(&self, id: &ModuleId) -> Result<Option<Vec<u8>>> {
tokio::task::block_in_place(|| {
Handle::current().block_on(async {
let mut states = self.rooch.get_states(None, AccessPath::module(id)).await?;
let mut states = self
.rooch
.get_states(AccessPath::module(id), StateRootHash::empty())
.await?;
states
.pop()
.flatten()
Expand Down Expand Up @@ -196,7 +200,10 @@ impl StatelessResolver for ClientResolver {
let mut object_state_view_list = self
.client
.rooch
.get_states(Some(hex::encode(state_root.0.as_slice())), access_path)
.get_states(
access_path,
StateRootHash::new(hex::encode(state_root.0.as_slice()).as_str()),
)
.await?;
Ok(object_state_view_list.pop().flatten().map(|state_view| {
let v: ObjectState = state_view.into();
Expand Down
15 changes: 8 additions & 7 deletions crates/rooch-rpc-client/src/rooch_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use moveos_types::move_std::string::MoveString;
use moveos_types::moveos_std::account::Account;
use moveos_types::moveos_std::object::ObjectID;
use moveos_types::state::{FieldKey, MoveStructState};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::{access_path::AccessPath, state::ObjectState, transaction::FunctionCall};
use rooch_rpc_api::api::btc_api::BtcAPIClient;
use rooch_rpc_api::api::rooch_api::RoochAPIClient;
Expand Down Expand Up @@ -91,25 +92,25 @@ impl RoochRpcClient {

pub async fn get_states(
&self,
state_root: Option<String>,
access_path: AccessPath,
state_root: StateRootHash,
) -> Result<Vec<Option<ObjectStateView>>> {
Ok(self
.http
.get_states(state_root, access_path.into(), None)
.get_states(access_path.into(), state_root.into(), None)
.await?)
}

pub async fn get_decoded_states(
&self,
state_root: Option<String>,
access_path: AccessPath,
state_root: StateRootHash,
) -> Result<Vec<Option<ObjectStateView>>> {
Ok(self
.http
.get_states(
state_root,
access_path.into(),
state_root.into(),
Some(StateOptions::default().decode(true)),
)
.await?)
Expand All @@ -122,8 +123,8 @@ impl RoochRpcClient {
Ok(self
.http
.get_states(
None,
access_path.into(),
StateRootHash::empty().into(),
Some(StateOptions::default().decode(true).show_display(true)),
)
.await?)
Expand Down Expand Up @@ -176,8 +177,8 @@ impl RoochRpcClient {
pub async fn get_sequence_number(&self, sender: RoochAddress) -> Result<u64> {
Ok(self
.get_states(
None,
AccessPath::object(Account::account_object_id(sender.into())),
StateRootHash::empty(),
)
.await?
.pop()
Expand Down Expand Up @@ -383,7 +384,7 @@ impl RoochRpcClient {
account: RoochAddress,
) -> Result<Option<T>> {
let access_path = AccessPath::resource(account.into(), T::struct_tag());
let mut states = self.get_states(None, access_path).await?;
let mut states = self.get_states(access_path, StateRootHash::empty()).await?;
let state = states.pop().flatten();
if let Some(state) = state {
let state = ObjectState::from(state);
Expand Down
20 changes: 11 additions & 9 deletions crates/rooch-rpc-server/src/server/rooch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use jsonrpsee::{core::async_trait, RpcModule};
use move_core_types::{
account_address::AccountAddress, identifier::Identifier, language_storage::ModuleId,
};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::{
access_path::AccessPath,
h256::H256,
Expand All @@ -25,8 +26,9 @@ use rooch_rpc_api::jsonrpc_types::{
IndexerEventPageView, IndexerObjectStatePageView, IndexerStateIDView, ModuleABIView,
ObjectIDVecView, ObjectStateFilterView, ObjectStateView, QueryOptions,
RawTransactionOutputView, RoochAddressView, StateChangeSetPageView,
StateChangeSetWithTxOrderView, StateKVView, StateOptions, StatePageView, StrView,
StructTagView, SyncStateFilterView, TransactionWithInfoPageView, TxOptions, UnitedAddressView,
StateChangeSetWithTxOrderView, StateKVView, StateOptions, StatePageView, StateRootHashView,
StrView, StructTagView, SyncStateFilterView, TransactionWithInfoPageView, TxOptions,
UnitedAddressView,
};
use rooch_rpc_api::{
api::rooch_api::RoochAPIServer,
Expand Down Expand Up @@ -195,8 +197,8 @@ impl RoochAPIServer for RoochServer {

async fn get_states(
&self,
state_root: Option<String>,
access_path: AccessPathView,
state_root: StateRootHashView,
state_option: Option<StateOptions>,
) -> RpcResult<Vec<Option<ObjectStateView>>> {
let state_option = state_option.unwrap_or_default();
Expand All @@ -213,7 +215,7 @@ impl RoochAPIServer for RoochServer {
let valid_states = states.iter().filter_map(|s| s.as_ref()).collect::<Vec<_>>();
let mut valid_display_field_views = self
.rpc_service
.get_display_fields_and_render(state_root, valid_states.as_slice())
.get_display_fields_and_render(valid_states.as_slice(), state_root.0)
.await?;
valid_display_field_views.reverse();
states
Expand All @@ -237,7 +239,7 @@ impl RoochAPIServer for RoochServer {
}
} else {
self.rpc_service
.get_states(state_root, access_path.into())
.get_states(access_path.into(), state_root.0)
.await?
.into_iter()
.map(|s| s.map(ObjectStateView::from))
Expand Down Expand Up @@ -276,7 +278,7 @@ impl RoochAPIServer for RoochServer {
if show_display {
let display_field_views = self
.rpc_service
.get_display_fields_and_render(None, state_refs.as_slice())
.get_display_fields_and_render(state_refs.as_slice(), StateRootHash::empty())
.await?;
key_states
.into_iter()
Expand Down Expand Up @@ -336,7 +338,7 @@ impl RoochAPIServer for RoochServer {
let mut valid_display_field_views = if show_display {
let valid_states = states.iter().filter_map(|s| s.as_ref()).collect::<Vec<_>>();
self.rpc_service
.get_display_fields_and_render(None, valid_states.as_slice())
.get_display_fields_and_render(valid_states.as_slice(), StateRootHash::empty())
.await?
} else {
vec![]
Expand Down Expand Up @@ -369,7 +371,7 @@ impl RoochAPIServer for RoochServer {
}
} else {
self.rpc_service
.get_states(None, access_path)
.get_states(access_path, StateRootHash::empty())
.await?
.into_iter()
.map(|s| s.map(Into::into))
Expand Down Expand Up @@ -622,7 +624,7 @@ impl RoochAPIServer for RoochServer {
let access_path = AccessPath::module(&module_id);
let module = self
.rpc_service
.get_states(None, access_path)
.get_states(access_path, StateRootHash::empty())
.await?
.pop()
.flatten();
Expand Down
5 changes: 3 additions & 2 deletions crates/rooch-rpc-server/src/service/aggregate_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use moveos_types::access_path::AccessPath;
use moveos_types::h256::H256;
use moveos_types::moveos_std::object::ObjectID;
use moveos_types::state::PlaceholderStruct;
use moveos_types::state_root_hash::StateRootHash;
use rooch_rpc_api::jsonrpc_types::account_view::BalanceInfoView;
use rooch_rpc_api::jsonrpc_types::CoinInfoView;
use rooch_types::address::RoochAddress;
Expand Down Expand Up @@ -45,7 +46,7 @@ impl AggregateService {
.collect(),
);
self.rpc_service
.get_states(None, access_path)
.get_states(access_path, StateRootHash::empty())
.await?
.into_iter()
.zip(coin_types)
Expand All @@ -72,7 +73,7 @@ impl AggregateService {
) -> Result<Vec<Option<CoinStoreInfo>>> {
let access_path = AccessPath::objects(coin_store_ids);
self.rpc_service
.get_states(None, access_path)
.get_states(access_path, StateRootHash::empty())
.await?
.into_iter()
.map(|state_opt| state_opt.map(CoinStoreInfo::try_from).transpose())
Expand Down
Loading

0 comments on commit d39956e

Please sign in to comment.