Skip to content

Commit f177211

Browse files
committed
CRC: coinbase height always exists even if in pre nakamoto
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 54ff7cb commit f177211

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

stacks-signer/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub(crate) mod tests {
322322
stacks_tip_consensus_hash: generate_random_consensus_hash(),
323323
unanchored_tip: None,
324324
unanchored_seq: Some(0),
325-
tenure_height: None,
325+
tenure_height: thread_rng().next_u64(),
326326
exit_at_block_height: None,
327327
is_fully_synced: false,
328328
genesis_chainstate_hash: Sha256Sum::zero(),

stackslib/src/net/api/getinfo.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ pub struct RPCPeerInfoData {
8282
pub genesis_chainstate_hash: Sha256Sum,
8383
pub unanchored_tip: Option<StacksBlockId>,
8484
pub unanchored_seq: Option<u16>,
85-
#[serde(default)]
86-
#[serde(skip_serializing_if = "Option::is_none")]
87-
pub tenure_height: Option<u64>,
85+
pub tenure_height: u64,
8886
pub exit_at_block_height: Option<u64>,
8987
pub is_fully_synced: bool,
9088
#[serde(default)]
@@ -110,7 +108,7 @@ impl RPCPeerInfoData {
110108
chainstate: &StacksChainState,
111109
exit_at_block_height: Option<u64>,
112110
genesis_chainstate_hash: &Sha256Sum,
113-
coinbase_height: Option<u64>,
111+
coinbase_height: u64,
114112
ibd: bool,
115113
) -> RPCPeerInfoData {
116114
let server_version = version_string(

stackslib/src/net/api/postmempoolquery.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ impl RPCRequestHandler for RPCMempoolQueryRequestHandler {
276276
let page_id = self.page_id.take();
277277

278278
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);
280-
279+
let coinbase_height = network.stacks_tip.coinbase_height;
281280
let max_txs = network.connection_opts.mempool_max_tx_query;
282281
debug!(
283282
"Begin mempool query";

stackslib/src/net/api/tests/getinfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,5 @@ fn test_try_make_response() {
103103
);
104104
let resp = response.decode_peer_info().unwrap();
105105

106-
assert_eq!(resp.tenure_height, Some(1));
106+
assert_eq!(resp.tenure_height, 1);
107107
}

stackslib/src/net/p2p.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +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>,
249+
pub coinbase_height: u64,
250250
pub is_nakamoto: bool,
251251
}
252252

@@ -256,7 +256,7 @@ impl StacksTipInfo {
256256
consensus_hash: ConsensusHash([0u8; 20]),
257257
block_hash: BlockHeaderHash([0u8; 32]),
258258
height: 0,
259-
coinbase_height: None,
259+
coinbase_height: 0,
260260
is_nakamoto: false,
261261
}
262262
}
@@ -4224,21 +4224,35 @@ impl PeerNetwork {
42244224
net_error::DBError(db_error::NotFoundError)
42254225
})?;
42264226

4227-
// TODO: Test this!
42284227
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(
4228+
let parent_stacks_tip_block_id = StacksBlockId::new(
4229+
&parent_tenure_start_header.consensus_hash,
4230+
&parent_stacks_tip_block_hash,
4231+
);
4232+
let parent_coinbase_height = NakamotoChainState::get_coinbase_height(
42304233
&mut chainstate.index_conn(),
4231-
&StacksBlockId::new(
4232-
&parent_tenure_start_header.consensus_hash,
4233-
&parent_stacks_tip_block_hash,
4234-
),
4234+
&parent_stacks_tip_block_id,
42354235
)?;
42364236

4237+
let coinbase_height = match parent_coinbase_height {
4238+
Some(cbh) => cbh,
4239+
None => {
4240+
if parent_tenure_start_header.is_epoch_2_block() {
4241+
// The coinbase height is the same as the stacks block height as
4242+
// every block contains a coinbase in epoch 2.x
4243+
parent_tenure_start_header.stacks_block_height
4244+
} else {
4245+
debug!("{:?}: get_parent_stacks_tip: No coinbase height found for nakamoto block {parent_stacks_tip_block_id}", self.get_local_peer());
4246+
return Err(net_error::DBError(db_error::NotFoundError));
4247+
}
4248+
}
4249+
};
4250+
42374251
let parent_stacks_tip = StacksTipInfo {
42384252
consensus_hash: parent_tenure_start_header.consensus_hash,
42394253
block_hash: parent_stacks_tip_block_hash,
42404254
height: parent_tenure_start_header.anchored_header.height(),
4241-
coinbase_height: parent_tenure_start_header_cbh,
4255+
coinbase_height,
42424256
is_nakamoto: parent_tenure_start_header
42434257
.anchored_header
42444258
.as_stacks_nakamoto()
@@ -4390,12 +4404,25 @@ impl PeerNetwork {
43904404
self.stacks_tip.is_nakamoto
43914405
};
43924406

4393-
// TODO: Test this!
43944407
let stacks_tip_cbh = NakamotoChainState::get_coinbase_height(
43954408
&mut chainstate.index_conn(),
43964409
&new_stacks_tip_block_id,
43974410
)?;
43984411

4412+
let coinbase_height = match stacks_tip_cbh {
4413+
Some(cbh) => cbh,
4414+
None => {
4415+
if !stacks_tip_is_nakamoto {
4416+
// The coinbase height is the same as the stacks block height as
4417+
// every block contains a coinbase in epoch 2.x
4418+
stacks_tip_height
4419+
} else {
4420+
debug!("{:?}: No coinbase height found for nakamoto block {new_stacks_tip_block_id}", self.get_local_peer());
4421+
return Err(net_error::DBError(db_error::NotFoundError));
4422+
}
4423+
}
4424+
};
4425+
43994426
let need_stackerdb_refresh = canonical_sn.canonical_stacks_tip_consensus_hash
44004427
!= self.burnchain_tip.canonical_stacks_tip_consensus_hash
44014428
|| burnchain_tip_changed
@@ -4434,7 +4461,7 @@ impl PeerNetwork {
44344461
consensus_hash: FIRST_BURNCHAIN_CONSENSUS_HASH.clone(),
44354462
block_hash: FIRST_STACKS_BLOCK_HASH.clone(),
44364463
height: 0,
4437-
coinbase_height: None,
4464+
coinbase_height: 0,
44384465
is_nakamoto: false,
44394466
}
44404467
}
@@ -4630,7 +4657,7 @@ impl PeerNetwork {
46304657
consensus_hash: stacks_tip_ch,
46314658
block_hash: stacks_tip_bhh,
46324659
height: stacks_tip_height,
4633-
coinbase_height: stacks_tip_cbh,
4660+
coinbase_height,
46344661
is_nakamoto: stacks_tip_is_nakamoto,
46354662
};
46364663
self.parent_stacks_tip = parent_stacks_tip;

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5402,7 +5402,7 @@ fn check_block_heights() {
54025402

54035403
// With the first Nakamoto block, the chain tip and the number of tenures
54045404
// must be the same (before Nakamoto every block counts as a tenure)
5405-
assert_eq!(info.tenure_height.unwrap(), info.stacks_tip_height);
5405+
assert_eq!(info.tenure_height, info.stacks_tip_height);
54065406

54075407
let mut last_burn_block_height;
54085408
let mut last_stacks_block_height = info.stacks_tip_height as u128;
@@ -5538,7 +5538,7 @@ fn check_block_heights() {
55385538
last_tenure_height = bh1;
55395539

55405540
let info = get_chain_info_result(&naka_conf).unwrap();
5541-
assert_eq!(info.tenure_height.unwrap(), bh3 as u64);
5541+
assert_eq!(info.tenure_height, bh3 as u64);
55425542

55435543
let sbh = heights3
55445544
.get("stacks-block-height")
@@ -5649,7 +5649,7 @@ fn check_block_heights() {
56495649
);
56505650

56515651
let info = get_chain_info_result(&naka_conf).unwrap();
5652-
assert_eq!(info.tenure_height.unwrap(), bh3 as u64);
5652+
assert_eq!(info.tenure_height, bh3 as u64);
56535653

56545654
let sbh = heights3
56555655
.get("stacks-block-height")
@@ -5692,10 +5692,7 @@ fn check_block_heights() {
56925692
);
56935693

56945694
let info = get_chain_info_result(&naka_conf).unwrap();
5695-
assert_eq!(
5696-
info.tenure_height.unwrap(),
5697-
block_height_pre_3_0 + tenure_count
5698-
);
5695+
assert_eq!(info.tenure_height, block_height_pre_3_0 + tenure_count);
56995696

57005697
coord_channel
57015698
.lock()

0 commit comments

Comments
 (0)