Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Feat tableinfo rpc (#3954)
* add tableinfo query rpc to state-api

* fix clippy warnings

* use types implemented with JsonSchema trait

* update state.json for rpc-schema
  • Loading branch information
simonjiao committed Aug 29, 2023
1 parent 57a0415 commit 50a0b3b
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions network-rpc/Cargo.toml
Expand Up @@ -28,6 +28,7 @@ starcoin-txpool-api = { workspace = true }
starcoin-storage = { package = "starcoin-storage", workspace = true }
starcoin-types = { package = "starcoin-types", workspace = true }
starcoin-state-tree = { workspace = true }
starcoin-vm-types = { workspace = true }

[dev-dependencies]
starcoin-account-api = { package = "starcoin-account-api", workspace = true }
Expand Down
9 changes: 9 additions & 0 deletions network-rpc/api/src/lib.rs
Expand Up @@ -19,6 +19,7 @@ use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_state::AccountState;
use starcoin_types::block::{Block, BlockHeader, BlockInfo, BlockNumber};
use starcoin_types::transaction::{SignedUserTransaction, Transaction, TransactionInfo};
use starcoin_vm_types::state_store::table::TableInfo;

mod remote_chain_state;

Expand Down Expand Up @@ -286,6 +287,12 @@ pub trait NetworkRpc: Sized + Send + Sync + 'static {
peer_id: PeerId,
request: GetStateWithTableItemProof,
) -> BoxFuture<Result<StateWithTableItemProof>>;

fn get_state_table_info(
&self,
peer_id: PeerId,
request: GetTableInfo,
) -> BoxFuture<Result<Option<TableInfo>>>;
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -294,3 +301,5 @@ pub struct GetStateWithTableItemProof {
pub handle: TableHandle,
pub key: Vec<u8>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetTableInfo(pub AccountAddress);
15 changes: 14 additions & 1 deletion network-rpc/api/src/remote_chain_state.rs
Expand Up @@ -3,6 +3,7 @@

use crate::{
gen_client::NetworkRpcClient, GetAccountState, GetStateWithProof, GetStateWithTableItemProof,
GetTableInfo,
};
use anyhow::{anyhow, Result};
use network_p2p_types::peer_id::PeerId;
Expand All @@ -14,7 +15,7 @@ use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_state::AccountState;
use starcoin_types::state_set::{AccountStateSet, ChainStateSet};
use starcoin_vm_types::state_store::state_key::StateKey;
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};

#[derive(Clone)]
pub struct RemoteChainStateReader {
Expand Down Expand Up @@ -121,6 +122,18 @@ impl ChainStateReader for RemoteChainStateReader {
state_table_item_proof.verify(handle, key)?;
Ok(state_table_item_proof)
}

fn get_table_info(&self, address: AccountAddress) -> Result<Option<TableInfo>> {
let peer_id = self
.peer_id
.clone()
.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)
}
}

