From 026a89693648a316b78248077e0aa241bacd4101 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 9 Dec 2020 17:37:32 +0200 Subject: [PATCH 1/2] Include wallet UTXO standard txs in account history Signed-off-by: Anthony Fieroni --- src/masternodes/mn_rpc.cpp | 92 ++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/src/masternodes/mn_rpc.cpp b/src/masternodes/mn_rpc.cpp index cdadd723ac..11250e7070 100644 --- a/src/masternodes/mn_rpc.cpp +++ b/src/masternodes/mn_rpc.cpp @@ -2989,6 +2989,17 @@ UniValue listpoolshares(const JSONRPCRequest& request) { return ret; } +UniValue AmountsToJSON(TAmounts const & diffs) { + UniValue obj(UniValue::VARR); + + for (auto const & diff : diffs) { + auto token = pcustomcsview->GetToken(diff.first); + auto const tokenIdStr = token->CreateSymbolKey(diff.first); + obj.push_back(ValueFromAmount(diff.second).getValStr() + "@" + tokenIdStr); + } + return obj; +} + UniValue accounthistoryToJSON(CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diffs) { UniValue obj(UniValue::VOBJ); @@ -3000,14 +3011,20 @@ UniValue accounthistoryToJSON(CScript const & owner, uint32_t height, uint32_t t obj.pushKV("txid", txid.ToString()); } - UniValue diffsObj(UniValue::VARR); - for (auto const & diff : diffs) { - auto token = pcustomcsview->GetToken(diff.first); - std::string const tokenIdStr = token->CreateSymbolKey(diff.first); + obj.pushKV("amounts", AmountsToJSON(diffs)); + return obj; +} - diffsObj.push_back(ValueFromAmount(diff.second).getValStr() + "@" + tokenIdStr); - } - obj.pushKV("amounts", diffsObj); +UniValue outputEntryToJSON(COutputEntry const & entry, uint32_t height, uint256 const & hashBlock, uint256 const & txid, std::string const & type) { + UniValue obj(UniValue::VOBJ); + + obj.pushKV("owner", EncodeDestination(entry.destination)); + obj.pushKV("blockHeight", (uint64_t)height); + obj.pushKV("blockHash", hashBlock.GetHex()); + obj.pushKV("type", type); + obj.pushKV("txn", (uint64_t) entry.vout); + obj.pushKV("txid", txid.ToString()); + obj.pushKV("amounts", AmountsToJSON({{DCT_ID{0}, entry.amount}})); return obj; } @@ -3079,12 +3096,21 @@ UniValue listaccounthistory(const JSONRPCRequest& request) { pwallet->BlockUntilSyncedToCurrentChain(); startBlock = std::min(startBlock, uint32_t(chainHeight(*pwallet->chain().lock()))); + CScript owner; + isminefilter filter = ISMINE_ALL_USED; + + std::set txs; + const bool shouldSearchInWallet = tokenFilter.empty() || tokenFilter == "DFI"; + UniValue ret(UniValue::VARR); + LOCK2(cs_main, pwallet->cs_wallet); + if (accounts == "mine") { // traversing through owned scripts CScript prevOwner{}; bool isMine = false; + filter = ISMINE_SPENDABLE; AccountHistoryKey startKey{ prevOwner, startBlock, std::numeric_limits::max() }; // starting from max txn values pcustomcsview->ForEachAccountHistory([&](CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diffs) { if (height > startKey.blockHeight || (depth <= startKey.blockHeight && (height < startKey.blockHeight - depth))) @@ -3120,6 +3146,9 @@ UniValue listaccounthistory(const JSONRPCRequest& request) { if (isMine) { ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs)); + if (shouldSearchInWallet) { + txs.insert(txid); + } } return true; @@ -3157,13 +3186,16 @@ UniValue listaccounthistory(const JSONRPCRequest& request) { } ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs)); + if (shouldSearchInWallet) { + txs.insert(txid); + } return true; }, startKey); } else { // parse single script/address: - CScript const owner = DecodeScript(accounts); + owner = DecodeScript(accounts); AccountHistoryKey startKey{ owner, startBlock, std::numeric_limits::max() }; // starting from max txn values pcustomcsview->ForEachAccountHistory([&](CScript const & owner, uint32_t height, uint32_t txn, uint256 const & txid, unsigned char category, TAmounts const & diffs) { @@ -3194,10 +3226,54 @@ UniValue listaccounthistory(const JSONRPCRequest& request) { } ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs)); + if (shouldSearchInWallet) { + txs.insert(txid); + } return true; }, startKey); } + if (shouldSearchInWallet) { + + CAmount nFee; + std::list listSent; + std::list listReceived; + + CTxDestination destination; + if (!owner.empty()) { + ExtractDestination(owner, destination); + } + + const auto& txOrdered = pwallet->wtxOrdered; + + for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) { + CWalletTx *const pwtx = (*it).second; + + if(pwtx->IsCoinBase()) { + continue; + } + const auto& txid = pwtx->GetHash(); + if (txs.count(txid)) { + continue; + } + pwtx->GetAmounts(listReceived, listSent, nFee, filter); + const auto index = LookupBlockIndex(pwtx->hashBlock); + for (auto& sent : listSent) { + if (IsValidDestination(destination) && destination != sent.destination) { + continue; + } + sent.amount = -sent.amount; + ret.push_back(outputEntryToJSON(sent, index->nHeight, pwtx->hashBlock, txid, "sent")); + } + for (auto& recv : listReceived) { + if (IsValidDestination(destination) && destination != recv.destination) { + continue; + } + ret.push_back(outputEntryToJSON(recv, index->nHeight, pwtx->hashBlock, txid, "receive")); + } + } + } + return ret; } From f51821b3d903e3b1612472b7c77cfefcaad98776 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Wed, 9 Dec 2020 18:26:29 +0000 Subject: [PATCH 2/2] RPC clearmempool --- src/rpc/blockchain.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 2db875f442..4d6d02ebe3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -529,6 +529,35 @@ static UniValue getrawmempool(const JSONRPCRequest& request) return MempoolToJSON(::mempool, fVerbose); } +static UniValue clearmempool(const JSONRPCRequest& request) +{ + RPCHelpMan("clearmempool", + "\nClears the memory pool and returns a list of the removed transactions.\n", + {}, + RPCResult{ + "[ (json array of string)\n" + " \"hash\" (string) The transaction hash\n" + " ,...\n" + "]\n" + }, + RPCExamples{ + HelpExampleCli("clearmempool", "") + + HelpExampleRpc("clearmempool", "") + } + ).Check(request); + + std::vector vtxid; + mempool.queryHashes(vtxid); + + UniValue removed(UniValue::VARR); + for (const uint256& hash : vtxid) + removed.push_back(hash.ToString()); + + mempool.clear(); + + return removed; +} + static UniValue getmempoolancestors(const JSONRPCRequest& request) { RPCHelpMan{"getmempoolancestors", @@ -2263,6 +2292,7 @@ static const CRPCCommand commands[] = { "blockchain", "getmempooldescendants", &getmempooldescendants, {"txid","verbose"} }, { "blockchain", "getmempoolentry", &getmempoolentry, {"txid"} }, { "blockchain", "getmempoolinfo", &getmempoolinfo, {} }, + { "blockchain", "clearmempool", &clearmempool, {} }, { "blockchain", "getrawmempool", &getrawmempool, {"verbose"} }, { "blockchain", "gettxout", &gettxout, {"txid","n","include_mempool"} }, { "blockchain", "gettxoutsetinfo", &gettxoutsetinfo, {} },