diff --git a/src/claimtrie/sqlite.h b/src/claimtrie/sqlite.h index 7f2b8d534..bb9be35c4 100644 --- a/src/claimtrie/sqlite.h +++ b/src/claimtrie/sqlite.h @@ -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 diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index 51c0254f0..7f6449cf6 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -131,8 +131,7 @@ 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() // only used for testing diff --git a/src/compat/byteswap.h b/src/compat/byteswap.h index fe47f4899..818bbda0c 100644 --- a/src/compat/byteswap.h +++ b/src/compat/byteswap.h @@ -30,26 +30,40 @@ #endif // !defined(bswap_16) #else + +#include + // 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 diff --git a/src/net.cpp b/src/net.cpp index 50fbb8163..0f60c6abf 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -172,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; } @@ -926,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(); } } @@ -1325,6 +1326,9 @@ void CConnman::ThreadSocketHandler() } } + if (interruptNet) + return; + #ifdef USE_POLL std::vector vpollfds; vpollfds.reserve(pollfds.size()); @@ -1359,8 +1363,6 @@ void CConnman::ThreadSocketHandler() } #endif - if (interruptNet) - return; // // Accept new connections // @@ -1385,7 +1387,7 @@ void CConnman::ThreadSocketHandler() for (CNode* pnode : vNodesCopy) { if (interruptNet) - return; + break; // // Receive @@ -1450,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(); } } diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 88999ba73..0be47f2b3 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -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; } diff --git a/src/txdb.cpp b/src/txdb.cpp index 6ecc2ff71..09e7ad83c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -158,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; } @@ -337,8 +340,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector