error.rs 13 KB

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