error.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
  19. /// Custom RPC errors available for darkfid.
  20. /// Please sort them sensefully.
  21. pub enum RpcError {
  22. // Transaction-related errors
  23. TxSimulationFail = -32110,
  24. TxGasCalculationFail = -32111,
  25. // State-related errors,
  26. NotSynced = -32120,
  27. UnknownBlockHeight = -32121,
  28. // Parsing errors
  29. ParseError = -32190,
  30. // Contract-related errors
  31. ContractZkasDbNotFound = -32200,
  32. ContractStateNotFound = -32201,
  33. ContractStateKeyNotFound = -32202,
  34. // Miner configuration errors
  35. MinerInvalidWalletConfig = -32301,
  36. MinerInvalidRecipient = -32302,
  37. MinerInvalidRecipientPrefix = -32303,
  38. MinerInvalidSpendHook = -32304,
  39. MinerInvalidUserData = -32305,
  40. // Stratum errors
  41. MinerMissingLogin = -32306,
  42. MinerInvalidLogin = -32307,
  43. MinerMissingPassword = -32308,
  44. MinerInvalidPassword = -32309,
  45. MinerMissingAgent = -32310,
  46. MinerInvalidAgent = -32311,
  47. MinerMissingAlgo = -32312,
  48. MinerInvalidAlgo = -32313,
  49. MinerRandomXNotSupported = -32314,
  50. MinerMissingClientId = -32315,
  51. MinerInvalidClientId = -32316,
  52. MinerUnknownClient = -32317,
  53. MinerMissingJobId = -32318,
  54. MinerInvalidJobId = -32319,
  55. MinerUnknownJob = -32320,
  56. MinerMissingNonce = -32321,
  57. MinerInvalidNonce = -32322,
  58. MinerMissingResult = -32323,
  59. MinerInvalidResult = -32324,
  60. // Merge mining errors
  61. MinerMissingAddress = -32325,
  62. MinerInvalidAddress = -32326,
  63. MinerMissingAuxHash = -32327,
  64. MinerInvalidAuxHash = -32328,
  65. MinerMissingHeight = -32329,
  66. MinerInvalidHeight = -32330,
  67. MinerMissingPrevId = -32331,
  68. MinerInvalidPrevId = -32332,
  69. MinerMissingAuxBlob = -32333,
  70. MinerInvalidAuxBlob = -32334,
  71. MinerMissingBlob = -32335,
  72. MinerInvalidBlob = -32336,
  73. MinerMissingMerkleProof = -32337,
  74. MinerInvalidMerkleProof = -32338,
  75. MinerMissingPath = -32339,
  76. MinerInvalidPath = -32340,
  77. MinerMissingSeedHash = -32341,
  78. MinerInvalidSeedHash = -32342,
  79. MinerMerkleProofConstructionFailed = -32343,
  80. MinerMoneroPowDataConstructionFailed = -32344,
  81. }
  82. fn to_tuple(e: RpcError) -> (i32, String) {
  83. let msg = match e {
  84. // Transaction-related errors
  85. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  86. RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
  87. // State-related errors
  88. RpcError::NotSynced => "Blockchain is not synced",
  89. RpcError::UnknownBlockHeight => "Did not find block height",
  90. // Parsing errors
  91. RpcError::ParseError => "Parse error",
  92. // Contract-related errors
  93. RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
  94. RpcError::ContractStateNotFound => "Records not found for given contract state",
  95. RpcError::ContractStateKeyNotFound => "Value not found for given contract state key",
  96. // Miner configuration errors
  97. RpcError::MinerInvalidWalletConfig => "Request wallet configuration is invalid",
  98. RpcError::MinerInvalidRecipient => "Request recipient wallet address is invalid",
  99. RpcError::MinerInvalidRecipientPrefix => {
  100. "Request recipient wallet address prefix is invalid"
  101. }
  102. RpcError::MinerInvalidSpendHook => "Request spend hook is invalid",
  103. RpcError::MinerInvalidUserData => "Request user data is invalid",
  104. // Stratum errors
  105. RpcError::MinerMissingLogin => "Request is missing the login",
  106. RpcError::MinerInvalidLogin => "Request login is invalid",
  107. RpcError::MinerMissingPassword => "Request is missing the password",
  108. RpcError::MinerInvalidPassword => "Request password is invalid",
  109. RpcError::MinerMissingAgent => "Request is missing the agent",
  110. RpcError::MinerInvalidAgent => "Request agent is invalid",
  111. RpcError::MinerMissingAlgo => "Request is missing the algo",
  112. RpcError::MinerInvalidAlgo => "Request algo is invalid",
  113. RpcError::MinerRandomXNotSupported => "Request doesn't support rx/0",
  114. RpcError::MinerMissingClientId => "Request is missing the client ID",
  115. RpcError::MinerInvalidClientId => "Request client ID is invalid",
  116. RpcError::MinerUnknownClient => "Request client is unknown",
  117. RpcError::MinerMissingJobId => "Request is missing the job ID",
  118. RpcError::MinerInvalidJobId => "Request job ID is invalid",
  119. RpcError::MinerUnknownJob => "Request job is unknown",
  120. RpcError::MinerMissingNonce => "Request is missing the nonce",
  121. RpcError::MinerInvalidNonce => "Request nonce is invalid",
  122. RpcError::MinerMissingResult => "Request is missing the result",
  123. RpcError::MinerInvalidResult => "Request nonce is result",
  124. // Merge mining errors
  125. RpcError::MinerMissingAddress => {
  126. "Request is missing the recipient wallet address configuration"
  127. }
  128. RpcError::MinerInvalidAddress => {
  129. "Request recipient wallet address configuration is invalid"
  130. }
  131. RpcError::MinerMissingAuxHash => "Request is missing the merge mining job (aux_hash)",
  132. RpcError::MinerInvalidAuxHash => "Request merge mining job (aux_hash) is invalid",
  133. RpcError::MinerMissingHeight => "Request is missing the Monero height",
  134. RpcError::MinerInvalidHeight => "Request Monero height is invalid",
  135. RpcError::MinerMissingPrevId => "Request is missing the hash of the previous Monero block",
  136. RpcError::MinerInvalidPrevId => "Request hash of the previous Monero block is invalid",
  137. RpcError::MinerMissingAuxBlob => "Request is missing the merge mining blob",
  138. RpcError::MinerInvalidAuxBlob => "Request merge mining bob is invalid",
  139. RpcError::MinerMissingBlob => "Request is missing the Monero block template",
  140. RpcError::MinerInvalidBlob => "Request Monero block template is invalid",
  141. RpcError::MinerMissingMerkleProof => "Request is missing the Merkle proof",
  142. RpcError::MinerInvalidMerkleProof => "Request Merkle proof is invalid",
  143. RpcError::MinerMissingPath => "Request is missing the Merkle proof path",
  144. RpcError::MinerInvalidPath => "Request Merkle proof path is invalid",
  145. RpcError::MinerMissingSeedHash => "Request is missing the RandomX seed key",
  146. RpcError::MinerInvalidSeedHash => "Request RandomX seed key is invalid",
  147. RpcError::MinerMerkleProofConstructionFailed => {
  148. "failed constructing aux chain Merkle proof"
  149. }
  150. RpcError::MinerMoneroPowDataConstructionFailed => "Failed constructing Monero PoW data",
  151. };
  152. (e as i32, msg.to_string())
  153. }
  154. pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
  155. let (code, default_msg) = to_tuple(e);
  156. if let Some(message) = msg {
  157. return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
  158. }
  159. JsonError::new(ServerError(code), Some(default_msg), id).into()
  160. }