subcoin_rpc_bitcoind/
mempool.rs

1//! Bitcoin Core compatible mempool RPC methods.
2//!
3//! Implements: getrawmempool, getmempoolinfo, getmempoolentry
4
5use crate::error::Error;
6use crate::types::{GetMempoolEntry, GetMempoolInfo};
7use bitcoin::Txid;
8use jsonrpsee::proc_macros::rpc;
9use std::collections::HashMap;
10
11/// Bitcoin Core compatible mempool RPC API.
12#[rpc(client, server)]
13pub trait MempoolApi {
14    /// Returns all transaction ids in memory pool.
15    ///
16    /// If verbose is false (default), returns array of transaction ids.
17    /// If verbose is true, returns a JSON object with txid -> mempool entry.
18    #[method(name = "getrawmempool", blocking)]
19    fn get_raw_mempool(
20        &self,
21        verbose: Option<bool>,
22        mempool_sequence: Option<bool>,
23    ) -> Result<serde_json::Value, Error>;
24
25    /// Returns details on the active state of the TX memory pool.
26    #[method(name = "getmempoolinfo", blocking)]
27    fn get_mempool_info(&self) -> Result<GetMempoolInfo, Error>;
28
29    /// Returns mempool data for given transaction.
30    #[method(name = "getmempoolentry", blocking)]
31    fn get_mempool_entry(&self, txid: Txid) -> Result<GetMempoolEntry, Error>;
32}
33
34/// Bitcoin Core compatible mempool RPC implementation.
35///
36/// TODO: Integrate with subcoin-mempool crate for real mempool data.
37pub struct MempoolRpc {
38    // In the future, this will hold a reference to the actual mempool
39}
40
41impl MempoolRpc {
42    /// Creates a new instance of [`MempoolRpc`].
43    pub fn new() -> Self {
44        Self {}
45    }
46}
47
48impl Default for MempoolRpc {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54#[async_trait::async_trait]
55impl MempoolApiServer for MempoolRpc {
56    fn get_raw_mempool(
57        &self,
58        verbose: Option<bool>,
59        _mempool_sequence: Option<bool>,
60    ) -> Result<serde_json::Value, Error> {
61        // TODO: Get actual mempool data from subcoin-mempool
62        if verbose.unwrap_or(false) {
63            // Return empty map
64            let entries: HashMap<Txid, GetMempoolEntry> = HashMap::new();
65            Ok(serde_json::to_value(entries)?)
66        } else {
67            // Return empty array of txids
68            let txids: Vec<Txid> = vec![];
69            Ok(serde_json::to_value(txids)?)
70        }
71    }
72
73    fn get_mempool_info(&self) -> Result<GetMempoolInfo, Error> {
74        // TODO: Get actual mempool info from subcoin-mempool
75        Ok(GetMempoolInfo {
76            loaded: true,
77            size: 0,
78            bytes: 0,
79            usage: 0,
80            total_fee: 0.0,
81            maxmempool: 300_000_000, // 300 MB default
82            mempoolminfee: 0.00001,  // 1 sat/vB in BTC/kvB
83            minrelaytxfee: 0.00001,
84            incrementalrelayfee: 0.00001,
85            unbroadcastcount: 0,
86            fullrbf: false,
87        })
88    }
89
90    fn get_mempool_entry(&self, _txid: Txid) -> Result<GetMempoolEntry, Error> {
91        // TODO: Get actual mempool entry from subcoin-mempool
92        Err(Error::TransactionNotFound)
93    }
94}