Skip to main content

kaspa_txscript/
engine_context.rs

1use crate::caches::Cache;
2use crate::covenants::{CovenantsContext, EMPTY_COV_CONTEXT};
3use crate::{SeqCommitAccessor, SigCacheKey};
4use kaspa_consensus_core::hashing::sighash::{SigHashReusedValues, SigHashReusedValuesSync, SigHashReusedValuesUnsync};
5
6/// Marker type indicating that sig-hash reused values have not been bound yet.
7#[derive(Default)]
8pub struct MissingReusedValues;
9
10pub struct EngineContext<'a, Reused> {
11    pub(crate) reused_values: &'a Reused,
12    pub(crate) sig_cache: &'a SigCache,
13    pub(crate) covenants_ctx: &'a CovenantsContext,
14    pub(crate) seq_commit_accessor: Option<&'a dyn SeqCommitAccessor>,
15}
16
17impl<'a, Reused> EngineContext<'a, Reused> {
18    /// Attach a covenants context to an existing engine context.
19    #[inline]
20    pub fn with_covenants_ctx(mut self, covenants_ctx: &'a CovenantsContext) -> Self {
21        self.covenants_ctx = covenants_ctx;
22        self
23    }
24
25    #[inline]
26    pub fn with_seq_commit_accessor(mut self, seq_commit_accessor: &'a dyn SeqCommitAccessor) -> Self {
27        self.seq_commit_accessor = Some(seq_commit_accessor);
28        self
29    }
30
31    #[inline]
32    pub fn with_seq_commit_accessor_opt(mut self, seq_commit_accessor: Option<&'a dyn SeqCommitAccessor>) -> Self {
33        self.seq_commit_accessor = seq_commit_accessor;
34        self
35    }
36}
37
38type SigCache = Cache<SigCacheKey, bool>;
39
40impl<'a> EngineContext<'a, MissingReusedValues> {
41    const MISSING: MissingReusedValues = MissingReusedValues;
42
43    /// Create a context without bound reused values, using an empty covenants context.
44    pub fn new(sig_cache: &'a SigCache) -> Self {
45        Self { reused_values: &Self::MISSING, sig_cache, covenants_ctx: &EMPTY_COV_CONTEXT, seq_commit_accessor: None }
46    }
47
48    /// Upgrade the context by binding concrete sig-hash reused values (one-way).
49    #[inline]
50    pub fn with_reused<New: SigHashReusedValues>(self, reused_values: &'a New) -> EngineContext<'a, New> {
51        EngineContext {
52            reused_values,
53            sig_cache: self.sig_cache,
54            covenants_ctx: self.covenants_ctx,
55            seq_commit_accessor: self.seq_commit_accessor,
56        }
57    }
58}
59
60impl<'a, Reused: SigHashReusedValues> Clone for EngineContext<'a, Reused> {
61    fn clone(&self) -> Self {
62        *self
63    }
64}
65
66impl<'a, Reused: SigHashReusedValues> Copy for EngineContext<'a, Reused> {}
67
68/// Engine context before binding sig-hash reused values.
69pub type EngineCtx<'a> = EngineContext<'a, MissingReusedValues>;
70
71/// Engine context for parallel script checking (thread-safe reused values).
72pub type EngineCtxSync<'a> = EngineContext<'a, SigHashReusedValuesSync>;
73
74/// Engine context for sequential script checking (non-thread-safe reused values).
75pub type EngineCtxUnsync<'a> = EngineContext<'a, SigHashReusedValuesUnsync>;