template_distribution_sv2/submit_solution.rs
1use alloc::{fmt, vec::Vec};
2use binary_sv2::{binary_codec_sv2, Deserialize, Serialize, B064K};
3use core::convert::TryInto;
4
5/// Message used by a downstream to submit a successful solution to a previously provided template.
6///
7/// The downstream is expected to send this message in addition to the `SubmitSolution` message
8/// from the Mining Protocol in order to propagate the solution to the Bitcoin network as soon as
9/// possible.
10///
11/// Upon receiving this message, upstream(Template Provider) **must** immediately construct the
12/// corresponding full block and attempt to propagate it to the Bitcoin network.
13#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
14pub struct SubmitSolution<'decoder> {
15 /// Identifies the template to which this solution corresponds.
16 ///
17 /// This is acquired from the [`crate::NewTemplate`] message.
18 pub template_id: u64,
19 /// Version field in the block header.
20 ///
21 /// Bits not defined by
22 /// [BIP320](https://github.com/bitcoin/bips/blob/master/bip-0320.mediawiki) as additional
23 /// nonce **must** be the same as they appear in the `NewMiningJob` or `NewExtendedMiningJob`
24 /// message, other bits may be set to any value.
25 pub version: u32,
26 /// nTime field in the block header.
27 ///
28 /// This **must** be greater than or equal to previously received
29 /// [`crate::SetNewPrevHash::header_timestamp`] and lower than or equal to that value plus the
30 /// number of seconds since receiving [`crate::SetNewPrevHash`] that message.
31 pub header_timestamp: u32,
32 /// Nonce field in the header.
33 pub header_nonce: u32,
34 /// Full serialized coinbase transaction, meeting all the requirements of the `NewMiningJob` or
35 /// `NewExtendedMiningJob` message.
36 pub coinbase_tx: B064K<'decoder>,
37}
38
39impl fmt::Display for SubmitSolution<'_> {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 write!(
42 f,
43 "SubmitSolution {{ template_id: {}, version: {}, header_timestamp: {}, header_nonce: {}, coinbase_tx: {} }}",
44 self.template_id,
45 self.version,
46 self.header_timestamp,
47 self.header_nonce,
48 self.coinbase_tx
49 )
50 }
51}