near_primitives_core/
types.rs1use std::num::ParseIntError;
2use std::ops::Add;
3use std::str::FromStr;
4
5use crate::hash::CryptoHash;
6
7pub use crate::account::id::AccountId;
9pub use crate::gas::Gas;
10pub type MerkleHash = CryptoHash;
12pub type ValidatorId = u64;
14pub type ValidatorMask = Vec<bool>;
16pub type StorageUsage = u64;
18pub type StorageUsageChange = i64;
20pub type Nonce = u64;
22pub type NonceIndex = u32;
24pub type BlockHeight = u64;
26pub type EpochHeight = u64;
28pub type Balance = near_token::NearToken;
30pub type Compute = u64;
32
33#[derive(Clone, Debug, PartialEq, Eq)]
36pub struct GasWeight(pub u64);
37
38pub type NumBlocks = u64;
40pub type NumShards = u64;
42pub type NumSeats = u64;
44pub type BlockHeightDelta = u64;
46
47pub type ReceiptIndex = usize;
48pub type PromiseId = Vec<ReceiptIndex>;
49
50pub type ProtocolVersion = u32;
51
52pub type ShardIndex = usize;
57
58#[derive(
66 arbitrary::Arbitrary,
67 borsh::BorshSerialize,
68 borsh::BorshDeserialize,
69 serde::Serialize,
70 serde::Deserialize,
71 Hash,
72 Clone,
73 Copy,
74 PartialEq,
75 Eq,
76 PartialOrd,
77 Ord,
78)]
79#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
80pub struct ShardId(u64);
81
82impl ShardId {
83 pub const fn new(id: u64) -> Self {
104 Self(id)
105 }
106
107 pub fn to_le_bytes(self) -> [u8; 8] {
108 self.0.to_le_bytes()
109 }
110
111 pub fn to_be_bytes(self) -> [u8; 8] {
112 self.0.to_be_bytes()
113 }
114
115 pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
116 Self(u64::from_le_bytes(bytes))
117 }
118
119 pub fn max() -> Self {
122 Self(u64::MAX)
123 }
124}
125
126impl std::fmt::Debug for ShardId {
127 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128 write!(f, "{}", self.0)
129 }
130}
131
132impl std::fmt::Display for ShardId {
133 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134 write!(f, "{}", self.0)
135 }
136}
137
138impl From<u64> for ShardId {
139 fn from(id: u64) -> Self {
140 Self(id)
141 }
142}
143
144impl Into<u64> for ShardId {
145 fn into(self) -> u64 {
146 self.0
147 }
148}
149
150impl From<u32> for ShardId {
151 fn from(id: u32) -> Self {
152 Self(id as u64)
153 }
154}
155
156impl Into<u32> for ShardId {
157 fn into(self) -> u32 {
158 self.0 as u32
159 }
160}
161
162impl From<i32> for ShardId {
163 fn from(id: i32) -> Self {
164 Self(id as u64)
165 }
166}
167
168impl From<usize> for ShardId {
169 fn from(id: usize) -> Self {
170 Self(id as u64)
171 }
172}
173
174impl From<u16> for ShardId {
175 fn from(id: u16) -> Self {
176 Self(id as u64)
177 }
178}
179
180impl Into<u16> for ShardId {
181 fn into(self) -> u16 {
182 self.0 as u16
183 }
184}
185
186impl Into<usize> for ShardId {
187 fn into(self) -> usize {
188 self.0 as usize
189 }
190}
191
192impl<T> Add<T> for ShardId
193where
194 T: Add<u64, Output = u64>,
195{
196 type Output = Self;
197
198 fn add(self, rhs: T) -> Self::Output {
199 Self(T::add(rhs, self.0))
200 }
201}
202
203impl PartialEq<u64> for ShardId {
204 fn eq(&self, other: &u64) -> bool {
205 self.0 == *other
206 }
207}
208
209impl FromStr for ShardId {
210 type Err = ParseIntError;
211
212 fn from_str(s: &str) -> Result<Self, Self::Err> {
213 let shard_id = s.parse::<u64>()?;
214 Ok(ShardId(shard_id))
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use crate::types::ShardId;
221
222 #[test]
226 fn test_shard_id_borsh() {
227 let shard_id_u64 = 42;
228 let shard_id = ShardId::new(shard_id_u64);
229
230 assert_eq!(borsh::to_vec(&shard_id_u64).unwrap(), borsh::to_vec(&shard_id).unwrap());
231 }
232}