error.rs 4.2 KB

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