-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize and Deserialize implementations of block::header::Header
do not match
#187
Comments
Good catch! And a sign we should better test even just for serde serialization and deserialization. I agree that solution 1 is the way to go. |
There is actually a third solution, that kinda combines the two others: Solution 3
#[serde(from = "Hex")]
#[serde(into = "Hex")]
To me it feels a nice tradeoff between the two approaches above, with a bit more safety as one just has to remember to put both The main downside I see is that it introduces more code. On the other hand this struct could be re-used in other ways outside of serde serialization. We could give @liamsi What do you think? |
Introducing another struct to (de)serialize a field (app_hash) in a struct (Header) whose (current) main purpose itself is serialization feels a bit weird. But I agree, it looks like a clean solution too. As longs as the Hex struct isn't publicly accessible, I think we are good with your last proposed option as well 👍 |
Alright, let's go with the leanest solution for now then :) We can always revisit the 3rd solution as some later stage if we see the need for the |
Problem
block::header::Header
defines anapp_hash
field as follows:This means that
app_hash
is serialized as a byte array, butserializers::parse_hex
expects a string holding hex-encoded data, which results in the following error when attempting to do a serialize-then-deserialize round trip:Proposed solutions
Solution 1
If I understand correctly,
app_hash
is not fixed-length hash (eg. SHA256) but variable-length hex-encoded data. I thus suggest keep theVec<u8>
type but add aserde
annotation to serialize the data withserialize_hex
:Pros
Cons
Solution 2
Alternatively, we could define a
Hex
struct with customSerialize
andDeserialize
instances:Pros
Cons
Recommendation
I am partial to solution 1 as it preserves the semantic meaning of the type, but would be happy to implement solution 2 as well if anyone has a good argument for it.
The text was updated successfully, but these errors were encountered: