Skip to content

Commit d575354

Browse files
committed
Implement methods from the mining section
Add types, implement in client, and verify all methods from the mining section except `submitheader` and `submitblock`. `submitblock` is done but I can't get the test to pass all the time. - `submitblock` has flaky (excluded) tests. - `submitheader` is untested. One the way I fixed a few things in the network section (`getpeerinfo`) because I ran into issues there at some stage.
1 parent 3edf026 commit d575354

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1337
-286
lines changed

client/src/client_sync/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,24 @@ pub struct WalletCreateFundedPsbtInput {
241241
txid: Txid,
242242
vout: u32,
243243
}
244+
245+
/// Arg for the `getblocktemplate` method.
246+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
247+
pub struct TemplateRequest {
248+
/// A list of strings.
249+
pub rules: Vec<TemplateRules>,
250+
}
251+
252+
/// Client side supported softfork deployment.
253+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
254+
#[serde(rename_all = "lowercase")]
255+
pub enum TemplateRules {
256+
/// SegWit v0 supported.
257+
Segwit,
258+
/// Signet supported.
259+
Signet,
260+
/// CSV supported.
261+
Csv,
262+
/// Taproot supported.
263+
Taproot,
264+
}

client/src/client_sync/v17/mining.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing JSON-RPC methods on a client.
4+
//!
5+
//! Specifically this is methods found under the `== Mining ==` section of the
6+
//! API docs of Bitcoin Core `v0.17`.
7+
//!
8+
//! All macros require `Client` to be in scope.
9+
//!
10+
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11+
12+
/// Implements Bitcoin Core JSON-RPC API method `getblocktemplate`
13+
#[macro_export]
14+
macro_rules! impl_client_v17__getblocktemplate {
15+
() => {
16+
impl Client {
17+
pub fn get_block_template(
18+
&self,
19+
request: &$crate::client_sync::TemplateRequest,
20+
) -> Result<GetBlockTemplate> {
21+
self.call("getblocktemplate", &[into_json(request)?])
22+
}
23+
}
24+
};
25+
}
26+
27+
/// Implements Bitcoin Core JSON-RPC API method `getmininginfo`
28+
#[macro_export]
29+
macro_rules! impl_client_v17__getmininginfo {
30+
() => {
31+
impl Client {
32+
pub fn get_mining_info(&self) -> Result<GetMiningInfo> {
33+
self.call("getmininginfo", &[])
34+
}
35+
}
36+
};
37+
}
38+
39+
/// Implements Bitcoin Core JSON-RPC API method `getnetworkhashps`
40+
#[macro_export]
41+
macro_rules! impl_client_v17__getnetworkhashps {
42+
() => {
43+
impl Client {
44+
pub fn get_network_hash_ps(&self) -> Result<f64> { self.call("getnetworkhashps", &[]) }
45+
}
46+
};
47+
}
48+
49+
/// Implements Bitcoin Core JSON-RPC API method `prioritisetransaction`
50+
#[macro_export]
51+
macro_rules! impl_client_v17__prioritisetransaction {
52+
() => {
53+
impl Client {
54+
pub fn prioritise_transaction(
55+
&self,
56+
txid: &Txid,
57+
fee_delta: bitcoin::SignedAmount,
58+
) -> Result<bool> {
59+
let sats = fee_delta.to_sat();
60+
self.call("prioritisetransaction", &[into_json(txid)?, 0.into(), sats.into()])
61+
}
62+
}
63+
};
64+
}
65+
66+
/// Implements Bitcoin Core JSON-RPC API method `submitblock`
67+
#[macro_export]
68+
macro_rules! impl_client_v17__submitblock {
69+
() => {
70+
impl Client {
71+
pub fn submit_block(&self, block: &Block) -> Result<()> {
72+
let hex: String = bitcoin::consensus::encode::serialize_hex(block);
73+
match self.call("submitblock", &[into_json(hex)?]) {
74+
Ok(serde_json::Value::Null) => Ok(()),
75+
Ok(res) => Err(Error::Returned(res.to_string())),
76+
Err(err) => Err(err.into()),
77+
}
78+
}
79+
}
80+
};
81+
}

client/src/client_sync/v17/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub mod blockchain;
88
pub mod control;
99
pub mod generating;
10+
pub mod mining;
1011
pub mod network;
1112
pub mod raw_transactions;
1213
pub mod wallet;
@@ -61,6 +62,13 @@ crate::impl_client_v17__generatetoaddress!();
6162
crate::impl_client_v17__generate!();
6263
crate::impl_client_v17__invalidateblock!();
6364

65+
// == Mining ==
66+
crate::impl_client_v17__getblocktemplate!();
67+
crate::impl_client_v17__getmininginfo!();
68+
crate::impl_client_v17__getnetworkhashps!();
69+
crate::impl_client_v17__prioritisetransaction!();
70+
crate::impl_client_v17__submitblock!();
71+
6472
// == Network ==
6573
crate::impl_client_v17__getaddednodeinfo!();
6674
crate::impl_client_v17__getnettotals!();

