Skip to content

Commit 9f1a9b4

Browse files
authored
refactor: add test_utils modules (#347)
* test-utils feature and modules introduced * chain module unit tests clean * created MockWallet * created NonMatchingMockWallet * moved MockNetwork into test_utils * client module done * helper in key-wallet move into dashcore * instalock done * mempool filters unit tests cleaned * Address dummy usage consolidated * key-wallet-manager cleaned * storage unit test cleaned * cleaning * removed duplicated MockNetworkManager struct * fixed test that was using the wrong block header * consistency improved in the test-utils modules
1 parent 8bfd0db commit 9f1a9b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+662
-1807
lines changed

.github/ci-groups.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ groups:
2525
- dashcore-rpc-json
2626

2727
tools:
28-
- dashcore-test-utils
2928
- dash-fuzz
3029

3130
# Crates intentionally not tested (with reason)

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["dash", "dash-network", "dash-network-ffi", "hashes", "internals", "fuzz", "rpc-client", "rpc-json", "rpc-integration-test", "key-wallet", "key-wallet-ffi", "key-wallet-manager", "dash-spv", "dash-spv-ffi", "test-utils"]
2+
members = ["dash", "dash-network", "dash-network-ffi", "hashes", "internals", "fuzz", "rpc-client", "rpc-json", "rpc-integration-test", "key-wallet", "key-wallet-ffi", "key-wallet-manager", "dash-spv", "dash-spv-ffi"]
33
resolver = "2"
44

55
[workspace.package]

dash-spv-ffi/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ clap = { version = "4.5", features = ["derive"] }
3737
tempfile = "3.8"
3838
serial_test = "3.0"
3939
env_logger = "0.10"
40-
dashcore-test-utils = { path = "../test-utils" }
4140

4241
[build-dependencies]
4342
cbindgen = "0.29"

dash-spv/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ hickory-resolver = "0.25"
5959
log = "0.4"
6060

6161
[dev-dependencies]
62+
dash-spv = { path = ".", features = ["test-utils"] }
63+
dashcore = { path = "../dash", features = ["test-utils"] }
64+
key-wallet-manager = { path = "../key-wallet-manager", features = ["test-utils"] }
6265
criterion = { version = "0.8.1", features = ["async_tokio"] }
6366
tempfile = "3.0"
6467
tokio-test = "0.4"
6568
env_logger = "0.10"
6669
hex = "0.4"
6770
test-case = "3.3"
68-
dashcore-test-utils = { path = "../test-utils" }
6971

7072
[[bench]]
7173
name = "storage"
@@ -83,3 +85,4 @@ path = "src/lib.rs"
8385
[features]
8486
# Terminal UI feature (off by default, for use by binary only)
8587
terminal-ui = ["dep:crossterm"]
88+
test-utils = []

dash-spv/src/chain/chain_tip.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,14 @@ impl ChainTipManager {
195195
#[cfg(test)]
196196
mod tests {
197197
use super::*;
198-
use dashcore::blockdata::constants::genesis_block;
199-
use dashcore::Network;
200-
201-
fn create_test_tip(height: u32, work_value: u8) -> ChainTip {
202-
let mut header = genesis_block(Network::Dash).header;
203-
header.nonce = height; // Make it unique
204-
205-
let mut work_bytes = [0u8; 32];
206-
work_bytes[31] = work_value;
207-
let chain_work = ChainWork::from_bytes(work_bytes);
208-
209-
ChainTip::new(header, height, chain_work)
210-
}
211198

212199
#[test]
213200
fn test_tip_manager() {
214201
let mut manager = ChainTipManager::new(5);
215202

216203
// Add some tips with different work
217204
for i in 0..3 {
218-
let tip = create_test_tip(i, i as u8);
205+
let tip = ChainTip::dummy(i, i as u8);
219206
manager.add_tip(tip).expect("Failed to add tip");
220207
}
221208

@@ -227,7 +214,7 @@ mod tests {
227214
assert!(active.is_active);
228215

229216
// Add a tip with more work
230-
let better_tip = create_test_tip(1, 10);
217+
let better_tip = ChainTip::dummy(1, 10);
231218
manager.add_tip(better_tip).expect("Failed to add better tip");
232219

233220
// Active tip should update
@@ -240,11 +227,11 @@ mod tests {
240227
let mut manager = ChainTipManager::new(2);
241228

242229
// Fill to capacity
243-
manager.add_tip(create_test_tip(1, 5)).expect("Failed to add first tip");
244-
manager.add_tip(create_test_tip(2, 10)).expect("Failed to add second tip");
230+
manager.add_tip(ChainTip::dummy(1, 5)).expect("Failed to add first tip");
231+
manager.add_tip(ChainTip::dummy(2, 10)).expect("Failed to add second tip");
245232

246233
// Adding another should evict the weakest
247-
manager.add_tip(create_test_tip(3, 7)).expect("Failed to add third tip");
234+
manager.add_tip(ChainTip::dummy(3, 7)).expect("Failed to add third tip");
248235

249236
assert_eq!(manager.tip_count(), 2);
250237

@@ -258,18 +245,16 @@ mod tests {
258245
let mut manager = ChainTipManager::new(2);
259246

260247
// Add two tips to fill capacity
261-
let tip1 = create_test_tip(1, 5);
248+
let tip1 = ChainTip::dummy(1, 5);
262249
let tip1_hash = tip1.hash;
263250
manager.add_tip(tip1).expect("Failed to add tip1");
264251

265-
let tip2 = create_test_tip(2, 10);
252+
let tip2 = ChainTip::dummy(2, 10);
266253
manager.add_tip(tip2).expect("Failed to add tip2");
267254

268255
// Extend tip1 successfully - since we remove tip1 first, there's room for the new tip
269-
let new_header = create_test_tip(3, 6).header;
270-
let mut work_bytes = [0u8; 32];
271-
work_bytes[31] = 7; // Give it some work value
272-
let new_work = ChainWork::from_bytes(work_bytes);
256+
let new_header = ChainTip::dummy(3, 6).header;
257+
let new_work = ChainWork::dummy(7);
273258

274259
// The extend operation should succeed
275260
let result = manager.extend_tip(&tip1_hash, new_header, new_work);
@@ -295,25 +280,23 @@ mod tests {
295280
let mut manager = ChainTipManager::new(3);
296281

297282
// Add three tips
298-
let tip1 = create_test_tip(1, 5);
283+
let tip1 = ChainTip::dummy(1, 5);
299284
let tip1_hash = tip1.hash;
300285
manager.add_tip(tip1).expect("Failed to add tip1");
301286

302-
let tip2 = create_test_tip(2, 10);
287+
let tip2 = ChainTip::dummy(2, 10);
303288
manager.add_tip(tip2).expect("Failed to add tip2");
304289

305-
let tip3 = create_test_tip(3, 8);
290+
let tip3 = ChainTip::dummy(3, 8);
306291
manager.add_tip(tip3).expect("Failed to add tip3");
307292

308293
// Verify initial state
309294
assert_eq!(manager.tip_count(), 3);
310295
assert!(manager.get_tip(&tip1_hash).is_some());
311296

312297
// Extend tip1 - this should work and be atomic
313-
let new_header = create_test_tip(4, 6).header;
314-
let mut work_bytes = [0u8; 32];
315-
work_bytes[31] = 6;
316-
let new_work = ChainWork::from_bytes(work_bytes);
298+
let new_header = ChainTip::dummy(4, 6).header;
299+
let new_work = ChainWork::dummy(6);
317300

318301
let result = manager.extend_tip(&tip1_hash, new_header, new_work);
319302
assert!(result.is_ok());

dash-spv/src/chain/chain_work.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ mod tests {
155155

156156
#[test]
157157
fn test_chain_work_comparison() {
158-
let work1 = ChainWork::from_bytes([0u8; 32]);
159-
let mut bytes2 = [0u8; 32];
160-
bytes2[31] = 1;
161-
let work2 = ChainWork::from_bytes(bytes2);
158+
let work1 = ChainWork::dummy(0);
159+
let work2 = ChainWork::dummy(1);
162160

163161
assert!(work1 < work2);
164162
assert!(work2 > work1);
@@ -167,13 +165,8 @@ mod tests {
167165

168166
#[test]
169167
fn test_chain_work_addition() {
170-
let mut bytes1 = [0u8; 32];
171-
bytes1[31] = 100;
172-
let work1 = ChainWork::from_bytes(bytes1);
173-
174-
let mut bytes2 = [0u8; 32];
175-
bytes2[31] = 200;
176-
let work2 = ChainWork::from_bytes(bytes2);
168+
let work1 = ChainWork::dummy(100);
169+
let work2 = ChainWork::dummy(200);
177170

178171
let sum = work1.add(work2);
179172
assert_eq!(sum.work[31], 44); // 100 + 200 = 300, which is 44 + 256
@@ -189,13 +182,7 @@ mod tests {
189182

190183
#[test]
191184
fn test_chain_work_ordering() {
192-
let works: Vec<ChainWork> = (0..5)
193-
.map(|i| {
194-
let mut bytes = [0u8; 32];
195-
bytes[31] = i;
196-
ChainWork::from_bytes(bytes)
197-
})
198-
.collect();
185+
let works: Vec<ChainWork> = (0..5).map(ChainWork::dummy).collect();
199186

200187
for i in 0..4 {
201188
assert!(works[i] < works[i + 1]);

dash-spv/src/chain/chainlock_test.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ mod tests {
55
storage::{BlockHeaderStorage, DiskStorageManager},
66
types::ChainState,
77
};
8-
use dashcore::{constants::genesis_block, ChainLock, Network};
9-
use dashcore_test_utils::fixtures::test_block_hash;
10-
11-
/// Create a test ChainLock with minimal valid data
12-
fn create_test_chainlock(height: u32, block_hash: BlockHash) -> ChainLock {
13-
ChainLock {
14-
block_height: height,
15-
block_hash,
16-
signature: dashcore::bls_sig_utils::BLSSignature::from([0u8; 96]), // BLS signature placeholder
17-
}
18-
}
8+
use dashcore::{Header, Network};
199

2010
#[tokio::test]
2111
async fn test_chainlock_processing() {
@@ -25,12 +15,7 @@ mod tests {
2515
let chainlock_manager = ChainLockManager::new(true);
2616
let chain_state = ChainState::new_for_network(Network::Testnet);
2717

28-
// Create a test ChainLock
29-
let chainlock = ChainLock {
30-
block_height: 1000,
31-
block_hash: test_block_hash(1),
32-
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
33-
};
18+
let chainlock = ChainLock::dummy(1000);
3419

3520
// Process the ChainLock
3621
let result = chainlock_manager
@@ -58,20 +43,15 @@ mod tests {
5843
let chainlock_manager = ChainLockManager::new(true);
5944
let chain_state = ChainState::new_for_network(Network::Testnet);
6045

61-
// Process first ChainLock at height 1000
62-
let chainlock1 = create_test_chainlock(1000, test_block_hash(1));
46+
let chainlock1 = ChainLock::dummy(1000);
6347

6448
chainlock_manager
6549
.process_chain_lock(chainlock1.clone(), &chain_state, &mut storage)
6650
.await
6751
.expect("First ChainLock should process successfully");
6852

69-
// Process second ChainLock at height 2000
70-
let chainlock2 = ChainLock {
71-
block_height: 2000,
72-
block_hash: test_block_hash(2),
73-
signature: dashcore::bls_sig_utils::BLSSignature::from([1; 96]),
74-
};
53+
let chainlock2 = ChainLock::dummy(2000);
54+
7555
chainlock_manager
7656
.process_chain_lock(chainlock2.clone(), &chain_state, &mut storage)
7757
.await
@@ -95,11 +75,7 @@ mod tests {
9575

9676
// Add ChainLocks at heights 1000, 2000, 3000
9777
for height in [1000, 2000, 3000] {
98-
let chainlock = ChainLock {
99-
block_height: height,
100-
block_hash: test_block_hash(height),
101-
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
102-
};
78+
let chainlock = ChainLock::dummy(height);
10379
chainlock_manager
10480
.process_chain_lock(chainlock, &chain_state, &mut storage)
10581
.await
@@ -119,9 +95,9 @@ mod tests {
11995
let chainlock_manager = ChainLockManager::new(true);
12096

12197
// Queue multiple ChainLocks
122-
let chain_lock1 = create_test_chainlock(100, BlockHash::from([1u8; 32]));
123-
let chain_lock2 = create_test_chainlock(200, BlockHash::from([2u8; 32]));
124-
let chain_lock3 = create_test_chainlock(300, BlockHash::from([3u8; 32]));
98+
let chain_lock1 = ChainLock::dummy(100);
99+
let chain_lock2 = ChainLock::dummy(200);
100+
let chain_lock3 = ChainLock::dummy(300);
125101

126102
chainlock_manager.queue_pending_chainlock(chain_lock1).unwrap();
127103
chainlock_manager.queue_pending_chainlock(chain_lock2).unwrap();
@@ -145,11 +121,11 @@ mod tests {
145121
let chainlock_manager = ChainLockManager::new(true);
146122

147123
// Add test headers
148-
let genesis = genesis_block(Network::Dash).header;
149-
storage.store_headers_at_height(&[genesis], 0).await.unwrap();
124+
let header = Header::dummy(0);
125+
storage.store_headers_at_height(&[header], 0).await.unwrap();
150126

151127
// Create and process a ChainLock
152-
let chain_lock = create_test_chainlock(0, genesis.block_hash());
128+
let chain_lock = ChainLock::dummy(0);
153129
let chain_state = ChainState::new();
154130
let _ = chainlock_manager
155131
.process_chain_lock(chain_lock.clone(), &chain_state, &mut storage)
@@ -162,7 +138,7 @@ mod tests {
162138
assert!(entry.is_some());
163139
assert_eq!(entry.unwrap().chain_lock.block_height, 0);
164140

165-
let entry_by_hash = chainlock_manager.get_chain_lock_by_hash(&genesis.block_hash());
141+
let entry_by_hash = chainlock_manager.get_chain_lock_by_hash(&header.block_hash());
166142
assert!(entry_by_hash.is_some());
167143
assert_eq!(entry_by_hash.unwrap().chain_lock.block_height, 0);
168144
}

0 commit comments

Comments
 (0)