Skip to content

Commit 1351430

Browse files
committed
Move eth error_code and description to ErrorCodes
1 parent 76521da commit 1351430

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

src/peersafe/app/util/Common.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
2424
#include <peersafe/protocol/STETx.h>
2525
#include <ripple/ledger/OpenView.h>
2626
#include <ripple/protocol/STTx.h>
27+
#include <ripple/protocol/ErrorCodes.h>
2728

2829
namespace ripple {
2930

@@ -93,6 +94,19 @@ ethAddrChecksum(std::string addr)
9394
return ret;
9495
}
9596

97+
Json::Value
98+
formatEthError(int code)
99+
{
100+
Json::Value jvResult;
101+
Json::Value jvError;
102+
jvError["code"] = code;
103+
jvError["message"] = RPC::get_error_msg(error_code_eth(code));
104+
jvError["data"] = {};
105+
jvResult["error"] = jvError;
106+
107+
return jvResult;
108+
}
109+
96110
Json::Value
97111
formatEthError(int code, std::string const& msg)
98112
{

src/peersafe/app/util/Common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class STTx;
3636
class OpenView;
3737
using H256Set = std::unordered_set<uint256>;
3838

39-
const int defaultEthErrorCode = -32000;
40-
const int ethMethodNotFound = -32601;
4139
const std::uint64_t weiPerDrop = std::uint64_t(1e12);
4240
const std::uint64_t compressDrop = std::uint64_t(1e3);
4341
const std::uint64_t weiPerDropWithFeature = std::uint64_t(1e9);
@@ -84,6 +82,9 @@ inline compressDrops2Str(uint64_t drops,bool bGasPriceFeatureEnabled)
8482
std::string
8583
ethAddrChecksum(std::string addr);
8684

85+
Json::Value
86+
formatEthError(int code);
87+
8788
Json::Value
8889
formatEthError(int code, std::string const& msg);
8990

src/peersafe/rpc/handlers/EthRpcApi.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ getTxByHash(std::string txHash, Schema& app)
107107
auto hash = from_hex_text<uint256>(txHash);
108108
auto txn = app.getMasterTransaction().fetch(hash);
109109
if (!txn)
110-
return formatEthError(defaultEthErrorCode, rpcTXN_NOT_FOUND);
110+
return formatEthError(ethERROR_DEFAULT, rpcTXN_NOT_FOUND);
111111

112112
auto tx =
113113
std::dynamic_pointer_cast<STETx const>(txn->getSTransaction());
114114
if (!tx)
115-
return formatEthError(defaultEthErrorCode, "Not a eth tx.");
115+
return formatEthError(ethERROR_DEFAULT, "Not a eth tx.");
116116

117117
auto ledger = app.getLedgerMaster().getLedgerBySeq(txn->getLedger());
118118
jvResult["hash"] = "0x" + txHash;
@@ -154,7 +154,7 @@ getTxByHash(std::string txHash, Schema& app)
154154
jvResult["r"] = toHexString(r);
155155
jvResult["s"] = toHexString(s);
156156
} catch (std::exception& ex) {
157-
return formatEthError(defaultEthErrorCode, ex.what());
157+
return formatEthError(ethERROR_DEFAULT, ex.what());
158158
}
159159

160160
return jvResult;
@@ -357,7 +357,7 @@ doEthSendTransaction(RPC::JsonContext& context)
357357
context.app.config());
358358
if (validity != Validity::Valid)
359359
{
360-
return formatEthError(defaultEthErrorCode, "Check validity failed.");
360+
return formatEthError(ethERROR_DEFAULT, "Check validity failed.");
361361
}
362362

363363
std::string reason2;
@@ -395,11 +395,11 @@ doEthSendRawTransaction(RPC::JsonContext& context)
395395
// 500KB
396396
if (ret->size() > RPC::Tuning::max_txn_size)
397397
{
398-
return formatEthError(defaultEthErrorCode,rpcTXN_BIGGER_THAN_MAXSIZE);
398+
return formatEthError(ethERROR_DEFAULT,rpcTXN_BIGGER_THAN_MAXSIZE);
399399
}
400400

