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
18 changes: 5 additions & 13 deletions src/beacon/drand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl BeaconSchedule {
epoch: ChainEpoch,
parent_epoch: ChainEpoch,
prev: &BeaconEntry,
) -> Result<Vec<BeaconEntry>, anyhow::Error> {
) -> anyhow::Result<Vec<BeaconEntry>> {
let (cb_epoch, curr_beacon) = self.beacon_for_epoch(epoch)?;
// Before quicknet upgrade, we had "chained" beacons, and so required two entries at a fork
if curr_beacon.network().is_chained() {
Expand Down Expand Up @@ -154,11 +154,7 @@ impl Beacon for BeaconImpl {
}

/// Verify beacon entries that are sorted by round.
fn verify_entries(
&self,
entries: &[BeaconEntry],
prev: &BeaconEntry,
) -> Result<bool, anyhow::Error> {
fn verify_entries(&self, entries: &[BeaconEntry], prev: &BeaconEntry) -> anyhow::Result<bool> {
delegate_beacon_impl!(self.verify_entries(entries, prev))
}

Expand Down Expand Up @@ -199,11 +195,7 @@ pub trait Beacon {
fn network(&self) -> DrandNetwork;

/// Verify beacon entries that are sorted by round.
fn verify_entries(
&self,
entries: &[BeaconEntry],
prev: &BeaconEntry,
) -> Result<bool, anyhow::Error>;
fn verify_entries(&self, entries: &[BeaconEntry], prev: &BeaconEntry) -> anyhow::Result<bool>;

/// Returns a `BeaconEntry` given a round. It fetches the `BeaconEntry` from a `Drand` node over [`gRPC`](https://grpc.io/)
/// In the future, we will cache values, and support streaming.
Expand Down Expand Up @@ -295,7 +287,7 @@ impl Beacon for DrandBeacon {
&self,
entries: &'a [BeaconEntry],
prev: &'a BeaconEntry,
) -> Result<bool, anyhow::Error> {
) -> anyhow::Result<bool> {
let mut validated = vec![];
let is_valid = if self.network.is_unchained() {
let mut messages = vec![];
Expand Down Expand Up @@ -404,7 +396,7 @@ impl Beacon for DrandBeacon {
.retry(ExponentialBuilder::default())
.notify(|err, dur| {
debug!(
"retrying fetch_entry {err} after {}",
"retrying fetch_entry after {}: {err:#}",
humantime::format_duration(dur)
);
})
Expand Down
4 changes: 2 additions & 2 deletions src/beacon/mock_beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Beacon for MockBeacon {
&self,
entries: &'a [BeaconEntry],
mut prev: &'a BeaconEntry,
) -> Result<bool, anyhow::Error> {
) -> anyhow::Result<bool> {
for curr in entries.iter() {
let oe = Self::entry_for_index(prev.round());
if oe.signature() != curr.signature() {
Expand All @@ -41,7 +41,7 @@ impl Beacon for MockBeacon {
Ok(true)
}

async fn entry(&self, round: u64) -> Result<BeaconEntry, anyhow::Error> {
async fn entry(&self, round: u64) -> anyhow::Result<BeaconEntry> {
Ok(Self::entry_for_index(round))
}

Expand Down
53 changes: 31 additions & 22 deletions src/blocks/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ impl RawBlockHeader {
let signature = self
.signature
.as_ref()
.ok_or_else(|| Error::InvalidSignature("Signature is nil in header".to_owned()))?;
.ok_or_else(|| Error::InvalidSignature("Signature is nil in header".into()))?;

signature
.verify(&self.signing_bytes(), addr)
.map_err(|e| Error::InvalidSignature(format!("Block signature invalid: {e}")))?;
signature.verify(&self.signing_bytes(), addr).map_err(|e| {
Error::InvalidSignature(format!("Block signature invalid: {e:#}").into())
})?;

Ok(())
}
Expand All @@ -105,7 +105,7 @@ impl RawBlockHeader {
) -> Result<(), Error> {
let (cb_epoch, curr_beacon) = b_schedule
.beacon_for_epoch(self.epoch)
.map_err(|e| Error::Validation(e.to_string()))?;
.map_err(|e| Error::Validation(format!("{e:#}").into()))?;
tracing::trace!(
"beacon network at {}: {:?}, is_chained: {}",
self.epoch,
Expand All @@ -116,20 +116,23 @@ impl RawBlockHeader {
if curr_beacon.network().is_chained() {
let (pb_epoch, _) = b_schedule
.beacon_for_epoch(parent_epoch)
.map_err(|e| Error::Validation(e.to_string()))?;
.map_err(|e| Error::Validation(format!("{e:#}").into()))?;
if cb_epoch != pb_epoch {
// Fork logic
if self.beacon_entries.len() != 2 {
return Err(Error::Validation(format!(
"Expected two beacon entries at beacon fork, got {}",
self.beacon_entries.len()
)));
return Err(Error::Validation(
format!(
"Expected two beacon entries at beacon fork, got {}",
self.beacon_entries.len()
)
.into(),
));
}

#[allow(clippy::indexing_slicing)]
curr_beacon
.verify_entries(&self.beacon_entries[1..], &self.beacon_entries[0])
.map_err(|e| Error::Validation(e.to_string()))?;
.map_err(|e| Error::Validation(format!("{e:#}").into()))?;

return Ok(());
}
Expand All @@ -139,10 +142,13 @@ impl RawBlockHeader {
// We don't expect to ever actually meet this condition
if max_round == prev_entry.round() {
if !self.beacon_entries.is_empty() {
return Err(Error::Validation(format!(
"expected not to have any beacon entries in this block, got: {}",
self.beacon_entries.len()
)));
return Err(Error::Validation(
format!(
"expected not to have any beacon entries in this block, got: {}",
self.beacon_entries.len()
)
.into(),
));
}
return Ok(());
}
Expand All @@ -158,22 +164,25 @@ impl RawBlockHeader {
Some(last) => last,
None => {
return Err(Error::Validation(
"Block must include at least 1 beacon entry".to_string(),
"Block must include at least 1 beacon entry".into(),
));
}
};

if last.round() != max_round {
return Err(Error::Validation(format!(
"expected final beacon entry in block to be at round {}, got: {}",
max_round,
last.round()
)));
return Err(Error::Validation(
format!(
"expected final beacon entry in block to be at round {}, got: {}",
max_round,
last.round()
)
.into(),
));
}

if !curr_beacon
.verify_entries(&self.beacon_entries, prev_entry)
.map_err(|e| Error::Validation(e.to_string()))?
.map_err(|e| Error::Validation(format!("{e:#}").into()))?
{
return Err(Error::Validation("beacon entry was invalid".into()));
}
Expand Down
5 changes: 3 additions & 2 deletions src/blocks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::borrow::Cow;
use thiserror::Error;

mod block;
Expand Down Expand Up @@ -29,10 +30,10 @@ pub use vrf_proof::VRFProof;
pub enum Error {
/// Invalid signature
#[error("Invalid signature: {0}")]
InvalidSignature(String),
InvalidSignature(Cow<'static, str>),
/// Error in validating arbitrary data
#[error("Error validating data: {0}")]
Validation(String),
Validation(Cow<'static, str>),
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/chain/store/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ where
Err(e) => {
// Do not warn when the old head is genesis
if old_head.epoch() > 0 {
error!("failed to get chain path changes: {e}");
error!("failed to get chain path changes: {e:#}");
}
// Fallback to single apply
PathChanges {
Expand Down
2 changes: 1 addition & 1 deletion src/chain/store/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl From<String> for Error {

impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
Error::Other(e.to_string())
Error::Other(format!("{e:#}"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/chain_sync/chain_follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ impl SyncTask {
{
Ok(parents) => Some(SyncEvent::NewFullTipsets(parents)),
Err(e) => {
tracing::warn!(%key, %epoch, "failed to fetch tipset: {e}");
tracing::warn!(%key, %epoch, "failed to fetch tipset: {e:#}");
None
}
}
Expand Down
Loading
Loading