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 2 commits 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 @@ -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/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 @@ -44,6 +44,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 @@ -31,9 +31,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
51 changes: 24 additions & 27 deletions integration_test/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use bitcoin::address::{Address, NetworkChecked};
use bitcoin::Amount;
use integration_test::{Node, NodeExt as _, Wallet};
use node::AddressType;
use node::vtype::*; // All the version specific types.
use node::mtype;
use node::vtype::*; // All the version specific types.
use node::{mtype, AddressType};

#[test]
#[cfg(feature = "TODO")]
Expand Down Expand Up @@ -59,16 +58,15 @@ fn wallet__create_wallet__modelled() {
fn wallet__dump_priv_key__modelled() {
// As of Core v23 the default wallet is an native descriptor wallet which does not
// support dumping private keys. Legacy wallets are supported upto v25 it seems.
#[cfg(any(
feature = "v23",
feature = "v24",
feature = "v25",
))]
#[cfg(any(feature = "v23", feature = "v24", feature = "v25",))]
{
let node = Node::with_wallet(Wallet::None, &[]);

node.client.create_legacy_wallet("legacy_wallet").expect("legacy create_wallet");
let address = node.client.get_new_address(Some("label"), Some(AddressType::Legacy)).expect("legacy get_new_address");
let address = node
.client
.get_new_address(Some("label"), Some(AddressType::Legacy))
.expect("legacy get_new_address");
let address = address.into_model().unwrap().0.assume_checked();

let json: DumpPrivKey = node.client.dump_priv_key(&address).expect("dumpprivkey");
Expand Down Expand Up @@ -98,11 +96,7 @@ fn wallet__dump_priv_key__modelled() {
fn wallet__dump_wallet() {
// As of Core v23 the default wallet is an native descriptor wallet which does not
// support dumping private keys. Legacy wallets are supported upto v25 it seems.
#[cfg(any(
feature = "v23",
feature = "v24",
feature = "v25",
))]
#[cfg(any(feature = "v23", feature = "v24", feature = "v25",))]
{
let node = Node::with_wallet(Wallet::None, &[]);

Expand Down Expand Up @@ -134,7 +128,8 @@ fn wallet__get_addresses_by_label__modelled() {
let label = "some-label";
let addr = node.client.new_address_with_label(label).expect("failed to get new address");

let json: GetAddressesByLabel = node.client.get_addresses_by_label(label).expect("getaddressesbylabel");
let json: GetAddressesByLabel =
node.client.get_addresses_by_label(label).expect("getaddressesbylabel");
let model: Result<mtype::GetAddressesByLabel, _> = json.into_model();
let map = model.unwrap();

Expand All @@ -144,8 +139,8 @@ fn wallet__get_addresses_by_label__modelled() {
}

#[test]
#[cfg(feature = "TODO")] // FIXME: The types are broken.
// TODO: Consider testing a few different address types.
#[cfg(feature = "TODO")] // FIXME: The types are broken.
// TODO: Consider testing a few different address types.
fn wallet__get_address_info__modelled() {
let node = Node::with_wallet(Wallet::Default, &[]);
let address = node.client.new_address().expect("failed to create new address");
Expand All @@ -169,7 +164,6 @@ fn wallet__get_balance__modelled() {
json.into_model().unwrap();
}


#[test]
#[cfg(all(not(feature = "v17"), not(feature = "v18")))]
fn wallet__get_balances() {
Expand Down Expand Up @@ -197,7 +191,8 @@ fn wallet__get_new_address__modelled() {
#[test]
fn wallet__get_raw_change_address__modelled() {
let node = Node::with_wallet(Wallet::Default, &[]);
let json: GetRawChangeAddress = node.client.get_raw_change_address().expect("getrawchangeaddress");
let json: GetRawChangeAddress =
node.client.get_raw_change_address().expect("getrawchangeaddress");
let model: Result<mtype::GetRawChangeAddress, _> = json.into_model();
model.unwrap();
}
Expand All @@ -214,7 +209,8 @@ fn wallet__get_received_by_address__modelled() {
node.client.send_to_address(&address, amount).expect("sendtoaddress").txid().unwrap();
node.mine_a_block();

let json: GetReceivedByAddress = node.client.get_received_by_address(&address).expect("getreceivedbyaddress");
let json: GetReceivedByAddress =
node.client.get_received_by_address(&address).expect("getreceivedbyaddress");
let model: Result<mtype::GetReceivedByAddress, _> = json.into_model();
let model = model.unwrap();

Expand All @@ -240,18 +236,14 @@ fn wallet__get_transaction__modelled() {
}

#[test]
fn wallet__load_wallet__modelled() {
create_load_unload_wallet();
}
fn wallet__load_wallet__modelled() { create_load_unload_wallet(); }

// This is tested in raw_transactions.rs `create_sign_send()`.
#[test]
fn wallet__sign_raw_transaction_with_wallet__modelled() {}

#[test]
fn wallet__unload_wallet() {
create_load_unload_wallet();
}
fn wallet__unload_wallet() { create_load_unload_wallet(); }

#[test]
fn wallet__send_to_address__modelled() {
Expand All @@ -276,7 +268,12 @@ fn create_load_unload_wallet() {
let _ = node.client.unload_wallet(&wallet).expect("unloadwallet");

// From version 21 Core returns warnings for `unloadwallet`.
#[cfg(all(not(feature = "v17"), not(feature = "v18"), not(feature = "v19"), not(feature = "v20")))]
#[cfg(all(
not(feature = "v17"),
not(feature = "v18"),
not(feature = "v19"),
not(feature = "v20")
))]
{
let json: UnloadWallet = node.client.unload_wallet(&wallet).expect("unloadwallet");
let _: mtype::UnloadWallet = json.into_model();
Expand Down
Loading
Loading