mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-08 01:28:15 -07:00
mempool: general improvements
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user