impl StateView for RemoteChainStateReader {
Expand Down
15 changes: 13 additions & 2 deletions network-rpc/src/rpc.rs
Expand Up @@ -11,8 +11,8 @@ use starcoin_chain_service::{ChainAsyncService, ChainReaderService};
use starcoin_crypto::HashValue;
use starcoin_network_rpc_api::{
gen_server, BlockBody, GetAccountState, GetAccumulatorNodeByNodeHash, GetBlockHeadersByNumber,
GetBlockIds, GetStateWithProof, GetStateWithTableItemProof, GetTxnsWithHash, GetTxnsWithSize,
Ping, RpcRequest, MAX_BLOCK_HEADER_REQUEST_SIZE, MAX_BLOCK_INFO_REQUEST_SIZE,
GetBlockIds, GetStateWithProof, GetStateWithTableItemProof, GetTableInfo, GetTxnsWithHash,
GetTxnsWithSize, Ping, RpcRequest, MAX_BLOCK_HEADER_REQUEST_SIZE, MAX_BLOCK_INFO_REQUEST_SIZE,
MAX_BLOCK_REQUEST_SIZE, MAX_TXN_REQUEST_SIZE,
};
use starcoin_service_registry::ServiceRef;
Expand All @@ -28,6 +28,7 @@ use starcoin_types::{
block::{BlockHeader, BlockInfo, BlockNumber},
transaction::{SignedUserTransaction, Transaction, TransactionInfo},
};
use starcoin_vm_types::state_store::table::TableInfo;
use std::sync::Arc;

pub struct NetworkRpcImpl {
Expand Down Expand Up @@ -251,6 +252,16 @@ impl gen_server::NetworkRpc for NetworkRpcImpl {
Box::pin(fut)
}

fn get_state_table_info(
&self,
_peer_id: PeerId,
request: GetTableInfo,
) -> BoxFuture<Result<Option<TableInfo>>> {
let state_service = self.state_service.clone();
let fut = async move { state_service.get_table_info(request.0).await };
Box::pin(fut)
}

fn get_account_state(
&self,
_peer_id: PeerId,
Expand Down
37 changes: 37 additions & 0 deletions rpc/api/generated_rpc_schema/state.json
Expand Up @@ -985,6 +985,43 @@
}
}
},
{
"name": "state.get_table_info",
"params": [
{
"name": "address",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AccountAddress",
"type": "string",
"format": "AccountAddress"
}
}
],
"result": {
"name": "Option < TableInfoView >",
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_table_info",
"type": [
"object",
"null"
],
"required": [
"key_type",
"value_type"
],
"properties": {
"key_type": {
"type": "string"
},
"value_type": {
"type": "string"
}
}
}
}
},
{
"name": "state.get_with_table_item_proof",
"params": [
Expand Down
6 changes: 5 additions & 1 deletion rpc/api/src/state/mod.rs
Expand Up @@ -4,7 +4,7 @@
pub use self::gen_client::Client as StateClient;
use crate::types::{
AccountStateSetView, CodeView, ListCodeView, ListResourceView, ResourceView,
StateWithProofView, StateWithTableItemProofView, StrView, StructTagView,
StateWithProofView, StateWithTableItemProofView, StrView, StructTagView, TableInfoView,
};
use crate::FutureResult;
use openrpc_derive::openrpc;
Expand Down Expand Up @@ -63,6 +63,10 @@ pub trait StateApi {
state_root: HashValue,
) -> FutureResult<StrView<Vec<u8>>>;

/// Return the TableInfo according to queried AccountAddress
#[rpc(name = "state.get_table_info")]
fn get_table_info(&self, address: AccountAddress) -> FutureResult<Option<TableInfoView>>;

/// Return the TableItem value and provide a State Proof at `state_root`
#[rpc(name = "state.get_with_table_item_proof")]
fn get_with_table_item_proof(
Expand Down
34 changes: 29 additions & 5 deletions rpc/api/src/types.rs
Expand Up @@ -1180,7 +1180,7 @@ use starcoin_chain_api::{EventWithProof, TransactionInfoWithProof};
use starcoin_types::account_address::AccountAddress;
use starcoin_vm_types::move_resource::MoveResource;
use starcoin_vm_types::state_store::state_key::{StateKey, TableItem};
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
pub use vm_status_translator::VmStatusExplainView;

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -1903,15 +1903,14 @@ impl From<BlockInfo> for BlockInfoView {
#[serde(rename = "table_item")]
pub struct TableItemView {
handle: TableHandle,
#[schemars(with = "String")]
key: Vec<u8>,
key: StrView<Vec<u8>>,
}

impl From<TableItem> for TableItemView {
fn from(table_item: TableItem) -> Self {
Self {
handle: table_item.handle,
key: table_item.key,
key: table_item.key.into(),
}
}
}
Expand All @@ -1930,12 +1929,37 @@ impl From<StateKey> for StateKeyView {
StateKey::AccessPath(access_path) => Self::AccessPath(access_path),
StateKey::TableItem(table_item) => Self::TableItem(TableItemView {
handle: table_item.handle,
key: table_item.key,
key: table_item.key.into(),
}),
}
}
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename = "table_info")]
pub struct TableInfoView {
key_type: TypeTagView,
value_type: TypeTagView,
}

impl From<TableInfo> for TableInfoView {
fn from(value: TableInfo) -> Self {
Self {
key_type: value.key_type.into(),
value_type: value.value_type.into(),
}
}
}

impl From<TableInfoView> for TableInfo {
fn from(value: TableInfoView) -> Self {
Self {
key_type: value.key_type.0,
value_type: value.value_type.0,
}
}
}

#[cfg(test)]
mod tests {
use crate::types::{ByteCodeOrScriptFunction, FunctionId, StrView};
Expand Down
12 changes: 10 additions & 2 deletions rpc/client/src/lib.rs
Expand Up @@ -37,8 +37,8 @@ use starcoin_rpc_api::types::{
DryRunTransactionRequest, FactoryAction, FunctionIdView, ListCodeView, ListResourceView,
MintedBlockView, ModuleIdView, PeerInfoView, ResourceView, SignedMessageView,
SignedUserTransactionView, StateWithProofView, StateWithTableItemProofView, StrView,
StructTagView, TransactionEventResponse, TransactionInfoView, TransactionInfoWithProofView,
TransactionRequest, TransactionView,
StructTagView, TableInfoView, TransactionEventResponse, TransactionInfoView,
TransactionInfoWithProofView, TransactionRequest, TransactionView,
};
use starcoin_rpc_api::{
account::AccountClient, chain::ChainClient, contract_api::ContractClient, debug::DebugClient,
Expand Down Expand Up @@ -656,6 +656,14 @@ impl RpcClient {
.map_err(map_err)
}

pub fn state_get_table_info(
&self,
address: AccountAddress,
) -> anyhow::Result<Option<TableInfoView>> {
self.call_rpc_blocking(|inner| inner.state_client.get_table_info(address))
.map_err(map_err)
}

pub fn get_state_node_by_node_hash(
&self,
key_hash: HashValue,
Expand Down
7 changes: 6 additions & 1 deletion rpc/client/src/remote_state_reader.rs
Expand Up @@ -12,7 +12,7 @@ use starcoin_types::account_state::AccountState;
use starcoin_types::block::BlockNumber;
use starcoin_types::state_set::{AccountStateSet, ChainStateSet};
use starcoin_vm_types::state_store::state_key::StateKey;
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
use std::str::FromStr;

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -109,6 +109,11 @@ impl<'a> ChainStateReader for RemoteStateReader<'a> {
.state_get_with_table_item_proof_by_root(*handle, key.to_vec(), self.state_root)
.map(Into::into)
}
fn get_table_info(&self, address: AccountAddress) -> Result<Option<TableInfo>> {
self.client
.state_get_table_info(address)
.map(|v| v.map(Into::into))
}
}

impl<'a> StateView for RemoteStateReader<'a> {
Expand Down
11 changes: 11 additions & 0 deletions rpc/server/src/module/state_rpc.rs
Expand Up @@ -15,6 +15,7 @@ use starcoin_rpc_api::state::{
use starcoin_rpc_api::types::{
AccountStateSetView, AnnotatedMoveStructView, CodeView, ListCodeView, ListResourceView,
ResourceView, StateWithProofView, StateWithTableItemProofView, StrView, StructTagView,
TableInfoView,
};
use starcoin_rpc_api::FutureResult;
use starcoin_state_api::{ChainStateAsyncService, StateView};
Expand Down Expand Up @@ -185,6 +186,16 @@ where
Box::pin(fut)
}

fn get_table_info(&self, address: AccountAddress) -> FutureResult<Option<TableInfoView>> {
let fut = self
.service
.clone()
.get_table_info(address)
.map_ok(|v| v.map(Into::into))
.map_err(map_err);
Box::pin(fut)
}

fn get_with_table_item_proof(
&self,
handle: TableHandle,
Expand Down
4 changes: 3 additions & 1 deletion state/api/src/chain_state.rs
Expand Up @@ -19,7 +19,7 @@ use starcoin_types::{
use starcoin_vm_types::account_config::TABLE_HANDLE_ADDRESS_LIST;
use starcoin_vm_types::genesis_config::ChainId;
use starcoin_vm_types::on_chain_resource::{Epoch, EpochInfo, GlobalTimeOnChain};
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
use starcoin_vm_types::token::token_code::TokenCode;
use starcoin_vm_types::{
move_resource::MoveResource, on_chain_config::OnChainConfig, state_view::StateView,
Expand Down Expand Up @@ -135,6 +135,8 @@ pub trait ChainStateReader: StateView {
handle: &TableHandle,
key: &[u8],
) -> Result<StateWithTableItemProof>;

fn get_table_info(&self, address: AccountAddress) -> Result<Option<TableInfo>>;
}

pub trait ChainStateWriter {
Expand Down
13 changes: 12 additions & 1 deletion state/api/src/lib.rs
Expand Up @@ -21,7 +21,7 @@ use starcoin_types::state_set::AccountStateSet;
use starcoin_vm_types::access_path::DataPath;
use starcoin_vm_types::account_config::TABLE_HANDLE_ADDRESS_LIST;
use starcoin_vm_types::move_resource::MoveResource;
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
pub use starcoin_vm_types::state_view::{StateReaderExt, StateView};

mod chain_state;
Expand Down Expand Up @@ -92,6 +92,8 @@ pub trait ChainStateAsyncService: Clone + std::marker::Unpin + Send + Sync {
key: Vec<u8>,
state_root: HashValue,
) -> Result<StateWithTableItemProof>;

async fn get_table_info(self, address: AccountAddress) -> Result<Option<TableInfo>>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -216,6 +218,15 @@ where
panic!("Unexpect response type.")
}
}

async fn get_table_info(self, address: AccountAddress) -> Result<Option<TableInfo>> {
let response = self.send(StateRequest::GetTableInfo(address)).await??;
if let StateResponse::TableInfo(state) = response {
Ok(state)
} else {
panic!("Unexpect response type.")
}
}
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion state/api/src/message.rs
Expand Up @@ -9,7 +9,7 @@ use starcoin_types::state_set::AccountStateSet;
use starcoin_types::{
access_path::AccessPath, account_address::AccountAddress, account_state::AccountState,
};
use starcoin_vm_types::state_store::table::TableHandle;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};

#[derive(Debug, Clone)]
pub enum StateRequest {
Expand All @@ -25,6 +25,7 @@ pub enum StateRequest {
StateRoot(),
GetWithTableItemProof(TableHandle, Vec<u8>),
GetWithTableItemProofByRoot(TableHandle, Vec<u8>, HashValue),
GetTableInfo(AccountAddress),
}

impl ServiceRequest for StateRequest {
Expand All @@ -40,4 +41,5 @@ pub enum StateResponse {
AccountStateSet(Option<AccountStateSet>),
None,
StateWithTableItemProof(Box<StateWithTableItemProof>),
TableInfo(Option<TableInfo>),
}

0 comments on commit 50a0b3b

Please sign in to comment.