Running daemon without claimtrie functionality

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2020-01-29 18:09:51 +02:00
parent 85ea861144
commit e18650cbf0
4 changed files with 45 additions and 13 deletions

View file

@ -78,6 +78,8 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
std::unique_ptr<CConnman> g_connman; std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic; std::unique_ptr<PeerLogicValidation> peerLogic;
extern bool appIsFinance();
#if !(ENABLE_WALLET) #if !(ENABLE_WALLET)
class DummyWalletInit : public WalletInitInterface { class DummyWalletInit : public WalletInitInterface {
public: public:
@ -392,6 +394,7 @@ void SetupServerArgs()
#else #else
hidden_args.emplace_back("-sysperms"); hidden_args.emplace_back("-sysperms");
#endif #endif
gArgs.AddArg("-finance", "Run daemon without claimtrie db", false, OptionsCategory::OPTIONS);
gArgs.AddArg("-txindex", "Deprecated", false, OptionsCategory::HIDDEN); 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("-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); gArgs.AddArg("-banscore=<n>", strprintf("Threshold for disconnecting misbehaving peers (default: %u)", DEFAULT_BANSCORE_THRESHOLD), false, OptionsCategory::CONNECTION);
@ -1551,7 +1554,7 @@ bool AppInitMain()
auto tip = chainActive.Tip(); auto tip = chainActive.Tip();
LogPrintf("Checking existing claim trie consistency...\n"); 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"); strLoadError = _("Error validating the stored claim trie");
break; break;
} }

View file

@ -881,8 +881,11 @@ static const CRPCCommand commands[] =
{ "Claimtrie", "checknormalization", &checknormalization, { T_NAME } }, { "Claimtrie", "checknormalization", &checknormalization, { T_NAME } },
}; };
extern bool appIsFinance();
void RegisterClaimTrieRPCCommands(CRPCTable &tableRPC) void RegisterClaimTrieRPCCommands(CRPCTable &tableRPC)
{ {
if (!appIsFinance())
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]); tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]);
} }

View file

@ -990,8 +990,12 @@ static const CRPCCommand commands[] =
{ "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} }, { "hidden", "estimaterawfee", &estimaterawfee, {"conf_target", "threshold"} },
}; };
extern bool appIsFinance();
void RegisterMiningRPCCommands(CRPCTable &t) void RegisterMiningRPCCommands(CRPCTable &t)
{ {
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
if (!appIsFinance() || (commands[vcidx].name != "getblocktemplate"
&& commands[vcidx].name != "generatetoaddress"))
t.appendCommand(commands[vcidx].name, &commands[vcidx]); t.appendCommand(commands[vcidx].name, &commands[vcidx]);
} }

View file

@ -46,6 +46,7 @@
#include <warnings.h> #include <warnings.h>
#include <cmath> #include <cmath>
#include <functional>
#include <future> #include <future>
#include <sstream> #include <sstream>
@ -97,6 +98,22 @@ CScript COINBASE_FLAGS;
const std::string strMessageMagic = "LBRYcrd Signed Message:\n"; 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 // Internal stuff
namespace { namespace {
CBlockIndex *&pindexBestInvalid = g_chainstate.pindexBestInvalid; 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) DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CClaimTrieCache& trieCache)
{ {
assert(pindex->GetBlockHash() == view.GetBestBlock()); 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", LogPrintf("%s: Indexed claim hash doesn't match current: %s vs %s\n",
__func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString()); __func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
assert(false); assert(false);
@ -1584,7 +1601,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
// move best block pointer to prevout block // move best block pointer to prevout block
view.SetBestBlock(pindex->pprev->GetBlockHash()); view.SetBestBlock(pindex->pprev->GetBlockHash());
assert(trieCache.finalizeDecrement()); 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); LogPrintf("Hash comparison failure at block %d\n", pindex->nHeight);
assert(false); assert(false);
} }
@ -1797,7 +1814,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
assert(hashPrevBlock == view.GetBestBlock()); assert(hashPrevBlock == view.GetBestBlock());
// also verify that the trie cache's current state corresponds to the previous block // 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", LogPrintf("%s: Previous block claim hash doesn't match current: %s vs %s\n",
__func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString()); __func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
assert(false); 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. // 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()); 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 " 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(), "(actual=%s vs block=%s on height=%d)", trieCache.getMerkleHash().GetHex(),
block.hashClaimTrie.GetHex(), pindex->nHeight), REJECT_INVALID, "bad-claim-merkle-hash"); block.hashClaimTrie.GetHex(), pindex->nHeight), REJECT_INVALID, "bad-claim-merkle-hash");
@ -2350,9 +2367,11 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString()); return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
bool flushed = view.Flush(); bool flushed = view.Flush();
assert(flushed); assert(flushed);
if (!appIsFinance()) {
assert(trieCache.flush()); assert(trieCache.flush());
assert(pindexDelete->pprev->hashClaimTrie == trieCache.getMerkleHash()); assert(pindexDelete->pprev->hashClaimTrie == trieCache.getMerkleHash());
} }
}
LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI); LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
// Write the chain state to disk, if necessary. // Write the chain state to disk, if necessary.
if (!FlushStateToDisk(chainparams, state, FlushStateMode::IF_NEEDED)) if (!FlushStateToDisk(chainparams, state, FlushStateMode::IF_NEEDED))
@ -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); LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
bool flushed = view.Flush(); bool flushed = view.Flush();
assert(flushed); assert(flushed);
if (!appIsFinance()) {
flushed = trieCache.flush(); flushed = trieCache.flush();
assert(flushed); assert(flushed);
}
// for verifying that rollback code works: // for verifying that rollback code works:
// auto result = DisconnectBlock(blockConnecting, pindexNew, view, trieCache); // auto result = DisconnectBlock(blockConnecting, pindexNew, view, trieCache);
@ -4198,6 +4219,7 @@ bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
cache.SetBestBlock(pindexNew->GetBlockHash()); cache.SetBestBlock(pindexNew->GetBlockHash());
cache.Flush(); cache.Flush();
if (!appIsFinance())
trieCache.flush(); trieCache.flush();
uiInterface.ShowProgress("", 100, false); uiInterface.ShowProgress("", 100, false);
return true; return true;