Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rpc] Get protocol config endpoint #11510

Merged
merged 8 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions crates/sui-indexer/src/apis/read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use jsonrpsee::RpcModule;
use sui_json_rpc::api::{ReadApiClient, ReadApiServer};
use sui_json_rpc::SuiRpcModule;
use sui_json_rpc_types::{
Checkpoint, CheckpointId, CheckpointPage, SuiEvent, SuiGetPastObjectRequest,
SuiObjectDataOptions, SuiObjectResponse, SuiPastObjectResponse, SuiTransactionBlockResponse,
SuiTransactionBlockResponseOptions,
Checkpoint, CheckpointId, CheckpointPage, ProtocolConfigResponse, SuiEvent,
SuiGetPastObjectRequest, SuiObjectDataOptions, SuiObjectResponse, SuiPastObjectResponse,
SuiTransactionBlockResponse, SuiTransactionBlockResponseOptions,
};
use sui_open_rpc::Module;
use sui_types::base_types::{ObjectID, SequenceNumber};
Expand Down Expand Up @@ -353,6 +353,20 @@ where
dynamic_fields_load_obj_guard.stop_and_record();
dyn_fields_resp
}

async fn get_protocol_config(
&self,
version: Option<BigInt<u64>>,
) -> RpcResult<ProtocolConfigResponse> {
let protocol_config_guard = self
.state
.indexer_metrics()
.get_protocol_config_latency
.start_timer();
let protocol_config_resp = self.fullnode.get_protocol_config(version).await;
protocol_config_guard.stop_and_record();
protocol_config_resp
}
}

impl<S> SuiRpcModule for ReadApi<S>
Expand Down
8 changes: 8 additions & 0 deletions crates/sui-indexer/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct IndexerMetrics {
pub query_events_latency: Histogram,
pub get_dynamic_fields_latency: Histogram,
pub get_dynamic_field_object_latency: Histogram,
pub get_protocol_config_latency: Histogram,
}

