Fix serde for block.body.grafitti

This commit is contained in:
Paul Hauner 2019-08-14 11:55:12 +10:00
parent 2bf0d5c071
commit 980f533b3b
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,5 @@
use crate::test_utils::TestRandom;
use crate::utils::graffiti_from_hex_str;
use crate::utils::{graffiti_from_hex_str, graffiti_to_hex_str};
use crate::*;
use serde_derive::{Deserialize, Serialize};
@ -16,7 +16,10 @@ use tree_hash_derive::TreeHash;
pub struct BeaconBlockBody<T: EthSpec> {
pub randao_reveal: Signature,
pub eth1_data: Eth1Data,
#[serde(deserialize_with = "graffiti_from_hex_str")]
#[serde(
serialize_with = "graffiti_to_hex_str",
deserialize_with = "graffiti_from_hex_str"
)]
pub graffiti: [u8; 32],
pub proposer_slashings: VariableList<ProposerSlashing, T::MaxProposerSlashings>,
pub attester_slashings: VariableList<AttesterSlashing<T>, T::MaxAttesterSlashings>,

View File

@ -46,8 +46,20 @@ where
Ok(array)
}
// #[allow(clippy::trivially_copy_pass_by_ref)] // Serde requires the `byte` to be a ref.
pub fn fork_to_hex_str<S>(bytes: &[u8; 4], serializer: S) -> Result<S::Ok, S::Error>
pub fn fork_to_hex_str<S>(bytes: &[u8; FORK_BYTES_LEN], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut hex_string: String = "0x".to_string();
hex_string.push_str(&hex::encode(&bytes));
serializer.serialize_str(&hex_string)
}
pub fn graffiti_to_hex_str<S>(
bytes: &[u8; GRAFFITI_BYTES_LEN],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{