From b8e7c35bdd515f9f948f60f7018fb130d2af4fad Mon Sep 17 00:00:00 2001 From: danda Date: Wed, 17 Apr 2024 18:57:12 -0700 Subject: [PATCH] feat: add utxo_digest RPC call The utxo_digest rpc enables client to lookup a UTXO digest from a leaf index. --- src/rpc_server.rs | 29 ++++++++++++++++------ src/util_types/mutator_set/archival_mmr.rs | 6 +++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 53583829..c53a5e32 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -50,17 +50,19 @@ pub struct DashBoardOverviewDataFromClient { pub confirmations: Option, } +/// Provides summary information about a Block #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct BlockInfo { - height: BlockHeight, - digest: Digest, - timestamp: Timestamp, - difficulty: U32s, - num_inputs: usize, - num_outputs: usize, - fee: NeptuneCoins, + pub height: BlockHeight, + pub digest: Digest, + pub timestamp: Timestamp, + pub difficulty: U32s, + pub num_inputs: usize, + pub num_outputs: usize, + pub fee: NeptuneCoins, } +/// Provides alternatives for looking up a block. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub enum BlockSelector { Digest(Digest), // Identifies block by Digest (hash) @@ -125,6 +127,9 @@ pub trait RPC { /// Return the digest for the specified block selector (genesis, tip, or height) async fn block_digest(block_selector: BlockSelector) -> Option; + /// Return the digest for the specified UTXO index + async fn utxo_digest(index: u64) -> Option; + /// Return the block header for the specified block async fn header(block_selector: BlockSelector) -> Option; @@ -253,6 +258,16 @@ impl RPC for NeptuneRPCServer { self.confirmations_internal().await } + async fn utxo_digest(self, _: context::Context, index: u64) -> Option { + let state = self.state.lock_guard().await; + let aocl = &state.chain.archival_state().archival_mutator_set.ams().aocl; + + match index > 0 && index < aocl.len().await { + true => Some(aocl.get_leaf_async(index).await), + false => None, + } + } + async fn block_digest( self, _: context::Context, diff --git a/src/util_types/mutator_set/archival_mmr.rs b/src/util_types/mutator_set/archival_mmr.rs index f72299ce..e52a87e1 100644 --- a/src/util_types/mutator_set/archival_mmr.rs +++ b/src/util_types/mutator_set/archival_mmr.rs @@ -52,6 +52,12 @@ where self.digests.len().await == 1 } + /// Returns len of Digests. Note that elem 0 + /// is always a dummy digest. + pub async fn len(&self) -> u64 { + self.digests.len().await + } + /// Return the number of leaves in the tree pub async fn count_leaves(&self) -> u64 { node_index_to_leaf_index(self.digests.len().await).unwrap()