Repository navigation
tests: Add unix timestamp tests for OP_CLTV and max mediantimepast tests - #32229
Christewart wants to merge 1 commit into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste |
4f483ea to
4d2e108
Compare
4d2e108 to
69b6bb9
Compare
There was a problem hiding this comment.
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?
|
Hi @mabu44 . Thanks for taking a look at this.
Yes I believe so. I took a look at the values in
This test also directly tests the median-time-past logic. This is not tested in Interestingly, i think there may be one test case overlap with
Some more eyes on this could be good as I may be making a mistake. |
69b6bb9 to
062c31b
Compare
mabu44
left a comment
There was a problem hiding this comment.
So this is an integration test of:
- Transaction validity based on input script and transaction locktime: currently tested in transactions_tests.cpp;
- Transaction validity based on median block time: currently tested in miner_tests.cpp (lines 525-534) and feature_csv_activation.py (lines 340-355)
with some more edge cases relative to the year 2106. Right?
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.
| 4294967295 | 4294967295 | true |
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.
|
Thank you again for the careful review. I've integrated your code review in 024c3bf
Yes.
That is my understanding as well 👍 |
024c3bf to
cbc1095
Compare
|
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 |
|
@Christewart any chance you want to follow up to the above? |
|
Hi @sedited
In my opinion no so since this is testing absolute wall clock locktimes via a different opcode - The real interesting part to me is the setting the unix timestamp of the node to Since we cannot satisfy the MTP requirement of BIP113 (i.e. we cannot create a block with timestamp From a quick glance through In general, I think @mabu44 's comment was good: EDIT: Looking closer at |
Yes, 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 |
cbc1095 to
533d0c6
Compare
|
Hi @sedited , I pushed a new commit: 533d0c6 Here are the things that i've changed since you've last reviewed
Let me know if this addresses your concerns or if there's anything else you'd like changed. |
| # Mine 6 blocks at UINT32_MAX - 1 so that MTP (the median of the last 11 blocks) becomes UINT32_MAX - 1 | ||
| for _ in range(6): | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX - 1) | ||
| block.solve() | ||
| self.send_blocks([block]) | ||
| self.tip = block.hash_int | ||
| self.tipheight += 1 | ||
| assert_equal(self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'], UINT32_MAX - 1) | ||
|
|
||
| # UINT32_MAX - 2 is the highest locktime that can be included in the next block: | ||
| # BIP113 requires nLockTime to be strictly less than the MTP of the previous block | ||
| max_locktime_tx = mtp_edge_case_tx(mtp_edge_case_inputs[0], UINT32_MAX - 2) | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX, txlist=[max_locktime_tx]) | ||
| block.solve() | ||
| self.send_blocks([block]) | ||
| self.tip = block.hash_int | ||
| self.tipheight += 1 | ||
|
|
||
| # A transaction locked to UINT32_MAX itself is never final: MTP can never exceed | ||
| # UINT32_MAX, so "nLockTime < MTP" can never be satisfied for nLockTime == UINT32_MAX | ||
| never_final_tx = mtp_edge_case_tx(mtp_edge_case_inputs[1], UINT32_MAX) | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX, txlist=[never_final_tx]) | ||
| block.solve() | ||
| self.send_blocks([block], success=False, reject_reason='bad-txns-nonfinal') |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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:
diff --git a/test/functional/feature_csv_activation.py b/test/functional/feature_csv_activation.py
index 410278f407b..943890d81bc 100755
--- a/test/functional/feature_csv_activation.py
+++ b/test/functional/feature_csv_activation.py
@@ -61,7 +61,7 @@ from test_framework.wallet import (
)
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
-MTP_EDGE_CASE_INPUT_COUNT = 2 # Extra inputs for the "median-time-past can never reach UINT32_MAX" BIP113 test
+MTP_EDGE_CASE_INPUT_COUNT = 3 # Extra inputs for the "median-time-past can never reach UINT32_MAX" BIP113 test
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT + MTP_EDGE_CASE_INPUT_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
BASE_RELATIVE_LOCKTIME = 10
SEQ_DISABLE_FLAG = 1 << 31
@@ -520,6 +520,10 @@ class BIP68_112_113Test(BitcoinTestFramework):
block.solve()
self.send_blocks([block], success=False, reject_reason='bad-txns-nonfinal')
+ # A transaction locked to UINT32_MAX - 1 is not final until the last block is mined
+ max_locktime_standard_tx = mtp_edge_case_tx(mtp_edge_case_inputs[2], UINT32_MAX - 1)
+ assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, max_locktime_standard_tx.serialize().hex())
+
# max out MTP on the network to UINT32_MAX, so that the next block's nTime can't exceed it (and thus any block will be rejected for time-too-old)
while self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'] < UINT32_MAX:
block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX)
@@ -530,6 +534,10 @@ class BIP68_112_113Test(BitcoinTestFramework):
assert_equal(self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'], UINT32_MAX)
+ # The transaction locked to UINT32_MAX - 1 is now final and enters the mempool, but there can't be another block, so it won't be mined
+ self.nodes[0].sendrawtransaction(max_locktime_standard_tx.serialize().hex())
+ assert max_locktime_standard_tx.txid_hex in self.nodes[0].getrawmempool()
+
# submitting a transaction to the mempool with nLockTime at UINT32_MAX fails
assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, never_final_tx.serialize().hex())…d BIP113 interaction Add a functional (p2p/block-validation) test for OP_CHECKLOCKTIMEVERIFY using unix-timestamp locktimes, including its interplay with BIP113 median-time-past (MTP) rules: a block containing an OP_CLTV-locked output is only accepted once MTP satisfies the requested locktime. Also add a BIP113 test to feature_csv_activation.py showing that MTP can never exceed UINT32_MAX, so an output with a plain nLockTime of UINT32_MAX can never be confirmed: BIP113 requires nLockTime to be strictly less than MTP, and MTP itself can reach but never exceed UINT32_MAX (nTime is a uint32 field, and a block's timestamp must exceed the MTP of the blocks preceding it, so once MTP hits UINT32_MAX no further block -- and thus no further MTP advance -- is possible). This is a general BIP113 property independent of OP_CLTV, so it lives alongside the other BIP113 tests rather than in feature_cltv.py.
d6fb328 to
1f05d8e
Compare
| def cltv_validate(tx, height): | ||
| # Modify the signature in vin 0 and nSequence/nLockTime of the tx to pass CLTV | ||
| scheme = [[CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP], 0, height] | ||
| scheme = [[CScriptNum.encode(CScriptNum(height))[1:], OP_CHECKLOCKTIMEVERIFY, OP_DROP], 0, height] |
There was a problem hiding this comment.
Why is this line changed? Seems like it is not actually changing anything behaviour wise.
|
|
||
|
|
||
| CLTV_HEIGHT = 111 | ||
| CLTV_HEIGHT = 112 |
There was a problem hiding this comment.
The test passes if this line is left the same, so maybe this should not be changed?
| self.log.info("Mining %d blocks", CLTV_HEIGHT - 2) | ||
| self.generate(wallet, 10) | ||
| self.generate(self.nodes[0], CLTV_HEIGHT - 2 - 10) | ||
| num_utxos = 12 |
There was a problem hiding this comment.
Why twelve, doesn't this test only need eleven?
| tip = block.hash_int | ||
|
|
||
| current_time = int(time.time()) | ||
| locktime = current_time + (60 * 60) # 1 hour |
There was a problem hiding this comment.
Might it be better to derive this from the mediantime directly? Something like:
locktime = self.nodes[0].getblockheader(block.hash_hex)['mediantime'] + (60 * 60) # 1 hour| block2 = create_block(tip, height=CLTV_HEIGHT + 1, ntime=locktime, version=4) | ||
| block2.vtx.append(spendtx) | ||
| block2.hashMerkleRoot = block2.calc_merkle_root() |
There was a problem hiding this comment.
Nit: create_block has a txlist arg that this without having to do it manually.
| # Mine 6 blocks at UINT32_MAX - 1 so that MTP (the median of the last 11 blocks) becomes UINT32_MAX - 1 | ||
| for _ in range(6): | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX - 1) | ||
| block.solve() | ||
| self.send_blocks([block]) | ||
| self.tip = block.hash_int | ||
| self.tipheight += 1 | ||
| assert_equal(self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'], UINT32_MAX - 1) | ||
|
|
||
| # UINT32_MAX - 2 is the highest locktime that can be included in the next block: | ||
| # BIP113 requires nLockTime to be strictly less than the MTP of the previous block | ||
| max_locktime_tx = mtp_edge_case_tx(mtp_edge_case_inputs[0], UINT32_MAX - 2) | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX, txlist=[max_locktime_tx]) | ||
| block.solve() | ||
| self.send_blocks([block]) | ||
| self.tip = block.hash_int | ||
| self.tipheight += 1 | ||
|
|
||
| # A transaction locked to UINT32_MAX itself is never final: MTP can never exceed | ||
| # UINT32_MAX, so "nLockTime < MTP" can never be satisfied for nLockTime == UINT32_MAX | ||
| never_final_tx = mtp_edge_case_tx(mtp_edge_case_inputs[1], UINT32_MAX) | ||
| block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX, txlist=[never_final_tx]) | ||
| block.solve() | ||
| self.send_blocks([block], success=False, reject_reason='bad-txns-nonfinal') |
There was a problem hiding this comment.
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:
diff --git a/test/functional/feature_csv_activation.py b/test/functional/feature_csv_activation.py
index 410278f407b..943890d81bc 100755
--- a/test/functional/feature_csv_activation.py
+++ b/test/functional/feature_csv_activation.py
@@ -61,7 +61,7 @@ from test_framework.wallet import (
)
TESTING_TX_COUNT = 83 # Number of testing transactions: 1 BIP113 tx, 16 BIP68 txs, 66 BIP112 txs (see comments above)
-MTP_EDGE_CASE_INPUT_COUNT = 2 # Extra inputs for the "median-time-past can never reach UINT32_MAX" BIP113 test
+MTP_EDGE_CASE_INPUT_COUNT = 3 # Extra inputs for the "median-time-past can never reach UINT32_MAX" BIP113 test
COINBASE_BLOCK_COUNT = TESTING_TX_COUNT + MTP_EDGE_CASE_INPUT_COUNT # Number of coinbase blocks we need to generate as inputs for our txs
BASE_RELATIVE_LOCKTIME = 10
SEQ_DISABLE_FLAG = 1 << 31
@@ -520,6 +520,10 @@ class BIP68_112_113Test(BitcoinTestFramework):
block.solve()
self.send_blocks([block], success=False, reject_reason='bad-txns-nonfinal')
+ # A transaction locked to UINT32_MAX - 1 is not final until the last block is mined
+ max_locktime_standard_tx = mtp_edge_case_tx(mtp_edge_case_inputs[2], UINT32_MAX - 1)
+ assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, max_locktime_standard_tx.serialize().hex())
+
# max out MTP on the network to UINT32_MAX, so that the next block's nTime can't exceed it (and thus any block will be rejected for time-too-old)
while self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'] < UINT32_MAX:
block = create_block(self.tip, height=self.tipheight + 1, ntime=UINT32_MAX)
@@ -530,6 +534,10 @@ class BIP68_112_113Test(BitcoinTestFramework):
assert_equal(self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())['mediantime'], UINT32_MAX)
+ # The transaction locked to UINT32_MAX - 1 is now final and enters the mempool, but there can't be another block, so it won't be mined
+ self.nodes[0].sendrawtransaction(max_locktime_standard_tx.serialize().hex())
+ assert max_locktime_standard_tx.txid_hex in self.nodes[0].getrawmempool()
+
# submitting a transaction to the mempool with nLockTime at UINT32_MAX fails
assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, never_final_tx.serialize().hex())|
@Christewart can you follow up here? |
@Christewart interested in taking the review here? |
|
I don't plan on picking this up. I feel like we are going in circles. |

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.