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

Propagate light node sync errors to RPC caller #1783

Merged
merged 1 commit into from
Aug 17, 2020
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
67 changes: 53 additions & 14 deletions core/src/light_protocol/common/ledger_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ impl LedgerInfo {
.get_data_manager()
.block_by_hash(&hash, false /* update_cache */)
.map(|b| (*b).clone())
// FIXME: what's this internal error?
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!("Block {:?} not found", hash))
.into()
})
}

/// Get header `hash`, if it exists.
Expand All @@ -53,12 +55,15 @@ impl LedgerInfo {
.get_data_manager()
.block_header_by_hash(&hash)
.map(|h| (*h).clone())
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!("Header {:?} not found", hash))
.into()
})
}

/// Get hash of block at `height` on the pivot chain, if it exists.
#[inline]
pub fn pivot_hash_of(&self, height: u64) -> Result<H256, Error> {
fn pivot_hash_of(&self, height: u64) -> Result<H256, Error> {
let epoch = EpochNumber::Number(height);
Ok(self.consensus.get_hash_from_epoch_number(epoch)?)
}
Expand All @@ -81,7 +86,7 @@ impl LedgerInfo {
/// Get the correct deferred state root of the block at `height` on the
/// pivot chain based on local execution information.
#[inline]
pub fn correct_deferred_state_root_hash_of(
fn correct_deferred_state_root_hash_of(
&self, height: u64,
) -> Result<H256, Error> {
let epoch = height.saturating_sub(DEFERRED_STATE_EPOCH_COUNT);
Expand All @@ -92,7 +97,7 @@ impl LedgerInfo {
/// Get the correct deferred receipts root of the block at `height` on the
/// pivot chain based on local execution information.
#[inline]
pub fn correct_deferred_receipts_root_hash_of(
fn correct_deferred_receipts_root_hash_of(
&self, height: u64,
) -> Result<H256, Error> {
let epoch = height.saturating_sub(DEFERRED_STATE_EPOCH_COUNT);
Expand All @@ -102,13 +107,19 @@ impl LedgerInfo {
.get_data_manager()
.get_epoch_execution_commitment(&pivot)
.map(|c| c.receipts_root)
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!(
"Execution commitments for {:?} not found",
pivot
))
.into()
})
}

/// Get the correct deferred logs bloom root of the block at `height` on the
/// pivot chain based on local execution information.
#[inline]
pub fn correct_deferred_logs_root_hash_of(
fn correct_deferred_logs_root_hash_of(
&self, height: u64,
) -> Result<H256, Error> {
let epoch = height.saturating_sub(DEFERRED_STATE_EPOCH_COUNT);
Expand All @@ -118,7 +129,13 @@ impl LedgerInfo {
.get_data_manager()
.get_epoch_execution_commitment(&pivot)
.map(|c| c.logs_bloom_hash)
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!(
"Execution commitments for {:?} not found",
pivot
))
.into()
})
}

/// Get the number of epochs per snapshot period.
Expand All @@ -129,7 +146,7 @@ impl LedgerInfo {

/// Get the state trie corresponding to the execution of `epoch`.
#[inline]
pub fn state_of(&self, epoch: u64) -> Result<State, Error> {
fn state_of(&self, epoch: u64) -> Result<State, Error> {
let pivot = self.pivot_hash_of(epoch)?;

let maybe_state_index = self
Expand All @@ -145,7 +162,12 @@ impl LedgerInfo {

match state {
Some(Ok(Some(state))) => Ok(state),
_ => Err(ErrorKind::InternalError.into()),
_ => {
bail!(ErrorKind::InternalError(format!(
"State of epoch {} not found",
epoch
)));
}
}
}

Expand All @@ -156,7 +178,12 @@ impl LedgerInfo {
) -> Result<StateRootWithAuxInfo, Error> {
match self.state_of(epoch)?.get_state_root() {
Ok(root) => Ok(root),
_ => Err(ErrorKind::InternalError.into()),
Err(e) => {
bail!(ErrorKind::InternalError(format!(
"State root of epoch {} not found: {:?}",
epoch, e
)));
}
}
}

Expand Down Expand Up @@ -207,7 +234,13 @@ impl LedgerInfo {
false, /* update_cache */
)
.map(|res| (*res.block_receipts).clone())
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!(
"Receipts of epoch {} not found",
epoch
))
.into()
})
})
.collect()
}
Expand All @@ -232,7 +265,13 @@ impl LedgerInfo {
false, /* update_cache */
)
.map(|res| res.bloom)
.ok_or(ErrorKind::InternalError.into())
.ok_or_else(|| {
ErrorKind::InternalError(format!(
"Logs bloom of epoch {} not found",
epoch
))
.into()
})
})
.collect::<Result<Vec<Bloom>, Error>>()?;

Expand Down
Loading