Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ chrono = { version = "0.4.38", optional = true }

[features]
default = ["chrono"]
all = ["chrono", "stl", "serde"]
all = ["liquid", "chrono", "stl", "serde"]

liquid = []

stl = ["strict_types"]
serde = [
"dep:serde",
Expand Down
29 changes: 28 additions & 1 deletion consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]

Check warning on line 37 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L37

Added line #L37 was not covered by tests
#[wrapper(BorrowSlice, Index, RangeOps, Debug, Hex, Display, FromStr)]
pub struct BlockHash(
#[from]
Expand Down Expand Up @@ -73,53 +73,67 @@
0xbf5beb43_6012afca,
0x590b1a11_466e2206,
]);
#[cfg(feature = "liquid")]
pub const LIQUID_MAINNET: BlockHash = BlockHash::from_u64_be_array([
0x14662758_36220db2,
0x944ca059_a3a10ef6,
0xfd2ea684_b0688d2c,
0x37929688_8a206003,
]);
#[cfg(feature = "liquid")]
pub const LIQUID_TESTNET: BlockHash = BlockHash::from_u64_be_array([
0xa771da8e_52ee6ad5,
0x81ed1e9a_99825e5b,
0x3b799222_5534eaa2,
0xae23244f_e26ab1c1,
]);

pub const fn from_u64_be_array(array: [u64; 4]) -> Self {
let mut buf = [0u8; 32];
let x = array[0].to_be_bytes();
buf[31] = x[0];
buf[30] = x[1];
buf[29] = x[2];
buf[28] = x[3];
buf[27] = x[4];
buf[26] = x[5];
buf[25] = x[6];
buf[24] = x[7];
let x = array[1].to_be_bytes();
buf[23] = x[0];
buf[22] = x[1];
buf[21] = x[2];
buf[20] = x[3];
buf[19] = x[4];
buf[18] = x[5];
buf[17] = x[6];
buf[16] = x[7];
let x = array[2].to_be_bytes();
buf[15] = x[0];
buf[14] = x[1];
buf[13] = x[2];
buf[12] = x[3];
buf[11] = x[4];
buf[10] = x[5];
buf[9] = x[6];
buf[8] = x[7];
let x = array[3].to_be_bytes();
buf[7] = x[0];
buf[6] = x[1];
buf[5] = x[2];
buf[4] = x[3];
buf[3] = x[4];
buf[2] = x[5];
buf[1] = x[6];
buf[0] = x[7];
Self(Bytes32StrRev::from_array(buf))
}

Check warning on line 130 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L91-L130

Added lines #L91 - L130 were not covered by tests
}

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]

Check warning on line 136 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L136

Added line #L136 was not covered by tests
#[wrapper(BorrowSlice, Index, RangeOps, Debug, Hex, Display, FromStr)]
pub struct BlockMerkleRoot(
#[from]
Expand Down Expand Up @@ -174,7 +188,7 @@

#[derive(Clone, PartialEq, Eq, Hash, Debug, Display)]
#[display(LowerHex)]
#[derive(StrictType, StrictEncode, StrictDecode, StrictDumb)]

Check warning on line 191 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L191

Added line #L191 was not covered by tests
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Block {
Expand All @@ -183,22 +197,22 @@
}

impl LowerHex for Block {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(&self.consensus_serialize().to_hex())
}

Check warning on line 202 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L200-L202

Added lines #L200 - L202 were not covered by tests
}

impl FromStr for Block {
type Err = BlockDataParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let data = Vec::<u8>::from_hex(s)?;
Block::consensus_deserialize(data).map_err(BlockDataParseError::from)
}

Check warning on line 211 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L208-L211

Added lines #L208 - L211 were not covered by tests
}

impl Block {
pub fn block_hash(&self) -> BlockHash { self.header.block_hash() }

Check warning on line 215 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L215

Added line #L215 was not covered by tests
}

#[cfg(test)]
Expand Down Expand Up @@ -231,7 +245,7 @@
}

#[test]
fn genesis_hashes() {
fn bitcoin_genesis_hashes() {
assert_eq!(
&BlockHash::GENESIS_MAINNET.to_string(),
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
Expand All @@ -253,4 +267,17 @@
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
);
}

#[test]
#[cfg(feature = "liquid")]
fn liquid_genesis_hashes() {
assert_eq!(
&BlockHash::LIQUID_MAINNET.to_string(),
"1466275836220db2944ca059a3a10ef6fd2ea684b0688d2c379296888a206003"
);
assert_eq!(
&BlockHash::LIQUID_TESTNET.to_string(),
"a771da8e52ee6ad581ed1e9a99825e5b3b7992225534eaa2ae23244fe26ab1c1"
);
}
}
Loading