Compare commits

...
5 Commits
Author SHA1 Message Date
Anthony Fieroni e18650cbf0 Running daemon without claimtrie functionality
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
2020-01-29 18:09:51 +02: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
10 changed files with 154 additions and 27 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.
+12 -2
View File
@@ -436,11 +436,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 56789) OR LENGTH(n.parent) < 2";
for (auto&& row: query) {
std::string name;
uint256 hash;
+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);
+10 -2
View File
@@ -78,6 +78,8 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic;
extern bool appIsFinance();
#if !(ENABLE_WALLET)
class DummyWalletInit : public WalletInitInterface {
public:
@@ -392,6 +394,7 @@ void SetupServerArgs()
#else
hidden_args.emplace_back("-sysperms");
#endif
gArgs.AddArg("-finance", "Run daemon without claimtrie db", false, OptionsCategory::OPTIONS);
gArgs.AddArg("-txindex", "Deprecated", false, OptionsCategory::HIDDEN);
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);
@@ -941,8 +944,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);
@@ -1546,7 +1554,7 @@ bool AppInitMain()
auto tip = chainActive.Tip();
LogPrintf("Checking existing claim trie consistency...\n");
if (tip && !CClaimTrieCache(pclaimTrie).validateDb(tip->nHeight, tip->hashClaimTrie)) {
if (tip && !appIsFinance() && !CClaimTrieCache(pclaimTrie).validateDb(tip->nHeight, tip->hashClaimTrie)) {
strLoadError = _("Error validating the stored claim trie");
break;
}
+56 -6
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]
@@ -1232,23 +1241,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,24 +1297,51 @@ 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
}
}
}
#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 (interruptNet)
return;
if (nSelect == SOCKET_ERROR)
{
@@ -1310,7 +1357,10 @@ void CConnman::ThreadSocketHandler()
if (!interruptNet.sleep_for(std::chrono::milliseconds(timeout.tv_usec/1000)))
return;
}
#endif
if (interruptNet)
return;
//
// Accept new connections
//
+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)
+5 -2
View File
@@ -881,8 +881,11 @@ static const CRPCCommand commands[] =
{ "Claimtrie", "checknormalization", &checknormalization, { T_NAME } },
};
extern bool appIsFinance();
void RegisterClaimTrieRPCCommands(CRPCTable &tableRPC)
{
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]);
if (!appIsFinance())
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]);
}
+5 -1
View File
@@ -990,8 +990,12 @@ static const CRPCCommand commands[] =
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
};
extern bool appIsFinance();
void RegisterMiningRPCCommands(CRPCTable &t)
{
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
t.appendCommand(commands[vcidx].name, &commands[vcidx]);
if (!appIsFinance() || (commands[vcidx].name != "getblocktemplate"
&& commands[vcidx].name != "generatetoaddress"))
t.appendCommand(commands[vcidx].name, &commands[vcidx]);
}
+31 -9
View File
@@ -46,6 +46,7 @@
#include <warnings.h>
#include <cmath>
#include <functional>
#include <future>
#include <sstream>
@@ -97,6 +98,22 @@ CScript COINBASE_FLAGS;
const std::string strMessageMagic = "LBRYcrd Signed Message:\n";
bool appIsFinance()
{
static const auto finance = []() -> bool {
auto finance = gArgs.GetBoolArg("-finance", false);
if (finance)
LogPrintf("NOTE: daemon is running without claimtrie functionality\n");
return finance;
}();
return finance;
}
const std::function<bool(const uint256&, const uint256&)> hashComp =
[](const uint256& lhs, const uint256& rhs) {
return appIsFinance() || lhs == rhs;
};
// Internal stuff
namespace {
CBlockIndex *&pindexBestInvalid = g_chainstate.pindexBestInvalid;
@@ -1510,7 +1527,7 @@ int ApplyTxInUndo(unsigned int index, CTxUndo& txUndo, CCoinsViewCache& view, CC
DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CClaimTrieCache& trieCache)
{
assert(pindex->GetBlockHash() == view.GetBestBlock());
if (pindex->hashClaimTrie != trieCache.getMerkleHash()) {
if (!hashComp(pindex->hashClaimTrie, trieCache.getMerkleHash())) {
LogPrintf("%s: Indexed claim hash doesn't match current: %s vs %s\n",
__func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
assert(false);
@@ -1584,7 +1601,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
// move best block pointer to prevout block
view.SetBestBlock(pindex->pprev->GetBlockHash());
assert(trieCache.finalizeDecrement());
if (trieCache.getMerkleHash() != pindex->pprev->hashClaimTrie) {
if (!hashComp(trieCache.getMerkleHash(), pindex->pprev->hashClaimTrie)) {
LogPrintf("Hash comparison failure at block %d\n", pindex->nHeight);
assert(false);
}
@@ -1797,7 +1814,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
assert(hashPrevBlock == view.GetBestBlock());
// also verify that the trie cache's current state corresponds to the previous block
if (pindex->pprev != nullptr && pindex->pprev->hashClaimTrie != trieCache.getMerkleHash()) {
if (pindex->pprev != nullptr && !hashComp(pindex->pprev->hashClaimTrie, trieCache.getMerkleHash())) {
LogPrintf("%s: Previous block claim hash doesn't match current: %s vs %s\n",
__func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
assert(false);
@@ -2059,7 +2076,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
// TODO: if the "just check" flag is set, we should reduce the work done here. Incrementing blocks twice per mine is not efficient.
assert(trieCache.incrementBlock());
if (trieCache.getMerkleHash() != block.hashClaimTrie) {
if (!hashComp(trieCache.getMerkleHash(), block.hashClaimTrie)) {
return state.DoS(100, error("ConnectBlock() : the merkle root of the claim trie does not match "
"(actual=%s vs block=%s on height=%d)", trieCache.getMerkleHash().GetHex(),
block.hashClaimTrie.GetHex(), pindex->nHeight), REJECT_INVALID, "bad-claim-merkle-hash");
@@ -2350,8 +2367,10 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
bool flushed = view.Flush();
assert(flushed);
assert(trieCache.flush());
assert(pindexDelete->pprev->hashClaimTrie == trieCache.getMerkleHash());
if (!appIsFinance()) {
assert(trieCache.flush());
assert(pindexDelete->pprev->hashClaimTrie == trieCache.getMerkleHash());
}
}
LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
// Write the chain state to disk, if necessary.
@@ -2490,8 +2509,10 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
bool flushed = view.Flush();
assert(flushed);
flushed = trieCache.flush();
assert(flushed);
if (!appIsFinance()) {
flushed = trieCache.flush();
assert(flushed);
}
// for verifying that rollback code works:
// auto result = DisconnectBlock(blockConnecting, pindexNew, view, trieCache);
@@ -4198,7 +4219,8 @@ bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
cache.SetBestBlock(pindexNew->GetBlockHash());
cache.Flush();
trieCache.flush();
if (!appIsFinance())
trieCache.flush();
uiInterface.ShowProgress("", 100, false);
return true;
}