add pub functionality for zaino (#8964)

* add pub functionality for zaino

* updated doc comment with review suggestion
This commit is contained in:
idky137 2024-11-05 19:15:27 +00:00 committed by GitHub
parent d7fbde3176
commit c26c3f2be1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 116 additions and 6 deletions

View File

@ -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()

View File

@ -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 {

View File

@ -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 {
@ -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<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
@ -136,9 +161,21 @@ struct Treestate<Tree: AsRef<[u8]>> {
///
/// [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
}
}