dusk_node_data/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

#![deny(unused_crate_dependencies)]
#![deny(unused_extern_crates)]

pub mod archive;
pub mod bls;
pub mod encoding;
pub mod events;
pub mod ledger;
pub mod message;

use std::io::{self, Read, Write};
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StepName {
    Proposal = 0,
    Validation = 1,
    Ratification = 2,
}

impl StepName {
    pub fn to_step(self, iteration: u8) -> u8 {
        iteration * 3 + (self as u8)
    }
}

pub trait Serializable {
    fn write<W: Write>(&self, w: &mut W) -> io::Result<()>;
    fn read<R: Read>(r: &mut R) -> io::Result<Self>
    where
        Self: Sized;

    fn read_bytes<R: Read, const N: usize>(r: &mut R) -> io::Result<[u8; N]> {
        let mut buffer = [0u8; N];
        r.read_exact(&mut buffer)?;
        Ok(buffer)
    }

    fn read_u8<R: Read>(r: &mut R) -> io::Result<u8> {
        let mut num = [0u8; 1];
        r.read_exact(&mut num)?;
        Ok(num[0])
    }

    fn read_u16_le<R: Read>(r: &mut R) -> io::Result<u16> {
        let data = Self::read_bytes(r)?;
        Ok(u16::from_le_bytes(data))
    }

    fn read_u64_le<R: Read>(r: &mut R) -> io::Result<u64> {
        let data = Self::read_bytes(r)?;
        Ok(u64::from_le_bytes(data))
    }
    fn read_u32_le<R: Read>(r: &mut R) -> io::Result<u32> {
        let data = Self::read_bytes(r)?;
        Ok(u32::from_le_bytes(data))
    }

    /// Writes length-prefixed fields
    fn write_var_le_bytes32<W: Write>(w: &mut W, buf: &[u8]) -> io::Result<()> {
        let len = buf.len() as u32;
        w.write_all(&len.to_le_bytes())?;
        w.write_all(buf)?;
        Ok(())
    }

    /// Reads length-prefixed fields
    fn read_var_le_bytes32<R: Read>(r: &mut R) -> io::Result<Vec<u8>> {
        let len = Self::read_u32_le(r)? as usize;

        let mut buf = vec![0u8; len];
        r.read_exact(&mut buf)?;

        Ok(buf)
    }
}

impl<const N: usize> Serializable for [u8; N] {
    fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
        w.write_all(&self[..])
    }

    fn read<R: Read>(r: &mut R) -> io::Result<Self>
    where
        Self: Sized,
    {
        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)
}

pub fn get_current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|n| n.as_secs())
        .expect("This is heavy.")
}