impl IndexerMetrics {
Expand Down Expand Up @@ -321,6 +322,13 @@ impl IndexerMetrics {
registry
)
.unwrap(),
get_protocol_config_latency: register_histogram_with_registry!(
"get_protocol_config_latency",
"Time spent in get_protocol_config_latency on the fullnode behind.",
LATENCY_SEC_BUCKETS.to_vec(),
registry
)
.unwrap(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-json-rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use sui_extended::*;
pub use sui_governance::*;
pub use sui_move::*;
pub use sui_object::*;
pub use sui_protocol::*;
pub use sui_transaction::*;
use sui_types::base_types::ObjectID;
use sui_types::dynamic_field::DynamicFieldInfo;
Expand All @@ -30,6 +31,7 @@ mod sui_extended;
mod sui_governance;
mod sui_move;
mod sui_object;
mod sui_protocol;
mod sui_transaction;

pub type DynamicFieldPage = Page<DynamicFieldInfo, ObjectID>;
Expand Down
74 changes: 74 additions & 0 deletions crates/sui-json-rpc-types/src/sui_protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use serde_with::DisplayFromStr;
use sui_protocol_config::{ProtocolConfig, ProtocolConfigValue, ProtocolVersion};
use sui_types::sui_serde::Readable;
use sui_types::sui_serde::{AsProtocolVersion, BigInt};

#[serde_as]
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase", rename = "ProtocolConfigValue")]
pub enum SuiProtocolConfigValue {
U32(
#[schemars(with = "BigInt<u32>")]
#[serde_as(as = "BigInt<u32>")]
u32,
),
U64(
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
u64,
),
F64(
#[schemars(with = "String")]
#[serde_as(as = "DisplayFromStr")]
f64,
),
}

impl From<ProtocolConfigValue> for SuiProtocolConfigValue {
fn from(value: ProtocolConfigValue) -> Self {
match value {
ProtocolConfigValue::u32(y) => SuiProtocolConfigValue::U32(y),
ProtocolConfigValue::u64(x) => SuiProtocolConfigValue::U64(x),
ProtocolConfigValue::f64(z) => SuiProtocolConfigValue::F64(z),
}
}
}

#[serde_as]
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase", rename = "ProtocolConfig")]
pub struct ProtocolConfigResponse {
#[schemars(with = "AsProtocolVersion")]
#[serde_as(as = "Readable<AsProtocolVersion, _>")]
pub min_supported_protocol_version: ProtocolVersion,
#[schemars(with = "AsProtocolVersion")]
#[serde_as(as = "Readable<AsProtocolVersion, _>")]
pub max_supported_protocol_version: ProtocolVersion,
#[schemars(with = "AsProtocolVersion")]
#[serde_as(as = "Readable<AsProtocolVersion, _>")]
pub protocol_version: ProtocolVersion,
pub attributes: BTreeMap<String, Option<SuiProtocolConfigValue>>,
}

impl From<ProtocolConfig> for ProtocolConfigResponse {
fn from(config: ProtocolConfig) -> Self {
ProtocolConfigResponse {
protocol_version: config.version,
attributes: config
.attr_map()
.into_iter()
.map(|(k, v)| (k, v.map(SuiProtocolConfigValue::from)))
.collect(),
min_supported_protocol_version: ProtocolVersion::MIN,
max_supported_protocol_version: ProtocolVersion::MAX,
}
}
}
12 changes: 11 additions & 1 deletion crates/sui-json-rpc/src/api/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee_proc_macros::rpc;

use sui_json_rpc_types::SuiLoadedChildObjectsResponse;
use sui_json_rpc_types::{
Checkpoint, CheckpointId, CheckpointPage, SuiEvent, SuiGetPastObjectRequest,
SuiObjectDataOptions, SuiObjectResponse, SuiPastObjectResponse, SuiTransactionBlockResponse,
SuiTransactionBlockResponseOptions,
};
use sui_json_rpc_types::{ProtocolConfigResponse, SuiLoadedChildObjectsResponse};
use sui_open_rpc_macros::open_rpc;
use sui_types::base_types::{ObjectID, SequenceNumber, TransactionDigest};
use sui_types::sui_serde::BigInt;
Expand Down Expand Up @@ -139,4 +139,14 @@ pub trait ReadApi {
/// Return the sequence number of the latest checkpoint that has been executed
#[method(name = "getLatestCheckpointSequenceNumber")]
async fn get_latest_checkpoint_sequence_number(&self) -> RpcResult<BigInt<u64>>;

/// Return the protocol config table for the given version number.
/// If the version number is not specified, return the latest protocol config table
/// supported by the node.
#[method(name = "getProtocolConfig")]
async fn get_protocol_config(
&self,
/// An optional protocol version specifier. If omitted, the latest protocol config table for the node will be returned.
version: Option<BigInt<u64>>,
) -> RpcResult<ProtocolConfigResponse>;
}
31 changes: 28 additions & 3 deletions crates/sui-json-rpc/src/read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use shared_crypto::intent::{AppId, Intent, IntentMessage, IntentScope, IntentVer
use sui_core::authority::AuthorityState;
use sui_json_rpc_types::{
BalanceChange, Checkpoint, CheckpointId, CheckpointPage, DisplayFieldsResponse, EventFilter,
ObjectChange, SuiEvent, SuiGetPastObjectRequest, SuiMoveStruct, SuiMoveValue,
SuiObjectDataOptions, SuiObjectResponse, SuiPastObjectResponse, SuiTransactionBlock,
SuiTransactionBlockEvents, SuiTransactionBlockResponse, SuiTransactionBlockResponseOptions,
ObjectChange, ProtocolConfigResponse, SuiEvent, SuiGetPastObjectRequest, SuiMoveStruct,
SuiMoveValue, SuiObjectDataOptions, SuiObjectResponse, SuiPastObjectResponse,
SuiTransactionBlock, SuiTransactionBlockEvents, SuiTransactionBlockResponse,
SuiTransactionBlockResponseOptions,
};
use sui_json_rpc_types::{SuiLoadedChildObject, SuiLoadedChildObjectsResponse};
use sui_open_rpc::Module;
use sui_protocol_config::{ProtocolConfig, ProtocolVersion};
use sui_types::base_types::{ObjectID, SequenceNumber, TransactionDigest};
use sui_types::collection_types::VecMap;
use sui_types::crypto::default_hash;
Expand Down Expand Up @@ -900,6 +902,29 @@ impl ReadApiServer for ReadApi {
})
})
}

