main.cpp from dev
This commit is contained in:
151
src/main.cpp
151
src/main.cpp
@@ -220,19 +220,19 @@ namespace {
|
||||
|
||||
namespace {
|
||||
|
||||
struct CBlockReject {
|
||||
struct CBlockReject {
|
||||
unsigned char chRejectCode;
|
||||
string strRejectReason;
|
||||
uint256 hashBlock;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* Maintain validation-specific state about nodes, protected by cs_main, instead
|
||||
* by CNode's own locks. This simplifies asynchronous operation, where
|
||||
* processing of incoming data is done after the ProcessMessage call returns,
|
||||
* and we're no longer holding the node's locks.
|
||||
*/
|
||||
struct CNodeState {
|
||||
struct CNodeState {
|
||||
//! The peer's address
|
||||
CService address;
|
||||
//! Whether we have a fully established connection.
|
||||
@@ -274,49 +274,49 @@ struct CNodeState {
|
||||
nBlocksInFlightValidHeaders = 0;
|
||||
fPreferredDownload = false;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/** Map maintaining per-node state. Requires cs_main. */
|
||||
map<NodeId, CNodeState> mapNodeState;
|
||||
/** Map maintaining per-node state. Requires cs_main. */
|
||||
map<NodeId, CNodeState> mapNodeState;
|
||||
|
||||
// Requires cs_main.
|
||||
CNodeState *State(NodeId pnode) {
|
||||
// Requires cs_main.
|
||||
CNodeState *State(NodeId pnode) {
|
||||
map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
|
||||
if (it == mapNodeState.end())
|
||||
return NULL;
|
||||
return &it->second;
|
||||
}
|
||||
}
|
||||
|
||||
int GetHeight()
|
||||
{
|
||||
int GetHeight()
|
||||
{
|
||||
LOCK(cs_main);
|
||||
return chainActive.Height();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePreferredDownload(CNode* node, CNodeState* state)
|
||||
{
|
||||
void UpdatePreferredDownload(CNode* node, CNodeState* state)
|
||||
{
|
||||
nPreferredDownload -= state->fPreferredDownload;
|
||||
|
||||
// Whether this node should be marked as a preferred download node.
|
||||
state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
|
||||
|
||||
nPreferredDownload += state->fPreferredDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns time at which to timeout block request (nTime in microseconds)
|
||||
int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consensus::Params &consensusParams)
|
||||
{
|
||||
// Returns time at which to timeout block request (nTime in microseconds)
|
||||
int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consensus::Params &consensusParams)
|
||||
{
|
||||
return nTime + 500000 * consensusParams.nPowTargetSpacing * (4 + nValidatedQueuedBefore);
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeNode(NodeId nodeid, const CNode *pnode) {
|
||||
void InitializeNode(NodeId nodeid, const CNode *pnode) {
|
||||
LOCK(cs_main);
|
||||
CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
|
||||
state.name = pnode->addrName;
|
||||
state.address = pnode->addr;
|
||||
}
|
||||
}
|
||||
|
||||
void FinalizeNode(NodeId nodeid) {
|
||||
void FinalizeNode(NodeId nodeid) {
|
||||
LOCK(cs_main);
|
||||
CNodeState *state = State(nodeid);
|
||||
|
||||
@@ -333,11 +333,11 @@ void FinalizeNode(NodeId nodeid) {
|
||||
nPreferredDownload -= state->fPreferredDownload;
|
||||
|
||||
mapNodeState.erase(nodeid);
|
||||
}
|
||||
}
|
||||
|
||||
void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age)
|
||||
{
|
||||
/* int expired = pool.Expire(GetTime() - age);
|
||||
void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age)
|
||||
{
|
||||
/* int expired = pool.Expire(GetTime() - age);
|
||||
if (expired != 0)
|
||||
LogPrint("mempool", "Expired %i transactions from the memory pool\n", expired);
|
||||
|
||||
@@ -345,11 +345,11 @@ void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age)
|
||||
pool.TrimToSize(limit, &vNoSpendsRemaining);
|
||||
BOOST_FOREACH(const uint256& removed, vNoSpendsRemaining)
|
||||
pcoinsTip->Uncache(removed);*/
|
||||
}
|
||||
}
|
||||
|
||||
// Requires cs_main.
|
||||
// Returns a bool indicating whether we requested this block.
|
||||
bool MarkBlockAsReceived(const uint256& hash) {
|
||||
// Requires cs_main.
|
||||
// Returns a bool indicating whether we requested this block.
|
||||
bool MarkBlockAsReceived(const uint256& hash) {
|
||||
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
|
||||
if (itInFlight != mapBlocksInFlight.end()) {
|
||||
CNodeState *state = State(itInFlight->second.first);
|
||||
@@ -362,10 +362,10 @@ bool MarkBlockAsReceived(const uint256& hash) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Requires cs_main.
|
||||
void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
|
||||
// Requires cs_main.
|
||||
void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
|
||||
CNodeState *state = State(nodeid);
|
||||
assert(state != NULL);
|
||||
|
||||
@@ -379,10 +379,10 @@ void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Pa
|
||||
state->nBlocksInFlight++;
|
||||
state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
|
||||
mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
|
||||
}
|
||||
}
|
||||
|
||||
/** Check whether the last unknown block a peer advertized is not yet known. */
|
||||
void ProcessBlockAvailability(NodeId nodeid) {
|
||||
/** Check whether the last unknown block a peer advertized is not yet known. */
|
||||
void ProcessBlockAvailability(NodeId nodeid) {
|
||||
CNodeState *state = State(nodeid);
|
||||
assert(state != NULL);
|
||||
|
||||
@@ -395,10 +395,10 @@ void ProcessBlockAvailability(NodeId nodeid) {
|
||||
state->hashLastUnknownBlock.SetNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Update tracking information about which blocks a peer is assumed to have. */
|
||||
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
|
||||
/** Update tracking information about which blocks a peer is assumed to have. */
|
||||
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
|
||||
CNodeState *state = State(nodeid);
|
||||
assert(state != NULL);
|
||||
|
||||
@@ -414,11 +414,11 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
|
||||
// An unknown block was announced; just assume that the latest one is the best one.
|
||||
state->hashLastUnknownBlock = hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Find the last common ancestor two blocks have.
|
||||
/** Find the last common ancestor two blocks have.
|
||||
* Both pa and pb must be non-NULL. */
|
||||
CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
|
||||
CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
|
||||
if (pa->nHeight > pb->nHeight) {
|
||||
pa = pa->GetAncestor(pb->nHeight);
|
||||
} else if (pb->nHeight > pa->nHeight) {
|
||||
@@ -433,11 +433,11 @@ CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
|
||||
// Eventually all chain branches meet at the genesis block.
|
||||
assert(pa == pb);
|
||||
return pa;
|
||||
}
|
||||
}
|
||||
|
||||
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
|
||||
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
|
||||
* at most count entries. */
|
||||
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller) {
|
||||
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller) {
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
@@ -517,7 +517,7 @@ void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBl
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // anon namespace
|
||||
|
||||
@@ -1552,7 +1552,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock
|
||||
}
|
||||
|
||||
/*char *komodo_getspendscript(uint256 hash,int32_t n)
|
||||
{
|
||||
{
|
||||
CTransaction tx; uint256 hashBlock;
|
||||
if ( !GetTransaction(hash,tx,hashBlock,true) )
|
||||
{
|
||||
@@ -1563,7 +1563,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock
|
||||
return((char *)tx.vout[n].scriptPubKey.ToString().c_str());
|
||||
else printf("getspendscript illegal n.%d\n",n);
|
||||
return(0);
|
||||
}*/
|
||||
}*/
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -1699,7 +1699,7 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
|
||||
return(nSubsidy);
|
||||
} else return(0);
|
||||
}
|
||||
/*
|
||||
/*
|
||||
// Mining slow start
|
||||
// The subsidy is ramped up linearly, skipping the middle payout of
|
||||
// MAX_SUBSIDY/2 to keep the monetary curve consistent with no slow start.
|
||||
@@ -1950,8 +1950,8 @@ int GetSpendHeight(const CCoinsViewCache& inputs)
|
||||
}
|
||||
|
||||
namespace Consensus {
|
||||
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, const Consensus::Params& consensusParams)
|
||||
{
|
||||
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, const Consensus::Params& consensusParams)
|
||||
{
|
||||
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
|
||||
// for an attacker to attempt to split the network.
|
||||
if (!inputs.HaveInputs(tx))
|
||||
@@ -1998,7 +1998,7 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
|
||||
int64_t interest; int32_t txheight; uint32_t locktime;
|
||||
if ( (interest= komodo_accrued_interest(&txheight,&locktime,prevout.hash,prevout.n,0,coins->vout[prevout.n].nValue,(int32_t)nSpendHeight-1)) != 0 )
|
||||
{
|
||||
//fprintf(stderr,"checkResult %.8f += val %.8f interest %.8f ht.%d lock.%u tip.%u\n",(double)nValueIn/COIN,(double)coins->vout[prevout.n].nValue/COIN,(double)interest/COIN,txheight,locktime,chainActive.Tip()->nTime);
|
||||
//fprintf(stderr,"checkResult %.8f += val %.8f interest %.8f ht.%d lock.%u tip.%u\n",(double)nValueIn/COIN,(double)coins->vout[prevout.n].nValue/COIN,(double)interest/COIN,txheight,locktime,chainActive.Tip()->nTime);
|
||||
nValueIn += interest;
|
||||
}
|
||||
}
|
||||
@@ -2031,7 +2031,7 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
|
||||
return state.DoS(100, error("CheckInputs(): nFees out of range"),
|
||||
REJECT_INVALID, "bad-txns-fee-outofrange");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}// namespace Consensus
|
||||
|
||||
bool ContextualCheckInputs(
|
||||
@@ -2104,7 +2104,7 @@ bool ContextualCheckInputs(
|
||||
|
||||
|
||||
/*bool ContextualCheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, const Consensus::Params& consensusParams, std::vector<CScriptCheck> *pvChecks)
|
||||
{
|
||||
{
|
||||
if (!NonContextualCheckInputs(tx, state, inputs, fScriptChecks, flags, cacheStore, consensusParams, pvChecks)) {
|
||||
fprintf(stderr,"ContextualCheckInputs failure.0\n");
|
||||
return false;
|
||||
@@ -2139,12 +2139,12 @@ bool ContextualCheckInputs(
|
||||
}
|
||||
|
||||
return true;
|
||||
}*/
|
||||
}*/
|
||||
|
||||
namespace {
|
||||
|
||||
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
|
||||
{
|
||||
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
|
||||
{
|
||||
// Open history file to append
|
||||
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
|
||||
if (fileout.IsNull())
|
||||
@@ -2168,10 +2168,10 @@ bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint
|
||||
fileout << hasher.GetHash();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
|
||||
{
|
||||
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
|
||||
{
|
||||
// Open history file to read
|
||||
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
|
||||
if (filein.IsNull())
|
||||
@@ -2195,11 +2195,11 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uin
|
||||
return error("%s: Checksum mismatch", __func__);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Abort with a message */
|
||||
bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
|
||||
{
|
||||
/** Abort with a message */
|
||||
bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
|
||||
{
|
||||
strMiscWarning = strMessage;
|
||||
LogPrintf("*** %s\n", strMessage);
|
||||
uiInterface.ThreadSafeMessageBox(
|
||||
@@ -2207,13 +2207,13 @@ bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
|
||||
"", CClientUIInterface::MSG_ERROR);
|
||||
StartShutdown();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
|
||||
{
|
||||
bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
|
||||
{
|
||||
AbortNode(strMessage, userMessage);
|
||||
return state.Error(strMessage);
|
||||
}
|
||||
}
|
||||
|
||||
} // anon namespace
|
||||
|
||||
@@ -2528,7 +2528,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
|
||||
if (nSigOps > MAX_BLOCK_SIGOPS)
|
||||
return state.DoS(100, error("ConnectBlock(): too many sigops"),
|
||||
REJECT_INVALID, "bad-blk-sigops");
|
||||
//fprintf(stderr,"ht.%d vout0 t%u\n",pindex->nHeight,tx.nLockTime);
|
||||
//fprintf(stderr,"ht.%d vout0 t%u\n",pindex->nHeight,tx.nLockTime);
|
||||
if (!tx.IsCoinBase())
|
||||
{
|
||||
if (!view.HaveInputs(tx))
|
||||
@@ -3524,6 +3524,8 @@ int32_t komodo_reverify_blockcheck(CValidationState& state,int32_t height,CBlock
|
||||
{
|
||||
static int32_t oneshot;
|
||||
CBlockIndex *tipindex; int32_t rewindtarget;
|
||||
if ( KOMODO_REWIND != 0 )
|
||||
oneshot = KOMODO_REWIND;
|
||||
if ( oneshot == 0 && IsInitialBlockDownload() == 0 && (tipindex= chainActive.Tip()) != 0 )
|
||||
{
|
||||
// if 200 blocks behind longestchain and no blocks for 2 hours
|
||||
@@ -3743,7 +3745,12 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
|
||||
if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK )
|
||||
{
|
||||
komodo_reverify_blockcheck(state,pindex->nHeight,pindex);
|
||||
if ( KOMODO_LONGESTCHAIN != 0 && pindex->nHeight > KOMODO_LONGESTCHAIN-100 )
|
||||
return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
|
||||
else
|
||||
{
|
||||
pindex->nStatus &= ~BLOCK_FAILED_MASK;
|
||||
}
|
||||
}
|
||||
if ( pindex != 0 && IsInitialBlockDownload() == 0 ) // jl777 debug test
|
||||
{
|
||||
@@ -6435,9 +6442,9 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string CBlockFileInfo::ToString() const {
|
||||
std::string CBlockFileInfo::ToString() const {
|
||||
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user