Skip to content

Commit

Permalink
feat: add utxo_digest RPC call
Browse files Browse the repository at this point in the history
The utxo_digest rpc enables client to lookup a UTXO digest from a
leaf index.
  • Loading branch information
dan-da committed Apr 18, 2024
1 parent 587e9eb commit b8e7c35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ pub struct DashBoardOverviewDataFromClient {
pub confirmations: Option<BlockHeight>,
}

/// Provides summary information about a Block
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct BlockInfo {
height: BlockHeight,
digest: Digest,
timestamp: Timestamp,
difficulty: U32s<TARGET_DIFFICULTY_U32_SIZE>,
num_inputs: usize,
num_outputs: usize,
fee: NeptuneCoins,
pub height: BlockHeight,
pub digest: Digest,
pub timestamp: Timestamp,
pub difficulty: U32s<TARGET_DIFFICULTY_U32_SIZE>,
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)
Expand Down Expand Up @@ -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<Digest>;

/// Return the digest for the specified UTXO index
async fn utxo_digest(index: u64) -> Option<Digest>;

/// Return the block header for the specified block
async fn header(block_selector: BlockSelector) -> Option<BlockHeader>;

Expand Down Expand Up @@ -253,6 +258,16 @@ impl RPC for NeptuneRPCServer {
self.confirmations_internal().await
}

async fn utxo_digest(self, _: context::Context, index: u64) -> Option<Digest> {
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,
Expand Down
6 changes: 6 additions & 0 deletions src/util_types/mutator_set/archival_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b8e7c35

Please sign in to comment.