Skip to content

Commit 426558c

Browse files
committed
ergo-chain-types: Add Header.unparsedBytes field
1 parent 2185bf7 commit 426558c

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

ergo-chain-generation/src/chain_generation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ fn prove_block(
191191
extension_root,
192192
autolykos_solution: dummy_autolykos_solution,
193193
votes,
194+
unparsed_bytes: Box::new([]),
194195
};
195196
let msg = blake2b256_hash(&header.serialize_without_pow().unwrap())
196197
.0

ergo-chain-generation/src/fake_pow_scheme.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ mod tests {
141141
extension_root,
142142
autolykos_solution: dummy_autolykos_solution,
143143
votes,
144+
unparsed_bytes: Box::new([]),
144145
};
145146

146147
let x = DlogProverInput::random();

ergo-chain-types/src/header.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ pub struct Header {
5656
/// 3 bytes in accordance to Scala implementation, but will use `Vec` until further improvements
5757
#[cfg_attr(feature = "json", serde(rename = "votes"))]
5858
pub votes: Votes,
59+
/// Unparsed bytes that encode new possible fields
60+
#[cfg_attr(
61+
feature = "json",
62+
serde(
63+
rename = "unparsedBytes",
64+
default,
65+
serialize_with = "crate::json::autolykos_solution::as_base16_string",
66+
deserialize_with = "crate::json::autolykos_solution::from_base16_string"
67+
)
68+
)]
69+
pub unparsed_bytes: Box<[u8]>,
5970
}
6071

6172
impl Header {
@@ -82,7 +93,8 @@ impl Header {
8293
// For block version >= 2, this new byte encodes length of possible new fields.
8394
// Set to 0 for now, so no new fields.
8495
if self.version > 1 {
85-
w.put_i8(0)?;
96+
w.put_u8(self.unparsed_bytes.len().try_into()?)?;
97+
w.write_all(&self.unparsed_bytes)?;
8698
}
8799
Ok(data)
88100
}
@@ -127,13 +139,18 @@ impl ScorexSerializable for Header {
127139

128140
// For block version >= 2, a new byte encodes length of possible new fields. If this byte >
129141
// 0, we read new fields but do nothing, as semantics of the fields is not known.
130-
if version > 1 {
142+
let unparsed_bytes: Box<[u8]> = if version > 1 {
131143
let new_field_size = r.get_u8()?;
132144
if new_field_size > 0 {
133145
let mut field_bytes: Vec<u8> = vec![0; new_field_size as usize];
134146
r.read_exact(&mut field_bytes)?;
147+
field_bytes.into()
148+
} else {
149+
Box::new([])
135150
}
136-
}
151+
} else {
152+
Box::new([])
153+
};
137154

138155
// Parse `AutolykosSolution`
139156
let autolykos_solution = if version == 1 {
@@ -182,6 +199,7 @@ impl ScorexSerializable for Header {
182199
extension_root,
183200
autolykos_solution: autolykos_solution.clone(),
184201
votes,
202+
unparsed_bytes,
185203
};
186204

187205
let mut id_bytes = header.serialize_without_pow()?;
@@ -296,6 +314,7 @@ mod arbitrary {
296314
prop::sample::select(vec![1_u8, 2]),
297315
any::<Box<AutolykosSolution>>(),
298316
uniform3(1u8..),
317+
proptest::collection::vec(any::<u8>(), 0..=255),
299318
)
300319
.prop_map(
301320
|(
@@ -309,6 +328,7 @@ mod arbitrary {
309328
version,
310329
autolykos_solution,
311330
votes,
331+
unparsed_bytes,
312332
)| {
313333
let parent_id = BlockId(Digest(parent_id));
314334
let ad_proofs_root = Digest(ad_proofs_root);
@@ -332,6 +352,11 @@ mod arbitrary {
332352
extension_root,
333353
autolykos_solution: *autolykos_solution.clone(),
334354
votes,
355+
unparsed_bytes: if version > 1 {
356+
unparsed_bytes.into()
357+
} else {
358+
Box::new([])
359+
},
335360
};
336361
let mut id_bytes = header.serialize_without_pow().unwrap();
337362
let mut data = Vec::new();

ergo-chain-types/src/json/autolykos_solution.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ where
1616
serializer.serialize_str(&base16::encode_lower(value))
1717
}
1818

19-
pub(crate) fn from_base16_string<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
19+
pub(crate) fn from_base16_string<'de, D, T: From<Vec<u8>>>(deserializer: D) -> Result<T, D::Error>
2020
where
2121
D: Deserializer<'de>,
2222
{
2323
use serde::de::Error;
2424
String::deserialize(deserializer)
2525
.and_then(|string| base16::decode(&string).map_err(|err| Error::custom(err.to_string())))
26+
.map(From::from)
2627
}
2728

2829
/// Serialize `BigInt` as a string

0 commit comments

Comments
 (0)