From bd42f156df30694469e6d6a57cf6069feca7d646 Mon Sep 17 00:00:00 2001 From: Boyu Yang Date: Mon, 16 Oct 2023 16:51:57 +0800 Subject: [PATCH] refactor: set default value of `Hex` to `0x` --- protocol/src/lazy.rs | 2 +- protocol/src/types/primitive.rs | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/protocol/src/lazy.rs b/protocol/src/lazy.rs index 8e2721004..2b715adf9 100644 --- a/protocol/src/lazy.rs +++ b/protocol/src/lazy.rs @@ -5,7 +5,7 @@ use crate::{ckb_blake2b_256, types::Hex}; lazy_static::lazy_static! { pub static ref CHAIN_ID: ArcSwap = ArcSwap::from_pointee(Default::default()); - pub static ref PROTOCOL_VERSION: ArcSwap = ArcSwap::from_pointee(Default::default()); + pub static ref PROTOCOL_VERSION: ArcSwap = ArcSwap::from_pointee(Hex::with_length(8)); pub static ref DUMMY_INPUT_OUT_POINT: packed::OutPoint = packed::OutPointBuilder::default() diff --git a/protocol/src/types/primitive.rs b/protocol/src/types/primitive.rs index eb02ae548..4e4685d97 100644 --- a/protocol/src/types/primitive.rs +++ b/protocol/src/types/primitive.rs @@ -79,6 +79,10 @@ impl Hex { Hex(Bytes::default()) } + pub fn with_length(len: usize) -> Self { + Hex(vec![0u8; len].into()) + } + pub fn is_empty(&self) -> bool { self.0.is_empty() } @@ -108,12 +112,6 @@ impl Hex { } } -impl Default for Hex { - fn default() -> Self { - Hex(vec![0u8; 8].into()) - } -} - impl AsRef<[u8]> for Hex { fn as_ref(&self) -> &[u8] { &self.0 @@ -459,7 +457,7 @@ pub struct Validator { pub vote_weight: u32, } -#[derive(RlpEncodable, RlpDecodable, Serialize, Deserialize, Clone, PartialEq, Eq, Default)] +#[derive(RlpEncodable, RlpDecodable, Serialize, Deserialize, Clone, PartialEq, Eq)] pub struct ValidatorExtend { pub bls_pub_key: Hex, pub pub_key: Hex, @@ -492,6 +490,18 @@ impl From<&ValidatorExtend> for Validator { } } +impl Default for ValidatorExtend { + fn default() -> Self { + Self { + bls_pub_key: Hex::with_length(8), + pub_key: Hex::with_length(8), + address: Default::default(), + propose_weight: Default::default(), + vote_weight: Default::default(), + } + } +} + impl std::fmt::Debug for ValidatorExtend { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let bls_pub_key = self.bls_pub_key.as_string_trim0x();