Skip to main content

bsv_script/interpreter/
config.rs

1//! Interpreter configuration with pre/post-genesis limits.
2
3/// Maximum number of non-push opcodes allowed before genesis activation.
4pub const MAX_OPS_BEFORE_GENESIS: usize = 500;
5/// Maximum combined stack size (data + alt) allowed before genesis activation.
6pub const MAX_STACK_SIZE_BEFORE_GENESIS: usize = 1000;
7/// Maximum script byte size allowed before genesis activation.
8pub const MAX_SCRIPT_SIZE_BEFORE_GENESIS: usize = 10000;
9/// Maximum single data element byte size allowed before genesis activation.
10pub const MAX_SCRIPT_ELEMENT_SIZE_BEFORE_GENESIS: usize = 520;
11/// Maximum byte length for numeric script values before genesis activation.
12pub const MAX_SCRIPT_NUMBER_LENGTH_BEFORE_GENESIS: usize = 4;
13/// Maximum number of public keys in a multisig operation before genesis activation.
14pub const MAX_PUB_KEYS_PER_MULTISIG_BEFORE_GENESIS: usize = 20;
15
16/// Script configuration limits.
17pub struct Config {
18    /// Whether post-genesis rules are active (relaxes most limits).
19    pub after_genesis: bool,
20}
21
22impl Config {
23    /// Create a configuration with pre-genesis (legacy) limits.
24    pub fn before_genesis() -> Self {
25        Config { after_genesis: false }
26    }
27
28    /// Create a configuration with post-genesis (relaxed) limits.
29    pub fn after_genesis() -> Self {
30        Config { after_genesis: true }
31    }
32
33    /// Return the maximum number of non-push opcodes allowed per script.
34    pub fn max_ops(&self) -> usize {
35        if self.after_genesis { i32::MAX as usize } else { MAX_OPS_BEFORE_GENESIS }
36    }
37
38    /// Return the maximum combined stack size (data + alt).
39    pub fn max_stack_size(&self) -> usize {
40        if self.after_genesis { i32::MAX as usize } else { MAX_STACK_SIZE_BEFORE_GENESIS }
41    }
42
43    /// Return the maximum script byte size.
44    pub fn max_script_size(&self) -> usize {
45        if self.after_genesis { i32::MAX as usize } else { MAX_SCRIPT_SIZE_BEFORE_GENESIS }
46    }
47
48    /// Return the maximum byte size for a single data element.
49    pub fn max_script_element_size(&self) -> usize {
50        if self.after_genesis { i32::MAX as usize } else { MAX_SCRIPT_ELEMENT_SIZE_BEFORE_GENESIS }
51    }
52
53    /// Return the maximum byte length for numeric script values.
54    pub fn max_script_number_length(&self) -> usize {
55        if self.after_genesis { 750 * 1000 } else { MAX_SCRIPT_NUMBER_LENGTH_BEFORE_GENESIS }
56    }
57
58    /// Return the maximum number of public keys in a multisig operation.
59    pub fn max_pub_keys_per_multisig(&self) -> usize {
60        if self.after_genesis { i32::MAX as usize } else { MAX_PUB_KEYS_PER_MULTISIG_BEFORE_GENESIS }
61    }
62}