Skip to content
Merged
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
1 change: 0 additions & 1 deletion .github/ci-groups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ groups:
- dashcore-rpc-json

tools:
- dashcore-test-utils
- dash-fuzz

# Crates intentionally not tested (with reason)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
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"]
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"]
resolver = "2"

[workspace.package]
Expand Down
1 change: 0 additions & 1 deletion dash-spv-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ clap = { version = "4.5", features = ["derive"] }
tempfile = "3.8"
serial_test = "3.0"
env_logger = "0.10"
dashcore-test-utils = { path = "../test-utils" }

[build-dependencies]
cbindgen = "0.29"
Expand Down
5 changes: 4 additions & 1 deletion dash-spv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ hickory-resolver = "0.25"
log = "0.4"

[dev-dependencies]
dash-spv = { path = ".", features = ["test-utils"] }
dashcore = { path = "../dash", features = ["test-utils"] }
key-wallet-manager = { path = "../key-wallet-manager", features = ["test-utils"] }
criterion = { version = "0.8.1", features = ["async_tokio"] }
tempfile = "3.0"
tokio-test = "0.4"
env_logger = "0.10"
hex = "0.4"
test-case = "3.3"
dashcore-test-utils = { path = "../test-utils" }

[[bench]]
name = "storage"
Expand All @@ -83,3 +85,4 @@ path = "src/lib.rs"
[features]
# Terminal UI feature (off by default, for use by binary only)
terminal-ui = ["dep:crossterm"]
test-utils = []
45 changes: 14 additions & 31 deletions dash-spv/src/chain/chain_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,14 @@ impl ChainTipManager {
#[cfg(test)]
mod tests {
use super::*;
use dashcore::blockdata::constants::genesis_block;
use dashcore::Network;

fn create_test_tip(height: u32, work_value: u8) -> ChainTip {
let mut header = genesis_block(Network::Dash).header;
header.nonce = height; // Make it unique

let mut work_bytes = [0u8; 32];
work_bytes[31] = work_value;
let chain_work = ChainWork::from_bytes(work_bytes);

ChainTip::new(header, height, chain_work)
}

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

// Add some tips with different work
for i in 0..3 {
let tip = create_test_tip(i, i as u8);
let tip = ChainTip::dummy(i, i as u8);
manager.add_tip(tip).expect("Failed to add tip");
}

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

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

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

// Fill to capacity
manager.add_tip(create_test_tip(1, 5)).expect("Failed to add first tip");
manager.add_tip(create_test_tip(2, 10)).expect("Failed to add second tip");
manager.add_tip(ChainTip::dummy(1, 5)).expect("Failed to add first tip");
manager.add_tip(ChainTip::dummy(2, 10)).expect("Failed to add second tip");

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

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

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

// Add two tips to fill capacity
let tip1 = create_test_tip(1, 5);
let tip1 = ChainTip::dummy(1, 5);
let tip1_hash = tip1.hash;
manager.add_tip(tip1).expect("Failed to add tip1");

let tip2 = create_test_tip(2, 10);
let tip2 = ChainTip::dummy(2, 10);
manager.add_tip(tip2).expect("Failed to add tip2");

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

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

// Add three tips
let tip1 = create_test_tip(1, 5);
let tip1 = ChainTip::dummy(1, 5);
let tip1_hash = tip1.hash;
manager.add_tip(tip1).expect("Failed to add tip1");

let tip2 = create_test_tip(2, 10);
let tip2 = ChainTip::dummy(2, 10);
manager.add_tip(tip2).expect("Failed to add tip2");

let tip3 = create_test_tip(3, 8);
let tip3 = ChainTip::dummy(3, 8);
manager.add_tip(tip3).expect("Failed to add tip3");

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

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

let result = manager.extend_tip(&tip1_hash, new_header, new_work);
assert!(result.is_ok());
Expand Down
23 changes: 5 additions & 18 deletions dash-spv/src/chain/chain_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ mod tests {

#[test]
fn test_chain_work_comparison() {
let work1 = ChainWork::from_bytes([0u8; 32]);
let mut bytes2 = [0u8; 32];
bytes2[31] = 1;
let work2 = ChainWork::from_bytes(bytes2);
let work1 = ChainWork::dummy(0);
let work2 = ChainWork::dummy(1);

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

#[test]
fn test_chain_work_addition() {
let mut bytes1 = [0u8; 32];
bytes1[31] = 100;
let work1 = ChainWork::from_bytes(bytes1);

let mut bytes2 = [0u8; 32];
bytes2[31] = 200;
let work2 = ChainWork::from_bytes(bytes2);
let work1 = ChainWork::dummy(100);
let work2 = ChainWork::dummy(200);

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

#[test]
fn test_chain_work_ordering() {
let works: Vec<ChainWork> = (0..5)
.map(|i| {
let mut bytes = [0u8; 32];
bytes[31] = i;
ChainWork::from_bytes(bytes)
})
.collect();
let works: Vec<ChainWork> = (0..5).map(ChainWork::dummy).collect();

for i in 0..4 {
assert!(works[i] < works[i + 1]);
Expand Down
50 changes: 13 additions & 37 deletions dash-spv/src/chain/chainlock_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ mod tests {
storage::{BlockHeaderStorage, DiskStorageManager},
types::ChainState,
};
use dashcore::{constants::genesis_block, ChainLock, Network};
use dashcore_test_utils::fixtures::test_block_hash;

/// Create a test ChainLock with minimal valid data
fn create_test_chainlock(height: u32, block_hash: BlockHash) -> ChainLock {
ChainLock {
block_height: height,
block_hash,
signature: dashcore::bls_sig_utils::BLSSignature::from([0u8; 96]), // BLS signature placeholder
}
}
use dashcore::{Header, Network};

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

// Create a test ChainLock
let chainlock = ChainLock {
block_height: 1000,
block_hash: test_block_hash(1),
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
};
let chainlock = ChainLock::dummy(1000);

// Process the ChainLock
let result = chainlock_manager
Expand Down Expand Up @@ -58,20 +43,15 @@ mod tests {
let chainlock_manager = ChainLockManager::new(true);
let chain_state = ChainState::new_for_network(Network::Testnet);

// Process first ChainLock at height 1000
let chainlock1 = create_test_chainlock(1000, test_block_hash(1));
let chainlock1 = ChainLock::dummy(1000);

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

// Process second ChainLock at height 2000
let chainlock2 = ChainLock {
block_height: 2000,
block_hash: test_block_hash(2),
signature: dashcore::bls_sig_utils::BLSSignature::from([1; 96]),
};
let chainlock2 = ChainLock::dummy(2000);

chainlock_manager
.process_chain_lock(chainlock2.clone(), &chain_state, &mut storage)
.await
Expand All @@ -95,11 +75,7 @@ mod tests {

// Add ChainLocks at heights 1000, 2000, 3000
for height in [1000, 2000, 3000] {
let chainlock = ChainLock {
block_height: height,
block_hash: test_block_hash(height),
signature: dashcore::bls_sig_utils::BLSSignature::from([0; 96]),
};
let chainlock = ChainLock::dummy(height);
chainlock_manager
.process_chain_lock(chainlock, &chain_state, &mut storage)
.await
Expand All @@ -119,9 +95,9 @@ mod tests {
let chainlock_manager = ChainLockManager::new(true);

// Queue multiple ChainLocks
let chain_lock1 = create_test_chainlock(100, BlockHash::from([1u8; 32]));
let chain_lock2 = create_test_chainlock(200, BlockHash::from([2u8; 32]));
let chain_lock3 = create_test_chainlock(300, BlockHash::from([3u8; 32]));
let chain_lock1 = ChainLock::dummy(100);
let chain_lock2 = ChainLock::dummy(200);
let chain_lock3 = ChainLock::dummy(300);

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

// Add test headers
let genesis = genesis_block(Network::Dash).header;
storage.store_headers_at_height(&[genesis], 0).await.unwrap();
let header = Header::dummy(0);
storage.store_headers_at_height(&[header], 0).await.unwrap();

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

let entry_by_hash = chainlock_manager.get_chain_lock_by_hash(&genesis.block_hash());
let entry_by_hash = chainlock_manager.get_chain_lock_by_hash(&header.block_hash());
assert!(entry_by_hash.is_some());
assert_eq!(entry_by_hash.unwrap().chain_lock.block_height, 0);
}
Expand Down
Loading