Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Apply Clippy lint manual_inspect #5748

Merged
merged 2 commits into from
Jan 29, 2025
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
10 changes: 4 additions & 6 deletions stackslib/src/burnchains/bitcoin/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@ impl SegwitBitcoinAddress {

pub fn from_bech32(s: &str) -> Option<SegwitBitcoinAddress> {
let (hrp, quintets, variant) = bech32::decode(s)
.map_err(|e| {
test_debug!("Failed to decode '{}': {:?}", s, &e);
e
.inspect_err(|_e| {
test_debug!("Failed to decode '{s}': {_e:?}");
})
.ok()?;

Expand All @@ -327,9 +326,8 @@ impl SegwitBitcoinAddress {
prog.append(&mut quintets[1..].to_vec());

let bytes = Vec::from_base32(&prog)
.map_err(|e| {
test_debug!("Failed to decode quintets: {:?}", &e);
e
.inspect_err(|_e| {
test_debug!("Failed to decode quintets: {_e:?}");
})
.ok()?;

Expand Down
21 changes: 5 additions & 16 deletions stackslib/src/burnchains/bitcoin/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,15 @@ impl BitcoinTxInputStructured {
Instruction::PushBytes(payload) => payload,
_ => {
// not pushbytes, so this can't be a multisig script
test_debug!(
"Not a multisig script: Instruction {} is not a PushBytes",
i
);
test_debug!("Not a multisig script: Instruction {i} is not a PushBytes");
return None;
}
};

let pubk = BitcoinPublicKey::from_slice(payload)
.map_err(|e| {
.inspect_err(|&e| {
// not a public key
warn!(
"Not a multisig script: pushbytes {} is not a public key ({:?})",
i, e
);
e
warn!("Not a multisig script: pushbytes {i} is not a public key ({e:?})");
})
.ok()?;

Expand Down Expand Up @@ -169,13 +162,9 @@ impl BitcoinTxInputStructured {
for i in 0..pubkey_vecs.len() {
let payload = &pubkey_vecs[i];
let pubk = BitcoinPublicKey::from_slice(&payload[..])
.map_err(|e| {
.inspect_err(|&e| {
// not a public key
warn!(
"Not a multisig script: item {} is not a public key ({:?})",
i, e
);
e
warn!("Not a multisig script: item {i} is not a public key ({e:?})");
})
.ok()?;

Expand Down
14 changes: 4 additions & 10 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,8 @@ impl BitcoinIndexer {
)?;

// what's the last header we have from the canonical history?
let canonical_end_block = orig_spv_client.get_headers_height().map_err(|e| {
error!(
"Failed to get the last block from {}",
canonical_headers_path
);
e
let canonical_end_block = orig_spv_client.get_headers_height().inspect_err(|_e| {
error!("Failed to get the last block from {canonical_headers_path}");
})?;

// bootstrap reorg client
Expand Down Expand Up @@ -694,13 +690,12 @@ impl BitcoinIndexer {

let reorg_headers = reorg_spv_client
.read_block_headers(start_block, start_block + REORG_BATCH_SIZE)
.map_err(|e| {
.inspect_err(|_e| {
error!(
"Failed to read reorg Bitcoin headers from {} to {}",
start_block,
start_block + REORG_BATCH_SIZE
);
e
})?;

if reorg_headers.is_empty() {
Expand All @@ -724,13 +719,12 @@ impl BitcoinIndexer {
// got reorg headers. Find the equivalent headers in our canonical history
let canonical_headers = orig_spv_client
.read_block_headers(start_block, start_block + REORG_BATCH_SIZE)
.map_err(|e| {
.inspect_err(|_e| {
error!(
"Failed to read canonical headers from {} to {}",
start_block,
start_block + REORG_BATCH_SIZE
);
e
})?;

assert!(
Expand Down
70 changes: 20 additions & 50 deletions stackslib/src/burnchains/bitcoin/spv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,33 +832,23 @@ impl SpvClient {
// fetching headers in ascending order, so verify that the first item in
// `block_headers` connects to a parent in the DB (if it has one)
self.insert_block_headers_after(insert_height, block_headers)
.map_err(|e| {
error!("Failed to insert block headers: {:?}", &e);
e
})?;
.inspect_err(|e| error!("Failed to insert block headers: {e:?}"))?;

// check work
let chain_tip = self.get_headers_height()?;
self.validate_header_work(
(insert_height.saturating_sub(1)) / BLOCK_DIFFICULTY_CHUNK_SIZE,
chain_tip / BLOCK_DIFFICULTY_CHUNK_SIZE + 1,
)
.map_err(|e| {
error!(
"Received headers with bad target, difficulty, or continuity: {:?}",
&e
);
e
.inspect_err(|e| {
error!("Received headers with bad target, difficulty, or continuity: {e:?}")
})?;
} else {
// fetching headers in descending order, so verify that the last item in
// `block_headers` connects to a child in the DB (if it has one)
let headers_len = block_headers.len() as u64;
self.insert_block_headers_before(insert_height, block_headers)
.map_err(|e| {
error!("Failed to insert block headers: {:?}", &e);
e
})?;
.inspect_err(|e| error!("Failed to insert block headers: {e:?}"))?;

// check work
let interval_start = if insert_height % BLOCK_DIFFICULTY_CHUNK_SIZE == 0 {
Expand All @@ -870,29 +860,21 @@ impl SpvClient {
let interval_end = (insert_height + 1 + headers_len) / BLOCK_DIFFICULTY_CHUNK_SIZE + 1;

self.validate_header_work(interval_start, interval_end)
.map_err(|e| {
error!(
"Received headers with bad target, difficulty, or continuity: {:?}",
&e
);
e
.inspect_err(|e| {
error!("Received headers with bad target, difficulty, or continuity: {e:?}")
})?;
}

if num_headers > 0 {
let total_work_after = self.update_chain_work()?;
if total_work_after < total_work_before {
error!(
"New headers represent less work than the old headers ({} < {})",
total_work_before, total_work_after
"New headers represent less work than the old headers ({total_work_before} < {total_work_after})"
);
return Err(btc_error::InvalidChainWork);
}

debug!(
"Handled {} Headers: {}-{}",
num_headers, first_header_hash, last_header_hash
);
debug!("Handled {num_headers} Headers: {first_header_hash}-{last_header_hash}");
} else {
debug!("Handled empty header reply");
}
Expand Down Expand Up @@ -956,22 +938,16 @@ impl SpvClient {
);

SpvClient::validate_header_integrity(start_height, &block_headers, self.check_txcount)
.map_err(|e| {
error!("Received invalid headers: {:?}", &e);
e
})?;

let parent_header = match self.read_block_header(start_height)? {
Some(header) => header,
None => {
warn!(
"No header for block {} -- cannot insert {} headers into {}",
start_height,
block_headers.len(),
self.headers_path
);
return Err(btc_error::NoncontiguousHeader);
}
.inspect_err(|e| error!("Received invalid headers: {e:?}"))?;

let Some(parent_header) = self.read_block_header(start_height)? else {
warn!(
"No header for block {} -- cannot insert {} headers into {}",
start_height,
block_headers.len(),
self.headers_path
);
return Err(btc_error::NoncontiguousHeader);
};

// contiguous?
Expand Down Expand Up @@ -1010,10 +986,7 @@ impl SpvClient {
);

SpvClient::validate_header_integrity(start_height, &block_headers, self.check_txcount)
.map_err(|e| {
error!("Received invalid headers: {:?}", &e);
e
})?;
.inspect_err(|e| error!("Received invalid headers: {e:?}"))?;

match self.read_block_header(end_height)? {
Some(child_header) => {
Expand All @@ -1028,10 +1001,7 @@ impl SpvClient {
None => {
// if we're inserting headers in reverse order, we're not guaranteed to have the
// child.
debug!(
"No header for child block {}, so will not validate continuity",
end_height
);
debug!("No header for child block {end_height}, so will not validate continuity");
}
}

Expand Down
33 changes: 13 additions & 20 deletions stackslib/src/burnchains/burnchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,12 @@ impl Burnchain {

if headers_height == 0 || headers_height < self.first_block_height {
debug!("Fetch initial headers");
indexer.sync_headers(headers_height, None).map_err(|e| {
error!("Failed to sync initial headers");
sleep_ms(100);
e
})?;
indexer
.sync_headers(headers_height, None)
.inspect_err(|_e| {
error!("Failed to sync initial headers");
sleep_ms(100);
})?;
}
Ok(())
}
Expand Down Expand Up @@ -1137,13 +1138,9 @@ impl Burnchain {
let headers_path = indexer.get_headers_path();

// sanity check -- what is the height of our highest header
let headers_height = indexer.get_highest_header_height().map_err(|e| {
error!(
"Failed to read headers height from {}: {:?}",
headers_path, &e
);
e
})?;
let headers_height = indexer
.get_highest_header_height()
.inspect_err(|e| error!("Failed to read headers height from {headers_path}: {e:?}"))?;

if headers_height == 0 {
return Ok((0, false));
Expand All @@ -1152,16 +1149,12 @@ impl Burnchain {
// did we encounter a reorg since last sync? Find the highest common ancestor of the
// remote bitcoin peer's chain state.
// Note that this value is 0-indexed -- the smallest possible value it returns is 0.
let reorg_height = indexer.find_chain_reorg().map_err(|e| {
error!("Failed to check for reorgs from {}: {:?}", headers_path, &e);
e
})?;
let reorg_height = indexer
.find_chain_reorg()
.inspect_err(|e| error!("Failed to check for reorgs from {headers_path}: {e:?}"))?;

if reorg_height < headers_height {
warn!(
"Burnchain reorg detected: highest common ancestor at height {}",
reorg_height
);
warn!("Burnchain reorg detected: highest common ancestor at height {reorg_height}");
return Ok((reorg_height, true));
} else {
// no reorg
Expand Down
10 changes: 3 additions & 7 deletions stackslib/src/chainstate/burn/db/sortdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4896,16 +4896,12 @@ impl SortitionDB {
let qry = "SELECT * FROM snapshots WHERE sortition_id = ?1";
let args = [&sortition_id];
query_row_panic(conn, qry, &args, || {
format!(
"FATAL: multiple block snapshots for the same block {}",
sortition_id
)
format!("FATAL: multiple block snapshots for the same block {sortition_id}")
})
.map(|x| {
.inspect(|x| {
if x.is_none() {
test_debug!("No snapshot with sortition ID {}", sortition_id);
test_debug!("No snapshot with sortition ID {sortition_id}");
}
x
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,19 +1131,17 @@ impl LeaderBlockCommitOp {
.is_after_pox_sunset_end(self.block_height, epoch.epoch_id)
{
// sunset has begun and we're not in epoch 2.1 or later, so apply sunset check
self.check_after_pox_sunset().map_err(|e| {
warn!("Invalid block-commit: bad PoX after sunset: {:?}", &e;
self.check_after_pox_sunset().inspect_err(|e| {
warn!("Invalid block-commit: bad PoX after sunset: {e:?}";
"apparent_sender" => %apparent_sender_repr);
e
})?;
vec![]
} else {
// either in epoch 2.1, or the PoX sunset hasn't completed yet
self.check_pox(epoch.epoch_id, burnchain, tx, reward_set_info)
.map_err(|e| {
warn!("Invalid block-commit: bad PoX: {:?}", &e;
.inspect_err(|e| {
warn!("Invalid block-commit: bad PoX: {e:?}";
"apparent_sender" => %apparent_sender_repr);
e
})?
};

Expand Down
Loading
Loading