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
//! Unified fork activation: table and trait for "is fork X active at height H?".
//!
//! Used by block validation to query activation from a single precomputed table
//! (built by the node from constants, version-bits, and config overrides)
//! without passing per-BIP parameters.
use blvm_spec_lock::spec_locked;
use crate::constants::*;
use crate::types::{ForkId, Network};
/// Trait for querying fork activation at a given height.
///
/// Implemented by `ForkActivationTable` and by `BlockValidationContext` (delegating to its table).
/// Allows validation code to ask "is fork F active?" without depending on a concrete context type.
pub trait IsForkActive {
/// Returns true if the fork is active at the given block height.
fn is_fork_active(&self, fork: ForkId, height: u64) -> bool;
}
/// Precomputed activation heights for all built-in forks.
///
/// Fixed-size storage: one field per fork (no HashMap). Built by the node from
/// chain params, version-bits (e.g. BIP54), and config overrides. Consensus only reads.
#[derive(Debug, Clone)]
pub struct ForkActivationTable {
/// BIP30: active when height <= this (deactivation fork).
pub bip30_deactivation: u64,
/// Activation heights (active when height >= value; u64::MAX = never active).
pub bip16: u64,
pub bip34: u64,
pub bip66: u64,
pub bip65: u64,
pub bip112: u64,
pub bip147: u64,
pub segwit: u64,
pub taproot: u64,
pub ctv: u64,
pub csfs: u64,
pub bip54: u64,
}
impl IsForkActive for ForkActivationTable {
#[inline]
fn is_fork_active(&self, fork: ForkId, height: u64) -> bool {
match fork {
ForkId::Bip30 => height <= self.bip30_deactivation,
ForkId::Bip16 => height >= self.bip16,
ForkId::Bip34 => height >= self.bip34,
ForkId::Bip66 => height >= self.bip66,
ForkId::Bip65 => height >= self.bip65,
ForkId::Bip112 => height >= self.bip112,
ForkId::Bip147 => height >= self.bip147,
ForkId::SegWit => height >= self.segwit,
ForkId::Taproot => height >= self.taproot,
ForkId::Ctv => self.ctv != u64::MAX && height >= self.ctv,
ForkId::Csfs => self.csfs != u64::MAX && height >= self.csfs,
ForkId::Bip54 => height >= self.bip54,
}
}
}
impl ForkActivationTable {
/// Build table from network and constants. BIP54 uses per-network constant (u64::MAX by default).
pub fn from_network(network: Network) -> Self {
Self::from_network_and_bip54_override(network, None)
}
/// Build table from network and optional BIP54 activation override (e.g. from version bits).
pub fn from_network_and_bip54_override(
network: Network,
bip54_activation_override: Option<u64>,
) -> Self {
let (
bip30_deactivation,
bip16,
bip34,
bip66,
bip65,
bip112,
bip147,
segwit,
taproot,
ctv,
csfs,
) = match network {
Network::Mainnet => (
BIP30_DEACTIVATION_MAINNET,
BIP16_P2SH_ACTIVATION_MAINNET,
BIP34_ACTIVATION_MAINNET,
BIP66_ACTIVATION_MAINNET,
BIP65_ACTIVATION_MAINNET,
BIP112_CSV_ACTIVATION_MAINNET,
BIP147_ACTIVATION_MAINNET,
SEGWIT_ACTIVATION_MAINNET,
TAPROOT_ACTIVATION_MAINNET,
if CTV_ACTIVATION_MAINNET == 0 {
u64::MAX
} else {
CTV_ACTIVATION_MAINNET
},
if CSFS_ACTIVATION_MAINNET == 0 {
u64::MAX
} else {
CSFS_ACTIVATION_MAINNET
},
),
Network::Testnet => (
BIP30_DEACTIVATION_TESTNET,
BIP16_P2SH_ACTIVATION_TESTNET,
BIP34_ACTIVATION_TESTNET,
BIP66_ACTIVATION_TESTNET,
BIP65_ACTIVATION_TESTNET,
BIP112_CSV_ACTIVATION_TESTNET,
BIP147_ACTIVATION_TESTNET,
SEGWIT_ACTIVATION_TESTNET,
TAPROOT_ACTIVATION_TESTNET,
if CTV_ACTIVATION_TESTNET == 0 {
u64::MAX
} else {
CTV_ACTIVATION_TESTNET
},
if CSFS_ACTIVATION_TESTNET == 0 {
u64::MAX
} else {
CSFS_ACTIVATION_TESTNET
},
),
Network::Regtest => (
BIP30_DEACTIVATION_REGTEST,
BIP16_P2SH_ACTIVATION_REGTEST,
BIP34_ACTIVATION_REGTEST,
BIP66_ACTIVATION_REGTEST,
0,
BIP112_CSV_ACTIVATION_REGTEST,
0,
0,
0,
CTV_ACTIVATION_REGTEST,
CSFS_ACTIVATION_REGTEST,
),
Network::Signet => (
BIP30_DEACTIVATION_REGTEST,
BIP16_P2SH_ACTIVATION_REGTEST,
1,
1,
1,
1,
1,
1,
1,
u64::MAX,
u64::MAX,
),
};
let bip54 = bip54_activation_override.unwrap_or(match network {
Network::Mainnet => BIP54_ACTIVATION_MAINNET,
Network::Testnet => BIP54_ACTIVATION_TESTNET,
Network::Regtest => BIP54_ACTIVATION_REGTEST,
Network::Signet => u64::MAX,
});
Self {
bip30_deactivation,
bip16,
bip34,
bip66,
bip65,
bip112,
bip147,
segwit,
taproot,
ctv,
csfs,
bip54,
}
}
}
/// Taproot (BIP341) activation height for `network` (Core `chainparams` mainnet vs testnet3).
#[spec_locked("11.2", "TaprootActivationHeight")]
#[blvm_spec_lock::ensures(result == 0 || result == 1 || result == 709632 || result == 2011968)]
#[inline]
pub fn taproot_activation_height(network: Network) -> u64 {
// if/else (not enum match) so spec-lock Z3 can model control flow with Signet.
if network == Network::Mainnet {
TAPROOT_ACTIVATION_MAINNET
} else if network == Network::Testnet {
TAPROOT_ACTIVATION_TESTNET
} else if network == Network::Signet {
1
} else {
0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::Network;
#[test]
fn fork_table_testnet_matches_primitives() {
let t = ForkActivationTable::from_network(Network::Testnet);
assert_eq!(t.bip65, BIP65_ACTIVATION_TESTNET);
assert_eq!(t.bip112, BIP112_CSV_ACTIVATION_TESTNET);
assert_eq!(t.bip147, BIP147_ACTIVATION_TESTNET);
assert_eq!(t.segwit, SEGWIT_ACTIVATION_TESTNET);
assert_eq!(t.taproot, TAPROOT_ACTIVATION_TESTNET);
}
#[test]
fn taproot_activation_height_matches_table() {
for net in [
Network::Mainnet,
Network::Testnet,
Network::Regtest,
Network::Signet,
] {
let h = taproot_activation_height(net);
let t = ForkActivationTable::from_network(net).taproot;
assert_eq!(h, t, "{net:?}");
}
}
}