sapio 0.2.4

A programming framework for bitcoin smart contracts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright Judica, Inc 2021
//
// This Source Code Form is subject to the terms of the Mozilla Public
//  License, v. 2.0. If a copy of the MPL was not distributed with this
//  file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! The primary compilation traits and types
use super::actions::ConditionalCompileType;
use super::AnyContract;
use super::CompilationError;
use super::Compiled;
use super::Context;
use crate::contract::abi::continuation::ContinuationPoint;
use crate::contract::actions::conditional_compile::CCILWrapper;
use crate::contract::actions::CallableAsFoF;
use crate::contract::TxTmplIt;
use crate::util::amountrange::AmountRange;

use ::miniscript::*;
use bitcoin::schnorr::TweakedPublicKey;
use bitcoin::XOnlyPublicKey;
use sapio_base::effects::EffectDB;
use sapio_base::effects::EffectPath;
use sapio_base::effects::PathFragment;

use sapio_base::serialization_helpers::SArc;
use sapio_base::Clause;
use std::collections::{BTreeMap, BTreeSet};

use std::sync::Arc;
mod cache;
mod util;
use cache::*;
use util::*;
/// Used to prevent unintended callers to internal_clone.
pub struct InternalCompilerTag {
    _secret: (),
}

/// private::ImplSeal prevents anyone from implementing Compilable except by
/// implementing Contract.
mod private {
    pub trait ImplSeal {}
    /// Allow Contract to implement Compile
    impl ImplSeal for super::Compiled {}
    impl ImplSeal for bitcoin::XOnlyPublicKey {}
    impl<'a, C> ImplSeal for C where C: super::AnyContract {}
}
/// Compilable is a trait for anything which can be compiled
pub trait Compilable: private::ImplSeal {
    /// Compile a compilable object returning errors, if any.
    fn compile(&self, ctx: Context) -> Result<Compiled, CompilationError>;
}

/// Implements a basic identity
impl Compilable for Compiled {
    fn compile(&self, _ctx: Context) -> Result<Compiled, CompilationError> {
        Ok(self.clone())
    }
}

impl Compilable for bitcoin::XOnlyPublicKey {
    // TODO: Taproot; make infallible API
    fn compile(&self, ctx: Context) -> Result<Compiled, CompilationError> {
        let addr = bitcoin::Address::p2tr_tweaked(
            TweakedPublicKey::dangerous_assume_tweaked(*self),
            ctx.network,
        );
        let mut amt = AmountRange::new();
        amt.update_range(ctx.funds());
        Ok(Compiled::from_address(addr, Some(amt)))
    }
}

#[derive(PartialEq, Eq, Debug)]
enum Nullable {
    Yes,
    No,
}

const UNIQUE_DERIVE_PANIC_MSG: &str = "Must be a valid derivation or internal invariant not held";
fn compute_all_effects<C, A: Default>(
    mut top_effect_ctx: Context,
    self_ref: &C,
    func: &dyn CallableAsFoF<C, A>,
) -> TxTmplIt {
    let default_applied_effect_ctx = top_effect_ctx.derive(PathFragment::DefaultEffect)?;
    let def = func.call(self_ref, default_applied_effect_ctx, Default::default())?;
    if !func.web_api() {
        return Ok(def);
    }
    let mut applied_effects_ctx = top_effect_ctx.derive(PathFragment::Effects)?;
    let r = top_effect_ctx
        .get_effects(InternalCompilerTag { _secret: () })
        .get_value(top_effect_ctx.path())
        // always gets the default expansion, but will also attempt
        // operating with the effects passed in through the Context Object.
        .try_fold(def, |a, (k, arg)| -> TxTmplIt {
            let v = a;
            let c = applied_effects_ctx
                .derive(PathFragment::Named(SArc(k.clone())))
                .expect(UNIQUE_DERIVE_PANIC_MSG);
            let w = func.call_json(self_ref, c, arg.clone())?;
            Ok(Box::new(v.chain(w)))
        });
    r
}

struct Renamer {
    used_names: BTreeSet<String>,
}

