1
1
use serde:: { Deserialize , Serialize } ;
2
2
3
- use crate :: constants:: PROTOCOL_VERSION ;
4
-
5
3
use super :: ArcBlockWithHash ;
4
+ use crate :: constants:: PROTOCOL_VERSION ;
6
5
7
6
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
8
7
pub enum BlockPrevalidationError {
@@ -23,64 +22,89 @@ pub enum BlockPrevalidationError {
23
22
InvalidDeltaBlockChainProof ,
24
23
}
25
24
26
- pub fn prevalidate_block (
25
+ pub fn validate_block_timing (
27
26
block : & ArcBlockWithHash ,
28
27
genesis : & ArcBlockWithHash ,
29
28
cur_global_slot : u32 ,
30
29
allow_block_too_late : bool ,
31
30
) -> Result < ( ) , BlockPrevalidationError > {
32
31
let block_global_slot = block. global_slot ( ) ;
33
-
34
32
let delta = genesis. constants ( ) . delta . as_u32 ( ) ;
33
+
35
34
if cur_global_slot < block_global_slot {
36
- // Too_early
37
35
return Err ( BlockPrevalidationError :: ReceivedTooEarly {
38
36
current_global_slot : cur_global_slot,
39
37
block_global_slot,
40
38
} ) ;
41
39
} else if !allow_block_too_late && cur_global_slot. saturating_sub ( block_global_slot) > delta {
42
- // Too_late
43
40
return Err ( BlockPrevalidationError :: ReceivedTooLate {
44
41
current_global_slot : cur_global_slot,
45
42
block_global_slot,
46
43
delta,
47
44
} ) ;
48
45
}
49
46
47
+ Ok ( ( ) )
48
+ }
49
+
50
+ pub fn validate_genesis_state (
51
+ block : & ArcBlockWithHash ,
52
+ genesis : & ArcBlockWithHash ,
53
+ ) -> Result < ( ) , BlockPrevalidationError > {
50
54
if block. header ( ) . genesis_state_hash ( ) != genesis. hash ( ) {
51
55
return Err ( BlockPrevalidationError :: InvalidGenesisProtocolState ) ;
52
56
}
57
+ Ok ( ( ) )
58
+ }
53
59
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) ;
72
68
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 {
74
74
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 {
76
80
return Err ( BlockPrevalidationError :: MismatchedProtocolVersion ) ;
77
81
}
78
82
83
+ Ok ( ( ) )
84
+ }
85
+
86
+ pub fn validate_constants (
87
+ block : & ArcBlockWithHash ,
88
+ genesis : & ArcBlockWithHash ,
89
+ ) -> Result < ( ) , BlockPrevalidationError > {
79
90
// NOTE: currently these cannot change between blocks, but that
80
91
// may not always be true?
81
92
if block. constants ( ) != genesis. constants ( ) {
82
93
return Err ( BlockPrevalidationError :: ConsantsMismatch ) ;
83
94
}
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) ?;
84
108
85
109
// TODO(tizoc): check for InvalidDeltaBlockChainProof
86
110
// https://github.com/MinaProtocol/mina/blob/d800da86a764d8d37ffb8964dd8d54d9f522b358/src/lib/mina_block/validation.ml#L369
0 commit comments