From bb7659a3adf525186ea2efa32efe04ec95584470 Mon Sep 17 00:00:00 2001 From: idky137 Date: Tue, 3 Dec 2024 15:28:42 +0000 Subject: [PATCH 1/9] added getters and setter for GetInfo and GetBlockchainInfo structs --- zebra-rpc/src/methods.rs | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 52f28d606b3..c5558563793 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1516,6 +1516,18 @@ impl Default for GetInfo { } } +impl GetInfo { + /// Constructs [`GetInfo`] from its constituent parts. + pub fn from_parts(build: String, subversion: String) -> Self { + Self { build, subversion } + } + + /// Returns the contents of ['GetInfo']. + pub fn into_parts(self) -> (String, String) { + (self.build, self.subversion) + } +} + /// Response to a `getblockchaininfo` RPC request. /// /// See the notes for the [`Rpc::get_blockchain_info` method]. @@ -1565,6 +1577,68 @@ impl Default for GetBlockChainInfo { } } +impl GetBlockChainInfo { + /// Creates a new [`GetBlockChainInfo`] instance. + pub fn new( + chain: String, + blocks: Height, + best_block_hash: block::Hash, + estimated_height: Height, + value_pools: [types::ValuePoolBalance; 5], + upgrades: IndexMap, + consensus: TipConsensusBranch, + ) -> Self { + Self { + chain, + blocks, + best_block_hash, + estimated_height, + value_pools, + upgrades, + consensus, + } + } + + /// Returns the current network name as defined in BIP70 (main, test, regtest). + pub fn chain(&self) -> String { + self.chain.clone() + } + + /// Returns the current number of blocks processed in the server. + pub fn blocks(&self) -> Height { + self.blocks + } + + /// Returns the hash of the currently best block, in big-endian order, hex-encoded. + pub fn best_block_hash(&self) -> &block::Hash { + &self.best_block_hash + } + + /// Returns the estimated height of the chain. + /// + /// If syncing, the estimated height of the chain, else the current best height, numeric. + /// + /// In Zebra, this is always the height estimate, so it might be a little inaccurate. + pub fn estimated_height(&self) -> Height { + self.estimated_height + } + + /// Returns the value pool balances. + pub fn value_pools(&self) -> &[types::ValuePoolBalance; 5] { + &self.value_pools + } + + /// Returns the network upgrades. + pub fn upgrades(&self) -> &IndexMap { + &self.upgrades + } + + /// Returns the Branch IDs of the current and upcoming consensus rules. + pub fn consensus(&self) -> &TipConsensusBranch { + &self.consensus + } +} + /// A wrapper type with a list of transparent address strings. /// /// This is used for the input parameter of [`Rpc::get_address_balance`], From 8f6581ef84f57969a5aff7d62b747f7b2e83fd37 Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 4 Dec 2024 14:48:22 +0000 Subject: [PATCH 2/9] updated BlockHeader --- zebra-rpc/src/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index c5558563793..29ac091e281 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1971,7 +1971,7 @@ pub struct GetBlockHeaderObject { /// The Equihash solution in the requested block header. #[serde(with = "hex")] - solution: Solution, + pub solution: Solution, /// The difficulty threshold of the requested block header displayed in compact form. #[serde(with = "hex")] From ec79c2832da151ccb6eef9c0fe4d94b33f280c37 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 30 Dec 2024 11:25:35 +0000 Subject: [PATCH 3/9] added fetcher for address strings --- zebra-rpc/src/methods.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 5ec32dd4ff8..117acfa5879 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1647,6 +1647,28 @@ impl AddressStrings { Ok(valid_addresses) } + + /// Given a list of addresses as strings: + /// - check if provided list have all valid transparent addresses. + /// - return valid addresses as a vec of strings. + pub fn valid_address_strings( + self, + ) -> std::result::Result, server::error::LegacyCode> { + // Reference for the legacy error code: + // + let valid_addresses = self + .addresses + .into_iter() + .map(|address| { + address + .parse::
() + .map(|_| address) + .map_err(|_| server::error::LegacyCode::InvalidAddressOrKey) + }) + .collect::, server::error::LegacyCode>>()?; + + Ok(valid_addresses) + } } /// The transparent balance of a set of addresses. From 3275e0cfcab7b56211e4a6f8b539cb9b3e75fca8 Mon Sep 17 00:00:00 2001 From: idky137 Date: Mon, 30 Dec 2024 11:29:58 +0000 Subject: [PATCH 4/9] publicised address balance --- zebra-rpc/src/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 117acfa5879..db5bfed1f84 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1675,7 +1675,7 @@ impl AddressStrings { #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, serde::Serialize)] pub struct AddressBalance { /// The total transparent balance. - balance: u64, + pub balance: u64, } /// A hex-encoded [`ConsensusBranchId`] string. From a81df30f02810c075d7eea45155a84be66f3d0ec Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 1 Jan 2025 15:35:30 +0000 Subject: [PATCH 5/9] added getter and setter for SentTransactionHash --- zebra-rpc/src/methods.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index db5bfed1f84..abf6e4fac9c 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1792,6 +1792,18 @@ impl Default for SentTransactionHash { } } +impl SentTransactionHash { + /// Constructs a new [`SentTransactionHash`]. + pub fn new(hash: transaction::Hash) -> Self { + SentTransactionHash(hash) + } + + /// Returns the contents of ['SentTransactionHash']. + pub fn inner(&self) -> transaction::Hash { + self.0 + } +} + /// Response to a `getblock` RPC request. /// /// See the notes for the [`RpcServer::get_block`] method. From f69f0c72631bddcece9af495d1cf543255af598b Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 1 Jan 2025 19:02:04 +0000 Subject: [PATCH 6/9] finished adding pub func --- zebra-rpc/src/methods.rs | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index abf6e4fac9c..8a1a4de648f 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -2179,6 +2179,48 @@ impl Default for GetAddressUtxos { } } +impl GetAddressUtxos { + /// Constructs a new instance of [`GetAddressUtxos`]. + pub fn from_parts( + address: transparent::Address, + txid: transaction::Hash, + output_index: OutputIndex, + script: transparent::Script, + satoshis: u64, + height: Height, + ) -> Self { + GetAddressUtxos { + address, + txid, + output_index, + script, + satoshis, + height, + } + } + + /// Returns the contents of [`GetAddressUtxos`]. + pub fn into_parts( + &self, + ) -> ( + transparent::Address, + transaction::Hash, + OutputIndex, + transparent::Script, + u64, + Height, + ) { + ( + self.address.clone(), + self.txid, + self.output_index, + self.script.clone(), + self.satoshis, + self.height, + ) + } +} + /// A struct to use as parameter of the `getaddresstxids`. /// /// See the notes for the [`Rpc::get_address_tx_ids` method]. @@ -2192,6 +2234,21 @@ pub struct GetAddressTxIdsRequest { end: u32, } +impl GetAddressTxIdsRequest { + /// Constructs [`GetAddressTxIdsRequest`] from its constituent parts. + pub fn from_parts(addresses: Vec, start: u32, end: u32) -> Self { + GetAddressTxIdsRequest { + addresses, + start, + end, + } + } + /// Returns the contents of [`GetAddressTxIdsRequest`]. + pub fn into_parts(&self) -> (Vec, u32, u32) { + (self.addresses.clone(), self.start, self.end) + } +} + /// Information about the sapling and orchard note commitment trees if any. #[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)] pub struct GetBlockTrees { From 5dc8aa50a56b2f304074ed7e0c2187b620398c5c Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 2 Jan 2025 15:02:20 +0000 Subject: [PATCH 7/9] added pub constructor for addressstrings --- zebra-rpc/src/methods.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 8a1a4de648f..0246ac3b9da 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1629,6 +1629,25 @@ impl AddressStrings { AddressStrings { addresses } } + /// Creates a new [`AddessStrings`] from a given vector, returns an error if any addresses are incorrect. + pub fn new_valid( + addresses: Vec, + ) -> std::result::Result { + let checked_addresses = addresses + .into_iter() + .map(|address| { + address + .parse::
() + .map(|_| address) + .map_err(|_| server::error::LegacyCode::InvalidAddressOrKey) + }) + .collect::, server::error::LegacyCode>>()?; + + Ok(AddressStrings { + addresses: checked_addresses, + }) + } + /// Given a list of addresses as strings: /// - check if provided list have all valid transparent addresses. /// - return valid addresses as a set of `Address`. From 8e8ebe2972459a40461a718fe277b45d5d2fe7b3 Mon Sep 17 00:00:00 2001 From: idky137 Date: Thu, 2 Jan 2025 15:08:29 +0000 Subject: [PATCH 8/9] added debug to legacycode --- zebra-rpc/src/server/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zebra-rpc/src/server/error.rs b/zebra-rpc/src/server/error.rs index 5130a16d533..835e3c4581c 100644 --- a/zebra-rpc/src/server/error.rs +++ b/zebra-rpc/src/server/error.rs @@ -8,7 +8,7 @@ use jsonrpsee_types::{ErrorCode, ErrorObject, ErrorObjectOwned}; /// ## Notes /// /// - All explicit discriminants fit within `i64`. -#[derive(Default)] +#[derive(Default, Debug)] pub enum LegacyCode { // General application defined errors /// `std::exception` thrown in command handling From a65f84700bb6654e849cf19cfa1423298f3c56a3 Mon Sep 17 00:00:00 2001 From: idky137 Date: Wed, 15 Jan 2025 13:09:40 +0000 Subject: [PATCH 9/9] review changes --- zebra-rpc/src/methods.rs | 42 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 0246ac3b9da..7b3611c72a8 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -1582,7 +1582,7 @@ impl GetBlockChainInfo { self.blocks } - /// Returns the hash of the currently best block, in big-endian order, hex-encoded. + /// Returns the hash of the current best chain tip block, in big-endian order, hex-encoded. pub fn best_block_hash(&self) -> &block::Hash { &self.best_block_hash } @@ -1630,22 +1630,10 @@ impl AddressStrings { } /// Creates a new [`AddessStrings`] from a given vector, returns an error if any addresses are incorrect. - pub fn new_valid( - addresses: Vec, - ) -> std::result::Result { - let checked_addresses = addresses - .into_iter() - .map(|address| { - address - .parse::
() - .map(|_| address) - .map_err(|_| server::error::LegacyCode::InvalidAddressOrKey) - }) - .collect::, server::error::LegacyCode>>()?; - - Ok(AddressStrings { - addresses: checked_addresses, - }) + pub fn new_valid(addresses: Vec) -> Result { + let address_strings = Self { addresses }; + address_strings.clone().valid_addresses()?; + Ok(address_strings) } /// Given a list of addresses as strings: @@ -1670,23 +1658,9 @@ impl AddressStrings { /// Given a list of addresses as strings: /// - check if provided list have all valid transparent addresses. /// - return valid addresses as a vec of strings. - pub fn valid_address_strings( - self, - ) -> std::result::Result, server::error::LegacyCode> { - // Reference for the legacy error code: - // - let valid_addresses = self - .addresses - .into_iter() - .map(|address| { - address - .parse::
() - .map(|_| address) - .map_err(|_| server::error::LegacyCode::InvalidAddressOrKey) - }) - .collect::, server::error::LegacyCode>>()?; - - Ok(valid_addresses) + pub fn valid_address_strings(self) -> Result> { + self.clone().valid_addresses()?; + Ok(self.addresses) } }