Thiserror changeover (#3728)
* WIP remove failure from all `Cargo.toml` * WIP remove `extern crate failure_derive` * Use `thiserror` to fix all errors * StoreErr is still a tuple * Remove another set of unnecessary `.into()`s * update fuzz tests * update pool/fuzz dependencies in cargo.lock * small changes based on feedback Co-authored-by: trevyn <trevyn-git@protonmail.com>
This commit is contained in:
+2
-2
@@ -36,10 +36,10 @@ pub use self::pmmr::segment::*;
|
||||
pub use self::transaction::*;
|
||||
|
||||
/// Common errors
|
||||
#[derive(Fail, Debug)]
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
/// Human readable represenation of amount is invalid
|
||||
#[fail(display = "Amount string was invalid")]
|
||||
#[error("Amount string was invalid")]
|
||||
InvalidAmountString,
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ use util::from_hex;
|
||||
use util::{secp, static_secp_instance};
|
||||
|
||||
/// Errors thrown by Block validation
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Fail)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// The sum of output minus input commitments does not
|
||||
/// match the sum of kernel commitments
|
||||
|
||||
@@ -14,26 +14,25 @@
|
||||
|
||||
//! The Committed trait and associated errors.
|
||||
|
||||
use failure::Fail;
|
||||
use keychain::BlindingFactor;
|
||||
use util::secp::key::SecretKey;
|
||||
use util::secp::pedersen::Commitment;
|
||||
use util::{secp, secp_static, static_secp_instance};
|
||||
|
||||
/// Errors from summing and verifying kernel excesses via committed trait.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Fail, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Serialize, Deserialize)]
|
||||
pub enum Error {
|
||||
/// Keychain related error.
|
||||
#[fail(display = "Keychain error {}", _0)]
|
||||
#[error("Keychain error {0}")]
|
||||
Keychain(keychain::Error),
|
||||
/// Secp related error.
|
||||
#[fail(display = "Secp error {}", _0)]
|
||||
#[error("Secp error {0}")]
|
||||
Secp(secp::Error),
|
||||
/// Kernel sums do not equal output sums.
|
||||
#[fail(display = "Kernel sum mismatch")]
|
||||
#[error("Kernel sum mismatch")]
|
||||
KernelSumMismatch,
|
||||
/// Committed overage (fee or reward) is invalid
|
||||
#[fail(display = "Invalid value")]
|
||||
#[error("Invalid value")]
|
||||
InvalidValue,
|
||||
}
|
||||
|
||||
|
||||
@@ -19,32 +19,25 @@ use crate::core::pmmr::{self, Backend, ReadablePMMR, ReadonlyPMMR};
|
||||
use crate::ser::{Error, PMMRIndexHashable, PMMRable, Readable, Reader, Writeable, Writer};
|
||||
use croaring::Bitmap;
|
||||
use std::cmp::min;
|
||||
use std::fmt::{self, Debug};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
/// Error related to segment creation or validation
|
||||
pub enum SegmentError {
|
||||
/// An expected leaf was missing
|
||||
#[error("Missing leaf at pos {0}")]
|
||||
MissingLeaf(u64),
|
||||
/// An expected hash was missing
|
||||
#[error("Missing hash at pos {0}")]
|
||||
MissingHash(u64),
|
||||
/// The segment does not exist
|
||||
#[error("Segment does not exist")]
|
||||
NonExistent,
|
||||
/// Mismatch between expected and actual root hash
|
||||
#[error("Root hash mismatch")]
|
||||
Mismatch,
|
||||
}
|
||||
|
||||
impl fmt::Display for SegmentError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
SegmentError::MissingLeaf(idx) => write!(f, "Missing leaf at pos {}", idx),
|
||||
SegmentError::MissingHash(idx) => write!(f, "Missing hash at pos {}", idx),
|
||||
SegmentError::NonExistent => write!(f, "Segment does not exist"),
|
||||
SegmentError::Mismatch => write!(f, "Root hash mismatch"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tuple that defines a segment of a given PMMR
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub struct SegmentIdentifier {
|
||||
|
||||
@@ -30,8 +30,6 @@ extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
#[macro_use]
|
||||
extern crate failure_derive;
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
|
||||
pub mod consensus;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
//! This module interfaces into the underlying
|
||||
//! [Rust Aggsig library](https://github.com/mimblewimble/rust-secp256k1-zkp/blob/master/src/aggsig.rs)
|
||||
|
||||
use crate::libtx::error::{Error, ErrorKind};
|
||||
use crate::libtx::error::Error;
|
||||
use keychain::{BlindingFactor, Identifier, Keychain, SwitchCommitmentType};
|
||||
use util::secp::key::{PublicKey, SecretKey};
|
||||
use util::secp::pedersen::Commitment;
|
||||
@@ -192,7 +192,7 @@ pub fn verify_partial_sig(
|
||||
pubkey_sum,
|
||||
true,
|
||||
) {
|
||||
return Err(ErrorKind::Signature("Signature validation error".to_string()).into());
|
||||
return Err(Error::Signature("Signature validation error".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -324,7 +324,7 @@ pub fn verify_single_from_commit(
|
||||
) -> Result<(), Error> {
|
||||
let pubkey = commit.to_pubkey(secp)?;
|
||||
if !verify_single(secp, sig, msg, None, &pubkey, Some(&pubkey), false) {
|
||||
return Err(ErrorKind::Signature("Signature validation error".to_string()).into());
|
||||
return Err(Error::Signature("Signature validation error".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -392,7 +392,7 @@ pub fn verify_completed_sig(
|
||||
msg: &secp::Message,
|
||||
) -> Result<(), Error> {
|
||||
if !verify_single(secp, sig, msg, None, pubkey, pubkey_sum, true) {
|
||||
return Err(ErrorKind::Signature("Signature validation error".to_string()).into());
|
||||
return Err(Error::Signature("Signature validation error".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+23
-79
@@ -14,96 +14,40 @@
|
||||
|
||||
//! libtx specific errors
|
||||
use crate::core::transaction;
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use std::fmt::{self, Display};
|
||||
use util::secp;
|
||||
|
||||
/// Lib tx error definition
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
inner: Context<ErrorKind>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Fail, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq, Serialize, Deserialize)]
|
||||
/// Libwallet error types
|
||||
pub enum ErrorKind {
|
||||
pub enum Error {
|
||||
/// SECP error
|
||||
#[fail(display = "Secp Error")]
|
||||
Secp(secp::Error),
|
||||
#[error("Secp Error")]
|
||||
Secp {
|
||||
/// SECP error
|
||||
#[from]
|
||||
source: secp::Error,
|
||||
},
|
||||
/// Keychain error
|
||||
#[fail(display = "Keychain Error")]
|
||||
Keychain(keychain::Error),
|
||||
#[error("Keychain Error")]
|
||||
Keychain {
|
||||
/// Keychain error
|
||||
#[from]
|
||||
source: keychain::Error,
|
||||
},
|
||||
/// Transaction error
|
||||
#[fail(display = "Transaction Error")]
|
||||
Transaction(transaction::Error),
|
||||
#[error("Transaction Error")]
|
||||
Transaction {
|
||||
/// Transaction error
|
||||
#[from]
|
||||
source: transaction::Error,
|
||||
},
|
||||
/// Signature error
|
||||
#[fail(display = "Signature Error")]
|
||||
#[error("Signature Error")]
|
||||
Signature(String),
|
||||
/// Rangeproof error
|
||||
#[fail(display = "Rangeproof Error")]
|
||||
#[error("Rangeproof Error")]
|
||||
RangeProof(String),
|
||||
/// Other error
|
||||
#[fail(display = "Other Error")]
|
||||
#[error("Other Error")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl Fail for Error {
|
||||
fn cause(&self) -> Option<&dyn Fail> {
|
||||
self.inner.cause()
|
||||
}
|
||||
|
||||
fn backtrace(&self) -> Option<&Backtrace> {
|
||||
self.inner.backtrace()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
Display::fmt(&self.inner, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Return errorkind
|
||||
pub fn kind(&self) -> ErrorKind {
|
||||
self.inner.get_context().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorKind> for Error {
|
||||
fn from(kind: ErrorKind) -> Error {
|
||||
Error {
|
||||
inner: Context::new(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<ErrorKind>> for Error {
|
||||
fn from(inner: Context<ErrorKind>) -> Error {
|
||||
Error { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<secp::Error> for Error {
|
||||
fn from(error: secp::Error) -> Error {
|
||||
Error {
|
||||
inner: Context::new(ErrorKind::Secp(error)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<keychain::Error> for Error {
|
||||
fn from(error: keychain::Error) -> Error {
|
||||
Error {
|
||||
inner: Context::new(ErrorKind::Keychain(error)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<transaction::Error> for Error {
|
||||
fn from(error: transaction::Error) -> Error {
|
||||
Error {
|
||||
inner: Context::new(ErrorKind::Transaction(error)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ use crate::core::Transaction;
|
||||
use crate::global::get_accept_fee_base;
|
||||
|
||||
pub use self::proof::ProofBuilder;
|
||||
pub use crate::libtx::error::{Error, ErrorKind};
|
||||
pub use crate::libtx::error::Error;
|
||||
|
||||
/// Transaction fee calculation given numbers of inputs, outputs, and kernels
|
||||
pub fn tx_fee(input_len: usize, output_len: usize, kernel_len: usize) -> u64 {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
//! Rangeproof library functions
|
||||
|
||||
use crate::libtx::error::{Error, ErrorKind};
|
||||
use crate::libtx::error::Error;
|
||||
use blake2::blake2b::blake2b;
|
||||
use keychain::extkey_bip32::BIP32GrinHasher;
|
||||
use keychain::{Identifier, Keychain, SwitchCommitmentType, ViewKey};
|
||||
@@ -81,7 +81,7 @@ where
|
||||
{
|
||||
let nonce = b
|
||||
.rewind_nonce(secp, &commit)
|
||||
.map_err(|e| ErrorKind::RangeProof(e.to_string()))?;
|
||||
.map_err(|e| Error::RangeProof(e.to_string()))?;
|
||||
let info = secp.rewind_bullet_proof(commit, nonce, extra_data, proof);
|
||||
if info.is_err() {
|
||||
return Ok(None);
|
||||
@@ -91,7 +91,7 @@ where
|
||||
let amount = info.value;
|
||||
let check = b
|
||||
.check_output(secp, &commit, amount, info.message)
|
||||
.map_err(|e| ErrorKind::RangeProof(e.to_string()))?;
|
||||
.map_err(|e| Error::RangeProof(e.to_string()))?;
|
||||
|
||||
Ok(check.map(|(id, switch)| (amount, id, switch)))
|
||||
}
|
||||
@@ -164,7 +164,7 @@ where
|
||||
};
|
||||
let res = blake2b(32, &commit.0, hash);
|
||||
SecretKey::from_slice(self.keychain.secp(), res.as_bytes())
|
||||
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
|
||||
.map_err(|e| Error::RangeProof(format!("Unable to create nonce: {:?}", e)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ where
|
||||
fn nonce(&self, commit: &Commitment) -> Result<SecretKey, Error> {
|
||||
let res = blake2b(32, &commit.0, &self.root_hash);
|
||||
SecretKey::from_slice(self.keychain.secp(), res.as_bytes())
|
||||
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
|
||||
.map_err(|e| Error::RangeProof(format!("Unable to create nonce: {:?}", e)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,7 +360,7 @@ impl ProofBuild for ViewKey {
|
||||
fn rewind_nonce(&self, secp: &Secp256k1, commit: &Commitment) -> Result<SecretKey, Error> {
|
||||
let res = blake2b(32, &commit.0, &self.rewind_hash);
|
||||
SecretKey::from_slice(secp, res.as_bytes())
|
||||
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
|
||||
.map_err(|e| Error::RangeProof(format!("Unable to create nonce: {:?}", e)))
|
||||
}
|
||||
|
||||
fn private_nonce(&self, _secp: &Secp256k1, _commit: &Commitment) -> Result<SecretKey, Error> {
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
use crate::global;
|
||||
use crate::pow::common::CuckooParams;
|
||||
use crate::pow::error::{Error, ErrorKind};
|
||||
use crate::pow::error::Error;
|
||||
use crate::pow::siphash::siphash_block;
|
||||
use crate::pow::{PoWContext, Proof};
|
||||
|
||||
@@ -39,7 +39,7 @@ pub fn new_cuckaroo_ctx(edge_bits: u8, proof_size: usize) -> Result<Box<dyn PoWC
|
||||
|
||||
/// Error returned for cuckaroo request beyond HardFork4
|
||||
pub fn no_cuckaroo_ctx() -> Result<Box<dyn PoWContext>, Error> {
|
||||
Err(ErrorKind::Verification("no cuckaroo past HardFork4".to_owned()).into())
|
||||
Err(Error::Verification("no cuckaroo past HardFork4".to_owned()))
|
||||
}
|
||||
|
||||
/// Cuckaroo cycle context. Only includes the verifier for now.
|
||||
@@ -64,7 +64,7 @@ impl PoWContext for CuckarooContext {
|
||||
fn verify(&self, proof: &Proof) -> Result<(), Error> {
|
||||
let size = proof.proof_size();
|
||||
if size != global::proofsize() {
|
||||
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
|
||||
return Err(Error::Verification("wrong cycle length".to_owned()).into());
|
||||
}
|
||||
let nonces = &proof.nonces;
|
||||
let mut uvs = vec![0u64; 2 * size];
|
||||
@@ -78,10 +78,10 @@ impl PoWContext for CuckarooContext {
|
||||
|
||||
for n in 0..size {
|
||||
if nonces[n] > self.params.edge_mask {
|
||||
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
|
||||
return Err(Error::Verification("edge too big".to_owned()));
|
||||
}
|
||||
if n > 0 && nonces[n] <= nonces[n - 1] {
|
||||
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
|
||||
return Err(Error::Verification("edges not ascending".to_owned()));
|
||||
}
|
||||
// 21 is standard siphash rotation constant
|
||||
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, false);
|
||||
@@ -102,7 +102,7 @@ impl PoWContext for CuckarooContext {
|
||||
xor1 ^= v;
|
||||
}
|
||||
if xor0 | xor1 != 0 {
|
||||
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
|
||||
return Err(Error::Verification("endpoints don't match up".to_owned()));
|
||||
}
|
||||
// make prev lists circular
|
||||
for n in 0..size {
|
||||
@@ -130,13 +130,13 @@ impl PoWContext for CuckarooContext {
|
||||
if uvs[k] == uvs[i] {
|
||||
// find other edge endpoint matching one at i
|
||||
if j != i {
|
||||
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
|
||||
return Err(Error::Verification("branch in cycle".to_owned()));
|
||||
}
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
if j == i {
|
||||
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
|
||||
return Err(Error::Verification("cycle dead ends".to_owned()));
|
||||
}
|
||||
i = j ^ 1;
|
||||
n += 1;
|
||||
@@ -147,7 +147,7 @@ impl PoWContext for CuckarooContext {
|
||||
if n == size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
|
||||
Err(Error::Verification("cycle too short".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
use crate::global;
|
||||
use crate::pow::common::CuckooParams;
|
||||
use crate::pow::error::{Error, ErrorKind};
|
||||
use crate::pow::error::Error;
|
||||
use crate::pow::siphash::siphash_block;
|
||||
use crate::pow::{PoWContext, Proof};
|
||||
|
||||
@@ -58,7 +58,7 @@ impl PoWContext for CuckaroodContext {
|
||||
fn verify(&self, proof: &Proof) -> Result<(), Error> {
|
||||
let size = proof.proof_size();
|
||||
if size != global::proofsize() {
|
||||
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
|
||||
return Err(Error::Verification("wrong cycle length".to_owned()));
|
||||
}
|
||||
let nonces = &proof.nonces;
|
||||
let mut uvs = vec![0u64; 2 * size];
|
||||
@@ -74,13 +74,13 @@ impl PoWContext for CuckaroodContext {
|
||||
for n in 0..size {
|
||||
let dir = (nonces[n] & 1) as usize;
|
||||
if ndir[dir] >= size / 2 {
|
||||
return Err(ErrorKind::Verification("edges not balanced".to_owned()).into());
|
||||
return Err(Error::Verification("edges not balanced".to_owned()));
|
||||
}
|
||||
if nonces[n] > self.params.edge_mask {
|
||||
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
|
||||
return Err(Error::Verification("edge too big".to_owned()));
|
||||
}
|
||||
if n > 0 && nonces[n] <= nonces[n - 1] {
|
||||
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
|
||||
return Err(Error::Verification("edges not ascending".to_owned()));
|
||||
}
|
||||
// cuckarood uses a non-standard siphash rotation constant 25 as anti-ASIC tweak
|
||||
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 25, false);
|
||||
@@ -103,7 +103,7 @@ impl PoWContext for CuckaroodContext {
|
||||
ndir[dir] += 1;
|
||||
}
|
||||
if xor0 | xor1 != 0 {
|
||||
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
|
||||
return Err(Error::Verification("endpoints don't match up".to_owned()));
|
||||
}
|
||||
let mut n = 0;
|
||||
let mut i = 0;
|
||||
@@ -120,14 +120,14 @@ impl PoWContext for CuckaroodContext {
|
||||
if uvs[k] == uvs[i] {
|
||||
// find reverse edge endpoint identical to one at i
|
||||
if j != i {
|
||||
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
|
||||
return Err(Error::Verification("branch in cycle".to_owned()));
|
||||
}
|
||||
j = k;
|
||||
}
|
||||
k = prev[k];
|
||||
}
|
||||
if j == i {
|
||||
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
|
||||
return Err(Error::Verification("cycle dead ends".to_owned()));
|
||||
}
|
||||
i = j ^ 1;
|
||||
n += 1;
|
||||
@@ -138,7 +138,7 @@ impl PoWContext for CuckaroodContext {
|
||||
if n == size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
|
||||
Err(Error::Verification("cycle too short".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
use crate::global;
|
||||
use crate::pow::common::CuckooParams;
|
||||
use crate::pow::error::{Error, ErrorKind};
|
||||
use crate::pow::error::Error;
|
||||
use crate::pow::siphash::siphash_block;
|
||||
use crate::pow::{PoWContext, Proof};
|
||||
|
||||
@@ -57,7 +57,7 @@ impl PoWContext for CuckaroomContext {
|
||||
fn verify(&self, proof: &Proof) -> Result<(), Error> {
|
||||
let size = proof.proof_size();
|
||||
if size != global::proofsize() {
|
||||
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
|
||||
return Err(Error::Verification("wrong cycle length".to_owned()));
|
||||
}
|
||||
let nonces = &proof.nonces;
|
||||
let mut from = vec![0u64; size];
|
||||
@@ -71,10 +71,10 @@ impl PoWContext for CuckaroomContext {
|
||||
|
||||
for n in 0..size {
|
||||
if nonces[n] > self.params.edge_mask {
|
||||
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
|
||||
return Err(Error::Verification("edge too big".to_owned()));
|
||||
}
|
||||
if n > 0 && nonces[n] <= nonces[n - 1] {
|
||||
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
|
||||
return Err(Error::Verification("edges not ascending".to_owned()));
|
||||
}
|
||||
// 21 is standard siphash rotation constant
|
||||
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, true);
|
||||
@@ -89,7 +89,7 @@ impl PoWContext for CuckaroomContext {
|
||||
xor_to ^= to[n];
|
||||
}
|
||||
if xor_from != xor_to {
|
||||
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
|
||||
return Err(Error::Verification("endpoints don't match up".to_owned()));
|
||||
}
|
||||
let mut visited = vec![false; size];
|
||||
let mut n = 0;
|
||||
@@ -97,13 +97,13 @@ impl PoWContext for CuckaroomContext {
|
||||
loop {
|
||||
// follow cycle
|
||||
if visited[i] {
|
||||
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
|
||||
return Err(Error::Verification("branch in cycle".to_owned()));
|
||||
}
|
||||
visited[i] = true;
|
||||
let mut k = head[(to[i] & mask) as usize];
|
||||
loop {
|
||||
if k == size {
|
||||
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
|
||||
return Err(Error::Verification("cycle dead ends".to_owned()));
|
||||
}
|
||||
if from[k] == to[i] {
|
||||
break;
|
||||
@@ -120,7 +120,7 @@ impl PoWContext for CuckaroomContext {
|
||||
if n == size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
|
||||
Err(Error::Verification("cycle too short".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
use crate::global;
|
||||
use crate::pow::common::CuckooParams;
|
||||
use crate::pow::error::{Error, ErrorKind};
|
||||
use crate::pow::error::Error;
|
||||
use crate::pow::siphash::siphash_block;
|
||||
use crate::pow::{PoWContext, Proof};
|
||||
|
||||
@@ -58,7 +58,7 @@ impl PoWContext for CuckaroozContext {
|
||||
fn verify(&self, proof: &Proof) -> Result<(), Error> {
|
||||
let size = proof.proof_size();
|
||||
if size != global::proofsize() {
|
||||
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
|
||||
return Err(Error::Verification("wrong cycle length".to_owned()));
|
||||
}
|
||||
let nonces = &proof.nonces;
|
||||
let mut uvs = vec![0u64; 2 * size];
|
||||
@@ -70,10 +70,10 @@ impl PoWContext for CuckaroozContext {
|
||||
|
||||
for n in 0..size {
|
||||
if nonces[n] > self.params.edge_mask {
|
||||
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
|
||||
return Err(Error::Verification("edge too big".to_owned()));
|
||||
}
|
||||
if n > 0 && nonces[n] <= nonces[n - 1] {
|
||||
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
|
||||
return Err(Error::Verification("edges not ascending".to_owned()));
|
||||
}
|
||||
// 21 is standard siphash rotation constant
|
||||
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, true);
|
||||
@@ -93,7 +93,7 @@ impl PoWContext for CuckaroozContext {
|
||||
xoruv ^= uvs[2 * n] ^ uvs[2 * n + 1];
|
||||
}
|
||||
if xoruv != 0 {
|
||||
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
|
||||
return Err(Error::Verification("endpoints don't match up".to_owned()));
|
||||
}
|
||||
// make prev lists circular
|
||||
for n in 0..(2 * size) {
|
||||
@@ -117,13 +117,13 @@ impl PoWContext for CuckaroozContext {
|
||||
if uvs[k] == uvs[i] {
|
||||
// find other edge endpoint matching one at i
|
||||
if j != i {
|
||||
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
|
||||
return Err(Error::Verification("branch in cycle".to_owned()));
|
||||
}
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
if j == i {
|
||||
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
|
||||
return Err(Error::Verification("cycle dead ends".to_owned()));
|
||||
}
|
||||
i = j ^ 1;
|
||||
n += 1;
|
||||
@@ -134,7 +134,7 @@ impl PoWContext for CuckaroozContext {
|
||||
if n == self.params.proof_size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
|
||||
Err(Error::Verification("cycle too short".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-12
@@ -14,7 +14,7 @@
|
||||
//! Implementation of Cuckatoo Cycle designed by John Tromp.
|
||||
use crate::global;
|
||||
use crate::pow::common::{CuckooParams, Link};
|
||||
use crate::pow::error::{Error, ErrorKind};
|
||||
use crate::pow::error::Error;
|
||||
use crate::pow::{PoWContext, Proof};
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use croaring::Bitmap;
|
||||
@@ -46,7 +46,7 @@ impl Graph {
|
||||
/// Create a new graph with given parameters
|
||||
pub fn new(max_edges: u64, max_sols: u32, proof_size: usize) -> Result<Graph, Error> {
|
||||
if max_edges >= u64::max_value() / 2 {
|
||||
return Err(ErrorKind::Verification("graph is to big to build".to_string()).into());
|
||||
return Err(Error::Verification("graph is to big to build".to_string()));
|
||||
}
|
||||
let max_nodes = 2 * max_edges;
|
||||
Ok(Graph {
|
||||
@@ -79,7 +79,7 @@ impl Graph {
|
||||
/// Add an edge to the graph
|
||||
pub fn add_edge(&mut self, u: u64, mut v: u64) -> Result<(), Error> {
|
||||
if u >= self.max_nodes || v >= self.max_nodes {
|
||||
return Err(ErrorKind::EdgeAddition.into());
|
||||
return Err(Error::EdgeAddition);
|
||||
}
|
||||
v = v + self.max_nodes;
|
||||
let adj_u = self.adj_list[(u ^ 1) as usize];
|
||||
@@ -92,7 +92,7 @@ impl Graph {
|
||||
let ulink = self.links.len() as u64;
|
||||
let vlink = (self.links.len() + 1) as u64;
|
||||
if vlink == self.nil {
|
||||
return Err(ErrorKind::EdgeAddition.into());
|
||||
return Err(Error::EdgeAddition);
|
||||
}
|
||||
self.links.push(Link {
|
||||
next: self.adj_list[u as usize],
|
||||
@@ -246,7 +246,7 @@ impl CuckatooContext {
|
||||
self.verify_impl(&s)?;
|
||||
}
|
||||
if self.graph.solutions.is_empty() {
|
||||
Err(ErrorKind::NoSolution.into())
|
||||
Err(Error::NoSolution)
|
||||
} else {
|
||||
Ok(self.graph.solutions.clone())
|
||||
}
|
||||
@@ -257,7 +257,7 @@ impl CuckatooContext {
|
||||
pub fn verify_impl(&self, proof: &Proof) -> Result<(), Error> {
|
||||
let size = proof.proof_size();
|
||||
if size != global::proofsize() {
|
||||
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
|
||||
return Err(Error::Verification("wrong cycle length".to_owned()));
|
||||
}
|
||||
let nonces = &proof.nonces;
|
||||
let mut uvs = vec![0u64; 2 * size];
|
||||
@@ -271,10 +271,10 @@ impl CuckatooContext {
|
||||
|
||||
for n in 0..size {
|
||||
if nonces[n] > self.params.edge_mask {
|
||||
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
|
||||
return Err(Error::Verification("edge too big".to_owned()));
|
||||
}
|
||||
if n > 0 && nonces[n] <= nonces[n - 1] {
|
||||
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
|
||||
return Err(Error::Verification("edges not ascending".to_owned()));
|
||||
}
|
||||
let u = self.params.sipnode(nonces[n], 0)?;
|
||||
let v = self.params.sipnode(nonces[n], 1)?;
|
||||
@@ -293,7 +293,7 @@ impl CuckatooContext {
|
||||
xor1 ^= v;
|
||||
}
|
||||
if xor0 | xor1 != 0 {
|
||||
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
|
||||
return Err(Error::Verification("endpoints don't match up".to_owned()));
|
||||
}
|
||||
// make prev lists circular
|
||||
for n in 0..size {
|
||||
@@ -321,13 +321,13 @@ impl CuckatooContext {
|
||||
if uvs[k] >> 1 == uvs[i] >> 1 {
|
||||
// find other edge endpoint matching one at i
|
||||
if j != i {
|
||||
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
|
||||
return Err(Error::Verification("branch in cycle".to_owned()));
|
||||
}
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
if j == i || uvs[j] == uvs[i] {
|
||||
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
|
||||
return Err(Error::Verification("cycle dead ends".to_owned()));
|
||||
}
|
||||
i = j ^ 1;
|
||||
n += 1;
|
||||
@@ -338,7 +338,7 @@ impl CuckatooContext {
|
||||
if n == size {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
|
||||
Err(Error::Verification("cycle too short".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-63
@@ -13,83 +13,34 @@
|
||||
// limitations under the License.
|
||||
|
||||
//! Cuckatoo specific errors
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use std::fmt::{self, Display};
|
||||
use std::io;
|
||||
|
||||
/// Cuckatoo solver or validation error
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
inner: Context<ErrorKind>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Fail, PartialEq)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
/// Libwallet error types
|
||||
pub enum ErrorKind {
|
||||
pub enum Error {
|
||||
/// Verification error
|
||||
#[fail(display = "Verification Error: {}", _0)]
|
||||
#[error("Verification Error: {0}")]
|
||||
Verification(String),
|
||||
/// IO Error
|
||||
#[fail(display = "IO Error")]
|
||||
IOError,
|
||||
#[error("IO Error")]
|
||||
IOError {
|
||||
/// Io Error Convert
|
||||
#[from]
|
||||
source: std::io::Error,
|
||||
},
|
||||
/// Unexpected Edge Error
|
||||
#[fail(display = "Edge Addition Error")]
|
||||
#[error("Edge Addition Error")]
|
||||
EdgeAddition,
|
||||
/// Path Error
|
||||
#[fail(display = "Path Error")]
|
||||
#[error("Path Error")]
|
||||
Path,
|
||||
/// Invalid cycle
|
||||
#[fail(display = "Invalid Cycle length: {}", _0)]
|
||||
#[error("Invalid Cycle length: {0}")]
|
||||
InvalidCycle(usize),
|
||||
/// No Cycle
|
||||
#[fail(display = "No Cycle")]
|
||||
#[error("No Cycle")]
|
||||
NoCycle,
|
||||
/// No Solution
|
||||
#[fail(display = "No Solution")]
|
||||
#[error("No Solution")]
|
||||
NoSolution,
|
||||
}
|
||||
|
||||
impl Fail for Error {
|
||||
fn cause(&self) -> Option<&dyn Fail> {
|
||||
self.inner.cause()
|
||||
}
|
||||
|
||||
fn backtrace(&self) -> Option<&Backtrace> {
|
||||
self.inner.backtrace()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
Display::fmt(&self.inner, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Return errorkind
|
||||
pub fn kind(&self) -> ErrorKind {
|
||||
self.inner.get_context().clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorKind> for Error {
|
||||
fn from(kind: ErrorKind) -> Error {
|
||||
Error {
|
||||
inner: Context::new(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<ErrorKind>> for Error {
|
||||
fn from(inner: Context<ErrorKind>) -> Error {
|
||||
Error { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(_error: io::Error) -> Error {
|
||||
Error {
|
||||
inner: Context::new(ErrorKind::IOError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user