Skip to content

Commit

Permalink
refactor(rpc-server): Tweaks and clean up around query.call_function …
Browse files Browse the repository at this point in the history
…method implementation (#258)

* refactor(rpc-server): Tweaks and clean up around query.call_function method implementation

* Follow the clippy suggestion

* Drop redundant pieces of code
  • Loading branch information
khorolets authored May 23, 2024
1 parent e265ebb commit b8d8fcf
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 600 deletions.
21 changes: 9 additions & 12 deletions rpc-server/src/modules/blocks/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async fn block_call(
mut block_request: near_jsonrpc::primitives::types::blocks::RpcBlockRequest,
) -> Result<near_jsonrpc::primitives::types::blocks::RpcBlockResponse, RPCError> {
tracing::debug!("`block` called with parameters: {:?}", block_request);
let result = fetch_block(&data, block_request.block_reference.clone(), "block").await;
let result = fetch_block(&data, &block_request.block_reference, "block").await;

#[cfg(feature = "shadow_data_consistency")]
{
Expand Down Expand Up @@ -180,7 +180,7 @@ async fn changes_in_block_call(
{
let cache_block = fetch_block_from_cache_or_get(
&data,
params.block_reference.clone(),
&params.block_reference,
"EXPERIMENTAL_changes_in_block",
)
.await
Expand Down Expand Up @@ -213,13 +213,10 @@ async fn changes_in_block_by_type_call(
data: Data<ServerContext>,
mut params: near_jsonrpc::primitives::types::changes::RpcStateChangesInBlockByTypeRequest,
) -> Result<near_jsonrpc::primitives::types::changes::RpcStateChangesInBlockResponse, RPCError> {
let cache_block = fetch_block_from_cache_or_get(
&data,
params.block_reference.clone(),
"EXPERIMENTAL_changes",
)
.await
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?;
let cache_block =
fetch_block_from_cache_or_get(&data, &params.block_reference, "EXPERIMENTAL_changes")
.await
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?;
let result = fetch_changes_in_block_by_type(
&data,
cache_block,
Expand Down Expand Up @@ -251,7 +248,7 @@ async fn changes_in_block_by_type_call(
#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
pub async fn fetch_block(
data: &Data<ServerContext>,
block_reference: near_primitives::types::BlockReference,
block_reference: &near_primitives::types::BlockReference,
method_name: &str,
) -> Result<
near_jsonrpc::primitives::types::blocks::RpcBlockResponse,
Expand All @@ -260,11 +257,11 @@ pub async fn fetch_block(
tracing::debug!("`fetch_block` call");
let block_height = match block_reference {
near_primitives::types::BlockReference::BlockId(block_id) => match block_id {
near_primitives::types::BlockId::Height(block_height) => Ok(block_height),
near_primitives::types::BlockId::Height(block_height) => Ok(*block_height),
near_primitives::types::BlockId::Hash(block_hash) => {
match data
.db_manager
.get_block_by_hash(block_hash, method_name)
.get_block_by_hash(*block_hash, method_name)
.await
{
Ok(block_height) => Ok(block_height),
Expand Down
8 changes: 4 additions & 4 deletions rpc-server/src/modules/blocks/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ pub async fn fetch_chunk_from_s3(
#[cfg_attr(feature = "tracing-instrumentation", tracing::instrument(skip(data)))]
pub async fn fetch_block_from_cache_or_get(
data: &jsonrpc_v2::Data<ServerContext>,
block_reference: near_primitives::types::BlockReference,
block_reference: &near_primitives::types::BlockReference,
method_name: &str,
) -> Result<CacheBlock, near_jsonrpc::primitives::types::blocks::RpcBlockError> {
let block = match block_reference.clone() {
let block = match block_reference {
near_primitives::types::BlockReference::BlockId(block_id) => {
let block_height = match block_id {
near_primitives::types::BlockId::Height(block_height) => block_height,
near_primitives::types::BlockId::Height(block_height) => *block_height,
near_primitives::types::BlockId::Hash(hash) => data
.db_manager
.get_block_by_hash(hash, method_name)
.get_block_by_hash(*hash, method_name)
.await
.map_err(|err| {
near_jsonrpc::primitives::types::blocks::RpcBlockError::UnknownBlock {
Expand Down
2 changes: 1 addition & 1 deletion rpc-server/src/modules/gas/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn gas_price_call(
data: &Data<ServerContext>,
block_reference: near_primitives::types::BlockReference,
) -> Result<CacheBlock, near_jsonrpc::primitives::types::gas_price::RpcGasPriceError> {
let block = fetch_block_from_cache_or_get(data, block_reference, "gas_price")
let block = fetch_block_from_cache_or_get(data, &block_reference, "gas_price")
.await
.map_err(|err| {
near_jsonrpc::primitives::types::gas_price::RpcGasPriceError::UnknownBlock {
Expand Down
13 changes: 8 additions & 5 deletions rpc-server/src/modules/network/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ pub async fn validators_ordered(

if let Some(block_id) = &request.block_id {
let block_reference = near_primitives::types::BlockReference::from(block_id.clone());
if let Ok(block) =
fetch_block_from_cache_or_get(&data, block_reference, "EXPERIMENTAL_validators_ordered")
.await
if let Ok(block) = fetch_block_from_cache_or_get(
&data,
&block_reference,
"EXPERIMENTAL_validators_ordered",
)
.await
{
let final_block = data.blocks_info_by_finality.final_cache_block().await;
// `expected_earliest_available_block` calculated by formula:
Expand Down Expand Up @@ -237,7 +240,7 @@ async fn validators_call(
})?,
near_primitives::types::EpochReference::BlockId(block_id) => {
let block_reference = near_primitives::types::BlockReference::BlockId(block_id.clone());
let block = fetch_block_from_cache_or_get(data, block_reference, "validators")
let block = fetch_block_from_cache_or_get(data, &block_reference, "validators")
.await
.map_err(|_err| {
near_jsonrpc::primitives::types::validator::RpcValidatorError::UnknownEpoch
Expand All @@ -263,7 +266,7 @@ async fn protocol_config_call(
near_jsonrpc::primitives::types::config::RpcProtocolConfigError,
> {
let block =
fetch_block_from_cache_or_get(data, block_reference, "EXPERIMENTAL_protocol_config")
fetch_block_from_cache_or_get(data, &block_reference, "EXPERIMENTAL_protocol_config")
.await
.map_err(|err| {
near_jsonrpc::primitives::types::config::RpcProtocolConfigError::UnknownBlock {
Expand Down
Loading

0 comments on commit b8d8fcf

Please sign in to comment.