miden-node-rpc 0.15.0

Miden node's front-end RPC server
use miden_node_proto::decode::read_block_range;
use miden_node_proto::generated as proto;
use miden_node_utils::limiter::QueryParamNullifierPrefixLimit;
use miden_node_utils::tracing::OpenTelemetrySpanExt;
use tonic::Status;
use tracing::{Span, debug};

use super::{
    COMPONENT,
    RpcInvalidBlockRange,
    RpcService,
    check,
    database_error_to_status,
    invalid_block_range_to_status,
};

#[tonic::async_trait]
impl proto::server::rpc_api::SyncNullifiers for RpcService {
    type Input = proto::rpc::SyncNullifiersRequest;
    type Output = proto::rpc::SyncNullifiersResponse;

    fn decode(request: proto::rpc::SyncNullifiersRequest) -> tonic::Result<Self::Input> {
        Ok(request)
    }

    fn encode(output: Self::Output) -> tonic::Result<proto::rpc::SyncNullifiersResponse> {
        Ok(output)
    }

    async fn handle(&self, request: Self::Input) -> tonic::Result<Self::Output> {
        let range = read_block_range::<Status>(request.block_range, "SyncNullifiersRequest")?;

        let span = Span::current();
        span.set_attribute("block_range.from", range.block_from);
        span.set_attribute("block_range.to", range.block_to);

        debug!(target: COMPONENT, ?request);

        check::<QueryParamNullifierPrefixLimit>(request.nullifiers.len())?;

        if request.prefix_len != 16 {
            return Err(Status::invalid_argument(format!(
                "unsupported prefix length: {} (only 16-bit prefixes are supported)",
                request.prefix_len
            )));
        }
        let block_range = range
            .into_inclusive_range::<RpcInvalidBlockRange>()
            .map_err(invalid_block_range_to_status)?;
        let chain_tip = self.range_bounds_check(&block_range).await?;

        let (nullifiers, block_num) = self
            .store
            .sync_nullifiers(request.prefix_len, request.nullifiers, block_range)
            .await
            .map_err(|err| database_error_to_status(&err))?;
        let nullifiers = nullifiers
            .into_iter()
            .map(|nullifier_info| proto::rpc::sync_nullifiers_response::NullifierUpdate {
                nullifier: Some(nullifier_info.nullifier.into()),
                block_num: nullifier_info.block_num.as_u32(),
            })
            .collect();

        Ok(proto::rpc::SyncNullifiersResponse {
            pagination_info: Some(proto::rpc::PaginationInfo {
                chain_tip: chain_tip.as_u32(),
                block_num: block_num.as_u32(),
            }),
            nullifiers,
        })
    }
}