Skip to content

Commit 1bc1a4f

Browse files
committed
refactor: Split block prevalidations into individual functions
1 parent 21fb876 commit 1bc1a4f

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

core/src/block/prevalidate.rs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::constants::PROTOCOL_VERSION;
4-
53
use super::ArcBlockWithHash;
4+
use crate::constants::PROTOCOL_VERSION;
65

76
#[derive(Serialize, Deserialize, Debug, Clone)]
87
pub enum BlockPrevalidationError {
@@ -23,64 +22,89 @@ pub enum BlockPrevalidationError {
2322
InvalidDeltaBlockChainProof,
2423
}
2524

26-
pub fn prevalidate_block(
25+
pub fn validate_block_timing(
2726
block: &ArcBlockWithHash,
2827
genesis: &ArcBlockWithHash,
2928
cur_global_slot: u32,
3029
allow_block_too_late: bool,
3130
) -> Result<(), BlockPrevalidationError> {
3231
let block_global_slot = block.global_slot();
33-
3432
let delta = genesis.constants().delta.as_u32();
33+
3534
if cur_global_slot < block_global_slot {
36-
// Too_early
3735
return Err(BlockPrevalidationError::ReceivedTooEarly {
3836
current_global_slot: cur_global_slot,
3937
block_global_slot,
4038
});
4139
} else if !allow_block_too_late && cur_global_slot.saturating_sub(block_global_slot) > delta {
42-
// Too_late
4340
return Err(BlockPrevalidationError::ReceivedTooLate {
4441
current_global_slot: cur_global_slot,
4542
block_global_slot,
4643
delta,
4744
});
4845
}
4946

47+
Ok(())
48+
}
49+
50+
pub fn validate_genesis_state(
51+
block: &ArcBlockWithHash,
52+
genesis: &ArcBlockWithHash,
53+
) -> Result<(), BlockPrevalidationError> {
5054
if block.header().genesis_state_hash() != genesis.hash() {
5155
return Err(BlockPrevalidationError::InvalidGenesisProtocolState);
5256
}
57+
Ok(())
58+
}
5359

54-
let (protocol_versions_are_valid, protocol_version_matches_daemon) = {
55-
let min_transaction_version = 1.into();
56-
let v = &block.header().current_protocol_version;
57-
let nv = block
58-
.header()
59-
.proposed_protocol_version_opt
60-
.as_ref()
61-
.unwrap_or(v);
62-
63-
// Our version values are unsigned, so there is no need to check that the
64-
// other parts are not negative.
65-
let valid =
66-
v.transaction >= min_transaction_version && nv.transaction >= min_transaction_version;
67-
let compatible =
68-
v.transaction == PROTOCOL_VERSION.transaction && v.network == PROTOCOL_VERSION.network;
69-
70-
(valid, compatible)
71-
};
60+
pub fn validate_protocol_versions(block: &ArcBlockWithHash) -> Result<(), BlockPrevalidationError> {
61+
let min_transaction_version = 1.into();
62+
let v = &block.header().current_protocol_version;
63+
let nv = block
64+
.header()
65+
.proposed_protocol_version_opt
66+
.as_ref()
67+
.unwrap_or(v);
7268

73-
if !protocol_versions_are_valid {
69+
// Our version values are unsigned, so there is no need to check that the
70+
// other parts are not negative.
71+
let valid =
72+
v.transaction >= min_transaction_version && nv.transaction >= min_transaction_version;
73+
if !valid {
7474
return Err(BlockPrevalidationError::InvalidProtocolVersion);
75-
} else if !protocol_version_matches_daemon {
75+
}
76+
77+
let compatible =
78+
v.transaction == PROTOCOL_VERSION.transaction && v.network == PROTOCOL_VERSION.network;
79+
if !compatible {
7680
return Err(BlockPrevalidationError::MismatchedProtocolVersion);
7781
}
7882

83+
Ok(())
84+
}
85+
86+
pub fn validate_constants(
87+
block: &ArcBlockWithHash,
88+
genesis: &ArcBlockWithHash,
89+
) -> Result<(), BlockPrevalidationError> {
7990
// NOTE: currently these cannot change between blocks, but that
8091
// may not always be true?
8192
if block.constants() != genesis.constants() {
8293
return Err(BlockPrevalidationError::ConsantsMismatch);
8394
}
95+
Ok(())
96+
}
97+
98+
pub fn prevalidate_block(
99+
block: &ArcBlockWithHash,
100+
genesis: &ArcBlockWithHash,
101+
cur_global_slot: u32,
102+
allow_block_too_late: bool,
103+
) -> Result<(), BlockPrevalidationError> {
104+
validate_block_timing(block, genesis, cur_global_slot, allow_block_too_late)?;
105+
validate_genesis_state(block, genesis)?;
106+
validate_protocol_versions(block)?;
107+
validate_constants(block, genesis)?;
84108

85109
// TODO(tizoc): check for InvalidDeltaBlockChainProof
86110
// https://github.com/MinaProtocol/mina/blob/d800da86a764d8d37ffb8964dd8d54d9f522b358/src/lib/mina_block/validation.ml#L369

0 commit comments

Comments
 (0)