use exonum_derive::ObjectHash;
use exonum_merkledb::{impl_binary_key_for_binary_value, BinaryValue};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{borrow::Cow, fmt, num::ParseIntError, str::FromStr};
pub type Milliseconds = u64;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(ObjectHash)]
pub struct Height(pub u64);
impl Height {
pub fn zero() -> Self {
Self(0)
}
pub fn next(self) -> Self {
Self(self.0 + 1)
}
pub fn previous(self) -> Self {
assert_ne!(0, self.0);
Self(self.0 - 1)
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn decrement(&mut self) {
assert_ne!(0, self.0);
self.0 -= 1;
}
}
impl BinaryValue for Height {
fn to_bytes(&self) -> Vec<u8> {
self.0.into_bytes()
}
fn from_bytes(value: Cow<'_, [u8]>) -> anyhow::Result<Self> {
let value = <u64 as BinaryValue>::from_bytes(value)?;
Ok(Self(value))
}
}
impl_binary_key_for_binary_value! { Height }
impl fmt::Display for Height {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Height> for u64 {
fn from(val: Height) -> Self {
val.0
}
}
impl Serialize for Height {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Height {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Self(u64::deserialize(deserializer)?))
}
}
impl FromStr for Height {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, ParseIntError> {
u64::from_str(s).map(Self)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize, Deserialize, ObjectHash)]
pub struct Round(pub u32);
impl Round {
pub fn zero() -> Self {
Self(0)
}
pub fn first() -> Self {
Self(1)
}
pub fn next(self) -> Self {
Self(self.0 + 1)
}
pub fn previous(self) -> Self {
assert_ne!(0, self.0);
Self(self.0 - 1)
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn decrement(&mut self) {
assert_ne!(0, self.0);
self.0 -= 1;
}
pub fn iter_to(self, to: Self) -> impl Iterator<Item = Self> {
(self.0..to.0).map(Self)
}
}
impl BinaryValue for Round {
fn to_bytes(&self) -> Vec<u8> {
self.0.into_bytes()
}
fn from_bytes(value: Cow<'_, [u8]>) -> anyhow::Result<Self> {
let value = <u32 as BinaryValue>::from_bytes(value)?;
Ok(Self(value))
}
}
impl fmt::Display for Round {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Round> for u32 {
fn from(val: Round) -> Self {
val.0
}
}
impl From<Round> for u64 {
fn from(val: Round) -> Self {
Self::from(val.0)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize, Deserialize)]
pub struct ValidatorId(pub u16);
impl ValidatorId {
pub fn zero() -> Self {
Self(0)
}
}
impl BinaryValue for ValidatorId {
fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> anyhow::Result<Self> {
u16::from_bytes(bytes).map(Self)
}
}
impl fmt::Display for ValidatorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<ValidatorId> for u16 {
fn from(val: ValidatorId) -> Self {
val.0
}
}
impl From<ValidatorId> for usize {
fn from(val: ValidatorId) -> Self {
val.0 as Self
}
}