Add testmempoolaccept endpoint

This commit is contained in:
Mononaut 2024-03-23 09:08:45 +00:00
parent d4f788fc3d
commit 055ac9f4ab
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E
3 changed files with 43 additions and 1 deletions

View File

@ -117,6 +117,26 @@ struct NetworkInfo {
relayfee: f64, // in BTC/kB
}
#[derive(Serialize, Deserialize, Debug)]
struct MempoolFees {
base: f64,
#[serde(rename = "effective-feerate")]
effective_feerate: f64,
#[serde(rename = "effective-includes")]
effective_includes: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MempoolAcceptResult {
txid: String,
wtxid: String,
allowed: Option<bool>,
vsize: Option<u32>,
fees: Option<MempoolFees>,
#[serde(rename = "reject-reason")]
reject_reason: Option<String>,
}
pub trait CookieGetter: Send + Sync {
fn get(&self) -> Result<Vec<u8>>;
}
@ -582,6 +602,12 @@ impl Daemon {
.chain_err(|| "failed to parse txid")
}
pub fn test_mempool_accept(&self, txhex: Vec<String>) -> Result<Vec<MempoolAcceptResult>> {
let result = self.request("testmempoolaccept", json!([txhex]))?;
serde_json::from_value::<Vec<MempoolAcceptResult>>(result)
.chain_err(|| "invalid testmempoolaccept reply")
}
// Get estimated feerates for the provided confirmation targets using a batch RPC request
// Missing estimates are logged but do not cause a failure, whatever is available is returned
#[allow(clippy::float_cmp)]

View File

@ -6,7 +6,7 @@ use std::time::{Duration, Instant};
use crate::chain::{Network, OutPoint, Transaction, TxOut, Txid};
use crate::config::Config;
use crate::daemon::Daemon;
use crate::daemon::{Daemon, MempoolAcceptResult};
use crate::errors::*;
use crate::new_index::{ChainQuery, Mempool, ScriptStats, SpendingInput, Utxo};
use crate::util::{is_spendable, BlockId, Bytes, TransactionStatus};
@ -87,6 +87,10 @@ impl Query {
Ok(txid)
}
pub fn test_mempool_accept(&self, txhex: Vec<String>) -> Result<Vec<MempoolAcceptResult>> {
self.daemon.test_mempool_accept(txhex)
}
pub fn utxo(&self, scripthash: &[u8]) -> Result<Vec<Utxo>> {
let mut utxos = self.chain.utxo(
scripthash,

View File

@ -1202,6 +1202,18 @@ fn handle_request(
.map_err(|err| HttpError::from(err.description().to_string()))?;
http_message(StatusCode::OK, txid.to_hex(), 0)
}
(&Method::POST, Some(&"txs"), Some(&"test"), None, None, None) => {
let txhexes: Vec<String> = String::from_utf8(body.to_vec())?
.split(',')
.map(|s| s.to_string())
.collect();
let result = query
.test_mempool_accept(txhexes)
.map_err(|err| HttpError::from(err.description().to_string()))?;
json_response(result, TTL_SHORT)
}
(&Method::GET, Some(&"txs"), Some(&"outspends"), None, None, None) => {
let txid_strings: Vec<&str> = query_params
.get("txids")