Skip to content

Commit

Permalink
Support new method EXPERIMENTAL_congestion_level (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobayurii authored Nov 15, 2024
1 parent 0d0c9e9 commit c1d13c6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Corrected state size calculation logic.
* Integrated cargo_pkg_version metric to reflect the current server version.
* Delete unnecessary debug logs about update blocks by finalities
* Support new method `EXPERIMENTAL_congestion_level`

### Supported Nearcore Version
- nearcore v2.3.1
Expand Down
6 changes: 6 additions & 0 deletions rpc-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ async fn rpc_handler(
})
.await
}
"EXPERIMENTAL_congestion_level" => {
process_method_call(request, |params| {
modules::blocks::methods::congestion_level(data, params)
})
.await
}
"EXPERIMENTAL_genesis_config" => {
process_method_call(request, |_: ()| {
modules::network::methods::genesis_config(data)
Expand Down
63 changes: 58 additions & 5 deletions rpc-server/src/modules/blocks/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn chunk(
near_jsonrpc::primitives::types::chunks::RpcChunkError,
> {
tracing::debug!("`chunk` called with parameters: {:?}", request_data);
let chunk_result = fetch_chunk(&data, request_data.chunk_reference.clone()).await;
let chunk_result = fetch_chunk(&data, request_data.chunk_reference.clone(), "chunk").await;
#[cfg(feature = "shadow-data-consistency")]
{
crate::utils::shadow_compare_results_handler(
Expand All @@ -67,6 +67,58 @@ pub async fn chunk(
chunk_result
}

// EXPERIMENTAL_congestion_level rpc method implementation
#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
pub async fn congestion_level(
data: Data<ServerContext>,
request_data: near_jsonrpc::primitives::types::congestion::RpcCongestionLevelRequest,
) -> Result<
near_jsonrpc::primitives::types::congestion::RpcCongestionLevelResponse,
near_jsonrpc::primitives::types::congestion::RpcCongestionLevelError,
> {
tracing::debug!(
"`EXPERIMENTAL_congestion_level` called with parameters: {:?}",
request_data
);
let chunk_view = fetch_chunk(
&data,
request_data.chunk_reference.clone(),
"EXPERIMENTAL_congestion_level",
)
.await?
.chunk_view;
let config_result = crate::modules::network::methods::protocol_config_call(
&data,
near_primitives::types::BlockReference::BlockId(near_primitives::types::BlockId::Height(
chunk_view.header.height_included,
)),
"EXPERIMENTAL_congestion_level",
)
.await;
let config = config_result.map_err(|err: near_jsonrpc::primitives::types::config::RpcProtocolConfigError| match err {
near_jsonrpc::primitives::types::config::RpcProtocolConfigError::UnknownBlock { error_message } => {
near_jsonrpc::primitives::types::congestion::RpcCongestionLevelError::UnknownBlock {
error_message,
}
}
near_jsonrpc::primitives::types::config::RpcProtocolConfigError::InternalError { error_message } => {
near_jsonrpc::primitives::types::congestion::RpcCongestionLevelError::InternalError {
error_message,
}
}
})?;
let congestion_level = chunk_view
.header
.congestion_info
.map(|info| info.congestion_level(config.runtime_config.congestion_control_config))
.unwrap_or(0.0);
Ok(
near_jsonrpc::primitives::types::congestion::RpcCongestionLevelResponse {
congestion_level,
},
)
}

/// `EXPERIMENTAL_changes` rpc method implementation
/// calls proxy_rpc_call to get `EXPERIMENTAL_changes` from near-rpc if request parameters not supported by read-rpc
/// as example: BlockReference for Finality::None is not supported by read-rpc
Expand Down Expand Up @@ -379,6 +431,7 @@ pub async fn fetch_block(
pub async fn fetch_chunk(
data: &Data<ServerContext>,
chunk_reference: near_jsonrpc::primitives::types::chunks::ChunkReference,
method_name: &str,
) -> Result<
near_jsonrpc::primitives::types::chunks::RpcChunkResponse,
near_jsonrpc::primitives::types::chunks::RpcChunkError,
Expand All @@ -400,7 +453,7 @@ pub async fn fetch_chunk(
}
near_primitives::types::BlockId::Hash(block_hash) => data
.db_manager
.get_block_height_by_hash(block_hash, "chunk")
.get_block_height_by_hash(block_hash, method_name)
.await
.map_err(|err| {
tracing::error!("Failed to fetch block by hash: {}", err);
Expand All @@ -412,7 +465,7 @@ pub async fn fetch_chunk(
// Check if the chunk stored in block with the given height
if let Ok(block_height_shard_id) = data
.db_manager
.get_block_by_height_and_shard_id(block_height, shard_id, "chunk")
.get_block_by_height_and_shard_id(block_height, shard_id, method_name)
.await
{
(block_height_shard_id.0, block_height_shard_id.1)
Expand All @@ -422,7 +475,7 @@ pub async fn fetch_chunk(
}
near_jsonrpc::primitives::types::chunks::ChunkReference::ChunkHash { chunk_id } => data
.db_manager
.get_block_by_chunk_hash(chunk_id, "chunk")
.get_block_by_chunk_hash(chunk_id, method_name)
.await
.map_err(
|_err| near_jsonrpc::primitives::types::chunks::RpcChunkError::UnknownChunk {
Expand All @@ -444,7 +497,7 @@ pub async fn fetch_chunk(
&near_primitives::types::BlockReference::BlockId(near_primitives::types::BlockId::Height(
block_height,
)),
"chunk",
method_name,
Some(block_height),
)
.await;
Expand Down
25 changes: 15 additions & 10 deletions rpc-server/src/modules/network/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ pub async fn protocol_config(
request_data
);

let config_view = protocol_config_call(&data, request_data.block_reference.clone()).await;
let config_view = protocol_config_call(
&data,
request_data.block_reference.clone(),
"EXPERIMENTAL_protocol_config",
)
.await;

#[cfg(feature = "shadow-data-consistency")]
{
Expand Down Expand Up @@ -366,21 +371,21 @@ async fn validators_call(
Ok(validators.validators_info)
}

async fn protocol_config_call(
pub async fn protocol_config_call(
data: &Data<ServerContext>,
block_reference: near_primitives::types::BlockReference,
method_name: &str,
) -> Result<
near_chain_configs::ProtocolConfigView,
near_jsonrpc::primitives::types::config::RpcProtocolConfigError,
> {
let protocol_version =
get_protocol_version(data, block_reference, "EXPERIMENTAL_protocol_config")
.await
.map_err(|err| {
near_jsonrpc::primitives::types::config::RpcProtocolConfigError::UnknownBlock {
error_message: err.to_string(),
}
})?;
let protocol_version = get_protocol_version(data, block_reference, method_name)
.await
.map_err(|err| {
near_jsonrpc::primitives::types::config::RpcProtocolConfigError::UnknownBlock {
error_message: err.to_string(),
}
})?;
// Stores runtime config for each protocol version
// Create store of runtime configs for the given chain id.
//
Expand Down

0 comments on commit c1d13c6

Please sign in to comment.