error.rs 13 KB

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