mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:44:38 +00:00
tests: Add unix timestamp tests for OP_CLTV and max mediantimepast tests #32229
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!32229
Loading…
Reference in a new issue
No description provided.
Delete branch "2025-04-06-locktime-tests"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR adds a functional (p2p/block-validation) test for
OP_CHECKLOCKTIMEVERIFY(BIP65) using unix-timestamp locktimes in test/functional/feature_cltv.py. It also adds tests around the maximum possible median-time-past (MTP) value,UINT32_MAX, and how that interacts with transaction finality.The unix-timestamp portion of BIP65 already has unit-level coverage in
transaction_tests.cpp(viatx_valid.json/tx_invalid.json), but those tests callVerifyScript()directly and never exercise median-time-past (MTP) or block connection. MTP-vs-nLockTime behavior itself is covered separately inminer_tests.cpp(lines 525-534) andfeature_csv_activation.py(lines 340-355), but neither exercisesOP_CLTVspecifically. This test closes that gap: it builds and submits full blocks over p2p and checks that a block containing anOP_CLTV-locked output is only accepted once BIP113 MTP actually satisfies the requested locktime.This PR also adds a BIP113 test to
feature_csv_activation.pyshowing that MTP can never advance pastUINT32_MAX- 1, so an output with a plain nLockTime ofUINT32_MAXcan never be confirmed. This is a general BIP113 property independent ofOP_CLTV.Thanks to @mabu44 and @sedited for the review so far.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.The Unix-timestamp portion of BIP65 is currently tested in the c++ unit tests in "transactions_tests.cpp". Test data includes transactions that have OP_CLTV with Unix timestamps (the files are /src/test/data/tx_valid.json and tx_invalid.json).
Does your test cover scenarios that are not covered in the existing tests?
nit: typo "meadian"
Do we need to create a new time-locked transaction? Cannot we use the one that is already in spendtx?
Here you are testing what will happen in the year 2106 if no changes are deployed in the meantime. Right?
Hi @mabu44 . Thanks for taking a look at this.
Yes I believe so. I took a look at the values in
tx_valid.jsonandtx_invalid.json. Here is a tableThis test also directly tests the median-time-past logic. This is not tested in
tx_{valid,invalid}.jsonas we just directly callVerifyScript()for individual transactions in the test files.Interestingly, i think there may be one test case overlap with
tx_valid.json. While this transaction could be valid with relay policy I don't believe a transaction with these locktimes would be allowed to be included in a block? This is because median-time-past logic for a block with this transaction could be not be satisfied as per my last test case here.Some more eyes on this could be good as I may be making a mistake.
I believe so because the previous one was mined in a valid block here:
github.com/Christewart/bitcoin@69b6bb9438/test/functional/feature_cltv.py (L213)So this is an integration test of:
with some more edge cases relative to the year 2106. Right?
I think that such a transaction will become relay-valid in 2106, but, unless consensus changes, it will never be mined because no blocks will be produced to mine it.
This tx was mined in line 195, not 213. I would move the creation of the transaction from line 230 to line 209. Otherwise, the check at line 218 could succeed not for the timelock (that is what we expect) but because of the duplicated transaction.
This line can be removed
This comment was not updated to reflect what the code is doing (same for the comments at line 256 and 258).
Thank you again for the careful review. I've integrated your code review in 024c3bf
Yes.
That is my understanding as well 👍
cc @petertodd
There hasn't been much activity lately. What is the status here?
Finding reviewers may take time. However, if the patch is no longer relevant, please close this pull request. If the author lost interest or time to work on this, please close it and mark it with one of the labels 'Up for grabs' or 'Insufficient Review', so that it can be picked up in the future.
If I understand this change correctly, this adds test cases for the finality of a block's transactions and how they might interplay with CLTV. Can you re-formulate the PR description to make it clear what this actually tests? Contrary to your current description, it looks like we are already testing unix timestamp values in the locktime extensively. Are the finality tests better suited for
feature_csv_activation?@Christewart any chance you want to follow up to the above?
Hi @sedited
feature_csv_activation.pytests relative lock times and theOP_CHECKSEQUENCEVERIFYopcode. This test case is testingOP_CHECKLOCKTIMEVERIFYand absolute wall clock locktimes ("unix timestamps"). Different fields on the transaction are used for verification (sequencevslocktime).In my opinion no so since this is testing absolute wall clock locktimes via a different opcode -
OP_CLTV.The real interesting part to me is the setting the unix timestamp of the node to
UINT32_MAX, and then attempting to spendOP_CLTVlocked utxos when we have reached theUINT32_MAXtimestamp in the locktime field.Since we cannot satisfy the MTP requirement of BIP113 (i.e. we cannot create a block with timestamp
UINT32_MAX+1so we are following BIP113 and monotonically increase the timestamp on the block) - the utxo can never be spent.From a quick glance through
feature_csv_activation.pyI don't see any references to2**32orUINT32_MAX- so I doubt this edge case is tested there - although if you could link to an exact line that it was tested on that would be helpful.In general, I think @mabu44 's comment was good:
https://github.com/bitcoin/bitcoin/pull/32229#pullrequestreview-2756380784
EDIT:
Looking closer at
feature_csv_activation.pyit does look like it tests BIP113 logic (its unfortunate the file is named after the opcode and is testing more than the opcode). The logic testingUINT32_MAXcould be moved over to that file, however theOP_CLTVtest logic should stay here and I still believe there is no integration tests for OP_CLTV and unix timestamps - please link if you find themYes, sorry for not being clearer on this before, but that is what I was suggesting.
Thanks for linking mabu44's comment again, I was referring to the same tests too. Maybe mention them in the PR descritpion?
I guess once that MTP is reached, we'll have to change some part of the calculation anyway, but I'm fine with having a test for documenting this.
Concept ACK
Hi @sedited , I pushed a new commit: 533d0c608312beaeb47bb5373169a17f9c756814
Here are the things that i've changed since you've last reviewed
UINT32_MAX/ median-time-past edge case out offeature_cltv.pyand intofeature_csv_activation.py, since that behavior is a general BIP113 property and not specific toOP_CLTV(per our earlier discussion).create_block'sheight=kwarg replacing the oldcreate_coinbase(...)calling convention), which broke a few call sites this PR touches. Fixed those up to match the new convention.Let me know if this addresses your concerns or if there's anything else you'd like changed.
Nit: Missing space (and for the other
cltv_validatecall too).How about:
As done further above in the file to capture the rejection reason?
If I remove this line, the test still passes. Is it required? Doesn't this also just append another scriptsig to the existing one?
Is this really exercising the edge case behaviour? It seems kind of obvious that for as long as the MTP isn't maxed out, the transaction will fail. From what I gather we want to show that a transaction with nLockTime will never be mined. Wouldn't it be better to mine to an MTP of UINT32_MAX, and then show that a) submitting a transaction to the mempool with nLockTime at UINT32_MAX fails, and that extending the chain further with a block containing the transaction will fail with "time-too-old".
@sedited I believe all your points are integrated into https://github.com/bitcoin/bitcoin/pull/32229/commits/1f05d8e0364fa4b009e558c97e821be70313b8bc
Why is this line changed? Seems like it is not actually changing anything behaviour wise.
The test passes if this line is left the same, so maybe this should not be changed?
Why twelve, doesn't this test only need eleven?
Might it be better to derive this from the mediantime directly? Something like:
Nit: create_block has a txlist arg that this without having to do it manually.
Looked at the latest version here and I think this is still not addressed, but my comment was not really helpful. The interesting case in my eyes is that on the final block, a UINT32_MAX - 1 timelocked transaction is considered standard and enters the mempool, but can't ever be mined. How about adding this:
@Christewart can you follow up here?
No verified findings are available from this review.
Review coverage was incomplete.
Reviewed:
1f05d8e0364f· Full review report · Previous reportspermalink
@Christewart interested in taking the review here?
I don't plan on picking this up. I feel like we are going in circles.