Skip to content

Commit 2816af0

Browse files
committed
Add support to testmempoolaccept RPC
1 parent 875fb61 commit 2816af0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/daemon.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ impl Daemon {
173173
.context("failed to broadcast transaction")
174174
}
175175

176+
pub(crate) fn test_mempool_accept(
177+
&self,
178+
txs: &[&Transaction],
179+
) -> Result<Vec<json::TestMempoolAcceptResult>> {
180+
self.rpc
181+
.test_mempool_accept(txs)
182+
.context("failed to broadcast transaction")
183+
}
184+
176185
pub(crate) fn get_transaction_info(
177186
&self,
178187
txid: &Txid,

src/electrum.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
types::ScriptHash,
2727
};
2828

29-
const PROTOCOL_VERSION: &str = "1.4";
29+
const PROTOCOL_VERSION: &str = "1.5";
3030
const UNKNOWN_FEE: isize = -1; // (allowed by Electrum protocol)
3131

3232
const UNSUBSCRIBED_QUERY_MESSAGE: &str = "your wallet uses less efficient method of querying electrs, consider contacting the developer of your wallet. Reason:";
@@ -364,6 +364,26 @@ impl Rpc {
364364
Ok(json!(txid))
365365
}
366366

367+
fn testmempoolaccept(&self, (txs_hex,): &(String,)) -> Result<Value> {
368+
let tx_hex_vec: Vec<String> = txs_hex.split(',').map(|s| s.trim().to_string()).collect();
369+
370+
let txs = tx_hex_vec
371+
.iter()
372+
.map(|tx_hex| {
373+
let tx_bytes = Vec::from_hex(tx_hex).context("non-hex transaction")?;
374+
let tx = deserialize(&tx_bytes).context("invalid transaction")?;
375+
Ok(tx)
376+
})
377+
.collect::<Result<Vec<bitcoin::Transaction>>>()?;
378+
379+
let txs_ref: Vec<&bitcoin::Transaction> = txs.iter().collect();
380+
let txs_arr = txs_ref.as_slice();
381+
382+
let result = self.daemon.test_mempool_accept(txs_arr).unwrap();
383+
384+
Ok(json!(result))
385+
}
386+
367387
fn transaction_get(&self, args: &TxGetArgs) -> Result<Value> {
368388
let (txid, verbose) = args.into();
369389
if verbose {
@@ -557,6 +577,7 @@ impl Rpc {
557577
Params::ScriptHashSubscribe(args) => self.scripthash_subscribe(client, args),
558578
Params::ScriptHashUnsubscribe(args) => self.scripthash_unsubscribe(client, args),
559579
Params::TransactionBroadcast(args) => self.transaction_broadcast(args),
580+
Params::TestMempoolAccept(args) => self.testmempoolaccept(args),
560581
Params::TransactionGet(args) => self.transaction_get(args),
561582
Params::TransactionGetMerkle(args) => self.transaction_get_merkle(args),
562583
Params::TransactionFromPosition(args) => self.transaction_from_pos(*args),
@@ -573,6 +594,7 @@ enum Params {
573594
BlockHeader((usize,)),
574595
BlockHeaders((usize, usize)),
575596
TransactionBroadcast((String,)),
597+
TestMempoolAccept((String,)),
576598
Donation,
577599
EstimateFee((u16,)),
578600
Features,
@@ -606,6 +628,7 @@ impl Params {
606628
"blockchain.scripthash.subscribe" => Params::ScriptHashSubscribe(convert(params)?),
607629
"blockchain.scripthash.unsubscribe" => Params::ScriptHashUnsubscribe(convert(params)?),
608630
"blockchain.transaction.broadcast" => Params::TransactionBroadcast(convert(params)?),
631+
"blockchain.mempool.testmempoolaccept" => Params::TestMempoolAccept(convert(params)?),
609632
"blockchain.transaction.get" => Params::TransactionGet(convert(params)?),
610633
"blockchain.transaction.get_merkle" => Params::TransactionGetMerkle(convert(params)?),
611634
"blockchain.transaction.id_from_pos" => {

0 commit comments

Comments
 (0)