Skip to content

Implement savemempool method and test #148

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

Merged
merged 1 commit into from
May 8, 2025
Merged
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
16 changes: 16 additions & 0 deletions client/src/client_sync/v17/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ macro_rules! impl_client_v17__pruneblockchain {
};
}

/// Implements Bitcoin Core JSON-RPC API method `savemempool`
#[macro_export]
macro_rules! impl_client_v17__savemempool {
() => {
impl Client {
pub fn save_mempool(&self) -> Result<()> {
match self.call("savemempool", &[]) {
Ok(serde_json::Value::Null) => Ok(()),
Ok(res) => Err(Error::Returned(res.to_string())),
Err(err) => Err(err.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 @@ -49,6 +49,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
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 @@ -43,6 +43,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
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 @@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
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 @@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
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 @@ -43,6 +43,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
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 @@ -44,6 +44,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v17__savemempool!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
19 changes: 19 additions & 0 deletions client/src/client_sync/v23/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: CC0-1.0

//! Macros for implementing JSON-RPC methods on a client.
//!
//! Specifically this is methods found under the `== Blockchain ==` section of the
//! API docs of Bitcoin Core `v0.23`.
//!
//! All macros require `Client` to be in scope.
//!
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
/// Implements Bitcoin Core JSON-RPC API method `savemempool`
#[macro_export]
macro_rules! impl_client_v23__savemempool {
() => {
impl Client {
pub fn save_mempool(&self) -> Result<SaveMempool> { self.call("savemempool", &[]) }
}
};
}
2 changes: 2 additions & 0 deletions client/src/client_sync/v23/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!
//! We ignore option arguments unless they effect the shape of the returned JSON data.

pub mod blockchain;
pub mod wallet;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -44,6 +45,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
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 @@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
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 @@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v17__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
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 @@ -45,6 +45,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
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 @@ -41,6 +41,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
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 @@ -43,6 +43,7 @@ crate::impl_client_v17__gettxoutproof!();
crate::impl_client_v26__gettxoutsetinfo!();
crate::impl_client_v17__preciousblock!();
crate::impl_client_v17__pruneblockchain!();
crate::impl_client_v23__savemempool!();
crate::impl_client_v17__verifytxoutproof!();

// == Control ==
Expand Down
31 changes: 31 additions & 0 deletions integration_test/tests/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,37 @@ fn blockchain__prune_blockchain() {
let _: Result<PruneBlockchain, _> = node.client.prune_blockchain(target_height);
}

#[test]
fn blockchain__savemempool() {
let node = Node::with_wallet(Wallet::Default, &[]);
node.fund_wallet();
let (_addr, _txid) = node.create_mempool_transaction();

#[cfg(any(
feature = "0_17_2",
feature = "0_18_1",
feature = "0_19_1",
feature = "0_20_2",
feature = "0_21_2",
feature = "22_1"
))]
{
node.client.save_mempool().expect("savemempool");
}

#[cfg(not(any(
feature = "0_17_2",
feature = "0_18_1",
feature = "0_19_1",
feature = "0_20_2",
feature = "0_21_2",
feature = "22_1"
)))]
{
let _: Result<SaveMempool, _> = node.client.save_mempool();
}
}

#[test]
fn blockchain__verify_tx_out_proof__modelled() {
let node = Node::with_wallet(Wallet::Default, &[]);
Expand Down
17 changes: 17 additions & 0 deletions types/src/v23/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: CC0-1.0

//! The JSON-RPC API for Bitcoin Core `v23` - blockchain.
//!
//! Types for methods found under the `== Blockchain ==` section of the API docs.
use serde::{Deserialize, Serialize};

/// Result of JSON-RPC method `savemempool`.
///
/// > savemempool
///
/// > Dumps the mempool to disk. It will fail until the previous dump is fully loaded.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct SaveMempool {
/// The directory and file where the mempool was saved.
pub filename: String,
}
10 changes: 7 additions & 3 deletions types/src/v23/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//! | gettxoutsetinfo | version + model | |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
//! | verifytxoutproof | version + model | |
Expand Down Expand Up @@ -236,11 +236,15 @@
//! </details>

// JSON-RPC types by API section.
mod blockchain;
mod raw_transactions;

