use zakura_chain::{block::Height, parameters::Network, work::difficulty::CompactDifficulty};
pub(super) const MAINNET_WORK_FLOOR_HEIGHT: Height = Height(3_535_411);
const MAINNET_REFERENCE_TARGET: CompactDifficulty =
CompactDifficulty::from_le_bytes(0x1b6aac57_u32.to_le_bytes());
const MAINNET_TARGET_MULTIPLIER: u32 = 10;
pub(super) fn has_sufficient_work(
network: &Network,
committed_tip_height: Option<Height>,
claimed_height: Height,
target: CompactDifficulty,
) -> bool {
if !matches!(network, Network::Mainnet)
|| (claimed_height <= MAINNET_WORK_FLOOR_HEIGHT
&& committed_tip_height.is_none_or(|tip| tip < MAINNET_WORK_FLOOR_HEIGHT))
{
return true;
}
let max_target = MAINNET_REFERENCE_TARGET
.to_expanded()
.expect("the fixed Mainnet reference target has a valid compact encoding")
* MAINNET_TARGET_MULTIPLIER;
target
.to_expanded()
.is_some_and(|target| target <= max_target)
}
#[cfg(test)]
mod tests {
use super::*;
use zakura_chain::work::difficulty::ParameterDifficulty as _;
#[test]
fn mainnet_work_floor_requires_both_historical_heights_for_exemption() {
let network = Network::Mainnet;
let easiest_target = network.target_difficulty_limit().to_compact();
for (tip, expected_exemptions) in [
(None, [true, true, true, false]),
(
Some(MAINNET_WORK_FLOOR_HEIGHT.previous().unwrap()),
[true, true, true, false],
),
(Some(MAINNET_WORK_FLOOR_HEIGHT), [false; 4]),
(Some(MAINNET_WORK_FLOOR_HEIGHT.next().unwrap()), [false; 4]),
] {
for (claimed_height, exempt) in [
Height(0),
MAINNET_WORK_FLOOR_HEIGHT.previous().unwrap(),
MAINNET_WORK_FLOOR_HEIGHT,
MAINNET_WORK_FLOOR_HEIGHT.next().unwrap(),
]
.into_iter()
.zip(expected_exemptions)
{
assert_eq!(
has_sufficient_work(&network, tip, claimed_height, easiest_target),
exempt,
"tip {tip:?}, claimed height {claimed_height:?}",
);
assert!(has_sufficient_work(
&network,
tip,
claimed_height,
MAINNET_REFERENCE_TARGET,
));
}
}
}
#[test]
fn mainnet_work_floor_preserves_historical_targets_in_both_sync_modes() {
let network = Network::Mainnet;
let historical_target = CompactDifficulty::from_le_bytes(0x1c07239f_u32.to_le_bytes());
for checkpoint_sync in [true, false] {
let config = zakura_consensus::Config {
checkpoint_sync,
..Default::default()
};
let (_, checkpoint) = zakura_consensus::router::init_checkpoint_list(config, &network);
for tip in [
Height(1_290_707),
Height(2_898_025),
MAINNET_WORK_FLOOR_HEIGHT.previous().unwrap(),
] {
if !checkpoint_sync {
assert!(tip > checkpoint);
}
assert!(has_sufficient_work(
&network,
Some(tip),
tip.next().unwrap(),
historical_target
));
}
assert!(!has_sufficient_work(
&network,
Some(MAINNET_WORK_FLOOR_HEIGHT),
Height(1_290_708),
historical_target
));
}
}
#[test]
fn mainnet_work_floor_preserves_other_networks() {
for network in [
Network::new_default_testnet(),
Network::new_regtest(Default::default()),
] {
let easiest_target = network.target_difficulty_limit().to_compact();
for tip in [
None,
Some(MAINNET_WORK_FLOOR_HEIGHT),
Some(MAINNET_WORK_FLOOR_HEIGHT.next().unwrap()),
] {
for claimed_height in [Height(0), MAINNET_WORK_FLOOR_HEIGHT.next().unwrap()] {
assert!(has_sufficient_work(
&network,
tip,
claimed_height,
easiest_target
));
}
}
}
}
#[test]
fn mainnet_work_floor_compares_expanded_targets_at_the_boundary() {
let tip = Some(MAINNET_WORK_FLOOR_HEIGHT);
let max_target =
MAINNET_REFERENCE_TARGET.to_expanded().unwrap() * MAINNET_TARGET_MULTIPLIER;
let easiest_allowed_encoding = max_target.to_compact();
let easier_encoding = CompactDifficulty::from_le_bytes(
(u32::from_le_bytes(easiest_allowed_encoding.to_le_bytes()) + 1).to_le_bytes(),
);
assert!(has_sufficient_work(
&Network::Mainnet,
tip,
MAINNET_WORK_FLOOR_HEIGHT,
MAINNET_REFERENCE_TARGET
));
assert!(has_sufficient_work(
&Network::Mainnet,
tip,
MAINNET_WORK_FLOOR_HEIGHT,
easiest_allowed_encoding
));
assert!(!has_sufficient_work(
&Network::Mainnet,
tip,
MAINNET_WORK_FLOOR_HEIGHT,
easier_encoding
));
assert!(!has_sufficient_work(
&Network::Mainnet,
tip,
MAINNET_WORK_FLOOR_HEIGHT,
CompactDifficulty::from_le_bytes([0; 4]),
));
}
}