error.rs 4.3 KB

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