Skip to content

Commit

Permalink
Fix invalid calculation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Oct 10, 2023
1 parent bcce69b commit f9442da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
55 changes: 26 additions & 29 deletions zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,31 @@ use zebra_network::AddressBookPeers;
use zebra_node_services::mempool;
use zebra_state::{ReadRequest, ReadResponse};

use crate::{
constants::no_blocks_in_state_error,
methods::{
best_chain_tip_height,
get_block_template_rpcs::{
constants::{
DEFAULT_SOLUTION_RATE_WINDOW_SIZE, GET_BLOCK_TEMPLATE_MEMPOOL_LONG_POLL_INTERVAL,
ZCASHD_FUNDING_STREAM_ORDER,
},
get_block_template::{
check_miner_address, check_synced_to_tip, fetch_mempool_transactions,
fetch_state_tip_and_local_time, validate_block_proposal,
},
// TODO: move the types/* modules directly under get_block_template_rpcs,
// and combine any modules with the same names.
types::{
get_block_template::GetBlockTemplate,
get_mining_info,
hex_data::HexData,
long_poll::LongPollInput,
peer_info::PeerInfo,
submit_block,
subsidy::{BlockSubsidy, FundingStream},
unified_address, validate_address, z_validate_address,
},
use crate::methods::{
best_chain_tip_height,
get_block_template_rpcs::{
constants::{
DEFAULT_SOLUTION_RATE_WINDOW_SIZE, GET_BLOCK_TEMPLATE_MEMPOOL_LONG_POLL_INTERVAL,
ZCASHD_FUNDING_STREAM_ORDER,
},
get_block_template::{
check_miner_address, check_synced_to_tip, fetch_mempool_transactions,
fetch_state_tip_and_local_time, validate_block_proposal,
},
// TODO: move the types/* modules directly under get_block_template_rpcs,
// and combine any modules with the same names.
types::{
get_block_template::GetBlockTemplate,
get_mining_info,
hex_data::HexData,
long_poll::LongPollInput,
peer_info::PeerInfo,
submit_block,
subsidy::{BlockSubsidy, FundingStream},
unified_address, validate_address, z_validate_address,
},
height_from_signed_int, GetBlockHash, MISSING_BLOCK_ERROR_CODE,
},
height_from_signed_int, GetBlockHash, MISSING_BLOCK_ERROR_CODE,
};

pub mod constants;
Expand Down Expand Up @@ -878,9 +875,9 @@ where
})?;

let solution_rate = match response {
ReadResponse::SolutionRate(solution_rate) => {
solution_rate.ok_or_else(|| no_blocks_in_state_error())?
}
// zcashd returns a 0 rate when the calculation is invalid
ReadResponse::SolutionRate(solution_rate) => solution_rate.unwrap_or(0),

_ => unreachable!("unmatched response to a solution rate request"),
};

Expand Down
10 changes: 4 additions & 6 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,6 @@ async fn rpc_getnetworksolps() {
use zebra_chain::chain_sync_status::MockSyncStatus;
use zebra_network::address_book_peers::MockAddressBookPeers;

use crate::constants::no_blocks_in_state_error;

let _init_guard = zebra_test::init();

// Create a continuous chain of mainnet blocks from genesis
Expand Down Expand Up @@ -1153,22 +1151,22 @@ async fn rpc_getnetworksolps() {
// num_blocks, height, return value
(None, None, Ok(2)),
(Some(-4), None, Ok(2)),
(Some(-3), Some(0), Err(no_blocks_in_state_error())),
(Some(-3), Some(0), Ok(0)),
(Some(-2), Some(-4), Ok(2)),
(Some(-1), Some(10), Ok(2)),
(Some(-1), Some(i32::MAX), Ok(2)),
(Some(0), None, Ok(2)),
(Some(0), Some(0), Err(no_blocks_in_state_error())),
(Some(0), Some(0), Ok(0)),
(Some(0), Some(-3), Ok(2)),
(Some(0), Some(10), Ok(2)),
(Some(0), Some(i32::MAX), Ok(2)),
(Some(1), None, Ok(4096)),
(Some(1), Some(0), Err(no_blocks_in_state_error())),
(Some(1), Some(0), Ok(0)),
(Some(1), Some(-2), Ok(4096)),
(Some(1), Some(10), Ok(4096)),
(Some(1), Some(i32::MAX), Ok(4096)),
(Some(i32::MAX), None, Ok(2)),
(Some(i32::MAX), Some(0), Err(no_blocks_in_state_error())),
(Some(i32::MAX), Some(0), Ok(0)),
(Some(i32::MAX), Some(-1), Ok(2)),
(Some(i32::MAX), Some(10), Ok(2)),
(Some(i32::MAX), Some(i32::MAX), Ok(2)),
Expand Down

0 comments on commit f9442da

Please sign in to comment.