11mod consensus_state;
22
3+ use std:: array;
4+
35use ark_ec:: short_weierstrass_jacobian:: GroupAffine ;
46use base64:: prelude:: * ;
57use consensus_state:: { select_longer_chain, LongerChainResult } ;
@@ -24,9 +26,7 @@ lazy_static! {
2426// TODO(xqft): check proof size
2527const MAX_PROOF_SIZE : usize = 16 * 1024 ;
2628const MAX_PUB_INPUT_SIZE : usize = 6 * 1024 ;
27- const PROTOCOL_STATE_HASH_SIZE : usize = 32 ;
28- // TODO(gabrielbosio): check that this length is always the same for every block
29- const PROTOCOL_STATE_SIZE : usize = 2056 ;
29+ const STATE_HASH_SIZE : usize = 32 ;
3030
3131#[ no_mangle]
3232pub extern "C" fn verify_protocol_state_proof_ffi (
@@ -110,14 +110,11 @@ pub fn parse_protocol_state_pub(
110110 ) ,
111111 String ,
112112> {
113- let ( tip_protocol_state_hash, tip_protocol_state) = parse_protocol_state_with_hash (
114- & protocol_state_pub[ ..( PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE ) ] ,
115- ) ?;
113+ let ( tip_protocol_state_hash, tip_protocol_state, candidate_start) =
114+ parse_protocol_state_with_hash ( & protocol_state_pub, 0 ) ?;
116115
117- let ( candidate_protocol_state_hash, candidate_protocol_state) = parse_protocol_state_with_hash (
118- & protocol_state_pub[ ( PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE )
119- ..( ( PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE ) * 2 ) ] ,
120- ) ?;
116+ let ( candidate_protocol_state_hash, candidate_protocol_state, _) =
117+ parse_protocol_state_with_hash ( & protocol_state_pub, candidate_start) ?;
121118
122119 Ok ( (
123120 tip_protocol_state_hash,
@@ -129,25 +126,52 @@ pub fn parse_protocol_state_pub(
129126
130127fn parse_protocol_state_with_hash (
131128 protocol_state_pub : & [ u8 ] ,
129+ start : usize ,
132130) -> Result <
133131 (
134132 ark_ff:: Fp256 < mina_curves:: pasta:: fields:: FpParameters > ,
135133 MinaStateProtocolStateValueStableV2 ,
134+ usize ,
136135 ) ,
137136 String ,
138137> {
138+ let protocol_state_hash_bytes: Vec < _ > = protocol_state_pub
139+ . iter ( )
140+ . skip ( start)
141+ . take ( STATE_HASH_SIZE )
142+ . map ( |byte| byte. clone ( ) )
143+ . collect ( ) ;
139144 let protocol_state_hash =
140- Fp :: from_bytes ( & protocol_state_pub[ ..32 ] ) . map_err ( |err| err. to_string ( ) ) ?;
145+ Fp :: from_bytes ( & protocol_state_hash_bytes) . map_err ( |err| err. to_string ( ) ) ?;
146+
147+ let protocol_state_len_vec: Vec < _ > = protocol_state_pub
148+ . iter ( )
149+ . skip ( start + STATE_HASH_SIZE )
150+ . take ( 8 )
151+ . collect ( ) ;
152+ let protocol_state_len_bytes: [ u8 ; 4 ] = array:: from_fn ( |i| protocol_state_len_vec[ i] . clone ( ) ) ;
153+ let protocol_state_len = u32:: from_be_bytes ( protocol_state_len_bytes) as usize ;
154+
155+ let protocol_state_bytes: Vec < _ > = protocol_state_pub
156+ . iter ( )
157+ . skip ( start + STATE_HASH_SIZE + 4 )
158+ . take ( protocol_state_len)
159+ . map ( |byte| byte. clone ( ) )
160+ . collect ( ) ;
141161 let protocol_state_base64 =
142- std:: str:: from_utf8 ( & protocol_state_pub [ 32 .. ] ) . map_err ( |err| err. to_string ( ) ) ?;
162+ std:: str:: from_utf8 ( & protocol_state_bytes ) . map_err ( |err| err. to_string ( ) ) ?;
143163 let protocol_state_binprot = BASE64_STANDARD
144164 . decode ( protocol_state_base64)
145165 . map_err ( |err| err. to_string ( ) ) ?;
146166 let protocol_state =
147167 MinaStateProtocolStateValueStableV2 :: binprot_read ( & mut protocol_state_binprot. as_slice ( ) )
148168 . map_err ( |err| err. to_string ( ) ) ?;
149169
150- Ok ( ( protocol_state_hash, protocol_state) )
170+ Ok ( (
171+ protocol_state_hash,
172+ protocol_state,
173+ start + STATE_HASH_SIZE + 4 + protocol_state_len,
174+ ) )
151175}
152176
153177#[ cfg( test) ]
0 commit comments