Skip to content

Commit

Permalink
fix: return Spentbook errors. don't panic
Browse files Browse the repository at this point in the history
* fix: return Error::SpentbookKeyImageAlreadySpent instead of panic
* fix: return Error::SpentbookRingSizeMismatch instead of assert failure
* add Error::SpentbookKeyImageAlreadySpent
* add Error::SpentbookRingSizeMismatch
  • Loading branch information
dan-da committed Feb 23, 2022
1 parent b15e280 commit b280bde
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ pub enum Error {
#[error("Secret key does not match public key")]
SecretKeyDoesNotMatchPublicKey,

// temporary. should be part of (future) spentbook module.
#[error("Key image has already been spent")]
SpentbookKeyImageAlreadySpent,

// temporary. should be part of (future) spentbook module.
#[error("The transaction input has {0:?} public keys but found {1:?} matching outputs in spentbook.")]
SpentbookRingSizeMismatch(usize, usize),

#[cfg_attr(feature = "serde", serde(skip))]
#[error("Bls error: {0}")]
Blsttc(#[from] blsttc::error::Error),
Expand Down
15 changes: 11 additions & 4 deletions src/spentbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,24 @@ impl SpentBookNodeMock {
})
.collect();

if output_proofs.len() != mlsag.public_keys().len() {
return Err(crate::Error::SpentbookRingSizeMismatch(
mlsag.public_keys().len(),
output_proofs.len(),
));
}

// collect commitments from OutputProofs
let commitments: Vec<Commitment> =
output_proofs.iter().map(|o| o.commitment()).collect();

// check our assumptions.
assert_eq!(commitments.len(), mlsag.public_keys().len());
assert!(commitments.len() == mlsag.ring.len());
assert_eq!(commitments.len(), mlsag.ring.len());

(mlsag.key_image.into(), commitments)
Ok((mlsag.key_image.into(), commitments))
})
.collect()
.collect::<Result<_>>()?
};

// Grab all commitments, grouped by input mlsag
Expand Down Expand Up @@ -214,7 +221,7 @@ impl SpentBookNodeMock {
})
} else {
// fixme: return an error. can wait until we refactor into a Mock feature flag.
panic!("Attempt to Double Spend")
Err(crate::Error::SpentbookKeyImageAlreadySpent)
}
}

Expand Down

0 comments on commit b280bde

Please sign in to comment.