Skip to main content

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 hard_fork;
14pub mod ledger;
15pub mod message;
16
17use std::io::{self, Read, Write};
18use std::time::{SystemTime, UNIX_EPOCH};
19
20/// Maximum number of transactions allowed in a block.
21pub const MAX_NUMBER_OF_TRANSACTIONS: usize = 1_000;
22
23/// Maximum number of faults allowed in a block.
24pub const MAX_NUMBER_OF_FAULTS: usize = 100;
25
26/// Covers mempool inventory exchanges while preventing unbounded allocation.
27pub(crate) const MAX_INV_ITEMS: u32 = 10_000;
28
29/// Maximum serialized error-string length in `SpentTransaction`.
30pub(crate) const MAX_SPENT_TX_ERROR_BYTES: usize = 2 * 1024 * 1024;
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub enum StepName {
34    Proposal = 0,
35    Validation = 1,
36    Ratification = 2,
37}
38
39impl StepName {
40    pub fn to_step(self, iteration: u8) -> u8 {
41        iteration * 3 + (self as u8)
42    }
43}
44
45pub trait Serializable {
46    fn write<W: Write>(&self, w: &mut W) -> io::Result<()>;
47    fn read<R: Read>(r: &mut R) -> io::Result<Self>
48    where
49        Self: Sized;
50
51    fn read_bytes<R: Read, const N: usize>(r: &mut R) -> io::Result<[u8; N]> {
52        let mut buffer = [0u8; N];
53        r.read_exact(&mut buffer)?;
54        Ok(buffer)
55    }
56
57    fn read_u8<R: Read>(r: &mut R) -> io::Result<u8> {
58        let mut num = [0u8; 1];
59        r.read_exact(&mut num)?;
60        Ok(num[0])
61    }
62
63    fn read_u16_le<R: Read>(r: &mut R) -> io::Result<u16> {
64        let data = Self::read_bytes(r)?;
65        Ok(u16::from_le_bytes(data))
66    }
67
68    fn read_u64_le<R: Read>(r: &mut R) -> io::Result<u64> {
69        let data = Self::read_bytes(r)?;
70        Ok(u64::from_le_bytes(data))
71    }
72    fn read_u32_le<R: Read>(r: &mut R) -> io::Result<u32> {
73        let data = Self::read_bytes(r)?;
74        Ok(u32::from_le_bytes(data))
75    }
76
77    /// Writes length-prefixed fields
78    fn write_var_le_bytes32<W: Write>(w: &mut W, buf: &[u8]) -> io::Result<()> {
79        let len = buf.len() as u32;
80        w.write_all(&len.to_le_bytes())?;
81        w.write_all(buf)?;
82        Ok(())
83    }
84
85    /// Reads length-prefixed fields with a caller-provided size cap.
86    fn read_var_le_bytes32<R: Read>(
87        r: &mut R,
88        max_size: usize,
89    ) -> io::Result<Vec<u8>> {
90        let len = Self::read_u32_le(r)? as usize;
91        if len > max_size {
92            return Err(io::Error::new(
93                io::ErrorKind::InvalidData,
94                format!("length-prefixed field too large: {len} > {max_size}"),
95            ));
96        }
97
98        let mut buf = vec![0u8; len];
99        r.read_exact(&mut buf)?;
100
101        Ok(buf)
102    }
103}
104
105impl<const N: usize> Serializable for [u8; N] {
106    fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
107        w.write_all(&self[..])
108    }
109
110    fn read<R: Read>(r: &mut R) -> io::Result<Self>
111    where
112        Self: Sized,
113    {
114        Self::read_bytes(r)
115    }
116}
117
118pub fn serialize_hex<const N: usize, S>(
119    t: &[u8; N],
120    serializer: S,
121) -> Result<S::Ok, S::Error>
122where
123    S: serde::Serializer,
124{
125    let hex = hex::encode(t);
126    serializer.serialize_str(&hex)
127}
128
129pub fn serialize_b58<const N: usize, S>(
130    t: &[u8; N],
131    serializer: S,
132) -> Result<S::Ok, S::Error>
133where
134    S: serde::Serializer,
135{
136    let hex = bs58::encode(t).into_string();
137    serializer.serialize_str(&hex)
138}
139
140pub fn get_current_timestamp() -> u64 {
141    SystemTime::now()
142        .duration_since(UNIX_EPOCH)
143        .map(|n| n.as_secs())
144        .expect("This is heavy.")
145}