Skip to content

Commit 7236b57

Browse files
authored
Merge pull request #138 from JeremyRubin/add-trivial-unsat-concrete
Add Trivial and Unsatisfiable types to Concrete Policy
2 parents efd971b + da6ae53 commit 7236b57

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/policy/compiler.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ struct CompilerExtData {
161161

162162
impl Property for CompilerExtData {
163163
fn from_true() -> Self {
164-
// only used in casts. should never be computed directly
165-
unreachable!();
164+
CompilerExtData {
165+
branch_prob: None,
166+
sat_cost: 0.0,
167+
dissat_cost: None,
168+
}
166169
}
167170

168171
fn from_false() -> Self {
@@ -811,6 +814,12 @@ where
811814
}
812815

813816
match *policy {
817+
Concrete::Unsatisfiable => {
818+
insert_wrap!(AstElemExt::terminal(Terminal::False));
819+
}
820+
Concrete::Trivial => {
821+
insert_wrap!(AstElemExt::terminal(Terminal::True));
822+
}
814823
Concrete::Key(ref pk) => {
815824
insert_wrap!(AstElemExt::terminal(Terminal::PkH(
816825
pk.to_pubkeyhash().clone()

src/policy/concrete.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ use {Error, MiniscriptKey};
3737
/// to assist the compiler
3838
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3939
pub enum Policy<Pk: MiniscriptKey> {
40+
/// Unsatisfiable
41+
Unsatisfiable,
42+
/// Trivially satisfiable
43+
Trivial,
4044
/// A public key which must sign to satisfy the descriptor
4145
Key(Pk),
4246
/// An absolute locktime restriction
@@ -147,6 +151,8 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
147151
Q: MiniscriptKey,
148152
{
149153
match *self {
154+
Policy::Unsatisfiable => Ok(Policy::Unsatisfiable),
155+
Policy::Trivial => Ok(Policy::Trivial),
150156
Policy::Key(ref pk) => translatefpk(pk).map(Policy::Key),
151157
Policy::Sha256(ref h) => Ok(Policy::Sha256(h.clone())),
152158
Policy::Hash256(ref h) => Ok(Policy::Hash256(h.clone())),
@@ -192,7 +198,9 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
192198
fn check_timelocks_helper(&self) -> TimeLockInfo {
193199
// timelocks[csv_h, csv_t, cltv_h, cltv_t, combination]
194200
match *self {
195-
Policy::Key(_)
201+
Policy::Unsatisfiable
202+
| Policy::Trivial
203+
| Policy::Key(_)
196204
| Policy::Sha256(_)
197205
| Policy::Hash256(_)
198206
| Policy::Ripemd160(_)
@@ -284,6 +292,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
284292
///
285293
pub fn is_safe_nonmalleable(&self) -> (bool, bool) {
286294
match *self {
295+
Policy::Unsatisfiable | Policy::Trivial => (true, true),
287296
Policy::Key(_) => (true, true),
288297
Policy::Sha256(_)
289298
| Policy::Hash256(_)
@@ -330,6 +339,8 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
330339
impl<Pk: MiniscriptKey> fmt::Debug for Policy<Pk> {
331340
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
332341
match *self {
342+
Policy::Unsatisfiable => f.write_str("UNSATISFIABLE()"),
343+
Policy::Trivial => f.write_str("TRIVIAL()"),
333344
Policy::Key(ref pk) => write!(f, "pk({:?})", pk),
334345
Policy::After(n) => write!(f, "after({})", n),
335346
Policy::Older(n) => write!(f, "older({})", n),
@@ -371,6 +382,8 @@ impl<Pk: MiniscriptKey> fmt::Debug for Policy<Pk> {
371382
impl<Pk: MiniscriptKey> fmt::Display for Policy<Pk> {
372383
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
373384
match *self {
385+
Policy::Unsatisfiable => f.write_str("UNSATISFIABLE"),
386+
Policy::Trivial => f.write_str("TRIVIAL"),
374387
Policy::Key(ref pk) => write!(f, "pk({})", pk),
375388
Policy::After(n) => write!(f, "after({})", n),
376389
Policy::Older(n) => write!(f, "older({})", n),
@@ -468,6 +481,8 @@ where
468481
}
469482
}
470483
match (frag_name, top.args.len() as u32) {
484+
("UNSATISFIABLE", 0) => Ok(Policy::Unsatisfiable),
485+
("TRIVIAL", 0) => Ok(Policy::Trivial),
471486
("pk", 1) => expression::terminal(&top.args[0], |pk| Pk::from_str(pk).map(Policy::Key)),
472487
("after", 1) => {
473488
let num = expression::terminal(&top.args[0], |x| expression::parse_num(x))?;

src/policy/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
141141
// involving combination of timelocks and heightlocks
142142
self.check_timelocks()?;
143143
let ret = match *self {
144+
Concrete::Unsatisfiable => Semantic::Unsatisfiable,
145+
Concrete::Trivial => Semantic::Trivial,
144146
Concrete::Key(ref pk) => Semantic::KeyHash(pk.to_pubkeyhash()),
145147
Concrete::After(t) => Semantic::After(t),
146148
Concrete::Older(t) => Semantic::Older(t),

0 commit comments

Comments
 (0)