error.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Miner configuration errors
  37. MinerInvalidWalletConfig = -32301,
  38. MinerInvalidRecipient = -32302,
  39. MinerInvalidRecipientPrefix = -32303,
  40. MinerInvalidSpendHook = -32304,
  41. MinerInvalidUserData = -32305,
  42. // Stratum errors
  43. MinerMissingLogin = -32306,
  44. MinerInvalidLogin = -32307,
  45. MinerMissingPassword = -32308,
  46. MinerInvalidPassword = -32309,
  47. MinerMissingAgent = -32310,
  48. MinerInvalidAgent = -32311,
  49. MinerMissingAlgo = -32312,
  50. MinerInvalidAlgo = -32313,
  51. MinerRandomXNotSupported = -32314,
  52. MinerMissingClientId = -32315,
  53. MinerInvalidClientId = -32316,
  54. MinerUnknownClient = -32317,
  55. MinerMissingJobId = -32318,
  56. MinerInvalidJobId = -32319,
  57. MinerMissingNonce = -32320,
  58. MinerInvalidNonce = -32321,
  59. MinerMissingResult = -32322,
  60. MinerInvalidResult = -32323,
  61. // Merge mining errors
  62. MinerMissingAddress = -32324,
  63. MinerInvalidAddress = -32325,
  64. MinerMissingAuxHash = -32326,
  65. MinerInvalidAuxHash = -32327,
  66. MinerMissingHeight = -32328,
  67. MinerInvalidHeight = -32329,
  68. MinerMissingPrevId = -32330,
  69. MinerInvalidPrevId = -32331,
  70. MinerMissingAuxBlob = -32332,
  71. MinerInvalidAuxBlob = -32333,
  72. MinerMissingBlob = -32334,
  73. MinerInvalidBlob = -32335,
  74. MinerMissingMerkleProof = -32336,
  75. MinerInvalidMerkleProof = -32337,
  76. MinerMissingPath = -32338,
  77. MinerInvalidPath = -32339,
  78. MinerMissingSeedHash = -32340,
  79. MinerInvalidSeedHash = -32341,
  80. MinerMerkleProofConstructionFailed = -32342,
  81. MinerMoneroPowDataConstructionFailed = -32343,
  82. }
  83. fn to_tuple(e: RpcError) -> (i32, String) {
  84. let msg = match e {
  85. // Transaction-related errors
  86. RpcError::TxSimulationFail => "Failed simulating transaction state change",
  87. RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
  88. // State-related errors
  89. RpcError::NotSynced => "Blockchain is not synced",
  90. RpcError::UnknownBlockHeight => "Did not find block height",
  91. // Parsing errors
  92. RpcError::ParseError => "Parse error",
  93. // Contract-related errors
  94. RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
  95. RpcError::ContractStateNotFound => "Records not found for given contract state",
  96. RpcError::ContractStateKeyNotFound => "Value not found for given contract state key",
  97. // Miner configuration errors
  98. RpcError::MinerInvalidWalletConfig => "Request wallet configuration is invalid",
  99. RpcError::MinerInvalidRecipient => "Request recipient wallet address is invalid",
  100. RpcError::MinerInvalidRecipientPrefix => {
  101. "Request recipient wallet address prefix is invalid"
  102. }
  103. RpcError::MinerInvalidSpendHook => "Request spend hook is invalid",
  104. RpcError::MinerInvalidUserData => "Request user data is invalid",
  105. // Stratum errors
  106. RpcError::MinerMissingLogin => "Request is missing the login",
  107. RpcError::MinerInvalidLogin => "Request login is invalid",
  108. RpcError::MinerMissingPassword => "Request is missing the password",
  109. RpcError::MinerInvalidPassword => "Request password is invalid",
  110. RpcError::MinerMissingAgent => "Request is missing the agent",
  111. RpcError::MinerInvalidAgent => "Request agent is invalid",
  112. RpcError::MinerMissingAlgo => "Request is missing the algo",
  113. RpcError::MinerInvalidAlgo => "Request algo is invalid",
  114. RpcError::MinerRandomXNotSupported => "Request doesn't support rx/0",
  115. RpcError::MinerMissingClientId => "Request is missing the client ID",
  116. RpcError::MinerInvalidClientId => "Request client ID is invalid",
  117. RpcError::MinerUnknownClient => "Request client is unknown",
  118. RpcError::MinerMissingJobId => "Request is missing the job ID",
  119. RpcError::MinerInvalidJobId => "Request job ID is invalid",
  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. }
  161. pub fn miner_status_response(id: u16, status: &str) -> JsonResult {
  162. JsonResponse::new(
  163. JsonValue::from(HashMap::from([(
  164. "status".to_string(),
  165. JsonValue::from(String::from(status)),
  166. )])),
  167. id,
  168. )
  169. .into()
  170. }