Browse Source

Started working on instant results.

cn
Jianping Wu 6 years ago
parent
commit
126f160722
  1. 34
      src/keva/main.cpp
  2. 18
      src/keva/main.h
  3. 8
      src/txmempool.cpp
  4. 16
      src/txmempool.h
  5. 25
      src/wallet/rpckeva.cpp
  6. 2
      src/wallet/rpcwallet.cpp

34
src/keva/main.cpp

@ -49,25 +49,51 @@ void
CKevaMemPool::addUnchecked (const uint256& hash, const CTxMemPoolEntry& entry) CKevaMemPool::addUnchecked (const uint256& hash, const CTxMemPoolEntry& entry)
{ {
AssertLockHeld (pool.cs); AssertLockHeld (pool.cs);
if (entry.isNamespaceRegistration()) { if (entry.isNamespaceRegistration()) {
const valtype& nameSpace = entry.getNamespace(); const valtype& nameSpace = entry.getNamespace();
assert(mapNamespaceRegs.count(nameSpace) == 0); assert(mapNamespaceRegs.count(nameSpace) == 0);
mapNamespaceRegs.insert(std::make_pair(nameSpace, hash)); mapNamespaceRegs.insert(std::make_pair(nameSpace, hash));
listUnconfirmedNamespaces.push_back(std::make_tuple(hash, nameSpace, entry.getDisplayName()));
} }
if (entry.isNamespaceKeyUpdate ()) { if (entry.isNamespaceKeyUpdate ()) {
const valtype& nameSpace = entry.getNamespace(); const valtype& nameSpace = entry.getNamespace();
assert(mapNamespaceUpdates.count(nameSpace) == 0); assert(mapNamespaceUpdates.count(nameSpace) == 0);
std::string(nameSpace.begin(), nameSpace.end()).c_str(), std::string(entry.getKey().begin(), entry.getKey().end()).c_str());
mapNamespaceUpdates.insert (std::make_pair(nameSpace, hash)); mapNamespaceUpdates.insert (std::make_pair(nameSpace, hash));
listUnconfirmedKeyValues.push_back(std::make_tuple(hash, nameSpace, entry.getKey(), entry.getValue()));
} }
} }
void bool
CKevaMemPool::remove (const CTxMemPoolEntry& entry) CKevaMemPool::getUnconfirmedKeyValue(const valtype& nameSpace, const valtype& key, valtype& value) const {
bool found = false;
for (auto entry : listUnconfirmedKeyValues) {
auto ns = std::get<1>(entry);
auto k = std::get<2>(entry);
if (ns == nameSpace && key == k) {
value = std::get<3>(entry);
found = true;
}
}
return found;
}
bool
CKevaMemPool::getUnconfirmedNamespaces(std::vector<std::tuple<valtype, valtype>>& nameSpaces) const {
bool found = false;
for (auto entry : listUnconfirmedNamespaces) {
auto ns = std::get<1>(entry);
auto displayName = std::get<2>(entry);
nameSpaces.push_back(std::make_tuple(ns, displayName));
found = true;
}
return found;
}
void CKevaMemPool::remove(const CTxMemPoolEntry& entry)
{ {
AssertLockHeld (pool.cs); AssertLockHeld (pool.cs);
if (entry.isNamespaceRegistration()) { if (entry.isNamespaceRegistration()) {
const NamespaceTxMap::iterator mit = mapNamespaceRegs.find(entry.getNamespace()); const NamespaceTxMap::iterator mit = mapNamespaceRegs.find(entry.getNamespace());
assert (mit != mapNamespaceRegs.end()); assert (mit != mapNamespaceRegs.end());

18
src/keva/main.h

@ -136,6 +136,18 @@ private:
*/ */
NamespaceTxMap mapNamespaceUpdates; NamespaceTxMap mapNamespaceUpdates;
/**
* Pending/unconfirmed namespaces.
* Tuple: txid, namespace, display name
*/
std::vector<std::tuple<uint256, valtype, valtype>> listUnconfirmedNamespaces;
/**
* Pending/unconfirmed key-values.
* Tuple: txid, namespace, key, value
*/
std::vector<std::tuple<uint256, valtype, valtype, valtype>> listUnconfirmedKeyValues;
/** /**
* Validate that the namespace is the hash of the first TxIn. * Validate that the namespace is the hash of the first TxIn.
*/ */
@ -232,6 +244,12 @@ public:
*/ */
bool checkTx (const CTransaction& tx) const; bool checkTx (const CTransaction& tx) const;
/** Keva get unconfirmed namespaces. */
bool getUnconfirmedNamespaces(std::vector<std::tuple<valtype, valtype>>& nameSpaces) const;
/** Keva get unconfirmed key value. */
bool getUnconfirmedKeyValue(const valtype& nameSpace, const valtype& key, valtype& value) const;
}; };
/* ************************************************************************** */ /* ************************************************************************** */

8
src/txmempool.cpp

@ -1083,4 +1083,12 @@ bool CTxMemPool::TransactionWithinChainLimit(const uint256& txid, size_t chainLi
it->GetCountWithDescendants() < chainLimit); it->GetCountWithDescendants() < chainLimit);
} }
bool CTxMemPool::getUnconfirmedKeyValue(const valtype& nameSpace, const valtype& key, valtype& value) const {
return kevaMemPool.getUnconfirmedKeyValue(nameSpace, key, value);
}
bool CTxMemPool::getUnconfirmedNamespaces(std::vector<std::tuple<valtype, valtype>>& nameSpaces) const {
return kevaMemPool.getUnconfirmedNamespaces(nameSpaces);
}
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {} SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}

