diff --git a/crates/rooch-executor/src/actor/messages.rs b/crates/rooch-executor/src/actor/messages.rs index 513b89c9c0..2e85ac7ec9 100644 --- a/crates/rooch-executor/src/actor/messages.rs +++ b/crates/rooch-executor/src/actor/messages.rs @@ -223,3 +223,12 @@ pub struct SaveStateChangeSetMessage { impl Message for SaveStateChangeSetMessage { type Result = Result<()>; } + +#[derive(Debug, Serialize, Deserialize)] +pub struct GetStateChangeSetsMessage { + pub tx_orders: Vec, +} + +impl Message for GetStateChangeSetsMessage { + type Result = Result>>; +} diff --git a/crates/rooch-executor/src/actor/reader_executor.rs b/crates/rooch-executor/src/actor/reader_executor.rs index 6db996878e..fc67b2bc5c 100644 --- a/crates/rooch-executor/src/actor/reader_executor.rs +++ b/crates/rooch-executor/src/actor/reader_executor.rs @@ -3,8 +3,8 @@ use super::messages::{ AnnotatedStatesMessage, ExecuteViewFunctionMessage, GetAnnotatedEventsByEventHandleMessage, - GetAnnotatedEventsByEventIDsMessage, GetEventsByEventHandleMessage, RefreshStateMessage, - StatesMessage, + GetAnnotatedEventsByEventIDsMessage, GetEventsByEventHandleMessage, GetStateChangeSetsMessage, + RefreshStateMessage, StatesMessage, }; use crate::actor::messages::{ GetEventsByEventIDsMessage, GetTxExecutionInfosByHashMessage, ListAnnotatedStatesMessage, @@ -24,7 +24,7 @@ use moveos_types::function_return_value::AnnotatedFunctionReturnValue; use moveos_types::moveos_std::event::EventHandle; use moveos_types::moveos_std::event::{AnnotatedEvent, Event}; use moveos_types::moveos_std::object::ObjectMeta; -use moveos_types::state::{AnnotatedState, ObjectState}; +use moveos_types::state::{AnnotatedState, ObjectState, StateChangeSetExt}; use moveos_types::state_resolver::RootObjectResolver; use moveos_types::state_resolver::{AnnotatedStateKV, AnnotatedStateReader, StateKV, StateReader}; use moveos_types::transaction::TransactionExecutionInfo; @@ -323,3 +323,17 @@ impl Handler for ReaderExecutorActor { Ok(()) } } + +#[async_trait] +impl Handler for ReaderExecutorActor { + async fn handle( + &mut self, + msg: GetStateChangeSetsMessage, + _ctx: &mut ActorContext, + ) -> Result>> { + let GetStateChangeSetsMessage { tx_orders } = msg; + self.rooch_store + .state_store + .multi_get_state_change_set(tx_orders) + } +} diff --git a/crates/rooch-executor/src/proxy/mod.rs b/crates/rooch-executor/src/proxy/mod.rs index 4e3a285f84..e88cd073b4 100644 --- a/crates/rooch-executor/src/proxy/mod.rs +++ b/crates/rooch-executor/src/proxy/mod.rs @@ -3,9 +3,9 @@ use crate::actor::messages::{ ConvertL2TransactionData, DryRunTransactionResult, GetAnnotatedEventsByEventIDsMessage, - GetEventsByEventHandleMessage, GetEventsByEventIDsMessage, GetTxExecutionInfosByHashMessage, - ListAnnotatedStatesMessage, ListStatesMessage, RefreshStateMessage, SaveStateChangeSetMessage, - ValidateL1BlockMessage, ValidateL1TxMessage, + GetEventsByEventHandleMessage, GetEventsByEventIDsMessage, GetStateChangeSetsMessage, + GetTxExecutionInfosByHashMessage, ListAnnotatedStatesMessage, ListStatesMessage, + RefreshStateMessage, SaveStateChangeSetMessage, ValidateL1BlockMessage, ValidateL1TxMessage, }; use crate::actor::reader_executor::ReaderExecutorActor; use crate::actor::{ @@ -251,6 +251,15 @@ impl ExecutorProxy { .map_err(|e| anyhow!(format!("Save state change set error: {:?}", e))) } + pub async fn get_state_change_sets( + &self, + tx_orders: Vec, + ) -> Result>> { + self.reader_actor + .send(GetStateChangeSetsMessage { tx_orders }) + .await? + } + pub async fn chain_id(&self) -> Result { self.get_states(AccessPath::object(ChainID::chain_id_object_id())) .await? diff --git a/crates/rooch-indexer/src/actor/indexer.rs b/crates/rooch-indexer/src/actor/indexer.rs index af015db52c..0445a02dc1 100644 --- a/crates/rooch-indexer/src/actor/indexer.rs +++ b/crates/rooch-indexer/src/actor/indexer.rs @@ -237,15 +237,12 @@ impl Handler for IndexerActor { async fn handle(&mut self, msg: IndexerRevertMessage, _ctx: &mut ActorContext) -> Result<()> { let IndexerRevertMessage { revert_tx_order, - // revert_ledger_tx, - // revert_execution_info, revert_state_change_set, root, object_mapping, } = msg; self.root = root; - // let tx_order = ledger_transaction.sequence_info.tx_order; // 1. revert indexer transaction self.indexer_store @@ -259,14 +256,6 @@ impl Handler for IndexerActor { let mut state_index_generator = IndexerObjectStatesIndexGenerator::default(); let mut indexer_object_state_change_set = IndexerObjectStateChangeSet::default(); - // // set genesis tx_order and state_index_generator for new indexer revert - // let tx_order: u64 = 0; - // let last_state_index = self - // .query_last_state_index_by_tx_order(tx_order, state_type.clone()) - // .await?; - // let mut state_index_generator = last_state_index.map_or(0, |x| x + 1); - - // let object_mapping = HashMap::::new(); for (_feild_key, object_change) in revert_state_change_set.state_change_set.changes { let _ = handle_revert_object_change( &mut state_index_generator, @@ -279,7 +268,6 @@ impl Handler for IndexerActor { self.indexer_store .apply_object_states(indexer_object_state_change_set)?; - // self.indexer_store.revert_states(object_state_change_set)?; Ok(()) } } diff --git a/crates/rooch-indexer/src/actor/messages.rs b/crates/rooch-indexer/src/actor/messages.rs index 677e18fdbe..7daecec8a9 100644 --- a/crates/rooch-indexer/src/actor/messages.rs +++ b/crates/rooch-indexer/src/actor/messages.rs @@ -153,8 +153,6 @@ impl Message for IndexerApplyObjectStatesMessage { #[derive(Debug, Serialize, Deserialize)] pub struct IndexerRevertMessage { pub revert_tx_order: u64, - // pub revert_ledger_tx: LedgerTransaction, - // pub revert_execution_info: TransactionExecutionInfo, pub revert_state_change_set: StateChangeSetExt, pub root: ObjectMeta, pub object_mapping: HashMap, diff --git a/crates/rooch-indexer/src/proxy/mod.rs b/crates/rooch-indexer/src/proxy/mod.rs index 084ffd032b..7bf760558a 100644 --- a/crates/rooch-indexer/src/proxy/mod.rs +++ b/crates/rooch-indexer/src/proxy/mod.rs @@ -214,8 +214,6 @@ impl IndexerProxy { pub async fn revert_indexer( &self, revert_tx_order: u64, - // revert_ledger_tx: LedgerTransaction, - // revert_execution_info: TransactionExecutionInfo, revert_state_change_set: StateChangeSetExt, root: ObjectMeta, object_mapping: HashMap, @@ -223,8 +221,6 @@ impl IndexerProxy { self.actor .notify(IndexerRevertMessage { revert_tx_order, - // revert_ledger_tx, - // revert_execution_info, revert_state_change_set, root, object_mapping, diff --git a/crates/rooch-indexer/src/tests/test_indexer.rs b/crates/rooch-indexer/src/tests/test_indexer.rs index bbcb3d92c7..95945db669 100644 --- a/crates/rooch-indexer/src/tests/test_indexer.rs +++ b/crates/rooch-indexer/src/tests/test_indexer.rs @@ -62,7 +62,7 @@ async fn test_transaction_store() -> Result<()> { let transactions = vec![indexer_transaction]; indexer_store.persist_transactions(transactions)?; - let filter = TransactionFilter::Sender(random_moveos_tx.ctx.sender.into()); + let filter = TransactionFilter::Sender(random_moveos_tx.ctx.sender); let query_transactions = indexer_reader.query_transactions_with_filter(filter, None, 1, true)?; assert_eq!(query_transactions.len(), 1); @@ -97,7 +97,7 @@ async fn test_event_store() -> Result<()> { let events = vec![indexer_event]; indexer_store.persist_events(events)?; - let filter = EventFilter::Sender(random_moveos_tx.ctx.sender.into()); + let filter = EventFilter::Sender(random_moveos_tx.ctx.sender); let query_events = indexer_reader.query_events_with_filter(filter, None, 1, true)?; assert_eq!(query_events.len(), 1); Ok(()) @@ -250,7 +250,7 @@ async fn test_escape_transaction() -> Result<()> { let transactions = vec![indexer_transaction]; indexer_store.persist_transactions(transactions)?; - let filter = TransactionFilter::Sender(random_moveos_tx.ctx.sender.into()); + let filter = TransactionFilter::Sender(random_moveos_tx.ctx.sender); let query_transactions = indexer_reader.query_transactions_with_filter(filter, None, 1, true)?; assert_eq!(query_transactions.len(), 1); diff --git a/crates/rooch-open-rpc-spec/schemas/openrpc.json b/crates/rooch-open-rpc-spec/schemas/openrpc.json index 640beb78b5..862ab58de1 100644 --- a/crates/rooch-open-rpc-spec/schemas/openrpc.json +++ b/crates/rooch-open-rpc-spec/schemas/openrpc.json @@ -746,6 +746,44 @@ "$ref": "#/components/schemas/primitive_types::H256" } } + }, + { + "name": "rooch_syncStates", + "description": "Sync state change sets", + "params": [ + { + "name": "filter", + "required": true, + "schema": { + "$ref": "#/components/schemas/SyncStateFilterView" + } + }, + { + "name": "cursor", + "schema": { + "$ref": "#/components/schemas/u64" + } + }, + { + "name": "limit", + "schema": { + "$ref": "#/components/schemas/u64" + } + }, + { + "name": "query_option", + "schema": { + "$ref": "#/components/schemas/QueryOptions" + } + } + ], + "result": { + "name": "StateChangeSetPageView", + "required": true, + "schema": { + "$ref": "#/components/schemas/PageView_for_StateChangeSetWithTxOrderView_and_u64" + } + } } ], "components": { @@ -2479,6 +2517,35 @@ } } }, + "PageView_for_StateChangeSetWithTxOrderView_and_u64": { + "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": { + "$ref": "#/components/schemas/StateChangeSetWithTxOrderView" + } + }, + "has_next_page": { + "type": "boolean" + }, + "next_cursor": { + "anyOf": [ + { + "$ref": "#/components/schemas/u64" + }, + { + "type": "null" + } + ] + } + } + }, "PageView_for_StateKVView_and_String": { "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", @@ -2712,6 +2779,21 @@ } } }, + "StateChangeSetWithTxOrderView": { + "type": "object", + "required": [ + "state_change_set", + "tx_order" + ], + "properties": { + "state_change_set": { + "$ref": "#/components/schemas/StateChangeSetView" + }, + "tx_order": { + "$ref": "#/components/schemas/u64" + } + } + }, "StateKVView": { "type": "object", "required": [ @@ -2742,6 +2824,30 @@ } } }, + "SyncStateFilterView": { + "oneOf": [ + { + "description": "Sync by object id.", + "type": "object", + "required": [ + "object_i_d" + ], + "properties": { + "object_i_d": { + "$ref": "#/components/schemas/ObjectID" + } + }, + "additionalProperties": false + }, + { + "description": "Sync all.", + "type": "string", + "enum": [ + "all" + ] + } + ] + }, "TransactionExecutionInfoView": { "type": "object", "required": [ diff --git a/crates/rooch-rpc-api/src/api/rooch_api.rs b/crates/rooch-rpc-api/src/api/rooch_api.rs index 46c47be864..d2c24ab904 100644 --- a/crates/rooch-rpc-api/src/api/rooch_api.rs +++ b/crates/rooch-rpc-api/src/api/rooch_api.rs @@ -11,8 +11,9 @@ use crate::jsonrpc_types::{ DryRunTransactionResponseView, EventOptions, EventPageView, ExecuteTransactionResponseView, FieldKeyView, FunctionCallView, H256View, IndexerEventPageView, IndexerObjectStatePageView, IndexerStateIDView, ModuleABIView, ObjectIDVecView, ObjectIDView, ObjectStateFilterView, - ObjectStateView, QueryOptions, RoochAddressView, StateOptions, StatePageView, StrView, - StructTagView, TransactionWithInfoPageView, TxOptions, + ObjectStateView, QueryOptions, RoochAddressView, StateChangeSetPageView, StateOptions, + StatePageView, StrView, StructTagView, SyncStateFilterView, TransactionWithInfoPageView, + TxOptions, }; use crate::RpcResult; use jsonrpsee::proc_macros::rpc; @@ -198,4 +199,15 @@ pub trait RoochAPI { repair_type: RepairIndexerTypeView, repair_params: RepairIndexerParamsView, ) -> RpcResult<()>; + + /// Sync state change sets + #[method(name = "syncStates")] + async fn sync_states( + &self, + filter: SyncStateFilterView, + // exclusive cursor if `Some`, otherwise start from the beginning + cursor: Option>, + limit: Option>, + query_option: Option, + ) -> RpcResult; } diff --git a/crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs b/crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs index 4ac106d4b7..e9303460c2 100644 --- a/crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs +++ b/crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use super::event_view::IndexerEventIDView; -use super::{HumanReadableDisplay, IndexerStateIDView}; +use super::{HumanReadableDisplay, IndexerStateIDView, StateChangeSetWithTxOrderView}; use crate::jsonrpc_types::account_view::BalanceInfoView; use crate::jsonrpc_types::btc::ord::InscriptionStateView; use crate::jsonrpc_types::btc::utxo::UTXOStateView; @@ -29,6 +29,7 @@ pub type IndexerObjectStatePageView = PageView; pub type InscriptionPageView = PageView; +pub type StateChangeSetPageView = PageView>; /// `next_cursor` points to the last item in the page; /// Reading with `next_cursor` will start from the next item after `next_cursor` if diff --git a/crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs b/crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs index e14ab36f5e..2faf1a3d96 100644 --- a/crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs +++ b/crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs @@ -3,7 +3,8 @@ use super::{ AnnotatedMoveStructView, BytesView, H256View, HumanReadableDisplay, ObjectIDVecView, - QueryOptions, RoochAddressView, StrView, StructTagView, TypeTagView, UnitedAddressView, + ObjectIDView, QueryOptions, RoochAddressView, StrView, StructTagView, TypeTagView, + UnitedAddressView, }; use anyhow::Result; use move_core_types::effects::Op; @@ -15,6 +16,7 @@ use moveos_types::{ state::{AnnotatedState, ObjectState, StateChangeSet}, }; use rooch_types::indexer::state::{IndexerStateID, ObjectStateFilter}; +use rooch_types::state::{StateChangeSetWithTxOrder, SyncStateFilter}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; @@ -517,3 +519,36 @@ fn parse_changed_objects( } (new_objs, modified_objs, deleted_objs) } + +#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum SyncStateFilterView { + /// Sync by object id. + ObjectID(ObjectIDView), + /// Sync all. + All, +} + +impl From for SyncStateFilter { + fn from(state_filter: SyncStateFilterView) -> Self { + match state_filter { + SyncStateFilterView::ObjectID(object_id) => SyncStateFilter::ObjectID(object_id.into()), + SyncStateFilterView::All => SyncStateFilter::All, + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +pub struct StateChangeSetWithTxOrderView { + pub tx_order: StrView, + pub state_change_set: StateChangeSetView, +} + +impl From for StateChangeSetWithTxOrderView { + fn from(state_change_set: StateChangeSetWithTxOrder) -> Self { + Self { + tx_order: state_change_set.tx_order.into(), + state_change_set: state_change_set.state_change_set.into(), + } + } +} diff --git a/crates/rooch-rpc-server/src/server/rooch_server.rs b/crates/rooch-rpc-server/src/server/rooch_server.rs index cfbf005772..db24f57402 100644 --- a/crates/rooch-rpc-server/src/server/rooch_server.rs +++ b/crates/rooch-rpc-server/src/server/rooch_server.rs @@ -24,8 +24,9 @@ use rooch_rpc_api::jsonrpc_types::{ EventPageView, ExecuteTransactionResponseView, FunctionCallView, H256View, IndexerEventPageView, IndexerObjectStatePageView, IndexerStateIDView, ModuleABIView, ObjectIDVecView, ObjectStateFilterView, ObjectStateView, QueryOptions, - RawTransactionOutputView, RoochAddressView, StateKVView, StateOptions, StatePageView, StrView, - StructTagView, TransactionWithInfoPageView, TxOptions, UnitedAddressView, + RawTransactionOutputView, RoochAddressView, StateChangeSetPageView, + StateChangeSetWithTxOrderView, StateKVView, StateOptions, StatePageView, StrView, + StructTagView, SyncStateFilterView, TransactionWithInfoPageView, TxOptions, UnitedAddressView, }; use rooch_rpc_api::{ api::rooch_api::RoochAPIServer, @@ -789,6 +790,63 @@ impl RoochAPIServer for RoochServer { .await?; Ok(()) } + + async fn sync_states( + &self, + filter: SyncStateFilterView, + // exclusive cursor if `Some`, otherwise start from the beginning + cursor: Option>, + limit: Option>, + query_option: Option, + ) -> RpcResult { + let limit_of = min( + limit.map(Into::into).unwrap_or(DEFAULT_RESULT_LIMIT_USIZE), + MAX_RESULT_LIMIT_USIZE, + ) as u64; + let cursor_of = cursor.map(|v| v.0); + // Sync from asc by default + let descending_order = query_option.map(|v| v.descending).unwrap_or(false); + + let last_sequencer_order = self.rpc_service.get_sequencer_order().await?; + let tx_orders = if descending_order { + let start = cursor_of.unwrap_or(last_sequencer_order + 1); + let end = if start >= limit_of { + start - limit_of + } else { + 0 + }; + + (end..start).rev().collect::>() + } else { + let start = cursor_of.unwrap_or(0); + let end_check = start + .checked_add(limit_of + 1) + .ok_or(RpcError::UnexpectedError( + "cursor value is overflow".to_string(), + ))?; + let end = min(end_check, last_sequencer_order + 1); + + (start..end).collect::>() + }; + + let mut data = self + .rpc_service + .sync_states(tx_orders, filter.into()) + .await? + .into_iter() + .map(StateChangeSetWithTxOrderView::from) + .collect::>(); + + let has_next_page = data.len() > limit_of as usize; + data.truncate(limit_of as usize); + let next_cursor = data.last().cloned().map_or(cursor, |t| Some(t.tx_order)); + + Ok(StateChangeSetPageView { + data, + next_cursor, + has_next_page, + }) + } } impl RoochRpcModule for RoochServer { diff --git a/crates/rooch-rpc-server/src/service/rpc_service.rs b/crates/rooch-rpc-server/src/service/rpc_service.rs index 272ce68090..57f3f9276e 100644 --- a/crates/rooch-rpc-server/src/service/rpc_service.rs +++ b/crates/rooch-rpc-server/src/service/rpc_service.rs @@ -12,7 +12,7 @@ use moveos_types::move_types::type_tag_match; use moveos_types::moveos_std::display::{get_object_display_id, RawDisplay}; use moveos_types::moveos_std::event::{AnnotatedEvent, Event, EventID}; use moveos_types::moveos_std::object::ObjectID; -use moveos_types::state::{AnnotatedState, FieldKey, ObjectState}; +use moveos_types::state::{AnnotatedState, FieldKey, ObjectState, StateChangeSet}; use moveos_types::state_resolver::{AnnotatedStateKV, StateKV}; use moveos_types::transaction::{FunctionCall, TransactionExecutionInfo}; use rooch_executor::actor::messages::DryRunTransactionResult; @@ -33,6 +33,7 @@ use rooch_types::indexer::state::{ }; use rooch_types::indexer::transaction::{IndexerTransaction, TransactionFilter}; use rooch_types::repair::{RepairIndexerParams, RepairIndexerType}; +use rooch_types::state::{StateChangeSetWithTxOrder, SyncStateFilter}; use rooch_types::transaction::{ ExecuteTransactionResponse, LedgerTransaction, RoochTransaction, RoochTransactionData, }; @@ -709,4 +710,46 @@ impl RpcService { Ok(()) } } + + pub async fn sync_states( + &self, + tx_orders: Vec, + filter: SyncStateFilter, + ) -> Result> { + let states = self + .executor + .get_state_change_sets(tx_orders.clone()) + .await? + .into_iter() + .zip(tx_orders) + .filter(|(x, _y)| x.is_some()) + .map(|(x, y)| StateChangeSetWithTxOrder::new(y, x.unwrap().state_change_set)) + .collect::>(); + + let result = match filter { + SyncStateFilter::ObjectID(object_id) => { + states + .into_iter() + .map(|s| { + let filter_changes = s + .state_change_set + .changes + .into_iter() + // Only includes Global Object, not include Child Object + .filter(|(_, value)| value.metadata.id == object_id) + .collect(); + let filter_state_change_set = StateChangeSet::new_with_changes( + s.state_change_set.state_root, + s.state_change_set.global_size, + filter_changes, + ); + StateChangeSetWithTxOrder::new(s.tx_order, filter_state_change_set) + }) + .collect() + } + SyncStateFilter::All => states, + }; + + Ok(result) + } } diff --git a/crates/rooch-types/src/indexer/state.rs b/crates/rooch-types/src/indexer/state.rs index 5992be4a6e..81595a12fb 100644 --- a/crates/rooch-types/src/indexer/state.rs +++ b/crates/rooch-types/src/indexer/state.rs @@ -293,7 +293,6 @@ pub fn collect_revert_object_change_ids( } Op::New(_value) => {} } - } else { } for (_key, change) in fields { @@ -367,13 +366,6 @@ impl Filter for ObjectStateFilter { } } -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum StateSyncFilter { - /// Query by object id. - ObjectId(ObjectID), -} - #[derive(Clone, Debug)] pub struct IndexerStateChangeSet { pub tx_order: u64, diff --git a/crates/rooch-types/src/lib.rs b/crates/rooch-types/src/lib.rs index 300f2f66a4..25d8ff93ec 100644 --- a/crates/rooch-types/src/lib.rs +++ b/crates/rooch-types/src/lib.rs @@ -23,6 +23,7 @@ pub mod rooch_network; pub mod rooch_signature; pub mod sequencer; pub mod service_status; +pub mod state; pub mod test_utils; pub mod to_bech32; pub mod transaction; diff --git a/crates/rooch-types/src/state.rs b/crates/rooch-types/src/state.rs new file mode 100644 index 0000000000..c087451081 --- /dev/null +++ b/crates/rooch-types/src/state.rs @@ -0,0 +1,39 @@ +// Copyright (c) RoochNetwork +// SPDX-License-Identifier: Apache-2.0 + +use moveos_types::moveos_std::object::ObjectID; +use moveos_types::state::StateChangeSet; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum SyncStateFilter { + /// Sync by object id. + ObjectID(ObjectID), + /// Sync all. + All, +} + +// impl SyncStateFilter { +// fn try_matches(&self, item: &StateChangeSet) -> Result { +// Ok(match self { +// SyncStateFilter::ObjectId(object_id) => object_id == &item.object_id, +// }) +// } +// } + +/// Global State change set ext. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] +pub struct StateChangeSetWithTxOrder { + pub tx_order: u64, + pub state_change_set: StateChangeSet, +} + +impl StateChangeSetWithTxOrder { + pub fn new(tx_order: u64, state_change_set: StateChangeSet) -> Self { + Self { + tx_order, + state_change_set, + } + } +} diff --git a/crates/rooch/src/commands/db/commands/rollback.rs b/crates/rooch/src/commands/db/commands/rollback.rs index 353b8c714a..9e1c873686 100644 --- a/crates/rooch/src/commands/db/commands/rollback.rs +++ b/crates/rooch/src/commands/db/commands/rollback.rs @@ -163,7 +163,6 @@ impl RollbackCommand { .rooch_store .get_state_change_set(previous_tx_order)?; if previous_state_change_set_ext_opt.is_some() && state_change_set_ext_opt.is_some() { - // let previoud_state_root = previous_state_change_set_ext_opt.unwrap().state_change_set.state_root; let previous_state_change_set_ext = previous_state_change_set_ext_opt.unwrap(); let state_change_set_ext = state_change_set_ext_opt.unwrap(); @@ -171,7 +170,7 @@ impl RollbackCommand { for (_feild_key, object_change) in state_change_set_ext.state_change_set.changes.clone() { - let _ = collect_revert_object_change_ids(object_change, &mut object_ids)?; + collect_revert_object_change_ids(object_change, &mut object_ids)?; } let root = ObjectMeta::root_metadata( @@ -185,8 +184,6 @@ impl RollbackCommand { .flatten() .map(|v| (v.metadata.id.clone(), v.metadata)) .collect::>(); - // let - // rooch_db.indexer_store. // 1. revert indexer transaction rooch_db @@ -205,16 +202,8 @@ impl RollbackCommand { let mut state_index_generator = IndexerObjectStatesIndexGenerator::default(); let mut indexer_object_state_change_set = IndexerObjectStateChangeSet::default(); - // // set genesis tx_order and state_index_generator for new indexer revert - // let tx_order: u64 = 0; - // let last_state_index = self - // .query_last_state_index_by_tx_order(tx_order, state_type.clone()) - // .await?; - // let mut state_index_generator = last_state_index.map_or(0, |x| x + 1); - - // let object_mapping = HashMap::::new(); for (_feild_key, object_change) in state_change_set_ext.state_change_set.changes { - let _ = handle_revert_object_change( + handle_revert_object_change( &mut state_index_generator, tx_order, &mut indexer_object_state_change_set, diff --git a/moveos/moveos-types/src/state.rs b/moveos/moveos-types/src/state.rs index 2e5e0eeab7..c5d5de1501 100644 --- a/moveos/moveos-types/src/state.rs +++ b/moveos/moveos-types/src/state.rs @@ -971,6 +971,18 @@ impl StateChangeSet { } } + pub fn new_with_changes( + state_root: H256, + global_size: u64, + changes: BTreeMap, + ) -> Self { + Self { + state_root, + global_size, + changes, + } + } + pub fn root_metadata(&self) -> ObjectMeta { ObjectMeta::root_metadata(self.state_root, self.global_size) }