#[instrument(skip(self))]
async fn get_protocol_config(
&self,
version: Option<BigInt<u64>>,
) -> RpcResult<ProtocolConfigResponse> {
with_tracing!("get_protocol_config", async move {
Ok(version
.map(|v| {
ProtocolConfig::get_for_version_if_supported((*v).into()).ok_or(anyhow!(
"Unsupported protocol version requested. Min supported: {}, max supported: {}",
ProtocolVersion::MIN.as_u64(),
ProtocolVersion::MAX.as_u64()
))
})
.unwrap_or(Ok(self
.state
.load_epoch_store_one_call_per_task()
.protocol_config()
.clone()))
.map(ProtocolConfigResponse::from)?)
})
}
}

impl SuiRpcModule for ReadApi {
Expand Down
101 changes: 101 additions & 0 deletions crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,31 @@
}
]
},
{
"name": "sui_getProtocolConfig",
"tags": [
{
"name": "Read API"
}
],
"description": "Return the protocol config table for the given version number. If the version number is not specified, return the latest protocol config table supported by the node.",
"params": [
{
"name": "version",
"description": "An optional protocol version specifier. If omitted, the latest protocol config table for the node will be returned.",
"schema": {
"$ref": "#/components/schemas/BigInt_for_uint64"
}
}
],
"result": {
"name": "ProtocolConfigResponse",
"required": true,
"schema": {
"$ref": "#/components/schemas/ProtocolConfig"
}
}
},
{
"name": "sui_getTotalTransactionBlocks",
"tags": [
Expand Down Expand Up @@ -3714,6 +3739,9 @@
"BigInt_for_uint128": {
"type": "string"
},
"BigInt_for_uint32": {
"type": "string"
},
"BigInt_for_uint64": {
"type": "string"
},
Expand Down Expand Up @@ -5989,6 +6017,79 @@
}
}
},
"ProtocolConfig": {
oxade marked this conversation as resolved.
Show resolved Hide resolved
"type": "object",
"required": [
"attributes",
"maxSupportedProtocolVersion",
"minSupportedProtocolVersion",
"protocolVersion"
],
"properties": {
"attributes": {
"type": "object",
"additionalProperties": {
"anyOf": [
{
"$ref": "#/components/schemas/ProtocolConfigValue"
},
{
"type": "null"
}
]
}
},
"maxSupportedProtocolVersion": {
"$ref": "#/components/schemas/ProtocolVersion"
},
"minSupportedProtocolVersion": {
"$ref": "#/components/schemas/ProtocolVersion"
},
"protocolVersion": {
"$ref": "#/components/schemas/ProtocolVersion"
}
}
},
"ProtocolConfigValue": {
"oneOf": [
{
"type": "object",
"required": [
"u32"
],
"properties": {
"u32": {
"$ref": "#/components/schemas/BigInt_for_uint32"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"u64"
],
"properties": {
"u64": {
"$ref": "#/components/schemas/BigInt_for_uint64"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"f64"
],
"properties": {
"f64": {
"type": "string"
}
},
"additionalProperties": false
}
]
},
"ProtocolVersion": {
"$ref": "#/components/schemas/BigInt_for_uint64"
},
Expand Down
Loading