Skip to content

Commit e1e6ce5

Browse files
committed
upstream(core): Finish migration of unsafe iterator callsites
1 parent e05b3a5 commit e1e6ce5

File tree

14 files changed

+109
-122
lines changed

14 files changed

+109
-122
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/iota-core/src/jsonrpc_index.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,10 +1388,11 @@ impl IndexStore {
13881388
let starting_coin_type =
13891389
coin_type_tag.unwrap_or_else(|| String::from_utf8([0u8].to_vec()).unwrap());
13901390
Ok(coin_index
1391-
.iter_with_bounds(
1391+
.safe_iter_with_bounds(
13921392
Some((owner, starting_coin_type.clone(), ObjectID::ZERO)),
13931393
None,
13941394
)
1395+
.map(|result| result.expect("iterator db error"))
13951396
.take_while(move |((addr, coin_type, _), _)| {
13961397
if addr != &owner {
13971398
return false;
@@ -1415,10 +1416,11 @@ impl IndexStore {
14151416
Ok(self
14161417
.tables
14171418
.coin_index
1418-
.iter_with_bounds(
1419+
.safe_iter_with_bounds(
14191420
Some((owner, starting_coin_type.clone(), starting_object_id)),
14201421
None,
14211422
)
1423+
.map(|result| result.expect("iterator db error"))
14221424
.filter(move |((_, _, obj_id), _)| obj_id != &starting_object_id)
14231425
.enumerate()
14241426
.take_while(move |(index, ((addr, coin_type, _), _))| {
@@ -1449,7 +1451,8 @@ impl IndexStore {
14491451
.tables
14501452
.owner_index
14511453
// The object id 0 is the smallest possible
1452-
.iter_with_bounds(Some((owner, starting_object_id)), None)
1454+
.safe_iter_with_bounds(Some((owner, starting_object_id)), None)
1455+
.map(|result| result.expect("iterator db error"))
14531456
.skip(usize::from(starting_object_id != ObjectID::ZERO))
14541457
.take_while(move |((address_owner, _), _)| address_owner == &owner)
14551458
.filter(move |(_, o)| {

crates/iota-core/src/rest_index.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,25 +405,30 @@ impl IndexStoreTables {
405405
&self,
406406
owner: IotaAddress,
407407
cursor: Option<ObjectID>,
408-
) -> Result<impl Iterator<Item = (OwnerIndexKey, OwnerIndexInfo)> + '_, TypedStoreError> {
408+
) -> Result<
409+
impl Iterator<Item = Result<(OwnerIndexKey, OwnerIndexInfo), TypedStoreError>> + '_,
410+
TypedStoreError,
411+
> {
409412
let lower_bound = OwnerIndexKey::new(owner, cursor.unwrap_or(ObjectID::ZERO));
410413
let upper_bound = OwnerIndexKey::new(owner, ObjectID::MAX);
411414
Ok(self
412415
.owner
413-
.iter_with_bounds(Some(lower_bound), Some(upper_bound)))
416+
.safe_iter_with_bounds(Some(lower_bound), Some(upper_bound)))
414417
}
415418

416419
fn dynamic_field_iter(
417420
&self,
418421
parent: ObjectID,
419422
cursor: Option<ObjectID>,
420-
) -> Result<impl Iterator<Item = (DynamicFieldKey, DynamicFieldIndexInfo)> + '_, TypedStoreError>
421-
{
423+
) -> Result<
424+
impl Iterator<Item = Result<(DynamicFieldKey, DynamicFieldIndexInfo), TypedStoreError>> + '_,
425+
TypedStoreError,
426+
> {
422427
let lower_bound = DynamicFieldKey::new(parent, cursor.unwrap_or(ObjectID::ZERO));
423428
let upper_bound = DynamicFieldKey::new(parent, ObjectID::MAX);
424429
let iter = self
425430
.dynamic_field
426-
.iter_with_bounds(Some(lower_bound), Some(upper_bound));
431+
.safe_iter_with_bounds(Some(lower_bound), Some(upper_bound));
427432
Ok(iter)
428433
}
429434

@@ -550,16 +555,21 @@ impl RestIndexStore {
550555
&self,
551556
owner: IotaAddress,
552557
cursor: Option<ObjectID>,
553-
) -> Result<impl Iterator<Item = (OwnerIndexKey, OwnerIndexInfo)> + '_, TypedStoreError> {
558+
) -> Result<
559+
impl Iterator<Item = Result<(OwnerIndexKey, OwnerIndexInfo), TypedStoreError>> + '_,
560+
TypedStoreError,
561+
> {
554562
self.tables.owner_iter(owner, cursor)
555563
}
556564

557565
pub fn dynamic_field_iter(
558566
&self,
559567
parent: ObjectID,
560568
cursor: Option<ObjectID>,
561-
) -> Result<impl Iterator<Item = (DynamicFieldKey, DynamicFieldIndexInfo)> + '_, TypedStoreError>
562-
{
569+
) -> Result<
570+
impl Iterator<Item = Result<(DynamicFieldKey, DynamicFieldIndexInfo), TypedStoreError>> + '_,
571+
TypedStoreError,
572+
> {
563573
self.tables.dynamic_field_iter(parent, cursor)
564574
}
565575

crates/iota-core/src/storage.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use move_core_types::language_storage::StructTag;
2626
use parking_lot::Mutex;
2727
use tap::Pipe;
2828
use tracing::instrument;
29+
use typed_store::TypedStoreError;
2930

3031
use crate::{
3132
authority::AuthorityState,
@@ -550,17 +551,20 @@ impl RestIndexes for RestIndexStore {
550551
&self,
551552
owner: IotaAddress,
552553
cursor: Option<ObjectID>,
553-
) -> Result<Box<dyn Iterator<Item = AccountOwnedObjectInfo> + '_>> {
554-
let iter = self.owner_iter(owner, cursor)?.map(
555-
|(OwnerIndexKey { owner, object_id }, OwnerIndexInfo { version, type_ })| {
556-
AccountOwnedObjectInfo {
557-
owner,
558-
object_id,
559-
version,
560-
type_,
561-
}
562-
},
563-
);
554+
) -> Result<Box<dyn Iterator<Item = Result<AccountOwnedObjectInfo, TypedStoreError>> + '_>>
555+
{
556+
let iter = self.owner_iter(owner, cursor)?.map(|result| {
557+
result.map(
558+
|(OwnerIndexKey { owner, object_id }, OwnerIndexInfo { version, type_ })| {
559+
AccountOwnedObjectInfo {
560+
owner,
561+
object_id,
562+
version,
563+
type_,
564+
}
565+
},
566+
)
567+
});
564568

565569
Ok(Box::new(iter) as _)
566570
}
@@ -570,10 +574,12 @@ impl RestIndexes for RestIndexStore {
570574
parent: ObjectID,
571575
cursor: Option<ObjectID>,
572576
) -> iota_types::storage::error::Result<
573-
Box<dyn Iterator<Item = (DynamicFieldKey, DynamicFieldIndexInfo)> + '_>,
577+
Box<
578+
dyn Iterator<Item = Result<(DynamicFieldKey, DynamicFieldIndexInfo), TypedStoreError>>
579+
+ '_,
580+
>,
574581
> {
575582
let iter = self.dynamic_field_iter(parent, cursor)?;
576-
577583
Ok(Box::new(iter) as _)
578584
}
579585

crates/iota-json-rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ move-bytecode-utils.workspace = true
5959
move-core-types.workspace = true
6060
move-package.workspace = true
6161
shared-crypto.workspace = true
62+
typed-store.workspace = true
6263

6364
[dev-dependencies]
6465
# external dependencies

crates/iota-json-rpc/src/authority_state.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use mockall::automock;
4747
use move_core_types::language_storage::TypeTag;
4848
use thiserror::Error;
4949
use tokio::task::JoinError;
50+
use typed_store::TypedStoreError;
5051

5152
use crate::ObjectProvider;
5253

@@ -438,7 +439,7 @@ impl StateRead for AuthorityState {
438439
balance: coin.balance,
439440
previous_transaction: coin.previous_transaction,
440441
})
441-
.collect::<Vec<_>>())
442+
.collect())
442443
}
443444

444445
async fn get_executed_transaction_and_effects(
@@ -671,3 +672,10 @@ impl From<anyhow::Error> for StateReadError {
671672
StateReadError::Internal(e.into())
672673
}
673674
}
675+
676+
impl From<TypedStoreError> for StateReadError {
677+
fn from(e: TypedStoreError) -> Self {
678+
let error: IotaError = e.into();
679+
StateReadError::Internal(error.into())
680+
}
681+
}

crates/iota-rest-api/src/accounts.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,25 @@ async fn list_account_objects(
5959
let mut object_info = indexes
6060
.account_owned_objects_info_iter(address.into(), start)?
6161
.take(limit + 1)
62-
.map(|info| {
63-
AccountOwnedObjectInfo {
64-
owner: info.owner.into(),
65-
object_id: info.object_id.into(),
66-
version: info.version.into(),
67-
type_: struct_tag_core_to_sdk(info.type_.into())?,
68-
}
69-
.pipe(Ok)
62+
.map(|result| {
63+
result
64+
.map_err(|err| {
65+
RestError::new(
66+
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
67+
err.to_string(),
68+
)
69+
})
70+
.and_then(|info| {
71+
AccountOwnedObjectInfo {
72+
owner: info.owner.into(),
73+
object_id: info.object_id.into(),
74+
version: info.version.into(),
75+
type_: struct_tag_core_to_sdk(info.type_.into())?,
76+
}
77+
.pipe(Ok)
78+
})
7079
})
71-
.collect::<Result<Vec<_>>>()?;
80+
.collect::<Result<Vec<_>, _>>()?;
7281

7382
let cursor = if object_info.len() > limit {
7483
// SAFETY: We've already verified that object_info is greater than limit, which

crates/iota-rest-api/src/objects.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,16 @@ async fn list_dynamic_fields(
222222
let mut dynamic_fields = indexes
223223
.dynamic_field_iter(parent.into(), start)?
224224
.take(limit + 1)
225-
.map(DynamicFieldInfo::try_from)
225+
.map(|result| {
226+
result
227+
.map_err(|err| {
228+
RestError::new(
229+
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
230+
err.to_string(),
231+
)
232+
})
233+
.and_then(|x| DynamicFieldInfo::try_from(x)?.pipe(Ok))
234+
})
226235
.collect::<Result<Vec<_>, _>>()?;
227236

228237
let cursor = if dynamic_fields.len() > limit {

crates/iota-rest-api/src/transactions/resolve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ fn select_gas(
566566
.indexes()
567567
.ok_or_else(RestError::not_found)?
568568
.account_owned_objects_info_iter(owner, None)?
569+
.filter_map(|result| result.ok())
569570
.filter(|info| info.type_.is_gas_coin())
570571
.filter(|info| !input_objects.contains(&info.object_id))
571572
.filter_map(|info| {

crates/iota-types/src/storage/read_store.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::Arc;
66

77
use move_core_types::language_storage::{StructTag, TypeTag};
88
use serde::{Deserialize, Serialize};
9+
use typed_store_error::TypedStoreError;
910

1011
use super::{ObjectStore, error::Result};
1112
use crate::{
@@ -816,6 +817,8 @@ pub trait RestStateReader: ObjectStore + ReadStore + Send + Sync {
816817
fn indexes(&self) -> Option<&dyn RestIndexes>;
817818
}
818819

820+
pub type DynamicFieldIteratorItem =
821+
Result<(DynamicFieldKey, DynamicFieldIndexInfo), TypedStoreError>;
819822
pub trait RestIndexes: Send + Sync {
820823
fn get_transaction_checkpoint(
821824
&self,
@@ -826,13 +829,13 @@ pub trait RestIndexes: Send + Sync {
826829
&self,
827830
owner: IotaAddress,
828831
cursor: Option<ObjectID>,
829-
) -> Result<Box<dyn Iterator<Item = AccountOwnedObjectInfo> + '_>>;
832+
) -> Result<Box<dyn Iterator<Item = Result<AccountOwnedObjectInfo, TypedStoreError>> + '_>>;
830833

831834
fn dynamic_field_iter(
832835
&self,
833836
parent: ObjectID,
834837
cursor: Option<ObjectID>,
835-
) -> Result<Box<dyn Iterator<Item = (DynamicFieldKey, DynamicFieldIndexInfo)> + '_>>;
838+
) -> Result<Box<dyn Iterator<Item = DynamicFieldIteratorItem> + '_>>;
836839

837840
fn get_coin_info(&self, coin_type: &StructTag) -> Result<Option<CoinInfo>>;
838841
}

0 commit comments

Comments
 (0)