16
src/txmempool.h

@ -147,11 +147,21 @@ public:
return kevaOp.getOpNamespace(); return kevaOp.getOpNamespace();
} }
inline const valtype& getDisplayName() const
{
return kevaOp.getOpNamespaceDisplayName();
}
inline const valtype& getKey() const inline const valtype& getKey() const
{ {
return kevaOp.getOpKey(); return kevaOp.getOpKey();
} }
inline const valtype& getValue() const
{
return kevaOp.getOpValue();
}
mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
}; };
@ -697,6 +707,12 @@ public:
return kevaMemPool.checkTx(tx); return kevaMemPool.checkTx(tx);
} }
/** Keva get unconfirmed key values. */
bool getUnconfirmedKeyValue(const valtype& nameSpace, const valtype& key, valtype& value) const;
/** Keva get unconfirmed namespaces. */
bool getUnconfirmedNamespaces(std::vector<std::tuple<valtype, valtype>>& nameSpaces) const;
CTransactionRef get(const uint256& hash) const; CTransactionRef get(const uint256& hash) const;
TxMempoolInfo info(const uint256& hash) const; TxMempoolInfo info(const uint256& hash) const;
std::vector<TxMempoolInfo> infoAll() const; std::vector<TxMempoolInfo> infoAll() const;

25
src/wallet/rpckeva.cpp

@ -182,6 +182,17 @@ UniValue keva_list_namespaces(const JSONRPCRequest& request)
res.push_back(item.first + " : " + item.second); res.push_back(item.first + " : " + item.second);
} }
{
LOCK (mempool.cs);
// Also get the unconfirmed ones from mempool.
std::vector<std::tuple<valtype, valtype>> unconfirmedNamespaces;
if (mempool.getUnconfirmedNamespaces(unconfirmedNamespaces)) {
for (auto entry : unconfirmedNamespaces) {
res.push_back(EncodeBase58(std::get<0>(entry)) + " : " + ValtypeToString(std::get<1>(entry)));
}
}
}
return res; return res;
} }
@ -330,9 +341,19 @@ UniValue keva_get(const JSONRPCRequest& request)
{ {
LOCK (cs_main); LOCK (cs_main);
if (!pcoinsTip->GetName(nameSpace, key, data)) { if (!pcoinsTip->GetName(nameSpace, key, data)) {
throw JSONRPCError (RPC_TRANSACTION_ERROR, "key not found"); //throw JSONRPCError (RPC_TRANSACTION_ERROR, "key not found");
}
}
auto value = data.getValue();
// Also get the unconfirmed ones.
{
LOCK (mempool.cs);
valtype val;
if (mempool.getUnconfirmedKeyValue(nameSpace, key, val)) {
value = val;
} }
} }
return ValtypeToString(data.getValue()); return ValtypeToString(value);
} }

2
src/wallet/rpcwallet.cpp

@ -1203,7 +1203,7 @@ UniValue sendmany(const JSONRPCRequest& request)
CAmount nFeeRequired = 0; CAmount nFeeRequired = 0;
int nChangePosRet = -1; int nChangePosRet = -1;
std::string strFailReason; std::string strFailReason;
std::vector<unsigned char> empty; empty; std::vector<unsigned char> empty;
bool fCreated = pwallet->CreateTransaction(vecSend, nullptr, empty, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control); bool fCreated = pwallet->CreateTransaction(vecSend, nullptr, empty, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
if (!fCreated) if (!fCreated)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason); throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);

Loading…
Cancel
Save