Browse Source

Improved keva_list_namespace implementation, made it faster and more concise.

issue_19
kevacoin 4 years ago
parent
commit
6bf95999d4
  1. 65
      src/wallet/rpckeva.cpp

65
src/wallet/rpckeva.cpp

@ -131,75 +131,46 @@ UniValue keva_list_namespaces(const JSONRPCRequest& request)
ObserveSafeMode (); ObserveSafeMode ();
std::map<std::string, std::tuple<int, bool, std::string>> mapObjects; std::map<std::string, std::string> mapObjects;
{ {
LOCK2 (cs_main, pwallet->cs_wallet); LOCK2 (cs_main, pwallet->cs_wallet);
for (const auto& item : pwallet->mapWallet) { std::set<CTxDestination> destinations;
const CWalletTx& tx = item.second; std::vector<COutput> vecOutputs;
if (!tx.tx->IsKevacoin()) { bool include_unsafe = true;
CAmount nMinimumAmount = 0;
CAmount nMaximumAmount = MAX_MONEY;
CAmount nMinimumSumAmount = MAX_MONEY;
uint64_t nMaximumCount = 0;
int nMinDepth = 1;
int nMaxDepth = 99999999;
pwallet->AvailableCoins(vecOutputs, !include_unsafe, nullptr, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
for (const COutput& out : vecOutputs) {
CTxDestination address;
const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
const CKevaScript kevaOp(scriptPubKey);
if (!kevaOp.isKevaOp()) {
continue; continue;
} }
CKevaScript kevaOp;
int nOut = -1;
for (unsigned i = 0; i < tx.tx->vout.size(); ++i) {
const CKevaScript cur(tx.tx->vout[i].scriptPubKey);
if (cur.isKevaOp()) {
if (nOut != -1) {
LogPrintf ("ERROR: wallet contains tx with multiple name outputs");
} else {
kevaOp = cur;
nOut = i;
}
}
}
if (nOut == -1) {
continue;
}
if (!kevaOp.isNamespaceRegistration() && !kevaOp.isAnyUpdate()) { if (!kevaOp.isNamespaceRegistration() && !kevaOp.isAnyUpdate()) {
continue; continue;
} }
const valtype nameSpace = kevaOp.getOpNamespace(); const valtype nameSpace = kevaOp.getOpNamespace();
const std::string nameSpaceStr = EncodeBase58Check(nameSpace); const std::string nameSpaceStr = EncodeBase58Check(nameSpace);
const CBlockIndex* pindex;
const int depth = tx.GetDepthInMainChain(pindex);
if (depth <= 0) {
continue;
}
CKevaData data; CKevaData data;
if (pcoinsTip->GetNamespace(nameSpace, data)) { if (pcoinsTip->GetNamespace(nameSpace, data)) {
const bool mine = IsMine(*pwallet, kevaOp.getAddress());
auto nsIter = mapObjects.find(nameSpaceStr);
if (nsIter == mapObjects.end() ) {
std::string displayName = ValtypeToString(data.getValue()); std::string displayName = ValtypeToString(data.getValue());
mapObjects[nameSpaceStr] = std::make_tuple(depth, mine, displayName); mapObjects[nameSpaceStr] = displayName;
} else {
// Always use the lastest one. The address may have been
// transferred to a different wallet.
const int curDepth = std::get<0>(nsIter->second);
if (depth < curDepth) {
std::string displayName = ValtypeToString(data.getValue());
mapObjects[nameSpaceStr] = std::make_tuple(depth, mine, displayName);
}
}
} }
} }
} }
UniValue res(UniValue::VARR); UniValue res(UniValue::VARR);
for (const auto& item : mapObjects) { for (const auto& item : mapObjects) {
if (std::get<1>(item.second)) {
// Only show our namespaces.
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("namespaceId", item.first); obj.pushKV("namespaceId", item.first);
obj.pushKV("displayName", std::get<2>(item.second)); obj.pushKV("displayName", item.second);
res.push_back(obj); res.push_back(obj);
} }
}
{ {
LOCK (mempool.cs); LOCK (mempool.cs);

Loading…
Cancel
Save