Skip to content

Commit 22ecca9

Browse files
committed
feat: make /v2/mempool/query compatible with Nakamoto by querying Nakamoto headers for the tip
1 parent c199312 commit 22ecca9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

stackslib/src/net/api/postmempoolquery.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use url::form_urlencoded;
2929
use {serde, serde_json};
3030

3131
use crate::burnchains::Txid;
32+
use crate::chainstate::nakamoto::NakamotoChainState;
3233
use crate::chainstate::stacks::db::StacksChainState;
3334
use crate::chainstate::stacks::{Error as ChainError, StacksTransaction};
3435
use crate::core::mempool::{decode_tx_stream, MemPoolDB, MemPoolSyncData};
@@ -89,8 +90,8 @@ pub struct StacksMemPoolStream {
8990
pub num_txs: u64,
9091
/// maximum we can visit in the query
9192
pub max_txs: u64,
92-
/// height of the chain at time of query
93-
pub height: u64,
93+
/// coinbase height of the chain at time of query
94+
pub coinbase_height: u64,
9495
/// Are we done sending transactions, and are now in the process of sending the trailing page
9596
/// ID?
9697
pub corked: bool,
@@ -105,7 +106,7 @@ impl StacksMemPoolStream {
105106
mempool_db: DBConn,
106107
tx_query: MemPoolSyncData,
107108
max_txs: u64,
108-
height: u64,
109+
coinbase_height: u64,
109110
page_id_opt: Option<Txid>,
110111
) -> Self {
111112
let last_randomized_txid = page_id_opt.unwrap_or_else(|| {
@@ -118,7 +119,7 @@ impl StacksMemPoolStream {
118119
last_randomized_txid: last_randomized_txid,
119120
num_txs: 0,
120121
max_txs: max_txs,
121-
height: height,
122+
coinbase_height,
122123
corked: false,
123124
finished: false,
124125
mempool_db,
@@ -159,7 +160,7 @@ impl HttpChunkGenerator for StacksMemPoolStream {
159160
MemPoolDB::static_find_next_missing_transactions(
160161
&self.mempool_db,
161162
&self.tx_query,
162-
self.height,
163+
self.coinbase_height,
163164
&self.last_randomized_txid,
164165
1,
165166
remaining,
@@ -275,12 +276,18 @@ impl RPCRequestHandler for RPCMempoolQueryRequestHandler {
275276
let page_id = self.page_id.take();
276277

277278
let stream_res = node.with_node_state(|network, sortdb, chainstate, mempool, _rpc_args| {
278-
let height = self.get_stacks_chain_tip(&preamble, sortdb, chainstate).map(|hdr| hdr.anchored_header.height()).unwrap_or(0);
279+
let header = self.get_stacks_chain_tip(&preamble, sortdb, chainstate)
280+
.map_err(|e| StacksHttpResponse::new_error(&preamble, &HttpServerError::new(format!("Failed to load chain tip: {:?}", &e))))?;
281+
282+
let coinbase_height = NakamotoChainState::get_coinbase_height(&mut chainstate.index_conn(), &header.index_block_hash())
283+
.map_err(|e| StacksHttpResponse::new_error(&preamble, &HttpServerError::new(format!("Failed to load coinbase height: {:?}", &e))))?
284+
.unwrap_or(0);
285+
279286
let max_txs = network.connection_opts.mempool_max_tx_query;
280287
debug!(
281288
"Begin mempool query";
282289
"page_id" => %page_id.map(|txid| format!("{}", &txid)).unwrap_or("(none".to_string()),
283-
"block_height" => height,
290+
"coinbase_height" => coinbase_height,
284291
"max_txs" => max_txs
285292
);
286293

@@ -291,7 +298,7 @@ impl RPCRequestHandler for RPCMempoolQueryRequestHandler {
291298
}
292299
};
293300

294-
Ok(StacksMemPoolStream::new(mempool_db, mempool_query, max_txs, height, page_id))
301+
Ok(StacksMemPoolStream::new(mempool_db, mempool_query, max_txs, coinbase_height, page_id))
295302
});
296303

297304
let stream = match stream_res {

0 commit comments

Comments
 (0)