Skip to content

Commit cec8afd

Browse files
committed
Implement pruneblock method and test
1 parent f0586c0 commit cec8afd

Some content is hidden

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

45 files changed

+161
-96
lines changed

client/src/client_sync/v17/blockchain.rs

+13
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@ macro_rules! impl_client_v17__preciousblock {
289289
};
290290
}
291291

292+
/// Implements Bitcoin Core JSON-RPC API method `pruneblockchain`
293+
#[macro_export]
294+
macro_rules! impl_client_v17__pruneblockchain {
295+
() => {
296+
impl Client {
297+
/// Instructs the node to prune the blockchain up to a specified height or timestamp.
298+
pub fn prune_blockchain(&self, target: u64) -> Result<PruneBlockchain> {
299+
self.call("pruneblockchain", &[target.into()])
300+
}
301+
}
302+
};
303+
}
304+
292305
/// Implements Bitcoin Core JSON-RPC API method `verifytxoutproof`
293306
#[macro_export]
294307
macro_rules! impl_client_v17__verifytxoutproof {

client/src/client_sync/v17/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ crate::impl_client_v17__gettxout!();
4848
crate::impl_client_v17__gettxoutproof!();
4949
crate::impl_client_v17__gettxoutsetinfo!();
5050
crate::impl_client_v17__preciousblock!();
51+
crate::impl_client_v17__pruneblockchain!();
5152
crate::impl_client_v17__verifytxoutproof!();
5253

5354
// == Control ==

client/src/client_sync/v18/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ crate::impl_client_v17__gettxoutproof!();
4242
crate::impl_client_v17__gettxoutsetinfo!();
4343
crate::impl_client_v17__preciousblock!();
4444
crate::impl_client_v17__verifytxoutproof!();
45+
crate::impl_client_v17__pruneblockchain!();
4546

4647
// == Control ==
4748
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v19/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
4444
crate::impl_client_v17__gettxoutsetinfo!();
4545
crate::impl_client_v17__preciousblock!();
4646
crate::impl_client_v17__verifytxoutproof!();
47+
crate::impl_client_v17__pruneblockchain!();
4748

4849
// == Control ==
4950
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v20.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
4141
crate::impl_client_v17__gettxoutsetinfo!();
4242
crate::impl_client_v17__preciousblock!();
4343
crate::impl_client_v17__verifytxoutproof!();
44+
crate::impl_client_v17__pruneblockchain!();
4445

4546
// == Control ==
4647
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v21/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ crate::impl_client_v17__gettxoutproof!();
4343
crate::impl_client_v17__gettxoutsetinfo!();
4444
crate::impl_client_v17__preciousblock!();
4545
crate::impl_client_v17__verifytxoutproof!();
46+
crate::impl_client_v17__pruneblockchain!();
4647

4748
// == Control ==
4849
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v22/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
4444
crate::impl_client_v17__gettxoutsetinfo!();
4545
crate::impl_client_v17__preciousblock!();
4646
crate::impl_client_v17__verifytxoutproof!();
47+
crate::impl_client_v17__pruneblockchain!();
4748

4849
// == Control ==
4950
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v23/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
4444
crate::impl_client_v17__gettxoutsetinfo!();
4545
crate::impl_client_v17__preciousblock!();
4646
crate::impl_client_v17__verifytxoutproof!();
47+
crate::impl_client_v17__pruneblockchain!();
4748

4849
// == Control ==
4950
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v24.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
4141
crate::impl_client_v17__gettxoutsetinfo!();
4242
crate::impl_client_v17__preciousblock!();
4343
crate::impl_client_v17__verifytxoutproof!();
44+
crate::impl_client_v17__pruneblockchain!();
4445

4546
// == Control ==
4647
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v25.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
4141
crate::impl_client_v17__gettxoutsetinfo!();
4242
crate::impl_client_v17__preciousblock!();
4343
crate::impl_client_v17__verifytxoutproof!();
44+
crate::impl_client_v17__pruneblockchain!();
4445

4546
// == Control ==
4647
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v26/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
4444
crate::impl_client_v26__gettxoutsetinfo!();
4545
crate::impl_client_v17__preciousblock!();
4646
crate::impl_client_v17__verifytxoutproof!();
47+
crate::impl_client_v17__pruneblockchain!();
4748

4849
// == Control ==
4950
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v27.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
4141
crate::impl_client_v26__gettxoutsetinfo!();
4242
crate::impl_client_v17__preciousblock!();
4343
crate::impl_client_v17__verifytxoutproof!();
44+
crate::impl_client_v17__pruneblockchain!();
4445

4546
// == Control ==
4647
crate::impl_client_v17__getmemoryinfo!();

client/src/client_sync/v28/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ crate::impl_client_v17__gettxoutproof!();
4343
crate::impl_client_v26__gettxoutsetinfo!();
4444
crate::impl_client_v17__preciousblock!();
4545
crate::impl_client_v17__verifytxoutproof!();
46+
crate::impl_client_v17__pruneblockchain!();
4647

4748
// == Control ==
4849
crate::impl_client_v17__getmemoryinfo!();

integration_test/tests/blockchain.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use integration_test::{Node, NodeExt as _, Wallet};
88
use node::client::client_sync;
9-
use node::vtype::*; // All the version specific types.
109
use node::mtype;
10+
use node::vtype::*; // All the version specific types.
1111

1212
#[test]
1313
fn blockchain__get_best_block_hash__modelled() {
@@ -27,7 +27,8 @@ fn blockchain__get_block__modelled() {
2727
let model: Result<mtype::GetBlockVerboseZero, _> = json.into_model();
2828
model.expect("GetBlock into model");
2929

30-
let json: GetBlockVerboseOne = node.client.get_block_verbose_one(block_hash).expect("getblock verbose=1");
30+
let json: GetBlockVerboseOne =
31+
node.client.get_block_verbose_one(block_hash).expect("getblock verbose=1");
3132
let model: Result<mtype::GetBlockVerboseOne, GetBlockVerboseOneError> = json.into_model();
3233
model.expect("GetBlockVerbose into model");
3334

@@ -86,7 +87,8 @@ fn blockchain__get_block_header__modelled() {
8687
model.unwrap();
8788

8889
// verbose = true
89-
let json:GetBlockHeaderVerbose = node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
90+
let json: GetBlockHeaderVerbose =
91+
node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
9092
let model: Result<mtype::GetBlockHeaderVerbose, GetBlockHeaderVerboseError> = json.into_model();
9193
model.unwrap();
9294
}
@@ -290,3 +292,25 @@ fn verify_tx_out_proof(node: &Node) -> Result<(), client_sync::Error> {
290292

291293
Ok(())
292294
}
295+
296+
#[test]
297+
fn blockchain__prune_blockchain() {
298+
const NBLOCKS: usize = 1;
299+
300+
let node = Node::with_wallet(Wallet::Default, &["-prune=550"]);
301+
let address = node.client.new_address().expect("Failed to get new address");
302+
303+
let gen_result = node
304+
.client
305+
.generate_to_address(NBLOCKS, &address)
306+
.expect("generate_to_address RPC call failed");
307+
assert_eq!(
308+
gen_result.0.len(),
309+
NBLOCKS,
310+
"generate_to_address did not return the expected number of block hashes"
311+
);
312+
313+
let target_height: u64 = 500;
314+
315+
let _: Result<PruneBlockchain, _> = node.client.prune_blockchain(target_height);
316+
}

integration_test/tests/control.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(non_snake_case)] // Test names intentionally use double underscore.
66

77
use integration_test::{Node, NodeExt as _, Wallet};
8-
use node::vtype::*; // All the version specific types.
8+
use node::vtype::*; // All the version specific types.
99

1010
#[test]
1111
fn control__get_memory_info() {

integration_test/tests/generating.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![allow(non_snake_case)] // Test names intentionally use double underscore.
66

77
use integration_test::{Node, NodeExt as _, Wallet};
8-
use node::vtype::*; // All the version specific types.
98
use node::mtype;
9+
use node::vtype::*; // All the version specific types.
1010

1111
#[test]
1212
// The `generate` method was deprecated in Core v18 and was removed in v19.
@@ -28,9 +28,10 @@ fn generating__generate_to_address__modelled() {
2828
let node = Node::with_wallet(Wallet::Default, &[]);
2929
let address = node.client.new_address().expect("failed to get new address");
3030

31-
let json: GenerateToAddress = node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
31+
let json: GenerateToAddress =
32+
node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
3233

33-
let model: Result<mtype::GenerateToAddress, _> = json.into_model();
34+
let model: Result<mtype::GenerateToAddress, _> = json.into_model();
3435
let _ = model.unwrap();
3536
}
3637

integration_test/tests/mining.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use bitcoin::SignedAmount;
88
use integration_test::{Node, NodeExt as _, Wallet};
99
use node::client::client_sync::{TemplateRequest, TemplateRules};
10-
use node::vtype::*; // All the version specific types.
1110
use node::mtype;
11+
use node::vtype::*; // All the version specific types.
1212

1313
#[test]
1414
fn mining__get_block_template__modelled() {
@@ -72,7 +72,7 @@ fn mining__prioritise_transaction() {
7272
}
7373

7474
#[test]
75-
#[cfg(feature = "TODO")] // This test is flaky - no clue why.
75+
#[cfg(feature = "TODO")] // This test is flaky - no clue why.
7676
fn mining__submit_block() {
7777
// Requires connected nodes otherwise the RPC call errors.
7878
let (node1, node2, node3) = integration_test::three_node_network();
@@ -96,8 +96,8 @@ fn mining__submit_block() {
9696
fn submit_empty_block(node: &Node, bt: &mtype::GetBlockTemplate) {
9797
use bitcoin::hashes::Hash as _;
9898
use bitcoin::{
99-
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, Sequence,
100-
Transaction, TxIn, TxOut, Witness, ScriptHash, TxMerkleNode,
99+
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, ScriptHash, Sequence,
100+
Transaction, TxIn, TxMerkleNode, TxOut, Witness,
101101
};
102102

103103
let txdata = vec![Transaction {
@@ -124,7 +124,10 @@ fn submit_empty_block(node: &Node, bt: &mtype::GetBlockTemplate) {
124124
version: block::Version::default(),
125125
prev_blockhash: bt.previous_block_hash,
126126
merkle_root: TxMerkleNode::all_zeros(),
127-
time: Ord::max(bt.min_time, std::time::UNIX_EPOCH.elapsed().expect("elapsed").as_secs() as u32) as u32,
127+
time: Ord::max(
128+
bt.min_time,
129+
std::time::UNIX_EPOCH.elapsed().expect("elapsed").as_secs() as u32,
130+
) as u32,
128131
bits: bt.bits,
129132
nonce: 0,
130133
},
@@ -148,8 +151,8 @@ fn submit_empty_block(node: &Node, bt: &mtype::GetBlockTemplate) {
148151
fn mining__submit_block_with_dummy_coinbase(node: &Node, bt: &mtype::GetBlockTemplate) {
149152
use bitcoin::hashes::Hash as _;
150153
use bitcoin::{
151-
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, Sequence,
152-
Transaction, TxIn, TxOut, Witness, TxMerkleNode,
154+
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, Sequence, Transaction,
155+
TxIn, TxMerkleNode, TxOut, Witness,
153156
};
154157

155158
let address = node.client.new_address().expect("failed to get new address");

integration_test/tests/network.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![allow(non_snake_case)] // Test names intentionally use double underscore.
66

77
use integration_test::{Node, NodeExt as _, Wallet};
8-
use node::vtype::*; // All the version specific types.
98
use node::mtype;
9+
use node::vtype::*; // All the version specific types.
1010

1111
#[test]
1212
fn network__get_added_node_info() {

integration_test/tests/raw_transactions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use std::collections::BTreeMap;
88

9-
use bitcoin::{Amount, Sequence};
109
#[cfg(feature = "TODO")]
11-
use bitcoin::{absolute, transaction, consensus, TxOut, Transaction};
10+
use bitcoin::{absolute, consensus, transaction, Transaction, TxOut};
11+
use bitcoin::{Amount, Sequence};
1212
use integration_test::{Node, NodeExt as _, Wallet};
1313
use node::client::client_sync::Input;
1414

0 commit comments

Comments
 (0)