Skip to content

Commit cf56672

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#994: Add a LockTime type
0ed78e5 Add lock time types (Tobin C. Harding) 1390ee1 Add a max scriptnum constant (Tobin C. Harding) Pull request description: Implement a `LockTime` type that adds support for lock time values based on nLockTime and OP_CHECKLOCKTIMEVERIFY. For example usage in `rust-miniscript` please see rust-bitcoin/rust-miniscript#408. ### Notes: I probably should have opened a new PR, this is a total re-write and very different from what is being discussed below, sorry, my bad. This is just half of the 'timelock' story, its the easier half. The reason I switched terminology from timelock to locktime is that we have to compare two u32s so it does not make sense to call them _both_ timelocks. If I have missed, or apparently ignored, anything you said reviewers please accept my apology in advance, it was not intentional. The thread of discussion is long and this topic is complex. Please do restate your views liberally :) Here is a useful blog post I used while touching up on timelock specifics: https://medium.com/summa-technology/bitcoins-time-locks-27e0c362d7a1. It links to all the relevant bips too. ACKs for top commit: Kixunil: ACK 0ed78e5 apoelstra: ACK 0ed78e5 Tree-SHA512: 486fcce859b38fa1e8e6b11cd2f494462d6d7d1d9030d096ce6b260f6c9d0342b8952afe35152bdf3afe323a234a8165ac3d6c946304afcc13406d7a0489d75a
2 parents 8b35d43 + 7fb774b commit cf56672

File tree

10 files changed

+844
-29
lines changed

10 files changed

+844
-29
lines changed

examples/ecdsa-psbt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use bitcoin::util::bip32::{
4343
};
4444
use bitcoin::util::psbt::{self, Input, Psbt, PsbtSighashType};
4545
use bitcoin::{
46-
Address, Amount, Network, OutPoint, PrivateKey, PublicKey, Script, Sequence, Transaction, TxIn,
47-
TxOut, Txid, Witness,
46+
Address, Amount, Network, OutPoint, PackedLockTime, PrivateKey, PublicKey, Script, Sequence,
47+
Transaction, TxIn, TxOut, Txid, Witness,
4848
};
4949

5050
use self::psbt_sign::*;
@@ -207,7 +207,7 @@ impl WatchOnly {
207207

208208
let tx = Transaction {
209209
version: 2,
210-
lock_time: 0,
210+
lock_time: PackedLockTime::ZERO,
211211
input: vec![TxIn {
212212
previous_output: OutPoint {
213213
txid: Txid::from_hex(INPUT_UTXO_TXID)?,

src/blockdata/constants.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::hashes::hex::{self, HexIterator};
1616
use crate::hashes::{Hash, sha256d};
1717
use crate::blockdata::opcodes;
1818
use crate::blockdata::script;
19+
use crate::blockdata::locktime::PackedLockTime;
1920
use crate::blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn, Sequence};
2021
use crate::blockdata::block::{Block, BlockHeader};
2122
use crate::blockdata::witness::Witness;
@@ -53,6 +54,8 @@ pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 196; // 0xc4
5354
pub const MAX_SCRIPT_ELEMENT_SIZE: usize = 520;
5455
/// How may blocks between halvings.
5556
pub const SUBSIDY_HALVING_INTERVAL: u32 = 210_000;
57+
/// Maximum allowed value for an integer in Script.
58+
pub const MAX_SCRIPTNUM_VALUE: u32 = 0x80000000; // 2^31
5659

5760
/// In Bitcoind this is insanely described as ~((u256)0 >> 32)
5861
pub fn max_target(_: Network) -> Uint256 {
@@ -71,7 +74,7 @@ fn bitcoin_genesis_tx() -> Transaction {
7174
// Base
7275
let mut ret = Transaction {
7376
version: 1,
74-
lock_time: 0,
77+
lock_time: PackedLockTime::ZERO,
7578
input: vec![],
7679
output: vec![],
7780
};
@@ -199,6 +202,7 @@ mod test {
199202
use crate::hashes::hex::{ToHex, FromHex};
200203
use crate::network::constants::Network;
201204
use crate::consensus::encode::serialize;
205+
use crate::blockdata::locktime::PackedLockTime;
202206

203207
#[test]
204208
fn bitcoin_genesis_first_transaction() {
@@ -216,7 +220,7 @@ mod test {
216220
assert_eq!(serialize(&gen.output[0].script_pubkey),
217221
Vec::from_hex("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac").unwrap());
218222
assert_eq!(gen.output[0].value, 50 * COIN_VALUE);
219-
assert_eq!(gen.lock_time, 0);
223+
assert_eq!(gen.lock_time, PackedLockTime::ZERO);
220224

221225
assert_eq!(gen.wtxid().to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
222226
}

0 commit comments

Comments
 (0)