Skip to content

Implement pruneblock method and test #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions client/src/client_sync/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ macro_rules! impl_client_v17__preciousblock {
};
}

/// Implements Bitcoin Core JSON-RPC API method `pruneblockchain`
#[macro_export]
macro_rules! impl_client_v17__pruneblockchain {
() => {
impl Client {
/// Instructs the node to prune the blockchain up to a specified height or timestamp.
pub fn prune_blockchain(&self, target: u64) -> Result<PruneBlockchain> {
self.call("pruneblockchain", &[target.into()])
}
}
};
}

/// Implements Bitcoin Core JSON-RPC API method `verifytxoutproof`
#[macro_export]
macro_rules! impl_client_v17__verifytxoutproof {
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v18/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v19/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v21/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ crate::impl_client_v17__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v22/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v23/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v26/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v27.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
1 change: 1 addition & 0 deletions client/src/client_sync/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ crate::impl_client_v22__gettxout!();
crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
30 changes: 27 additions & 3 deletions integration_test/tests/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use integration_test::{Node, NodeExt as _, Wallet};
use node::client::client_sync;
use node::vtype::*; // All the version specific types.
use node::mtype;
use node::vtype::*; // All the version specific types.

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

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

Expand Down Expand Up @@ -86,7 +87,8 @@ fn blockchain__get_block_header__modelled() {
model.unwrap();

// verbose = true
let json:GetBlockHeaderVerbose = node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
let json: GetBlockHeaderVerbose =
node.client.get_block_header_verbose(&block_hash).expect("getblockheader");
let model: Result<mtype::GetBlockHeaderVerbose, GetBlockHeaderVerboseError> = json.into_model();
model.unwrap();
}
Expand Down Expand Up @@ -290,3 +292,25 @@ fn verify_tx_out_proof(node: &Node) -> Result<(), client_sync::Error> {

Ok(())
}

#[test]
fn blockchain__prune_blockchain() {
const NBLOCKS: usize = 1;

let node = Node::with_wallet(Wallet::Default, &["-prune=550"]);
let address = node.client.new_address().expect("Failed to get new address");

let gen_result = node
.client
.generate_to_address(NBLOCKS, &address)
.expect("generate_to_address RPC call failed");
assert_eq!(
gen_result.0.len(),
NBLOCKS,
"generate_to_address did not return the expected number of block hashes"
);

let target_height: u64 = 500;

let _: Result<PruneBlockchain, _> = node.client.prune_blockchain(target_height);
}
2 changes: 1 addition & 1 deletion integration_test/tests/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(non_snake_case)] // Test names intentionally use double underscore.

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

#[test]
fn control__get_memory_info() {
Expand Down
7 changes: 4 additions & 3 deletions integration_test/tests/generating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![allow(non_snake_case)] // Test names intentionally use double underscore.

use integration_test::{Node, NodeExt as _, Wallet};
use node::vtype::*; // All the version specific types.
use node::mtype;
use node::vtype::*; // All the version specific types.

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

let json: GenerateToAddress = node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");
let json: GenerateToAddress =
node.client.generate_to_address(NBLOCKS, &address).expect("generatetoaddress");

let model: Result<mtype::GenerateToAddress, _> = json.into_model();
let model: Result<mtype::GenerateToAddress, _> = json.into_model();
let _ = model.unwrap();
}

Expand Down
17 changes: 10 additions & 7 deletions integration_test/tests/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use bitcoin::SignedAmount;
use integration_test::{Node, NodeExt as _, Wallet};
use node::client::client_sync::{TemplateRequest, TemplateRules};
use node::vtype::*; // All the version specific types.
use node::mtype;
use node::vtype::*; // All the version specific types.

#[test]
fn mining__get_block_template__modelled() {
Expand Down Expand Up @@ -72,7 +72,7 @@ fn mining__prioritise_transaction() {
}

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

let txdata = vec![Transaction {
Expand All @@ -124,7 +124,10 @@ fn submit_empty_block(node: &Node, bt: &mtype::GetBlockTemplate) {
version: block::Version::default(),
prev_blockhash: bt.previous_block_hash,
merkle_root: TxMerkleNode::all_zeros(),
time: Ord::max(bt.min_time, std::time::UNIX_EPOCH.elapsed().expect("elapsed").as_secs() as u32) as u32,
time: Ord::max(
bt.min_time,
std::time::UNIX_EPOCH.elapsed().expect("elapsed").as_secs() as u32,
) as u32,
bits: bt.bits,
nonce: 0,
},
Expand All @@ -148,8 +151,8 @@ fn submit_empty_block(node: &Node, bt: &mtype::GetBlockTemplate) {
fn mining__submit_block_with_dummy_coinbase(node: &Node, bt: &mtype::GetBlockTemplate) {
use bitcoin::hashes::Hash as _;
use bitcoin::{
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, Sequence,
Transaction, TxIn, TxOut, Witness, TxMerkleNode,
absolute, block, transaction, Amount, Block, OutPoint, ScriptBuf, Sequence, Transaction,
TxIn, TxMerkleNode, TxOut, Witness,
};

let address = node.client.new_address().expect("failed to get new address");
Expand Down
2 changes: 1 addition & 1 deletion integration_test/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![allow(non_snake_case)] // Test names intentionally use double underscore.

use integration_test::{Node, NodeExt as _, Wallet};
use node::vtype::*; // All the version specific types.
use node::mtype;
use node::vtype::*; // All the version specific types.

#[test]
fn network__get_added_node_info() {
Expand Down
4 changes: 2 additions & 2 deletions integration_test/tests/raw_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use std::collections::BTreeMap;

use bitcoin::{Amount, Sequence};
#[cfg(feature = "TODO")]
use bitcoin::{absolute, transaction, consensus, TxOut, Transaction};
use bitcoin::{absolute, consensus, transaction, Transaction, TxOut};
use bitcoin::{Amount, Sequence};
use integration_test::{Node, NodeExt as _, Wallet};
use node::client::client_sync::Input;

Expand Down
Loading