Skip to content

Commit 3a4b658

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 small data. Use test_block_hash helper from dashcore-test-utils instead of computing X11 hash.
1 parent 82eb15f commit 3a4b658

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

dash-spv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ tokio-test = "0.4"
6565
env_logger = "0.10"
6666
hex = "0.4"
6767
test-case = "3.3"
68+
dashcore-test-utils = { path = "../test-utils" }
6869

6970
[[bench]]
7071
name = "storage"

dash-spv/src/chain/chainlock_test.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
mod tests {
33
use super::super::*;
44
use crate::{storage::DiskStorageManager, types::ChainState};
5-
use dashcore::{BlockHash, ChainLock, Network};
6-
use dashcore_hashes::Hash;
5+
use dashcore::{ChainLock, Network};
6+
use dashcore_test_utils::fixtures::test_block_hash;
77

88
#[tokio::test]
99
async fn test_chainlock_processing() {
@@ -16,7 +16,7 @@ mod tests {
1616
// Create a test ChainLock
1717
let chainlock = ChainLock {
1818
block_height: 1000,
19-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[1, 2, 3])),
19+
block_hash: test_block_hash(1),
2020
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
2121
};
2222

@@ -49,7 +49,7 @@ mod tests {
4949
// Process first ChainLock at height 1000
5050
let chainlock1 = ChainLock {
5151
block_height: 1000,
52-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[1, 2, 3])),
52+
block_hash: test_block_hash(1),
5353
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
5454
};
5555
chainlock_manager
@@ -60,7 +60,7 @@ mod tests {
6060
// Process second ChainLock at height 2000
6161
let chainlock2 = ChainLock {
6262
block_height: 2000,
63-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(&[4, 5, 6])),
63+
block_hash: test_block_hash(2),
6464
signature: dashcore::bls_sig_utils::BLSSignature::from([1; 96]),
6565
};
6666
chainlock_manager
@@ -88,9 +88,7 @@ mod tests {
8888
for height in [1000, 2000, 3000] {
8989
let chainlock = ChainLock {
9090
block_height: height,
91-
block_hash: BlockHash::from_raw_hash(dashcore_hashes::hash_x11::Hash::hash(
92-
&height.to_le_bytes(),
93-
)),
91+
block_hash: test_block_hash(height),
9492
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
9593
};
9694
chainlock_manager

dash-spv/src/chain/checkpoint_test.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ mod tests {
55
use super::super::checkpoints::*;
66
use dashcore::{BlockHash, CompactTarget, Target};
77
use dashcore_hashes::Hash;
8+
use dashcore_test_utils::fixtures::test_block_hash;
89

910
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())
11+
let block_hash = test_block_hash(height);
12+
let prev_blockhash = if height > 0 {
13+
test_block_hash(height - 1)
1314
} else {
14-
dashcore_hashes::hash_x11::Hash::all_zeros()
15+
BlockHash::all_zeros()
1516
};
1617

1718
Checkpoint {
1819
height,
19-
block_hash: BlockHash::from_raw_hash(hash_bytes),
20-
prev_blockhash: BlockHash::from_raw_hash(prev_bytes),
20+
block_hash,
21+
prev_blockhash,
2122
timestamp,
2223
target: Target::from_compact(CompactTarget::from_consensus(0x1d00ffff)),
23-
merkle_root: Some(BlockHash::from_raw_hash(hash_bytes)),
24+
merkle_root: Some(block_hash),
2425
chain_work: format!("0x{:064x}", height * 1000),
2526
masternode_list_name: if height.is_multiple_of(100000) && height > 0 {
2627
Some(format!("ML{}__70230", height))

test-utils/src/fixtures.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub fn testnet_genesis_hash() -> BlockHash {
3838
BlockHash::from_slice(&reversed).unwrap()
3939
}
4040

41+
/// Create a deterministic test block hash from a u32 identifier
42+
pub fn test_block_hash(id: u32) -> BlockHash {
43+
let mut bytes = [0u8; 32];
44+
bytes[..4].copy_from_slice(&id.to_le_bytes());
45+
BlockHash::from_byte_array(bytes)
46+
}
47+
4148
/// Common test transaction IDs
4249
pub mod txids {
4350
use super::*;

0 commit comments

Comments
 (0)