Skip to main content

blvm_consensus/block/
script_cache.rs

1//! Script verification flags and script-exec cache helpers for block validation.
2//!
3//! Groups base/per-tx script flags, cache insertion/merge, and BIP143 precompute
4//! so block connect logic stays in the parent module.
5
6use crate::activation::{ForkActivationTable, IsForkActive};
7use crate::constants::*;
8use crate::opcodes::*;
9use crate::script::flags::{
10    SCRIPT_VERIFY_P2SH, SCRIPT_VERIFY_TAPROOT, SCRIPT_VERIFY_WITNESS,
11    SCRIPT_VERIFY_WITNESS_PUBKEYTYPE,
12};
13use crate::segwit::{is_segwit_transaction, Witness};
14use crate::transaction::is_coinbase;
15use crate::types::*;
16use blvm_spec_lock::spec_locked;
17#[cfg(feature = "production")]
18use rustc_hash::{FxHashMap, FxHashSet};
19use std::sync::LazyLock;
20
21use super::BlockValidationContext;
22
23// ---------------------------------------------------------------------------
24// Script flags (base, per-tx, and combined)
25// ---------------------------------------------------------------------------
26
27/// Base script flags for a block from activation context.
28/// Call once per block, then use `calculate_script_flags_for_block` or `add_per_tx_script_flags`.
29#[spec_locked("5.2.5", "CalculateScriptFlags")]
30#[inline]
31pub(crate) fn calculate_base_script_flags_for_block(
32    height: u64,
33    activation: &impl IsForkActive,
34) -> u32 {
35    let mut flags: u32 = 0;
36
37    if activation.is_fork_active(ForkId::Bip16, height) {
38        flags |= 0x01; // SCRIPT_VERIFY_P2SH
39    }
40    // BIP66: strict DER for ECDSA signatures only. Bitcoin Core `GetBlockScriptFlags`
41    // adds SCRIPT_VERIFY_DERSIG here — not SCRIPT_VERIFY_STRICTENC or SCRIPT_VERIFY_LOW_S
42    // (those are standardness / mempool policy; legacy blocks may contain high-S sigs).
43    if activation.is_fork_active(ForkId::Bip66, height) {
44        flags |= 0x04; // SCRIPT_VERIFY_DERSIG
45    }
46    if activation.is_fork_active(ForkId::Bip65, height) {
47        flags |= 0x200; // CHECKLOCKTIMEVERIFY
48    }
49    // BIP112 (CSV): CHECKSEQUENCEVERIFY at CSV deployment height (mainnet 419328).
50    // Bitcoin Core `GetBlockScriptFlags`: DEPLOYMENT_CSV, not SegWit.
51    if activation.is_fork_active(ForkId::Bip112, height) {
52        flags |= 0x400; // SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
53    }
54    // BIP147 NULLDUMMY: Bitcoin Core enables at DEPLOYMENT_SEGWIT (same height as BIP147 on mainnet).
55    if activation.is_fork_active(ForkId::Bip147, height) {
56        flags |= 0x10; // SCRIPT_VERIFY_NULLDUMMY
57    }
58    #[cfg(feature = "ctv")]
59    if activation.is_fork_active(ForkId::Ctv, height) {
60        flags |= 0x80000000; // CHECK_TEMPLATE_VERIFY_HASH
61    }
62
63    flags
64}
65
66/// Convenience: base script flags from (height, network) when no context is available (e.g. mempool).
67#[inline]
68pub fn calculate_base_script_flags_for_block_network(
69    height: u64,
70    network: crate::types::Network,
71) -> u32 {
72    let table = ForkActivationTable::from_network(network);
73    calculate_base_script_flags_for_block(height, &table)
74}
75
76/// Per-tx script flags (SegWit + Taproot). Add to base flags from `calculate_base_script_flags_for_block`.
77#[spec_locked("5.2.5", "CalculateScriptFlags")]
78#[inline]
79fn add_per_tx_script_flags(
80    base_flags: u32,
81    tx: &Transaction,
82    has_witness: bool,
83    height: u64,
84    activation: &impl IsForkActive,
85) -> u32 {
86    let mut flags = base_flags;
87    if activation.is_fork_active(ForkId::SegWit, height)
88        && (has_witness || is_segwit_transaction(tx))
89    {
90        flags |= 0x800;
91    }
92    if activation.is_fork_active(ForkId::Taproot, height) {
93        for output in &tx.outputs {
94            let script = &output.script_pubkey;
95            if script.len() == TAPROOT_SCRIPT_LENGTH
96                && script[0] == OP_1
97                && script[1] == PUSH_32_BYTES
98            {
99                flags |= 0x8000;
100                break;
101            }
102        }
103    }
104    flags
105}
106
107/// Calculate script verification flags for a transaction in a block (with activation context).
108#[spec_locked("5.2.5", "CalculateScriptFlags")]
109pub(crate) fn calculate_script_flags_for_block(
110    tx: &Transaction,
111    has_witness: bool,
112    height: u64,
113    activation: &impl IsForkActive,
114) -> u32 {
115    let base = calculate_base_script_flags_for_block(height, activation);
116    add_per_tx_script_flags(base, tx, has_witness, height, activation)
117}
118
119/// Convenience: script flags from (height, network) when no context is available (e.g. mempool, bench tools).
120#[spec_locked("5.2.5", "CalculateScriptFlags")]
121pub fn calculate_script_flags_for_block_network(
122    tx: &Transaction,
123    has_witness: bool,
124    height: u64,
125    network: crate::types::Network,
126) -> u32 {
127    let table = ForkActivationTable::from_network(network);
128    calculate_script_flags_for_block(tx, has_witness, height, &table)
129}
130
131/// Calculate script verification flags for a transaction in a block (with precomputed base flags).
132#[spec_locked("5.2.5", "CalculateScriptFlags")]
133#[inline]
134pub(crate) fn calculate_script_flags_for_block_with_base(
135    tx: &Transaction,
136    has_witness: bool,
137    base_flags: u32,
138    height: u64,
139    activation: &impl IsForkActive,
140) -> u32 {
141    add_per_tx_script_flags(base_flags, tx, has_witness, height, activation)
142}
143
144// ---------------------------------------------------------------------------
145// §5.2.6 Script flag exceptions (Orange Paper; table from Bitcoin Core chainparams)
146// ---------------------------------------------------------------------------
147
148/// RPC / explorer 64-hex block id → canonical digest order used by `hash256(serialize(header))` here.
149fn block_hash_from_rpc_hex(hex_str: &str) -> Hash {
150    let mut bytes = hex::decode(hex_str).expect("valid 64-char block hash hex");
151    assert_eq!(bytes.len(), 32);
152    bytes.reverse();
153    bytes.try_into().expect("length 32")
154}
155
156/// BIP16 exception block (mainnet). `SCRIPT_VERIFY_NONE` in Core.
157static MAINNET_SCRIPT_FLAG_EXCEPTION_BIP16: LazyLock<Hash> = LazyLock::new(|| {
158    block_hash_from_rpc_hex("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22")
159});
160/// Taproot exception block (mainnet). `SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS` in Core.
161static MAINNET_SCRIPT_FLAG_EXCEPTION_TAPROOT: LazyLock<Hash> = LazyLock::new(|| {
162    block_hash_from_rpc_hex("0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad")
163});
164/// BIP16 exception block (testnet3). `SCRIPT_VERIFY_NONE` in Core.
165static TESTNET_SCRIPT_FLAG_EXCEPTION_BIP16: LazyLock<Hash> = LazyLock::new(|| {
166    block_hash_from_rpc_hex("00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105")
167});
168
169/// Consensus script-flag override for `block_hash` on `network`, if any (partial map entry).
170///
171/// Bitcoin Core: `Consensus::Params::script_flag_exceptions` (`src/kernel/chainparams.cpp`).
172#[spec_locked("5.2.6", "ScriptFlagExceptions")]
173pub fn script_flag_exceptions_lookup(block_hash: &Hash, network: Network) -> Option<u32> {
174    match network {
175        Network::Mainnet => {
176            if block_hash == &*MAINNET_SCRIPT_FLAG_EXCEPTION_BIP16 {
177                Some(0)
178            } else if block_hash == &*MAINNET_SCRIPT_FLAG_EXCEPTION_TAPROOT {
179                Some(0x01 | 0x800) // SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS
180            } else {
181                None
182            }
183        }
184        Network::Testnet => {
185            if block_hash == &*TESTNET_SCRIPT_FLAG_EXCEPTION_BIP16 {
186                Some(0)
187            } else {
188                None
189            }
190        }
191        Network::Regtest => None,
192    }
193}
194
195/// Bitcoin Core `GetBlockScriptFlags` (`validation.cpp`): start from `SCRIPT_VERIFY_P2SH |
196/// SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_TAPROOT`, replace with the script-flag exception entry
197/// when present, then OR buried deployments (BIP66, BIP65, CSV, BIP147-at-SegWit). Same **block-level**
198/// bitmask is passed to script checks for every non-coinbase transaction.
199///
200/// This is **not** the Orange Paper §5.2.6 piecewise `GetBlockScriptFlags` (exception vs per-tx
201/// `CalculateScriptFlags`); use [`get_block_script_flags`] for that formulation. `connect_block`
202/// uses this function for **mainnet consensus parity** with Bitcoin Core.
203pub fn get_block_script_verify_flags_core(
204    block_hash: &Hash,
205    height: u64,
206    activation: &impl IsForkActive,
207    network: Network,
208) -> u32 {
209    // Baseline: P2SH + WITNESS + WITNESS_PUBKEYTYPE + TAPROOT.
210    // TAPROOT is 0x20000 (bit 17). Using 0x8000 here was a bug — that is WITNESS_PUBKEYTYPE.
211    let mut flags = SCRIPT_VERIFY_P2SH
212        | SCRIPT_VERIFY_WITNESS
213        | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
214        | SCRIPT_VERIFY_TAPROOT;
215    if let Some(v) = script_flag_exceptions_lookup(block_hash, network) {
216        flags = v;
217    }
218    if activation.is_fork_active(ForkId::Bip66, height) {
219        flags |= 0x04; // SCRIPT_VERIFY_DERSIG
220    }
221    if activation.is_fork_active(ForkId::Bip65, height) {
222        flags |= 0x200; // CHECKLOCKTIMEVERIFY
223    }
224    if activation.is_fork_active(ForkId::Bip112, height) {
225        flags |= 0x400; // CHECKSEQUENCEVERIFY
226    }
227    if activation.is_fork_active(ForkId::Bip147, height) {
228        flags |= 0x10; // NULLDUMMY (Core: `DEPLOYMENT_SEGWIT`)
229    }
230    #[cfg(feature = "ctv")]
231    if activation.is_fork_active(ForkId::Ctv, height) {
232        flags |= 0x80000000;
233    }
234    flags
235}
236
237/// Orange Paper §5.2.6: exception table wins when defined; otherwise per-tx `CalculateScriptFlags`.
238///
239/// **Note:** Bitcoin Core’s `GetBlockScriptFlags` (`validation.cpp`) starts from a default mask, applies
240/// this exception table, then ORs buried deployments (BIP66, BIP65, CSV, BIP147). **`connect_block`**
241/// uses [`get_block_script_verify_flags_core`] for that Core behavior; this function remains the
242/// Orange Paper §5.2.6 piecewise spec.
243#[spec_locked("5.2.6", "GetBlockScriptFlags")]
244pub fn get_block_script_flags(
245    block_hash: &Hash,
246    tx: &Transaction,
247    has_witness: bool,
248    height: u64,
249    network: Network,
250) -> u32 {
251    if let Some(flags) = script_flag_exceptions_lookup(block_hash, network) {
252        flags
253    } else {
254        calculate_script_flags_for_block_network(tx, has_witness, height, network)
255    }
256}
257
258// ---------------------------------------------------------------------------
259// Script-exec cache and overlay merge
260// ---------------------------------------------------------------------------
261
262/// Insert script exec cache keys for all txs in block (call when block validation passes).
263#[cfg(all(feature = "production", feature = "rayon"))]
264pub(super) fn insert_script_exec_cache_for_block(
265    block: &Block,
266    witnesses: &[Vec<Witness>],
267    height: u64,
268    context: &BlockValidationContext,
269) {
270    let block_hash = crate::crypto::OptimizedSha256::new().hash256(
271        &crate::serialization::block::serialize_block_header(&block.header),
272    );
273    let block_script_verify_flags =
274        get_block_script_verify_flags_core(&block_hash, height, context, context.network);
275    for (i, tx) in block.transactions.iter().enumerate() {
276        if is_coinbase(tx) {
277            continue;
278        }
279        let wits = witnesses.get(i).map(|w| w.as_slice()).unwrap_or(&[]);
280        let witnesses_vec: Vec<_> = if wits.len() == tx.inputs.len() {
281            wits.to_vec()
282        } else {
283            (0..tx.inputs.len()).map(|_| Vec::new()).collect()
284        };
285        let key =
286            crate::script_exec_cache::compute_key(tx, &witnesses_vec, block_script_verify_flags);
287        crate::script_exec_cache::insert(&key);
288    }
289}
290
291/// Merge overlay changes into cache. Updates bip30_index and optionally builds undo log.
292/// When `undo_log` is None (IBD mode), skips undo entry construction entirely.
293#[cfg(feature = "production")]
294pub(super) fn merge_overlay_changes_to_cache(
295    additions: &FxHashMap<OutPoint, std::sync::Arc<UTXO>>,
296    deletions: &FxHashSet<crate::utxo_overlay::UtxoDeletionKey>,
297    utxo_set: &mut UtxoSet,
298    mut bip30_index: Option<&mut crate::bip_validation::Bip30Index>,
299    mut undo_log: Option<&mut crate::reorganization::BlockUndoLog>,
300) {
301    use crate::reorganization::UndoEntry;
302
303    for del_key in deletions {
304        let outpoint = crate::utxo_overlay::utxo_deletion_key_to_outpoint(del_key);
305        if let Some(arc) = utxo_set.remove(&outpoint) {
306            if let Some(idx) = bip30_index.as_deref_mut() {
307                if arc.is_coinbase {
308                    if let std::collections::hash_map::Entry::Occupied(mut o) =
309                        idx.entry(outpoint.hash)
310                    {
311                        *o.get_mut() = o.get().saturating_sub(1);
312                        if *o.get() == 0 {
313                            o.remove();
314                        }
315                    }
316                }
317            }
318            if let Some(ref mut log) = undo_log {
319                log.entries.push(UndoEntry {
320                    outpoint,
321                    previous_utxo: Some(arc),
322                    new_utxo: None,
323                });
324            }
325        }
326    }
327    for (outpoint, arc) in additions {
328        if let Some(ref mut log) = undo_log {
329            log.entries.push(UndoEntry {
330                outpoint: *outpoint,
331                previous_utxo: None,
332                new_utxo: Some(std::sync::Arc::clone(arc)),
333            });
334        }
335        utxo_set.insert(*outpoint, std::sync::Arc::clone(arc));
336    }
337}
338
339/// Compute BIP143/precomputed sighash for CCheckQueue path. Uses local refs and specs Vecs
340/// (dropped before return) so buf borrow ends.
341#[cfg(all(feature = "production", feature = "rayon"))]
342pub(super) fn compute_bip143_and_precomp(
343    tx: &Transaction,
344    prevout_values: &[i64],
345    script_pubkey_indices: &[(usize, usize)],
346    script_pubkey_buffer: &[u8],
347    has_witness: bool,
348) -> (
349    Option<crate::transaction_hash::Bip143PrecomputedHashes>,
350    Vec<Option<[u8; 32]>>,
351) {
352    let buf = script_pubkey_buffer;
353    let refs: Vec<&[u8]> = script_pubkey_indices
354        .iter()
355        .map(|&(s, l)| buf[s..s + l].as_ref())
356        .collect();
357    let refs: &[&[u8]] = &refs;
358    if has_witness {
359        let bip =
360            crate::transaction_hash::Bip143PrecomputedHashes::compute(tx, prevout_values, refs);
361        let mut precomp = vec![None; script_pubkey_indices.len()];
362        let mut specs: Vec<(usize, u8, &[u8])> = Vec::new();
363        for (j, &(s, l)) in script_pubkey_indices.iter().enumerate() {
364            let spk = &buf[s..s + l];
365            if spk.len() == 22 && spk[0] == OP_0 && spk[1] == PUSH_20_BYTES {
366                let mut script_code = [0u8; 25];
367                script_code[0] = OP_DUP;
368                script_code[1] = OP_HASH160;
369                script_code[2] = PUSH_20_BYTES;
370                script_code[3..23].copy_from_slice(&spk[2..22]);
371                script_code[23] = OP_EQUALVERIFY;
372                script_code[24] = OP_CHECKSIG;
373                let amount = prevout_values.get(j).copied().unwrap_or(0);
374                if let Ok(h) = crate::transaction_hash::calculate_bip143_sighash(
375                    tx,
376                    j,
377                    &script_code,
378                    amount,
379                    0x01,
380                    Some(&bip),
381                ) {
382                    precomp[j] = Some(h);
383                }
384            } else if spk.len() == 23
385                && spk[0] == OP_HASH160
386                && spk[1] == PUSH_20_BYTES
387                && spk[22] == OP_EQUAL
388            {
389                if let Some((sighash_byte, redeem)) =
390                    crate::script::parse_p2sh_p2pkh_for_precompute(&tx.inputs[j].script_sig)
391                {
392                    specs.push((j, sighash_byte, redeem));
393                }
394            }
395        }
396        if !specs.is_empty() {
397            if let Ok(hashes) = crate::transaction_hash::batch_compute_legacy_sighashes(
398                tx,
399                prevout_values,
400                refs,
401                &specs,
402            ) {
403                for (k, &(j, _, _)) in specs.iter().enumerate() {
404                    precomp[j] = Some(hashes[k]);
405                }
406            }
407        }
408        (Some(bip), precomp)
409    } else {
410        let mut precomp = vec![None; script_pubkey_indices.len()];
411        let mut specs: Vec<(usize, u8, &[u8])> = Vec::new();
412        for (j, &(s, l)) in script_pubkey_indices.iter().enumerate() {
413            let spk = &buf[s..s + l];
414            if spk.len() == 25
415                && spk[0] == OP_DUP
416                && spk[1] == OP_HASH160
417                && spk[2] == PUSH_20_BYTES
418                && spk[23] == OP_EQUALVERIFY
419                && spk[24] == OP_CHECKSIG
420            {
421                let script_sig = &tx.inputs[j].script_sig;
422                if let Some((sig, _pubkey)) = crate::script::parse_p2pkh_script_sig(script_sig) {
423                    if !sig.is_empty() {
424                        specs.push((j, sig[sig.len() - 1], spk));
425                    }
426                }
427            } else if spk.len() == 23
428                && spk[0] == OP_HASH160
429                && spk[1] == PUSH_20_BYTES
430                && spk[22] == OP_EQUAL
431            {
432                if let Some((sighash_byte, redeem)) =
433                    crate::script::parse_p2sh_p2pkh_for_precompute(&tx.inputs[j].script_sig)
434                {
435                    specs.push((j, sighash_byte, redeem));
436                }
437            }
438        }
439        if !specs.is_empty() {
440            if let Ok(hashes) = crate::transaction_hash::batch_compute_legacy_sighashes(
441                tx,
442                prevout_values,
443                refs,
444                &specs,
445            ) {
446                for (k, &(j, _, _)) in specs.iter().enumerate() {
447                    precomp[j] = Some(hashes[k]);
448                }
449            }
450        }
451        (None, precomp)
452    }
453}
454
455#[cfg(test)]
456mod script_flag_exceptions_tests {
457    use super::{
458        calculate_script_flags_for_block_network, get_block_script_flags,
459        get_block_script_verify_flags_core, script_flag_exceptions_lookup,
460    };
461    use crate::activation::ForkActivationTable;
462    use crate::crypto::OptimizedSha256;
463    use crate::script::flags::{
464        SCRIPT_VERIFY_P2SH, SCRIPT_VERIFY_TAPROOT, SCRIPT_VERIFY_WITNESS,
465        SCRIPT_VERIFY_WITNESS_PUBKEYTYPE,
466    };
467    use crate::serialization::block::{deserialize_block_header, serialize_block_header};
468    use crate::types::{Network, Transaction};
469
470    fn hash_from_rpc_hex(hex_str: &str) -> [u8; 32] {
471        let mut bytes = hex::decode(hex_str).unwrap();
472        assert_eq!(bytes.len(), 32);
473        bytes.reverse();
474        bytes.try_into().unwrap()
475    }
476
477    #[test]
478    fn mainnet_bip16_exception_zero() {
479        let h =
480            hash_from_rpc_hex("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
481        assert_eq!(script_flag_exceptions_lookup(&h, Network::Mainnet), Some(0));
482    }
483
484    #[test]
485    fn mainnet_taproot_exception_p2sh_witness() {
486        let h =
487            hash_from_rpc_hex("0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad");
488        assert_eq!(
489            script_flag_exceptions_lookup(&h, Network::Mainnet),
490            Some(0x01 | 0x800)
491        );
492    }
493
494    #[test]
495    fn testnet_bip16_exception_zero() {
496        let h =
497            hash_from_rpc_hex("00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105");
498        assert_eq!(script_flag_exceptions_lookup(&h, Network::Testnet), Some(0));
499        assert_eq!(script_flag_exceptions_lookup(&h, Network::Mainnet), None);
500    }
501
502    #[test]
503    fn regtest_has_no_exceptions() {
504        let h =
505            hash_from_rpc_hex("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
506        assert_eq!(script_flag_exceptions_lookup(&h, Network::Regtest), None);
507    }
508
509    #[test]
510    fn get_block_script_flags_uses_exception() {
511        let h =
512            hash_from_rpc_hex("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
513        let tx = Transaction {
514            version: 1,
515            inputs: Default::default(),
516            outputs: Default::default(),
517            lock_time: 0,
518        };
519        let flags = get_block_script_flags(&h, &tx, false, 1_000_000, Network::Mainnet);
520        assert_eq!(flags, 0);
521    }
522
523    #[test]
524    fn get_block_script_flags_falls_back_to_calculate() {
525        let h = [7u8; 32];
526        let tx = Transaction {
527            version: 1,
528            inputs: Default::default(),
529            outputs: Default::default(),
530            lock_time: 0,
531        };
532        assert_eq!(
533            get_block_script_flags(&h, &tx, false, 100, Network::Mainnet),
534            calculate_script_flags_for_block_network(&tx, false, 100, Network::Mainnet),
535        );
536    }
537
538    #[test]
539    fn genesis_rpc_hex_matches_hash256_of_header() {
540        let genesis_block_hex = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c010100000001000000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000";
541        let bytes = hex::decode(genesis_block_hex).unwrap();
542        let header = deserialize_block_header(&bytes[..80]).unwrap();
543        let digest = OptimizedSha256::new().hash256(&serialize_block_header(&header));
544        let expected =
545            hash_from_rpc_hex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
546        assert_eq!(digest, expected);
547    }
548
549    #[test]
550    fn core_block_flags_non_exception_includes_p2sh_witness_taproot_and_deployments() {
551        let table = ForkActivationTable::from_network(Network::Mainnet);
552        let h = [0xabu8; 32];
553        let flags = get_block_script_verify_flags_core(&h, 800_000, &table, Network::Mainnet);
554        // P2SH (0x1) | WITNESS (0x800) | WITNESS_PUBKEYTYPE (0x8000) | TAPROOT (0x20000)
555        assert_eq!(
556            flags
557                & (SCRIPT_VERIFY_P2SH
558                    | SCRIPT_VERIFY_WITNESS
559                    | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
560                    | SCRIPT_VERIFY_TAPROOT),
561            SCRIPT_VERIFY_P2SH
562                | SCRIPT_VERIFY_WITNESS
563                | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
564                | SCRIPT_VERIFY_TAPROOT,
565            "P2SH | WITNESS | WITNESS_PUBKEYTYPE | TAPROOT baseline"
566        );
567        assert_ne!(flags & 0x04, 0, "DERSIG");
568        assert_ne!(flags & 0x200, 0, "CLTV");
569        assert_ne!(flags & 0x400, 0, "CSV");
570        assert_ne!(flags & 0x10, 0, "NULLDUMMY");
571    }
572
573    #[test]
574    fn core_block_flags_bip16_exception_orrs_buried_deployments() {
575        let table = ForkActivationTable::from_network(Network::Mainnet);
576        let h =
577            hash_from_rpc_hex("00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
578        assert_eq!(script_flag_exceptions_lookup(&h, Network::Mainnet), Some(0));
579        let flags = get_block_script_verify_flags_core(&h, 800_000, &table, Network::Mainnet);
580        let deployment_mask = 0x04 | 0x200 | 0x400 | 0x10;
581        assert_eq!(flags & 0x8801, 0);
582        assert_eq!(flags & deployment_mask, deployment_mask);
583        assert_eq!(flags & 0x800, 0);
584        assert_eq!(flags & 0x8000, 0);
585        assert_eq!(flags & 0x01, 0);
586    }
587
588    #[test]
589    fn core_block_flags_taproot_exception_orrs_deployments() {
590        let table = ForkActivationTable::from_network(Network::Mainnet);
591        let h =
592            hash_from_rpc_hex("0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad");
593        let flags = get_block_script_verify_flags_core(&h, 800_000, &table, Network::Mainnet);
594        assert_eq!(flags & 0x8801, 0x801);
595        assert_eq!(flags & 0x8000, 0);
596        assert_ne!(flags & 0x800, 0);
597        assert_ne!(flags & 0x04, 0);
598    }
599}