Skip to main content

rialo_types/
rex.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module contains types used to describe the REX network.
5
6use std::{fmt, mem::size_of};
7
8use borsh::{BorshDeserialize, BorshSerialize};
9use serde::{Deserialize, Serialize};
10
11/// The representation of an epoch ID.
12#[derive(
13    BorshSerialize,
14    BorshDeserialize,
15    Clone,
16    Copy,
17    Debug,
18    Default,
19    Deserialize,
20    Eq,
21    Ord,
22    PartialEq,
23    PartialOrd,
24    Serialize,
25)]
26pub struct EpochId(pub u64);
27
28impl EpochId {
29    /// The maximum size used when serializing an [`EpochId`] using [`borsh`].
30    pub const BORSH_SERIALIZED_SIZE: usize = size_of::<u64>();
31}
32
33impl fmt::Display for EpochId {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.0)
36    }
37}
38
39/// Index of a committee in the REX configuration.
40#[derive(
41    BorshSerialize,
42    BorshDeserialize,
43    Clone,
44    Copy,
45    Debug,
46    Default,
47    Deserialize,
48    Eq,
49    Ord,
50    PartialEq,
51    PartialOrd,
52    Serialize,
53)]
54pub struct CommitteeIndex(pub u32);
55
56impl CommitteeIndex {
57    /// The maximum size used when serializing a [`CommitteeIndex`] using [`borsh`].
58    pub const BORSH_SERIALIZED_SIZE: usize = size_of::<u32>();
59}
60
61impl fmt::Display for CommitteeIndex {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "{}", self.0)
64    }
65}
66
67/// Index of a node within a REX committee.
68#[derive(
69    BorshSerialize,
70    BorshDeserialize,
71    Clone,
72    Copy,
73    Debug,
74    Default,
75    Deserialize,
76    Eq,
77    Ord,
78    PartialEq,
79    PartialOrd,
80    Serialize,
81)]
82pub struct NodeIndex(pub u32);
83
84impl NodeIndex {
85    /// The maximum size used when serializing a [`NodeIndex`] using [`borsh`].
86    pub const BORSH_SERIALIZED_SIZE: usize = size_of::<u32>();
87}
88
89impl fmt::Display for NodeIndex {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        write!(f, "{}", self.0)
92    }
93}
94
95/// The identifier to locate a REX node in a REX configuration.
96#[derive(
97    BorshSerialize,
98    BorshDeserialize,
99    Clone,
100    Copy,
101    Debug,
102    Default,
103    Deserialize,
104    Eq,
105    Ord,
106    PartialEq,
107    PartialOrd,
108    Serialize,
109)]
110pub struct NodeId {
111    pub epoch: EpochId,
112    pub committee: CommitteeIndex,
113    pub index: NodeIndex,
114}
115
116impl NodeId {
117    /// The maximum size used when serializing an [`NodeId`] using [`borsh`].
118    pub const BORSH_SERIALIZED_SIZE: usize = EpochId::BORSH_SERIALIZED_SIZE
119        + CommitteeIndex::BORSH_SERIALIZED_SIZE
120        + NodeIndex::BORSH_SERIALIZED_SIZE;
121}