error.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. ParseIntError,
  21. AsyncChannelError,
  22. MalformedPacket,
  23. AddrParseError,
  24. BadVariableRefType,
  25. BadOperationType,
  26. BadConstraintType,
  27. InvalidParamName,
  28. MissingParams,
  29. VMError(ZKVMError),
  30. BadContract,
  31. Groth16Error(bellman::SynthesisError),
  32. }
  33. impl std::error::Error for Error {}
  34. impl fmt::Display for Error {
  35. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  36. match *self {
  37. Error::Foo => f.write_str("foo"),
  38. Error::CommitsDontAdd => f.write_str("Commits don't add up properly"),
  39. Error::InvalidCredential => f.write_str("Credential is invalid"),
  40. Error::TransactionPedersenCheckFailed => {
  41. f.write_str("Transaction pedersens for input and output don't sum up")
  42. }
  43. Error::TokenAlreadySpent => f.write_str("This input token is already spent"),
  44. Error::InputTokenVerifyFailed => f.write_str("Input token verify of credential failed"),
  45. Error::RangeproofPedersenMatchFailed => {
  46. f.write_str("Rangeproof pedersen check for match failed")
  47. }
  48. Error::ProofsFailed => f.write_str("Proof validation failed"),
  49. Error::MissingProofs => f.write_str("Missing proofs"),
  50. Error::Io(ref err) => fmt::Display::fmt(err, f),
  51. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  52. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  53. Error::ParseIntError => f.write_str("Parse int error"),
  54. Error::AsyncChannelError => f.write_str("async_channel error"),
  55. Error::MalformedPacket => f.write_str("Malformed packet"),
  56. Error::AddrParseError => f.write_str("Unable to parse address"),
  57. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  58. Error::BadOperationType => f.write_str("Bad operation type byte"),
  59. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  60. Error::InvalidParamName => f.write_str("Invalid param name"),
  61. Error::MissingParams => f.write_str("Missing params"),
  62. Error::VMError(_) => f.write_str("VM error"),
  63. Error::BadContract => f.write_str("Contract is poorly defined"),
  64. Error::Groth16Error(ref err) => write!(f, "groth16 error: {}", err),
  65. }
  66. }
  67. }
  68. impl From<std::io::Error> for Error {
  69. fn from(err: std::io::Error) -> Error {
  70. Error::Io(err)
  71. }
  72. }
  73. impl From<ZKVMError> for Error {
  74. fn from(err: ZKVMError) -> Error {
  75. Error::VMError(err)
  76. }
  77. }
  78. impl From<bellman::SynthesisError> for Error {
  79. fn from(err: bellman::SynthesisError) -> Error {
  80. Error::Groth16Error(err)
  81. }
  82. }
  83. impl<T> From<async_channel::SendError<T>> for Error {
  84. fn from(_err: async_channel::SendError<T>) -> Error {
  85. Error::AsyncChannelError
  86. }
  87. }
  88. impl From<async_channel::RecvError> for Error {
  89. fn from(_err: async_channel::RecvError) -> Error {
  90. Error::AsyncChannelError
  91. }
  92. }
  93. impl From<std::net::AddrParseError> for Error {
  94. fn from(_err: std::net::AddrParseError) -> Error {
  95. Error::AddrParseError
  96. }
  97. }
  98. impl From<std::num::ParseIntError> for Error {
  99. fn from(_err: std::num::ParseIntError) -> Error {
  100. Error::ParseIntError
  101. }
  102. }