Skip to content

Commit

Permalink
support state list iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
baichuan3 committed Aug 2, 2023
1 parent 2a30074 commit fc08baf
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 18 deletions.
29 changes: 28 additions & 1 deletion crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use super::messages::{
ExecuteViewFunctionMessage, GetEventsByEventHandleMessage, GetEventsMessage, ResolveMessage,
StatesMessage, ValidateTransactionMessage,
};
use crate::actor::messages::{GetTransactionInfosByTxHashMessage, GetTxSeqMappingByTxOrderMessage};
use crate::actor::messages::{
GetTransactionInfosByTxHashMessage, GetTxSeqMappingByTxOrderMessage,
ListAnnotatedStatesMessage, ListStatesMessage,
};
use anyhow::bail;
use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -237,6 +240,30 @@ impl Handler<AnnotatedStatesMessage> for ExecutorActor {
}
}

#[async_trait]
impl Handler<ListStatesMessage> for ExecutorActor {
async fn handle(
&mut self,
msg: ListStatesMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<(Vec<u8>, State)>>, anyhow::Error> {
let statedb = self.moveos.moveos_resolver();
statedb.list_states(msg.access_path, msg.cursor, msg.limit)
}
}

#[async_trait]
impl Handler<ListAnnotatedStatesMessage> for ExecutorActor {
async fn handle(
&mut self,
msg: ListAnnotatedStatesMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<(Vec<u8>, AnnotatedState)>>, anyhow::Error> {
let statedb = self.moveos.moveos_resolver();
statedb.list_annotated_states(msg.access_path, msg.cursor, msg.limit)
}
}

#[async_trait]
impl Handler<GetEventsByEventHandleMessage> for ExecutorActor {
async fn handle(
Expand Down
23 changes: 23 additions & 0 deletions crates/rooch-executor/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use moveos_types::event::AnnotatedMoveOSEvent;
use moveos_types::event_filter::EventFilter;
use moveos_types::function_return_value::AnnotatedFunctionReturnValue;
use moveos_types::h256::H256;
use moveos_types::list_access_path::AccessPathList;
use moveos_types::state::{AnnotatedState, State};
use moveos_types::transaction::FunctionCall;
use moveos_types::transaction::TransactionExecutionInfo;
Expand Down Expand Up @@ -81,6 +82,28 @@ impl Message for AnnotatedStatesMessage {
type Result = Result<Vec<Option<AnnotatedState>>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListStatesMessage {
pub access_path: AccessPathList,
pub cursor: Option<Vec<u8>>,
pub limit: usize,
}

impl Message for ListStatesMessage {
type Result = Result<Vec<Option<(Vec<u8>, State)>>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListAnnotatedStatesMessage {
pub access_path: AccessPathList,
pub cursor: Option<Vec<u8>>,
pub limit: usize,
}

impl Message for ListAnnotatedStatesMessage {
type Result = Result<Vec<Option<(Vec<u8>, AnnotatedState)>>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetEventsByEventHandleMessage {
pub event_handle_type: StructTag,
Expand Down
36 changes: 35 additions & 1 deletion crates/rooch-executor/src/proxy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::actor::messages::{GetTransactionInfosByTxHashMessage, GetTxSeqMappingByTxOrderMessage};
use crate::actor::messages::{
GetTransactionInfosByTxHashMessage, GetTxSeqMappingByTxOrderMessage,
ListAnnotatedStatesMessage, ListStatesMessage,
};
use crate::actor::{
executor::ExecutorActor,
messages::{
Expand All @@ -14,6 +17,7 @@ use coerce::actor::ActorRef;
use move_core_types::account_address::AccountAddress;
use move_core_types::language_storage::StructTag;
use moveos_types::h256::H256;
use moveos_types::list_access_path::AccessPathList;
use moveos_types::transaction::FunctionCall;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::TransactionOutput;
Expand Down Expand Up @@ -82,6 +86,36 @@ impl ExecutorProxy {
.await?
}

pub async fn list_states(
&self,
access_path: AccessPathList,
cursor: Option<Vec<u8>>,
limit: usize,
) -> Result<Vec<Option<(Vec<u8>, State)>>> {
self.actor
.send(ListStatesMessage {
access_path,
cursor,
limit,
})
.await?
}

pub async fn list_annotated_states(
&self,
access_path: AccessPathList,
cursor: Option<Vec<u8>>,
limit: usize,
) -> Result<Vec<Option<(Vec<u8>, AnnotatedState)>>> {
self.actor
.send(ListAnnotatedStatesMessage {
access_path,
cursor,
limit,
})
.await?
}

pub async fn get_events_by_event_handle(
&self,
event_handle_type: StructTag,
Expand Down
143 changes: 143 additions & 0 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,74 @@
}
}
},
{
"name": "rooch_listAnnotatedStates",
"description": "List the annotated states by access_path The annotated states include the decoded move value of the state",
"params": [
{
"name": "access_path",
"required": true,
"schema": {
"$ref": "#/components/schemas/moveos_types::list_access_path::AccessPathList"
}
},
{
"name": "cursor",
"schema": {
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
}
},
{
"name": "limit",
"schema": {
"type": "integer",
"format": "uint",
"minimum": 0.0
}
}
],
"result": {
"name": "ListAnnotatedStatesPageView",
"required": true,
"schema": {
"$ref": "#/components/schemas/PageView_for_Nullable_AnnotatedStateView_and_alloc::vec::Vec<u8>"
}
}
},
{
"name": "rooch_listStates",
"description": "List the states by access_path",
"params": [
{
"name": "access_path",
"required": true,
"schema": {
"$ref": "#/components/schemas/moveos_types::list_access_path::AccessPathList"
}
},
{
"name": "cursor",
"schema": {
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
}
},
{
"name": "limit",
"schema": {
"type": "integer",
"format": "uint",
"minimum": 0.0
}
}
],
"result": {
"name": "ListStatesPageView",
"required": true,
"schema": {
"$ref": "#/components/schemas/PageView_for_Nullable_StateView_and_alloc::vec::Vec<u8>"
}
}
},
{
"name": "rooch_sendRawTransaction",
"description": "Send the signed transaction in bcs hex format This method does not block waiting for the transaction to be executed.",
Expand Down Expand Up @@ -1035,6 +1103,78 @@
}
}
},
"PageView_for_Nullable_AnnotatedStateView_and_alloc::vec::Vec<u8>": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
"required": [
"data",
"has_next_page"
],
"properties": {
"data": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/components/schemas/AnnotatedStateView"
},
{
"type": "null"
}
]
}
},
"has_next_page": {
"type": "boolean"
},
"next_cursor": {
"anyOf": [
{
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
},
{
"type": "null"
}
]
}
}
},
"PageView_for_Nullable_StateView_and_alloc::vec::Vec<u8>": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
"required": [
"data",
"has_next_page"
],
"properties": {
"data": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/components/schemas/StateView"
},
{
"type": "null"
}
]
}
},
"has_next_page": {
"type": "boolean"
},
"next_cursor": {
"anyOf": [
{
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
},
{
"type": "null"
}
]
}
}
},
"PageView_for_Nullable_TransactionExecutionInfoView_and_uint128": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
Expand Down Expand Up @@ -1314,6 +1454,9 @@
"moveos_types::access_path::AccessPath": {
"type": "string"
},
"moveos_types::list_access_path::AccessPathList": {
"type": "string"
},
"moveos_types::move_types::FunctionId": {
"type": "string"
},
Expand Down
27 changes: 23 additions & 4 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::jsonrpc_types::{
AccessPathView, AnnotatedEventView, AnnotatedFunctionReturnValueView, AnnotatedStateView,
EventFilterView, EventPageView, ExecuteTransactionResponseView, FunctionCallView, H256View,
StateView, StrView, StructTagView, TransactionExecutionInfoView, TransactionInfoPageView,
TransactionView,
AccessPathListView, AccessPathView, AnnotatedEventView, AnnotatedFunctionReturnValueView,
AnnotatedStateView, EventFilterView, EventPageView, ExecuteTransactionResponseView,
FunctionCallView, H256View, ListAnnotatedStatesPageView, ListStatesPageView, StateView,
StrView, StructTagView, TransactionExecutionInfoView, TransactionInfoPageView, TransactionView,
};
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
Expand Down Expand Up @@ -47,6 +47,25 @@ pub trait RoochAPI {
access_path: AccessPathView,
) -> RpcResult<Vec<Option<AnnotatedStateView>>>;

