bitcoind_request/command/get_raw_mempool.rs
1use std::collections::HashMap;
2
3/*
4getrawmempool ( verbose mempool_sequence )
5
6Returns all transaction ids in memory pool as a json array of string transaction ids.
7
8Hint: use getmempoolentry to fetch a specific transaction from the mempool.
9
10Arguments:
111. verbose (boolean, optional, default=false) True for a json object, false for array of transaction ids
122. mempool_sequence (boolean, optional, default=false) If verbose=false, returns a json object with transaction list and mempool sequence number attached.
13
14Result (for verbose = false):
15[ (json array)
16 "hex", (string) The transaction id
17 ...
18]
19
20Result (for verbose = true):
21{ (json object)
22 "transactionid" : { (json object)
23 "vsize" : n, (numeric) virtual transaction size as defined in BIP 141. This is different from actual serialized size for witness transactions as witness data is discounted.
24 "weight" : n, (numeric) transaction weight as defined in BIP 141.
25 "fee" : n, (numeric) transaction fee in BTC (DEPRECATED)
26 "modifiedfee" : n, (numeric) transaction fee with fee deltas used for mining priority (DEPRECATED)
27 "time" : xxx, (numeric) local time transaction entered pool in seconds since 1 Jan 1970 GMT
28 "height" : n, (numeric) block height when transaction entered pool
29 "descendantcount" : n, (numeric) number of in-mempool descendant transactions (including this one)
30 "descendantsize" : n, (numeric) virtual transaction size of in-mempool descendants (including this one)
31 "descendantfees" : n, (numeric) modified fees (see above) of in-mempool descendants (including this one) (DEPRECATED)
32 "ancestorcount" : n, (numeric) number of in-mempool ancestor transactions (including this one)
33 "ancestorsize" : n, (numeric) virtual transaction size of in-mempool ancestors (including this one)
34 "ancestorfees" : n, (numeric) modified fees (see above) of in-mempool ancestors (including this one) (DEPRECATED)
35 "wtxid" : "hex", (string) hash of serialized transaction, including witness data
36 "fees" : { (json object)
37 "base" : n, (numeric) transaction fee in BTC
38 "modified" : n, (numeric) transaction fee with fee deltas used for mining priority in BTC
39 "ancestor" : n, (numeric) modified fees (see above) of in-mempool ancestors (including this one) in BTC
40 "descendant" : n (numeric) modified fees (see above) of in-mempool descendants (including this one) in BTC
41 },
42 "depends" : [ (json array) unconfirmed transactions used as inputs for this transaction
43 "hex", (string) parent transaction id
44 ...
45 ],
46 "spentby" : [ (json array) unconfirmed transactions spending outputs from this transaction
47 "hex", (string) child transaction id
48 ...
49 ],
50 "bip125-replaceable" : true|false, (boolean) Whether this transaction could be replaced due to BIP125 (replace-by-fee)
51 "unbroadcast" : true|false (boolean) Whether this transaction is currently unbroadcast (initial broadcast not yet acknowledged by any peers)
52 },
53 ...
54}
55
56Result (for verbose = false and mempool_sequence = true):
57{ (json object)
58 "txids" : [ (json array)
59 "hex", (string) The transaction id
60 ...
61 ],
62 "mempool_sequence" : n (numeric) The mempool sequence value.
63}
64
65Examples:
66> bitcoin-cli getrawmempool true
67> curl --user myusername --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getrawmempool", "params": [true]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
68 */
69use crate::client::Client;
70use crate::command::{request::request, CallableCommand};
71use serde::{Deserialize, Serialize};
72use serde_json::value::to_raw_value;
73
74pub type TransactionId = String;
75
76#[derive(Serialize, Deserialize, Debug)]
77#[serde(untagged)]
78pub enum GetRawMempoolCommandResponse {
79 TransacationIds(Vec<TransactionId>),
80 TransactionIdsWithSequence(TransactionIdsWithSequence),
81 Transactions(HashMap<TransactionId, Transaction>),
82}
83
84#[derive(Serialize, Deserialize, Debug)]
85pub struct TransactionIdsWithSequence {
86 pub txids: Vec<TransactionId>,
87 pub mempool_sequence: u64,
88}
89
90#[derive(Serialize, Deserialize, Debug)]
91pub struct Fees {
92 pub base: f64,
93 pub modified: f64,
94 pub ancestor: f64,
95 pub descendant: f64,
96}
97
98#[derive(Serialize, Deserialize, Debug)]
99pub struct Transaction {
100 pub vsize: u64,
101 pub weight: u64,
102 pub fee: f64,
103 pub modifiedfee: f64,
104 // TODO: Represent using a unix time
105 pub time: u64,
106 pub height: u64,
107 pub descendantcount: u64,
108 pub descendantsize: u64,
109 pub descendantfees: f64,
110 pub ancestorcount: u64,
111 pub ancestorsize: u64,
112 pub ancestorfees: u64,
113 pub wtxid: String,
114 pub fees: Fees,
115 pub depends: Vec<String>,
116 pub spentby: Vec<String>,
117 #[serde(alias = "bip125-replaceable")]
118 pub bip125_replaceable: bool,
119 pub unbroadcast: bool,
120}
121#[derive(Serialize, Deserialize, Debug)]
122pub struct Transactions(pub HashMap<TransactionId, Transaction>);
123
124pub struct GetRawMempoolCommand {
125 verbose: bool,
126 mempool_sequence: bool,
127}
128impl GetRawMempoolCommand {
129 pub fn new() -> Self {
130 GetRawMempoolCommand {
131 verbose: false,
132 mempool_sequence: false,
133 }
134 }
135 pub fn set_verbose(mut self, verbose: bool) -> Self {
136 self.verbose = verbose;
137 self
138 }
139 pub fn set_mempool_sequence(mut self, mempool_sequence: bool) -> Self {
140 self.mempool_sequence = mempool_sequence;
141 self
142 }
143}
144impl CallableCommand for GetRawMempoolCommand {
145 type Response = GetRawMempoolCommandResponse;
146 fn call(&self, client: &Client) -> Result<Self::Response, jsonrpc::Error> {
147 if self.verbose && self.mempool_sequence {
148 panic!("RPC command 'getrawmempool' has invalid arguments of verbose=true & mempool_sequence=true. Verbose results cannot contain mempool sequence values.");
149 }
150 let verbose_arg = self.verbose;
151 let mempool_sequence_arg = &self.mempool_sequence;
152 let verbose_arg_raw_value = to_raw_value(&verbose_arg).unwrap();
153 let mempool_sequence_arg_raw_value = to_raw_value(&mempool_sequence_arg).unwrap();
154 let command = "getrawmempool";
155 let params = vec![verbose_arg_raw_value, mempool_sequence_arg_raw_value];
156 let r = request(client, command, params);
157 let response: GetRawMempoolCommandResponse = r.result()?;
158 Ok(response)
159 }
160}