error.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 std::collections::HashMap;
  19. use tinyjson::JsonValue;
  20. use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResponse, JsonResult};
  21. /// Custom RPC errors available for darkfid.
  22. /// Please sort them sensefully.
  23. pub enum RpcError {
  24. // Transaction-related errors
  25. TxSimulationFail = -32110,
  26. TxGasCalculationFail = -32111,
  27. // State-related errors,
  28. NotSynced = -32120,
  29. UnknownBlockHeight = -32121,
  30. // Parsing errors
  31. ParseError = -32190,
  32. // Contract-related errors
  33. ContractZkasDbNotFound = -32200,
  34. ContractStateNotFound = -32201,
  35. ContractStateKeyNotFound = -32202,
  36. ContractWasmNotFound = -32203,
  37. // Miner configuration errors
  38. MinerInvalidWalletConfig = -32301,
  39. MinerInvalidRecipient = -32302,
  40. MinerInvalidRecipientPrefix = -32303,
  41. MinerInvalidSpendHook = -32304,
  42. MinerInvalidUserData = -32305,
  43. // Stratum errors
  44. MinerMissingLogin = -32306,
  45. MinerInvalidLogin = -32307,
  46. MinerMissingPassword = -32308,
  47. MinerInvalidPassword = -32309,
  48. MinerMissingAgent = -32310,
  49. MinerInvalidAgent = -32311,
  50. MinerMissingAlgo = -32312,
  51. MinerInvalidAlgo = -32313,
  52. MinerRandomXNotSupported = -32314,
  53. MinerMissingClientId = -32315,
  54. MinerInvalidClientId = -32316,
  55. MinerUnknownClient = -32317,
  56. MinerMissingJobId = -32318,
  57. MinerInvalidJobId = -32319,
  58. MinerMissingNonce = -32320,
  59. MinerInvalidNonce = -32321,
  60. MinerMissingResult = -32322,
  61. MinerInvalidResult = -32323,
  62. // Merge mining errors
  63. MinerMissingAddress = -32324,
  64. MinerInvalidAddress = -32325,
  65. MinerMissingAuxHash = -32326,
  66. MinerInvalidAuxHash = -32327,
  67. MinerMissingHeight = -32328,
  68. MinerInvalidHeight = -32329,
  69. MinerMissingPrevId = -32330,
  70. MinerInvalidPrevId = -32331,
  71. MinerMissingAuxBlob = -32332,
  72. MinerInvalidAuxBlob = -32333,
  73. MinerMissingBlob = -32334,
  74. MinerInvalidBlob = -32335,
  75. MinerMissingMerkleProof = -32336,
  76. MinerInvalidMerkleProof = -32337,
  77. MinerMissingPath = -32338,
  78. MinerInvalidPath = -32339,
  79. MinerMissingSeedHash = -32340,
  80. MinerInvalidSeedHash = -32341,
  81. MinerMerkleProofConstructionFailed = -32342,
  82. MinerMoneroPowDataConstructionFailed = -32343,
  83. }
  84. fn to_tuple(e: RpcError) -> (i32, String) {
  85. let msg = match e {
  86. // Transaction-related errors
  87. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  88. RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
  89. // State-related errors
  90. RpcError::NotSynced => "Blockchain is not synced",
  91. RpcError::UnknownBlockHeight => "Did not find block height",
  92. // Parsing errors
  93. RpcError::ParseError => "Parse error",
  94. // Contract-related errors
  95. RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
  96. RpcError::ContractStateNotFound => "Records not found for given contract state",
  97. RpcError::ContractStateKeyNotFound => "Value not found for given contract state key",
  98. RpcError::ContractWasmNotFound => "wasm bincode not found for given contract",
  99. // Miner configuration errors
  100. RpcError::MinerInvalidWalletConfig => "Request wallet configuration is invalid",
  101. RpcError::MinerInvalidRecipient => "Request recipient wallet address is invalid",
  102. RpcError::MinerInvalidRecipientPrefix => {
  103. "Request recipient wallet address prefix is invalid"
  104. }
  105. RpcError::MinerInvalidSpendHook => "Request spend hook is invalid",
  106. RpcError::MinerInvalidUserData => "Request user data is invalid",
  107. // Stratum errors
  108. RpcError::MinerMissingLogin => "Request is missing the login",
  109. RpcError::MinerInvalidLogin => "Request login is invalid",
  110. RpcError::MinerMissingPassword => "Request is missing the password",
  111. RpcError::MinerInvalidPassword => "Request password is invalid",
  112. RpcError::MinerMissingAgent => "Request is missing the agent",
  113. RpcError::MinerInvalidAgent => "Request agent is invalid",
  114. RpcError::MinerMissingAlgo => "Request is missing the algo",
  115. RpcError::MinerInvalidAlgo => "Request algo is invalid",
  116. RpcError::MinerRandomXNotSupported => "Request doesn't support rx/0",
  117. RpcError::MinerMissingClientId => "Request is missing the client ID",
  118. RpcError::MinerInvalidClientId => "Request client ID is invalid",
  119. RpcError::MinerUnknownClient => "Request client is unknown",
  120. RpcError::MinerMissingJobId => "Request is missing the job ID",
  121. RpcError::MinerInvalidJobId => "Request job ID is invalid",
  122. RpcError::MinerMissingNonce => "Request is missing the nonce",
  123. RpcError::MinerInvalidNonce => "Request nonce is invalid",
  124. RpcError::MinerMissingResult => "Request is missing the result",
  125. RpcError::MinerInvalidResult => "Request nonce is result",
  126. // Merge mining errors
  127. RpcError::MinerMissingAddress => {
  128. "Request is missing the recipient wallet address configuration"
  129. }
  130. RpcError::MinerInvalidAddress => {
  131. "Request recipient wallet address configuration is invalid"
  132. }
  133. RpcError::MinerMissingAuxHash => "Request is missing the merge mining job (aux_hash)",
  134. RpcError::MinerInvalidAuxHash => "Request merge mining job (aux_hash) is invalid",
  135. RpcError::MinerMissingHeight => "Request is missing the Monero height",
  136. RpcError::MinerInvalidHeight => "Request Monero height is invalid",
  137. RpcError::MinerMissingPrevId => "Request is missing the hash of the previous Monero block",
  138. RpcError::MinerInvalidPrevId => "Request hash of the previous Monero block is invalid",
  139. RpcError::MinerMissingAuxBlob => "Request is missing the merge mining blob",
  140. RpcError::MinerInvalidAuxBlob => "Request merge mining bob is invalid",
  141. RpcError::MinerMissingBlob => "Request is missing the Monero block template",
  142. RpcError::MinerInvalidBlob => "Request Monero block template is invalid",
  143. RpcError::MinerMissingMerkleProof => "Request is missing the Merkle proof",
  144. RpcError::MinerInvalidMerkleProof => "Request Merkle proof is invalid",
  145. RpcError::MinerMissingPath => "Request is missing the Merkle proof path",
  146. RpcError::MinerInvalidPath => "Request Merkle proof path is invalid",
  147. RpcError::MinerMissingSeedHash => "Request is missing the RandomX seed key",
  148. RpcError::MinerInvalidSeedHash => "Request RandomX seed key is invalid",
  149. RpcError::MinerMerkleProofConstructionFailed => {
  150. "failed constructing aux chain Merkle proof"
  151. }
  152. RpcError::MinerMoneroPowDataConstructionFailed => "Failed constructing Monero PoW data",
  153. };
  154. (e as i32, msg.to_string())
  155. }
  156. pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
  157. let (code, default_msg) = to_tuple(e);
  158. if let Some(message) = msg {
  159. return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
  160. }
  161. JsonError::new(ServerError(code), Some(default_msg), id).into()
  162. }
  163. pub fn miner_status_response(id: u16, status: &str) -> JsonResult {
  164. JsonResponse::new(
  165. JsonValue::from(HashMap::from([(
  166. "status".to_string(),
  167. JsonValue::from(String::from(status)),
  168. )])),
  169. id,
  170. )
  171. .into()
  172. }