Skip to content

Commit

Permalink
add rooch_listFieldStates and Deprecated the rooch_queryFieldStates
Browse files Browse the repository at this point in the history
  • Loading branch information
pause125 committed Jun 5, 2024
1 parent 147a270 commit 649977c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 199 deletions.
18 changes: 1 addition & 17 deletions crates/rooch-indexer/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state::StateChangeSet;
use moveos_types::transaction::{MoveAction, TransactionExecutionInfo, VerifiedMoveOSTransaction};
use rooch_types::indexer::event::{EventFilter, IndexerEvent, IndexerEventID};
use rooch_types::indexer::state::{
FieldStateFilter, IndexerFieldState, IndexerObjectState, IndexerStateID, ObjectStateFilter,
};
use rooch_types::indexer::state::{IndexerObjectState, IndexerStateID, ObjectStateFilter};
use rooch_types::indexer::transaction::{IndexerTransaction, TransactionFilter};
use rooch_types::transaction::LedgerTransaction;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -110,17 +108,3 @@ pub struct QueryIndexerObjectStatesMessage {
impl Message for QueryIndexerObjectStatesMessage {
type Result = Result<Vec<IndexerObjectState>>;
}

/// Query Indexer Table States Message
#[derive(Debug, Serialize, Deserialize)]
pub struct QueryIndexerFieldStatesMessage {
pub filter: FieldStateFilter,
// exclusive cursor if `Some`, otherwise start from the beginning
pub cursor: Option<IndexerStateID>,
pub limit: usize,
pub descending_order: bool,
}

impl Message for QueryIndexerFieldStatesMessage {
type Result = Result<Vec<IndexerFieldState>>;
}
24 changes: 2 additions & 22 deletions crates/rooch-indexer/src/actor/reader_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use crate::actor::messages::{
QueryIndexerEventsMessage, QueryIndexerFieldStatesMessage, QueryIndexerObjectStatesMessage,
QueryIndexerTransactionsMessage,
QueryIndexerEventsMessage, QueryIndexerObjectStatesMessage, QueryIndexerTransactionsMessage,
};
use crate::indexer_reader::IndexerReader;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use coerce::actor::{context::ActorContext, message::Handler, Actor};
use rooch_types::indexer::event::IndexerEvent;
use rooch_types::indexer::state::{IndexerFieldState, IndexerObjectState};
use rooch_types::indexer::state::IndexerObjectState;
use rooch_types::indexer::transaction::IndexerTransaction;

pub struct IndexerReaderActor {
Expand Down Expand Up @@ -81,22 +80,3 @@ impl Handler<QueryIndexerObjectStatesMessage> for IndexerReaderActor {
.map_err(|e| anyhow!(format!("Failed to query indexer global states: {:?}", e)))
}
}

#[async_trait]
impl Handler<QueryIndexerFieldStatesMessage> for IndexerReaderActor {
async fn handle(
&mut self,
msg: QueryIndexerFieldStatesMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<IndexerFieldState>> {
let QueryIndexerFieldStatesMessage {
filter,
cursor,
limit,
descending_order,
} = msg;
self.indexer_reader
.query_field_states_with_filter(filter, cursor, limit, descending_order)
.map_err(|e| anyhow!(format!("Failed to query indexer table states: {:?}", e)))
}
}
26 changes: 3 additions & 23 deletions crates/rooch-indexer/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::actor::indexer::IndexerActor;
use crate::actor::messages::{
IndexerEventsMessage, IndexerStatesMessage, IndexerTransactionMessage,
QueryIndexerEventsMessage, QueryIndexerFieldStatesMessage, QueryIndexerObjectStatesMessage,
QueryIndexerTransactionsMessage, UpdateIndexerMessage,
QueryIndexerEventsMessage, QueryIndexerObjectStatesMessage, QueryIndexerTransactionsMessage,
UpdateIndexerMessage,
};
use crate::actor::reader_indexer::IndexerReaderActor;
use anyhow::Result;
Expand All @@ -16,9 +16,7 @@ use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state::StateChangeSet;
use moveos_types::transaction::{MoveAction, TransactionExecutionInfo, VerifiedMoveOSTransaction};
use rooch_types::indexer::event::{EventFilter, IndexerEvent, IndexerEventID};
use rooch_types::indexer::state::{
FieldStateFilter, IndexerFieldState, IndexerObjectState, IndexerStateID, ObjectStateFilter,
};
use rooch_types::indexer::state::{IndexerObjectState, IndexerStateID, ObjectStateFilter};
use rooch_types::indexer::transaction::{IndexerTransaction, TransactionFilter};
use rooch_types::transaction::LedgerTransaction;

Expand Down Expand Up @@ -159,22 +157,4 @@ impl IndexerProxy {
})
.await?
}

