Skip to content

Commit 828b63d

Browse files
authored
Merge pull request #104 from tcharding/03-14-getblockstats
Test `getblockstats` for all versions
2 parents 5bdff2b + 86b07a3 commit 828b63d

File tree

12 files changed

+48
-58
lines changed

12 files changed

+48
-58
lines changed

integration_test/src/lib.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,37 @@ const NBLOCKS: usize = 101;
1515
#[allow(dead_code)] // Not all tests use this function.
1616
pub fn init_logger() { let _ = env_logger::try_init(); }
1717

18+
/// Controls the loaded wallet.
19+
pub enum Wallet {
20+
/// Load the default wallet.
21+
Default,
22+
/// Load a wallet with custom name.
23+
Load(String),
24+
/// Do not load a wallet.
25+
None,
26+
}
27+
1828
pub trait NodeExt {
1929
/// Returns a handle to a `bitcoind` instance after leading wallet if present.
20-
fn new(conf: node::Conf, wallet: Option<String>) -> Node;
30+
fn _new(wallet: Wallet, txindex: bool) -> Node;
2131

2232
/// Returns a handle to a `bitcoind` instance with "default" wallet loaded.
23-
fn new_with_default_wallet() -> Node {
24-
let conf = node::Conf::default();
25-
Self::new(conf, None)
26-
}
33+
fn new_with_default_wallet() -> Node { Self::_new(Wallet::Default, false) }
2734

2835
/// Returns a handle to a `bitcoind` instance with "default" wallet loaded and `-txindex` enabled.
29-
fn new_with_default_wallet_txindex() -> Node {
30-
let mut conf = node::Conf::default();
31-
conf.args.push("-txindex");
32-
Self::new(conf, None)
33-
}
36+
fn new_with_default_wallet_txindex() -> Node { Self::_new(Wallet::Default, true) }
3437

3538
/// Returns a handle to a `bitcoind` instance with `wallet` loaded.
36-
fn new_with_wallet(wallet: String) -> Node {
37-
let conf = node::Conf::default();
38-
Self::new(conf, Some(wallet))
39-
}
39+
fn new_with_wallet(wallet: String) -> Node { Self::_new(Wallet::Load(wallet), false) }
40+
41+
/// Returns a handle to a `bitcoind` instance with `wallet` loaded and `-txindex` enabled.
42+
fn new_with_wallet_txindex(wallet: String) -> Node { Self::_new(Wallet::Load(wallet), true) }
4043

4144
/// Returns a handle to a `bitcoind` instance without any wallet loaded.
42-
fn new_no_wallet() -> Node {
43-
let mut conf = node::Conf::default();
44-
conf.wallet = None;
45-
Self::new(conf, None)
46-
}
45+
fn new_no_wallet() -> Node { Self::_new(Wallet::None, false) }
4746

4847
/// Returns a handle to a `bitcoind` instance without any wallet loaded and `-txindex` enabled.
49-
fn new_no_wallet_txindex() -> Node {
50-
let mut conf = node::Conf::default();
51-
conf.args.push("-txindex");
52-
Self::new(conf, None)
53-
}
48+
fn new_no_wallet_txindex() -> Node { Self::_new(Wallet::None, true) }
5449

5550
/// Generates [`NBLOCKS`] to an address controlled by the loaded wallet.
5651
fn fund_wallet(&self);
@@ -76,11 +71,18 @@ pub trait NodeExt {
7671
}
7772

7873
impl NodeExt for Node {
79-
fn new(mut conf: node::Conf, wallet: Option<String>) -> Node {
74+
fn _new(wallet: Wallet, txindex: bool) -> Node {
8075
let exe = node::exe_path().expect("failed to get bitcoind executable");
8176

82-
if let Some(wallet) = wallet {
83-
conf.wallet = Some(wallet);
77+
let mut conf = node::Conf::default();
78+
match wallet {
79+
Wallet::Default => {}, // conf.wallet = Some("default")
80+
Wallet::Load(w) => conf.wallet = Some(w),
81+
Wallet::None => conf.wallet = None,
82+
}
83+
84+
if txindex {
85+
conf.args.push("-txindex");
8486
}
8587

8688
Node::with_conf(exe, &conf).expect("failed to create node")

integration_test/tests/blockchain.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
55
use integration_test::{Node, NodeExt as _};
66

7-
// FIXME: Do we need this?
8-
fn best_block_hash() -> bitcoin::BlockHash {
9-
let node = Node::new_no_wallet();
10-
node.client.best_block_hash().expect("best_block_hash failed")
11-
}
12-
137
#[test]
148
fn get_blockchain_info() {
159
let node = Node::new_no_wallet();
@@ -27,7 +21,7 @@ fn get_best_block_hash() {
2721
#[test]
2822
fn get_block() {
2923
let node = Node::new_no_wallet();
30-
let block_hash = best_block_hash();
24+
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
3125

3226
let json = node.client.get_block_verbose_zero(block_hash).expect("getblock verbose=0");
3327
assert!(json.into_model().is_ok());
@@ -57,23 +51,19 @@ fn get_block_hash() {
5751
#[test]
5852
fn get_block_header() { // verbose = false
5953
let node = Node::new_no_wallet();
60-
let block_hash = best_block_hash();
54+
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
6155
let json = node.client.get_block_header(&block_hash).expect("getblockheader");
6256
assert!(json.into_model().is_ok());
6357
}
6458

6559
#[test]
6660
fn get_block_header_verbose() { // verbose = true
6761
let node = Node::new_no_wallet();
68-
let block_hash = best_block_hash();
62+
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
6963
let json = node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
7064
assert!(json.into_model().is_ok());
7165
}
7266

73-
#[cfg(not(any(feature = "v19", feature = "v20", feature = "v21", feature = "v22", feature = "v23", feature = "v24")))]
74-
// `getblockstats` used to not work on the genesis block as it doesn't have undo data saved to disk
75-
// (see https://github.com/bitcoin/bitcoin/pull/19888). We therefore only run tests for versions
76-
// allowing to.
7767
#[test]
7868
fn get_block_stats() {
7969
// Version 18 cannot getblockstats if -txindex is not enabled.
@@ -84,28 +74,26 @@ fn get_block_stats() {
8474
getblockstats_txindex();
8575
}
8676

87-
#[cfg(not(any(feature = "v18", feature = "v19", feature = "v20", feature = "v21", feature = "v22", feature = "v23", feature = "v24")))]
8877
fn getblockstats() {
8978
let node = Node::new_with_default_wallet();
90-
node.mine_a_block();
79+
node.fund_wallet();
9180

9281
let json = node.client.get_block_stats_by_height(1).expect("getblockstats");
9382
assert!(json.into_model().is_ok());
9483

95-
let block_hash = best_block_hash();
84+
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
9685
let json = node.client.get_block_stats_by_block_hash(&block_hash).expect("getblockstats");
9786
assert!(json.into_model().is_ok());
9887
}
9988

100-
#[cfg(not(any(feature = "v19", feature = "v20", feature = "v21", feature = "v22", feature = "v23", feature = "v24")))]
10189
fn getblockstats_txindex() {
10290
let node = Node::new_with_default_wallet_txindex();
103-
node.mine_a_block();
91+
node.fund_wallet();
10492

105-
let json = node.client.get_block_stats_by_height(1).expect("getblockstats");
93+
let json = node.client.get_block_stats_by_height(101).expect("getblockstats");
10694
assert!(json.into_model().is_ok());
10795

108-
let block_hash = best_block_hash();
96+
let block_hash = node.client.best_block_hash().expect("best_block_hash failed");
10997
let json = node.client.get_block_stats_by_block_hash(&block_hash).expect("getblockstats");
11098
assert!(json.into_model().is_ok());
11199
}
@@ -234,7 +222,7 @@ fn get_tx_out_set_info() {
234222
fn precious_block() {
235223
let node = Node::new_with_default_wallet();
236224
node.mine_a_block();
237-
let hash = node.client.best_block_hash().expect("bestblockhash");
225+
let hash = node.client.best_block_hash().expect("best_block_hash failed");
238226
node.mine_a_block();
239227

240228
let _ = node.client.precious_block(hash).expect("preciousblock");

types/src/v19/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! | getblockfilter | todo |
2525
//! | getblockhash | done |
2626
//! | getblockheader | done |
27-
//! | getblockstats | done (untested) |
27+
//! | getblockstats | done |
2828
//! | getchaintips | done |
2929
//! | getchaintxstats | done |
3030
//! | getdifficulty | done |

types/src/v20/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! | getblockfilter | todo |
2525
//! | getblockhash | done |
2626
//! | getblockheader | done |
27-
//! | getblockstats | done (untested) |
27+
//! | getblockstats | done |
2828
//! | getchaintips | done |
2929
//! | getchaintxstats | done |
3030
//! | getdifficulty | done |

types/src/v21/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! | getblockfilter | todo |
2525
//! | getblockhash | done |
2626
//! | getblockheader | done |
27-
//! | getblockstats | done (untested) |
27+
//! | getblockstats | done |
2828
//! | getchaintips | done |
2929
//! | getchaintxstats | done |
3030
//! | getdifficulty | done |

types/src/v22/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! | getblockfilter | todo |
2525
//! | getblockhash | done |
2626
//! | getblockheader | done |
27-
//! | getblockstats | done (untested) |
27+
//! | getblockstats | done |
2828
//! | getchaintips | done |
2929
//! | getchaintxstats | done |
3030
//! | getdifficulty | done |

types/src/v23/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! | getblockfrompeer | todo |
2626
//! | getblockhash | done |
2727
//! | getblockheader | done |
28-
//! | getblockstats | done (untested) |
28+
//! | getblockstats | done |
2929
//! | getchaintips | done |
3030
//! | getchaintxstats | done |
3131
//! | getdeploymentinfo | todo |

types/src/v24/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! | getblockfrompeer | todo |
2626
//! | getblockhash | done |
2727
//! | getblockheader | done |
28-
//! | getblockstats | done (untested) |
28+
//! | getblockstats | done |
2929
//! | getchaintips | done |
3030
//! | getchaintxstats | done |
3131
//! | getdeploymentinfo | todo |

types/src/v25/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! | getblockfrompeer | todo |
2626
//! | getblockhash | done |
2727
//! | getblockheader | done |
28-
//! | getblockstats | done (untested) |
28+
//! | getblockstats | done |
2929
//! | getchaintips | done |
3030
//! | getchaintxstats | done |
3131
//! | getdeploymentinfo | todo |

types/src/v26/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! | getblockfrompeer | todo |
2727
//! | getblockhash | done |
2828
//! | getblockheader | done |
29-
//! | getblockstats | done (untested) |
29+
//! | getblockstats | done |
3030
//! | getchainstates | todo |
3131
//! | getchaintips | done |
3232
//! | getchaintxstats | done |

0 commit comments

Comments
 (0)