Skip to content

Conversation

Jiloc
Copy link
Contributor

@Jiloc Jiloc commented Aug 1, 2025

Description

The X-Canonical-Stacks-Tip-Height header is intended to be included in all succesfull RPC responses, and this has mostly been the case until epoch 2.5. However, over time this behavior was inconsistently applied, as the header had to be manually added in each endpoint's implementation. This made it easy to forget during implementation or review of new endpoints.

This PR refactors the code to centralize the logic for setting this header, ensuring it is automatically included in all relevant responses. So we eliminate the need to manually include it in each endpoint and write dedicated tests for it for each new endpoint. The presence of the header continues to be covered by existing integration tests for various endpoints (e.g.,

assert_eq!(
response.preamble().get_canonical_stacks_tip_height(),
Some(1)
);
)

The following endpoints were previously missing the header and are now covered:

  • /v2/blocks/:block_id
  • /v2/headers/:height
  • /v2/mempool/query
  • /v2/microblock/*
  • /v3/tenures/fork_info
  • /v3/blocks/:block_id
  • /v3/blocks/height/:block_height
  • /v3/health
  • /v3/sortitions/*
  • /v3/tenures/:block_id
  • /v3/tenures/info
  • /v3/tenures/tip/:consensus_hash
  • /v3/transaction/:txid

This change also lays the groundwork for a follow-up PR that will use this header to determine the highest Stacks tip height among peers via their RPC responses.

Other changes:

  • the various HttpPreambleExtensions functions to set the header now expect a u64 instead of a u32, avoiding the u64 -> u32 casting.
  • add a is_success function to HttpResponsePreamble to easily check the response header.

Applicable issues

  • fixes #

Additional info (benefits, drawbacks, caveats)

Checklist

  • Test coverage for new or modified code paths
  • Changelog is updated
  • Required documentation changes (e.g., docs/rpc/openapi.yaml and rpc-endpoints.md for v2 endpoints, event-dispatcher.md for new events)
  • New clarity functions have corresponding PR in clarity-benchmarking repo
  • New integration test(s) added to bitcoin-tests.yml

@Jiloc Jiloc self-assigned this Aug 1, 2025
@Jiloc Jiloc added this to the 3.2.0.0.1 milestone Aug 1, 2025
@Jiloc Jiloc moved this to Status: 💻 In Progress in Stacks Core Eng Aug 1, 2025
@Jiloc Jiloc requested a review from jcnelson August 4, 2025 09:47
@Jiloc Jiloc marked this pull request as ready for review August 4, 2025 09:48
@Jiloc Jiloc requested review from a team as code owners August 4, 2025 09:48
@Jiloc Jiloc requested a review from rdeioris August 4, 2025 11:19
@Jiloc Jiloc moved this from Status: 💻 In Progress to Status: In Review in Stacks Core Eng Aug 4, 2025
@aldur aldur linked an issue Aug 4, 2025 that may be closed by this pull request
@aldur aldur requested a review from fdefelici August 7, 2025 14:36
Copy link
Contributor

@fdefelici fdefelici left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks fine!

Waiting for open remarks to be addressed before approving

@Jiloc Jiloc moved this from Status: In Review to Status: 💻 In Progress in Stacks Core Eng Aug 13, 2025
Copy link
Contributor

@fdefelici fdefelici left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit a95d3e7 centralizes the get_canonical_stacks_tip_height() response header check in the TestRPC facility. This opens an opportunity to clean up the RPC endpoint tests regarding canonical height assertions (let's call it height assert for brevity).

Currently, each RPC endpoint should checks for the height assert in their tests. This approach has proved to be error-prone. As a result, about 60% of RPC (e.g., callreadonly.rs) perform this check in their tests, while 40% (e.g., getblock.rs) do not. Ideally, these inconsistencies should be addressed in this PR.

Given the new commit, I propose the following improvement to our test strategy:

  • Remove all individual height assert checks from the 60% of RPC endpoint tests that currently include them.
  • Introduce a dedicated test specifically for the height assert, which can target one or a few RPC endpoints (or just a mocked one if we could) and verify that the height assert matches the expected value.

This approach centralizes the height assert check and relieves all RPC endpoint tests (including any future additions) from having to perform this verification individually.

What do you think?

@Jiloc
Copy link
Contributor Author

Jiloc commented Aug 14, 2025

The new commit a95d3e7 centralizes the get_canonical_stacks_tip_height() response header check in the TestRPC facility. This opens an opportunity to clean up the RPC endpoint tests regarding canonical height assertions (let's call it height assert for brevity).

Currently, each RPC endpoint should checks for the height assert in their tests. This approach has proved to be error-prone. As a result, about 60% of RPC (e.g., callreadonly.rs) perform this check in their tests, while 40% (e.g., getblock.rs) do not. Ideally, these inconsistencies should be addressed in this PR.

Given the new commit, I propose the following improvement to our test strategy:

  • Remove all individual height assert checks from the 60% of RPC endpoint tests that currently include them.
  • Introduce a dedicated test specifically for the height assert, which can target one or a few RPC endpoints (or just a mocked one if we could) and verify that the height assert matches the expected value.

This approach centralizes the height assert check and relieves all RPC endpoint tests (including any future additions) from having to perform this verification individually.

What do you think?

I agree, this sounds like an improvement. I also had the same concern in the back of my mind. If we leave the tests as they are, future developers adding new endpoints might wonder whether they should include the height assert based on the mixed patterns in existing tests.

@Jiloc Jiloc moved this from Status: 💻 In Progress to Status: In Review in Stacks Core Eng Aug 14, 2025
@Jiloc Jiloc requested review from jcnelson and fdefelici August 14, 2025 13:23
fdefelici
fdefelici previously approved these changes Aug 14, 2025
Copy link
Contributor

@fdefelici fdefelici left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT!

@aldur aldur modified the milestones: 3.2.0.0.1, 3.2.0.0.2 Aug 20, 2025
@Jiloc Jiloc requested review from jcnelson and fdefelici August 25, 2025 14:25
@Jiloc Jiloc enabled auto-merge August 26, 2025 08:05
@Jiloc Jiloc added this pull request to the merge queue Aug 26, 2025
Merged via the queue into stacks-network:develop with commit 40825cc Aug 26, 2025
833 of 844 checks passed
@Jiloc Jiloc deleted the fix/rpc-return-stacks-canonical-height branch August 26, 2025 08:43
@github-project-automation github-project-automation bot moved this from Status: In Review to Status: ✅ Done in Stacks Core Eng Aug 26, 2025
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

❌ Patch coverage is 91.34615% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.60%. Comparing base (a9e2828) to head (72caf0c).
⚠️ Report is 164 commits behind head on develop.

Files with missing lines Patch % Lines
stackslib/src/net/httpcore.rs 66.66% 2 Missing ⚠️
stackslib/src/net/api/fastcallreadonly.rs 0.00% 1 Missing ⚠️
stackslib/src/net/api/getattachment.rs 0.00% 1 Missing ⚠️
stackslib/src/net/api/getconstantval.rs 0.00% 1 Missing ⚠️
stackslib/src/net/api/getdatavar.rs 0.00% 1 Missing ⚠️
stackslib/src/net/download/epoch2x.rs 66.66% 1 Missing ⚠️
...download/nakamoto/tenure_downloader_unconfirmed.rs 0.00% 1 Missing ⚠️
stackslib/src/net/tests/httpcore.rs 97.82% 1 Missing ⚠️

❌ Your project status has failed because the head coverage (64.60%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6344      +/-   ##
===========================================
+ Coverage    62.44%   64.60%   +2.15%     
===========================================
  Files          552      555       +3     
  Lines       351797   350866     -931     
===========================================
+ Hits        219676   226672    +6996     
+ Misses      132121   124194    -7927     
Files with missing lines Coverage Δ
stackslib/src/chainstate/stacks/db/mod.rs 82.36% <ø> (+3.96%) ⬆️
stackslib/src/net/api/callreadonly.rs 93.24% <100.00%> (+11.18%) ⬆️
stackslib/src/net/api/get_tenures_fork_info.rs 68.61% <ø> (-0.54%) ⬇️
stackslib/src/net/api/getaccount.rs 91.41% <100.00%> (+9.09%) ⬆️
stackslib/src/net/api/getattachmentsinv.rs 79.61% <100.00%> (-0.13%) ⬇️
stackslib/src/net/api/getblock.rs 73.80% <ø> (ø)
stackslib/src/net/api/getblock_v3.rs 79.21% <ø> (ø)
stackslib/src/net/api/getblockbyheight.rs 78.22% <ø> (ø)
stackslib/src/net/api/getclaritymarfvalue.rs 85.95% <100.00%> (-0.12%) ⬇️
stackslib/src/net/api/getclaritymetadata.rs 89.72% <100.00%> (+42.10%) ⬆️
... and 61 more

... and 341 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9e2828...72caf0c. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

[Bug] /v3/health always fails with HTTP 500
4 participants