401401
if (!ret || !ret->size())
402-
return formatEthError(defaultEthErrorCode, rpcINVALID_PARAMS);
402+
return formatEthError(ethERROR_DEFAULT, rpcINVALID_PARAMS);
403403

404404
auto lastLedgerSeq = context.ledgerMaster.getCurrentLedgerIndex() + LAST_LEDGER_SEQ_OFFSET;
405405
//Construct STETx
@@ -414,7 +414,7 @@ doEthSendRawTransaction(RPC::JsonContext& context)
414414
context.app.config());
415415
if (validity != Validity::Valid)
416416
{
417-
return formatEthError(defaultEthErrorCode, "Check validity failed.");
417+
return formatEthError(ethERROR_DEFAULT, "Check validity failed.");
418418
}
419419

420420
std::string reason2;
@@ -638,7 +638,7 @@ doEthGasPrice(RPC::JsonContext& context)
638638
Json::Value
639639
doEthFeeHistory(RPC::JsonContext& context)
640640
{
641-
return formatEthError(ethMethodNotFound, "Method not found");
641+
return formatEthError(ethMETHOD_NOT_FOUND);
642642
}
643643

644644
Json::Value

src/peersafe/rpc/handlers/SmartContractHandler.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Json::Value ContractLocalCallResultImpl(Json::Value originJson, TER terResult, s
6969
{
7070
auto errMsg = "Exception occurred during JSON handling.";
7171
if (isEthCall)
72-
return formatEthError(defaultEthErrorCode, "Exception occurred during JSON handling.");
72+
return formatEthError(ethERROR_DEFAULT, "Exception occurred during JSON handling.");
7373
else
7474
return RPC::make_error(rpcINTERNAL, errMsg);
7575
}
@@ -178,7 +178,7 @@ doContractCall(RPC::JsonContext& context)
178178
if (isRpcError(checkResult))
179179
{
180180
if (isEthCall)
181-
return formatEthError(defaultEthErrorCode, checkResult[jss::error_message].asString());
181+
return formatEthError(ethERROR_DEFAULT, checkResult[jss::error_message].asString());
182182
return checkResult;
183183
}
184184

@@ -190,7 +190,7 @@ doContractCall(RPC::JsonContext& context)
190190
auto accId = parseHex<AccountID>(ethParams["from"].asString());
191191
if (!accId)
192192
return formatEthError(
193-
defaultEthErrorCode, rpcDST_ACT_MALFORMED);
193+
ethERROR_DEFAULT, rpcDST_ACT_MALFORMED);
194194
accountID = *accId;
195195
}
196196
}
@@ -210,7 +210,7 @@ doContractCall(RPC::JsonContext& context)
210210
{
211211
if (isEthCall && result.isMember(jss::error_message))
212212
return formatEthError(
213-
defaultEthErrorCode, result[jss::error_message].asString());
213+
ethERROR_DEFAULT, result[jss::error_message].asString());
214214
return result;
215215
}
216216

@@ -220,7 +220,7 @@ doContractCall(RPC::JsonContext& context)
220220
{
221221
auto optID = parseHex<AccountID>(ethParams["to"].asString());
222222
if (!optID)
223-
return formatEthError(defaultEthErrorCode, rpcDST_ACT_MALFORMED);
223+
return formatEthError(ethERROR_DEFAULT, rpcDST_ACT_MALFORMED);
224224
contractAddrID = *optID;
225225

226226
if(!ethParams.isMember("from"))
@@ -248,7 +248,7 @@ doContractCall(RPC::JsonContext& context)
248248
{
249249
errMsgStr = "contract_data is invalid.";
250250
if (isEthCall)
251-
return formatEthError(defaultEthErrorCode, errMsgStr);
251+
return formatEthError(ethERROR_DEFAULT, errMsgStr);
252252
return RPC::make_error(rpcINVALID_PARAMS, errMsgStr);
253253
}
254254
auto contractDataBlob = *strUnHexRes;
@@ -297,7 +297,7 @@ doEstimateGas(RPC::JsonContext& context)
297297

298298
auto optID = RPC::accountFromStringStrict(ethParams["from"].asString());
299299
if (!optID)
300-
return formatEthError(defaultEthErrorCode, rpcDST_ACT_MALFORMED);
300+
return formatEthError(ethERROR_DEFAULT, rpcDST_ACT_MALFORMED);
301301

302302
accountID = *optID;
303303

@@ -306,7 +306,7 @@ doEstimateGas(RPC::JsonContext& context)
306306
if (!ledger)
307307
return result;
308308
if (!ledger->exists(keylet::account(accountID)))
309-
return formatEthError(defaultEthErrorCode, rpcACT_NOT_FOUND);
309+
return formatEthError(ethERROR_DEFAULT, rpcACT_NOT_FOUND);
310310

311311
std::shared_ptr<OpenView> openViewTemp =
312312
std::make_shared<OpenView>(ledger.get());
@@ -319,7 +319,7 @@ doEstimateGas(RPC::JsonContext& context)
319319
auto optID = parseHex<AccountID>(ethParams["to"].asString());
320320
if (!optID)
321321
return formatEthError(
322-
defaultEthErrorCode, rpcDST_ACT_MALFORMED);
322+
ethERROR_DEFAULT, rpcDST_ACT_MALFORMED);
323323

