From 0479a3e06f9186e3c594a49d827166521d811ad5 Mon Sep 17 00:00:00 2001 From: vegetabledogdog Date: Fri, 27 Sep 2024 09:29:30 +0800 Subject: [PATCH 1/2] [rpc] add limit to object_ids --- .../src/service/rpc_service.rs | 36 +++++++++++-------- moveos/moveos-types/src/access_path.rs | 13 +++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/crates/rooch-rpc-server/src/service/rpc_service.rs b/crates/rooch-rpc-server/src/service/rpc_service.rs index 6824a4a24d..c2d3877709 100644 --- a/crates/rooch-rpc-server/src/service/rpc_service.rs +++ b/crates/rooch-rpc-server/src/service/rpc_service.rs @@ -122,6 +122,7 @@ impl RpcService { access_path: AccessPath, state_root: Option, ) -> Result>> { + access_path.validate_max_object_ids()?; self.executor.get_states(access_path, state_root).await } @@ -136,6 +137,7 @@ impl RpcService { &self, access_path: AccessPath, ) -> Result>> { + access_path.validate_max_object_ids()?; self.executor.get_annotated_states(access_path).await } @@ -146,6 +148,7 @@ impl RpcService { cursor: Option, limit: usize, ) -> Result> { + access_path.validate_max_object_ids()?; self.executor .list_states(state_root, access_path, cursor, limit) .await @@ -157,6 +160,7 @@ impl RpcService { cursor: Option, limit: usize, ) -> Result> { + access_path.validate_max_object_ids()?; self.executor .list_annotated_states(access_path, cursor, limit) .await @@ -588,18 +592,23 @@ impl RpcService { match repair_type { RepairIndexerType::ObjectState => match repair_params { RepairIndexerParams::ObjectId(object_ids) => { - self.repair_indexer_object_states( - object_ids.clone(), + let states = self + .get_states(AccessPath::objects(object_ids.clone()), None) + .await?; + for state_type in [ ObjectStateType::ObjectState, - ) - .await?; - self.repair_indexer_object_states( - object_ids.clone(), ObjectStateType::UTXO, - ) - .await?; - self.repair_indexer_object_states(object_ids, ObjectStateType::Inscription) - .await + ObjectStateType::Inscription, + ] { + self.repair_indexer_object_states( + states.clone(), + &object_ids, + state_type, + ) + .await?; + } + + Ok(()) } _ => Err(format_err!( "Invalid params when repair indexer for ObjectState" @@ -617,14 +626,11 @@ impl RpcService { pub async fn repair_indexer_object_states( &self, - object_ids: Vec, + states: Vec>, + object_ids: &[ObjectID], state_type: ObjectStateType, ) -> Result<()> { { - let states = self - .get_states(AccessPath::objects(object_ids.clone()), None) - .await?; - let mut remove_object_ids = vec![]; let mut object_states_mapping = HashMap::new(); for (idx, state_opt) in states.into_iter().enumerate() { diff --git a/moveos/moveos-types/src/access_path.rs b/moveos/moveos-types/src/access_path.rs index 76d0c37c19..11e4f5f19b 100644 --- a/moveos/moveos-types/src/access_path.rs +++ b/moveos/moveos-types/src/access_path.rs @@ -291,6 +291,19 @@ impl AccessPath { }) } + pub fn validate_max_object_ids(&self) -> Result<()> { + const MAX_OBJECT_IDS: usize = 100; + if let Path::Object { object_ids } = &self.0 { + if object_ids.len() > MAX_OBJECT_IDS { + return Err(anyhow::anyhow!( + "Too many object IDs requested. Maximum allowed: {}", + MAX_OBJECT_IDS + )); + } + } + Ok(()) + } + /// Convert AccessPath to StateQuery, return the ObjectID and field keys pub fn into_state_query(self) -> StateQuery { match self.0 { From e577d351045aafabca5989f1d0569856b4232790 Mon Sep 17 00:00:00 2001 From: vegetabledogdog Date: Fri, 27 Sep 2024 12:40:16 +0800 Subject: [PATCH 2/2] [rpc] refactor --- .../src/server/rooch_server.rs | 2 ++ .../src/service/rpc_service.rs | 18 +++++++++--------- moveos/moveos-types/src/access_path.rs | 7 +++---- moveos/moveos-types/src/moveos_std/object.rs | 2 ++ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/rooch-rpc-server/src/server/rooch_server.rs b/crates/rooch-rpc-server/src/server/rooch_server.rs index 1ea6bb9641..b1e5010f76 100644 --- a/crates/rooch-rpc-server/src/server/rooch_server.rs +++ b/crates/rooch-rpc-server/src/server/rooch_server.rs @@ -201,6 +201,7 @@ impl RoochAPIServer for RoochServer { access_path: AccessPathView, state_option: Option, ) -> RpcResult>> { + access_path.0.validate_max_object_ids()?; let state_option = state_option.unwrap_or_default(); let show_display = state_option.show_display && (access_path.0.is_object() || access_path.0.is_resource()); @@ -257,6 +258,7 @@ impl RoochAPIServer for RoochServer { limit: Option>, state_option: Option, ) -> RpcResult { + access_path.0.validate_max_object_ids()?; let state_option = state_option.unwrap_or_default(); let show_display = state_option.show_display && (access_path.0.is_object() || access_path.0.is_resource()); diff --git a/crates/rooch-rpc-server/src/service/rpc_service.rs b/crates/rooch-rpc-server/src/service/rpc_service.rs index c2d3877709..c3268e0bae 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::module_binding::MoveFunctionCaller; 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::moveos_std::object::{ObjectID, MAX_OBJECT_IDS_PER_QUERY}; use moveos_types::state::{AnnotatedState, FieldKey, ObjectState, StateChangeSet}; use moveos_types::state_resolver::{AnnotatedStateKV, StateKV}; use moveos_types::transaction::{FunctionCall, TransactionExecutionInfo}; @@ -122,7 +122,6 @@ impl RpcService { access_path: AccessPath, state_root: Option, ) -> Result>> { - access_path.validate_max_object_ids()?; self.executor.get_states(access_path, state_root).await } @@ -137,7 +136,6 @@ impl RpcService { &self, access_path: AccessPath, ) -> Result>> { - access_path.validate_max_object_ids()?; self.executor.get_annotated_states(access_path).await } @@ -148,7 +146,6 @@ impl RpcService { cursor: Option, limit: usize, ) -> Result> { - access_path.validate_max_object_ids()?; self.executor .list_states(state_root, access_path, cursor, limit) .await @@ -160,7 +157,6 @@ impl RpcService { cursor: Option, limit: usize, ) -> Result> { - access_path.validate_max_object_ids()?; self.executor .list_annotated_states(access_path, cursor, limit) .await @@ -362,16 +358,14 @@ impl RpcService { show_display: bool, state_type: ObjectStateType, ) -> Result> { - const MAX_OBJECT_IDS: usize = 100; - let indexer_ids = match filter { // Compatible with object_ids query after split object_states // Do not query the indexer, directly return the states query results. ObjectStateFilter::ObjectId(object_ids) => { - if object_ids.len() > MAX_OBJECT_IDS { + if object_ids.len() > MAX_OBJECT_IDS_PER_QUERY { return Err(anyhow::anyhow!( "Too many object IDs requested. Maximum allowed: {}", - MAX_OBJECT_IDS + MAX_OBJECT_IDS_PER_QUERY )); } object_ids @@ -592,6 +586,12 @@ impl RpcService { match repair_type { RepairIndexerType::ObjectState => match repair_params { RepairIndexerParams::ObjectId(object_ids) => { + if object_ids.len() > MAX_OBJECT_IDS_PER_QUERY { + return Err(anyhow::anyhow!( + "Too many object IDs requested. Maximum allowed: {}", + MAX_OBJECT_IDS_PER_QUERY + )); + } let states = self .get_states(AccessPath::objects(object_ids.clone()), None) .await?; diff --git a/moveos/moveos-types/src/access_path.rs b/moveos/moveos-types/src/access_path.rs index 11e4f5f19b..9bbeaa1a79 100644 --- a/moveos/moveos-types/src/access_path.rs +++ b/moveos/moveos-types/src/access_path.rs @@ -6,7 +6,7 @@ use crate::moveos_std::module_store::Package; use crate::state::FieldKey; use crate::{ move_types::{random_identity, random_struct_tag}, - moveos_std::object::ObjectID, + moveos_std::object::{ObjectID, MAX_OBJECT_IDS_PER_QUERY}, }; use anyhow::{ensure, Result}; use move_core_types::language_storage::ModuleId; @@ -292,12 +292,11 @@ impl AccessPath { } pub fn validate_max_object_ids(&self) -> Result<()> { - const MAX_OBJECT_IDS: usize = 100; if let Path::Object { object_ids } = &self.0 { - if object_ids.len() > MAX_OBJECT_IDS { + if object_ids.len() > MAX_OBJECT_IDS_PER_QUERY { return Err(anyhow::anyhow!( "Too many object IDs requested. Maximum allowed: {}", - MAX_OBJECT_IDS + MAX_OBJECT_IDS_PER_QUERY )); } } diff --git a/moveos/moveos-types/src/moveos_std/object.rs b/moveos/moveos-types/src/moveos_std/object.rs index f80cb8f31f..c0e82ee6b2 100644 --- a/moveos/moveos-types/src/moveos_std/object.rs +++ b/moveos/moveos-types/src/moveos_std/object.rs @@ -52,6 +52,8 @@ pub const SYSTEM_OWNER_ADDRESS: AccountAddress = AccountAddress::ZERO; pub const SHARED_OBJECT_FLAG_MASK: u8 = 1; pub const FROZEN_OBJECT_FLAG_MASK: u8 = 1 << 1; +pub const MAX_OBJECT_IDS_PER_QUERY: usize = 100; + // New table's state_root should be the place holder hash. pub static GENESIS_STATE_ROOT: Lazy = Lazy::new(|| *SPARSE_MERKLE_PLACEHOLDER_HASH);