Skip to content

Commit c98de73

Browse files
committed
WIP: Use new LockTime type
1 parent d589fe9 commit c98de73

File tree

25 files changed

+179
-173
lines changed

25 files changed

+179
-173
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ no-std = ["hashbrown", "bitcoin/no-std"]
1717
compiler = []
1818
trace = []
1919
unstable = []
20-
use-serde = ["serde", "bitcoin/use-serde"]
20+
use-serde = ["serde", "bitcoin/serde"]
2121
rand = ["bitcoin/rand"]
2222

2323
[dependencies]
24-
bitcoin = { version = "0.28.1", default-features = false }
24+
bitcoin = { path = "../rust-bitcoin", default-features = false }
2525
serde = { version = "1.0", optional = true }
2626
hashbrown = { version = "0.11", optional = true }
2727

2828
[dev-dependencies]
29-
bitcoind = {version = "0.26.1", features=["22_0"]}
29+
bitcoind = { path = "../../RCasatta/bitcoind", features=["22_0"] }
3030
actual-rand = { package = "rand", version = "0.8.4"}
3131
secp256k1 = {version = "0.22.1", features = ["rand-std"]}
32+
bitcoin = { path = "../rust-bitcoin", features = ["rand"] }
3233

3334
[[example]]
3435
name = "htlc"

examples/sign_multisig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use std::collections::HashMap;
1818
use std::str::FromStr;
1919

20+
use bitcoin::{secp256k1, LockTime};
2021
use bitcoin::blockdata::witness::Witness;
21-
use bitcoin::secp256k1;
2222

