Skip to content

Commit

Permalink
node-data: add serde derive for block header
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Aug 8, 2024
1 parent 1eb6b60 commit 9eb99ff
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
7 changes: 5 additions & 2 deletions node-data/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use dusk_bytes::{DeserializableSlice, Serializable};

use rand::rngs::StdRng;
use rand_core::SeedableRng;
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::cmp::Ordering;
use std::fmt::Debug;
Expand Down Expand Up @@ -104,8 +105,10 @@ impl std::fmt::Debug for PublicKey {
}
}
/// a wrapper of 96-sized array
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct PublicKeyBytes(pub [u8; PUBLIC_BLS_SIZE]);
#[derive(Clone, Copy, Eq, Hash, PartialEq, Serialize)]
pub struct PublicKeyBytes(
#[serde(serialize_with = "crate::serialize_b58")] pub [u8; PUBLIC_BLS_SIZE],
);

impl Default for PublicKeyBytes {
fn default() -> Self {
Expand Down
15 changes: 10 additions & 5 deletions node-data/src/ledger/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use serde::Serialize;

use super::*;

use crate::message::payload::RatificationResult;

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Serialize)]
#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
pub struct Attestation {
pub result: RatificationResult,
pub validation: StepVotes,
pub ratification: StepVotes,
}

#[derive(Debug, Default, Clone, Copy, Eq, Hash, PartialEq)]
#[derive(Debug, Default, Clone, Copy, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
pub struct StepVotes {
pub bitset: u64,
Expand All @@ -41,8 +43,10 @@ impl StepVotes {
}

/// a wrapper of 48-sized array to facilitate Signature
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Signature([u8; 48]);
#[derive(Clone, Copy, Eq, Hash, PartialEq, Serialize)]
pub struct Signature(
#[serde(serialize_with = "crate::serialize_hex")] [u8; 48],
);

impl Signature {
pub const EMPTY: [u8; 48] = [0u8; 48];
Expand Down Expand Up @@ -80,7 +84,8 @@ impl Default for Signature {
pub type IterationInfo = (Attestation, PublicKeyBytes);

/// Defines a set of attestations of any former iterations
#[derive(Default, Eq, PartialEq, Clone)]
#[derive(Default, Eq, PartialEq, Clone, Serialize)]
#[serde(transparent)]
pub struct IterationsInfo {
/// Represents a list of attestations where position is the iteration
/// number
Expand Down
11 changes: 10 additions & 1 deletion node-data/src/ledger/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,43 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use serde::Serialize;

use crate::message::ConsensusHeader;

use super::*;

pub type Seed = Signature;
#[derive(Default, Eq, PartialEq, Clone)]
#[derive(Default, Eq, PartialEq, Clone, Serialize)]
#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
pub struct Header {
// Hashable fields
pub version: u8,
pub height: u64,
pub timestamp: u64,
#[serde(serialize_with = "crate::serialize_hex")]
pub prev_block_hash: Hash,
pub seed: Seed,
#[serde(serialize_with = "crate::serialize_hex")]
pub state_hash: Hash,
#[serde(serialize_with = "crate::serialize_hex")]
pub event_hash: Hash,
pub generator_bls_pubkey: PublicKeyBytes,
#[serde(serialize_with = "crate::serialize_hex")]
pub txroot: Hash,
#[serde(serialize_with = "crate::serialize_hex")]
pub faultroot: Hash,
pub gas_limit: u64,
pub iteration: u8,
pub prev_block_cert: Attestation,
pub failed_iterations: IterationsInfo,

// Block hash
#[serde(serialize_with = "crate::serialize_hex")]
pub hash: Hash,

// Non-hashable fields
#[serde(skip_serializing)]
pub att: Attestation,
}

Expand Down
22 changes: 22 additions & 0 deletions node-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,25 @@ impl Serializable for Hash {
Self::read_bytes(r)
}
}

pub fn serialize_hex<const N: usize, S>(
t: &[u8; N],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex = hex::encode(t);
serializer.serialize_str(&hex)
}

pub fn serialize_b58<const N: usize, S>(
t: &[u8; N],
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex = bs58::encode(t).into_string();
serializer.serialize_str(&hex)
}
12 changes: 8 additions & 4 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ pub mod payload {
use std::time::{SystemTime, UNIX_EPOCH};

use super::{ConsensusHeader, SignInfo};
use serde::Serialize;

#[derive(Debug, Clone)]
#[cfg_attr(
Expand All @@ -396,13 +397,15 @@ pub mod payload {
pub sign_info: SignInfo,
}

#[derive(Clone, Copy, Hash, Eq, PartialEq, Default, PartialOrd, Ord)]
#[derive(
Clone, Copy, Hash, Eq, PartialEq, Default, PartialOrd, Ord, Serialize,
)]
#[cfg_attr(any(feature = "faker", test), derive(fake::Dummy))]
#[repr(u8)]
pub enum Vote {
NoCandidate = 0,
Valid(Hash) = 1,
Invalid(Hash) = 2,
Valid(#[serde(serialize_with = "crate::serialize_hex")] Hash) = 1,
Invalid(#[serde(serialize_with = "crate::serialize_hex")] Hash) = 2,

#[default]
NoQuorum = 3,
Expand Down Expand Up @@ -594,7 +597,8 @@ pub mod payload {
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize)]
#[serde(untagged)]
#[cfg_attr(any(feature = "faker", test), derive(fake::Dummy))]
pub enum RatificationResult {
Fail(Vote),
Expand Down

0 comments on commit 9eb99ff

Please sign in to comment.