zebra_rpc/methods/types/get_block_template/
parameters.rs

1//! Parameter types for the `getblocktemplate` RPC.
2
3use derive_getters::Getters;
4use derive_new::new;
5
6use crate::methods::{hex_data::HexData, types::long_poll::LongPollId};
7
8/// Defines whether the RPC method should generate a block template or attempt to validate a block
9/// proposal.
10#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
11#[serde(rename_all = "lowercase")]
12pub enum GetBlockTemplateRequestMode {
13    /// Indicates a request for a block template.
14    Template,
15
16    /// Indicates a request to validate block data.
17    Proposal,
18}
19
20impl Default for GetBlockTemplateRequestMode {
21    fn default() -> Self {
22        Self::Template
23    }
24}
25
26/// Valid `capabilities` values that indicate client-side support.
27#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
28#[serde(rename_all = "lowercase")]
29pub enum GetBlockTemplateCapability {
30    /// Long Polling support.
31    /// Currently ignored by zebra.
32    LongPoll,
33
34    /// Information for coinbase transaction, default template data with the `coinbasetxn` field.
35    /// Currently ignored by zebra.
36    CoinbaseTxn,
37
38    /// Coinbase value, template response provides a `coinbasevalue` field and omits `coinbasetxn` field.
39    /// Currently ignored by zebra.
40    CoinbaseValue,
41
42    /// Components of the coinbase transaction.
43    /// Currently ignored by zebra.
44    CoinbaseAux,
45
46    /// Currently ignored by zcashd and zebra.
47    Proposal,
48
49    /// Currently ignored by zcashd and zebra.
50    ServerList,
51
52    /// Currently ignored by zcashd and zebra.
53    WorkId,
54
55    /// Unknown capability to fill in for mutations.
56    // TODO: Fill out valid mutations capabilities.
57    //       The set of possible capabilities is open-ended, so we need to keep UnknownCapability.
58    #[serde(other)]
59    UnknownCapability,
60}
61
62/// Optional parameter `jsonrequestobject` for `getblocktemplate` RPC request.
63///
64/// The `data` field must be provided in `proposal` mode, and must be omitted in `template` mode.
65/// All other fields are optional.
66#[derive(
67    Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize, Default, Getters, new,
68)]
69pub struct GetBlockTemplateParameters {
70    /// Defines whether the RPC method should generate a block template or attempt to
71    /// validate block data, checking against all of the server's usual acceptance rules
72    /// (excluding the check for a valid proof-of-work).
73    #[serde(default)]
74    pub(crate) mode: GetBlockTemplateRequestMode,
75
76    /// Must be omitted when `getblocktemplate` RPC is called in "template" mode (or when `mode` is omitted).
77    /// Must be provided when `getblocktemplate` RPC is called in "proposal" mode.
78    ///
79    /// Hex-encoded block data to be validated and checked against the server's usual acceptance rules
80    /// (excluding the check for a valid proof-of-work).
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub(crate) data: Option<HexData>,
83
84    /// A list of client-side supported capability features
85    #[serde(default)]
86    #[serde(skip_serializing_if = "Vec::is_empty")]
87    pub(crate) capabilities: Vec<GetBlockTemplateCapability>,
88
89    /// An ID that delays the RPC response until the template changes.
90    ///
91    /// In Zebra, the ID represents the chain tip, max time, and mempool contents.
92    #[serde(rename = "longpollid")]
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub(crate) long_poll_id: Option<LongPollId>,
95
96    /// The workid for the block template.
97    ///
98    /// currently unused.
99    #[serde(rename = "workid")]
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub(crate) _work_id: Option<String>,
102}
103
104impl GetBlockTemplateParameters {
105    /// Returns Some(data) with the block proposal hexdata if in `Proposal` mode and `data` is provided.
106    pub fn block_proposal_data(&self) -> Option<HexData> {
107        match self {
108            Self { data: None, .. }
109            | Self {
110                mode: GetBlockTemplateRequestMode::Template,
111                ..
112            } => None,
113
114            Self {
115                mode: GetBlockTemplateRequestMode::Proposal,
116                data,
117                ..
118            } => data.clone(),
119        }
120    }
121}