account_info doesn't need ledger_index. fix bug where multiple

ledger objects are fetched
This commit is contained in:
CJ Cobb
2021-01-11 10:16:52 -05:00
parent 72add230f6
commit 850d7a4a45
4 changed files with 50 additions and 15 deletions

View File

@@ -21,6 +21,7 @@
#include <ripple/protocol/STLedgerEntry.h>
#include <boost/json.hpp>
#include <handlers/RPCHelpers.h>
#include <reporting/Pg.h>
#include <reporting/ReportingBackend.h>
// {
@@ -42,7 +43,8 @@
boost::json::object
doAccountInfo(
boost::json::object const& request,
CassandraFlatMapBackend const& backend)
CassandraFlatMapBackend const& backend,
std::shared_ptr<PgPool>& postgres)
{
boost::json::object response;
std::string strIdent;
@@ -55,7 +57,25 @@ doAccountInfo(
response["error"] = "missing account field";
return response;
}
size_t ledgerSequence = request.at("ledger_index").as_int64();
size_t ledgerSequence = 0;
if (not request.contains("ledger_index"))
{
std::optional<ripple::LedgerInfo> latest = getLedger({}, postgres);
if (not latest)
{
response["error"] = "database is empty";
return response;
}
else
{
ledgerSequence = latest->seq;
}
}
else
{
ledgerSequence = request.at("ledger_index").as_int64();
}
// bool bStrict = request.contains("strict") &&
// params.at("strict").as_bool();
@@ -71,8 +91,13 @@ doAccountInfo(
}
auto key = ripple::keylet::account(accountID.value());
auto start = std::chrono::system_clock::now();
std::optional<std::vector<unsigned char>> dbResponse =
backend.fetch(key.key.data(), ledgerSequence);
auto end = std::chrono::system_clock::now();
auto time =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
if (!dbResponse)
{
response["error"] = "no response from db";
@@ -88,6 +113,7 @@ doAccountInfo(
{
response["success"] = "fetched successfully!";
response["object"] = getJson(sle);
response["db_time"] = time;
return response;
}

View File

@@ -489,7 +489,8 @@ public:
query = {};
query << "SELECT object, sequence FROM " << tableName << "flat"
<< " WHERE key = ? AND sequence <= ? ORDER BY sequence DESC";
<< " WHERE key = ? AND sequence <= ? ORDER BY sequence DESC "
"LIMIT 1";
prepare_future =
cass_session_prepare(session_.get(), query.str().c_str());

27
test.py
View File

@@ -12,21 +12,26 @@ import threading
async def account_info(ip, port):
async def account_info(ip, port, account, ledger):
address = 'ws://' + str(ip) + ':' + str(port)
try:
async with websockets.connect(address) as ws:
await ws.send(json.dumps({"command":"account_info","ledger_index":60392449,"account":"rLC64xxNif3GiY9FQnbaM4kcE6VvDhwRod"}))
res = json.loads(await ws.recv())
print(res)
if ledger is None:
await ws.send(json.dumps({"command":"account_info","account":account}))
res = json.loads(await ws.recv())
print(res)
else:
await ws.send(json.dumps({"command":"account_info","account":account, "ledger_index":int(ledger)}))
res = json.loads(await ws.recv())
print(res)
except websockets.exceptions.ConnectionClosedError as e:
print(e)
async def account_tx(ip, port):
async def account_tx(ip, port, account):
address = 'ws://' + str(ip) + ':' + str(port)
try:
async with websockets.connect(address) as ws:
await ws.send(json.dumps({"command":"account_tx","account":"rDzTZxa7NwD9vmNf5dvTbW4FQDNSRsfPv6"}))
await ws.send(json.dumps({"command":"account_tx","account":account}))
res = json.loads(await ws.recv())
print(res)
except websockets.exceptions.ConnectionClosedError as e:
@@ -50,6 +55,8 @@ parser.add_argument('action', choices=["account_info", "tx", "account_tx"])
parser.add_argument('--ip', default='127.0.0.1')
parser.add_argument('--port', default='8080')
parser.add_argument('--hash')
parser.add_argument('--account', default="rLC64xxNif3GiY9FQnbaM4kcE6VvDhwRod")
parser.add_argument('--ledger')
@@ -59,13 +66,13 @@ def run(args):
asyncio.set_event_loop(asyncio.new_event_loop())
if args.action == "account_info":
asyncio.get_event_loop().run_until_complete(
account_info(args.ip, args.port))
if args.action == "tx":
account_info(args.ip, args.port, args.account, args.ledger))
elif args.action == "tx":
asyncio.get_event_loop().run_until_complete(
tx(args.ip, args.port, args.hash))
if args.action == "account_tx":
elif args.action == "account_tx":
asyncio.get_event_loop().run_until_complete(
account_tx(args.ip, args.port))
account_tx(args.ip, args.port, args.account))
else:
print("incorrect arguments")

View File

@@ -45,7 +45,8 @@ std::unordered_map<std::string, RPCCommand> commandMap{
boost::json::object
doAccountInfo(
boost::json::object const& request,
CassandraFlatMapBackend const& backend);
CassandraFlatMapBackend const& backend,
std::shared_ptr<PgPool>& postgres);
boost::json::object
doTx(
boost::json::object const& request,
@@ -76,7 +77,7 @@ buildResponse(
case ledger:
break;
case account_info:
return doAccountInfo(request, backend);
return doAccountInfo(request, backend, pgPool);
break;
default:
BOOST_LOG_TRIVIAL(error) << "Unknown command: " << command;