mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11: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
|
||||
/// 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();
|
||||
txhashset.merkle_proof(commit)
|
||||
}
|
||||
|
|
|
@ -131,6 +131,9 @@ pub enum ErrorKind {
|
|||
/// We cannot process data once the Grin server has been stopped.
|
||||
#[fail(display = "Stopped (Grin Shutting Down)")]
|
||||
Stopped,
|
||||
/// Internal Roaring Bitmap error
|
||||
#[fail(display = "Roaring Bitmap error")]
|
||||
Bitmap,
|
||||
}
|
||||
|
||||
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
|
||||
/// the kernel set.
|
||||
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!(
|
||||
"Kernel root at {} does not match",
|
||||
self.header.height
|
||||
|
|
|
@ -272,9 +272,11 @@ impl TxHashSet {
|
|||
}
|
||||
|
||||
/// build a new merkle proof for the given position.
|
||||
pub fn merkle_proof(&mut self, commit: Commitment) -> Result<MerkleProof, String> {
|
||||
let pos = self.commit_index.get_output_pos(&commit).unwrap();
|
||||
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos).merkle_proof(pos)
|
||||
pub fn merkle_proof(&mut self, commit: Commitment) -> Result<MerkleProof, Error> {
|
||||
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)
|
||||
.map_err(|_| ErrorKind::MerkleProof.into())
|
||||
}
|
||||
|
||||
/// 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)?;
|
||||
}
|
||||
|
||||
let bitmap = bitmap_fast_or(None, &mut block_input_bitmaps).unwrap();
|
||||
Ok(bitmap)
|
||||
bitmap_fast_or(None, &mut block_input_bitmaps).ok_or_else(|| ErrorKind::Bitmap.into())
|
||||
}
|
||||
|
|
|
@ -95,9 +95,9 @@ where
|
|||
|
||||
/// Computes the root of the MMR. Find all the peaks in the current
|
||||
/// 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() {
|
||||
return ZERO_HASH;
|
||||
return Ok(ZERO_HASH);
|
||||
}
|
||||
let mut res = None;
|
||||
for peak in self.peaks().iter().rev() {
|
||||
|
@ -106,7 +106,7 @@ where
|
|||
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.
|
||||
|
|
Loading…
Reference in a new issue