#[doc(inline)]
pub use self::raw_transactions::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
pub use self::{
blockchain::SaveMempool,
raw_transactions::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
},
};
#[doc(inline)]
pub use crate::{
Expand Down
3 changes: 2 additions & 1 deletion types/src/v24/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! | gettxspendingprevout | version + model | TODO |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
//! | verifytxoutproof | version + model | |
Expand Down Expand Up @@ -294,4 +294,5 @@ pub use crate::{
},
v21::UnloadWallet,
v22::{GetTxOut, GetTxOutError, Logging, ScriptPubkey},
v23::SaveMempool,
};
3 changes: 2 additions & 1 deletion types/src/v25/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! | gettxspendingprevout | version + model | TODO |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scanblocks | version + model | TODO |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
Expand Down Expand Up @@ -290,6 +290,7 @@ pub use crate::{
SoftforkType,
},
v22::{GetTxOut, GetTxOutError, Logging, ScriptPubkey},
v23::SaveMempool,
v24::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
Expand Down
3 changes: 2 additions & 1 deletion types/src/v26/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! | loadtxoutset | version + model | TODO |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scanblocks | version + model | TODO |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
Expand Down Expand Up @@ -310,6 +310,7 @@ pub use crate::{
SoftforkType,
},
v22::{GetTxOut, GetTxOutError, Logging, ScriptPubkey},
v23::SaveMempool,
v24::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
Expand Down
3 changes: 2 additions & 1 deletion types/src/v27/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! | loadtxoutset | version + model | TODO |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scanblocks | version + model | TODO |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
Expand Down Expand Up @@ -294,6 +294,7 @@ pub use crate::{
SoftforkType,
},
v22::{GetTxOut, GetTxOutError, Logging, ScriptPubkey},
v23::SaveMempool,
v24::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
Expand Down
3 changes: 2 additions & 1 deletion types/src/v28/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! | loadtxoutset | version + model | TODO |
//! | preciousblock | returns nothing | |
//! | pruneblockchain | returns numeric | |
//! | savemempool | returns nothing | |
//! | savemempool | version | |
//! | scanblocks | version + model | TODO |
//! | scantxoutset | omitted | API marked as experimental |
//! | verifychain | returns boolean | |
Expand Down Expand Up @@ -300,6 +300,7 @@ pub use crate::{
MempoolEntryError, MempoolEntryFees, MempoolEntryFeesError, Softfork, SoftforkType,
},
v22::{GetTxOut, GetTxOutError, Logging, ScriptPubkey},
v23::SaveMempool,
v24::{
DecodePsbt, DecodePsbtError, GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
TaprootBip32Deriv, TaprootLeaf, TaprootScript, TaprootScriptPathSig,
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub const METHODS: &[Method] = &[
Method::new_modelled("gettxoutsetinfo", "GetTxOutSetInfo", "get_tx_out_set_info"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Method::new_modelled("verifytxoutproof", "VerifyTxOutProof", "verify_tx_out_proof"),
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const METHODS: &[Method] = &[
Method::new_modelled("gettxspendingprevout", "GetTxSpendingPrevout", "get_tx_spending_prevout"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Method::new_modelled("verifytxoutproof", "VerifyTxOutProof", "verify_tx_out_proof"),
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const METHODS: &[Method] = &[
Method::new_modelled("gettxspendingprevout", "GetTxSpendingPrevout", "get_tx_spending_prevout"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scanblocks", "ScanBlocks", "scan_blocks"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v26.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const METHODS: &[Method] = &[
Method::new_no_model("loadtxoutset", "LoadTxOutSet", "load_tx_out_set"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scanblocks", "ScanBlocks", "scan_blocks"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v27.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const METHODS: &[Method] = &[
Method::new_no_model("loadtxoutset", "LoadTxOutSet", "load_tx_out_set"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scanblocks", "ScanBlocks", "scan_blocks"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Expand Down
2 changes: 1 addition & 1 deletion verify/src/method/v28.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const METHODS: &[Method] = &[
Method::new_no_model("loadtxoutset", "LoadTxOutSet", "load_tx_out_set"),
Method::new_nothing("preciousblock", "precious_block"),
Method::new_no_model("pruneblockchain", "PruneBlockchain", "prune_blockchain"),
Method::new_nothing("savemempool", "save_mempool"),
Method::new_no_model("savemempool", "SaveMempool", "save_mempool"),
Method::new_modelled("scanblocks", "ScanBlocks", "scan_blocks"),
Method::new_modelled("scantxoutset", "ScanTxOutSet", "scan_tx_out_set"),
Method::new_bool("verifychain", "verify_chain"),
Expand Down