pub async fn query_field_states(
&self,
filter: FieldStateFilter,
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: usize,
descending_order: bool,
) -> Result<Vec<IndexerFieldState>> {
self.reader_actor
.send(QueryIndexerFieldStatesMessage {
filter,
cursor,
limit,
descending_order,
})
.await?
}
}
19 changes: 4 additions & 15 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::jsonrpc_types::event_view::EventFilterView;
use crate::jsonrpc_types::transaction_view::{TransactionFilterView, TransactionWithInfoView};
use crate::jsonrpc_types::{
AccessPathView, AnnotatedFunctionResultView, BalanceInfoPageView, BytesView, EventOptions,
EventPageView, ExecuteTransactionResponseView, FieldStateFilterView, FieldStatePageView,
FunctionCallView, H256View, IndexerEventPageView, IndexerObjectStatePageView, ObjectIDVecView,
ObjectIDView, ObjectStateFilterView, ObjectStateView, QueryOptions, StateOptions,
StatePageView, StateView, StrView, StructTagView, TransactionWithInfoPageView, TxOptions,
EventPageView, ExecuteTransactionResponseView, FunctionCallView, H256View,
IndexerEventPageView, IndexerObjectStatePageView, ObjectIDVecView, ObjectIDView,
ObjectStateFilterView, ObjectStateView, QueryOptions, StateOptions, StatePageView, StateView,
StrView, StructTagView, TransactionWithInfoPageView, TxOptions,
};
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
Expand Down Expand Up @@ -165,15 +165,4 @@ pub trait RoochAPI {
limit: Option<StrView<usize>>,
query_option: Option<QueryOptions>,
) -> RpcResult<IndexerObjectStatePageView>;

/// Query the Object field states indexer by state filter
#[method(name = "queryFieldStates")]
async fn query_field_states(
&self,
filter: FieldStateFilterView,
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: Option<StrView<usize>>,
query_option: Option<QueryOptions>,
) -> RpcResult<IndexerFieldStatePageView>;
}
3 changes: 1 addition & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::jsonrpc_types::event_view::{EventView, IndexerEventView};
use crate::jsonrpc_types::transaction_view::TransactionWithInfoView;
use crate::jsonrpc_types::{
move_types::{MoveActionTypeView, MoveActionView},
BytesView, FieldStateView, IndexerObjectStateView, StateKVView, StrView, StructTagView,
BytesView, IndexerObjectStateView, StateKVView, StrView, StructTagView,
};
use move_core_types::u256::U256;
use rooch_types::framework::coin::CoinInfo;
Expand All @@ -26,7 +26,6 @@ pub type BalanceInfoPageView = PageView<BalanceInfoView, IndexerStateID>;
pub type IndexerEventPageView = PageView<IndexerEventView, IndexerEventID>;

pub type IndexerObjectStatePageView = PageView<IndexerObjectStateView, IndexerStateID>;
pub type FieldStatePageView = PageView<FieldStateView, IndexerStateID>;

