From c26c3f2be1213285706b2ba2ccbe5655353fb992 Mon Sep 17 00:00:00 2001 From: idky137 <150072198+idky137@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:15:27 +0000 Subject: [PATCH] add pub functionality for zaino (#8964) * add pub functionality for zaino * updated doc comment with review suggestion --- zebra-chain/src/parameters/network_upgrade.rs | 6 ++ zebra-rpc/src/methods.rs | 75 ++++++++++++++++++- zebra-rpc/src/methods/trees.rs | 41 +++++++++- 3 files changed, 116 insertions(+), 6 deletions(-) diff --git a/zebra-chain/src/parameters/network_upgrade.rs b/zebra-chain/src/parameters/network_upgrade.rs index 57165d0c7..b08cfec52 100644 --- a/zebra-chain/src/parameters/network_upgrade.rs +++ b/zebra-chain/src/parameters/network_upgrade.rs @@ -163,6 +163,12 @@ impl From for u32 { } } +impl From for ConsensusBranchId { + fn from(branch: u32) -> Self { + ConsensusBranchId(branch) + } +} + impl ToHex for &ConsensusBranchId { fn encode_hex>(&self) -> T { self.bytes_in_display_order().encode_hex() diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 8becc5bb7..736021c76 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -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. @@ -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, @@ -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, @@ -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. @@ -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 { diff --git a/zebra-rpc/src/methods/trees.rs b/zebra-rpc/src/methods/trees.rs index 79059688a..70838bb71 100644 --- a/zebra-rpc/src/methods/trees.rs +++ b/zebra-rpc/src/methods/trees.rs @@ -105,6 +105,19 @@ impl GetTreestate { orchard, } } + + /// Returns the contents of ['GetTreeState']. + pub fn into_parts(self) -> (Hash, Height, u32, Option>, Option>) { + ( + 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 { @@ -123,12 +136,24 @@ impl Default for GetTreestate { /// /// [1]: https://zcash.github.io/rpc/z_gettreestate.html #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] -struct Treestate> { +pub struct Treestate> { /// Contains an Orchard or Sapling serialized note commitment tree, /// hex-encoded. commitments: Commitments, } +impl> Treestate { + /// Returns a new instance of ['Treestate']. + pub fn new(commitments: Commitments) -> Self { + Treestate { commitments } + } + + /// Returns a reference to the commitments. + pub fn inner(&self) -> &Commitments { + &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 @@ -136,9 +161,21 @@ struct Treestate> { /// /// [1]: https://zcash.github.io/rpc/z_gettreestate.html #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] -struct Commitments> { +pub struct Commitments> { /// Orchard or Sapling serialized note commitment tree, hex-encoded. #[serde(with = "hex")] #[serde(rename = "finalState")] final_state: Tree, } + +impl> Commitments { + /// 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 + } +}