1515
1616use super :: * ;
1717
18- use snarkos_node_network:: built_info ;
18+ use snarkos_node_network:: get_repo_commit_hash ;
1919
2020#[ derive( Clone , Debug , PartialEq , Eq ) ]
2121pub struct ChallengeRequest < N : Network > {
2222 pub version : u32 ,
2323 pub listener_port : u16 ,
2424 pub address : Address < N > ,
2525 pub nonce : u64 ,
26- pub snarkos_sha : String ,
26+ pub snarkos_sha : Option < [ u8 ; 40 ] > ,
2727}
2828
2929impl < N : Network > ChallengeRequest < N > {
30- /// Creates a new `ChallengeRequest` event.
30+ /// Constant for an unknown commit hash.
31+ const UNKNOWN_COMMIT_HASH : [ u8 ; 40 ] = [ b'?' ; 40 ] ;
32+
3133 pub fn new ( listener_port : u16 , address : Address < N > , nonce : u64 ) -> Self {
32- Self {
33- version : Event :: < N > :: VERSION ,
34- listener_port,
35- address,
36- nonce,
37- snarkos_sha : built_info:: GIT_COMMIT_HASH . unwrap_or_default ( ) . into ( ) ,
38- }
34+ Self { version : Event :: < N > :: VERSION , listener_port, address, nonce, snarkos_sha : get_repo_commit_hash ( ) }
3935 }
4036}
4137
@@ -48,35 +44,38 @@ impl<N: Network> EventTrait for ChallengeRequest<N> {
4844}
4945
5046impl < N : Network > ToBytes for ChallengeRequest < N > {
51- fn write_le < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
47+ fn write_le < W : Write > ( & self , mut writer : W ) -> io :: Result < ( ) > {
5248 self . version . write_le ( & mut writer) ?;
5349 self . listener_port . write_le ( & mut writer) ?;
5450 self . address . write_le ( & mut writer) ?;
5551 self . nonce . write_le ( & mut writer) ?;
56- self . snarkos_sha . as_bytes ( ) . write_le ( & mut writer) ?;
52+ // Serialize `None` as a constant.
53+ self . snarkos_sha . unwrap_or ( Self :: UNKNOWN_COMMIT_HASH ) . write_le ( & mut writer) ?;
5754
5855 Ok ( ( ) )
5956 }
6057}
6158
6259impl < N : Network > FromBytes for ChallengeRequest < N > {
63- fn read_le < R : Read > ( mut reader : R ) -> IoResult < Self > {
60+ fn read_le < R : Read > ( mut reader : R ) -> io :: Result < Self > {
6461 let version = u32:: read_le ( & mut reader) ?;
6562 let listener_port = u16:: read_le ( & mut reader) ?;
6663 let address = Address :: < N > :: read_le ( & mut reader) ?;
6764 let nonce = u64:: read_le ( & mut reader) ?;
68- let snarkos_sha = str:: from_utf8 ( & <[ u8 ; 40 ] >:: read_le ( & mut reader) . unwrap_or ( [ b'?' ; 40 ] ) [ ..] )
69- . map ( |str| if str. starts_with ( '?' ) { "unknown" } else { str } )
70- . map_err ( |_| error ( "Invalid snarkOS SHA" ) ) ?
71- . to_owned ( ) ;
65+ let snarkos_sha = {
66+ let bytes =
67+ <[ u8 ; 40 ] >:: read_le ( & mut reader) . map_err ( |err| io_error ( format ! ( "Invalid snarkOS SHA - {err}" ) ) ) ?;
68+ if bytes == Self :: UNKNOWN_COMMIT_HASH { None } else { Some ( bytes) }
69+ } ;
7270
7371 Ok ( Self { version, listener_port, address, nonce, snarkos_sha } )
7472 }
7573}
7674
7775#[ cfg( test) ]
7876pub mod prop_tests {
79- use crate :: ChallengeRequest ;
77+ use super :: * ;
78+
8079 use snarkvm:: {
8180 console:: prelude:: { FromBytes , ToBytes } ,
8281 prelude:: { Address , TestRng , Uniform } ,
@@ -97,12 +96,11 @@ pub mod prop_tests {
9796
9897 pub fn any_challenge_request ( ) -> BoxedStrategy < ChallengeRequest < CurrentNetwork > > {
9998 ( any_valid_address ( ) , any :: < u64 > ( ) , any :: < u32 > ( ) , any :: < u16 > ( ) , collection:: vec ( 0u8 ..=127 , 40 ) )
100- . prop_map ( |( address, nonce, version, listener_port, sha) | ChallengeRequest {
101- address,
102- nonce,
103- version,
104- listener_port,
105- snarkos_sha : sha. into_iter ( ) . map ( |b| b as char ) . collect ( ) ,
99+ . prop_map ( |( address, nonce, version, listener_port, sha) | {
100+ let sha: [ u8 ; 40 ] = sha. try_into ( ) . unwrap ( ) ;
101+ let snarkos_sha =
102+ if sha == ChallengeRequest :: < CurrentNetwork > :: UNKNOWN_COMMIT_HASH { None } else { Some ( sha) } ;
103+ ChallengeRequest { address, nonce, version, listener_port, snarkos_sha }
106104 } )
107105 . boxed ( )
108106 }
@@ -112,12 +110,8 @@ pub mod prop_tests {
112110 let mut buf = BytesMut :: default ( ) . writer ( ) ;
113111 ChallengeRequest :: write_le ( & original, & mut buf) . unwrap ( ) ;
114112
115- let mut deserialized: ChallengeRequest < CurrentNetwork > =
113+ let deserialized: ChallengeRequest < CurrentNetwork > =
116114 ChallengeRequest :: read_le ( buf. into_inner ( ) . reader ( ) ) . unwrap ( ) ;
117- // Upon deserialization, unsupplied SHA is registered as "unknown".
118- if deserialized. snarkos_sha == "unknown" {
119- deserialized. snarkos_sha = original. snarkos_sha . clone ( ) ;
120- }
121115 assert_eq ! ( original, deserialized) ;
122116 }
123117}
0 commit comments