Skip to content

Commit

Permalink
Merge pull request #51 from anonymousbitcoin/feature/bitcore-integration
Browse files Browse the repository at this point in the history
Create integrations for Bitcore/Insight explorer tools
  • Loading branch information
nlevo authored Sep 24, 2018
2 parents 35cc2db + 6967e28 commit bfd8f5a
Show file tree
Hide file tree
Showing 20 changed files with 1,784 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ LIBZCASH_H = \
BITCOIN_CORE_H = \
addrman.h \
alert.h \
addressindex.h \
amount.h \
activemasternode.h \
amqp/amqpabstractnotifier.h \
Expand Down
82 changes: 82 additions & 0 deletions src/addressindex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_ADDRESSINDEX_H
#define BITCOIN_ADDRESSINDEX_H

#include "uint256.h"
#include "amount.h"

struct CMempoolAddressDelta
{
int64_t time;
CAmount amount;
uint256 prevhash;
unsigned int prevout;

CMempoolAddressDelta(int64_t t, CAmount a, uint256 hash, unsigned int out) {
time = t;
amount = a;
prevhash = hash;
prevout = out;
}

CMempoolAddressDelta(int64_t t, CAmount a) {
time = t;
amount = a;
prevhash.SetNull();
prevout = 0;
}
};

struct CMempoolAddressDeltaKey
{
int type;
uint160 addressBytes;
uint256 txhash;
unsigned int index;
int spending;

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s) {
type = addressType;
addressBytes = addressHash;
txhash = hash;
index = i;
spending = s;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash) {
type = addressType;
addressBytes = addressHash;
txhash.SetNull();
index = 0;
spending = 0;
}
};

struct CMempoolAddressDeltaKeyCompare
{
bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const {
if (a.type == b.type) {
if (a.addressBytes == b.addressBytes) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
} else {
return a.index < b.index;
}
} else {
return a.txhash < b.txhash;
}
} else {
return a.addressBytes < b.addressBytes;
}
} else {
return a.type < b.type;
}
}
};

#endif // BITCOIN_ADDRESSINDEX_H
17 changes: 17 additions & 0 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ CTxDestination CBitcoinAddress::Get() const
return CNoDestination();
}

bool CBitcoinAddress::GetIndexKey(uint160& hashBytes, int& type) const
{
if (!IsValid()) {
return false;
} else if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) {
memcpy(&hashBytes, &vchData[0], 20);
type = 1;
return true;
} else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS)) {
memcpy(&hashBytes, &vchData[0], 20);
type = 2;
return true;
}

return false;
}

bool CBitcoinAddress::GetKeyID(CKeyID& keyID) const
{
if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
Expand Down
1 change: 1 addition & 0 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class CBitcoinAddress : public CBase58Data {

CTxDestination Get() const;
bool GetKeyID(CKeyID &keyID) const;
bool GetIndexKey(uint160& hashBytes, int& type) const;
bool IsScript() const;
};

Expand Down
30 changes: 27 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ std::string HelpMessage(HelpMessageMode mode)
#endif
strUsage += HelpMessageOpt("-txindex", strprintf(_("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)"), 0));

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));
strUsage += HelpMessageOpt("-spentindex", strprintf(_("Maintain a full spent index, used to query the spending txid and input index for an outpoint (default: %u)"), DEFAULT_SPENTINDEX));

strUsage += HelpMessageGroup(_("Connection options:"));
strUsage += HelpMessageOpt("-addnode=<ip>", _("Add a node to connect to and attempt to keep the connection open"));
strUsage += HelpMessageOpt("-banscore=<n>", strprintf(_("Threshold for disconnecting misbehaving peers (default: %u)"), 100));
Expand Down Expand Up @@ -1377,18 +1381,37 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
}
}

// block tree db settings
int dbMaxOpenFiles = GetArg("-dbmaxopenfiles", DEFAULT_DB_MAX_OPEN_FILES);
bool dbCompression = GetBoolArg("-dbcompression", DEFAULT_DB_COMPRESSION);

LogPrintf("Block index database configuration:\n");
LogPrintf("* Using %d max open files\n", dbMaxOpenFiles);
LogPrintf("* Compression is %s\n", dbCompression ? "enabled" : "disabled");

// cache size calculations
int64_t nTotalCache = (GetArg("-dbcache", nDefaultDbCache) << 20);
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greated than nMaxDbcache
int64_t nBlockTreeDBCache = nTotalCache / 8;
if (nBlockTreeDBCache > (1 << 21) && !GetBoolArg("-txindex", false))
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB

// if (nBlockTreeDBCache > (1 << 21) && !GetBoolArg("-txindex", false))
// nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
if (GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) || GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) {
// enable 3/4 of the cache if addressindex and/or spentindex is enabled
nBlockTreeDBCache = nTotalCache * 3 / 4;
} else {
if (nBlockTreeDBCache > (1 << 21) && !GetBoolArg("-txindex", false)) {
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
}
}

nTotalCache -= nBlockTreeDBCache;
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
nTotalCache -= nCoinDBCache;
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
LogPrintf("Cache configuration:\n");
LogPrintf("* Max cache setting possible %.1fMiB\n", nMaxDbCache);
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
Expand All @@ -1409,7 +1432,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
delete pcoinscatcher;
delete pblocktree;

pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
// pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex, dbCompression, dbMaxOpenFiles);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
Expand Down
15 changes: 10 additions & 5 deletions src/leveldbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ void HandleError(const leveldb::Status& status)
throw leveldb_error("Unknown database error");
}

static leveldb::Options GetOptions(size_t nCacheSize)
// static leveldb::Options GetOptions(size_t nCacheSize)
static leveldb::Options GetOptions(size_t nCacheSize, bool compression, int maxOpenFiles)
{
leveldb::Options options;
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
options.compression = leveldb::kNoCompression;
options.max_open_files = 64;
// options.compression = leveldb::kNoCompression;
// options.max_open_files = 64;
options.compression = compression ? leveldb::kSnappyCompression : leveldb::kNoCompression;
options.max_open_files = maxOpenFiles;
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
// on corruption in later versions.
Expand All @@ -43,14 +46,16 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options;
}

CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe)
// CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe)
CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool compression, int maxOpenFiles)
{
penv = NULL;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
options = GetOptions(nCacheSize);
// options = GetOptions(nCacheSize);
options = GetOptions(nCacheSize, compression, maxOpenFiles);
options.create_if_missing = true;
if (fMemory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());
Expand Down
3 changes: 2 additions & 1 deletion src/leveldbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class CLevelDBWrapper
leveldb::DB* pdb;

public:
CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
// CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool compression = false, int maxOpenFiles = 64);
~CLevelDBWrapper();

template <typename K, typename V>
Expand Down
Loading

0 comments on commit bfd8f5a

Please sign in to comment.