error.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // Hello developer. Please add your error to the according subsection
  2. // that is commented, or make a new subsection. Keep it clean.
  3. /// Main result type used throughout the codebase.
  4. pub type Result<T> = std::result::Result<T, Error>;
  5. /// Result type used in transaction verifications
  6. pub type VerifyResult<T> = std::result::Result<T, VerifyFailed>;
  7. /// Result type used in the Client module
  8. pub type ClientResult<T> = std::result::Result<T, ClientFailed>;
  9. /// General library errors used throughout the codebase.
  10. #[derive(Debug, Clone, thiserror::Error)]
  11. pub enum Error {
  12. // ==============
  13. // Parsing errors
  14. // ==============
  15. #[error("Parse failed: {0}")]
  16. ParseFailed(&'static str),
  17. #[error(transparent)]
  18. ParseIntError(#[from] std::num::ParseIntError),
  19. #[error(transparent)]
  20. ParseFloatError(#[from] std::num::ParseFloatError),
  21. #[cfg(feature = "num-bigint")]
  22. #[error(transparent)]
  23. ParseBigIntError(#[from] num_bigint::ParseBigIntError),
  24. #[cfg(feature = "num-bigint")]
  25. #[error(transparent)]
  26. TryFromBigIntError(#[from] num_bigint::TryFromBigIntError<num_bigint::BigUint>),
  27. #[cfg(feature = "url")]
  28. #[error(transparent)]
  29. UrlParseError(#[from] url::ParseError),
  30. #[error("URL parse error: {0}")]
  31. UrlParse(String),
  32. #[error(transparent)]
  33. AddrParseError(#[from] std::net::AddrParseError),
  34. #[error("Could not parse token parameter")]
  35. TokenParseError,
  36. // ===============
  37. // Encoding errors
  38. // ===============
  39. #[error("decode failed: {0}")]
  40. DecodeError(&'static str),
  41. #[error("encode failed: {0}")]
  42. EncodeError(&'static str),
  43. #[error("VarInt was encoded in a non-minimal way")]
  44. NonMinimalVarInt,
  45. #[error(transparent)]
  46. Utf8Error(#[from] std::string::FromUtf8Error),
  47. #[error(transparent)]
  48. StrUtf8Error(#[from] std::str::Utf8Error),
  49. #[cfg(feature = "serde_json")]
  50. #[error("serde_json error: {0}")]
  51. SerdeJsonError(String),
  52. #[cfg(feature = "toml")]
  53. #[error(transparent)]
  54. TomlDeserializeError(#[from] toml::de::Error),
  55. #[cfg(feature = "bincode")]
  56. #[error("bincode error: {0}")]
  57. BincodeError(String),
  58. #[cfg(feature = "bs58")]
  59. #[error(transparent)]
  60. Bs58DecodeError(#[from] bs58::decode::Error),
  61. #[error("Bad operation type byte")]
  62. BadOperationType,
  63. // ======================
  64. // Network-related errors
  65. // ======================
  66. #[error("Unsupported network transport: {0}")]
  67. UnsupportedTransport(String),
  68. #[error("Transport error: {0}")]
  69. TransportError(String),
  70. #[error("Connection failed")]
  71. ConnectFailed,
  72. #[error("Timeout Error")]
  73. TimeoutError,
  74. #[error("Connection timed out")]
  75. ConnectTimeout,
  76. #[error("Channel stopped")]
  77. ChannelStopped,
  78. #[error("Channel timed out")]
  79. ChannelTimeout,
  80. #[error("Service stopped")]
  81. ServiceStopped,
  82. #[error("Operation failed")]
  83. OperationFailed,
  84. #[error("Malformed packet")]
  85. MalformedPacket,
  86. #[error("Socks proxy error: {0}")]
  87. SocksError(String),
  88. #[error("No Socks5 URL found")]
  89. NoSocks5UrlFound,
  90. #[error("No URL found")]
  91. NoUrlFound,
  92. #[cfg(feature = "tungstenite")]
  93. #[error("tungstenite error: {0}")]
  94. TungsteniteError(String),
  95. #[cfg(feature = "async-native-tls")]
  96. #[error("async_native_tls error: {0}")]
  97. AsyncNativeTlsError(String),
  98. // =============
  99. // Crypto errors
  100. // =============
  101. #[cfg(feature = "halo2_proofs")]
  102. #[error("halo2 plonk error: {0}")]
  103. PlonkError(String),
  104. #[error("Unable to decrypt mint note")]
  105. NoteDecryptionFailed,
  106. #[error("No keypair file detected")]
  107. KeypairPathNotFound,
  108. #[error("Failed converting bytes to PublicKey")]
  109. PublicKeyFromBytes,
  110. #[error("Failed converting bytes to SecretKey")]
  111. SecretKeyFromBytes,
  112. #[error("Failed converting b58 string to PublicKey")]
  113. PublicKeyFromStr,
  114. #[error("Failed converting bs58 string to SecretKey")]
  115. SecretKeyFromStr,
  116. #[error("Invalid DarkFi address")]
  117. InvalidAddress,
  118. // =======================
  119. // Protocol-related errors
  120. // =======================
  121. #[error("Unsupported chain")]
  122. UnsupportedChain,
  123. #[error("Unsupported token")]
  124. UnsupportedToken,
  125. #[error("Unsupported coin network")]
  126. UnsupportedCoinNetwork,
  127. #[error("Raft error: {0}")]
  128. RaftError(String),
  129. #[error("JSON-RPC error: {0}")]
  130. JsonRpcError(String),
  131. #[error("Cashier error: {0}")]
  132. CashierError(String),
  133. // ===============
  134. // Database errors
  135. // ===============
  136. #[cfg(feature = "sqlx")]
  137. #[error("Sqlx error: {0}")]
  138. SqlxError(String),
  139. #[cfg(feature = "sled")]
  140. #[error(transparent)]
  141. SledError(#[from] sled::Error),
  142. #[error("Transaction {0} not found in database")]
  143. TransactionNotFound(String),
  144. #[error("Block {0} not found in database")]
  145. BlockNotFound(String),
  146. #[error("Block in slot {0} not found in database")]
  147. SlotNotFound(u64),
  148. #[error("Block {0} metadata not found in database")]
  149. BlockMetadataNotFound(String),
  150. // =============
  151. // Wallet errors
  152. // =============
  153. #[error("Wallet password is empty")]
  154. WalletEmptyPassword,
  155. #[error("Merkle tree already exists in wallet")]
  156. WalletTreeExists,
  157. // ===================
  158. // wasm runtime errors
  159. // ===================
  160. #[cfg(feature = "wasm-runtime")]
  161. #[error("Wasmer compile error: {0}")]
  162. WasmerCompileError(String),
  163. #[cfg(feature = "wasm-runtime")]
  164. #[error("Wasmer export error: {0}")]
  165. WasmerExportError(String),
  166. #[cfg(feature = "wasm-runtime")]
  167. #[error("Wasmer runtime error: {0}")]
  168. WasmerRuntimeError(String),
  169. #[cfg(feature = "wasm-runtime")]
  170. #[error("Wasmer instantiation error: {0}")]
  171. WasmerInstantiationError(String),
  172. #[cfg(feature = "wasm-runtime")]
  173. #[error("wasm runtime out of memory")]
  174. WasmerOomError,
  175. // ====================
  176. // Miscellaneous errors
  177. // ====================
  178. #[error("IO error: {0}")]
  179. Io(std::io::ErrorKind),
  180. #[error("Infallible error: {0}")]
  181. InfallibleError(String),
  182. #[cfg(feature = "async-channel")]
  183. #[error("async_channel sender error: {0}")]
  184. AsyncChannelSendError(String),
  185. #[cfg(feature = "async-channel")]
  186. #[error("async_channel receiver error: {0}")]
  187. AsyncChannelRecvError(String),
  188. #[error("SetLogger (log crate) failed: {0}")]
  189. SetLoggerError(String),
  190. #[error("ValueIsNotObject")]
  191. ValueIsNotObject,
  192. #[error("No config file detected")]
  193. ConfigNotFound,
  194. #[error("Failed decoding bincode: {0}")]
  195. ZkasDecoderError(&'static str),
  196. // ==============================================
  197. // Wrappers for other error types in this library
  198. // ==============================================
  199. #[error(transparent)]
  200. VerifyFailed(#[from] VerifyFailed),
  201. #[error(transparent)]
  202. ClientFailed(#[from] ClientFailed),
  203. }
  204. /// Transaction verification errors
  205. #[derive(Debug, Clone, thiserror::Error)]
  206. pub enum VerifyFailed {
  207. #[error("Invalid cashier/faucet public key for clear input {0}")]
  208. InvalidCashierOrFaucetKey(usize),
  209. #[error("Invalid Merkle root for input {0}")]
  210. InvalidMerkle(usize),
  211. #[error("Nullifier already exists for input {0}")]
  212. NullifierExists(usize),
  213. #[error("Invalid signature for input {0}")]
  214. InputSignature(usize),
  215. #[error("Invalid signature for clear input {0}")]
  216. ClearInputSignature(usize),
  217. #[error("Token commitments in inputs or outputs to not match")]
  218. TokenMismatch,
  219. #[error("Money in does not match money out (value commitments)")]
  220. MissingFunds,
  221. #[error("Mint proof verification failure for input {0}")]
  222. MintProof(usize),
  223. #[error("Burn proof verification failure for input {0}")]
  224. BurnProof(usize),
  225. #[error("Failed verifying zk proofs: {0}")]
  226. ProofVerifyFailed(String),
  227. #[error("Internal error: {0}")]
  228. InternalError(String),
  229. }
  230. /// Client module errors
  231. #[derive(Debug, Clone, thiserror::Error)]
  232. pub enum ClientFailed {
  233. #[error("Not enough value: {0}")]
  234. NotEnoughValue(u64),
  235. #[error("Invalid address: {0}")]
  236. InvalidAddress(String),
  237. #[error("Invalid amount: {0}")]
  238. InvalidAmount(u64),
  239. #[error("Internal error: {0}")]
  240. InternalError(String),
  241. #[error("Verify error: {0}")]
  242. VerifyError(String),
  243. }
  244. impl From<Error> for VerifyFailed {
  245. fn from(err: Error) -> Self {
  246. Self::InternalError(err.to_string())
  247. }
  248. }
  249. impl From<Error> for ClientFailed {
  250. fn from(err: Error) -> Self {
  251. Self::InternalError(err.to_string())
  252. }
  253. }
  254. impl From<VerifyFailed> for ClientFailed {
  255. fn from(err: VerifyFailed) -> Self {
  256. Self::VerifyError(err.to_string())
  257. }
  258. }
  259. // TEMP
  260. #[cfg(feature = "net3")]
  261. impl<T: std::fmt::Display> From<crate::net3::transport::TransportError<T>> for Error {
  262. fn from(err: crate::net3::transport::TransportError<T>) -> Self {
  263. Self::TransportError(err.to_string())
  264. }
  265. }
  266. #[cfg(feature = "async-std")]
  267. impl From<async_std::future::TimeoutError> for Error {
  268. fn from(_err: async_std::future::TimeoutError) -> Self {
  269. Self::TimeoutError
  270. }
  271. }
  272. impl From<std::io::Error> for Error {
  273. fn from(err: std::io::Error) -> Self {
  274. Self::Io(err.kind())
  275. }
  276. }
  277. impl From<std::convert::Infallible> for Error {
  278. fn from(err: std::convert::Infallible) -> Self {
  279. Self::InfallibleError(err.to_string())
  280. }
  281. }
  282. impl From<()> for Error {
  283. fn from(_err: ()) -> Self {
  284. Self::InfallibleError("Infallible".into())
  285. }
  286. }
  287. #[cfg(feature = "async-channel")]
  288. impl<T> From<async_channel::SendError<T>> for Error {
  289. fn from(err: async_channel::SendError<T>) -> Self {
  290. Self::AsyncChannelSendError(err.to_string())
  291. }
  292. }
  293. #[cfg(feature = "async-channel")]
  294. impl From<async_channel::RecvError> for Error {
  295. fn from(err: async_channel::RecvError) -> Self {
  296. Self::AsyncChannelRecvError(err.to_string())
  297. }
  298. }
  299. #[cfg(feature = "async-native-tls")]
  300. impl From<async_native_tls::Error> for Error {
  301. fn from(err: async_native_tls::Error) -> Self {
  302. Self::AsyncNativeTlsError(err.to_string())
  303. }
  304. }
  305. impl From<log::SetLoggerError> for Error {
  306. fn from(err: log::SetLoggerError) -> Self {
  307. Self::SetLoggerError(err.to_string())
  308. }
  309. }
  310. #[cfg(feature = "sqlx")]
  311. impl From<sqlx::error::Error> for Error {
  312. fn from(err: sqlx::error::Error) -> Self {
  313. Self::SqlxError(err.to_string())
  314. }
  315. }
  316. #[cfg(feature = "halo2_proofs")]
  317. impl From<halo2_proofs::plonk::Error> for Error {
  318. fn from(err: halo2_proofs::plonk::Error) -> Self {
  319. Self::PlonkError(err.to_string())
  320. }
  321. }
  322. #[cfg(feature = "tungstenite")]
  323. impl From<tungstenite::Error> for Error {
  324. fn from(err: tungstenite::Error) -> Self {
  325. Self::TungsteniteError(err.to_string())
  326. }
  327. }
  328. #[cfg(feature = "bincode")]
  329. impl From<bincode::ErrorKind> for Error {
  330. fn from(err: bincode::ErrorKind) -> Self {
  331. Self::BincodeError(err.to_string())
  332. }
  333. }
  334. #[cfg(feature = "bincode")]
  335. impl From<Box<bincode::ErrorKind>> for Error {
  336. fn from(err: Box<bincode::ErrorKind>) -> Self {
  337. Self::BincodeError(err.to_string())
  338. }
  339. }
  340. #[cfg(feature = "serde_json")]
  341. impl From<serde_json::Error> for Error {
  342. fn from(err: serde_json::Error) -> Self {
  343. Self::SerdeJsonError(err.to_string())
  344. }
  345. }
  346. #[cfg(feature = "fast-socks5")]
  347. impl From<fast_socks5::SocksError> for Error {
  348. fn from(err: fast_socks5::SocksError) -> Self {
  349. Self::SocksError(err.to_string())
  350. }
  351. }
  352. #[cfg(feature = "wasm-runtime")]
  353. impl From<wasmer::CompileError> for Error {
  354. fn from(err: wasmer::CompileError) -> Self {
  355. Self::WasmerCompileError(err.to_string())
  356. }
  357. }
  358. #[cfg(feature = "wasm-runtime")]
  359. impl From<wasmer::ExportError> for Error {
  360. fn from(err: wasmer::ExportError) -> Self {
  361. Self::WasmerExportError(err.to_string())
  362. }
  363. }
  364. #[cfg(feature = "wasm-runtime")]
  365. impl From<wasmer::RuntimeError> for Error {
  366. fn from(err: wasmer::RuntimeError) -> Self {
  367. Self::WasmerRuntimeError(err.to_string())
  368. }
  369. }
  370. #[cfg(feature = "wasm-runtime")]
  371. impl From<wasmer::InstantiationError> for Error {
  372. fn from(err: wasmer::InstantiationError) -> Self {
  373. Self::WasmerInstantiationError(err.to_string())
  374. }
  375. }