2323
fn main() {
2424
let mut tx = spending_transaction();
@@ -91,7 +91,7 @@ fn main() {
9191
fn spending_transaction() -> bitcoin::Transaction {
9292
bitcoin::Transaction {
9393
version: 2,
94-
lock_time: 0,
94+
lock_time: LockTime::ZERO,
9595
input: vec![bitcoin::TxIn {
9696
previous_output: Default::default(),
9797
script_sig: bitcoin::Script::new(),

examples/verify_tx.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use std::str::FromStr;
1818

19+
use bitcoin::LockTime;
1920
use bitcoin::consensus::Decodable;
2021
use bitcoin::secp256k1::{self, Secp256k1};
2122
use bitcoin::util::sighash;
@@ -34,7 +35,7 @@ fn main() {
3435
&tx.input[0].script_sig,
3536
&tx.input[0].witness,
3637
0,
37-
0,
38+
LockTime::ZERO,
3839
)
3940
.unwrap();
4041

src/descriptor/bare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636

3737
/// Create a Bare Descriptor. That is descriptor that is
3838
/// not wrapped in sh or wsh. This covers the Pk descriptor
39-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
39+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
4040
pub struct Bare<Pk: MiniscriptKey> {
4141
/// underlying miniscript
4242
ms: Miniscript<Pk, BareCtx>,

src/descriptor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub use self::key::{
7171
pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;
7272

7373
/// Script descriptor
74-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
74+
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash)]
7575
pub enum Descriptor<Pk: MiniscriptKey> {
7676
/// A raw scriptpubkey (including pay-to-pubkey) under Legacy context
7777
Bare(Bare<Pk>),

src/descriptor/segwitv0.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
Translator,
3333
};
3434
/// A Segwitv0 wsh descriptor
35-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
35+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
3636
pub struct Wsh<Pk: MiniscriptKey> {
3737
/// underlying miniscript
3838
inner: WshInner<Pk>,
@@ -176,7 +176,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
176176
}
177177

178178
/// Wsh Inner
179-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
179+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
180180
pub enum WshInner<Pk: MiniscriptKey> {
181181
/// Sorted Multi
182182
SortedMulti(SortedMultiVec<Pk, Segwitv0>),

src/descriptor/sh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ use crate::{
3636
};
3737

3838
/// A Legacy p2sh Descriptor
39-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
39+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
4040
pub struct Sh<Pk: MiniscriptKey> {
4141
/// underlying miniscript
4242
inner: ShInner<Pk>,
4343
}
4444

4545
/// Sh Inner
46-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
46+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
4747
pub enum ShInner<Pk: MiniscriptKey> {
4848
/// Nested Wsh
4949
Wsh(Wsh<Pk>),

src/descriptor/tr.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
/// A Taproot Tree representation.
2626
// Hidden leaves are not yet supported in descriptor spec. Conceptually, it should
2727
// be simple to integrate those here, but it is best to wait on core for the exact syntax.
28-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
28+
#[derive(Clone, PartialOrd, Eq, PartialEq, Hash)]
2929
pub enum TapTree<Pk: MiniscriptKey> {
3030
/// A taproot tree structure
3131
Tree(Arc<TapTree<Pk>>, Arc<TapTree<Pk>>),
@@ -89,16 +89,6 @@ impl<Pk: MiniscriptKey> PartialOrd for Tr<Pk> {
8989
}
9090
}
9191

92-
impl<Pk: MiniscriptKey> Ord for Tr<Pk> {
93-
fn cmp(&self, other: &Self) -> cmp::Ordering {
94-
match self.internal_key.cmp(&other.internal_key) {
95-
cmp::Ordering::Equal => {}
96-
ord => return ord,
97-
}
98-
self.tree.cmp(&other.tree)
99-
}
100-
}
101-
10292
impl<Pk: MiniscriptKey> hash::Hash for Tr<Pk> {
10393
fn hash<H: hash::Hasher>(&self, state: &mut H) {
10494
self.internal_key.hash(state);
@@ -257,6 +247,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
257247
TaprootBuilderError::EmptyTree => {
258248
unreachable!("Taptree is a well formed tree with atleast 1 element")
259249
}
250+
_ => unreachable!("non_exhaustive catchall")
260251
},
261252
}
262253
};

src/interpreter/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use crate::prelude::*;
2929
pub enum Error {
3030
/// Could not satisfy, absolute locktime not met
3131
AbsoluteLocktimeNotMet(u32),
32+
/// Could not satisfy, lock time values are different units
33+
AbsoluteLocktimeComparisonInvalid(u32, u32),
3234
/// Cannot Infer a taproot descriptor
3335
/// Key spends cannot infer the internal key of the descriptor
3436
/// Inferring script spends is possible, but is hidden nodes are currently
@@ -187,6 +189,7 @@ impl fmt::Display for Error {
187189
Error::VerifyFailed => {
188190
f.write_str("Expected Satisfied Boolean at stack top for VERIFY")
189191
}
192+
_ => f.write_str("Unknown error, non_exhaustive catch all"),
190193
}
191194
}
192195
}
@@ -234,6 +237,7 @@ impl error::Error for Error {
234237
Secp(e) => Some(e),
235238
SchnorrSig(e) => Some(e),
236239
SighashError(e) => Some(e),
240+
_ => None // non_exhaustive catch all.
237241
}
238242
}
239243
}

src/interpreter/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::str::FromStr;
2525
use bitcoin::blockdata::witness::Witness;
2626
use bitcoin::hashes::{hash160, ripemd160, sha256};
2727
use bitcoin::util::{sighash, taproot};
28-
use bitcoin::{self, secp256k1, TxOut};
28+
use bitcoin::{self, secp256k1, TxOut, LockTime};
2929

3030
use crate::miniscript::context::NoChecks;
3131
use crate::miniscript::ScriptContext;
@@ -49,7 +49,7 @@ pub struct Interpreter<'txin> {
4949
/// is the leaf script; for key-spends it is `None`.
5050
script_code: Option<bitcoin::Script>,
5151
age: u32,
52-
lock_time: u32,
52+
lock_time: LockTime,
5353
}
5454

5555
// A type representing functions for checking signatures that accept both
@@ -173,8 +173,8 @@ impl<'txin> Interpreter<'txin> {
173173
spk: &bitcoin::Script,
174174
script_sig: &'txin bitcoin::Script,
175175
witness: &'txin Witness,
176-
age: u32, // CSV, relative lock time.
177-
lock_time: u32, // CLTV, absolute lock time.
176+
age: u32, // CSV, relative lock time.
177+
lock_time: LockTime, // CLTV, absolute lock time.
178178
) -> Result<Self, Error> {
179179
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
180180
Ok(Interpreter {
@@ -496,7 +496,7 @@ pub enum SatisfiedConstraint {
496496
///Absolute Timelock for CLTV.
497497
AbsoluteTimelock {
498498
/// The value of Absolute timelock
499-
time: u32,
499+
n: LockTime,
500500
},
501501
}
502502

@@ -532,7 +532,7 @@ pub struct Iter<'intp, 'txin: 'intp> {
532532
state: Vec<NodeEvaluationState<'intp>>,
533533
stack: Stack<'txin>,
534534
age: u32,
535-
lock_time: u32,
535+
lock_time: LockTime,
536536
has_errored: bool,
537537
}
538538

@@ -1145,7 +1145,7 @@ mod tests {
11451145
n_satisfied: 0,
11461146
}],
11471147
age: 1002,
1148-
lock_time: 1002,
1148+
lock_time: LockTime::from_consensus(1002),
11491149
has_errored: false,
11501150
}
11511151
}
@@ -1208,7 +1208,7 @@ mod tests {
12081208
let after_satisfied: Result<Vec<SatisfiedConstraint>, Error> = constraints.collect();
12091209
assert_eq!(
12101210
after_satisfied.unwrap(),
1211-
vec![SatisfiedConstraint::AbsoluteTimelock { time: 1000 }]
1211+
vec![SatisfiedConstraint::AbsoluteTimelock { n: LockTime::from_consensus(1000) }]
12121212
);
12131213

12141214
//Check Older

0 commit comments

Comments
 (0)