Merge branch 'sqlite_sync'

This commit is contained in:
Brannon King 2020-01-31 10:07:37 -07:00
commit e9f2f4be9e
6 changed files with 54 additions and 13 deletions

View file

@ -88,6 +88,21 @@ namespace sqlite
} }
return code; 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 #endif // SQLITE_H

View file

@ -131,8 +131,7 @@ CClaimTrieCacheBase::~CClaimTrieCacheBase()
bool CClaimTrie::SyncToDisk() bool CClaimTrie::SyncToDisk()
{ {
// alternatively, switch to full sync after we are caught up on the chain // 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 sqlite::sync(db) == SQLITE_OK;
return rc == SQLITE_OK;
} }
bool CClaimTrie::empty() // only used for testing bool CClaimTrie::empty() // only used for testing

View file

@ -30,26 +30,40 @@
#endif // !defined(bswap_16) #endif // !defined(bswap_16)
#else #else
#include <boost/endian/detail/intrinsic.hpp>
// Non-Mac OS X / non-Darwin // Non-Mac OS X / non-Darwin
#if HAVE_DECL_BSWAP_16 == 0 #if HAVE_DECL_BSWAP_16 == 0
inline uint16_t bswap_16(uint16_t x) 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); return (x >> 8) | (x << 8);
#endif
} }
#endif // HAVE_DECL_BSWAP16 == 0 #endif // HAVE_DECL_BSWAP16 == 0
#if HAVE_DECL_BSWAP_32 == 0 #if HAVE_DECL_BSWAP_32 == 0
inline uint32_t bswap_32(uint32_t x) 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) | return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
#endif
} }
#endif // HAVE_DECL_BSWAP32 == 0 #endif // HAVE_DECL_BSWAP32 == 0
#if HAVE_DECL_BSWAP_64 == 0 #if HAVE_DECL_BSWAP_64 == 0
inline uint64_t bswap_64(uint64_t x) 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) return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40) | ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24) | ((x & 0x0000ff0000000000ull) >> 24)
@ -58,6 +72,7 @@ inline uint64_t bswap_64(uint64_t x)
| ((x & 0x0000000000ff0000ull) << 24) | ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40) | ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56)); | ((x & 0x00000000000000ffull) << 56));
#endif
} }
#endif // HAVE_DECL_BSWAP64 == 0 #endif // HAVE_DECL_BSWAP64 == 0

View file

@ -172,7 +172,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
static int GetnScore(const CService& addr) static int GetnScore(const CService& addr)
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
if (mapLocalHost.count(addr) == LOCAL_NONE) if (mapLocalHost.count(addr) == 0)
return 0; return 0;
return mapLocalHost[addr].nScore; return mapLocalHost[addr].nScore;
} }
@ -926,7 +926,8 @@ size_t CConnman::SocketSendData(CNode *pnode) const
int nErr = WSAGetLastError(); int nErr = WSAGetLastError();
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) 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(); pnode->CloseSocketDisconnect();
} }
} }
@ -1325,6 +1326,9 @@ void CConnman::ThreadSocketHandler()
} }
} }
if (interruptNet)
return;
#ifdef USE_POLL #ifdef USE_POLL
std::vector<struct pollfd> vpollfds; std::vector<struct pollfd> vpollfds;
vpollfds.reserve(pollfds.size()); vpollfds.reserve(pollfds.size());
@ -1359,8 +1363,6 @@ void CConnman::ThreadSocketHandler()
} }
#endif #endif
if (interruptNet)
return;
// //
// Accept new connections // Accept new connections
// //
@ -1385,7 +1387,7 @@ void CConnman::ThreadSocketHandler()
for (CNode* pnode : vNodesCopy) for (CNode* pnode : vNodesCopy)
{ {
if (interruptNet) if (interruptNet)
return; break;
// //
// Receive // Receive
@ -1450,7 +1452,8 @@ void CConnman::ThreadSocketHandler()
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
{ {
if (!pnode->fDisconnect) 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(); pnode->CloseSocketDisconnect();
} }
} }

View file

@ -1981,7 +1981,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
{ {
pfrom->AddInventoryKnown(inv); pfrom->AddInventoryKnown(inv);
if (fBlocksOnly) { 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()) { } else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
pfrom->AskFor(inv); 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))) if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
{ {
LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId()); LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
pfrom->fDisconnect = true;
return true; return true;
} }

View file

@ -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); LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
if (sync) { if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); auto code = sqlite::sync(db);
return rc == SQLITE_OK; if (code != SQLITE_OK) {
LogPrint(BCLog::COINDB, "Error syncing coin database. SQLite error: %d\n", code);
return false;
}
} }
return true; return true;
} }
@ -337,8 +340,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileI
} }
// by Sync they mean disk sync: // by Sync they mean disk sync:
if (sync) { if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); auto code = sqlite::sync(db);
return rc == SQLITE_OK; if (code != SQLITE_OK) {
LogPrintf("%s: Error syncing block database. SQLite error: %d\n", __func__, code);
return false;
}
} }
return true; return true;
} }