From fef18b709c729d2845d804b6f543a709fd7227f0 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Thu, 11 Nov 2021 06:11:21 +0000 Subject: [PATCH] Refactor rpc oracles (#886) Signed-off-by: Anthony Fieroni Co-authored-by: Prasanna Loganathar --- src/masternodes/rpc_oracles.cpp | 46 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/masternodes/rpc_oracles.cpp b/src/masternodes/rpc_oracles.cpp index 4ec381d4fb..92ab045f89 100644 --- a/src/masternodes/rpc_oracles.cpp +++ b/src/masternodes/rpc_oracles.cpp @@ -246,8 +246,8 @@ UniValue updateoracle(const JSONRPCRequest& request) { int targetHeight = chainHeight(*pwallet->chain().lock()) + 1; CUpdateOracleAppointMessage msg{ - oracleId, - CAppointOracleMessage{std::move(script), static_cast(weightage), std::move(allowedPairs)} + oracleId, + CAppointOracleMessage{std::move(script), uint8_t(weightage), std::move(allowedPairs)} }; // encode @@ -452,7 +452,7 @@ UniValue setoracledata(const JSONRPCRequest &request) { auto currency = value[oraclefields::Currency].getValStr(); auto tokenAmount = value[oraclefields::TokenAmount].getValStr(); auto amounts = ParseTokenAmount(tokenAmount); - if (!amounts.ok) { + if (!amounts) { throw JSONRPCError(RPC_DESERIALIZATION_ERROR, amounts.msg); } @@ -476,7 +476,7 @@ UniValue setoracledata(const JSONRPCRequest &request) { LOCK(cs_main); // check if tx parameters are valid auto oracleRes = pcustomcsview->GetOracleData(oracleId); - if (!oracleRes.ok) { + if (!oracleRes) { throw JSONRPCError(RPC_INVALID_REQUEST, oracleRes.msg); } oracleAddress = oracleRes.val->oracleAddress; @@ -611,7 +611,7 @@ UniValue getoracledata(const JSONRPCRequest &request) { CCustomCSView mnview(*pcustomcsview); // don't write into actual DB auto oracleRes = mnview.GetOracleData(oracleId); - if (!oracleRes.ok) { + if (!oracleRes) { throw JSONRPCError(RPC_DATABASE_ERROR, oracleRes.msg); } @@ -688,8 +688,7 @@ UniValue listoracles(const JSONRPCRequest &request) { return (true); } value.push_back(id.GetHex()); - limit--; - return limit != 0; + return --limit != 0; }, start); return value; @@ -805,7 +804,9 @@ UniValue listlatestrawprices(const JSONRPCRequest &request) { auto state = diffInHour(timestamp, lastBlockTime) ? oraclefields::Alive : oraclefields::Expired; value.pushKV(oraclefields::State, state); result.push_back(value); - limit--; + if (--limit == 0) { + return false; + } } } return limit != 0; @@ -879,6 +880,9 @@ namespace { if (!paginationObj["including_start"].isNull()) { including_start = paginationObj["including_start"].getBool(); } + if (!including_start) { + ++start; + } } if (limit == 0) { limit = std::numeric_limits::max(); @@ -888,14 +892,9 @@ namespace { std::set setTokenCurrency; view.ForEachOracle([&](const COracleId&, COracle oracle) { const auto& pairs = oracle.availablePairs; - if(start > pairs.size()-1) - return true; - const auto& startingPairIt = std::next(pairs.begin(), start); - if(!including_start){ - setTokenCurrency.insert(std::next(pairs.begin(), start+1), pairs.end()); - return true; + if (start < pairs.size()) { + setTokenCurrency.insert(std::next(pairs.begin(), start), pairs.end()); } - setTokenCurrency.insert(startingPairIt, pairs.end()); return true; }); for (const auto& tokenCurrency : setTokenCurrency) { @@ -912,9 +911,9 @@ namespace { item.pushKV(oraclefields::ValidityFlag, aggregatePrice.msg); } result.push_back(item); - limit--; - if (limit == 0) + if (--limit == 0) { break; + } } return result; } @@ -1043,14 +1042,17 @@ UniValue getfixedintervalprice(const JSONRPCRequest& request) { "Invalid parameter, argument fixedIntervalPriceId must be non-null"); auto fixedIntervalStr = request.params[0].getValStr(); + UniValue objPrice{UniValue::VOBJ}; objPrice.pushKV("fixedIntervalPriceId", fixedIntervalStr); auto pairId = DecodePriceFeedUni(objPrice); LOCK(cs_main); + LogPrint(BCLog::ORACLE,"%s()->", __func__); /* Continued */ + auto fixedPrice = pcustomcsview->GetFixedIntervalPrice(pairId); - if(!fixedPrice) + if (!fixedPrice) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, fixedPrice.msg); auto priceBlocks = GetFixedIntervalPriceBlocks(::ChainActive().Height(), *pcustomcsview); @@ -1113,7 +1115,6 @@ UniValue listfixedintervalprices(const JSONRPCRequest& request) { LOCK(cs_main); - UniValue listPrice{UniValue::VARR}; pcustomcsview->ForEachFixedIntervalPrice([&](const CTokenCurrencyPair&, CFixedIntervalPrice fixedIntervalPrice){ UniValue obj{UniValue::VOBJ}; @@ -1123,16 +1124,11 @@ UniValue listfixedintervalprices(const JSONRPCRequest& request) { obj.pushKV("timestamp", fixedIntervalPrice.timestamp); obj.pushKV("isLive", fixedIntervalPrice.isLive(pcustomcsview->GetPriceDeviation())); listPrice.push_back(obj); - limit--; - return limit != 0; + return --limit != 0; }, start); return listPrice; } - - - - static const CRPCCommand commands[] = { // category name actor (function) params