Skip to content

Commit f9caf5c

Browse files
committed
Use new concrete types for block header version and bits fields
1 parent 083f6ad commit f9caf5c

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

lightning-block-sync/src/convert.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,17 @@ impl TryFrom<serde_json::Value> for BlockHeaderData {
8989

9090
Ok(BlockHeaderData {
9191
header: Header {
92-
version: get_field!("version", as_i64).try_into().map_err(|_| ())?,
92+
version: bitcoin::blockdata::block::Version::from_consensus(
93+
get_field!("version", as_i64).try_into().map_err(|_| ())?
94+
),
9395
prev_blockhash: if let Some(hash_str) = response.get("previousblockhash") {
9496
BlockHash::from_hex(hash_str.as_str().ok_or(())?).map_err(|_| ())?
9597
} else { BlockHash::all_zeros() },
9698
merkle_root: TxMerkleNode::from_hex(get_field!("merkleroot", as_str)).map_err(|_| ())?,
9799
time: get_field!("time", as_u64).try_into().map_err(|_| ())?,
98-
bits: u32::from_be_bytes(<[u8; 4]>::from_hex(get_field!("bits", as_str)).map_err(|_| ())?),
100+
bits: bitcoin::CompactTarget::from_consensus(
101+
u32::from_be_bytes(<[u8; 4]>::from_hex(get_field!("bits", as_str)).map_err(|_| ())?)
102+
),
99103
nonce: get_field!("nonce", as_u64).try_into().map_err(|_| ())?,
100104
},
101105
chainwork: hex_to_work(get_field!("chainwork", as_str)).map_err(|_| ())?,
@@ -300,7 +304,7 @@ pub(crate) mod tests {
300304
serde_json::json!({
301305
"chainwork": chainwork.to_string()["0x".len()..],
302306
"height": height,
303-
"version": header.version,
307+
"version": header.version.to_consensus(),
304308
"merkleroot": header.merkle_root.to_hex(),
305309
"time": header.time,
306310
"nonce": header.nonce,

lightning-block-sync/src/poll.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ mod tests {
301301
let best_known_chain_tip = chain.at_height(0);
302302

303303
// Invalidate the tip by changing its target.
304-
chain.blocks.last_mut().unwrap().header.bits =
305-
BlockHeader::compact_target_from_u256(&Target::from_be_bytes([0; 32]));
304+
chain.blocks.last_mut().unwrap().header.bits = Target::from_be_bytes([0; 32]).to_compact_lossy();
306305

307306
let poller = ChainPoller::new(&chain, Network::Bitcoin);
308307
match poller.poll_chain_tip(best_known_chain_tip).await {

lightning-block-sync/src/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache};
22
use crate::poll::{Validate, ValidatedBlockHeader};
33

4-
use bitcoin::blockdata::block::{Block, Header};
4+
use bitcoin::blockdata::block::{Block, Header, Version};
55
use bitcoin::blockdata::constants::genesis_block;
66
use bitcoin::blockdata::locktime::absolute::LockTime;
77
use bitcoin::hash_types::{BlockHash, TxMerkleNode};
@@ -35,7 +35,7 @@ impl Blockchain {
3535

3636
pub fn with_height(mut self, height: usize) -> Self {
3737
self.blocks.reserve_exact(height);
38-
let bits = Header::compact_target_from_u256(&Target::from_be_bytes([0xff; 32]));
38+
let bits = Target::from_be_bytes([0xff; 32]).to_compact_lossy();
3939
for i in 1..=height {
4040
let prev_block = &self.blocks[i - 1];
4141
let prev_blockhash = prev_block.block_hash();
@@ -53,7 +53,7 @@ impl Blockchain {
5353
let merkle_root = TxMerkleNode::from_raw_hash(coinbase.txid().as_hash()).unwrap();
5454
self.blocks.push(Block {
5555
header: Header {
56-
version: 0,
56+
version: Version::NO_SOFT_FORK_SIGNALLING,
5757
prev_blockhash,
5858
merkle_root,
5959
time,

lightning/src/ln/functional_test_utils.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ use crate::util::ser::{ReadableArgs, Writeable};
3333
#[cfg(test)]
3434
use crate::util::logger::Logger;
3535

36-
use bitcoin::blockdata::block::{Block, Header};
36+
use bitcoin::blockdata::block::{Block, Header, Version};
3737
use bitcoin::blockdata::locktime::absolute::LockTime;
3838
use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut};
3939
use bitcoin::hash_types::{BlockHash, TxMerkleNode};
4040
use bitcoin::hashes::sha256::Hash as Sha256;
4141
use bitcoin::hashes::Hash as _;
4242
use bitcoin::network::constants::Network;
43+
use bitcoin::pow::CompactTarget;
4344
use bitcoin::secp256k1::{PublicKey, SecretKey};
4445

4546
use crate::io;
@@ -80,7 +81,14 @@ pub fn mine_transactions<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, txn: &[&Tra
8081
pub fn mine_transaction_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &Transaction) {
8182
let height = node.best_block_info().1 + 1;
8283
let mut block = Block {
83-
header: Header { version: 0x20000000, prev_blockhash: node.best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: height, bits: 42, nonce: 42 },
84+
header: Header {
85+
version: Version::NO_SOFT_FORK_SIGNALLING,
86+
prev_blockhash: node.best_block_hash(),
87+
merkle_root: TxMerkleNode::all_zeros(),
88+
time: height,
89+
bits: CompactTarget::from_consensus(42),
90+
nonce: 42,
91+
},
8492
txdata: Vec::new(),
8593
};
8694
for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
@@ -206,11 +214,11 @@ impl ConnectStyle {
206214

207215
pub fn create_dummy_header(prev_blockhash: BlockHash, time: u32) -> Header {
208216
Header {
209-
version: 0x2000_0000,
217+
version: Version::NO_SOFT_FORK_SIGNALLING,
210218
prev_blockhash,
211219
merkle_root: TxMerkleNode::all_zeros(),
212220
time,
213-
bits: 42,
221+
bits: CompactTarget::from_consensus(42),
214222
nonce: 42,
215223
}
216224
}

0 commit comments

Comments
 (0)