client/src/client_sync/v18/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ crate::impl_client_v17__generatetoaddress!();
5656
crate::impl_client_v17__generate!();
5757
crate::impl_client_v17__invalidateblock!();
5858

59+
// == Mining ==
60+
crate::impl_client_v17__getblocktemplate!();
61+
crate::impl_client_v17__getmininginfo!();
62+
crate::impl_client_v17__getnetworkhashps!();
63+
crate::impl_client_v17__prioritisetransaction!();
64+
crate::impl_client_v17__submitblock!();
65+
5966
// == Network ==
6067
crate::impl_client_v17__getaddednodeinfo!();
6168
crate::impl_client_v17__getnettotals!();

client/src/client_sync/v19/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ crate::impl_client_v17__uptime!();
5656
crate::impl_client_v17__generatetoaddress!();
5757
crate::impl_client_v17__invalidateblock!();
5858

59+
// == Mining ==
60+
crate::impl_client_v17__getblocktemplate!();
61+
crate::impl_client_v17__getmininginfo!();
62+
crate::impl_client_v17__getnetworkhashps!();
63+
crate::impl_client_v17__prioritisetransaction!();
64+
crate::impl_client_v17__submitblock!();
65+
5966
// == Network ==
6067
crate::impl_client_v17__getnetworkinfo!();
6168
crate::impl_client_check_expected_server_version!({ [190100] });
69+
crate::impl_client_v17__getpeerinfo!();
6270

6371
// == Rawtransactions ==
6472
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v20.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ crate::impl_client_v17__uptime!();
5353
crate::impl_client_v17__generatetoaddress!();
5454
crate::impl_client_v17__invalidateblock!();
5555

56+
// == Mining ==
57+
crate::impl_client_v17__getblocktemplate!();
58+
crate::impl_client_v17__getmininginfo!();
59+
crate::impl_client_v17__getnetworkhashps!();
60+
crate::impl_client_v17__prioritisetransaction!();
61+
crate::impl_client_v17__submitblock!();
62+
5663
// == Network ==
5764
crate::impl_client_v17__getnetworkinfo!();
5865
crate::impl_client_check_expected_server_version!({ [200200] });
66+
crate::impl_client_v17__getpeerinfo!();
5967

6068
// == Rawtransactions ==
6169
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v21/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,17 @@ crate::impl_client_v17__uptime!();
5555
crate::impl_client_v17__generatetoaddress!();
5656
crate::impl_client_v17__invalidateblock!();
5757

58+
// == Mining ==
59+
crate::impl_client_v17__getblocktemplate!();
60+
crate::impl_client_v17__getmininginfo!();
61+
crate::impl_client_v17__getnetworkhashps!();
62+
crate::impl_client_v17__prioritisetransaction!();
63+
crate::impl_client_v17__submitblock!();
64+
5865
// == Network ==
5966
crate::impl_client_v17__getnetworkinfo!();
6067
crate::impl_client_check_expected_server_version!({ [210200] });
68+
crate::impl_client_v17__getpeerinfo!();
6169

6270
// == Rawtransactions ==
6371
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v22/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ crate::impl_client_v17__uptime!();
5656
crate::impl_client_v17__generatetoaddress!();
5757
crate::impl_client_v17__invalidateblock!();
5858

59+
// == Mining ==
60+
crate::impl_client_v17__getblocktemplate!();
61+
crate::impl_client_v17__getmininginfo!();
62+
crate::impl_client_v17__getnetworkhashps!();
63+
crate::impl_client_v17__prioritisetransaction!();
64+
crate::impl_client_v17__submitblock!();
65+
5966
// == Network ==
6067
crate::impl_client_v17__getnetworkinfo!();
6168
crate::impl_client_check_expected_server_version!({ [220000, 220100] });
69+
crate::impl_client_v17__getpeerinfo!();
6270

6371
// == Rawtransactions ==
6472
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v23.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ crate::impl_client_v17__uptime!();
5454
crate::impl_client_v17__generatetoaddress!();
5555
crate::impl_client_v17__invalidateblock!();
5656

57+
// == Mining ==
58+
crate::impl_client_v17__getblocktemplate!();
59+
crate::impl_client_v17__getmininginfo!();
60+
crate::impl_client_v17__getnetworkhashps!();
61+
crate::impl_client_v17__prioritisetransaction!();
62+
crate::impl_client_v17__submitblock!();
63+
5764
// == Network ==
5865
crate::impl_client_v17__getnetworkinfo!();
5966
crate::impl_client_check_expected_server_version!({ [230000, 230100, 230200] });
67+
crate::impl_client_v17__getpeerinfo!();
6068

6169
// == Rawtransactions ==
6270
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v24.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ crate::impl_client_v17__uptime!();
5353
crate::impl_client_v17__generatetoaddress!();
5454
crate::impl_client_v17__invalidateblock!();
5555

