Skip to content

Commit

Permalink
fixes prop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Feb 7, 2025
1 parent 7a5489a commit 2b05c22
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
17 changes: 5 additions & 12 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,7 @@ where
),
};

let estimated_network_height = self
.latest_chain_tip
.estimate_network_chain_tip_height(&network, now)
.ok_or_misc_error("could not estimate network chain tip height")?;
let verification_progress =
(f64::from(tip_height.0) / f64::from(estimated_network_height.0)).min(1.0);
let verification_progress = f64::from(tip_height.0) / f64::from(estimated_height.0);
let zebra_state::ReadResponse::UsageInfo(size_on_disk) = state
.ready()
.and_then(|service| service.call(zebra_state::ReadRequest::UsageInfo))
Expand Down Expand Up @@ -2561,15 +2556,13 @@ where

// Get expanded difficulties (256 bits), these are the inverse of the work
let pow_limit: U256 = network.target_difficulty_limit().into();
let difficulty: U256 = chain_info
.expected_difficulty
.to_expanded()
.expect("valid blocks have valid difficulties")
.into();
let Some(difficulty) = chain_info.expected_difficulty.to_expanded() else {
return Ok(0.0);
};

// Shift out the lower 128 bits (256 bits, but the top 128 are all zeroes)
let pow_limit = pow_limit >> 128;
let difficulty = difficulty >> 128;
let difficulty = U256::from(difficulty) >> 128;

// Convert to u128 then f64.
// We could also convert U256 to String, then parse as f64, but that's slower.
Expand Down
52 changes: 48 additions & 4 deletions zebra-rpc/src/methods/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use zebra_chain::{
block::{self, Block, Height},
chain_tip::{mock::MockChainTip, ChainTip, NoChainTip},
parameters::{ConsensusBranchId, Network, NetworkUpgrade},
serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize},
serialization::{DateTime32, ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize},
transaction::{self, Transaction, UnminedTx, VerifiedUnminedTx},
transparent,
value_balance::ValueBalance,
};
use zebra_node_services::mempool;
use zebra_state::{BoxError, HashOrHeight};
use zebra_state::{BoxError, GetBlockTemplateChainInfo, HashOrHeight};

use zebra_test::mock_service::MockService;

Expand Down Expand Up @@ -410,6 +410,7 @@ proptest! {
let block_height = block.coinbase_height().unwrap();
let block_hash = block.hash();
let block_time = block.header.time;
let expected_size_on_disk = 1_000;

// check no requests were made during this test
runtime.block_on(async move {
Expand Down Expand Up @@ -446,6 +447,26 @@ proptest! {
height: Height::MIN,
next_block_hash: None,
});

state
.expect_request(zebra_state::ReadRequest::UsageInfo)
.await
.expect("getblockchaininfo should call mock state service with correct request")
.respond(zebra_state::ReadResponse::UsageInfo(expected_size_on_disk));

state
.expect_request(zebra_state::ReadRequest::ChainInfo)
.await
.expect("getblockchaininfo should call mock state service with correct request")
.respond(zebra_state::ReadResponse::ChainInfo(GetBlockTemplateChainInfo {
tip_hash: block_hash,
tip_height: block_height,
history_tree: Default::default(),
expected_difficulty: Default::default(),
cur_time: DateTime32::now(),
min_time: DateTime32::now(),
max_time: DateTime32::now()
}));
}
};

Expand All @@ -457,6 +478,7 @@ proptest! {
prop_assert_eq!(info.chain, network.bip70_network_name());
prop_assert_eq!(info.blocks, block_height);
prop_assert_eq!(info.best_block_hash, block_hash);
prop_assert_eq!(info.size_on_disk, expected_size_on_disk);
prop_assert!(info.estimated_height < Height::MAX);

prop_assert_eq!(
Expand All @@ -480,8 +502,8 @@ proptest! {
prop_assert_eq!(u.1.status, status);
}
}
Err(_) => {
unreachable!("Test should never error with the data we are feeding it")
Err(err) => {
unreachable!("Test should never error with the data we are feeding it: {err}")
}
};

Expand Down Expand Up @@ -528,6 +550,7 @@ proptest! {
let block_nonce = genesis_block.header.nonce;
let block_solution = genesis_block.header.solution;
let block_hash = genesis_block.header.hash();
let expected_size_on_disk = 1_000;

runtime.block_on(async move {
let response_fut = rpc.get_blockchain_info();
Expand Down Expand Up @@ -559,6 +582,27 @@ proptest! {
height: Height::MIN,
next_block_hash: None,
});

state
.expect_request(zebra_state::ReadRequest::UsageInfo)
.await
.expect("getblockchaininfo should call mock state service with correct request")
.respond(zebra_state::ReadResponse::UsageInfo(expected_size_on_disk));

state
.expect_request(zebra_state::ReadRequest::ChainInfo)
.await
.expect("getblockchaininfo should call mock state service with correct request")
.respond(zebra_state::ReadResponse::ChainInfo(GetBlockTemplateChainInfo {
tip_hash: block_hash,
tip_height: Height(0),
history_tree: Default::default(),
expected_difficulty: Default::default(),
cur_time: DateTime32::now(),
min_time: DateTime32::now(),
max_time: DateTime32::now()
}));

}
};

Expand Down

0 comments on commit 2b05c22

Please sign in to comment.