Skip to content

Commit b0298a2

Browse files
committed
Adds current_with_activation_height() method and uses it in Commitment::from_bytes()
1 parent 99cb52b commit b0298a2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

zebra-chain/src/block/commitment.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,34 @@ impl Commitment {
109109
use Commitment::*;
110110
use CommitmentError::*;
111111

112-
match NetworkUpgrade::current(network, height) {
113-
Genesis | BeforeOverwinter | Overwinter => Ok(PreSaplingReserved(bytes)),
114-
Sapling | Blossom => match sapling::tree::Root::try_from(bytes) {
112+
match NetworkUpgrade::current_with_activation_height(network, height) {
113+
(Genesis | BeforeOverwinter | Overwinter, _) => Ok(PreSaplingReserved(bytes)),
114+
(Sapling | Blossom, _) => match sapling::tree::Root::try_from(bytes) {
115115
Ok(root) => Ok(FinalSaplingRoot(root)),
116116
_ => Err(InvalidSapingRootBytes),
117117
},
118+
(Heartwood, activation_height) if height == activation_height => {
119+
if bytes == CHAIN_HISTORY_ACTIVATION_RESERVED {
120+
Ok(ChainHistoryActivationReserved)
121+
} else {
122+
Err(InvalidChainHistoryActivationReserved { actual: bytes })
123+
}
124+
}
118125
// NetworkUpgrade::current() returns the latest network upgrade that's activated at the provided height, so
119126
// on Regtest for heights above height 0, it returns NU5, and it's possible for the current network upgrade
120127
// to be NU5 (or Canopy, or any network upgrade above Heartwood) at the Heartwood activation height.
121-
Heartwood | Canopy | Nu5 if Some(height) == Heartwood.activation_height(network) => {
128+
(Canopy | Nu5, activation_height)
129+
if height == activation_height
130+
&& Some(height) == Heartwood.activation_height(network) =>
131+
{
122132
if bytes == CHAIN_HISTORY_ACTIVATION_RESERVED {
123133
Ok(ChainHistoryActivationReserved)
124134
} else {
125135
Err(InvalidChainHistoryActivationReserved { actual: bytes })
126136
}
127137
}
128-
Heartwood | Canopy => Ok(ChainHistoryRoot(ChainHistoryMmrRootHash(bytes))),
129-
Nu5 => Ok(ChainHistoryBlockTxAuthCommitment(
138+
(Heartwood | Canopy, _) => Ok(ChainHistoryRoot(ChainHistoryMmrRootHash(bytes))),
139+
(Nu5, _) => Ok(ChainHistoryBlockTxAuthCommitment(
130140
ChainHistoryBlockTxAuthCommitmentHash(bytes),
131141
)),
132142
}

zebra-chain/src/parameters/network_upgrade.rs

+13
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ impl Network {
280280
}
281281

282282
impl NetworkUpgrade {
283+
/// Returns the current network upgrade and its activation height for `network` and `height`.
284+
pub fn current_with_activation_height(
285+
network: &Network,
286+
height: block::Height,
287+
) -> (NetworkUpgrade, block::Height) {
288+
network
289+
.activation_list()
290+
.range(..=height)
291+
.map(|(&h, &nu)| (nu, h))
292+
.next_back()
293+
.expect("every height has a current network upgrade")
294+
}
295+
283296
/// Returns the current network upgrade for `network` and `height`.
284297
pub fn current(network: &Network, height: block::Height) -> NetworkUpgrade {
285298
network

0 commit comments

Comments
 (0)