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
use crate::common::block_status::BlockStatus; use crate::serialization::block::BlockDB as ProtoBlockDB; use crate::traits::EnumConverter; use crate::traits::{BlockHeader, Decode, Encode, Proto}; use protobuf::Message as ProtoMessage; use std::error::Error; #[derive(Clone, Debug)] pub struct Meta<HeaderType> where HeaderType: BlockHeader + Clone + Encode + Proto, { pub height: u32, pub header: HeaderType, pub t_ema: f64, pub p_ema: f64, pub next_difficulty: f64, pub total_work: f64, pub file_number: Option<u32>, pub offset: Option<u64>, pub length: Option<u32>, pub status: BlockStatus, } impl<HeaderType> Meta<HeaderType> where HeaderType: BlockHeader + Clone + Encode + Proto, { pub fn new( height: u32, header: HeaderType, t_ema: f64, p_ema: f64, next_difficulty: f64, total_work: f64, file_number: Option<u32>, offset: Option<u64>, length: Option<u32>, status: BlockStatus, ) -> Self { Self { height, header, t_ema, p_ema, next_difficulty, total_work, file_number, offset, length, status, } } } impl<HeaderType> Proto for Meta<HeaderType> where HeaderType: BlockHeader + Clone + Proto + Encode, { type ProtoType = ProtoBlockDB; fn to_proto(&self) -> Result<Self::ProtoType, Box<Error>> { let mut proto_meta = Self::ProtoType::new(); proto_meta.set_height(self.height); proto_meta.set_tEMA(self.t_ema); proto_meta.set_pEMA(self.p_ema); proto_meta.set_nextDifficulty(self.next_difficulty); proto_meta.set_totalWork(self.total_work); match self.file_number { Some(fd) => proto_meta.set_fileNumber(fd), None => {} } match self.offset { Some(offset) => proto_meta.set_offset(offset), None => {} } match self.length { Some(length) => proto_meta.set_length(length), None => {} } proto_meta.set_status(self.status.to_output() as u32); Ok(proto_meta) } fn from_proto(_prototype: &Self::ProtoType) -> Result<Self, Box<Error>> { unimplemented!() } } impl<HeaderType> Encode for Meta<HeaderType> where HeaderType: BlockHeader + Clone + Proto + Encode, { fn encode(&self) -> Result<Vec<u8>, Box<Error>> { let proto_meta = self.to_proto()?; Ok(proto_meta.write_to_bytes()?) } } impl<HeaderType> Decode for Meta<HeaderType> where HeaderType: Proto<ProtoType = crate::serialization::blockHeader::BlockHeader> + BlockHeader + Clone + Encode, { fn decode(buffer: &[u8]) -> Result<Meta<HeaderType>, Box<Error>> { let mut proto_meta = ProtoBlockDB::new(); proto_meta.merge_from_bytes(buffer)?; let meta_info = Meta::new( proto_meta.height, HeaderType::from_proto(&proto_meta.get_header())?, proto_meta.tEMA, proto_meta.pEMA, proto_meta.nextDifficulty, proto_meta.totalWork, Some(proto_meta.fileNumber), Some(proto_meta.offset), Some(proto_meta.length), BlockStatus::from_input(proto_meta.status as u8)?, ); Ok(meta_info) } } #[cfg(test)] mod tests { use super::*; use crate::common::block::tests::create_test_header; #[test] fn it_makes_meta_without_file_info() { let height = 150000; let t_ema = 30.00; let p_ema = 0.000001; let next_difficulty = 0.0001; let total_work = 1e15; let meta = Meta::new( height, create_test_header(), t_ema, p_ema, next_difficulty, total_work, None, None, None, BlockStatus::Nothing, ); assert_eq!(meta.height, height); assert_eq!(meta.t_ema, t_ema); assert_eq!(meta.p_ema, p_ema); assert_eq!(meta.total_work, total_work); assert_eq!(meta.file_number, None); assert_eq!(meta.offset, None); assert_eq!(meta.length, None); } #[test] fn it_makes_meta_with_file_info() { let height = 123456789; let t_ema = 1234.0; let p_ema = 0.1234; let next_difficulty = 0.012345; let total_work = 1e23; let offset = 123; let file_number = 234; let length = 345; let meta = Meta::new( height, create_test_header(), t_ema, p_ema, next_difficulty, total_work, Some(file_number), Some(offset), Some(length), BlockStatus::Header, ); assert_eq!(meta.height, height); assert_eq!(meta.t_ema, t_ema); assert_eq!(meta.p_ema, p_ema); assert_eq!(meta.next_difficulty, next_difficulty); assert_eq!(meta.total_work, total_work); assert_eq!(meta.offset.unwrap(), offset); assert_eq!(meta.file_number.unwrap(), file_number); assert_eq!(meta.length.unwrap(), length); } }