dusk_node_data/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7#![deny(unused_crate_dependencies)]
8#![deny(unused_extern_crates)]
9
10pub mod bls;
11pub mod encoding;
12pub mod events;
13pub mod ledger;
14pub mod message;
15
16use std::io::{self, Read, Write};
17use std::time::{SystemTime, UNIX_EPOCH};
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub enum StepName {
21    Proposal = 0,
22    Validation = 1,
23    Ratification = 2,
24}
25
26impl StepName {
27    pub fn to_step(self, iteration: u8) -> u8 {
28        iteration * 3 + (self as u8)
29    }
30}
31
32pub trait Serializable {
33    fn write<W: Write>(&self, w: &mut W) -> io::Result<()>;
34    fn read<R: Read>(r: &mut R) -> io::Result<Self>
35    where
36        Self: Sized;
37
38    fn read_bytes<R: Read, const N: usize>(r: &mut R) -> io::Result<[u8; N]> {
39        let mut buffer = [0u8; N];
40        r.read_exact(&mut buffer)?;
41        Ok(buffer)
42    }
43
44    fn read_u8<R: Read>(r: &mut R) -> io::Result<u8> {
45        let mut num = [0u8; 1];
46        r.read_exact(&mut num)?;
47        Ok(num[0])
48    }
49
50    fn read_u16_le<R: Read>(r: &mut R) -> io::Result<u16> {
51        let data = Self::read_bytes(r)?;
52        Ok(u16::from_le_bytes(data))
53    }
54
55    fn read_u64_le<R: Read>(r: &mut R) -> io::Result<u64> {
56        let data = Self::read_bytes(r)?;
57        Ok(u64::from_le_bytes(data))
58    }
59    fn read_u32_le<R: Read>(r: &mut R) -> io::Result<u32> {
60        let data = Self::read_bytes(r)?;
61        Ok(u32::from_le_bytes(data))
62    }
63
64    /// Writes length-prefixed fields
65    fn write_var_le_bytes32<W: Write>(w: &mut W, buf: &[u8]) -> io::Result<()> {
66        let len = buf.len() as u32;
67        w.write_all(&len.to_le_bytes())?;
68        w.write_all(buf)?;
69        Ok(())
70    }
71
72    /// Reads length-prefixed fields
73    fn read_var_le_bytes32<R: Read>(r: &mut R) -> io::Result<Vec<u8>> {
74        let len = Self::read_u32_le(r)? as usize;
75
76        let mut buf = vec![0u8; len];
77        r.read_exact(&mut buf)?;
78
79        Ok(buf)
80    }
81}
82
83impl<const N: usize> Serializable for [u8; N] {
84    fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
85        w.write_all(&self[..])
86    }
87
88    fn read<R: Read>(r: &mut R) -> io::Result<Self>
89    where
90        Self: Sized,
91    {
92        Self::read_bytes(r)
93    }
94}
95
96pub fn serialize_hex<const N: usize, S>(
97    t: &[u8; N],
98    serializer: S,
99) -> Result<S::Ok, S::Error>
100where
101    S: serde::Serializer,
102{
103    let hex = hex::encode(t);
104    serializer.serialize_str(&hex)
105}
106
107pub fn serialize_b58<const N: usize, S>(
108    t: &[u8; N],
109    serializer: S,
110) -> Result<S::Ok, S::Error>
111where
112    S: serde::Serializer,
113{
114    let hex = bs58::encode(t).into_string();
115    serializer.serialize_str(&hex)
116}
117
118pub fn get_current_timestamp() -> u64 {
119    SystemTime::now()
120        .duration_since(UNIX_EPOCH)
121        .map(|n| n.as_secs())
122        .expect("This is heavy.")
123}