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