Skip to content

Conversation

evanlinjin
Copy link
Member

@evanlinjin evanlinjin commented Sep 18, 2025

Description

This PR adds the ability to calculate median-time-past (MTP) for CheckPoint structures, implementing the functionality described in #2036.

MTP is a critical component of Bitcoin's consensus rules (BIP113) used for time-based transaction validations. It provides a more robust timestamp by using the pseudo-median of up to 11 previous block timestamps.

Notes to the reviewers

The MTP calculation needs double checking. CheckPoint::median_time_past calculates the MTP value by looking at the 11 blocks that are BEFORE the current. If we call .median_time_past on the tip, is this the right value to compare against timelocked transactions going into the mempool?

Changelog notice

Added

  • Added block_time() method to ToBlockHash trait to optionally return block timestamps
  • Added median_time_past() and next_median_time_past() methods to CheckPoint for calculating MTP according to BIP113

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Fixes #2036

🤖 Generated with Claude Code

Add MTP calculation methods for CheckPoint following Bitcoin's BIP113:
- `median_time_past()`: calculates MTP for current block using previous 11 blocks
- `next_median_time_past()`: calculates MTP for next block using current + previous 10 blocks
- Returns None when timestamps unavailable or required blocks missing

Includes comprehensive tests for all edge cases.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@evanlinjin evanlinjin self-assigned this Sep 18, 2025
@evanlinjin evanlinjin added the new feature New feature or request label Sep 18, 2025
@evanlinjin evanlinjin moved this to Needs Review in BDK Chain Sep 18, 2025
@evanlinjin evanlinjin added this to the Wallet 3.0.0 milestone Sep 18, 2025
Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

cACK 0f75c22

I'll just give it a try with some other test scenarios before final ACK.

@evanlinjin evanlinjin force-pushed the feature/mtp branch 2 times, most recently from f97553f to 46e751f Compare September 19, 2025 01:00
Comment on lines +236 to +240
pub fn median_time_past(&self) -> Option<u32> {
let current_height = self.height();
let earliest_height = current_height.saturating_sub(Self::MTP_BLOCK_COUNT);
self._median_time_past(earliest_height..current_height)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious if these new APIs could output wrong/invalid values if the blocks inserted have a wrong/invalid time field e.g malicious block source.

Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely. We are trusting the chain source here (and everywhere in BDK). Eventually, we should think about how to verify all consensus rules in BDK: difficulty, PoW, MTP, etc.

Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

cACK 74d30d8

I didn't really find other test vectors that we could use.

Another thing that should probably be handled in a future PR is updating the calculation of Balance for the immature category, we probably should update it to consider transactions that are not yet mature considering the Locktime and MTP for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

Add median-time-past (MTP) calculation to CheckPoint
2 participants