Browse Source

Handled group join/leave in mempool.

views
Just Wonder 4 years ago
parent
commit
cc0512bbe3
  1. 78
      src/rpc/rpckeva_nonwallet.cpp

78
src/rpc/rpckeva_nonwallet.cpp

@ -147,6 +147,22 @@ enum InitiatorType : int
INITIATOR_TYPE_OTHER INITIATOR_TYPE_OTHER
}; };
// Check if the key has the format _g:NamespaceId. If yes,
// return the namespace.
bool isNamespaceGroup(const valtype& key, valtype& targetNS)
{
std::string keyStr = ValtypeToString(key);
if (keyStr.rfind(CKevaData::ASSOCIATE_PREFIX, 0) != 0) {
return false;
}
keyStr.erase(0, CKevaData::ASSOCIATE_PREFIX.length());
if (!DecodeKevaNamespace(keyStr, Params(), targetNS)) {
return false;
}
return true;
}
void getNamespaceGroup(const valtype& nameSpace, std::set<valtype>& namespaces, const InitiatorType type) void getNamespaceGroup(const valtype& nameSpace, std::set<valtype>& namespaces, const InitiatorType type)
{ {
CKevaData data; CKevaData data;
@ -165,22 +181,15 @@ void getNamespaceGroup(const valtype& nameSpace, std::set<valtype>& namespaces,
// Find the namespace connection initialized by us. // Find the namespace connection initialized by us.
std::unique_ptr<CKevaIterator> iterKeys(pcoinsTip->IterateKeys(nameSpace)); std::unique_ptr<CKevaIterator> iterKeys(pcoinsTip->IterateKeys(nameSpace));
valtype targetNS;
valtype key; valtype key;
while (iterKeys->next(key, data)) { while (iterKeys->next(key, data)) {
valtype targetNS;
// Find the value with the format _g:NamespaceId // Find the value with the format _g:NamespaceId
std::string keyStr = ValtypeToString(key); if (isNamespaceGroup(key, targetNS)) {
if (keyStr.rfind(CKevaData::ASSOCIATE_PREFIX, 0) != 0) {
continue;
}
keyStr.erase(0, CKevaData::ASSOCIATE_PREFIX.length());
if (!DecodeKevaNamespace(keyStr, Params(), targetNS)) {
continue;
}
namespaces.insert(targetNS); namespaces.insert(targetNS);
} }
} }
}
UniValue keva_group_get(const JSONRPCRequest& request) UniValue keva_group_get(const JSONRPCRequest& request)
{ {
@ -763,21 +772,59 @@ UniValue keva_group_show(const JSONRPCRequest& request)
} }
} }
// Find the namespace connection initialized by us. // Find the namespace connection initialized by us, and not confirmed yet.
{
LOCK (mempool.cs);
std::vector<std::tuple<valtype, valtype, valtype, uint256>> unconfirmedKeyValueList;
mempool.getUnconfirmedKeyValueList(unconfirmedKeyValueList, nameSpace);
std::set<valtype> nsList;
valtype targetNS;
for (auto entry: unconfirmedKeyValueList) {
const valtype ns = std::get<0>(entry);
if (ns != nameSpace) {
continue;
}
const valtype key = std::get<1>(entry);
// Find the value with the format _g:NamespaceId
if (!isNamespaceGroup(key, targetNS)) {
continue;
}
valtype val;
if (mempool.getUnconfirmedKeyValue(nameSpace, key, val) && val.size() > 0) {
CKevaData nsData;
valtype nsName;
if (pcoinsTip->GetName(targetNS, nsDisplayKey, nsData)) {
nsName = nsData.getValue();
}
UniValue obj(UniValue::VOBJ);
obj.pushKV("namespaceId", EncodeBase58Check(targetNS));
obj.pushKV("display_name", ValtypeToString(nsName));
obj.pushKV("height", -1);
obj.pushKV("initiator", false);
namespaces.push_back(obj);
}
}
}
// Find the namespace connection initialized by us and confirmed.
std::unique_ptr<CKevaIterator> iterKeys(pcoinsTip->IterateKeys(nameSpace)); std::unique_ptr<CKevaIterator> iterKeys(pcoinsTip->IterateKeys(nameSpace));
valtype targetNS; valtype targetNS;
valtype key; valtype key;
while (iterKeys->next(key, data)) { while (iterKeys->next(key, data)) {
// Find the value with the format _g:NamespaceId // Find the value with the format _g:NamespaceId
std::string keyStr = ValtypeToString(key); if (!isNamespaceGroup(key, targetNS)) {
if (keyStr.rfind(CKevaData::ASSOCIATE_PREFIX, 0) != 0) {
continue; continue;
} }
keyStr.erase(0, CKevaData::ASSOCIATE_PREFIX.length());
if (!DecodeKevaNamespace(keyStr, Params(), targetNS)) { // If it has been removed but not yet confirmed, skip it anyway.
{
LOCK (mempool.cs);
valtype val;
if (mempool.getUnconfirmedKeyValue(nameSpace, key, val) && val.size() == 0) {
continue; continue;
} }
}
const int age = chainActive.Height() - data.getHeight(); const int age = chainActive.Height() - data.getHeight();
assert(age >= 0); assert(age >= 0);
@ -811,7 +858,6 @@ UniValue keva_group_show(const JSONRPCRequest& request)
} }
} }
if (stats) { if (stats) {
UniValue res(UniValue::VOBJ); UniValue res(UniValue::VOBJ);
res.pushKV("blocks", chainActive.Height()); res.pushKV("blocks", chainActive.Height());

Loading…
Cancel
Save