Skip to content

Commit 1f869f5

Browse files
committed
Add getmininginfo v30 support
Redefine the type in v30 for the changes and remove TODO from table. Update the export, into function and test feature gate.
1 parent d61029b commit 1f869f5

File tree

9 files changed

+161
-12
lines changed

9 files changed

+161
-12
lines changed

integration_test/tests/mining.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fn mining__get_block_template__modelled() {
3737
}
3838

3939
#[test]
40-
#[cfg(feature = "v29_and_below")]
4140
fn mining__get_mining_info() {
4241
let node = Node::with_wallet(Wallet::Default, &[]);
4342

types/src/model/mining.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
use std::collections::BTreeMap;
99

1010
use bitcoin::{
11-
block, Amount, BlockHash, CompactTarget, SignedAmount, Target, Transaction, Txid, Weight, Wtxid,
11+
block, Amount, BlockHash, CompactTarget, FeeRate, SignedAmount, Target, Transaction, Txid,
12+
Weight, Wtxid,
1213
};
1314
use serde::{Deserialize, Serialize};
1415

@@ -115,6 +116,8 @@ pub struct GetMiningInfo {
115116
pub network_hash_ps: i64,
116117
/// The size of the mempool.
117118
pub pooled_tx: i64,
119+
/// Minimum feerate of packages selected for block inclusion.
120+
pub block_min_tx_fee: Option<FeeRate>,
118121
/// Current network name as defined in BIP70 (main, test, regtest).
119122
pub chain: String,
120123
/// The block challenge (aka. block script).

types/src/v17/mining/into.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl GetMiningInfo {
100100
target: None,
101101
network_hash_ps: self.network_hash_ps,
102102
pooled_tx: self.pooled_tx,
103+
block_min_tx_fee: None,
103104
chain: self.chain,
104105
signet_challenge: None,
105106
next: None,

types/src/v28/mining.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl GetMiningInfo {
5353
target: None,
5454
network_hash_ps: self.network_hash_ps,
5555
pooled_tx: self.pooled_tx,
56+
block_min_tx_fee: None,
5657
chain: self.chain,
5758
signet_challenge: None,
5859
next: None,

types/src/v29/mining/into.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl GetMiningInfo {
2525
target: Some(target),
2626
network_hash_ps: self.network_hash_ps,
2727
pooled_tx: self.pooled_tx,
28+
block_min_tx_fee: None,
2829
chain: self.chain,
2930
signet_challenge: self.signet_challenge,
3031
next: Some(next),

types/src/v30/mining/error.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use core::fmt;
4+
5+
use bitcoin::amount::ParseAmountError;
6+
use bitcoin::error::UnprefixedHexError;
7+
8+
use super::NextBlockInfoError;
9+
use crate::error::write_err;
10+
11+
/// Error when converting a `GetMiningInfo` type into the model type.
12+
#[derive(Debug)]
13+
pub enum GetMiningInfoError {
14+
/// Conversion of the `bits` field failed.
15+
Bits(UnprefixedHexError),
16+
/// Conversion of the `target` field failed.
17+
Target(UnprefixedHexError),
18+
/// Conversion of the `block_min_tx_fee` field failed.
19+
BlockMinTxFee(ParseAmountError),
20+
/// Conversion of one of the items in field `next` failed.
21+
Next(NextBlockInfoError),
22+
}
23+
24+
impl fmt::Display for GetMiningInfoError {
25+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
match *self {
27+
Self::Bits(ref e) => write_err!(f, "conversion of the `bits` field failed"; e),
28+
Self::Target(ref e) => write_err!(f, "conversion of the `target` field failed"; e),
29+
Self::BlockMinTxFee(ref e) =>
30+
write_err!(f, "conversion of the `block_min_tx_fee` field failed"; e),
31+
Self::Next(ref e) =>
32+
write_err!(f, "conversion of one of the items in field `next` failed"; e),
33+
}
34+
}
35+
}
36+
37+
#[cfg(feature = "std")]
38+
impl std::error::Error for GetMiningInfoError {
39+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
40+
match *self {
41+
Self::Bits(ref e) => Some(e),
42+
Self::Target(ref e) => Some(e),
43+
Self::BlockMinTxFee(ref e) => Some(e),
44+
Self::Next(ref e) => Some(e),
45+
}
46+
}
47+
}

types/src/v30/mining/into.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
use bitcoin::{CompactTarget, Target, Weight};
4+
5+
use super::{GetMiningInfo, GetMiningInfoError};
6+
use crate::model;
7+
8+
impl GetMiningInfo {
9+
/// Converts version specific type to a version nonspecific, more strongly typed type.
10+
pub fn into_model(self) -> Result<model::GetMiningInfo, GetMiningInfoError> {
11+
use GetMiningInfoError as E;
12+
13+
let current_block_weight = self.current_block_weight.map(Weight::from_wu);
14+
let bits = CompactTarget::from_unprefixed_hex(&self.bits).map_err(E::Bits)?;
15+
let target = Target::from_unprefixed_hex(self.target.as_ref()).map_err(E::Target)?;
16+
let block_min_tx_fee =
17+
crate::btc_per_kb(self.block_min_tx_fee).map_err(E::BlockMinTxFee)?;
18+
19+
let next = self.next.into_model().map_err(E::Next)?;
20+
21+
Ok(model::GetMiningInfo {
22+
blocks: self.blocks,
23+
current_block_weight,
24+
current_block_tx: self.current_block_tx,
25+
bits: Some(bits),
26+
difficulty: self.difficulty,
27+
target: Some(target),
28+
network_hash_ps: self.network_hash_ps,
29+
pooled_tx: self.pooled_tx,
30+
block_min_tx_fee,
31+
chain: self.chain,
32+
signet_challenge: self.signet_challenge,
33+
next: Some(next),
34+
warnings: self.warnings,
35+
})
36+
}
37+
}

types/src/v30/mining/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! The JSON-RPC API for Bitcoin Core `v29` - mining.
4+
//!
5+
//! Types for methods found under the `== Mining ==` section of the API docs.
6+
7+
mod error;
8+
mod into;
9+
10+
use serde::{Deserialize, Serialize};
11+
12+
pub use self::error::GetMiningInfoError;
13+
pub use super::{NextBlockInfo, NextBlockInfoError};
14+
15+
/// Result of the JSON-RPC method `getmininginfo`.
16+
///
17+
/// > getmininginfo
18+
/// >
19+
/// > Returns a json object containing mining-related information.
20+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
21+
#[cfg_attr(feature = "serde-deny-unknown-fields", serde(deny_unknown_fields))]
22+
pub struct GetMiningInfo {
23+
/// The current block.
24+
pub blocks: u64,
25+
/// The block weight (including reserved weight for block header, txs count and coinbase tx) of
26+
/// the last assembled block (only present if a block was ever assembled).
27+
#[serde(rename = "currentblockweight")]
28+
pub current_block_weight: Option<u64>,
29+
/// The number of block transactions (excluding coinbase) of the last assembled block (only
30+
/// present if a block was ever assembled).
31+
#[serde(rename = "currentblocktx")]
32+
pub current_block_tx: Option<i64>,
33+
/// The current nBits, compact representation of the block difficulty target.
34+
pub bits: String,
35+
/// The current difficulty.
36+
pub difficulty: f64,
37+
/// The current target.
38+
pub target: String,
39+
/// The network hashes per second.
40+
#[serde(rename = "networkhashps")]
41+
pub network_hash_ps: i64,
42+
/// The size of the mempool.
43+
#[serde(rename = "pooledtx")]
44+
pub pooled_tx: i64,
45+
/// Minimum feerate of packages selected for block inclusion in BTC/kvB.
46+
#[serde(rename = "blockmintxfee")]
47+
pub block_min_tx_fee: f64,
48+
/// Current network name as defined in BIP70 (main, test, regtest).
49+
pub chain: String,
50+
/// The block challenge (aka. block script), in hexadecimal (only present if the current network
51+
/// is a signet).
52+
pub signet_challenge: Option<String>,
53+
/// The next block.
54+
pub next: NextBlockInfo,
55+
/// Any network and blockchain warnings.
56+
pub warnings: Vec<String>,
57+
}

types/src/v30/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
//! | JSON-RPC Method Name | Returns | Notes |
8787
//! |:-----------------------------------|:---------------:|:--------------------------------------:|
8888
//! | getblocktemplate | version + model | |
89-
//! | getmininginfo | version + model | TODO |
89+
//! | getmininginfo | version + model | |
9090
//! | getnetworkhashps | returns boolean | |
9191
//! | getprioritisedtransactions | version + model | |
9292
//! | prioritisetransaction | returns boolean | |
@@ -243,9 +243,13 @@
243243
//! </details>
244244
245245
mod blockchain;
246+
mod mining;
246247

247248
#[doc(inline)]
248-
pub use self::{blockchain::GetMempoolInfo};
249+
pub use self::{
250+
blockchain::GetMempoolInfo,
251+
mining::{GetMiningInfo, GetMiningInfoError},
252+
};
249253
#[doc(inline)]
250254
pub use crate::{
251255
v17::{
@@ -301,12 +305,11 @@ pub use crate::{
301305
},
302306
v24::{
303307
DecodePsbt, DecodePsbtError, GetMempoolAncestors, GetMempoolAncestorsVerbose,
304-
GetMempoolDescendants, GetMempoolDescendantsVerbose, GetMempoolEntry,
305-
GetRawMempoolVerbose, GetTransactionDetail, GetTxSpendingPrevout,
306-
GetTxSpendingPrevoutError, GlobalXpub, ListUnspent, ListUnspentItem, MempoolEntry,
307-
MigrateWallet, Proprietary, PsbtInput, PsbtOutput, SendAll, SendAllError,
308-
SimulateRawTransaction, TaprootBip32Deriv, TaprootLeaf, TaprootScript,
309-
TaprootScriptPathSig,
308+
GetMempoolDescendants, GetMempoolDescendantsVerbose, GetMempoolEntry, GetRawMempoolVerbose,
309+
GetTransactionDetail, GetTxSpendingPrevout, GetTxSpendingPrevoutError, GlobalXpub,
310+
ListUnspent, ListUnspentItem, MempoolEntry, MigrateWallet, Proprietary, PsbtInput,
311+
PsbtOutput, SendAll, SendAllError, SimulateRawTransaction, TaprootBip32Deriv, TaprootLeaf,
312+
TaprootScript, TaprootScriptPathSig,
310313
},
311314
v25::{
312315
GenerateBlock, GenerateBlockError, GetBlockStats, ListDescriptors, MempoolAcceptanceError,
@@ -334,7 +337,7 @@ pub use crate::{
334337
GetBlockHeaderVerbose, GetBlockHeaderVerboseError, GetBlockVerboseOne,
335338
GetBlockVerboseOneError, GetBlockchainInfo, GetBlockchainInfoError, GetChainStates,
336339
GetChainStatesError, GetDescriptorActivity, GetDescriptorActivityError, GetDescriptorInfo,
337-
GetMiningInfo, GetMiningInfoError, MempoolAcceptance, NextBlockInfo, NextBlockInfoError,
338-
ReceiveActivity, SpendActivity, TestMempoolAccept,
340+
MempoolAcceptance, NextBlockInfo, NextBlockInfoError, ReceiveActivity, SpendActivity,
341+
TestMempoolAccept,
339342
},
340343
};

0 commit comments

Comments
 (0)