error.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use std::fmt;
  2. use crate::vm::ZKVMError;
  3. pub type Result<T> = std::result::Result<T, Error>;
  4. #[derive(Debug)]
  5. pub enum Error {
  6. Foo,
  7. CommitsDontAdd,
  8. InvalidCredential,
  9. TransactionPedersenCheckFailed,
  10. TokenAlreadySpent,
  11. InputTokenVerifyFailed,
  12. RangeproofPedersenMatchFailed,
  13. ProofsFailed,
  14. MissingProofs,
  15. Io(std::io::Error),
  16. /// VarInt was encoded in a non-minimal way
  17. NonMinimalVarInt,
  18. /// Parsing error
  19. ParseFailed(&'static str),
  20. AsyncChannelError,
  21. MalformedPacket,
  22. AddrParseError,
  23. BadVariableRefType,
  24. BadOperationType,
  25. BadConstraintType,
  26. InvalidParamName,
  27. MissingParams,
  28. VMError(ZKVMError),
  29. BadContract,
  30. Groth16Error(bellman::SynthesisError),
  31. }
  32. impl std::error::Error for Error {}
  33. impl fmt::Display for Error {
  34. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  35. match *self {
  36. Error::Foo => f.write_str("foo"),
  37. Error::CommitsDontAdd => f.write_str("Commits don't add up properly"),
  38. Error::InvalidCredential => f.write_str("Credential is invalid"),
  39. Error::TransactionPedersenCheckFailed => {
  40. f.write_str("Transaction pedersens for input and output don't sum up")
  41. }
  42. Error::TokenAlreadySpent => f.write_str("This input token is already spent"),
  43. Error::InputTokenVerifyFailed => f.write_str("Input token verify of credential failed"),
  44. Error::RangeproofPedersenMatchFailed => {
  45. f.write_str("Rangeproof pedersen check for match failed")
  46. }
  47. Error::ProofsFailed => f.write_str("Proof validation failed"),
  48. Error::MissingProofs => f.write_str("Missing proofs"),
  49. Error::Io(ref err) => fmt::Display::fmt(err, f),
  50. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  51. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  52. Error::AsyncChannelError => f.write_str("async_channel error"),
  53. Error::MalformedPacket => f.write_str("Malformed packet"),
  54. Error::AddrParseError => f.write_str("Unable to parse address"),
  55. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  56. Error::BadOperationType => f.write_str("Bad operation type byte"),
  57. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  58. Error::InvalidParamName => f.write_str("Invalid param name"),
  59. Error::MissingParams => f.write_str("Missing params"),
  60. Error::VMError(_) => f.write_str("VM error"),
  61. Error::BadContract => f.write_str("Contract is poorly defined"),
  62. Error::Groth16Error(ref err) => write!(f, "groth16 error: {}", err),
  63. }
  64. }
  65. }
  66. impl From<std::io::Error> for Error {
  67. fn from(err: std::io::Error) -> Error {
  68. Error::Io(err)
  69. }
  70. }
  71. impl From<ZKVMError> for Error {
  72. fn from(err: ZKVMError) -> Error {
  73. Error::VMError(err)
  74. }
  75. }
  76. impl From<bellman::SynthesisError> for Error {
  77. fn from(err: bellman::SynthesisError) -> Error {
  78. Error::Groth16Error(err)
  79. }
  80. }