Skip to content

Commit da6e940

Browse files
committed
graphql-alt: latest ProtocolConfigs
## Description Fetch the latest protocol config in scope if no protocol version is provided. ## Test plan Update E2E tests: ``` sui$ cargo nextest run \ -p sui-indexer-alt-e2e-tests \ -- graphql/epochs/protocol_configs ```
1 parent fa0498b commit da6e940

File tree

8 files changed

+59
-17
lines changed

8 files changed

+59
-17
lines changed

crates/sui-indexer-alt-e2e-tests/tests/graphql/epochs/protocol_configs.move

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
}
2828
}
2929

30+
//# run-graphql
31+
{ # Latest protocol config
32+
protocolConfigs { protocolVersion }
33+
}
34+
3035
//# run-graphql
3136
{ # Fetch protocol config version via epoch
3237
epoch(epochId: 0) { protocolConfigs { protocolVersion } }

crates/sui-indexer-alt-e2e-tests/tests/graphql/epochs/protocol_configs.snap

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: external-crates/move/crates/move-transactional-test-runner/src/framework.rs
33
---
4-
processed 5 tasks
4+
processed 6 tasks
55

66
init:
77
A: object(0,0)
@@ -42,7 +42,17 @@ Response: {
4242
}
4343
}
4444

45-
task 4, lines 30-36:
45+
task 4, lines 30-33:
46+
//# run-graphql
47+
Response: {
48+
"data": {
49+
"protocolConfigs": {
50+
"protocolVersion": 70
51+
}
52+
}
53+
}
54+
55+
task 5, lines 35-41:
4656
//# run-graphql
4757
Response: {
4858
"data": {

crates/sui-indexer-alt-graphql/schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ type Query {
449449
"""
450450
package(address: SuiAddress!, version: UInt53, atCheckpoint: UInt53): MovePackage
451451
"""
452-
Fetch the protocol config by protocol version.
452+
Fetch the protocol config by protocol version, or the latest protocol config used on chain if no version is provided.
453453
"""
454-
protocolConfigs(version: UInt53!): ProtocolConfigs
454+
protocolConfigs(version: UInt53): ProtocolConfigs
455455
"""
456456
Configuration for this RPC service.
457457
"""

crates/sui-indexer-alt-graphql/src/api/query.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,18 @@ impl Query {
227227
.await
228228
}
229229

230-
/// Fetch the protocol config by protocol version.
231-
async fn protocol_configs(&self, version: UInt53) -> Option<ProtocolConfigs> {
232-
Some(ProtocolConfigs::with_protocol_version(version.into()))
230+
/// Fetch the protocol config by protocol version, or the latest protocol config used on chain if no version is provided.
231+
async fn protocol_configs(
232+
&self,
233+
ctx: &Context<'_>,
234+
version: Option<UInt53>,
235+
) -> Result<Option<ProtocolConfigs>, RpcError> {
236+
if let Some(version) = version {
237+
Ok(Some(ProtocolConfigs::with_protocol_version(version.into())))
238+
} else {
239+
let scope = self.scope(ctx)?;
240+
ProtocolConfigs::latest(ctx, &scope).await
241+
}
233242
}
234243

235244
/// Configuration for this RPC service.

crates/sui-indexer-alt-graphql/src/api/types/protocol_configs.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::collections::BTreeMap;
4+
use std::{collections::BTreeMap, sync::Arc};
55

66
use anyhow::Context as _;
7-
use async_graphql::{Context, Object, SimpleObject};
7+
use async_graphql::{dataloader::DataLoader, Context, Object, SimpleObject};
88
use diesel::{ExpressionMethods, QueryDsl as _};
9-
use sui_indexer_alt_reader::pg_reader::PgReader;
9+
use sui_indexer_alt_reader::{epochs::CheckpointBoundedEpochStartKey, pg_reader::PgReader};
1010
use sui_indexer_alt_schema::{
1111
epochs::{StoredFeatureFlag, StoredProtocolConfig},
1212
schema::{kv_feature_flags, kv_protocol_configs},
1313
};
1414

15-
use crate::{api::scalars::uint53::UInt53, error::RpcError};
15+
use crate::{api::scalars::uint53::UInt53, error::RpcError, scope::Scope};
1616

1717
pub(crate) struct ProtocolConfigs {
1818
protocol_version: u64,
@@ -109,6 +109,24 @@ impl ProtocolConfigs {
109109
pub(crate) fn with_protocol_version(protocol_version: u64) -> Self {
110110
Self { protocol_version }
111111
}
112+
113+
/// Fetch the protocol config for the latest epoch in scope.
114+
pub(crate) async fn latest(ctx: &Context<'_>, scope: &Scope) -> Result<Option<Self>, RpcError> {
115+
let pg_loader: &Arc<DataLoader<PgReader>> = ctx.data()?;
116+
117+
let cp = scope.checkpoint_viewed_at();
118+
let Some(stored) = pg_loader
119+
.load_one(CheckpointBoundedEpochStartKey(cp))
120+
.await
121+
.context("Failed to fetch latest epoch")?
122+
else {
123+
return Ok(None);
124+
};
125+
126+
Ok(Some(Self {
127+
protocol_version: stored.protocol_version as u64,
128+
}))
129+
}
112130
}
113131

114132
impl ConfigContent {

crates/sui-indexer-alt-graphql/src/snapshots/sui_indexer_alt_graphql__tests__schema.graphql.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ type Query {
453453
"""
454454
package(address: SuiAddress!, version: UInt53, atCheckpoint: UInt53): MovePackage
455455
"""
456-
Fetch the protocol config by protocol version.
456+
Fetch the protocol config by protocol version, or the latest protocol config used on chain if no version is provided.
457457
"""
458-
protocolConfigs(version: UInt53!): ProtocolConfigs
458+
protocolConfigs(version: UInt53): ProtocolConfigs
459459
"""
460460
Configuration for this RPC service.
461461
"""

crates/sui-indexer-alt-graphql/src/snapshots/sui_indexer_alt_graphql__tests__staging.graphql.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ type Query {
453453
"""
454454
package(address: SuiAddress!, version: UInt53, atCheckpoint: UInt53): MovePackage
455455
"""
456-
Fetch the protocol config by protocol version.
456+
Fetch the protocol config by protocol version, or the latest protocol config used on chain if no version is provided.
457457
"""
458-
protocolConfigs(version: UInt53!): ProtocolConfigs
458+
protocolConfigs(version: UInt53): ProtocolConfigs
459459
"""
460460
Configuration for this RPC service.
461461
"""

crates/sui-indexer-alt-graphql/staging.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ type Query {
449449
"""
450450
package(address: SuiAddress!, version: UInt53, atCheckpoint: UInt53): MovePackage
451451
"""
452-
Fetch the protocol config by protocol version.
452+
Fetch the protocol config by protocol version, or the latest protocol config used on chain if no version is provided.
453453
"""
454-
protocolConfigs(version: UInt53!): ProtocolConfigs
454+
protocolConfigs(version: UInt53): ProtocolConfigs
455455
"""
456456
Configuration for this RPC service.
457457
"""

0 commit comments

Comments
 (0)