1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
use std::{fmt, iter::Sum}; use datasize::DataSize; use num::Zero; use serde::{Deserialize, Serialize}; use casper_types::{ bytesrepr::{self, FromBytes, ToBytes}, U512, }; use crate::shared::gas::Gas; #[derive( DataSize, Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, )] pub struct Motes(U512); impl Motes { pub fn new(value: U512) -> Motes { Motes(value) } pub fn checked_add(&self, rhs: Self) -> Option<Self> { self.0.checked_add(rhs.value()).map(Self::new) } pub fn value(&self) -> U512 { self.0 } pub fn from_gas(gas: Gas, conv_rate: u64) -> Option<Self> { gas.value() .checked_mul(U512::from(conv_rate)) .map(Self::new) } } impl fmt::Display for Motes { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self.0) } } impl std::ops::Add for Motes { type Output = Motes; fn add(self, rhs: Self) -> Self::Output { let val = self.value() + rhs.value(); Motes::new(val) } } impl std::ops::Sub for Motes { type Output = Motes; fn sub(self, rhs: Self) -> Self::Output { let val = self.value() - rhs.value(); Motes::new(val) } } impl std::ops::Div for Motes { type Output = Motes; fn div(self, rhs: Self) -> Self::Output { let val = self.value() / rhs.value(); Motes::new(val) } } impl std::ops::Mul for Motes { type Output = Motes; fn mul(self, rhs: Self) -> Self::Output { let val = self.value() * rhs.value(); Motes::new(val) } } impl Zero for Motes { fn zero() -> Self { Motes::new(U512::zero()) } fn is_zero(&self) -> bool { self.0.is_zero() } } impl Sum for Motes { fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { iter.fold(Motes::zero(), std::ops::Add::add) } } impl ToBytes for Motes { fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> { self.0.to_bytes() } fn serialized_length(&self) -> usize { self.0.serialized_length() } } impl FromBytes for Motes { fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> { let (value, remainder) = FromBytes::from_bytes(bytes)?; Ok((Motes::new(value), remainder)) } } #[cfg(test)] mod tests { use casper_types::U512; use crate::shared::{gas::Gas, motes::Motes}; #[test] fn should_be_able_to_get_instance_of_motes() { let initial_value = 1; let motes = Motes::new(U512::from(initial_value)); assert_eq!( initial_value, motes.value().as_u64(), "should have equal value" ) } #[test] fn should_be_able_to_compare_two_instances_of_motes() { let left_motes = Motes::new(U512::from(1)); let right_motes = Motes::new(U512::from(1)); assert_eq!(left_motes, right_motes, "should be equal"); let right_motes = Motes::new(U512::from(2)); assert_ne!(left_motes, right_motes, "should not be equal") } #[test] fn should_be_able_to_add_two_instances_of_motes() { let left_motes = Motes::new(U512::from(1)); let right_motes = Motes::new(U512::from(1)); let expected_motes = Motes::new(U512::from(2)); assert_eq!( (left_motes + right_motes), expected_motes, "should be equal" ) } #[test] fn should_be_able_to_subtract_two_instances_of_motes() { let left_motes = Motes::new(U512::from(1)); let right_motes = Motes::new(U512::from(1)); let expected_motes = Motes::new(U512::from(0)); assert_eq!( (left_motes - right_motes), expected_motes, "should be equal" ) } #[test] fn should_be_able_to_multiply_two_instances_of_motes() { let left_motes = Motes::new(U512::from(100)); let right_motes = Motes::new(U512::from(10)); let expected_motes = Motes::new(U512::from(1000)); assert_eq!( (left_motes * right_motes), expected_motes, "should be equal" ) } #[test] fn should_be_able_to_divide_two_instances_of_motes() { let left_motes = Motes::new(U512::from(1000)); let right_motes = Motes::new(U512::from(100)); let expected_motes = Motes::new(U512::from(10)); assert_eq!( (left_motes / right_motes), expected_motes, "should be equal" ) } #[test] fn should_be_able_to_convert_from_motes() { let gas = Gas::new(U512::from(100)); let motes = Motes::from_gas(gas, 10).expect("should have value"); let expected_motes = Motes::new(U512::from(1000)); assert_eq!(motes, expected_motes, "should be equal") } #[test] fn should_be_able_to_default() { let motes = Motes::default(); let expected_motes = Motes::new(U512::from(0)); assert_eq!(motes, expected_motes, "should be equal") } #[test] fn should_be_able_to_compare_relative_value() { let left_motes = Motes::new(U512::from(100)); let right_motes = Motes::new(U512::from(10)); assert!(left_motes > right_motes, "should be gt"); let right_motes = Motes::new(U512::from(100)); assert!(left_motes >= right_motes, "should be gte"); assert!(left_motes <= right_motes, "should be lte"); let left_motes = Motes::new(U512::from(10)); assert!(left_motes < right_motes, "should be lt"); } #[test] fn should_default() { let left_motes = Motes::new(U512::from(0)); let right_motes = Motes::default(); assert_eq!(left_motes, right_motes, "should be equal"); let u512 = U512::zero(); assert_eq!(left_motes.value(), u512, "should be equal"); } #[test] fn should_support_checked_mul_from_gas() { let gas = Gas::new(U512::MAX); let conv_rate = 10; let maybe = Motes::from_gas(gas, conv_rate); assert!(maybe.is_none(), "should be none due to overflow"); } }