impl Renamer {
    fn new() -> Self {
        Renamer {
            used_names: Default::default(),
        }
    }
    fn get_name(&mut self, a: &String) -> String {
        let count = 0u64;
        let mut name: String = a.clone();
        loop {
            if self.used_names.insert(name.clone()) {
                return name;
            } else {
                name = format!("{}_renamed_{}", a, count);
            }
        }
    }
}

#[derive(Default)]
struct ContinueAPIs {
    inner: BTreeMap<SArc<EffectPath>, ContinuationPoint>,
}

type ContinueAPIEntry = Option<(SArc<EffectPath>, ContinuationPoint)>;

impl Extend<ContinueAPIEntry> for ContinueAPIs {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = ContinueAPIEntry>,
    {
        self.inner.extend(iter.into_iter().flatten())
    }
}

impl<'a, T> Compilable for T
where
    T: AnyContract + 'a,
    T::Ref: 'a,
{
    /// The main Compilation Logic for a Contract.
    /// TODO: Better Document Semantics
    fn compile(&self, mut ctx: Context) -> Result<Compiled, CompilationError> {
        let self_ref = self.get_inner_ref();
        let mut guard_clauses = GuardCache::new();

        // The below maps track metadata that is useful for consumers / verification.
        // track transactions that are *guaranteed* via CTV
        let mut comitted_txns = BTreeMap::new();
        // All other transactions
        let mut other_txns = BTreeMap::new();

        // the min and max amount of funds spendable in the transactions
        let mut amount_range = AmountRange::new();

        // amount ensuring that the funds required don't get tweaked
        // during recompilation passes
        // TODO: Maybe do not just cloned?
        let amount_range_ctx = ctx.derive(PathFragment::Cloned)?;
        let ensured_amount = self.ensure_amount(amount_range_ctx)?;
        amount_range.update_range(ensured_amount);

        // The code for then_fns and finish_or_fns is very similar, differing
        // only in that then_fns have a CTV enforcing the contract and
        // finish_or_fns do not. We can lazily chain iterators to process them
        // in a row.
        //
        // we need a unique context for each.
        let mut action_ctx = ctx.derive(PathFragment::Action)?;
        let mut renamer = Renamer::new();
        let all_values = self
            .then_fns()
            .iter()
            .filter_map(|func| func())
            // We currently need to allocate for the the Callable as a
            // trait object since it only exists temporarily.
            // TODO: Without allocations?
            .map(|x| -> Box<dyn CallableAsFoF<_, _>> { Box::new(x) })
            .chain(self.finish_or_fns().iter().filter_map(|func| func()))
            .map(|mut x| {
                let new_name = Arc::new(renamer.get_name(x.get_name().as_ref()));
                x.rename(new_name.clone());
                let name = PathFragment::Named(SArc(new_name));
                let f_ctx = action_ctx.derive(name).expect(UNIQUE_DERIVE_PANIC_MSG);
                (f_ctx, x)
            })
            // flat_map will discard any
            // skippable / never branches here
            .flat_map(|(mut f_ctx, func)| {
                let mut this_ctx = f_ctx
                    // this should always be Ok(_)
                    .derive(PathFragment::CondCompIf)
                    .expect(UNIQUE_DERIVE_PANIC_MSG);
                match CCILWrapper(func.get_conditional_compile_if())
                    .assemble(self_ref, &mut this_ctx)
                {
                    // Throw errors
                    ConditionalCompileType::Fail(errors) => {
                        Some(Err(CompilationError::ConditionalCompilationFailed(errors)))
                    }
                    // Non nullable
                    ConditionalCompileType::Required | ConditionalCompileType::NoConstraint => {
                        Some(Ok((f_ctx, func, Nullable::No)))
                    }
                    // Nullable
                    ConditionalCompileType::Nullable => Some(Ok((f_ctx, func, Nullable::Yes))),
                    // Drop these
                    ConditionalCompileType::Skippable | ConditionalCompileType::Never => None,
                }
            })
            .map(|r| {
                let (mut f_ctx, func, nullability) = r?;
                let gctx = f_ctx.derive(PathFragment::Guard)?;
                let simp_ctx = f_ctx.derive(PathFragment::Metadata)?;
                // TODO: Suggested path frag?
                let (guards, guard_metadata) =
                    create_guards(self_ref, gctx, func.get_guard(), &mut guard_clauses)?;
                let effect_ctx = f_ctx.derive(if func.get_returned_txtmpls_modify_guards() {
                    PathFragment::Next
                } else {
                    PathFragment::Suggested
                })?;
                let effect_path = effect_ctx.path().clone();
                let transactions = compute_all_effects(effect_ctx, self_ref, func.as_ref());
                // If no guards and not CTV, then nothing gets added (not
                // interpreted as Trivial True)
                //   - If CTV and no guards, just CTV added.
                //   - If CTV and guards, CTV & guards added.
                // it would be an error if any of r_txtmpls is an error
                // instead of just an empty iterator.
                let txtmpl_clauses = transactions?
                    .map(|r_txtmpl| {
                        let txtmpl = r_txtmpl?;
                        let h = txtmpl.hash();
                        amount_range.update_range(txtmpl.max);
                        // Add the addition guards to these clauses
                        let txtmpl = if func.get_returned_txtmpls_modify_guards() {
                            &mut comitted_txns
                        } else {
                            &mut other_txns
                        }
                        .entry(h)
                        .or_insert(txtmpl);

                        let extractor = func.get_extract_clause_from_txtmpl();
                        (extractor)(txtmpl, &ctx)
                    })
                    // Drop None values
                    .filter_map(|s| s.transpose())
                    // Forces any error to abort the whole thing
                    .collect::<Result<Vec<Clause>, CompilationError>>()?;

                // N.B. the order of the matches below is significant
                Ok(if func.get_returned_txtmpls_modify_guards() {
                    let r = (
                        None,
                        combine_txtmpls(nullability, txtmpl_clauses, guards)?,
                        guard_metadata,
                    );
                    r
                } else {
                    let mut cp =
                        ContinuationPoint::at(func.get_schema().clone(), effect_path.clone());
                    for simp in func.gen_simps(self_ref, simp_ctx)? {
                        cp = cp.add_simp(simp.as_ref())?;
                    }
                    let v = optimizer_flatten_and_compile(guards)?;
                    (Some((SArc(effect_path), cp)), v, guard_metadata)
                })
            })
            .collect::<Result<Vec<(_, Vec<Miniscript<XOnlyPublicKey, Tap>>, _)>, CompilationError>>(
            )?;

        let mut continue_apis = ContinueAPIs::default();
        let mut clause_accumulator = vec![];
        let mut all_guard_simps: BTreeMap<Clause, GuardSimps> = Default::default();
        for (v, b, c) in all_values {
            continue_apis.extend(std::iter::once(v));
            clause_accumulator.push(b);
            for (pol, mut simps) in c {
                all_guard_simps.entry(pol).or_default().append(&mut simps)
            }
        }
        for guard_simps in all_guard_simps.values_mut() {
            guard_simps.sort_by_key(|k| k as *const _ as usize);
            guard_simps.dedup_by(|a, b| std::ptr::eq(a, b))
        }

        let branches: Vec<Miniscript<XOnlyPublicKey, Tap>> = {
            let mut finish_fns_ctx = ctx.derive(PathFragment::FinishFn)?;
            // Compute all finish_functions at this level, caching if requested.
            let guards = self
                .finish_fns()
                .iter()
                // note that this zip with would loop forever if there were to be a bug here
                .zip((0..).filter_map(|i| {
                    let mut new = finish_fns_ctx.derive(PathFragment::Branch(i as u64)).ok()?;
                    let simp = new.derive(PathFragment::Metadata).ok()?;
                    Some((new, simp))
                }))
                .filter_map(|(func, (c, simp_c))| {
                    guard_clauses.get(self_ref, *func, c, simp_c).transpose()
                })
                .collect::<Result<Vec<_>, _>>()?;
            let all_g = guards
                .into_iter()
                .map(|(policy, _m)| optimizer_flatten_and_compile(policy))
                .collect::<Result<Vec<_>, _>>()?;

            all_g
                .into_iter()
                .chain(clause_accumulator.into_iter())
                .flatten()
                .collect()
        };
        // TODO: Pick a better branch that is guaranteed to work!
        let some_key = pick_key_from_miniscripts(branches.iter());
        // Don't remove the key from the scripts in case it was bogus
        let tree = branches_to_tree(branches);
        let descriptor = Descriptor::Tr(descriptor::Tr::new(some_key, tree)?);
        let estimated_max_size = descriptor.max_satisfaction_weight()?;
        // TODO: Convert into an address instead of keeping descriptor,
        // hot-fix workaround
        let address = descriptor.clone().into();
        let descriptor = Some(descriptor.into());
        let root_path = SArc(ctx.path().clone());

        let failed_estimate = comitted_txns.values().any(|a| {
            // witness space not scaled
            let tx_size = a.tx.get_weight() + estimated_max_size;
            let fees = amount_range.max() - a.total_amount();
            a.min_feerate_sats_vbyte
                .map(|m| fees.as_sat() < (m.as_sat() * tx_size as u64))
                == Some(false)
        });
        if failed_estimate {
            Err(CompilationError::MinFeerateError)
        } else {
            let metadata_ctx = ctx.derive(PathFragment::Metadata)?;
            let metadata = self
                .metadata(metadata_ctx)?
                .add_guard_simps(all_guard_simps)?;
            Ok(Compiled {
                ctv_to_tx: comitted_txns,
                suggested_txs: other_txns,
                continue_apis: continue_apis.inner,
                root_path,
                address,
                descriptor,
                amount_range,
                metadata,
            })
        }
    }
}

