convert from ripple::Json to boost::json

This commit is contained in:
CJ Cobb
2020-12-23 10:20:45 -05:00
parent ca4f38dad9
commit 251c6f6c49
4 changed files with 56 additions and 26 deletions

View File

@@ -20,26 +20,9 @@
#include <ripple/protocol/Indexes.h> #include <ripple/protocol/Indexes.h>
#include <ripple/protocol/STLedgerEntry.h> #include <ripple/protocol/STLedgerEntry.h>
#include <boost/json.hpp> #include <boost/json.hpp>
#include <handlers/RPCHelpers.h>
#include <reporting/ReportingBackend.h> #include <reporting/ReportingBackend.h>
std::optional<ripple::AccountID>
accountFromStringStrict(std::string const& account)
{
boost::optional<ripple::AccountID> result;
auto const publicKey = ripple::parseBase58<ripple::PublicKey>(
ripple::TokenType::AccountPublic, account);
if (publicKey)
result = ripple::calcAccountID(*publicKey);
else
result = ripple::parseBase58<ripple::AccountID>(account);
if (result)
return result.value();
else
return {};
}
// { // {
// account: <ident>, // account: <ident>,
// strict: <bool> // optional (default false) // strict: <bool> // optional (default false)
@@ -94,9 +77,9 @@ doAccountInfo(
{ {
response["error"] = "no response from db"; response["error"] = "no response from db";
} }
auto sle = std::make_shared<ripple::SLE>( ripple::STLedgerEntry sle{
ripple::SerialIter{dbResponse->data(), dbResponse->size()}, key.key); ripple::SerialIter{dbResponse->data(), dbResponse->size()}, key.key};
if (!key.check(*sle)) if (!key.check(sle))
{ {
response["error"] = "error fetching record from db"; response["error"] = "error fetching record from db";
return response; return response;
@@ -104,7 +87,7 @@ doAccountInfo(
else else
{ {
response["success"] = "fetched successfully!"; response["success"] = "fetched successfully!";
response["object"] = sle->getFullText(); response["object"] = getJson(sle);
return response; return response;
} }
@@ -130,3 +113,4 @@ doAccountInfo(
return response; return response;
} }

38
handlers/RPCHelpers.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef XRPL_REPORTING_RPCHELPERS_H_INCLUDED
#define XRPL_REPORTING_RPCHELPERS_H_INCLUDED
#include <ripple/protocol/STLedgerEntry.h>
#include <boost/json.hpp>
std::optional<ripple::AccountID>
accountFromStringStrict(std::string const& account)
{
boost::optional<ripple::AccountID> result;
auto const publicKey = ripple::parseBase58<ripple::PublicKey>(
ripple::TokenType::AccountPublic, account);
if (publicKey)
result = ripple::calcAccountID(*publicKey);
else
result = ripple::parseBase58<ripple::AccountID>(account);
if (result)
return result.value();
else
return {};
}
boost::json::object
getJson(ripple::SLE const& sle)
{
auto start = std::chrono::system_clock::now();
boost::json::value value = boost::json::parse(
sle.getJson(ripple::JsonOptions::none).toStyledString());
auto end = std::chrono::system_clock::now();
value.as_object()["deserialization_time_microseconds"] =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
return value.as_object();
}
#endif

View File

@@ -633,6 +633,7 @@ public:
fetch(void const* key, uint32_t sequence) const fetch(void const* key, uint32_t sequence) const
{ {
BOOST_LOG_TRIVIAL(trace) << "Fetching from cassandra"; BOOST_LOG_TRIVIAL(trace) << "Fetching from cassandra";
auto start = std::chrono::system_clock::now();
CassStatement* statement = cass_prepared_bind(selectObject_); CassStatement* statement = cass_prepared_bind(selectObject_);
cass_statement_set_consistency(statement, CASS_CONSISTENCY_QUORUM); cass_statement_set_consistency(statement, CASS_CONSISTENCY_QUORUM);
CassError rc = cass_statement_bind_bytes( CassError rc = cass_statement_bind_bytes(
@@ -690,6 +691,13 @@ public:
} }
std::vector<unsigned char> result{buf, buf + bufSize}; std::vector<unsigned char> result{buf, buf + bufSize};
cass_result_free(res); cass_result_free(res);
auto end = std::chrono::system_clock::now();
BOOST_LOG_TRIVIAL(debug)
<< "Fetched from cassandra in "
<< std::chrono::duration_cast<std::chrono::microseconds>(
end - start)
.count()
<< " microseconds";
return result; return result;
} }
/* /*

View File

@@ -12,7 +12,7 @@ import threading
async def ping(ip, port): async def account_info(ip, port):
address = 'ws://' + str(ip) + ':' + str(port) address = 'ws://' + str(ip) + ':' + str(port)
try: try:
async with websockets.connect(address) as ws: async with websockets.connect(address) as ws:
@@ -26,7 +26,7 @@ async def ping(ip, port):
parser = argparse.ArgumentParser(description='test script for xrpl-reporting') parser = argparse.ArgumentParser(description='test script for xrpl-reporting')
parser.add_argument('action', choices=["ping"]) parser.add_argument('action', choices=["account_info"])
parser.add_argument('--ip', default='127.0.0.1') parser.add_argument('--ip', default='127.0.0.1')
parser.add_argument('--port', default='8080') parser.add_argument('--port', default='8080')
@@ -36,9 +36,9 @@ args = parser.parse_args()
def run(args): def run(args):
asyncio.set_event_loop(asyncio.new_event_loop()) asyncio.set_event_loop(asyncio.new_event_loop())
if args.action == "ping": if args.action == "account_info":
asyncio.get_event_loop().run_until_complete( asyncio.get_event_loop().run_until_complete(
ping(args.ip, args.port)) account_info(args.ip, args.port))
else: else:
print("incorrect arguments") print("incorrect arguments")