324324
contractAddrID = *optID;
325325
isCreation = false;
@@ -347,7 +347,7 @@ doEstimateGas(RPC::JsonContext& context)
347347
if (!strUnHexRes)
348348
{
349349
return formatEthError(
350-
defaultEthErrorCode, "contract_data is not in hex");
350+
ethERROR_DEFAULT, "contract_data is not in hex");
351351
}
352352
contractDataBlob = *strUnHexRes;
353353
if (contractDataBlob.size() == 0)
@@ -443,7 +443,7 @@ doEstimateGas(RPC::JsonContext& context)
443443
}
444444
else
445445
{
446-
return formatEthError(defaultEthErrorCode, errMsg);
446+
return formatEthError(ethERROR_DEFAULT, errMsg);
447447
}
448448

449449
//JLOG(context.j.warn())

src/ripple/protocol/ErrorCodes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ enum warning_code_i {
201201
warnRPC_AMENDMENT_BLOCKED = 1002,
202202
};
203203

204+
enum error_code_eth {
205+
ethERROR_DEFAULT = -32000,
206+
ethMETHOD_NOT_FOUND = -32601,
207+
};
208+
204209
//------------------------------------------------------------------------------
205210

206211
// VFALCO NOTE these should probably not be in the RPC namespace.
@@ -233,6 +238,9 @@ struct ErrorInfo
233238
ErrorInfo const&
234239
get_error_info(error_code_i code);
235240

241+
std::string const&
242+
get_error_msg(error_code_eth code);
243+
236244
/** Add or update the json update to reflect the error code. */
237245
/** @{ */
238246
template <class JsonValue>

src/ripple/protocol/impl/ErrorCodes.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,14 @@ constexpr static ErrorInfo unorderedErrorInfos[]{
174174
// add (rpcWRONG_SEED, "wrongSeed", "The regular key does not point as the master key.");
175175
};
176176

177-
// C++ does not allow you to return an array from a function. You must
177+
static std::map<
178+
error_code_eth, std::string> const eth_codes
179+
{
180+
{ ethERROR_DEFAULT, "Internal error" },
181+
{ ethMETHOD_NOT_FOUND, "Method not found" },
182+
183+
};
184+
// C++ does not allow you to return an array from a function. You must
178185
// return an object which may in turn contain an array. The following
179186
// struct is simply defined so the enclosed array can be returned from a
180187
// constexpr function.
@@ -260,6 +267,15 @@ get_error_info(error_code_i code)
260267
return detail::sortedErrorInfos.infos[code - 1];
261268
}
262269

270+
std::string const&
271+
get_error_msg(error_code_eth code)
272+
{
273+
if (detail::eth_codes.find(code) != detail::eth_codes.end())
274+
return detail::eth_codes.at(code);
275+
return detail::eth_codes.at(ethERROR_DEFAULT);
276+
}
277+
278+
263279
Json::Value
264280
make_error(error_code_i code)
265281
{

0 commit comments

Comments
 (0)