Skip to content

Commit

Permalink
refactor: set default value of Hex to 0x
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Oct 16, 2023
1 parent ec26fab commit bd42f15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion protocol/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{ckb_blake2b_256, types::Hex};

lazy_static::lazy_static! {
pub static ref CHAIN_ID: ArcSwap<u64> = ArcSwap::from_pointee(Default::default());
pub static ref PROTOCOL_VERSION: ArcSwap<Hex> = ArcSwap::from_pointee(Default::default());
pub static ref PROTOCOL_VERSION: ArcSwap<Hex> = ArcSwap::from_pointee(Hex::with_length(8));

pub static ref DUMMY_INPUT_OUT_POINT: packed::OutPoint
= packed::OutPointBuilder::default()
Expand Down
24 changes: 17 additions & 7 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bd42f15

Please sign in to comment.