Skip to content

Commit 097c4db

Browse files
committed
feat: cache coinbase_height
1 parent e9cb781 commit 097c4db

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

stackslib/src/net/api/getinfo.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,8 @@ impl RPCRequestHandler for RPCPeerInfoRequestHandler {
225225
let ibd = node.ibd;
226226

227227
let rpc_peer_info: Result<RPCPeerInfoData, StacksHttpResponse> =
228-
node.with_node_state(|network, sortdb, chainstate, _mempool, rpc_args| {
229-
let coinbase_height = NakamotoChainState::get_coinbase_height(
230-
&mut chainstate.index_conn(),
231-
&StacksBlockId::new(
232-
&network.stacks_tip.consensus_hash,
233-
&network.stacks_tip.block_hash,
234-
),
235-
)
236-
.map_err(|e| {
237-
StacksHttpResponse::new_error(
238-
&preamble,
239-
&HttpServerError::new(format!("Failed to load coinbase height: {:?}", &e)),
240-
)
241-
})?;
228+
node.with_node_state(|network, _sortdb, chainstate, _mempool, rpc_args| {
229+
let coinbase_height = network.stacks_tip.coinbase_height;
242230

243231
Ok(RPCPeerInfoData::from_network(
244232
network,

stackslib/src/net/api/postmempoolquery.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,8 @@ impl RPCRequestHandler for RPCMempoolQueryRequestHandler {
275275
.ok_or(NetError::SendError("`mempool_query` not set".into()))?;
276276
let page_id = self.page_id.take();
277277

278-
let stream_res = node.with_node_state(|network, sortdb, chainstate, mempool, _rpc_args| {
279-
let coinbase_height = NakamotoChainState::get_coinbase_height(
280-
&mut chainstate.index_conn(),
281-
&StacksBlockId::new(
282-
&network.stacks_tip.consensus_hash,
283-
&network.stacks_tip.block_hash
284-
),
285-
)
286-
.map_err(|e| StacksHttpResponse::new_error(&preamble, &HttpServerError::new(format!("Failed to load coinbase height: {:?}", &e))))?
287-
.unwrap_or(0);
278+
let stream_res = node.with_node_state(|network, _sortdb, _chainstate, mempool, _rpc_args| {
279+
let coinbase_height = network.stacks_tip.coinbase_height.unwrap_or(0);
288280

289281
let max_txs = network.connection_opts.mempool_max_tx_query;
290282
debug!(

stackslib/src/net/p2p.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ pub struct StacksTipInfo {
246246
pub consensus_hash: ConsensusHash,
247247
pub block_hash: BlockHeaderHash,
248248
pub height: u64,
249+
pub coinbase_height: Option<u64>,
249250
pub is_nakamoto: bool,
250251
}
251252

@@ -255,6 +256,7 @@ impl StacksTipInfo {
255256
consensus_hash: ConsensusHash([0u8; 20]),
256257
block_hash: BlockHeaderHash([0u8; 32]),
257258
height: 0,
259+
coinbase_height: None,
258260
is_nakamoto: false,
259261
}
260262
}
@@ -4218,14 +4220,25 @@ impl PeerNetwork {
42184220

42194221
let parent_tenure_start_header = NakamotoChainState::get_tenure_start_block_header(&mut chainstate.index_conn(), stacks_tip_block_id, &parent_header.consensus_hash)?
42204222
.ok_or_else(|| {
4221-
debug!("{:?}: get_parent_stacks_tip: No tenure-start block for parent tenure {} off of child {} (parnet {})", self.get_local_peer(), &parent_header.consensus_hash, stacks_tip_block_id, &parent_block_id);
4223+
debug!("{:?}: get_parent_stacks_tip: No tenure-start block for parent tenure {} off of child {} (parent {})", self.get_local_peer(), &parent_header.consensus_hash, stacks_tip_block_id, &parent_block_id);
42224224
net_error::DBError(db_error::NotFoundError)
42234225
})?;
42244226

4227+
// TODO: Test this!
4228+
let parent_stacks_tip_block_hash = parent_tenure_start_header.anchored_header.block_hash();
4229+
let parent_tenure_start_header_cbh = NakamotoChainState::get_coinbase_height(
4230+
&mut chainstate.index_conn(),
4231+
&StacksBlockId::new(
4232+
&parent_tenure_start_header.consensus_hash,
4233+
&parent_stacks_tip_block_hash,
4234+
),
4235+
)?;
4236+
42254237
let parent_stacks_tip = StacksTipInfo {
42264238
consensus_hash: parent_tenure_start_header.consensus_hash,
4227-
block_hash: parent_tenure_start_header.anchored_header.block_hash(),
4239+
block_hash: parent_stacks_tip_block_hash,
42284240
height: parent_tenure_start_header.anchored_header.height(),
4241+
coinbase_height: parent_tenure_start_header_cbh,
42294242
is_nakamoto: parent_tenure_start_header
42304243
.anchored_header
42314244
.as_stacks_nakamoto()
@@ -4377,6 +4390,12 @@ impl PeerNetwork {
43774390
self.stacks_tip.is_nakamoto
43784391
};
43794392

4393+
// TODO: Test this!
4394+
let stacks_tip_cbh = NakamotoChainState::get_coinbase_height(
4395+
&mut chainstate.index_conn(),
4396+
&new_stacks_tip_block_id,
4397+
)?;
4398+
43804399
let need_stackerdb_refresh = canonical_sn.canonical_stacks_tip_consensus_hash
43814400
!= self.burnchain_tip.canonical_stacks_tip_consensus_hash
43824401
|| burnchain_tip_changed
@@ -4415,6 +4434,7 @@ impl PeerNetwork {
44154434
consensus_hash: FIRST_BURNCHAIN_CONSENSUS_HASH.clone(),
44164435
block_hash: FIRST_STACKS_BLOCK_HASH.clone(),
44174436
height: 0,
4437+
coinbase_height: None,
44184438
is_nakamoto: false,
44194439
}
44204440
}
@@ -4610,6 +4630,7 @@ impl PeerNetwork {
46104630
consensus_hash: stacks_tip_ch,
46114631
block_hash: stacks_tip_bhh,
46124632
height: stacks_tip_height,
4633+
coinbase_height: stacks_tip_cbh,
46134634
is_nakamoto: stacks_tip_is_nakamoto,
46144635
};
46154636
self.parent_stacks_tip = parent_stacks_tip;

0 commit comments

Comments
 (0)