error.rs 13 KB

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