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

Add listnames RPC command for dotNav #937

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 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
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ libnavcoin_consensus_a_SOURCES = \
ctokens/tokenid.h \
dotnav/namedata.h \
dotnav/namerecord.h \
dotnav/namerecordname.h \
dotnav/names.h \
dotnav/names.cpp \
aes_helper.c \
Expand Down
219 changes: 201 additions & 18 deletions src/coins.cpp

Large diffs are not rendered by default.

100 changes: 81 additions & 19 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <consensus/dao.h>
#include <ctokens/ctokens.h>
#include <ctokens/tokenutxos.h>
#include <dotnav/namerecord.h>
#include <dotnav/namedata.h>
#include <dotnav/namerecord.h>
#include <dotnav/namerecord.h>
#include <dotnav/namerecordname.h>

#include <assert.h>
#include <stdint.h>
Expand Down Expand Up @@ -395,6 +397,10 @@ class CStateView
virtual bool GetAllNameRecords(NameRecordMap& map);
virtual bool HaveNameRecord(const uint256 &id) const;

virtual bool GetNameRecordName(const uint256 &id, NameRecordNameValue& name) const;
virtual bool GetAllNameRecordNames(NameRecordNameMap& map);
virtual bool HaveNameRecordName(const uint256 &id) const;

virtual bool GetNameData(const uint256& id, NameDataValues& data);
virtual bool HaveNameData(const uint256& id) const;

Expand All @@ -406,12 +412,21 @@ class CStateView

//! Do a bulk modification (multiple CCoins changes + BestBlock change).
//! The passed mapCoins can be modified.
virtual bool BatchWrite(CCoinsMap &mapCoins, CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests, CVoteMap &mapVotes,
CConsultationMap &mapConsultations, CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus, TokenMap& mapTokens, TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords, NameDataMap& mapNameData,
const uint256 &hashBlock, const int &nCacheExcludeVotes);
virtual bool BatchWrite(
CCoinsMap &mapCoins,
CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests,
CVoteMap &mapVotes,
CConsultationMap &mapConsultations,
CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus,
TokenMap& mapTokens,
TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords,
NameRecordNameMap& mapNameRecordNames,
NameDataMap& mapNameData,
const uint256 &hashBlock,
const int &nCacheExcludeVotes);

//! Get a cursor to iterate over the whole state
virtual CStateViewCursor *Cursor() const;
Expand Down Expand Up @@ -459,6 +474,10 @@ class CStateViewBacked : public CStateView
bool GetAllNameRecords(NameRecordMap& map);
bool HaveNameRecord(const uint256 &id) const;

bool GetNameRecordName(const uint256 &id, NameRecordNameValue& name) const;
bool GetAllNameRecordNames(NameRecordNameMap& map);
bool HaveNameRecordName(const uint256 &id) const;

bool GetNameData(const uint256 &id, NameDataValues& data);
bool HaveNameData(const uint256 &id) const;

Expand All @@ -467,12 +486,21 @@ class CStateViewBacked : public CStateView

uint256 GetBestBlock() const;
void SetBackend(CStateView &viewIn);
bool BatchWrite(CCoinsMap &mapCoins, CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests, CVoteMap &mapVotes,
CConsultationMap &mapConsultations, CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus, TokenMap& mapTokens, TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords, NameDataMap& mapNameData,
const uint256 &hashBlock, const int &nCacheExcludeVotes);
bool BatchWrite(
CCoinsMap &mapCoins,
CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests,
CVoteMap &mapVotes,
CConsultationMap &mapConsultations,
CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus,
TokenMap& mapTokens,
TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords,
NameRecordNameMap& mapNameRecordNames,
NameDataMap& mapNameData,
const uint256 &hashBlock,
const int &nCacheExcludeVotes);
CStateViewCursor *Cursor() const;
};

Expand Down Expand Up @@ -628,6 +656,22 @@ class NameRecordModifier
friend class CStateViewCache;
};

class NameRecordNameModifier
{
private:
CStateViewCache& cache;
NameRecordNameMap::iterator it;
NameRecordNameModifier(CStateViewCache& cache_, NameRecordNameMap::iterator it_, int height=0);
NameRecordNameValue prev;
int height;

public:
NameRecordNameValue* operator->() { return &it->second; }
NameRecordNameValue& operator*() { return it->second; }
~NameRecordNameModifier();
friend class CStateViewCache;
};

