Skip to content

Commit a0f254b

Browse files
committed
Cleanup up Deployment
1 parent 7125156 commit a0f254b

File tree

3 files changed

+78
-42
lines changed

3 files changed

+78
-42
lines changed

ledger/block/src/transaction/deployment/bytes.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ use super::*;
1818
impl<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
}

ledger/block/src/transaction/deployment/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub struct Deployment<N: Network> {
3737
/// The mapping of function names to their verifying key and certificate.
3838
verifying_keys: Vec<(Identifier<N>, (VerifyingKey<N>, Certificate<N>))>,
3939
/// An optional checksum for the program.
40-
/// This purpose of this field is to create an implicit versioning mechanism for deployments.
41-
/// Existing deployments do not have a checksum, while new deployments will have a checksum.
40+
/// This purpose of this field is to create an implicit, backwards-compatible versioning mechanism for deployments.
4241
/// Before a given migration height, the checksum will **not** be allowed.
4342
/// After the migration height, the checksum will be required.
4443
program_checksum: Option<Field<N>>,
@@ -178,6 +177,25 @@ impl<N: Network> Deployment<N> {
178177
pub fn to_deployment_id(&self) -> Result<Field<N>> {
179178
Ok(*Transaction::deployment_tree(self)?.root())
180179
}
180+
181+
/// Sets the program checksum.
182+
pub fn set_program_checksum(&mut self, program_checksum: Option<Field<N>>) {
183+
self.program_checksum = program_checksum;
184+
}
185+
186+
// An internal function to return the implicit deployment version.
187+
fn version(&self) -> DeploymentVersion {
188+
match self.program_checksum {
189+
None => DeploymentVersion::V1,
190+
Some(_) => DeploymentVersion::V2,
191+
}
192+
}
193+
}
194+
195+
// An internal enum to represent the deployment version.
196+
enum DeploymentVersion {
197+
V1 = 1,
198+
V2 = 2,
181199
}
182200

183201
#[cfg(test)]
@@ -222,4 +240,15 @@ function compute:
222240
})
223241
.clone()
224242
}
243+
244+
pub(crate) fn sample_deployment_with_checksum(rng: &mut TestRng) -> Deployment<CurrentNetwork> {
245+
// Get the deployment.
246+
let mut deployment = sample_deployment(rng);
247+
// Add a checksum.
248+
let program_checksum = deployment.program.checksum().unwrap();
249+
// Set the checksum.
250+
deployment.set_program_checksum(Some(program_checksum));
251+
// Return the deployment.
252+
deployment
253+
}
225254
}

ledger/block/src/transaction/deployment/serialize.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ impl<N: Network> Serialize for Deployment<N> {
2020
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2121
match serializer.is_human_readable() {
2222
true => {
23-
let len = if self.program_checksum.is_some() { 4 } else { 3 };
23+
let len = match self.version() {
24+
DeploymentVersion::V1 => 3,
25+
DeploymentVersion::V2 => 4,
26+
};
2427
let mut deployment = serializer.serialize_struct("Deployment", len)?;
2528
deployment.serialize_field("edition", &self.edition)?;
2629
deployment.serialize_field("program", &self.program)?;
@@ -71,17 +74,17 @@ mod tests {
7174
fn test_serde_json() -> Result<()> {
7275
let rng = &mut TestRng::default();
7376

74-
// Sample the deployment.
75-
let expected = test_helpers::sample_deployment(rng);
77+
// Sample the deployments.
78+
for expected in [test_helpers::sample_deployment(rng), test_helpers::sample_deployment_with_checksum(rng)] {
79+
// Serialize
80+
let expected_string = &expected.to_string();
81+
let candidate_string = serde_json::to_string(&expected)?;
82+
assert_eq!(expected, serde_json::from_str(&candidate_string)?);
7683

77-
// Serialize
78-
let expected_string = &expected.to_string();
79-
let candidate_string = serde_json::to_string(&expected)?;
80-
assert_eq!(expected, serde_json::from_str(&candidate_string)?);
81-
82-
// Deserialize
83-
assert_eq!(expected, Deployment::from_str(expected_string)?);
84-
assert_eq!(expected, serde_json::from_str(&candidate_string)?);
84+
// Deserialize
85+
assert_eq!(expected, Deployment::from_str(expected_string)?);
86+
assert_eq!(expected, serde_json::from_str(&candidate_string)?);
87+
}
8588

8689
Ok(())
8790
}
@@ -90,17 +93,17 @@ mod tests {
9093
fn test_bincode() -> Result<()> {
9194
let rng = &mut TestRng::default();
9295

93-
// Sample the deployment.
94-
let expected = test_helpers::sample_deployment(rng);
96+
// Sample the deployments
97+
for expected in [test_helpers::sample_deployment(rng), test_helpers::sample_deployment_with_checksum(rng)] {
98+
// Serialize
99+
let expected_bytes = expected.to_bytes_le()?;
100+
let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
101+
assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
95102

96-
// Serialize
97-
let expected_bytes = expected.to_bytes_le()?;
98-
let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
99-
assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
100-
101-
// Deserialize
102-
assert_eq!(expected, Deployment::read_le(&expected_bytes[..])?);
103-
assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
103+
// Deserialize
104+
assert_eq!(expected, Deployment::read_le(&expected_bytes[..])?);
105+
assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
106+
}
104107

105108
Ok(())
106109
}

0 commit comments

Comments
 (0)