casper_types/system/auction/
reservation.rs1use alloc::vec::Vec;
2use core::fmt::{self, Display, Formatter};
3#[cfg(any(feature = "testing", test))]
4use rand::{
5 distributions::{Distribution, Standard},
6 Rng,
7};
8
9#[cfg(feature = "datasize")]
10use datasize::DataSize;
11#[cfg(feature = "json-schema")]
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14
15use crate::{
16 bytesrepr::{self, FromBytes, ToBytes},
17 CLType, CLTyped, PublicKey,
18};
19
20use super::{DelegationRate, DelegatorKind};
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24#[cfg_attr(feature = "datasize", derive(DataSize))]
25#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
26#[serde(deny_unknown_fields)]
27pub struct Reservation {
28 delegator_kind: DelegatorKind,
30 validator_public_key: PublicKey,
32 delegation_rate: DelegationRate,
34}
35
36impl Reservation {
37 pub fn new(
39 validator_public_key: PublicKey,
40 delegator_kind: DelegatorKind,
41 delegation_rate: DelegationRate,
42 ) -> Self {
43 Self {
44 delegator_kind,
45 validator_public_key,
46 delegation_rate,
47 }
48 }
49
50 pub fn delegator_kind(&self) -> &DelegatorKind {
52 &self.delegator_kind
53 }
54
55 pub fn validator_public_key(&self) -> &PublicKey {
57 &self.validator_public_key
58 }
59
60 pub fn delegation_rate(&self) -> &DelegationRate {
62 &self.delegation_rate
63 }
64}
65
66impl CLTyped for Reservation {
67 fn cl_type() -> CLType {
68 CLType::Any
69 }
70}
71
72impl ToBytes for Reservation {
73 fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
74 let mut buffer = bytesrepr::allocate_buffer(self)?;
75 buffer.extend(self.delegator_kind.to_bytes()?);
76 buffer.extend(self.validator_public_key.to_bytes()?);
77 buffer.extend(self.delegation_rate.to_bytes()?);
78 Ok(buffer)
79 }
80
81 fn serialized_length(&self) -> usize {
82 self.delegator_kind.serialized_length()
83 + self.validator_public_key.serialized_length()
84 + self.delegation_rate.serialized_length()
85 }
86
87 fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
88 self.delegator_kind.write_bytes(writer)?;
89 self.validator_public_key.write_bytes(writer)?;
90 self.delegation_rate.write_bytes(writer)?;
91 Ok(())
92 }
93}
94
95impl FromBytes for Reservation {
96 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
97 let (delegator_kind, bytes) = DelegatorKind::from_bytes(bytes)?;
98 let (validator_public_key, bytes) = PublicKey::from_bytes(bytes)?;
99 let (delegation_rate, bytes) = FromBytes::from_bytes(bytes)?;
100 Ok((
101 Self {
102 delegator_kind,
103 validator_public_key,
104 delegation_rate,
105 },
106 bytes,
107 ))
108 }
109}
110
111impl Display for Reservation {
112 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
113 write!(
114 formatter,
115 "Reservation {{ delegator {}, validator {} }}",
116 self.delegator_kind, self.validator_public_key
117 )
118 }
119}
120
121#[cfg(any(feature = "testing", test))]
122impl Distribution<Reservation> for Standard {
123 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Reservation {
124 Reservation {
125 delegator_kind: rng.gen(),
126 validator_public_key: rng.gen(),
127 delegation_rate: rng.gen(),
128 }
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use crate::{bytesrepr, system::auction::Reservation, PublicKey, SecretKey};
135
136 #[test]
137 fn serialization_roundtrip() {
138 let delegator_kind = PublicKey::from(
139 &SecretKey::ed25519_from_bytes([42; SecretKey::ED25519_LENGTH]).unwrap(),
140 )
141 .into();
142
143 let validator_public_key: PublicKey = PublicKey::from(
144 &SecretKey::ed25519_from_bytes([43; SecretKey::ED25519_LENGTH]).unwrap(),
145 );
146 let entry = Reservation::new(validator_public_key, delegator_kind, 0);
147 bytesrepr::test_serialization_roundtrip(&entry);
148 }
149}
150
151#[cfg(test)]
152mod prop_tests {
153 use proptest::prelude::*;
154
155 use crate::{bytesrepr, gens};
156
157 proptest! {
158 #[test]
159 fn test_value_bid(bid in gens::reservation_arb()) {
160 bytesrepr::test_serialization_roundtrip(&bid);
161 }
162 }
163}