Skip to content

Commit

Permalink
chore(rpc-alt): assorted renames
Browse files Browse the repository at this point in the history
## Description
Applying a number of suggested renames as a follow-up from code review.

## Test plan
CI
  • Loading branch information
amnn committed Jan 28, 2025
1 parent a248d11 commit 0663beb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
19 changes: 10 additions & 9 deletions crates/sui-indexer-alt-jsonrpc/src/api/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sui_types::{

use crate::{
context::Context,
data::{object_versions::LatestObjectKey, objects::ObjectVersionKey},
data::{object_versions::LatestObjectVersionKey, objects::VersionedObjectKey},
error::{internal_error, rpc_bail, InternalContext, RpcError},
};

Expand Down Expand Up @@ -94,9 +94,10 @@ async fn rgp_response(ctx: &Context) -> Result<BigInt<u64>, RpcError> {
async fn latest_sui_system_state_response(
ctx: &Context,
) -> Result<SuiSystemStateSummary, RpcError> {
let wrapper: SuiSystemStateWrapper = fetch_latest(ctx, SUI_SYSTEM_STATE_OBJECT_ID)
.await
.internal_context("Failed to fetch system state wrapper object")?;
let wrapper: SuiSystemStateWrapper =
fetch_latest_for_system_state(ctx, SUI_SYSTEM_STATE_OBJECT_ID)
.await
.internal_context("Failed to fetch system state wrapper object")?;

let inner_id = derive_dynamic_field_id(
SUI_SYSTEM_STATE_OBJECT_ID,
Expand All @@ -106,12 +107,12 @@ async fn latest_sui_system_state_response(
.context("Failed to derive inner system state field ID")?;

Ok(match wrapper.version {
1 => fetch_latest::<Field<u64, SuiSystemStateInnerV1>>(ctx, inner_id)
1 => fetch_latest_for_system_state::<Field<u64, SuiSystemStateInnerV1>>(ctx, inner_id)
.await
.internal_context("Failed to fetch inner system state object")?
.value
.into_sui_system_state_summary(),
2 => fetch_latest::<Field<u64, SuiSystemStateInnerV2>>(ctx, inner_id)
2 => fetch_latest_for_system_state::<Field<u64, SuiSystemStateInnerV2>>(ctx, inner_id)
.await
.internal_context("Failed to fetch inner system state object")?
.value
Expand All @@ -127,20 +128,20 @@ async fn latest_sui_system_state_response(
/// API, but it does not generalize beyond that, because it assumes that the objects being loaded
/// are never deleted or wrapped and have always existed (because it loads using `LatestObjectKey`
/// directly without checking the live object set).
async fn fetch_latest<T: DeserializeOwned>(
async fn fetch_latest_for_system_state<T: DeserializeOwned>(
ctx: &Context,
object_id: ObjectID,
) -> Result<T, RpcError> {
let loader = ctx.loader();

let latest_version = loader
.load_one(LatestObjectKey(object_id))
.load_one(LatestObjectVersionKey(object_id))
.await
.context("Failed to load latest version")?
.ok_or_else(|| internal_error!("No latest version found"))?;

let stored = loader
.load_one(ObjectVersionKey(
.load_one(VersionedObjectKey(
object_id,
latest_version.object_version as u64,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tokio::join;
use crate::{
context::Context,
data::{
objects::ObjectVersionKey, transactions::TransactionKey,
objects::VersionedObjectKey, transactions::TransactionKey,
tx_balance_changes::TxBalanceChangeKey,
},
error::{internal_error, invalid_params, rpc_bail, RpcError},
Expand Down Expand Up @@ -217,10 +217,10 @@ async fn object_changes(
for change in &native_changes {
let id = change.id;
if let Some(version) = change.input_version {
keys.push(ObjectVersionKey(id, version.value()));
keys.push(VersionedObjectKey(id, version.value()));
}
if let Some(version) = change.output_version {
keys.push(ObjectVersionKey(id, version.value()));
keys.push(VersionedObjectKey(id, version.value()));
}
}

Expand All @@ -244,7 +244,7 @@ async fn object_changes(
let v = v.value();

let stored = objects
.get(&ObjectVersionKey(id, v))
.get(&VersionedObjectKey(id, v))
.ok_or_else(|| invalid_params(Error::PrunedObject(digest, id, v)))?;

let bytes = stored
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-indexer-alt-jsonrpc/src/data/object_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use super::reader::{ReadError, Reader};
/// Key for fetching the latest version of an object, not accounting for deletions or wraps. If the
/// object has been deleted or wrapped, the version before the delete/wrap is returned.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct LatestObjectKey(pub ObjectID);
pub(crate) struct LatestObjectVersionKey(pub ObjectID);

#[async_trait::async_trait]
impl Loader<LatestObjectKey> for Reader {
impl Loader<LatestObjectVersionKey> for Reader {
type Value = StoredObjVersion;
type Error = Arc<ReadError>;

async fn load(
&self,
keys: &[LatestObjectKey],
) -> Result<HashMap<LatestObjectKey, StoredObjVersion>, Self::Error> {
keys: &[LatestObjectVersionKey],
) -> Result<HashMap<LatestObjectVersionKey, StoredObjVersion>, Self::Error> {
use obj_versions::dsl as v;

if keys.is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions crates/sui-indexer-alt-jsonrpc/src/data/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ use sui_types::base_types::ObjectID;

use super::reader::{ReadError, Reader};

/// Key for fetching a particular version of an object.
/// Key for fetching the contents a particular version of an object.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct ObjectVersionKey(pub ObjectID, pub u64);
pub(crate) struct VersionedObjectKey(pub ObjectID, pub u64);

#[async_trait::async_trait]
impl Loader<ObjectVersionKey> for Reader {
impl Loader<VersionedObjectKey> for Reader {
type Value = StoredObject;
type Error = Arc<ReadError>;

async fn load(
&self,
keys: &[ObjectVersionKey],
) -> Result<HashMap<ObjectVersionKey, StoredObject>, Self::Error> {
keys: &[VersionedObjectKey],
) -> Result<HashMap<VersionedObjectKey, StoredObject>, Self::Error> {
use kv_objects::dsl as o;

if keys.is_empty() {
Expand All @@ -33,7 +33,7 @@ impl Loader<ObjectVersionKey> for Reader {

let mut query = o::kv_objects.into_boxed();

for ObjectVersionKey(id, version) in keys {
for VersionedObjectKey(id, version) in keys {
query = query.or_filter(
o::object_id
.eq(id.into_bytes())
Expand Down

0 comments on commit 0663beb

Please sign in to comment.