协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
bitcoin-timelocks【开发与工程】Bitcoin time-locks: nLockTime (BIP65 absolute), nSequence (BIP68/112 relative), CLTV/CSV opcodes, MTP rule (BIP113). Block-height vs unix-time, encoding rules. USE WHEN: building HTLCs, vaults, scheduled payments, escrow with cooldown, refund paths.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Two locking dimensions: absolute (lock until time/block T) and relative (lock for N blocks/seconds after parent confirms).
nLockTime field + CLTV opcodenLockTime (4 bytes, in tx):
0 → tx is valid immediately.< 500_000_000 → tx valid only at/after block height N.≥ 500_000_000 → tx valid only at/after unix timestamp.For nLockTime to be enforced, at least one input must have
nSequence < 0xffffffff.
<TARGET_HEIGHT_OR_TIME> OP_CHECKLOCKTIMEVERIFY OP_DROP <... rest of script>
OP_CLTV aborts the script if tx.nLockTime < target. Doesn't pop
the value, so OP_DROP follows.
Use case: scheduled payment that becomes valid after a date.
When comparing time-based (≥ 500M) lock against block timestamps,
use median time past of the last 11 blocks, not the current block's
timestamp. Mitigates miner fudging.
nSequence field + CSV opcodenSequence (4 bytes, per input):
0x80000000) → relative locktime DISABLED for this
input. Required for legacy txs (nSequence = 0xfffffffe or
0xffffffff).0x00400000):
Requires tx.version ≥ 2 for relative timelock to be enforced.
<DELAY> OP_CHECKSEQUENCEVERIFY OP_DROP <... rest of script>
OP_CSV checks the input's sequence against the encoded delay value
in the same format (block-height / 512s units). Aborts if input's
nSequence < delay (with bit conventions matched).
Use case: vault cooldown, HTLC timeout path, channel commitment delay.
Block-based:
DELAY = 144 # ≈ 1 day
nSequence = DELAY # bit 22 = 0, low 16 = 144
Time-based:
DELAY_SECONDS = 86400 / 512 = 169 # ≈ 1 day in 512s units
nSequence = (1 << 22) | 169 # 0x004000A9
In script: <DELAY> CSV where DELAY uses the same encoding.
IF
<preimage_hash> OP_HASH160 OP_EQUALVERIFY <recipient_pubkey> OP_CHECKSIG
ELSE
<timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP <sender_pubkey> OP_CHECKSIG
ENDIF
Recipient redeems with preimage; sender refunds after timeout.
IF
<144> OP_CSV OP_DROP <hot_pubkey> OP_CHECKSIG
ELSE
<cold_pubkey> OP_CHECKSIG
ENDIF
<2_500_000> OP_CLTV OP_DROP <recipient_pubkey> OP_CHECKSIG
Becomes spendable at block 2,500,000.
In Tapscript, opcodes work the same. CSV / CLTV are valid in leaf
scripts. Combine with OP_CHECKSIGADD for multisig + timelock:
<DELAY> CSV DROP <pkA> CHECKSIG <pkB> CHECKSIGADD <2> NUMEQUAL
(2-of-2 with cooldown.)
tx.version = 2 — relative timelocks don't apply.0xfffffffe) — disables RBF and disables
relative timelock for that input.