pub type UTXOPageView = PageView<UTXOStateView, IndexerStateID>;
pub type InscriptionPageView = PageView<InscriptionStateView, IndexerStateID>;
Expand Down
50 changes: 1 addition & 49 deletions crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use moveos_types::{
state::{AnnotatedState, State, StateChangeSet, TableTypeInfo},
};
use rooch_types::indexer::state::{
FieldStateFilter, IndexerFieldState, IndexerObjectState, IndexerStateChangeSet,
ObjectStateFilter, StateSyncFilter,
IndexerObjectState, IndexerStateChangeSet, ObjectStateFilter, StateSyncFilter,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -463,53 +462,6 @@ impl ObjectStateFilterView {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct FieldStateView {
pub object_id: ObjectID,
pub key_hex: String,
pub value: Option<AnnotatedMoveValueView>,
pub key_type: TypeTagView,
pub value_type: TypeTagView,
pub tx_order: u64,
pub state_index: u64,
pub created_at: u64,
pub updated_at: u64,
}

impl FieldStateView {
pub fn new_from_field_state(
annotated_state: Option<AnnotatedState>,
state: IndexerFieldState,
) -> FieldStateView {
FieldStateView {
object_id: state.object_id,
key_hex: state.key_hex,
value: annotated_state.map(|v| AnnotatedMoveValueView::from(v.decoded_value)),
key_type: state.key_type.into(),
value_type: state.value_type.into(),
tx_order: state.tx_order,
state_index: state.state_index,
created_at: state.created_at,
updated_at: state.updated_at,
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum FieldStateFilterView {
/// Query by object id.
ObjectId(ObjectID),
}

impl From<FieldStateFilterView> for FieldStateFilter {
fn from(state_filter: FieldStateFilterView) -> Self {
match state_filter {
FieldStateFilterView::ObjectId(object_id) => Self::ObjectId(object_id),
}
}
}

/// Object state view. Used as return type of `getObjectStates`.
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct ObjectStateView {
Expand Down
56 changes: 3 additions & 53 deletions crates/rooch-rpc-server/src/server/rooch_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ use moveos_types::{
};
use rooch_rpc_api::jsonrpc_types::transaction_view::TransactionFilterView;
use rooch_rpc_api::jsonrpc_types::{
account_view::BalanceInfoView, FieldStateFilterView, FieldStatePageView, FieldStateView,
IndexerEventPageView, IndexerObjectStatePageView, IndexerObjectStateView, KeyStateView,
ObjectIDVecView, ObjectStateFilterView, ObjectStateView, QueryOptions, StateKVView,
StateOptions, TxOptions,
account_view::BalanceInfoView, IndexerEventPageView, IndexerObjectStatePageView,
IndexerObjectStateView, KeyStateView, ObjectIDVecView, ObjectStateFilterView, ObjectStateView,
QueryOptions, StateKVView, StateOptions, TxOptions,
};
use rooch_rpc_api::jsonrpc_types::{
event_view::{EventFilterView, EventView, IndexerEventView},
Expand Down Expand Up @@ -711,55 +710,6 @@ impl RoochAPIServer for RoochServer {
has_next_page,
})
}

async fn query_field_states(
&self,
filter: FieldStateFilterView,
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: Option<StrView<usize>>,
query_option: Option<QueryOptions>,
) -> RpcResult<FieldStatePageView> {
let limit_of = min(
limit.map(Into::into).unwrap_or(DEFAULT_RESULT_LIMIT_USIZE),
MAX_RESULT_LIMIT_USIZE,
);
let query_option = query_option.unwrap_or_default();
let descending_order = query_option.descending;

let states = self
.rpc_service
.query_field_states(filter.into(), cursor, limit_of + 1, descending_order)
.await?;

let object_ids = states
.iter()
.map(|m| m.object_id.clone())
.collect::<Vec<_>>();
let access_path = AccessPath::objects(object_ids.clone());
let mut data = self
.rpc_service
.get_annotated_states(access_path)
.await?
.into_iter()
.zip(states)
.map(|(annotated_state, state)| {
FieldStateView::new_from_field_state(annotated_state, state)
})
.collect::<Vec<_>>();

let has_next_page = data.len() > limit_of;
data.truncate(limit_of);
let next_cursor = data.last().cloned().map_or(cursor, |t| {
Some(IndexerStateID::new(t.tx_order, t.state_index))
});

Ok(FieldStatePageView {
data,
next_cursor,
has_next_page,
})
}
}

impl RoochRpcModule for RoochServer {
Expand Down
19 changes: 1 addition & 18 deletions crates/rooch-rpc-server/src/service/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use rooch_sequencer::proxy::SequencerProxy;
use rooch_types::address::{BitcoinAddress, RoochAddress};
use rooch_types::framework::address_mapping::RoochToBitcoinAddressMapping;
use rooch_types::indexer::event::{EventFilter, IndexerEvent, IndexerEventID};
use rooch_types::indexer::state::{
FieldStateFilter, IndexerFieldState, IndexerObjectState, IndexerStateID, ObjectStateFilter,
};
use rooch_types::indexer::state::{IndexerObjectState, IndexerStateID, ObjectStateFilter};
use rooch_types::indexer::transaction::{IndexerTransaction, TransactionFilter};
use rooch_types::sequencer::SequencerOrder;
use rooch_types::transaction::{ExecuteTransactionResponse, LedgerTransaction, RoochTransaction};
Expand Down Expand Up @@ -250,21 +248,6 @@ impl RpcService {
Ok(resp)
}

pub async fn query_field_states(
&self,
filter: FieldStateFilter,
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: usize,
descending_order: bool,
) -> Result<Vec<IndexerFieldState>> {
let resp = self
.indexer
.query_field_states(filter, cursor, limit, descending_order)
.await?;
Ok(resp)
}

pub async fn get_bitcoin_addresses(
&self,
rooch_addresses: Vec<RoochAddress>,
Expand Down

0 comments on commit 649977c

Please sign in to comment.