Compare commits

...
10 Commits
Author SHA1 Message Date
Brannon King 043e17d85d tweak consistency check 2020-01-31 10:28:33 -07:00
Brannon King e9f2f4be9e Merge branch 'sqlite_sync' 2020-01-31 10:07:37 -07:00
Brannon King 39e2f3e5bc byte swap performance on windows 2020-01-30 17:10:57 -07:00
Brannon King b18091f63c backported networking tweaks 2020-01-30 15:39:22 -07:00
Anthony Fieroni 55730549af Increase attempts to database sync
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
2020-01-30 17:42:21 +02:00
Brannon King 7af6e43bb5 optimized hash calc, fixed bad nChainTx, re-used db connection 2020-01-29 14:06:53 -07:00
Brannon King 85ea861144 Merge branch 'poll_for_select' 2020-01-28 12:37:04 -07:00
Brannon King d2b1aa52d5 more careful initialization 2020-01-28 12:33:10 -07:00
Anthony Fieroni e5c233691f Use poll instead of select
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
2020-01-28 10:41:16 -07:00
Brannon King 558f85e411 improve integrity check time 2020-01-28 10:41:02 -07:00
15 changed files with 254 additions and 88 deletions

No files matched your search

+2 -2
View File
@@ -166,10 +166,10 @@ public:
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 1548288000; // Jan 24, 2019
// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("00000000000000000000000000000000000000000000024108e3204a44a57a5a"); //621000
consensus.nMinimumChainWork = uint256S("0000000000000000000000000000000000000000000002bfdb5232f364d6774e"); //700k
// By default assume that the signatures in ancestors of this block are valid.
consensus.defaultAssumeValid = uint256S("7899464514d0d8854919e87eb234fd5f0c35d06418bd5fd3c1a8f7092b2a9317"); //620000
consensus.defaultAssumeValid = uint256S("beaf6432c9a7be3ea8c333bd7a90d4b3e07b0f20c86aa2e5dfebc9eba340201c"); //700k
/**
* The message start string is designed to be unlikely to occur in normal data.
+2 -2
View File
@@ -254,14 +254,14 @@ uint256 CClaimTrieCacheHashFork::computeNodeHash(const std::string& name, int ta
childHashQuery++;
std::vector<uint256> claimHashes;
//if (takeoverHeight > 0) {
if (takeoverHeight > 0) {
COutPoint p;
for (auto &&row: claimHashQuery << nNextHeight << name) {
row >> p.hash >> p.n;
claimHashes.push_back(getValueHash(p, takeoverHeight));
}
claimHashQuery++;
//}
}
auto left = childHashes.empty() ? leafHash : ComputeMerkleRoot(std::move(childHashes));
auto right = claimHashes.empty() ? emptyHash : ComputeMerkleRoot(std::move(claimHashes));
+15
View File
@@ -88,6 +88,21 @@ namespace sqlite
}
return code;
}
inline int sync(database& db, std::size_t attempts = 200)
{
int code = SQLITE_OK;
for (auto i = 0u; i < attempts; ++i) {
code = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
if (code != SQLITE_OK) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
continue;
}
break;
}
return code;
}
}
#endif // SQLITE_H
+42 -22
View File
@@ -40,10 +40,11 @@ static const sqlite::sqlite_config sharedConfig {
void applyPragmas(sqlite::database& db, std::size_t cache)
{
db << "PRAGMA cache_size=-" + std::to_string(cache); // in -KB
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA temp_store=MEMORY";
db << "PRAGMA case_sensitive_like=true";
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit; we handle that elsewhere
db << "PRAGMA wal_autocheckpoint=4000"; // 4k page size * 4000 = 16MB
db.define("POPS", [](std::string s) -> std::string { if (!s.empty()) s.pop_back(); return s; });
db.define("REVERSE", [](std::vector<uint8_t> s) -> std::vector<uint8_t> { std::reverse(s.begin(), s.end()); return s; });
@@ -71,7 +72,7 @@ CClaimTrie::CClaimTrie(std::size_t cacheBytes, bool fWipe, int height,
nExtendedClaimExpirationForkHeight(nExtendedClaimExpirationForkHeight),
nAllClaimsInMerkleForkHeight(nAllClaimsInMerkleForkHeight)
{
applyPragmas(db, 5U * 1024U); // in KB
applyPragmas(db, cacheBytes >> 10U); // in KB
db << "CREATE TABLE IF NOT EXISTS node (name BLOB NOT NULL PRIMARY KEY, "
"parent BLOB REFERENCES node(name) DEFERRABLE INITIALLY DEFERRED, "
@@ -130,14 +131,15 @@ CClaimTrieCacheBase::~CClaimTrieCacheBase()
bool CClaimTrie::SyncToDisk()
{
// alternatively, switch to full sync after we are caught up on the chain
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
return rc == SQLITE_OK;
return sqlite::sync(db) == SQLITE_OK;
}
bool CClaimTrie::empty()
bool CClaimTrie::empty() // only used for testing
{
sqlite::database local(dbFile, sharedConfig);
applyPragmas(local, 100);
int64_t count;
db << "SELECT COUNT(*) FROM (SELECT 1 FROM claim WHERE activationHeight < ?1 AND expirationHeight >= ?1 LIMIT 1)" << nNextHeight >> count;
local << "SELECT COUNT(*) FROM (SELECT 1 FROM claim WHERE activationHeight < ?1 AND expirationHeight >= ?1 LIMIT 1)" << nNextHeight >> count;
return count == 0;
}
@@ -425,10 +427,12 @@ uint256 CClaimTrieCacheBase::computeNodeHash(const std::string& name, int takeov
};
childHashQuery++;
CClaimValue claim;
if (getInfoForName(name, claim)) {
auto valueHash = getValueHash(claim.outPoint, takeoverHeight);
vchToHash.insert(vchToHash.end(), valueHash.begin(), valueHash.end());
if (takeoverHeight > 0) {
CClaimValue claim;
if (getInfoForName(name, claim)) {
auto valueHash = getValueHash(claim.outPoint, takeoverHeight);
vchToHash.insert(vchToHash.end(), valueHash.begin(), valueHash.end());
}
}
return vchToHash.empty() ? one : Hash(vchToHash.begin(), vchToHash.end());
@@ -436,11 +440,21 @@ uint256 CClaimTrieCacheBase::computeNodeHash(const std::string& name, int takeov
bool CClaimTrieCacheBase::checkConsistency()
{
// verify that all claims hash to the values on the nodes
auto checkQuery = db << "PRAGMA quick_check";
for (auto&& row: checkQuery) {
std::string message;
row >> message;
if (message != "ok") {
logPrint << message << Clog::endl;
return false;
}
}
// not checking everything as it takes too long
auto query = db << "SELECT n.name, n.hash, "
"IFNULL((SELECT CASE WHEN t.claimID IS NULL THEN 0 ELSE t.height END "
"FROM takeover t WHERE t.name = n.name ORDER BY t.height DESC LIMIT 1), 0) FROM node n";
"FROM takeover t WHERE t.name = n.name ORDER BY t.height DESC LIMIT 1), 0) FROM node n "
"WHERE n.name IN (SELECT r.name FROM node r ORDER BY RANDOM() LIMIT 100000) OR n.parent = x''";
for (auto&& row: query) {
std::string name;
uint256 hash;
@@ -507,21 +521,21 @@ extern const std::string proofClaimQuery_s =
"ORDER BY n.name";
CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base)
: base(base), db(base->dbFile, sharedConfig), transacting(false),
: base(base), db(base->db.connection()), transacting(false),
childHashQuery(db << childHashQuery_s),
claimHashQuery(db << claimHashQuery_s),
claimHashQueryLimit(db << claimHashQueryLimit_s)
{
assert(base);
nNextHeight = base->nNextHeight;
applyPragmas(db, base->dbCacheBytes >> 10U); // in KB
}
void CClaimTrieCacheBase::ensureTransacting()
{
if (!transacting) {
transacting = true;
int isNotInTransaction = sqlite3_get_autocommit(db.connection().get());
assert(isNotInTransaction);
db << "BEGIN";
}
}
@@ -720,11 +734,20 @@ bool CClaimTrieCacheBase::incrementBlock()
"UNION SELECT nodeName FROM support WHERE expirationHeight = ?1 OR activationHeight = ?1)"
<< nNextHeight;
auto insertTakeoverQuery = db << "INSERT INTO takeover(name, height, claimID) VALUES(?, ?, ?)";
insertTakeovers();
nNextHeight++;
return true;
}
void CClaimTrieCacheBase::insertTakeovers(bool allowReplace) {
auto insertTakeoverQuery = allowReplace ?
db << "INSERT OR REPLACE INTO takeover(name, height, claimID) VALUES(?, ?, ?)" :
db << "INSERT INTO takeover(name, height, claimID) VALUES(?, ?, ?)";
// takeover handling:
db << "SELECT name FROM node WHERE hash IS NULL"
>> [this, &insertTakeoverQuery](const std::string& nameWithTakeover) {
db << "SELECT name FROM node WHERE hash IS NULL"
>> [this, &insertTakeoverQuery](const std::string& nameWithTakeover) {
// if somebody activates on this block and they are the new best, then everybody activates on this block
CClaimValue candidateValue;
auto hasCandidate = getInfoForName(nameWithTakeover, candidateValue, 1);
@@ -758,9 +781,6 @@ bool CClaimTrieCacheBase::incrementBlock()
};
insertTakeoverQuery.used(true);
nNextHeight++;
return true;
}
bool CClaimTrieCacheBase::activateAllFor(const std::string& name)
+1
View File
@@ -127,6 +127,7 @@ protected:
bool deleteNodeIfPossible(const std::string& name, std::string& parent, int64_t& claims);
void ensureTreeStructureIsUpToDate();
void ensureTransacting();
void insertTakeovers(bool allowReplace=false);
private:
bool transacting;
+8 -1
View File
@@ -102,8 +102,15 @@ typedef void* sockopt_arg_type;
typedef char* sockopt_arg_type;
#endif
// Note these both should work with the current usage of poll, but best to be safe
// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
// __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
#if defined(__linux__)
#define USE_POLL
#endif
bool static inline IsSelectableSocket(const SOCKET& s) {
#ifdef WIN32
#if defined(USE_POLL) || defined(WIN32)
return true;
#else
return (s < FD_SETSIZE);
+15
View File
@@ -30,26 +30,40 @@
#endif // !defined(bswap_16)
#else
#include <boost/endian/detail/intrinsic.hpp>
// Non-Mac OS X / non-Darwin
#if HAVE_DECL_BSWAP_16 == 0
inline uint16_t bswap_16(uint16_t x)
{
#ifdef BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
#else
return (x >> 8) | (x << 8);
#endif
}
#endif // HAVE_DECL_BSWAP16 == 0
#if HAVE_DECL_BSWAP_32 == 0
inline uint32_t bswap_32(uint32_t x)
{
#ifdef BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
#else
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
#endif
}
#endif // HAVE_DECL_BSWAP32 == 0
#if HAVE_DECL_BSWAP_64 == 0
inline uint64_t bswap_64(uint64_t x)
{
#ifdef BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8
return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
#else
return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
@@ -58,6 +72,7 @@ inline uint64_t bswap_64(uint64_t x)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
#endif
}
#endif // HAVE_DECL_BSWAP64 == 0
+6 -1
View File
@@ -941,8 +941,13 @@ bool AppInitParameterInteraction()
// Trim requested connection counts, to fit into system limitations
// <int> in std::min<int>(...) to work around FreeBSD compilation issue described in #2695
nMaxConnections = std::max(std::min<int>(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);
nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS + MAX_ADDNODE_CONNECTIONS);
#ifdef USE_POLL
int fd_max = nFD;
#else
int fd_max = FD_SETSIZE;
#endif
nMaxConnections = std::max(std::min<int>(nMaxConnections, fd_max - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS), 0);
if (nFD < MIN_CORE_FILEDESCRIPTORS)
return InitError(_("Not enough file descriptors available."));
nMaxConnections = std::min(nFD - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS, nMaxConnections);
+63 -10
View File
@@ -26,6 +26,10 @@
#include <fcntl.h>
#endif
#ifdef USE_POLL
#include <poll.h>
#endif
#ifdef USE_UPNP
#include <miniupnpc/miniupnpc.h>
#include <miniupnpc/miniwget.h>
@@ -35,6 +39,7 @@
#include <math.h>
#include <unordered_map>
// Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
#define DUMP_ADDRESSES_INTERVAL 900
@@ -71,6 +76,10 @@ enum BindFlags {
BF_WHITELIST = (1U << 2),
};
// The set of sockets cannot be modified while waiting
// The sleep time needs to be small to avoid new sockets stalling
static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50;
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
@@ -163,7 +172,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
static int GetnScore(const CService& addr)
{
LOCK(cs_mapLocalHost);
if (mapLocalHost.count(addr) == LOCAL_NONE)
if (mapLocalHost.count(addr) == 0)
return 0;
return mapLocalHost[addr].nScore;
}
@@ -917,7 +926,8 @@ size_t CConnman::SocketSendData(CNode *pnode) const
int nErr = WSAGetLastError();
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
{
LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
LogPrintf("socket send error: %s%s\n", NetworkErrorString(nErr),
fLogIPs ? (", peeraddr=" + pnode->addrName) : std::string());
pnode->CloseSocketDisconnect();
}
}
@@ -1232,23 +1242,34 @@ void CConnman::ThreadSocketHandler()
//
// Find which sockets have data to receive
//
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 50000; // frequency to poll pnode->vSend
fd_set fdsetRecv;
fd_set fdsetSend;
fd_set fdsetError;
FD_ZERO(&fdsetRecv);
FD_ZERO(&fdsetSend);
FD_ZERO(&fdsetError);
#ifdef USE_POLL
std::unordered_map<SOCKET, struct pollfd> pollfds;
#else
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000; // frequency to poll pnode->vSend
SOCKET hSocketMax = 0;
bool have_fds = false;
#endif
for (const ListenSocket& hListenSocket : vhListenSocket) {
#ifdef USE_POLL
pollfds[hListenSocket.socket].fd = hListenSocket.socket;
pollfds[hListenSocket.socket].events = POLLIN;
pollfds[hListenSocket.socket].revents = 0;
#else
FD_SET(hListenSocket.socket, &fdsetRecv);
hSocketMax = std::max(hSocketMax, hListenSocket.socket);
have_fds = true;
#endif
}
{
@@ -1277,25 +1298,55 @@ void CConnman::ThreadSocketHandler()
if (pnode->hSocket == INVALID_SOCKET)
continue;
#ifdef USE_POLL
pollfds[pnode->hSocket].fd = pnode->hSocket;
pollfds[pnode->hSocket].events = POLLERR|POLLHUP;
pollfds[pnode->hSocket].revents = 0;
#else
FD_SET(pnode->hSocket, &fdsetError);
hSocketMax = std::max(hSocketMax, pnode->hSocket);
have_fds = true;
#endif
if (select_send) {
#ifdef USE_POLL
pollfds[pnode->hSocket].events |= POLLOUT;
#else
FD_SET(pnode->hSocket, &fdsetSend);
#endif
continue;
}
if (select_recv) {
#ifdef USE_POLL
pollfds[pnode->hSocket].events |= POLLIN;
#else
FD_SET(pnode->hSocket, &fdsetRecv);
#endif
}
}
}
int nSelect = select(have_fds ? hSocketMax + 1 : 0,
&fdsetRecv, &fdsetSend, &fdsetError, &timeout);
if (interruptNet)
return;
#ifdef USE_POLL
std::vector<struct pollfd> vpollfds;
vpollfds.reserve(pollfds.size());
for (auto it : pollfds)
vpollfds.push_back(std::move(it.second));
if (poll(vpollfds.data(), vpollfds.size(), SELECT_TIMEOUT_MILLISECONDS) < 0)
return;
for (struct pollfd pollfd_entry : vpollfds) {
if (pollfd_entry.revents & POLLIN) FD_SET(pollfd_entry.fd, &fdsetRecv);
if (pollfd_entry.revents & POLLOUT) FD_SET(pollfd_entry.fd, &fdsetSend);
if (pollfd_entry.revents & (POLLERR|POLLHUP)) FD_SET(pollfd_entry.fd, &fdsetError);
}
#else
int nSelect = select(have_fds ? hSocketMax + 1 : 0,
&fdsetRecv, &fdsetSend, &fdsetError, &timeout);
if (nSelect == SOCKET_ERROR)
{
if (have_fds)
@@ -1310,6 +1361,7 @@ void CConnman::ThreadSocketHandler()
if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
return;
}
#endif
//
// Accept new connections
@@ -1335,7 +1387,7 @@ void CConnman::ThreadSocketHandler()
for (CNode* pnode : vNodesCopy)
{
if (interruptNet)
return;
break;
//
// Receive
@@ -1400,7 +1452,8 @@ void CConnman::ThreadSocketHandler()
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
{
if (!pnode->fDisconnect)
LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
LogPrintf("socket recv error: %s%s\n", NetworkErrorString(nErr),
fLogIPs ? (", peeraddr=" + pnode->addrName) : std::string());
pnode->CloseSocketDisconnect();
}
}
+4 -1
View File
@@ -1981,7 +1981,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
{
pfrom->AddInventoryKnown(inv);
if (fBlocksOnly) {
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom->GetId());
pfrom->fDisconnect = true;
return true;
} else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
pfrom->AskFor(inv);
}
@@ -2206,6 +2208,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
{
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
pfrom->fDisconnect = true;
return true;
}
+24 -1
View File
@@ -19,6 +19,10 @@
#include <fcntl.h>
#endif
#ifdef USE_POLL
#include <poll.h>
#endif
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#if !defined(MSG_NOSIGNAL)
@@ -264,11 +268,22 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
if (!IsSelectableSocket(hSocket)) {
return IntrRecvError::NetworkError;
}
struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
// Only wait at most maxWait milliseconds at a time, unless
// we're approaching the end of the specified total timeout
int timeout_ms = std::min(endTime - curTime, maxWait);
#ifdef USE_POLL
struct pollfd pollfd;
pollfd.fd = hSocket;
pollfd.events = POLLIN;
pollfd.revents = 0;
int nRet = poll(&pollfd, 1, timeout_ms);
#else
struct timeval tval = MillisToTimeval(timeout_ms);
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(hSocket, &fdset);
int nRet = select(hSocket + 1, &fdset, nullptr, nullptr, &tval);
#endif
if (nRet == SOCKET_ERROR) {
return IntrRecvError::NetworkError;
}
@@ -499,11 +514,19 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
// WSAEINVAL is here because some legacy version of winsock uses it
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
{
#ifdef USE_POLL
struct pollfd pollfd;
pollfd.fd = hSocket;
pollfd.events = POLLIN | POLLOUT;
pollfd.revents = 0;
int nRet = poll(&pollfd, 1, nTimeout);
#else
struct timeval timeout = MillisToTimeval(nTimeout);
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(hSocket, &fdset);
int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout);
#endif
if (nRet == 0)
{
LogPrint(BCLog::NET, "connection to %s timeout\n", addrConnect.ToString());
+1 -1
View File
@@ -288,7 +288,7 @@ CLAIM_OUTPUT
"]",
// GETCLAIMPROOFBYBID
S1("getclaimproofbyid \"" T_NAME "\" ( " T_BID " \"" T_BLOCKHASH R"(" )
S1("getclaimproofbybid \"" T_NAME "\" ( " T_BID " \"" T_BLOCKHASH R"(" )
Return the cryptographic proof that a name maps to a value or doesn't by a bid.
Arguments:)")
S3("1. ", T_NAME, NAME_TEXT)
+47 -31
View File
@@ -15,7 +15,7 @@ class CClaimTrieCacheTest : public CClaimTrieCacheBase
public:
explicit CClaimTrieCacheTest(CClaimTrie* base): CClaimTrieCacheBase(base)
{
nNextHeight = 2;
nNextHeight = std::max(2, nNextHeight);
}
bool insertClaimIntoTrie(const std::string& key, const CClaimValue& value)
@@ -63,6 +63,10 @@ public:
assert(!ret || nodeName == key);
return ret;
}
void processTakeovers() {
insertTakeovers(true);
}
};
BOOST_FIXTURE_TEST_SUITE(claimtriecache_tests, RegTestingSetup)
@@ -97,110 +101,122 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
COutPoint tx6OutPoint(tx6.GetHash(), 0);
uint256 hash1;
hash1.SetHex("71c7b8d35b9a3d7ad9a1272b68972979bbd18589f1efe6f27b0bf260a6ba78fa");
hash1.SetHex("917106c2e4a5d454c6463e366ec2c70fde57a3deacb36f566fc0c8568e4523d5");
uint256 hash2;
hash2.SetHex("c4fc0e2ad56562a636a0a237a96a5f250ef53495c2cb5edd531f087a8de83722");
hash2.SetHex("79d0482510cb3da12b31096d459f8221cd094d268c03d96d897c932716e130e5");
uint256 hash3;
hash3.SetHex("baf52472bd7da19fe1e35116cfb3bd180d8770ffbe3ae9243df1fb58a14b0975");
hash3.SetHex("14ba954095acc42e8a5c1c3c1ca1203a1f1fb44d66b4141272cbb5a16837e3e6");
uint256 hash4;
hash4.SetHex("c73232a755bf015f22eaa611b283ff38100f2a23fb6222e86eca363452ba0c51");
hash4.SetHex("446b9dd04a1ff062c984cbbf0d5ab5aaea4ee3db1a9b65c5d06be0cb2090dbcf");
auto dataDir = GetDataDir() / "merkle_test";
fs::create_directories(dataDir);
CClaimTrie trie(10*1024*1024, true, 3, dataDir.string(), 1000, 1000, -1, 1000, 1000, 1000, 1000, 1);
{
CClaimTrieCacheTest ntState(pclaimTrie);
ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0));
ntState.insertClaimIntoTrie(std::string("test2"), CClaimValue(tx2OutPoint, {}, 50, 0, 0));
CClaimTrieCacheTest ntState(&trie);
ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 2, 2));
ntState.insertClaimIntoTrie(std::string("test2"), CClaimValue(tx2OutPoint, {}, 50, 2, 2));
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(trie.empty());
BOOST_CHECK_EQUAL(ntState.getTotalClaimsInTrie(), 2U);
ntState.processTakeovers();
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1);
ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 1, 1));
ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 3, 3));
ntState.processTakeovers();
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1);
ntState.insertClaimIntoTrie(std::string("tes"), CClaimValue(tx4OutPoint, {}, 50, 0, 0));
ntState.insertClaimIntoTrie(std::string("tes"), CClaimValue(tx4OutPoint, {}, 50, 2, 2));
ntState.processTakeovers();
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
ntState.insertClaimIntoTrie(std::string("testtesttesttest"),
CClaimValue(tx5OutPoint, {}, 50, 0, 0));
CClaimValue(tx5OutPoint, {}, 50, 2, 2));
ntState.removeClaimFromTrie(std::string("testtesttesttest"), tx5OutPoint);
ntState.processTakeovers();
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
ntState.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
BOOST_CHECK(ntState.checkConsistency());
}
{
CClaimTrieCacheTest ntState1(pclaimTrie);
CClaimTrieCacheTest ntState1(&trie);
ntState1.removeClaimFromTrie(std::string("test"), tx1OutPoint);
ntState1.removeClaimFromTrie(std::string("test2"), tx2OutPoint);
ntState1.removeClaimFromTrie(std::string("test"), tx3OutPoint);
ntState1.removeClaimFromTrie(std::string("tes"), tx4OutPoint);
ntState1.processTakeovers();
BOOST_CHECK_EQUAL(ntState1.getMerkleHash(), hash0);
}
{
CClaimTrieCacheTest ntState2(pclaimTrie);
ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 0, 200));
CClaimTrieCacheTest ntState2(&trie);
ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 2, 200));
ntState2.removeClaimFromTrie(std::string("test"), tx1OutPoint);
ntState2.processTakeovers();
BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3);
ntState2.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3);
BOOST_CHECK(ntState2.checkConsistency());
}
{
CClaimTrieCacheTest ntState3(pclaimTrie);
ntState3.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0));
CClaimTrieCacheTest ntState3(&trie);
ntState3.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 2, 2));
ntState3.processTakeovers();
BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4);
ntState3.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4);
BOOST_CHECK(ntState3.checkConsistency());
}
{
CClaimTrieCacheTest ntState4(pclaimTrie);
CClaimTrieCacheTest ntState4(&trie);
ntState4.removeClaimFromTrie(std::string("abab"), tx6OutPoint);
ntState4.processTakeovers();
BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2);
ntState4.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2);
BOOST_CHECK(ntState4.checkConsistency());
}
{
CClaimTrieCacheTest ntState5(pclaimTrie);
CClaimTrieCacheTest ntState5(&trie);
ntState5.removeClaimFromTrie(std::string("test"), tx3OutPoint);
ntState5.processTakeovers();
BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2);
ntState5.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2);
BOOST_CHECK(ntState5.checkConsistency());
}
{
CClaimTrieCacheTest ntState6(pclaimTrie);
ntState6.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 1, 1));
CClaimTrieCacheTest ntState6(&trie);
ntState6.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 3, 3));
ntState6.processTakeovers();
BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2);
ntState6.flush();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!trie.empty());
BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2);
BOOST_CHECK(ntState6.checkConsistency());
}
{
CClaimTrieCacheTest ntState7(pclaimTrie);
CClaimTrieCacheTest ntState7(&trie);
ntState7.removeClaimFromTrie(std::string("test"), tx3OutPoint);
ntState7.removeClaimFromTrie(std::string("test"), tx1OutPoint);
ntState7.removeClaimFromTrie(std::string("tes"), tx4OutPoint);
ntState7.removeClaimFromTrie(std::string("test2"), tx2OutPoint);
ntState7.processTakeovers();
BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0);
ntState7.flush();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(trie.empty());
BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0);
BOOST_CHECK(ntState7.checkConsistency());
}
+23 -15
View File
@@ -30,10 +30,11 @@ CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe)
: db(fMemory ? ":memory:" : (GetDataDir() / "coins.sqlite").string(), sharedConfig)
{
db << "PRAGMA cache_size=-" + std::to_string(nCacheSize >> 10); // in -KB
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA temp_store=MEMORY";
db << "PRAGMA case_sensitive_like=true";
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit; we handle that elsewhere
db << "PRAGMA wal_autocheckpoint=4000"; // 4k page size * 4000 = 16MB
db << "CREATE TABLE IF NOT EXISTS unspent (txID BLOB NOT NULL COLLATE BINARY, txN INTEGER NOT NULL, "
"isCoinbase INTEGER NOT NULL, blockHeight INTEGER NOT NULL, amount INTEGER NOT NULL, "
@@ -157,8 +158,11 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
}
LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
return rc == SQLITE_OK;
auto code = sqlite::sync(db);
if (code != SQLITE_OK) {
LogPrint(BCLog::COINDB, "Error syncing coin database. SQLite error: %d\n", code);
return false;
}
}
return true;
}
@@ -174,10 +178,11 @@ CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe)
: db(fMemory ? ":memory:" : (GetDataDir() / "block_index.sqlite").string(), sharedConfig)
{
db << "PRAGMA cache_size=-" + std::to_string(nCacheSize >> 10); // in -KB
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA temp_store=MEMORY";
db << "PRAGMA case_sensitive_like=true";
db << "PRAGMA journal_mode=WAL";
db << "PRAGMA synchronous=OFF"; // don't disk sync after transaction commit; we handle that elsewhere
db << "PRAGMA wal_autocheckpoint=4000"; // 4k page size * 4000 = 16MB
db << "CREATE TABLE IF NOT EXISTS block_file ("
"file INTEGER NOT NULL PRIMARY KEY, "
@@ -335,8 +340,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileI
}
// by Sync they mean disk sync:
if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
return rc == SQLITE_OK;
auto code = sqlite::sync(db);
if (code != SQLITE_OK) {
LogPrintf("%s: Error syncing block database. SQLite error: %d\n", __func__, code);
return false;
}
}
return true;
}
@@ -383,13 +391,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
>> pindexNew->nNonce
>> pindexNew->nStatus;
if ((pindexNew->nHeight & 0x3ff) == 0x3ff) { // don't check for shutdown on every single block
boost::this_thread::interruption_point();
if (ShutdownRequested())
return false;
}
pindexNew->nChainTx = pindexNew->pprev ? pindexNew->pprev->nChainTx + pindexNew->nTx : pindexNew->nTx;
// nChainTx gets set later; don't set it here or you'll mess up the Unlinked list
if (!CheckProofOfWork(pindexNew->GetBlockPoWHash(), pindexNew->nBits, consensusParams))
{
@@ -397,6 +399,12 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
LogPrintf("%s: CheckProofOfWorkFailed: %s (hash %s, nBits=%x, nTime=%d)\n", __func__, pindexNew->GetBlockPoWHash().GetHex(), pindexNew->GetBlockHash().GetHex(), pindexNew->nBits, pindexNew->nTime);
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
}
if ((pindexNew->nHeight & 0x3ff) == 0x3ff) { // don't check for shutdown on every single block
boost::this_thread::interruption_point();
if (ShutdownRequested())
return false;
}
}
return true;
+1 -1
View File
@@ -2531,7 +2531,7 @@ CBlockIndex* CChainState::FindMostWorkChain() {
// Find the best candidate header.
{
std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
auto it = setBlockIndexCandidates.rbegin();
if (it == setBlockIndexCandidates.rend())
return nullptr;
pindexNew = *it;