Kaynağa Gözat

blockchain/block_store: Derive serialization instead of manual impl

parazyd 2 yıl önce
ebeveyn
işleme
1960ca795c
1 değiştirilmiş dosya ile 2 ekleme ve 58 silme
  1. 2 58
      src/blockchain/block_store.rs

+ 2 - 58
src/blockchain/block_store.rs

@@ -164,7 +164,7 @@ pub struct BlockOrder {
 /// Note: we only need height cummulative ranks, but we also keep its actual
 /// ranks, so we can verify the sequence and/or know specific block height
 /// ranks, if ever needed.
-#[derive(Debug)]
+#[derive(Debug, SerialEncodable, SerialDecodable)]
 pub struct BlockRanks {
     /// Block target rank
     pub target_rank: BigUint,
@@ -187,39 +187,11 @@ impl BlockRanks {
     }
 }
 
-// Note: Doing all the imports here as this might get obselete if
-// we implemented Encodable/Decodable for num_bigint::BigUint.
-impl darkfi_serial::Encodable for BlockRanks {
-    fn encode<S: std::io::Write>(&self, s: &mut S) -> std::io::Result<usize> {
-        let mut len = 0;
-        len += self.target_rank.to_bytes_be().encode(s)?;
-        len += self.targets_rank.to_bytes_be().encode(s)?;
-        len += self.hash_rank.to_bytes_be().encode(s)?;
-        len += self.hashes_rank.to_bytes_be().encode(s)?;
-        Ok(len)
-    }
-}
-
-impl darkfi_serial::Decodable for BlockRanks {
-    fn decode<D: std::io::Read>(d: &mut D) -> std::io::Result<Self> {
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let target_rank: BigUint = BigUint::from_bytes_be(&bytes);
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let targets_rank: BigUint = BigUint::from_bytes_be(&bytes);
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let hash_rank: BigUint = BigUint::from_bytes_be(&bytes);
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let hashes_rank: BigUint = BigUint::from_bytes_be(&bytes);
-        let ret = Self { target_rank, targets_rank, hash_rank, hashes_rank };
-        Ok(ret)
-    }
-}
-
 /// Auxiliary structure used to keep track of block PoW difficulty information.
 /// Note: we only need height cummulative difficulty, but we also keep its actual
 /// difficulty, so we can verify the sequence and/or know specific block height
 /// difficulty, if ever needed.
-#[derive(Debug)]
+#[derive(Debug, SerialEncodable, SerialDecodable)]
 pub struct BlockDifficulty {
     /// Block height number
     pub height: u32,
@@ -256,34 +228,6 @@ impl BlockDifficulty {
     }
 }
 
-// Note: Doing all the imports here as this might get obselete if
-// we implemented Encodable/Decodable for num_bigint::BigUint.
-impl darkfi_serial::Encodable for BlockDifficulty {
-    fn encode<S: std::io::Write>(&self, s: &mut S) -> std::io::Result<usize> {
-        let mut len = 0;
-        len += self.height.encode(s)?;
-        len += self.timestamp.encode(s)?;
-        len += self.difficulty.to_bytes_be().encode(s)?;
-        len += self.cummulative_difficulty.to_bytes_be().encode(s)?;
-        len += self.ranks.encode(s)?;
-        Ok(len)
-    }
-}
-
-impl darkfi_serial::Decodable for BlockDifficulty {
-    fn decode<D: std::io::Read>(d: &mut D) -> std::io::Result<Self> {
-        let height: u32 = darkfi_serial::Decodable::decode(d)?;
-        let timestamp: Timestamp = darkfi_serial::Decodable::decode(d)?;
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let difficulty: BigUint = BigUint::from_bytes_be(&bytes);
-        let bytes: Vec<u8> = darkfi_serial::Decodable::decode(d)?;
-        let cummulative_difficulty: BigUint = BigUint::from_bytes_be(&bytes);
-        let ranks: BlockRanks = darkfi_serial::Decodable::decode(d)?;
-        let ret = Self { height, timestamp, difficulty, cummulative_difficulty, ranks };
-        Ok(ret)
-    }
-}
-
 pub const SLED_BLOCK_TREE: &[u8] = b"_blocks";
 pub const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
 pub const SLED_BLOCK_DIFFICULTY_TREE: &[u8] = b"_block_difficulty";