Skip to content

Commit cd07015

Browse files
committed
fix: avoid X11 hash with wrong input size in tests
X11 hash requires exactly 80 bytes (block header size). Tests were incorrectly passing arbitrary small data. Use BlockHash::from_byte_array with padded test data instead.
1 parent a39f946 commit cd07015

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

dash-spv/src/chain/chainlock_test.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ mod tests {
55
use dashcore::{BlockHash, ChainLock, Network};
66
use dashcore_hashes::Hash;
77

8+
/// Create a test block hash from a single byte (for distinct test hashes)
9+
fn test_block_hash(id: u8) -> BlockHash {
10+
let mut bytes = [0u8; 32];
11+
bytes[0] = id;
12+
BlockHash::from_byte_array(bytes)
13+
}
14+
815
#[tokio::test]
916
async fn test_chainlock_processing() {
1017
// Create storage and ChainLock manager
@@ -16,7 +23,7 @@ mod tests {
1623
// Create a test ChainLock
1724
let chainlock = ChainLock {
1825
block_height: 1000,
19-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[1, 2, 3])),
26+
block_hash: test_block_hash(1),
2027
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
2128
};
2229

@@ -49,7 +56,7 @@ mod tests {
4956
// Process first ChainLock at height 1000
5057
let chainlock1 = ChainLock {
5158
block_height: 1000,
52-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[1, 2, 3])),
59+
block_hash: test_block_hash(1),
5360
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
5461
};
5562
chainlock_manager
@@ -60,7 +67,7 @@ mod tests {
6067
// Process second ChainLock at height 2000
6168
let chainlock2 = ChainLock {
6269
block_height: 2000,
63-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[4, 5, 6])),
70+
block_hash: test_block_hash(2),
6471
signature: dashcore::bls_sig_utils::BLSSignature::from([1; 96]),
6572
};
6673
chainlock_manager
@@ -85,12 +92,10 @@ mod tests {
8592
DiskStorageManager::with_temp_dir().await.expect("Failed to create tmp storage");
8693

8794
// Add ChainLocks at heights 1000, 2000, 3000
88-
for height in [1000, 2000, 3000] {
95+
for (i, height) in [1000u32, 2000, 3000].iter().enumerate() {
8996
let chainlock = ChainLock {
90-
block_height: height,
91-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(
92-
&height.to_le_bytes(),
93-
)),
97+
block_height: *height,
98+
block_hash: test_block_hash((i + 10) as u8),
9499
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
95100
};
96101
chainlock_manager

dash-spv/src/chain/checkpoint_test.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,28 @@ mod tests {
66
use dashcore::{BlockHash, CompactTarget, Target};
77
use dashcore_hashes::Hash;
88

9+
/// Create a test block hash from a height (for distinct test hashes)
10+
fn test_block_hash(height: u32) -> BlockHash {
11+
let mut bytes = [0u8; 32];
12+
bytes[..4].copy_from_slice(&height.to_le_bytes());
13+
BlockHash::from_byte_array(bytes)
14+
}
15+
916
fn create_test_checkpoint(height: u32, timestamp: u32) -> Checkpoint {
10-
let hash_bytes = dashcore_hashes::hash_x11::Hash::hash(&height.to_le_bytes());
11-
let prev_bytes = if height > 0 {
12-
dashcore_hashes::hash_x11::Hash::hash(&(height - 1).to_le_bytes())
17+
let block_hash = test_block_hash(height);
18+
let prev_blockhash = if height > 0 {
19+
test_block_hash(height - 1)
1320
} else {
14-
dashcore_hashes::hash_x11::Hash::all_zeros()
21+
BlockHash::from_byte_array([0u8; 32])
1522
};
1623

1724
Checkpoint {
1825
height,
19-
block_hash: BlockHash::from_raw_hash(hash_bytes),
20-
prev_blockhash: BlockHash::from_raw_hash(prev_bytes),
26+
block_hash,
27+
prev_blockhash,
2128
timestamp,
2229
target: Target::from_compact(CompactTarget::from_consensus(0x1d00ffff)),
23-
merkle_root: Some(BlockHash::from_raw_hash(hash_bytes)),
30+
merkle_root: Some(block_hash),
2431
chain_work: format!("0x{:064x}", height * 1000),
2532
masternode_list_name: if height.is_multiple_of(100000) && height > 0 {
2633
Some(format!("ML{}__70230", height))

0 commit comments

Comments
 (0)