Skip to main content

zebra_rpc/methods/types/
submit_block.rs

1//! Parameter and response types for the `submitblock` RPC.
2
3use std::time::Duration;
4
5use tokio::sync::mpsc;
6
7use zebra_chain::block;
8
9// Allow doc links to these imports.
10#[allow(unused_imports)]
11use crate::methods::GetBlockTemplateHandler;
12
13/// How long `submitblock` waits for a block to be verified and committed, before it returns
14/// `inconclusive`.
15///
16/// A block is verified and committed in well under a second. But the block verifier waits for a
17/// block's parent, and for the transparent outputs it spends. So a block whose parent never
18/// arrives isn't answered until the state drops it from its queue, which takes about a hundred
19/// blocks. A miner can find a block like that on work from another node.
20pub(crate) const SUBMIT_BLOCK_TIMEOUT: Duration = Duration::from_secs(30);
21
22/// How long the verification of a submitted block carries on in the background, after
23/// `submitblock` has returned `inconclusive` for it.
24///
25/// This is the time that Zebra's syncer gives the block verifier for a block, which is longer
26/// than any wait inside the verifier. A block that is in the state's queue by then stays there.
27pub(crate) const SUBMIT_BLOCK_VERIFY_TIMEOUT: Duration = Duration::from_secs(8 * 60);
28
29/// Optional argument `jsonparametersobject` for `submitblock` RPC request
30///
31/// See the notes for the [`submit_block`](crate::methods::RpcServer::submit_block) RPC.
32#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, schemars::JsonSchema)]
33pub struct SubmitBlockParameters {
34    /// The workid for the block template. Currently unused.
35    ///
36    /// > If the server provided a workid, it MUST be included with submissions,
37    ///
38    /// Rationale:
39    ///
40    /// > If servers allow all mutations, it may be hard to identify which job it is based on.
41    /// > While it may be possible to verify the submission by its content, it is much easier
42    /// > to compare it to the job issued. It is very easy for the miner to keep track of this.
43    /// > Therefore, using a "workid" is a very cheap solution to enable more mutations.
44    ///
45    /// <https://en.bitcoin.it/wiki/BIP_0022#Rationale>
46    #[serde(rename = "workid")]
47    pub _work_id: Option<String>,
48}
49
50/// Response to a `submitblock` RPC request.
51///
52/// Zebra never returns "duplicate-invalid", because it does not store invalid blocks.
53#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
54#[serde(rename_all = "kebab-case")]
55pub enum SubmitBlockErrorResponse {
56    /// Block was already committed to the non-finalized or finalized state
57    Duplicate,
58    /// Block was already added to the state queue or channel, but not yet committed to the non-finalized state
59    DuplicateInconclusive,
60    /// Block was already committed to the non-finalized state, but not on the best chain, or
61    /// the block is still being verified
62    Inconclusive,
63    /// Block rejected as invalid
64    Rejected,
65}
66
67/// Response to a `submitblock` RPC request.
68///
69/// Zebra never returns "duplicate-invalid", because it does not store invalid blocks.
70#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
71#[serde(untagged)]
72pub enum SubmitBlockResponse {
73    /// Block was not successfully submitted, return error
74    ErrorResponse(SubmitBlockErrorResponse),
75    /// Block successfully submitted, returns null
76    Accepted,
77}
78
79impl SubmitBlockResponse {
80    /// Counts this response in `rpc.submitblock.outcomes.total`, by its zcashd-compatible result.
81    ///
82    /// Operational RPC errors return before a response exists, so they aren't counted as
83    /// rejections.
84    pub(in crate::methods) fn with_metrics(self) -> Self {
85        let outcome = match &self {
86            Self::Accepted => "accepted",
87            Self::ErrorResponse(SubmitBlockErrorResponse::Duplicate) => "duplicate",
88            Self::ErrorResponse(SubmitBlockErrorResponse::DuplicateInconclusive) => {
89                "duplicate-inconclusive"
90            }
91            Self::ErrorResponse(SubmitBlockErrorResponse::Inconclusive) => "inconclusive",
92            Self::ErrorResponse(SubmitBlockErrorResponse::Rejected) => "rejected",
93        };
94        metrics::counter!("rpc.submitblock.outcomes.total", "outcome" => outcome).increment(1);
95        self
96    }
97}
98
99impl Default for SubmitBlockResponse {
100    fn default() -> Self {
101        Self::ErrorResponse(SubmitBlockErrorResponse::Rejected)
102    }
103}
104
105impl From<SubmitBlockErrorResponse> for SubmitBlockResponse {
106    fn from(error_response: SubmitBlockErrorResponse) -> Self {
107        Self::ErrorResponse(error_response)
108    }
109}
110
111/// A submit block channel, used to inform the gossip task about mined blocks.
112pub struct SubmitBlockChannel {
113    /// The channel sender
114    sender: mpsc::Sender<(block::Hash, block::Height)>,
115    /// The channel receiver
116    receiver: mpsc::Receiver<(block::Hash, block::Height)>,
117}
118
119impl SubmitBlockChannel {
120    /// Creates a new submit block channel
121    pub fn new() -> Self {
122        /// How many unread messages the submit block channel should buffer before rejecting sends.
123        ///
124        /// This should be large enough to usually avoid rejecting sends. This channel is used by
125        /// the block hash gossip task, which waits for a ready peer in the peer set while
126        /// processing messages from this channel and could be much slower to gossip block hashes
127        /// than it is to commit blocks and produce new block templates.
128        const SUBMIT_BLOCK_CHANNEL_CAPACITY: usize = 10_000;
129
130        let (sender, receiver) = mpsc::channel(SUBMIT_BLOCK_CHANNEL_CAPACITY);
131        Self { sender, receiver }
132    }
133
134    /// Get the channel sender
135    pub fn sender(&self) -> mpsc::Sender<(block::Hash, block::Height)> {
136        self.sender.clone()
137    }
138
139    /// Get the channel receiver
140    pub fn receiver(self) -> mpsc::Receiver<(block::Hash, block::Height)> {
141        self.receiver
142    }
143}
144
145impl Default for SubmitBlockChannel {
146    fn default() -> Self {
147        Self::new()
148    }
149}