From 18ba6aa392d4507444c005fdc77a4c2d3328df26 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 28 Oct 2024 20:55:53 +0000 Subject: [PATCH] implemented get_block_nullifiers --- zaino-fetch/src/chain/block.rs | 52 +++++++++++++++++++++++++++++++++- zaino-serve/src/rpc/service.rs | 19 +++++++++---- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/zaino-fetch/src/chain/block.rs b/zaino-fetch/src/chain/block.rs index cae1196..4af2e14 100644 --- a/zaino-fetch/src/chain/block.rs +++ b/zaino-fetch/src/chain/block.rs @@ -13,7 +13,9 @@ use crate::{ }; use sha2::{Digest, Sha256}; use std::io::Cursor; -use zaino_proto::proto::compact_formats::{ChainMetadata, CompactBlock}; +use zaino_proto::proto::compact_formats::{ + ChainMetadata, CompactBlock, CompactOrchardAction, CompactTx, +}; /// A block header, containing metadata about a block. /// @@ -428,3 +430,51 @@ pub async fn get_block_from_node( Err(e) => Err(e.into()), } } + +/// Returns a compact block holding only action nullifiers. +/// +/// Retrieves a full block from zebrad/zcashd using 2 get_block calls. +/// This is because a get_block verbose = 1 call is require to fetch txids. +/// +/// TODO / NOTE: This should be rewritten when the BlockCache is added. +pub async fn get_nullifiers_from_node( + zebra_uri: &http::Uri, + height: &u32, +) -> Result { + match get_block_from_node(zebra_uri, height).await { + Ok(block) => Ok(CompactBlock { + proto_version: block.proto_version, + height: block.height, + hash: block.hash, + prev_hash: block.prev_hash, + time: block.time, + header: block.header, + vtx: block + .vtx + .into_iter() + .map(|tx| CompactTx { + index: tx.index, + hash: tx.hash, + fee: tx.fee, + spends: tx.spends, + outputs: Vec::new(), + actions: tx + .actions + .into_iter() + .map(|action| CompactOrchardAction { + nullifier: action.nullifier, + cmx: Vec::new(), + ephemeral_key: Vec::new(), + ciphertext: Vec::new(), + }) + .collect(), + }) + .collect(), + chain_metadata: Some(ChainMetadata { + sapling_commitment_tree_size: 0, + orchard_commitment_tree_size: 0, + }), + }), + Err(e) => Err(e.into()), + } +} diff --git a/zaino-serve/src/rpc/service.rs b/zaino-serve/src/rpc/service.rs index 3e8f58b..0d064b4 100644 --- a/zaino-serve/src/rpc/service.rs +++ b/zaino-serve/src/rpc/service.rs @@ -6,7 +6,10 @@ use tokio_stream::wrappers::ReceiverStream; use crate::{rpc::GrpcClient, utils::get_build_info}; use zaino_fetch::{ - chain::{block::get_block_from_node, mempool::Mempool}, + chain::{ + block::{get_block_from_node, get_nullifiers_from_node}, + mempool::Mempool, + }, jsonrpc::{connector::JsonRpcConnector, response::GetTransactionResponse}, }; use zaino_proto::proto::{ @@ -153,11 +156,10 @@ impl CompactTxStreamer for GrpcClient { /// Same as GetBlock except actions contain only nullifiers. /// - /// This RPC has not been implemented as it is not currently used by zingolib. - /// If you require this RPC please open an issue or PR at the Zingo-Indexer github (https://github.com/zingolabs/zingo-indexer). + /// NOTE: This should be reimplemented with the introduction of the BlockCache. fn get_block_nullifiers<'life0, 'async_trait>( &'life0 self, - _request: tonic::Request, + request: tonic::Request, ) -> core::pin::Pin< Box< dyn core::future::Future< @@ -172,7 +174,14 @@ impl CompactTxStreamer for GrpcClient { { println!("[TEST] Received call of get_block_nullifiers."); Box::pin(async { - Err(tonic::Status::unimplemented("get_block_nullifiers not yet implemented. If you require this RPC please open an issue or PR at the Zingo-Indexer github (https://github.com/zingolabs/zingo-indexer).")) + let zebrad_uri = self.zebrad_uri.clone(); + let height = request.into_inner().height as u32; + match get_nullifiers_from_node(&zebrad_uri, &height).await { + Ok(block) => Ok(tonic::Response::new(block)), + Err(e) => { + return Err(tonic::Status::internal(e.to_string())); + } + } }) }