Browse Source

Changed HasNamespace to GetNamespace. Fixed BackedView error.

Started working on keva_put.
cn
Jianping Wu 6 years ago
parent
commit
45fecd334e
  1. 21
      src/coins.cpp
  2. 8
      src/coins.h
  3. 8
      src/keva/common.cpp
  4. 5
      src/keva/common.h
  5. 5
      src/txdb.cpp
  6. 1
      src/txdb.h
  7. 42
      src/wallet/rpckeva.cpp

21
src/coins.cpp

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
bool CCoinsView::HasNamespace(const valtype &nameSpace) const { return false; }
bool CCoinsView::GetNamespace(const valtype &nameSpace, CKevaData &data) const { return false; }
bool CCoinsView::GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const { return false; }
bool CCoinsView::GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const { return false; }
CNameIterator* CCoinsView::IterateNames() const { assert (false); }
@ -29,9 +29,15 @@ bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { re @@ -29,9 +29,15 @@ bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { re
bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
bool CCoinsViewBacked::HasNamespace(const valtype &nameSpace) const { return false; }
bool CCoinsViewBacked::GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const { return false; }
bool CCoinsViewBacked::GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const { return false; }
bool CCoinsViewBacked::GetNamespace(const valtype &nameSpace, CKevaData &data) const {
return base->GetNamespace(nameSpace, data);
}
bool CCoinsViewBacked::GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const {
return base->GetName(nameSpace, key, data);
}
bool CCoinsViewBacked::GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const {
return base->GetNamesForHeight(nHeight, names);
}
CNameIterator* CCoinsViewBacked::IterateNames() const { return base->IterateNames(); }
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CKevaCache &names) {
@ -154,8 +160,11 @@ void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) { @@ -154,8 +160,11 @@ void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
hashBlock = hashBlockIn;
}
bool CCoinsViewCache::HasNamespace(const valtype &nameSpace) const {
return cacheNames.hasNamespace(nameSpace);
bool CCoinsViewCache::GetNamespace(const valtype &nameSpace, CKevaData &data) const {
if (cacheNames.GetNamespace(nameSpace, data)) {
return true;
}
return base->GetNamespace(nameSpace, data);
}
bool CCoinsViewCache::GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const {

8
src/coins.h

@ -165,7 +165,7 @@ public: @@ -165,7 +165,7 @@ public:
virtual std::vector<uint256> GetHeadBlocks() const;
// Check if a namespace exists.
virtual bool HasNamespace(const valtype& nameSpace) const;
virtual bool GetNamespace(const valtype& nameSpace, CKevaData& data) const;
// Get a name (if it exists)
virtual bool GetName(const valtype& nameSpace, const valtype& key, CKevaData& data) const;
@ -206,7 +206,7 @@ public: @@ -206,7 +206,7 @@ public:
bool HaveCoin(const COutPoint &outpoint) const override;
uint256 GetBestBlock() const override;
std::vector<uint256> GetHeadBlocks() const override;
bool HasNamespace(const valtype& nameSpace) const override;
bool GetNamespace(const valtype& nameSpace, CKevaData& data) const override;
bool GetName(const valtype& nameSpace, const valtype& key, CKevaData& data) const override;
bool GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const override;
CNameIterator* IterateNames() const override;
@ -248,8 +248,8 @@ public: @@ -248,8 +248,8 @@ public:
bool HaveCoin(const COutPoint &outpoint) const override;
uint256 GetBestBlock() const override;
void SetBestBlock(const uint256 &hashBlock);
bool HasNamespace(const valtype &nameSpace) const override;
bool GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const override;
bool GetNamespace(const valtype &nameSpace, CKevaData& data) const override;
bool GetName(const valtype &nameSpace, const valtype &key, CKevaData& data) const override;
bool GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const override;
CNameIterator* IterateNames() const override;
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CKevaCache &names) override;

8
src/keva/common.cpp

@ -194,10 +194,10 @@ CKevaCache::get(const valtype& nameSpace, const valtype& key, CKevaData& data) c @@ -194,10 +194,10 @@ CKevaCache::get(const valtype& nameSpace, const valtype& key, CKevaData& data) c
return true;
}
bool CKevaCache::hasNamespace(const valtype& nameSpace) const
bool
CKevaCache::GetNamespace(const valtype& nameSpace, CKevaData& data) const
{
auto ni = namespaces.find(nameSpace);
return (ni != namespaces.end());
return get(nameSpace, ValtypeFromString(CKevaScript::KEVA_DISPLAY_NAME_KEY), data);
}
void
@ -215,8 +215,6 @@ CKevaCache::set(const valtype& nameSpace, const valtype& key, const CKevaData& d @@ -215,8 +215,6 @@ CKevaCache::set(const valtype& nameSpace, const valtype& key, const CKevaData& d
ei->second = data;
else
entries.insert (std::make_pair(name, data));
namespaces.insert(nameSpace);
}
void

5
src/keva/common.h

@ -379,7 +379,6 @@ public: @@ -379,7 +379,6 @@ public:
* by the unit tests.
*/
typedef std::map<std::tuple<valtype, valtype>, CKevaData, NameComparator> EntryMap;
typedef std::set<valtype> NamespaceSet;
private:
@ -387,8 +386,6 @@ private: @@ -387,8 +386,6 @@ private:
EntryMap entries;
/** Deleted names. */
std::set<valtype> deleted;
/** Namespaces */
NamespaceSet namespaces;
#if 0
/**
@ -455,7 +452,7 @@ public: @@ -455,7 +452,7 @@ public:
in entries, and doesn't care about deleted data. */
bool get(const valtype& nameSpace, const valtype& key, CKevaData& data) const;
bool hasNamespace(const valtype& nameSpace) const;
bool GetNamespace(const valtype& nameSpace, CKevaData& data) const;
/* Insert (or update) a name. If it is marked as "deleted", this also
removes the "deleted" mark. */

5
src/txdb.cpp

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
#include <util.h>
#include <ui_interface.h>
#include <init.h>
#include <script/keva.h>
#include <stdint.h>
@ -142,6 +143,10 @@ CNameIterator* CCoinsViewDB::IterateNames() const { @@ -142,6 +143,10 @@ CNameIterator* CCoinsViewDB::IterateNames() const {
return new CDbNameIterator(db);
}
bool CCoinsViewDB::GetNamespace(const valtype &nameSpace, CKevaData &data) const {
return db.Read(std::make_pair(DB_NAME, std::make_pair(nameSpace, CKevaScript::KEVA_DISPLAY_NAME_KEY)), data);
}
bool CCoinsViewDB::GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const {
return db.Read(std::make_pair(DB_NAME, std::make_pair(nameSpace, key)), data);
}

1
src/txdb.h

@ -76,6 +76,7 @@ public: @@ -76,6 +76,7 @@ public:
bool HaveCoin(const COutPoint &outpoint) const override;
uint256 GetBestBlock() const override;
std::vector<uint256> GetHeadBlocks() const override;
bool GetNamespace(const valtype &nameSpace, CKevaData &data) const override;
bool GetName(const valtype &nameSpace, const valtype &key, CKevaData &data) const override;
bool GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) const override;
CNameIterator* IterateNames() const override;

42
src/wallet/rpckeva.cpp

@ -138,11 +138,6 @@ UniValue keva_put(const JSONRPCRequest& request) @@ -138,11 +138,6 @@ UniValue keva_put(const JSONRPCRequest& request)
if (value.size () > MAX_VALUE_LENGTH)
throw JSONRPCError (RPC_INVALID_PARAMETER, "the value is too long");
bool createNamespace = false;
if (!request.params[3].isNull()) {
createNamespace = request.params[3].get_bool();
}
/* Reject updates to a name for which the mempool already has
a pending update. This is not a hard rule enforced by network
rules, but it is necessary with the current mempool implementation. */
@ -157,52 +152,39 @@ UniValue keva_put(const JSONRPCRequest& request) @@ -157,52 +152,39 @@ UniValue keva_put(const JSONRPCRequest& request)
CKevaData oldData;
{
LOCK (cs_main);
if (!pcoinsTip->HasNamespace(nameSpace)) {
if (!pcoinsTip->GetNamespace(nameSpace, oldData)) {
throw JSONRPCError (RPC_TRANSACTION_ERROR,
"this name can not be updated");
}
}
const COutPoint outp = oldData.getUpdateOutpoint ();
const COutPoint outp = oldData.getUpdateOutpoint();
const CTxIn txIn(outp);
/* No more locking required, similarly to name_new. */
EnsureWalletIsUnlocked (pwallet);
EnsureWalletIsUnlocked(pwallet);
CReserveKey keyName(pwallet);
CPubKey pubKeyReserve;
const bool ok = keyName.GetReservedKey (pubKeyReserve, true);
assert (ok);
const bool ok = keyName.GetReservedKey(pubKeyReserve, true);
assert(ok);
bool usedKey = false;
CScript addrName;
if (request.params.size () == 3)
{
keyName.ReturnKey ();
const CTxDestination dest
= DecodeDestination (request.params[2].get_str ());
if (!IsValidDestination (dest))
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, "invalid address");
addrName = GetScriptForDestination (dest);
}
else
{
usedKey = true;
addrName = GetScriptForDestination (pubKeyReserve.GetID ());
}
usedKey = true;
addrName = GetScriptForDestination(pubKeyReserve.GetID());
const CScript nameScript
= CKevaScript::buildKevaPut(addrName, nameSpace, key, value);
const CScript kevaScript = CKevaScript::buildKevaPut(addrName, nameSpace, key, value);
CCoinControl coinControl;
CWalletTx wtx;
SendMoneyToScript(pwallet, nameScript, &txIn,
SendMoneyToScript(pwallet, kevaScript, &txIn,
KEVA_LOCKED_AMOUNT, false, wtx, coinControl);
if (usedKey)
if (usedKey) {
keyName.KeepKey ();
}
return wtx.GetHash ().GetHex ();
return wtx.GetHash().GetHex();
}

Loading…
Cancel
Save