mempool: general improvements

This commit is contained in:
nym21
2026-04-28 18:46:37 +02:00
parent 66494c081c
commit f1749472e7
95 changed files with 2545 additions and 2670 deletions
@@ -4,6 +4,8 @@ use schemars::JsonSchema;
use brk_types::Txid;
const MAX_TXIDS: usize = 250;
/// Query parameter for transaction-times endpoint.
#[derive(JsonSchema)]
pub struct TxidsParam {
@@ -24,6 +26,9 @@ impl TxidsParam {
format!("malformed query parameter `{pair}`, expected `txId[]=<txid>`")
})?;
if key == "txId[]" || key == "txId%5B%5D" {
if txids.len() == MAX_TXIDS {
return Err(format!("too many txids, max {MAX_TXIDS} per request"));
}
let txid = Txid::from_str(val).map_err(|e| format!("invalid txid `{val}`: {e}"))?;
txids.push(txid);
} else {
@@ -35,3 +40,31 @@ impl TxidsParam {
Ok(Self { txids })
}
}
#[cfg(test)]
mod tests {
use super::*;
const T1: &str = "0000000000000000000000000000000000000000000000000000000000000001";
const T2: &str = "0000000000000000000000000000000000000000000000000000000000000002";
#[test]
fn parses_empty_single_and_multi() {
assert!(TxidsParam::from_query("").unwrap().txids.is_empty());
assert_eq!(TxidsParam::from_query(&format!("txId[]={T1}")).unwrap().txids.len(), 1);
assert_eq!(
TxidsParam::from_query(&format!("txId%5B%5D={T1}&txId[]={T2}"))
.unwrap()
.txids
.len(),
2,
);
}
#[test]
fn rejects_unknown_key_and_invalid_txid() {
assert!(TxidsParam::from_query("foo=bar").is_err());
assert!(TxidsParam::from_query("txId[]=notahex").is_err());
assert!(TxidsParam::from_query("noequals").is_err());
}
}