Skip to content

Commit

Permalink
add pub functionality for zaino (#8964)
Browse files Browse the repository at this point in the history
* add pub functionality for zaino

* updated doc comment with review suggestion
  • Loading branch information
idky137 authored Nov 5, 2024
1 parent d7fbde3 commit c26c3f2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
6 changes: 6 additions & 0 deletions zebra-chain/src/parameters/network_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ impl From<ConsensusBranchId> for u32 {
}
}

impl From<u32> for ConsensusBranchId {
fn from(branch: u32) -> Self {
ConsensusBranchId(branch)
}
}

impl ToHex for &ConsensusBranchId {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
self.bytes_in_display_order().encode_hex()
Expand Down
75 changes: 71 additions & 4 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,11 +1510,23 @@ pub struct AddressBalance {

/// A hex-encoded [`ConsensusBranchId`] string.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
struct ConsensusBranchIdHex(#[serde(with = "hex")] ConsensusBranchId);
pub struct ConsensusBranchIdHex(#[serde(with = "hex")] ConsensusBranchId);

impl ConsensusBranchIdHex {
/// Returns a new instance of ['ConsensusBranchIdHex'].
pub fn new(consensus_branch_id: u32) -> Self {
ConsensusBranchIdHex(consensus_branch_id.into())
}

/// Returns the value of the ['ConsensusBranchId'].
pub fn inner(&self) -> u32 {
self.0.into()
}
}

/// Information about [`NetworkUpgrade`] activation.
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
struct NetworkUpgradeInfo {
pub struct NetworkUpgradeInfo {
/// Name of upgrade, string.
///
/// Ignored by lightwalletd, but useful for debugging.
Expand All @@ -1528,9 +1540,29 @@ struct NetworkUpgradeInfo {
status: NetworkUpgradeStatus,
}

impl NetworkUpgradeInfo {
/// Constructs [`NetworkUpgradeInfo`] from its constituent parts.
pub fn from_parts(
name: NetworkUpgrade,
activation_height: Height,
status: NetworkUpgradeStatus,
) -> Self {
Self {
name,
activation_height,
status,
}
}

/// Returns the contents of ['NetworkUpgradeInfo'].
pub fn into_parts(self) -> (NetworkUpgrade, Height, NetworkUpgradeStatus) {
(self.name, self.activation_height, self.status)
}
}

/// The activation status of a [`NetworkUpgrade`].
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
enum NetworkUpgradeStatus {
pub enum NetworkUpgradeStatus {
/// The network upgrade is currently active.
///
/// Includes all network upgrades that have previously activated,
Expand All @@ -1551,7 +1583,7 @@ enum NetworkUpgradeStatus {
///
/// These branch IDs are different when the next block is a network upgrade activation block.
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
struct TipConsensusBranch {
pub struct TipConsensusBranch {
/// Branch ID used to validate the current chain tip, big-endian, hex-encoded.
#[serde(rename = "chaintip")]
chain_tip: ConsensusBranchIdHex,
Expand All @@ -1561,6 +1593,21 @@ struct TipConsensusBranch {
next_block: ConsensusBranchIdHex,
}

impl TipConsensusBranch {
/// Constructs [`TipConsensusBranch`] from its constituent parts.
pub fn from_parts(chain_tip: u32, next_block: u32) -> Self {
Self {
chain_tip: ConsensusBranchIdHex::new(chain_tip),
next_block: ConsensusBranchIdHex::new(next_block),
}
}

/// Returns the contents of ['TipConsensusBranch'].
pub fn into_parts(self) -> (u32, u32) {
(self.chain_tip.inner(), self.next_block.inner())
}
}

/// Response to a `sendrawtransaction` RPC request.
///
/// Contains the hex-encoded hash of the sent transaction.
Expand Down Expand Up @@ -1793,6 +1840,26 @@ impl Default for GetBlockTrees {
}
}

impl GetBlockTrees {
/// Constructs a new instance of ['GetBlockTrees'].
pub fn new(sapling: u64, orchard: u64) -> Self {
GetBlockTrees {
sapling: SaplingTrees { size: sapling },
orchard: OrchardTrees { size: orchard },
}
}

/// Returns sapling data held by ['GetBlockTrees'].
pub fn sapling(self) -> u64 {
self.sapling.size
}

/// Returns orchard data held by ['GetBlockTrees'].
pub fn orchard(self) -> u64 {
self.orchard.size
}
}

/// Sapling note commitment tree information.
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct SaplingTrees {
Expand Down
41 changes: 39 additions & 2 deletions zebra-rpc/src/methods/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ impl GetTreestate {
orchard,
}
}

/// Returns the contents of ['GetTreeState'].
pub fn into_parts(self) -> (Hash, Height, u32, Option<Vec<u8>>, Option<Vec<u8>>) {
(
self.hash,
self.height,
self.time,
self.sapling
.map(|treestate| treestate.commitments.final_state),
self.orchard
.map(|treestate| treestate.commitments.final_state),
)
}
}

impl Default for GetTreestate {
Expand All @@ -123,22 +136,46 @@ impl Default for GetTreestate {
///
/// [1]: https://zcash.github.io/rpc/z_gettreestate.html
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
struct Treestate<Tree: AsRef<[u8]>> {
pub struct Treestate<Tree: AsRef<[u8]>> {
/// Contains an Orchard or Sapling serialized note commitment tree,
/// hex-encoded.
commitments: Commitments<Tree>,
}

impl<Tree: AsRef<[u8]>> Treestate<Tree> {
/// Returns a new instance of ['Treestate'].
pub fn new(commitments: Commitments<Tree>) -> Self {
Treestate { commitments }
}

/// Returns a reference to the commitments.
pub fn inner(&self) -> &Commitments<Tree> {
&self.commitments
}
}

/// A wrapper that contains either an Orchard or Sapling note commitment tree.
///
/// Note that in the original [`z_gettreestate`][1] RPC, [`Commitments`] also
/// contains the field `finalRoot`. Zebra does *not* use this field.
///
/// [1]: https://zcash.github.io/rpc/z_gettreestate.html
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
struct Commitments<Tree: AsRef<[u8]>> {
pub struct Commitments<Tree: AsRef<[u8]>> {
/// Orchard or Sapling serialized note commitment tree, hex-encoded.
#[serde(with = "hex")]
#[serde(rename = "finalState")]
final_state: Tree,
}

impl<Tree: AsRef<[u8]>> Commitments<Tree> {
/// Returns a new instance of ['Commitments'].
pub fn new(final_state: Tree) -> Self {
Commitments { final_state }
}

/// Returns a reference to the final_state.
pub fn inner(&self) -> &Tree {
&self.final_state
}
}

0 comments on commit c26c3f2

Please sign in to comment.