class NameDataModifier
{
private:
Expand Down Expand Up @@ -683,6 +727,7 @@ class CStateViewCache : public CStateViewBacked
mutable TokenMap cacheTokens;
mutable TokenUtxoMap cacheTokenUtxos;
mutable NameRecordMap cacheNameRecords;
mutable NameRecordNameMap cacheNameRecordNames;
mutable NameDataMap cacheNameData;
mutable int nCacheExcludeVotes;

Expand All @@ -705,6 +750,7 @@ class CStateViewCache : public CStateViewBacked
bool HaveToken(const uint256& id) const;
bool HaveTokenUtxos(const uint256 &id) const;
bool HaveNameRecord(const uint256& id) const;
bool HaveNameRecordName(const uint256& id) const;
bool HaveNameData(const uint256& id) const;
bool GetProposal(const uint256 &txid, CProposal &proposal) const;
bool GetPaymentRequest(const uint256 &txid, CPaymentRequest &prequest) const;
Expand All @@ -715,6 +761,7 @@ class CStateViewCache : public CStateViewBacked
bool GetToken(const uint256& pid, TokenInfo& token) const;
bool GetTokenUtxos(const uint256 &id, TokenUtxoValues &tokenUtxos);
bool GetNameRecord(const uint256& pid, NameRecordValue& height) const;
bool GetNameRecordName(const uint256& pid, NameRecordNameValue& name) const;
bool GetNameData(const uint256& pid, NameDataValues& data);
bool GetAllProposals(CProposalMap& map);
bool GetAllPaymentRequests(CPaymentRequestMap& map);
Expand All @@ -723,22 +770,33 @@ class CStateViewCache : public CStateViewBacked
bool GetAllConsultationAnswers(CConsultationAnswerMap& map);
bool GetAllTokens(TokenMap& map);
bool GetAllNameRecords(NameRecordMap& map);
bool GetAllNameRecordNames(NameRecordNameMap& map);
bool GetAnswersForConsultation(CConsultationAnswerMap& map, const uint256& parent);
uint256 GetBestBlock() const;
void SetBestBlock(const uint256 &hashBlock);
bool BatchWrite(CCoinsMap &mapCoins, CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests, CVoteMap &mapVotes,
CConsultationMap &mapConsultations, CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus, TokenMap& mapTokens, TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords, NameDataMap& mapNameData,
const uint256 &hashBlockIn, const int &nCacheExcludeVotes);
bool BatchWrite(
CCoinsMap &mapCoins,
CProposalMap &mapProposals,
CPaymentRequestMap &mapPaymentRequests,
CVoteMap &mapVotes,
CConsultationMap &mapConsultations,
CConsultationAnswerMap &mapAnswers,
CConsensusParameterMap& mapConsensus,
TokenMap& mapTokens,
TokenUtxoMap& mapTokenUtxos,
NameRecordMap& mapNameRecords,
NameRecordNameMap& mapNameRecordNames,
NameDataMap& mapNameData,
const uint256 &hashBlockIn,
const int &nCacheExcludeVotes);
bool AddProposal(const CProposal& proposal) const;
bool AddPaymentRequest(const CPaymentRequest& prequest) const;
bool AddCachedVoter(const CVoteMapKey &voter, CVoteMapValue& vote) const;
bool AddConsultation(const CConsultation& consultation) const;
bool AddToken(const Token& token) const;
bool AddTokenUtxo(const uint256 &id, const TokenUtxoEntry& utxo) const;
bool AddNameRecord(const NameRecord& record) const;
bool AddNameRecordName(const NameRecordName& name) const;
bool AddNameData(const uint256& id, const NameDataEntry& record) const;
bool AddConsultationAnswer(const CConsultationAnswer& answer);
bool RemoveProposal(const uint256 &pid) const;
Expand All @@ -747,6 +805,7 @@ class CStateViewCache : public CStateViewBacked
bool RemoveToken(const uint256 &pid) const;
bool RemoveTokenUtxo(const TokenUtxoKey &key) const;
bool RemoveNameRecord(const uint256 &pid) const;
bool RemoveNameRecordName(const uint256 &id, const int64_t& height) const;
bool RemoveNameData(const NameDataKey &id) const;
bool RemoveConsultation(const uint256 &cid);
bool RemoveConsultationAnswer(const uint256 &cid);
Expand Down Expand Up @@ -786,6 +845,7 @@ class CStateViewCache : public CStateViewBacked
TokenModifier ModifyToken(const uint256 &id, int nHeight = 0);
TokenUtxosModifier ModifyTokenUtxos(const uint256 &id, int blockHeight = 0);
NameRecordModifier ModifyNameRecord(const uint256 &id, int nHeight = 0);
NameRecordNameModifier ModifyNameRecordName(const uint256 &id, int nHeight = 0);
NameDataModifier ModifyNameData(const uint256& id, int nHeight = 0);

/**
Expand Down Expand Up @@ -850,6 +910,7 @@ class CStateViewCache : public CStateViewBacked
friend class TokenModifier;
friend class TokenUtxosModifier;
friend class NameRecordModifier;
friend class NameRecordNameModifier;
friend class NameDataModifier;

private:
Expand All @@ -864,6 +925,7 @@ class CStateViewCache : public CStateViewBacked
TokenMap::const_iterator FetchToken(const uint256 &id) const;
TokenUtxoMap::const_iterator FetchTokenUtxos(const uint256 &id) const;
NameRecordMap::const_iterator FetchNameRecord(const uint256 &id) const;
NameRecordNameMap::const_iterator FetchNameRecordName(const uint256 &id) const;
NameDataMap::const_iterator FetchNameData(const uint256 &id) const;

/**
Expand Down
62 changes: 62 additions & 0 deletions src/dotnav/namerecordname.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2021 The Navcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef NAVCOIN_NAME_RECORD_NAME_H
#define NAVCOIN_NAME_RECORD_NAME_H

#include <uint256.h>

class NameRecordNameValue {
public:
std::string domain;
std::string subdomain;
int64_t height;

NameRecordNameValue() {
SetNull();
}

NameRecordNameValue(const std::string& domain_, const std::string& subdomain_, const int64_t& height_) {
domain = domain_;
subdomain = subdomain_;
height = height_;
};

void SetNull() {
domain = "";
subdomain = "";
height = 0;
}

bool IsNull() const {
return domain == "";
}

bool operator==(const NameRecordNameValue& other) {
return (domain == other.domain && subdomain == other.subdomain && height == other.height);
}

void swap(NameRecordNameValue &to) {
std::swap(to.domain, domain);
std::swap(to.subdomain, subdomain);
std::swap(to.height, height);
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(domain);
READWRITE(subdomain);
READWRITE(height);
}
};

typedef std::map<uint256, NameRecordNameValue> NameRecordNameMap;
typedef std::pair<uint256, NameRecordNameValue> NameRecordName;


#endif // NAVCOIN_NAME_RECORD_NAME_H
7 changes: 7 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-allindex", strprintf(_("Maintain all indexes supported (default: %u)"), DEFAULT_ALLINDEX));
strUsage += HelpMessageOpt("-txindex", strprintf(_("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)"), DEFAULT_TXINDEX));
strUsage += HelpMessageOpt("-nftindex", strprintf(_("Maintain an index of ntf data, used by the nft related rpc calls (default: %u)"), DEFAULT_NFTINDEX));
strUsage += HelpMessageOpt("-nameindex", strprintf(_("Maintain an index of names, used by the `listnames` rpc call (default: %u)"), DEFAULT_NAMEINDEX));

strUsage += HelpMessageOpt("-addressindex", strprintf(_("Maintain a full address index, used to query for the balance, txids and unspent outputs for addresses (default: %u)"), DEFAULT_ADDRESSINDEX));
strUsage += HelpMessageOpt("-timestampindex", strprintf(_("Maintain a timestamp index for block hashes, used to query blocks hashes by a range of timestamps (default: %u)"), DEFAULT_TIMESTAMPINDEX));
Expand Down Expand Up @@ -1636,6 +1637,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler, const std
break;
}

// Check for changed -nameindex state
if (fNameIndex != GetBoolArg("-nameindex", DEFAULT_NAMEINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -nameindex");
break;
}

// Check for changed -addressindex state
if (fAddressIndex != GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -addressindex");
Expand Down
26 changes: 25 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool fReindex = false;
bool fVerifyChain = false;
bool fTxIndex = false;
bool fNftIndex = false;
bool fNameIndex = false;
bool fAddressIndex = false;
bool fTimestampIndex = false;
bool fSpentIndex = false;
Expand Down Expand Up @@ -2994,6 +2995,13 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
view.RemoveNameData(NameDataKey(program.sParameters[0], pindex->nHeight));
LogPrint("dotnav", "%s: removing name data for %s %d\n", __func__, program.sParameters[0], pindex->nHeight);
}

if (fNameIndex) {
if (program.action == UPDATE_NAME_FIRST || program.action == UPDATE_NAME || program.action == RENEW_NAME) {
if (view.HaveNameRecordName(DotNav::GetHashName(program.sParameters[1] + "." + program.sParameters[0])))
view.RemoveNameRecordName(DotNav::GetHashName(program.sParameters[1] + "." + program.sParameters[0]), pindex->nHeight);
}
}
}
} catch(...) {
return state.DoS(100, false, REJECT_INVALID, "error-program-vdata");
Expand Down Expand Up @@ -4995,6 +5003,13 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin

LogPrint("dotnav", "%s: updated name %s %s %s %s\n", __func__, program.sParameters[1], program.sParameters[0], program.sParameters[2], program.sParameters[3]);
}

if (fNameIndex) {
if (program.action == UPDATE_NAME_FIRST || program.action == UPDATE_NAME || program.action == RENEW_NAME) {
if (!view.HaveNameRecordName(DotNav::GetHashName(program.sParameters[1] + "." + program.sParameters[0])))
view.AddNameRecordName(std::make_pair(DotNav::GetHashName(program.sParameters[1] + "." + program.sParameters[0]), NameRecordNameValue(program.sParameters[0], program.sParameters[1], pindex->nHeight)));
}
}
}
} catch(...) {
return state.DoS(100, false, REJECT_INVALID, "error-program-vdata");
Expand Down Expand Up @@ -7352,6 +7367,10 @@ bool static LoadBlockIndexDB()
pblocktree->ReadFlag("nftindex", fNftIndex);
LogPrintf("%s: nft index %s\n", __func__, fNftIndex ? "enabled" : "disabled");

// Check whether we have a name index
pblocktree->ReadFlag("nameindex", fNameIndex);
LogPrintf("%s: name index %s\n", __func__, fNameIndex ? "enabled" : "disabled");

// Check whether we have an address index
pblocktree->ReadFlag("addressindex", fAddressIndex);
LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
Expand Down Expand Up @@ -7776,14 +7795,15 @@ bool InitBlockIndex(const CChainParams& chainparams)
// Load the flag values | use DEFAULT_* values if not set
fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
fNftIndex = GetBoolArg("-nftindex", DEFAULT_NFTINDEX);
fNameIndex = GetBoolArg("-nameindex", DEFAULT_NAMEINDEX);
fAddressIndex = GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX);
fTimestampIndex = GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX);
fSpentIndex = GetBoolArg("-spentindex", DEFAULT_SPENTINDEX);

// Check if we want all indexes
if (GetBoolArg("-allindex", DEFAULT_ALLINDEX))
{
fTxIndex = fNftIndex = fAddressIndex = fTimestampIndex = fSpentIndex = true;
fTxIndex = fNftIndex = fNameIndex = fAddressIndex = fTimestampIndex = fSpentIndex = true;
}

// Use the provided setting for -txindex in the new database
Expand All @@ -7794,6 +7814,10 @@ bool InitBlockIndex(const CChainParams& chainparams)
pblocktree->WriteFlag("nftindex", fNftIndex);
LogPrintf("%s: nft index %s\n", __func__, fNftIndex ? "enabled" : "disabled");

// Use the provided setting for -nameindex in the new database
pblocktree->WriteFlag("nameindex", fNameIndex);
LogPrintf("%s: name index %s\n", __func__, fNameIndex ? "enabled" : "disabled");

// Use the provided setting for -addressindex in the new database
pblocktree->WriteFlag("addressindex", fAddressIndex);
LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
Expand Down
2 changes: 2 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
static const bool DEFAULT_ALLINDEX = false;
static const bool DEFAULT_TXINDEX = false;
static const bool DEFAULT_NFTINDEX = false;
static const bool DEFAULT_NAMEINDEX = false;
static const bool DEFAULT_ADDRESSINDEX = false;
static const bool DEFAULT_TIMESTAMPINDEX = false;
static const bool DEFAULT_SPENTINDEX = false;
Expand Down Expand Up @@ -206,6 +207,7 @@ extern bool fVerifyChain;
extern int nScriptCheckThreads;
extern bool fTxIndex;
extern bool fNftIndex;
extern bool fNameIndex;
extern bool fAddressIndex;
extern bool fSpentIndex;
extern bool fTimestampIndex;
Expand Down
Loading