Skip to content

Commit

Permalink
Add Bitcore indexing to Omnicore
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Sep 16, 2020
1 parent 1565b37 commit 5e6d739
Show file tree
Hide file tree
Showing 19 changed files with 1,863 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/coins.cpp
Expand Up @@ -258,3 +258,9 @@ const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
}
return coinEmpty;
}

const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
{
const Coin& coins = AccessCoin(input.prevout);
return coins.out;
}
75 changes: 75 additions & 0 deletions src/coins.h
Expand Up @@ -19,6 +19,79 @@

#include <unordered_map>

struct CSpentIndexKey {
uint256 txid;
unsigned int outputIndex;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(txid);
READWRITE(outputIndex);
}

CSpentIndexKey(uint256 t, unsigned int i) {
txid = t;
outputIndex = i;
}

CSpentIndexKey() {
SetNull();
}

void SetNull() {
txid.SetNull();
outputIndex = 0;
}
};

struct CSpentIndexValue {
uint256 txid;
unsigned int inputIndex;
int blockHeight;
CAmount satoshis;
int addressType;
uint256 addressHash;

ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(txid);
READWRITE(inputIndex);
READWRITE(blockHeight);
READWRITE(satoshis);
READWRITE(addressType);
READWRITE(addressHash);
}

CSpentIndexValue(uint256 t, unsigned int i, int h, CAmount s, int type, uint256 a) {
txid = t;
inputIndex = i;
blockHeight = h;
satoshis = s;
addressType = type;
addressHash = a;
}

CSpentIndexValue() {
SetNull();
}

void SetNull() {
txid.SetNull();
inputIndex = 0;
blockHeight = 0;
satoshis = 0;
addressType = 0;
addressHash.SetNull();
}

bool IsNull() const {
return txid.IsNull();
}
};

/**
* A UTXO entry.
*
Expand Down Expand Up @@ -288,6 +361,8 @@ class CCoinsViewCache : public CCoinsViewBacked
//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
bool HaveInputs(const CTransaction& tx) const;

const CTxOut &GetOutputFor(const CTxIn& input) const;

private:
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
};
Expand Down
13 changes: 13 additions & 0 deletions src/init.cpp
Expand Up @@ -415,6 +415,7 @@ void SetupServerArgs()
hidden_args.emplace_back("-sysperms");
#endif
gArgs.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), false, OptionsCategory::OPTIONS);
gArgs.AddArg("-experimental-btc-balances", strprintf("Maintain a full address index (default: %u)", DEFAULT_ADDRINDEX), false, OptionsCategory::OPTIONS);

gArgs.AddArg("-addnode=<ip>", "Add a node to connect to and attempt to keep the connection open (see the `addnode` RPC command help for more info). This option can be specified multiple times to add multiple nodes.", false, OptionsCategory::CONNECTION);
gArgs.AddArg("-banscore=<n>", strprintf("Threshold for disconnecting misbehaving peers (default: %u)", DEFAULT_BANSCORE_THRESHOLD), false, OptionsCategory::CONNECTION);
Expand Down Expand Up @@ -1377,6 +1378,9 @@ bool AppInitMain(InitInterfaces& interfaces)

if (gArgs.GetArg("-omniuseragent", true)) {
uacomments.emplace(uacomments.begin(), OMNI_CLIENT_NAME + ":" + FormatVersion(OMNI_USERAGENT_VERSION));
if (gArgs.GetBoolArg("-experimental-btc-balances", DEFAULT_ADDRINDEX)) {
uacomments.push_back("bitcore");
}
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
} else {
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
Expand Down Expand Up @@ -1484,6 +1488,10 @@ bool AppInitMain(InitInterfaces& interfaces)
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
if (gArgs.GetBoolArg("-experimental-btc-balances", DEFAULT_ADDRINDEX)) {
// enable 3/4 of the cache if addressindex is enabled
nBlockTreeDBCache = nTotalCache * 3 / 4;
}
nTotalCache -= nBlockTreeDBCache;
int64_t nTxIndexCache = std::min(nTotalCache / 8, gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
nTotalCache -= nTxIndexCache;
Expand Down Expand Up @@ -1598,6 +1606,11 @@ bool AppInitMain(InitInterfaces& interfaces)
break;
}

if (fAddressIndex != gArgs.GetBoolArg("-experimental-btc-balances", DEFAULT_ADDRINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -experimental-btc-balances");
break;
}

if (!fReset) {
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
// It both disconnects blocks based on chainActive, and drops block data in
Expand Down
39 changes: 39 additions & 0 deletions src/key_io.cpp
Expand Up @@ -227,3 +227,42 @@ bool IsValidDestinationString(const std::string& str)
{
return IsValidDestinationString(str, Params());
}

bool DecodeIndexKey(const std::string &str, uint256 &hashBytes, int &type)
{
CTxDestination dest = DecodeDestination(str);
if (IsValidDestination(dest))
{
const CKeyID *keyID = boost::get<CKeyID>(&dest);
if(keyID)
{
memcpy(&hashBytes, keyID, 20);
type = 1;
return true;
}

const CScriptID *scriptID = boost::get<CScriptID>(&dest);
if(scriptID)
{
memcpy(&hashBytes, scriptID, 20);
type = 2;
return true;
}

const WitnessV0ScriptHash *witnessV0ScriptID = boost::get<WitnessV0ScriptHash>(&dest);
if (witnessV0ScriptID) {
memcpy(&hashBytes, witnessV0ScriptID, 32);
type = 3;
return true;
}

const WitnessV0KeyHash *witnessV0KeyID = boost::get<WitnessV0KeyHash>(&dest);
if (witnessV0KeyID) {
memcpy(&hashBytes, witnessV0KeyID, 20);
type = 4;
return true;
}
}

return false;
}
2 changes: 2 additions & 0 deletions src/key_io.h
Expand Up @@ -26,4 +26,6 @@ CTxDestination DecodeDestination(const std::string& str);
bool IsValidDestinationString(const std::string& str);
bool IsValidDestinationString(const std::string& str, const CChainParams& params);

bool DecodeIndexKey(const std::string& str, uint256& hashBytes, int& type);

#endif // BITCOIN_KEY_IO_H
9 changes: 9 additions & 0 deletions src/rpc/client.cpp
Expand Up @@ -149,6 +149,15 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "logging", 0, "include" },
{ "logging", 1, "exclude" },
{ "disconnectnode", 1, "nodeid" },
{ "getaddresstxids", 0, "addresses"},
{ "getaddressmempool", 0, "addresses"},
{ "getaddressdeltas", 0, "addresses"},
{ "getaddressbalance", 0, "addresses"},
{ "getaddressutxos", 0, "addresses"},
{ "getblockhashes", 0, "high"},
{ "getblockhashes", 1, "low"},
{ "getblockhashes", 2, "options"},
{ "getspentinfo", 0, "argument"},
// Echo with conversion (For testing only)
{ "echojson", 0, "arg0" },
{ "echojson", 1, "arg1" },
Expand Down

0 comments on commit 5e6d739

Please sign in to comment.