@@ -18,13 +18,12 @@ use super::*;
1818impl < N : Network > FromBytes for Deployment < N > {
1919 /// Reads the deployment from a buffer.
2020 fn read_le < R : Read > ( mut reader : R ) -> IoResult < Self > {
21- // Read the version.
22- let version = u8:: read_le ( & mut reader) ?;
23- // Ensure the version is valid.
24- match version {
25- 1 | 2 => { } // Do nothing.
26- 0 | 3 ..=u8:: MAX => return Err ( error ( "Invalid deployment version" ) ) ,
27- }
21+ // Read the version and ensure the version is valid.
22+ let version = match u8:: read_le ( & mut reader) ? {
23+ 1 => DeploymentVersion :: V1 ,
24+ 2 => DeploymentVersion :: V2 ,
25+ version => return Err ( error ( format ! ( "Invalid deployment version: {}" , version) ) ) ,
26+ } ;
2827
2928 // Read the edition.
3029 let edition = u16:: read_le ( & mut reader) ?;
@@ -46,10 +45,10 @@ impl<N: Network> FromBytes for Deployment<N> {
4645 verifying_keys. push ( ( identifier, ( verifying_key, certificate) ) ) ;
4746 }
4847
49- // If the version is 2, read the program checksum.
48+ // If the deployment version is 2, read the program checksum.
5049 let program_checksum = match version {
51- 0 | 1 | 3 ..=u8 :: MAX => None ,
52- 2 => Some ( Field :: < N > :: read_le ( & mut reader) ?) ,
50+ DeploymentVersion :: V1 => None ,
51+ DeploymentVersion :: V2 => Some ( Field :: < N > :: read_le ( & mut reader) ?) ,
5352 } ;
5453
5554 // Return the deployment.
@@ -61,10 +60,7 @@ impl<N: Network> ToBytes for Deployment<N> {
6160 /// Writes the deployment to a buffer.
6261 fn write_le < W : Write > ( & self , mut writer : W ) -> IoResult < ( ) > {
6362 // Write the version.
64- match self . program_checksum . is_some ( ) {
65- false => 1u8 . write_le ( & mut writer) ?,
66- true => 2u8 . write_le ( & mut writer) ?,
67- } ;
63+ ( self . version ( ) as u8 ) . write_le ( & mut writer) ?;
6864 // Write the edition.
6965 self . edition . write_le ( & mut writer) ?;
7066 // Write the program.
@@ -96,12 +92,20 @@ mod tests {
9692 fn test_bytes ( ) -> Result < ( ) > {
9793 let rng = & mut TestRng :: default ( ) ;
9894
99- // Construct a new deployment.
100- let expected = test_helpers:: sample_deployment ( rng) ;
95+ // Construct the deployments.
96+ for expected in [ test_helpers:: sample_deployment ( rng) , test_helpers:: sample_deployment_with_checksum ( rng) ] {
97+ // Check the byte representation.
98+ let expected_bytes = expected. to_bytes_le ( ) ?;
99+ assert_eq ! ( expected, Deployment :: read_le( & expected_bytes[ ..] ) ?) ;
100+
101+ // Construct a new deployment with a checksum.
102+ let expected = test_helpers:: sample_deployment_with_checksum ( rng) ;
103+
104+ // Check the byte representation.
105+ let expected_bytes = expected. to_bytes_le ( ) ?;
106+ assert_eq ! ( expected, Deployment :: read_le( & expected_bytes[ ..] ) ?) ;
107+ }
101108
102- // Check the byte representation.
103- let expected_bytes = expected. to_bytes_le ( ) ?;
104- assert_eq ! ( expected, Deployment :: read_le( & expected_bytes[ ..] ) ?) ;
105109 Ok ( ( ) )
106110 }
107111}
0 commit comments