mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Remove some unwrap/expect in chain crate (#2621)
* Return Result instead of calling expect in root(). It would kill peer's thread. Perhaps we should ban this peer as malicious. * Remove some unwraps
This commit is contained in:
parent
2df633b622
commit
e71eca1977
5 changed files with 15 additions and 10 deletions
|
@ -660,7 +660,7 @@ impl Chain {
|
||||||
|
|
||||||
/// Return a merkle proof valid for the current output pmmr state at the
|
/// Return a merkle proof valid for the current output pmmr state at the
|
||||||
/// given pos
|
/// given pos
|
||||||
pub fn get_merkle_proof_for_pos(&self, commit: Commitment) -> Result<MerkleProof, String> {
|
pub fn get_merkle_proof_for_pos(&self, commit: Commitment) -> Result<MerkleProof, Error> {
|
||||||
let mut txhashset = self.txhashset.write();
|
let mut txhashset = self.txhashset.write();
|
||||||
txhashset.merkle_proof(commit)
|
txhashset.merkle_proof(commit)
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,6 +131,9 @@ pub enum ErrorKind {
|
||||||
/// We cannot process data once the Grin server has been stopped.
|
/// We cannot process data once the Grin server has been stopped.
|
||||||
#[fail(display = "Stopped (Grin Shutting Down)")]
|
#[fail(display = "Stopped (Grin Shutting Down)")]
|
||||||
Stopped,
|
Stopped,
|
||||||
|
/// Internal Roaring Bitmap error
|
||||||
|
#[fail(display = "Roaring Bitmap error")]
|
||||||
|
Bitmap,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
|
|
|
@ -68,7 +68,8 @@ impl<'a> RewindableKernelView<'a> {
|
||||||
/// fast sync where a reorg past the horizon could allow a whole rewrite of
|
/// fast sync where a reorg past the horizon could allow a whole rewrite of
|
||||||
/// the kernel set.
|
/// the kernel set.
|
||||||
pub fn validate_root(&self) -> Result<(), Error> {
|
pub fn validate_root(&self) -> Result<(), Error> {
|
||||||
if self.pmmr.root() != self.header.kernel_root {
|
let root = self.pmmr.root().map_err(|_| ErrorKind::InvalidRoot)?;
|
||||||
|
if root != self.header.kernel_root {
|
||||||
return Err(ErrorKind::InvalidTxHashSet(format!(
|
return Err(ErrorKind::InvalidTxHashSet(format!(
|
||||||
"Kernel root at {} does not match",
|
"Kernel root at {} does not match",
|
||||||
self.header.height
|
self.header.height
|
||||||
|
|
|
@ -272,9 +272,11 @@ impl TxHashSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// build a new merkle proof for the given position.
|
/// build a new merkle proof for the given position.
|
||||||
pub fn merkle_proof(&mut self, commit: Commitment) -> Result<MerkleProof, String> {
|
pub fn merkle_proof(&mut self, commit: Commitment) -> Result<MerkleProof, Error> {
|
||||||
let pos = self.commit_index.get_output_pos(&commit).unwrap();
|
let pos = self.commit_index.get_output_pos(&commit)?;
|
||||||
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos).merkle_proof(pos)
|
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos)
|
||||||
|
.merkle_proof(pos)
|
||||||
|
.map_err(|_| ErrorKind::MerkleProof.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compact the MMR data files and flush the rm logs
|
/// Compact the MMR data files and flush the rm logs
|
||||||
|
@ -1590,6 +1592,5 @@ pub fn input_pos_to_rewind(
|
||||||
current = batch.get_previous_header(¤t)?;
|
current = batch.get_previous_header(¤t)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bitmap = bitmap_fast_or(None, &mut block_input_bitmaps).unwrap();
|
bitmap_fast_or(None, &mut block_input_bitmaps).ok_or_else(|| ErrorKind::Bitmap.into())
|
||||||
Ok(bitmap)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,9 +95,9 @@ where
|
||||||
|
|
||||||
/// Computes the root of the MMR. Find all the peaks in the current
|
/// Computes the root of the MMR. Find all the peaks in the current
|
||||||
/// tree and "bags" them to get a single peak.
|
/// tree and "bags" them to get a single peak.
|
||||||
pub fn root(&self) -> Hash {
|
pub fn root(&self) -> Result<Hash, String> {
|
||||||
if self.is_empty() {
|
if self.is_empty() {
|
||||||
return ZERO_HASH;
|
return Ok(ZERO_HASH);
|
||||||
}
|
}
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
for peak in self.peaks().iter().rev() {
|
for peak in self.peaks().iter().rev() {
|
||||||
|
@ -106,7 +106,7 @@ where
|
||||||
Some(rhash) => Some((*peak, rhash).hash_with_index(self.unpruned_size())),
|
Some(rhash) => Some((*peak, rhash).hash_with_index(self.unpruned_size())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.expect("no root, invalid tree")
|
res.ok_or_else(|| "no root, invalid tree".to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a vec of the peaks of this MMR.
|
/// Returns a vec of the peaks of this MMR.
|
||||||
|
|
Loading…
Reference in a new issue