Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable filter by tx type in listaccounthistory rpc method #120

Merged
merged 7 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2981,11 +2981,15 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
{"maxBlockHeight", RPCArg::Type::NUM, RPCArg::Optional::OMITTED,
"Optional height to iterate from (downto genesis block), (default = chaintip)."},
{"depth", RPCArg::Type::NUM, RPCArg::Optional::OMITTED,
"Maximum depth, 100 blocks by default for every account"},
"Maximum depth, 100 blocks by default for every account, if limit is set it does nothing"},
{"no_rewards", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED,
"Filter out rewards"},
{"token", RPCArg::Type::STR, RPCArg::Optional::OMITTED,
"Filter by token"},
{"txtype", RPCArg::Type::STR, RPCArg::Optional::OMITTED,
"Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG'"},
{"limit", RPCArg::Type::NUM, RPCArg::Optional::OMITTED,
"Maximum number of records to return, 100 by default"},
},
},
},
Expand All @@ -3007,6 +3011,8 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
uint32_t depth = 100;
bool noRewards = false;
std::string tokenFilter;
uint32_t limit = 100;
auto txType = CustomTxType::None;

if (request.params.size() > 1) {
UniValue optionsObj = request.params[1].get_obj();
Expand All @@ -3016,6 +3022,8 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
{"depth", UniValueType(UniValue::VNUM)},
{"no_rewards", UniValueType(UniValue::VBOOL)},
{"token", UniValueType(UniValue::VSTR)},
{"txtype", UniValueType(UniValue::VSTR)},
{"limit", UniValueType(UniValue::VNUM)},
}, true, true);

if (!optionsObj["maxBlockHeight"].isNull()) {
Expand All @@ -3032,10 +3040,26 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
if (!optionsObj["token"].isNull()) {
tokenFilter = optionsObj["token"].get_str();
}

if (!optionsObj["txtype"].isNull()) {
const auto str = optionsObj["txtype"].get_str();
if (str.size() == 1) {
txType = CustomTxCodeToType(str[0]);
}
}
if (!optionsObj["limit"].isNull()) {
limit = (uint32_t) optionsObj["limit"].get_int64();
depth = std::numeric_limits<decltype(depth)>::max();
}
if (limit == 0) {
limit = std::numeric_limits<decltype(limit)>::max();
ShengguangXiao marked this conversation as resolved.
Show resolved Hide resolved
}
}

pwallet->BlockUntilSyncedToCurrentChain();
startBlock = std::min(startBlock, uint32_t(chainHeight(*pwallet->chain().lock())));
depth = std::min(depth, std::numeric_limits<decltype(depth)>::max() - startBlock - 1);
depth += startBlock + 1;

UniValue ret(UniValue::VARR);

Expand All @@ -3045,9 +3069,13 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
bool isMine = false;
AccountHistoryKey startKey{ prevOwner, startBlock, std::numeric_limits<uint32_t>::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)))
if (height > startKey.blockHeight || depth <= startKey.blockHeight)
return true; // continue

if (CustomTxType::None != txType && category != uint8_t(txType)) {
return true;
}

if(!tokenFilter.empty()) {
bool hasToken = false;
for (auto const & diff : diffs) {
Expand Down Expand Up @@ -3078,19 +3106,24 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {

if (isMine) {
ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs));
--limit;
}

return true;
return limit != 0;
}, startKey);
}
else if (accounts == "all") {
// traversing the whole DB, skipping wrong heights
AccountHistoryKey startKey{ CScript{}, startBlock, std::numeric_limits<uint32_t>::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))) {
if (height > startKey.blockHeight || depth <= startKey.blockHeight) {
return true; // continue
}

if (CustomTxType::None != txType && category != uint8_t(txType)) {
return true;
}

if(!tokenFilter.empty()) {
bool hasToken = false;
for (auto const & diff : diffs) {
Expand All @@ -3115,7 +3148,7 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
}

ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs));
return true;
return --limit != 0;
}, startKey);

}
Expand All @@ -3125,9 +3158,13 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {

AccountHistoryKey startKey{ owner, startBlock, std::numeric_limits<uint32_t>::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 (owner != startKey.owner || (height > startKey.blockHeight || (depth <= startKey.blockHeight && (height < startKey.blockHeight - depth))))
if (owner != startKey.owner || height > startKey.blockHeight || depth <= startKey.blockHeight)
return false;

if (CustomTxType::None != txType && category != uint8_t(txType)) {
return true;
}

if(!tokenFilter.empty()) {
bool hasToken = false;
for (auto const & diff : diffs) {
Expand All @@ -3152,7 +3189,7 @@ UniValue listaccounthistory(const JSONRPCRequest& request) {
}

ret.push_back(accounthistoryToJSON(owner, height, txn, txid, category, diffs));
return true;
return --limit != 0;
}, startKey);
}

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_tokens_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def run_test(self):
assert_equal(self.nodes[0].listtokens()['128']['destructionHeight'], -1)
assert_equal(self.nodes[0].listtokens()['128']['destructionTx'], '0000000000000000000000000000000000000000000000000000000000000000')

# Create new neither mintable nor tradable token
# Create new neither mintable nor tradable token
self.nodes[0].createtoken({
"symbol": "WK",
"name": "weak",
Expand Down