56+
// == Mining ==
57+
crate::impl_client_v17__getblocktemplate!();
58+
crate::impl_client_v17__getmininginfo!();
59+
crate::impl_client_v17__getnetworkhashps!();
60+
crate::impl_client_v17__prioritisetransaction!();
61+
crate::impl_client_v17__submitblock!();
62+
5663
// == Network ==
5764
crate::impl_client_v17__getnetworkinfo!();
5865
crate::impl_client_check_expected_server_version!({ [240001, 240100, 240200] });
66+
crate::impl_client_v17__getpeerinfo!();
5967

6068
// == Rawtransactions ==
6169
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v25.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ crate::impl_client_v17__uptime!();
5353
crate::impl_client_v17__generatetoaddress!();
5454
crate::impl_client_v17__invalidateblock!();
5555

56+
// == Mining ==
57+
crate::impl_client_v17__getblocktemplate!();
58+
crate::impl_client_v17__getmininginfo!();
59+
crate::impl_client_v17__getnetworkhashps!();
60+
crate::impl_client_v17__prioritisetransaction!();
61+
crate::impl_client_v17__submitblock!();
62+
5663
// == Network ==
5764
crate::impl_client_v17__getnetworkinfo!();
5865
crate::impl_client_check_expected_server_version!({ [250000, 250100, 250200] });
66+
crate::impl_client_v17__getpeerinfo!();
5967

6068
// == Rawtransactions ==
6169
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v26/mining.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing JSON-RPC methods on a client.
4+
//!
5+
//! Specifically this is methods found under the `== Mining ==` section of the
6+
//! API docs of Bitcoin Core `v0.26`.
7+
//!
8+
//! All macros require `Client` to be in scope.
9+
//!
10+
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11+
12+
/// Implements Bitcoin Core JSON-RPC API method `getprioritisedtransactions`
13+
#[macro_export]
14+
macro_rules! impl_client_v26__get_prioritised_transactions {
15+
() => {
16+
impl Client {
17+
pub fn get_prioritised_transactions(&self) -> Result<GetPrioritisedTransactions> {
18+
self.call("getprioritisedtransactions", &[])
19+
}
20+
}
21+
};
22+
}

client/src/client_sync/v26/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! We ignore option arguments unless they effect the shape of the returned JSON data.
66
77
pub mod blockchain;
8+
pub mod mining;
89

910
use std::collections::BTreeMap;
1011
use std::path::Path;
@@ -55,9 +56,18 @@ crate::impl_client_v17__uptime!();
5556
crate::impl_client_v17__generatetoaddress!();
5657
crate::impl_client_v17__invalidateblock!();
5758

59+
// == Mining ==
60+
crate::impl_client_v17__getblocktemplate!();
61+
crate::impl_client_v17__getmininginfo!();
62+
crate::impl_client_v17__getnetworkhashps!();
63+
crate::impl_client_v26__get_prioritised_transactions!();
64+
crate::impl_client_v17__prioritisetransaction!();
65+
crate::impl_client_v17__submitblock!();
66+
5867
// == Network ==
5968
crate::impl_client_v17__getnetworkinfo!();
6069
crate::impl_client_check_expected_server_version!({ [260000, 260100, 260200] });
70+
crate::impl_client_v17__getpeerinfo!();
6171

6272
// == Rawtransactions ==
6373
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v27.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,18 @@ crate::impl_client_v17__uptime!();
5353
crate::impl_client_v17__generatetoaddress!();
5454
crate::impl_client_v17__invalidateblock!();
5555

56+
// == Mining ==
57+
crate::impl_client_v17__getblocktemplate!();
58+
crate::impl_client_v17__getmininginfo!();
59+
crate::impl_client_v17__getnetworkhashps!();
60+
crate::impl_client_v26__get_prioritised_transactions!();
61+
crate::impl_client_v17__prioritisetransaction!();
62+
crate::impl_client_v17__submitblock!();
63+
5664
// == Network ==
5765
crate::impl_client_v17__getnetworkinfo!();
5866
crate::impl_client_check_expected_server_version!({ [270000, 270100] });
67+
crate::impl_client_v17__getpeerinfo!();
5968

6069
// == Rawtransactions ==
6170
crate::impl_client_v17__createrawtransaction!();

client/src/client_sync/v28/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ crate::impl_client_v17__uptime!();
5555
crate::impl_client_v17__generatetoaddress!();
5656
crate::impl_client_v17__invalidateblock!();
5757

58+
// == Mining ==
59+
crate::impl_client_v17__getblocktemplate!();
60+
crate::impl_client_v17__getmininginfo!();
61+
crate::impl_client_v17__getnetworkhashps!();
62+
crate::impl_client_v26__get_prioritised_transactions!();
63+
crate::impl_client_v17__prioritisetransaction!();
64+
crate::impl_client_v17__submitblock!();
65+
5866
// == Network ==
5967
crate::impl_client_v17__getnetworkinfo!();
6068
crate::impl_client_check_expected_server_version!({ [280000] });
69+
crate::impl_client_v17__getpeerinfo!();
6170

6271
// == Rawtransactions ==
6372
crate::impl_client_v17__createrawtransaction!();

0 commit comments

Comments
 (0)