/// List the states by access_path
#[method(name = "listStates")]
async fn list_states(
&self,
access_path: AccessPathListView,
cursor: Option<StrView<Vec<u8>>>,
limit: Option<usize>,
) -> RpcResult<ListStatesPageView>;

/// List the annotated states by access_path
/// The annotated states include the decoded move value of the state
#[method(name = "listAnnotatedStates")]
async fn list_annotated_states(
&self,
access_path: AccessPathListView,
cursor: Option<StrView<Vec<u8>>>,
limit: Option<usize>,
) -> RpcResult<ListAnnotatedStatesPageView>;

/// Get the events by event handle id
#[method(name = "getEventsByEventHandle")]
async fn get_events_by_event_handle(
Expand Down
5 changes: 3 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/move_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use moveos_types::{
use fastcrypto::encoding::Hex;
use serde_with::serde_as;

use moveos_types::list_access_path::AccessPathList;
use moveos_types::{
move_string::{MoveAsciiString, MoveString},
state::MoveStructState,
Expand All @@ -42,9 +43,9 @@ pub type StructTagView = StrView<StructTag>;
pub type FunctionIdView = StrView<FunctionId>;
pub type AccessPathView = StrView<AccessPath>;
pub type AccountAddressView = StrView<AccountAddress>;
pub type AccessPathListView = StrView<AccessPathList>;

impl_str_view_for! {TypeTag StructTag FunctionId AccessPath}
// impl_str_view_for! {TypeTag StructTag ModuleId FunctionId}
impl_str_view_for! {TypeTag StructTag FunctionId AccessPath AccessPathList}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct AnnotatedMoveStructView {
Expand Down
5 changes: 4 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use crate::jsonrpc_types::{
move_types::{MoveActionTypeView, MoveActionView},
AnnotatedMoveStructView, EventView, H256View, StrView, TransactionExecutionInfoView,
AnnotatedMoveStructView, AnnotatedStateView, EventView, H256View, StateView, StrView,
TransactionExecutionInfoView,
};
use moveos_types::event::AnnotatedMoveOSEvent;
use rooch_types::transaction::{AbstractTransaction, TransactionType, TypedTransaction};
Expand All @@ -14,6 +15,8 @@ use super::AccountAddressView;

pub type EventPageView = PageView<Option<AnnotatedEventView>, u64>;
pub type TransactionInfoPageView = PageView<Option<TransactionExecutionInfoView>, u128>;
pub type ListStatesPageView = PageView<Option<StateView>, StrView<Vec<u8>>>;
pub type ListAnnotatedStatesPageView = PageView<Option<AnnotatedStateView>, StrView<Vec<u8>>>;

/// `next_cursor` points to the last item in the page;
/// Reading with `next_cursor` will start from the next item after `next_cursor` if
Expand Down
Loading

0 comments on commit fc08baf

Please sign in to comment.