error.rs 13 KB

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