fn optimizer_flatten_and_compile(
    guards: policy::Concrete<XOnlyPublicKey>,
) -> Result<Vec<Miniscript<XOnlyPublicKey, Tap>>, CompilationError> {
    let v = optimizer_flatten_policy(guards)
        .into_iter()
        .map(|g| g.compile())
        .collect::<Result<Vec<_>, _>>()?;
    Ok(v)
}

fn combine_txtmpls(
    nullability: Nullable,
    txtmpl_clauses: Vec<Clause>,
    guards: Clause,
) -> Result<Vec<Miniscript<XOnlyPublicKey, Tap>>, CompilationError> {
    match (nullability, txtmpl_clauses.len(), guards) {
        // This is a nullable branch without any proposed
        // transactions.
        // Therefore, mark this branch dead.
        (Nullable::Yes, 0, _) => Ok(vec![]),
        // Error if we expect CTV, returned some templates, but our guard
        // was unsatisfiable, irrespective of nullability. This is because
        // the behavior should be captured through a compile_if if it is
        // intended.
        (_, n, Clause::Unsatisfiable) if n > 0 => {
            // TODO: Turn into a warning that the intended
            // behavior should be to compile_if
            Err(CompilationError::MissingTemplates)
        }
        // Error if 0 templates return and we don't want to be nullable
        (Nullable::No, 0, _) => Err(CompilationError::MissingTemplates),
        // If the guard is trivial, return the hashes standalone
        (_, _, Clause::Trivial) => {
            let r = Ok(txtmpl_clauses
                .into_iter()
                .map(|policy| policy.compile().map_err(Into::<CompilationError>::into))
                .collect::<Result<Vec<_>, _>>()?);
            r
        }
        // If the guard is non-trivial, zip it to each hash
        // TODO: Arc in miniscript to dedup memory?
        //       This could be Clause::Shared(x) or something...
        (_, _, guards) => Ok(txtmpl_clauses
            .into_iter()
            // extra_guards will contain any CTV
            .map(|extra_guards| {
                Clause::And(vec![guards.clone(), extra_guards])
                    .compile()
                    .map_err(Into::<CompilationError>::into)
            })
            .collect::<Result<Vec<_>, _>>()?),
    }
}

fn optimizer_flatten_policy(p: Clause) -> Vec<Clause> {
    match p {
        policy::Concrete::Or(v) => v
            .into_iter()
            .flat_map(|(_, b)| optimizer_flatten_policy(b))
            .collect(),
        policy::Concrete::Threshold(1, v) => {
            v.into_iter().flat_map(optimizer_flatten_policy).collect()
        }
        p => vec![p],
    }
}