From d3011186f7178a19322798bdbeec00a6ab04fc7b Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Sat, 22 Oct 2022 13:45:20 -0300 Subject: [PATCH 1/4] introduce getblocktemplate rpc call --- zebra-rpc/src/methods/get_block_template.rs | 109 ++++++++++++++++++ zebra-rpc/src/methods/tests/snapshot.rs | 16 +++ .../get_block_template@mainnet_10.snap | 31 +++++ .../get_block_template@testnet_10.snap | 31 +++++ zebra-rpc/src/methods/tests/vectors.rs | 65 +++++++++++ 5 files changed, 252 insertions(+) create mode 100644 zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap create mode 100644 zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap diff --git a/zebra-rpc/src/methods/get_block_template.rs b/zebra-rpc/src/methods/get_block_template.rs index de7d3f459c4..23583f663cb 100644 --- a/zebra-rpc/src/methods/get_block_template.rs +++ b/zebra-rpc/src/methods/get_block_template.rs @@ -35,8 +35,19 @@ pub trait GetBlockTemplateRpc { /// /// - If `index` is positive then index = block height. /// - If `index` is negative then -1 is the last known valid block. + /// - This rpc method is available only if zebra is built with `--features getblocktemplate-rpcs`. #[rpc(name = "getblockhash")] fn get_block_hash(&self, index: i32) -> BoxFuture>; + + /// Documentation to be filled as we go. + /// + /// zcashd reference: [`getblocktemplate`](https://zcash-rpc.github.io/getblocktemplate.html) + /// + /// # Notes + /// + /// - This rpc method is available only if zebra is built with `--features getblocktemplate-rpcs`. + #[rpc(name = "getblocktemplate")] + fn get_block_template(&self) -> BoxFuture>; } /// RPC method implementations. @@ -139,8 +150,106 @@ where } .boxed() } + + fn get_block_template(&self) -> BoxFuture> { + async move { + let empty_string = String::from(""); + + // Returns empty `GetBlockTemplate` + Ok(GetBlockTemplate { + capabilities: vec![], + version: 0, + previousblockhash: empty_string.clone(), + blockcommitmentshash: empty_string.clone(), + lightclientroothash: empty_string.clone(), + finalsaplingroothash: empty_string.clone(), + defaultroots: DefaultRoots { + merkleroot: empty_string.clone(), + chainhistoryroot: empty_string.clone(), + authdataroot: empty_string.clone(), + blockcommitmentshash: empty_string.clone(), + }, + transactions: vec![], + coinbasetxn: Coinbase {}, + longpollid: empty_string.clone(), + target: empty_string.clone(), + mintime: 0, + mutable: vec![], + noncerange: empty_string.clone(), + sigoplimit: 0, + sizelimit: 0, + curtime: 0, + bits: empty_string, + height: 0, + }) + } + .boxed() + } +} +/// Documentation to be added after we document all the individual fields. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct GetBlockTemplate { + /// Add documentation. + pub capabilities: Vec, + /// Add documentation. + pub version: usize, + /// Add documentation. + pub previousblockhash: String, + /// Add documentation. + pub blockcommitmentshash: String, + /// Add documentation. + pub lightclientroothash: String, + /// Add documentation. + pub finalsaplingroothash: String, + /// Add documentation. + pub defaultroots: DefaultRoots, + /// Add documentation. + pub transactions: Vec, + /// Add documentation. + pub coinbasetxn: Coinbase, + /// Add documentation. + pub longpollid: String, + /// Add documentation. + pub target: String, + /// Add documentation. + pub mintime: u32, + /// Add documentation. + pub mutable: Vec, + /// Add documentation. + pub noncerange: String, + /// Add documentation. + pub sigoplimit: u32, + /// Add documentation. + pub sizelimit: u32, + /// Add documentation. + pub curtime: u32, + /// Add documentation. + pub bits: String, + /// Add documentation. + pub height: u32, } +/// Documentation to be added in #5452 or #5455. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct DefaultRoots { + /// Add documentation. + pub merkleroot: String, + /// Add documentation. + pub chainhistoryroot: String, + /// Add documentation. + pub authdataroot: String, + /// Add documentation. + pub blockcommitmentshash: String, +} + +/// Documentation and fields to be added in #5454. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Transaction {} + +/// documentation and fields to be added in #5453. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Coinbase {} + /// Given a potentially negative index, find the corresponding `Height`. /// /// This function is used to parse the integer index argument of `get_block_hash`. diff --git a/zebra-rpc/src/methods/tests/snapshot.rs b/zebra-rpc/src/methods/tests/snapshot.rs index 044910e6fa4..71d7f63abe2 100644 --- a/zebra-rpc/src/methods/tests/snapshot.rs +++ b/zebra-rpc/src/methods/tests/snapshot.rs @@ -191,6 +191,13 @@ async fn test_rpc_response_data_for_network(network: Network) { .expect("We should have a GetBlockHash struct"); snapshot_rpc_getblockhash(get_block_hash, &settings); + + // `getblocktemplate` + let get_block_template = get_block_template_rpc + .get_block_template() + .await + .expect("We should have a GetBlockTemplate struct"); + snapshot_rpc_getblocktemplate(get_block_template, &settings); } } @@ -292,6 +299,15 @@ fn snapshot_rpc_getblockhash(block_hash: GetBlockHash, settings: &insta::Setting settings.bind(|| insta::assert_json_snapshot!("get_block_hash", block_hash)); } +#[cfg(feature = "getblocktemplate-rpcs")] +/// Snapshot `getblocktemplate` response, using `cargo insta` and JSON serialization. +fn snapshot_rpc_getblocktemplate( + block_template: crate::methods::get_block_template::GetBlockTemplate, + settings: &insta::Settings, +) { + settings.bind(|| insta::assert_json_snapshot!("get_block_template", block_template)); +} + /// Utility function to convert a `Network` to a lowercase string. fn network_string(network: Network) -> String { let mut net_suffix = network.to_string(); diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap new file mode 100644 index 00000000000..58c10f0d9e4 --- /dev/null +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap @@ -0,0 +1,31 @@ +--- +source: zebra-rpc/src/methods/tests/snapshot.rs +assertion_line: 308 +expression: block_template +--- +{ + "capabilities": [], + "version": 0, + "previousblockhash": "", + "blockcommitmentshash": "", + "lightclientroothash": "", + "finalsaplingroothash": "", + "defaultroots": { + "merkleroot": "", + "chainhistoryroot": "", + "authdataroot": "", + "blockcommitmentshash": "" + }, + "transactions": [], + "coinbasetxn": {}, + "longpollid": "", + "target": "", + "mintime": 0, + "mutable": [], + "noncerange": "", + "sigoplimit": 0, + "sizelimit": 0, + "curtime": 0, + "bits": "", + "height": 0 +} diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap new file mode 100644 index 00000000000..58c10f0d9e4 --- /dev/null +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap @@ -0,0 +1,31 @@ +--- +source: zebra-rpc/src/methods/tests/snapshot.rs +assertion_line: 308 +expression: block_template +--- +{ + "capabilities": [], + "version": 0, + "previousblockhash": "", + "blockcommitmentshash": "", + "lightclientroothash": "", + "finalsaplingroothash": "", + "defaultroots": { + "merkleroot": "", + "chainhistoryroot": "", + "authdataroot": "", + "blockcommitmentshash": "" + }, + "transactions": [], + "coinbasetxn": {}, + "longpollid": "", + "target": "", + "mintime": 0, + "mutable": [], + "noncerange": "", + "sigoplimit": 0, + "sizelimit": 0, + "curtime": 0, + "bits": "", + "height": 0 +} diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index 514646b4016..ddecbf3643f 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -757,3 +757,68 @@ async fn rpc_getblockhash() { mempool.expect_no_requests().await; } + +#[cfg(feature = "getblocktemplate-rpcs")] +#[tokio::test(flavor = "multi_thread")] +async fn rpc_getblocktemplate() { + let _init_guard = zebra_test::init(); + + // Create a continuous chain of mainnet blocks from genesis + let blocks: Vec> = zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS + .iter() + .map(|(_height, block_bytes)| block_bytes.zcash_deserialize_into().unwrap()) + .collect(); + + let mut mempool: MockService<_, _, _, BoxError> = MockService::build().for_unit_tests(); + // Create a populated state service + let (_state, read_state, latest_chain_tip, _chain_tip_change) = + zebra_state::populated_state(blocks.clone(), Mainnet).await; + + // Init RPCs + let _rpc = RpcImpl::new( + "RPC test", + Mainnet, + false, + Buffer::new(mempool.clone(), 1), + Buffer::new(read_state.clone(), 1), + latest_chain_tip.clone(), + ); + let get_block_template_rpc = + get_block_template::GetBlockTemplateRpcImpl::new(latest_chain_tip, read_state); + + let get_block_template = get_block_template_rpc + .get_block_template() + .await + .expect("We should have a GetBlockTemplate struct"); + + assert!(get_block_template.capabilities.is_empty()); + assert_eq!(get_block_template.version, 0); + assert!(get_block_template.previousblockhash.is_empty()); + assert!(get_block_template.blockcommitmentshash.is_empty()); + assert!(get_block_template.lightclientroothash.is_empty()); + assert!(get_block_template.finalsaplingroothash.is_empty()); + assert!(get_block_template.defaultroots.merkleroot.is_empty()); + assert!(get_block_template.defaultroots.chainhistoryroot.is_empty()); + assert!(get_block_template.defaultroots.authdataroot.is_empty()); + assert!(get_block_template + .defaultroots + .blockcommitmentshash + .is_empty()); + assert!(get_block_template.transactions.is_empty()); + assert_eq!( + get_block_template.coinbasetxn, + get_block_template::Coinbase {} + ); + assert!(get_block_template.longpollid.is_empty()); + assert!(get_block_template.target.is_empty()); + assert_eq!(get_block_template.mintime, 0); + assert!(get_block_template.mutable.is_empty()); + assert!(get_block_template.noncerange.is_empty()); + assert_eq!(get_block_template.sigoplimit, 0); + assert_eq!(get_block_template.sizelimit, 0); + assert_eq!(get_block_template.curtime, 0); + assert!(get_block_template.bits.is_empty()); + assert_eq!(get_block_template.height, 0); + + mempool.expect_no_requests().await; +} From 5756a77f89d8597ce146da071878d978c4706711 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 25 Oct 2022 09:40:35 -0300 Subject: [PATCH 2/4] remove optional `longpollid` field --- zebra-rpc/src/methods/get_block_template.rs | 3 --- .../methods/tests/snapshots/get_block_template@mainnet_10.snap | 1 - .../methods/tests/snapshots/get_block_template@testnet_10.snap | 1 - zebra-rpc/src/methods/tests/vectors.rs | 1 - 4 files changed, 6 deletions(-) diff --git a/zebra-rpc/src/methods/get_block_template.rs b/zebra-rpc/src/methods/get_block_template.rs index 23583f663cb..e53edbe68cc 100644 --- a/zebra-rpc/src/methods/get_block_template.rs +++ b/zebra-rpc/src/methods/get_block_template.rs @@ -171,7 +171,6 @@ where }, transactions: vec![], coinbasetxn: Coinbase {}, - longpollid: empty_string.clone(), target: empty_string.clone(), mintime: 0, mutable: vec![], @@ -208,8 +207,6 @@ pub struct GetBlockTemplate { /// Add documentation. pub coinbasetxn: Coinbase, /// Add documentation. - pub longpollid: String, - /// Add documentation. pub target: String, /// Add documentation. pub mintime: u32, diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap index 58c10f0d9e4..e3a0491f471 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_template@mainnet_10.snap @@ -18,7 +18,6 @@ expression: block_template }, "transactions": [], "coinbasetxn": {}, - "longpollid": "", "target": "", "mintime": 0, "mutable": [], diff --git a/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap b/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap index 58c10f0d9e4..e3a0491f471 100644 --- a/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap +++ b/zebra-rpc/src/methods/tests/snapshots/get_block_template@testnet_10.snap @@ -18,7 +18,6 @@ expression: block_template }, "transactions": [], "coinbasetxn": {}, - "longpollid": "", "target": "", "mintime": 0, "mutable": [], diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index ddecbf3643f..8694bd085e7 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -809,7 +809,6 @@ async fn rpc_getblocktemplate() { get_block_template.coinbasetxn, get_block_template::Coinbase {} ); - assert!(get_block_template.longpollid.is_empty()); assert!(get_block_template.target.is_empty()); assert_eq!(get_block_template.mintime, 0); assert!(get_block_template.mutable.is_empty()); From 92add9a9ca8fa5571bcf3ee1faa63ea6fbbbdd19 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 25 Oct 2022 12:06:26 -0300 Subject: [PATCH 3/4] add underscore --- zebra-rpc/src/methods/get_block_template.rs | 75 ++++++++++++--------- zebra-rpc/src/methods/tests/vectors.rs | 33 ++++----- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/zebra-rpc/src/methods/get_block_template.rs b/zebra-rpc/src/methods/get_block_template.rs index e53edbe68cc..7b8692bc94d 100644 --- a/zebra-rpc/src/methods/get_block_template.rs +++ b/zebra-rpc/src/methods/get_block_template.rs @@ -159,25 +159,25 @@ where Ok(GetBlockTemplate { capabilities: vec![], version: 0, - previousblockhash: empty_string.clone(), - blockcommitmentshash: empty_string.clone(), - lightclientroothash: empty_string.clone(), - finalsaplingroothash: empty_string.clone(), - defaultroots: DefaultRoots { - merkleroot: empty_string.clone(), - chainhistoryroot: empty_string.clone(), - authdataroot: empty_string.clone(), - blockcommitmentshash: empty_string.clone(), + previous_block_hash: empty_string.clone(), + block_commitments_hash: empty_string.clone(), + light_client_root_hash: empty_string.clone(), + final_sapling_root_hash: empty_string.clone(), + default_roots: DefaultRoots { + merkle_root: empty_string.clone(), + chain_history_root: empty_string.clone(), + auth_data_root: empty_string.clone(), + block_commitments_hash: empty_string.clone(), }, transactions: vec![], - coinbasetxn: Coinbase {}, + coinbase_txn: Coinbase {}, target: empty_string.clone(), - mintime: 0, + min_time: 0, mutable: vec![], - noncerange: empty_string.clone(), - sigoplimit: 0, - sizelimit: 0, - curtime: 0, + nonce_range: empty_string.clone(), + sigop_limit: 0, + size_limit: 0, + cur_time: 0, bits: empty_string, height: 0, }) @@ -193,33 +193,44 @@ pub struct GetBlockTemplate { /// Add documentation. pub version: usize, /// Add documentation. - pub previousblockhash: String, + #[serde(rename = "previousblockhash")] + pub previous_block_hash: String, /// Add documentation. - pub blockcommitmentshash: String, + #[serde(rename = "blockcommitmentshash")] + pub block_commitments_hash: String, /// Add documentation. - pub lightclientroothash: String, + #[serde(rename = "lightclientroothash")] + pub light_client_root_hash: String, /// Add documentation. - pub finalsaplingroothash: String, + #[serde(rename = "finalsaplingroothash")] + pub final_sapling_root_hash: String, /// Add documentation. - pub defaultroots: DefaultRoots, + #[serde(rename = "defaultroots")] + pub default_roots: DefaultRoots, /// Add documentation. pub transactions: Vec, /// Add documentation. - pub coinbasetxn: Coinbase, + #[serde(rename = "coinbasetxn")] + pub coinbase_txn: Coinbase, /// Add documentation. pub target: String, /// Add documentation. - pub mintime: u32, + #[serde(rename = "mintime")] + pub min_time: u32, /// Add documentation. pub mutable: Vec, /// Add documentation. - pub noncerange: String, + #[serde(rename = "noncerange")] + pub nonce_range: String, /// Add documentation. - pub sigoplimit: u32, + #[serde(rename = "sigoplimit")] + pub sigop_limit: u32, /// Add documentation. - pub sizelimit: u32, + #[serde(rename = "sizelimit")] + pub size_limit: u32, /// Add documentation. - pub curtime: u32, + #[serde(rename = "curtime")] + pub cur_time: u32, /// Add documentation. pub bits: String, /// Add documentation. @@ -230,13 +241,17 @@ pub struct GetBlockTemplate { #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] pub struct DefaultRoots { /// Add documentation. - pub merkleroot: String, + #[serde(rename = "merkleroot")] + pub merkle_root: String, /// Add documentation. - pub chainhistoryroot: String, + #[serde(rename = "chainhistoryroot")] + pub chain_history_root: String, /// Add documentation. - pub authdataroot: String, + #[serde(rename = "authdataroot")] + pub auth_data_root: String, /// Add documentation. - pub blockcommitmentshash: String, + #[serde(rename = "blockcommitmentshash")] + pub block_commitments_hash: String, } /// Documentation and fields to be added in #5454. diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index 8694bd085e7..dd644c7b7da 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -793,29 +793,32 @@ async fn rpc_getblocktemplate() { assert!(get_block_template.capabilities.is_empty()); assert_eq!(get_block_template.version, 0); - assert!(get_block_template.previousblockhash.is_empty()); - assert!(get_block_template.blockcommitmentshash.is_empty()); - assert!(get_block_template.lightclientroothash.is_empty()); - assert!(get_block_template.finalsaplingroothash.is_empty()); - assert!(get_block_template.defaultroots.merkleroot.is_empty()); - assert!(get_block_template.defaultroots.chainhistoryroot.is_empty()); - assert!(get_block_template.defaultroots.authdataroot.is_empty()); + assert!(get_block_template.previous_block_hash.is_empty()); + assert!(get_block_template.block_commitments_hash.is_empty()); + assert!(get_block_template.light_client_root_hash.is_empty()); + assert!(get_block_template.final_sapling_root_hash.is_empty()); + assert!(get_block_template.default_roots.merkle_root.is_empty()); assert!(get_block_template - .defaultroots - .blockcommitmentshash + .default_roots + .chain_history_root + .is_empty()); + assert!(get_block_template.default_roots.auth_data_root.is_empty()); + assert!(get_block_template + .default_roots + .block_commitments_hash .is_empty()); assert!(get_block_template.transactions.is_empty()); assert_eq!( - get_block_template.coinbasetxn, + get_block_template.coinbase_txn, get_block_template::Coinbase {} ); assert!(get_block_template.target.is_empty()); - assert_eq!(get_block_template.mintime, 0); + assert_eq!(get_block_template.min_time, 0); assert!(get_block_template.mutable.is_empty()); - assert!(get_block_template.noncerange.is_empty()); - assert_eq!(get_block_template.sigoplimit, 0); - assert_eq!(get_block_template.sizelimit, 0); - assert_eq!(get_block_template.curtime, 0); + assert!(get_block_template.nonce_range.is_empty()); + assert_eq!(get_block_template.sigop_limit, 0); + assert_eq!(get_block_template.size_limit, 0); + assert_eq!(get_block_template.cur_time, 0); assert!(get_block_template.bits.is_empty()); assert_eq!(get_block_template.height, 0); From 575e89d9a040f7eb724ddf641d3103ee36e6ed76 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Tue, 25 Oct 2022 17:32:37 -0300 Subject: [PATCH 4/4] create modules for types --- zebra-rpc/src/methods.rs | 4 +- ...template.rs => get_block_template_rpcs.rs} | 85 ++----------------- .../methods/get_block_template_rpcs/types.rs | 6 ++ .../get_block_template_rpcs/types/coinbase.rs | 5 ++ .../types/default_roots.rs | 18 ++++ .../types/get_block_template.rs | 57 +++++++++++++ .../types/transaction.rs | 5 ++ zebra-rpc/src/methods/tests/snapshot.rs | 2 +- zebra-rpc/src/methods/tests/vectors.rs | 10 +-- 9 files changed, 107 insertions(+), 85 deletions(-) rename zebra-rpc/src/methods/{get_block_template.rs => get_block_template_rpcs.rs} (75%) create mode 100644 zebra-rpc/src/methods/get_block_template_rpcs/types.rs create mode 100644 zebra-rpc/src/methods/get_block_template_rpcs/types/coinbase.rs create mode 100644 zebra-rpc/src/methods/get_block_template_rpcs/types/default_roots.rs create mode 100644 zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template.rs create mode 100644 zebra-rpc/src/methods/get_block_template_rpcs/types/transaction.rs diff --git a/zebra-rpc/src/methods.rs b/zebra-rpc/src/methods.rs index 8807eda64e4..3a77f16c77e 100644 --- a/zebra-rpc/src/methods.rs +++ b/zebra-rpc/src/methods.rs @@ -35,10 +35,10 @@ use zebra_state::{OutputIndex, OutputLocation, TransactionLocation}; use crate::queue::Queue; #[cfg(feature = "getblocktemplate-rpcs")] -mod get_block_template; +mod get_block_template_rpcs; #[cfg(feature = "getblocktemplate-rpcs")] -pub use get_block_template::{GetBlockTemplateRpc, GetBlockTemplateRpcImpl}; +pub use get_block_template_rpcs::{GetBlockTemplateRpc, GetBlockTemplateRpcImpl}; #[cfg(test)] mod tests; diff --git a/zebra-rpc/src/methods/get_block_template.rs b/zebra-rpc/src/methods/get_block_template_rpcs.rs similarity index 75% rename from zebra-rpc/src/methods/get_block_template.rs rename to zebra-rpc/src/methods/get_block_template_rpcs.rs index 7b8692bc94d..8af354c12d1 100644 --- a/zebra-rpc/src/methods/get_block_template.rs +++ b/zebra-rpc/src/methods/get_block_template_rpcs.rs @@ -6,7 +6,14 @@ use jsonrpc_core::{self, BoxFuture, Error, ErrorCode, Result}; use jsonrpc_derive::rpc; use tower::{Service, ServiceExt}; -use crate::methods::{GetBlockHash, MISSING_BLOCK_ERROR_CODE}; +pub(crate) mod types; + +use crate::methods::{ + get_block_template_rpcs::types::{ + coinbase::Coinbase, default_roots::DefaultRoots, get_block_template::GetBlockTemplate, + }, + GetBlockHash, MISSING_BLOCK_ERROR_CODE, +}; /// getblocktemplate RPC method signatures. #[rpc(server)] @@ -185,82 +192,6 @@ where .boxed() } } -/// Documentation to be added after we document all the individual fields. -#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct GetBlockTemplate { - /// Add documentation. - pub capabilities: Vec, - /// Add documentation. - pub version: usize, - /// Add documentation. - #[serde(rename = "previousblockhash")] - pub previous_block_hash: String, - /// Add documentation. - #[serde(rename = "blockcommitmentshash")] - pub block_commitments_hash: String, - /// Add documentation. - #[serde(rename = "lightclientroothash")] - pub light_client_root_hash: String, - /// Add documentation. - #[serde(rename = "finalsaplingroothash")] - pub final_sapling_root_hash: String, - /// Add documentation. - #[serde(rename = "defaultroots")] - pub default_roots: DefaultRoots, - /// Add documentation. - pub transactions: Vec, - /// Add documentation. - #[serde(rename = "coinbasetxn")] - pub coinbase_txn: Coinbase, - /// Add documentation. - pub target: String, - /// Add documentation. - #[serde(rename = "mintime")] - pub min_time: u32, - /// Add documentation. - pub mutable: Vec, - /// Add documentation. - #[serde(rename = "noncerange")] - pub nonce_range: String, - /// Add documentation. - #[serde(rename = "sigoplimit")] - pub sigop_limit: u32, - /// Add documentation. - #[serde(rename = "sizelimit")] - pub size_limit: u32, - /// Add documentation. - #[serde(rename = "curtime")] - pub cur_time: u32, - /// Add documentation. - pub bits: String, - /// Add documentation. - pub height: u32, -} - -/// Documentation to be added in #5452 or #5455. -#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct DefaultRoots { - /// Add documentation. - #[serde(rename = "merkleroot")] - pub merkle_root: String, - /// Add documentation. - #[serde(rename = "chainhistoryroot")] - pub chain_history_root: String, - /// Add documentation. - #[serde(rename = "authdataroot")] - pub auth_data_root: String, - /// Add documentation. - #[serde(rename = "blockcommitmentshash")] - pub block_commitments_hash: String, -} - -/// Documentation and fields to be added in #5454. -#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct Transaction {} - -/// documentation and fields to be added in #5453. -#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct Coinbase {} /// Given a potentially negative index, find the corresponding `Height`. /// diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types.rs new file mode 100644 index 00000000000..6109391009e --- /dev/null +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types.rs @@ -0,0 +1,6 @@ +//! Types used in mining RPC methods. + +pub(crate) mod coinbase; +pub(crate) mod default_roots; +pub(crate) mod get_block_template; +pub(crate) mod transaction; diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types/coinbase.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types/coinbase.rs new file mode 100644 index 00000000000..323f50ca105 --- /dev/null +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types/coinbase.rs @@ -0,0 +1,5 @@ +//! The `Coinbase` type is part of the `getblocktemplate` RPC method output. + +/// documentation and fields to be added in #5453. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Coinbase {} diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types/default_roots.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types/default_roots.rs new file mode 100644 index 00000000000..b8bf3a493bc --- /dev/null +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types/default_roots.rs @@ -0,0 +1,18 @@ +//! The `DefaultRoots` type is part of the `getblocktemplate` RPC method output. + +/// Documentation to be added in #5452 or #5455. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct DefaultRoots { + /// Add documentation. + #[serde(rename = "merkleroot")] + pub merkle_root: String, + /// Add documentation. + #[serde(rename = "chainhistoryroot")] + pub chain_history_root: String, + /// Add documentation. + #[serde(rename = "authdataroot")] + pub auth_data_root: String, + /// Add documentation. + #[serde(rename = "blockcommitmentshash")] + pub block_commitments_hash: String, +} diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template.rs new file mode 100644 index 00000000000..76a9bb8b062 --- /dev/null +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types/get_block_template.rs @@ -0,0 +1,57 @@ +//! The `GetBlockTempate` type is the output of the `getblocktemplate` RPC method. + +use crate::methods::get_block_template_rpcs::types::{ + coinbase::Coinbase, default_roots::DefaultRoots, transaction::Transaction, +}; + +/// Documentation to be added after we document all the individual fields. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct GetBlockTemplate { + /// Add documentation. + pub capabilities: Vec, + /// Add documentation. + pub version: usize, + /// Add documentation. + #[serde(rename = "previousblockhash")] + pub previous_block_hash: String, + /// Add documentation. + #[serde(rename = "blockcommitmentshash")] + pub block_commitments_hash: String, + /// Add documentation. + #[serde(rename = "lightclientroothash")] + pub light_client_root_hash: String, + /// Add documentation. + #[serde(rename = "finalsaplingroothash")] + pub final_sapling_root_hash: String, + /// Add documentation. + #[serde(rename = "defaultroots")] + pub default_roots: DefaultRoots, + /// Add documentation. + pub transactions: Vec, + /// Add documentation. + #[serde(rename = "coinbasetxn")] + pub coinbase_txn: Coinbase, + /// Add documentation. + pub target: String, + /// Add documentation. + #[serde(rename = "mintime")] + pub min_time: u32, + /// Add documentation. + pub mutable: Vec, + /// Add documentation. + #[serde(rename = "noncerange")] + pub nonce_range: String, + /// Add documentation. + #[serde(rename = "sigoplimit")] + pub sigop_limit: u32, + /// Add documentation. + #[serde(rename = "sizelimit")] + pub size_limit: u32, + /// Add documentation. + #[serde(rename = "curtime")] + pub cur_time: u32, + /// Add documentation. + pub bits: String, + /// Add documentation. + pub height: u32, +} diff --git a/zebra-rpc/src/methods/get_block_template_rpcs/types/transaction.rs b/zebra-rpc/src/methods/get_block_template_rpcs/types/transaction.rs new file mode 100644 index 00000000000..5ee0c528453 --- /dev/null +++ b/zebra-rpc/src/methods/get_block_template_rpcs/types/transaction.rs @@ -0,0 +1,5 @@ +//! The `Transaction` type is part of the `getblocktemplate` RPC method output. + +/// Documentation and fields to be added in #5454. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct Transaction {} diff --git a/zebra-rpc/src/methods/tests/snapshot.rs b/zebra-rpc/src/methods/tests/snapshot.rs index 71d7f63abe2..3211cd9274a 100644 --- a/zebra-rpc/src/methods/tests/snapshot.rs +++ b/zebra-rpc/src/methods/tests/snapshot.rs @@ -302,7 +302,7 @@ fn snapshot_rpc_getblockhash(block_hash: GetBlockHash, settings: &insta::Setting #[cfg(feature = "getblocktemplate-rpcs")] /// Snapshot `getblocktemplate` response, using `cargo insta` and JSON serialization. fn snapshot_rpc_getblocktemplate( - block_template: crate::methods::get_block_template::GetBlockTemplate, + block_template: crate::methods::get_block_template_rpcs::types::get_block_template::GetBlockTemplate, settings: &insta::Settings, ) { settings.bind(|| insta::assert_json_snapshot!("get_block_template", block_template)); diff --git a/zebra-rpc/src/methods/tests/vectors.rs b/zebra-rpc/src/methods/tests/vectors.rs index dd644c7b7da..07b89909fac 100644 --- a/zebra-rpc/src/methods/tests/vectors.rs +++ b/zebra-rpc/src/methods/tests/vectors.rs @@ -647,7 +647,7 @@ async fn rpc_getblockcount() { // Init RPC let get_block_template_rpc = - get_block_template::GetBlockTemplateRpcImpl::new(latest_chain_tip.clone(), read_state); + get_block_template_rpcs::GetBlockTemplateRpcImpl::new(latest_chain_tip.clone(), read_state); // Get the tip height using RPC method `get_block_count` let get_block_count = get_block_template_rpc @@ -686,7 +686,7 @@ async fn rpc_getblockcount_empty_state() { ); let get_block_template_rpc = - get_block_template::GetBlockTemplateRpcImpl::new(latest_chain_tip.clone(), read_state); + get_block_template_rpcs::GetBlockTemplateRpcImpl::new(latest_chain_tip.clone(), read_state); // Get the tip height using RPC method `get_block_count let get_block_count = get_block_template_rpc.get_block_count(); @@ -730,7 +730,7 @@ async fn rpc_getblockhash() { latest_chain_tip.clone(), ); let get_block_template_rpc = - get_block_template::GetBlockTemplateRpcImpl::new(latest_chain_tip, read_state); + get_block_template_rpcs::GetBlockTemplateRpcImpl::new(latest_chain_tip, read_state); // Query the hashes using positive indexes for (i, block) in blocks.iter().enumerate() { @@ -784,7 +784,7 @@ async fn rpc_getblocktemplate() { latest_chain_tip.clone(), ); let get_block_template_rpc = - get_block_template::GetBlockTemplateRpcImpl::new(latest_chain_tip, read_state); + get_block_template_rpcs::GetBlockTemplateRpcImpl::new(latest_chain_tip, read_state); let get_block_template = get_block_template_rpc .get_block_template() @@ -810,7 +810,7 @@ async fn rpc_getblocktemplate() { assert!(get_block_template.transactions.is_empty()); assert_eq!( get_block_template.coinbase_txn, - get_block_template::Coinbase {} + get_block_template_rpcs::types::coinbase::Coinbase {} ); assert!(get_block_template.target.is_empty()); assert_eq!(get_block_template.min_time, 0);