Sync main.cpp to jl777

This commit is contained in:
jl777
2018-04-15 20:27:23 +03:00
parent e708237b8a
commit 59e8a4f7ee

View File

@@ -53,7 +53,8 @@ using namespace std;
CCriticalSection cs_main; CCriticalSection cs_main;
extern uint8_t NOTARY_PUBKEY33[33]; extern uint8_t NOTARY_PUBKEY33[33];
extern int32_t KOMODO_LOADINGBLOCKS; extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN;
void komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block);
BlockMap mapBlockIndex; BlockMap mapBlockIndex;
CChain chainActive; CChain chainActive;
@@ -1241,11 +1242,14 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
return error("AcceptToMemoryPool: komodo_validate_interest failed"); return error("AcceptToMemoryPool: komodo_validate_interest failed");
} }
if (!CheckTransaction(tx, state, verifier)) if (!CheckTransaction(tx, state, verifier))
return error("AcceptToMemoryPool: CheckTransaction failed"); {
return error("AcceptToMemoryPool: CheckTransaction failed");
}
// DoS level set to 10 to be more forgiving. // DoS level set to 10 to be more forgiving.
// Check transaction contextually against the set of consensus rules which apply in the next block to be mined. // Check transaction contextually against the set of consensus rules which apply in the next block to be mined.
if (!ContextualCheckTransaction(tx, state, nextBlockHeight, 10)) { if (!ContextualCheckTransaction(tx, state, nextBlockHeight, 10))
{
return error("AcceptToMemoryPool: ContextualCheckTransaction failed"); return error("AcceptToMemoryPool: ContextualCheckTransaction failed");
} }
@@ -1408,7 +1412,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
CAmount txMinFee = GetMinRelayFee(tx, nSize, true); CAmount txMinFee = GetMinRelayFee(tx, nSize, true);
if (fLimitFree && nFees < txMinFee) if (fLimitFree && nFees < txMinFee)
{ {
fprintf(stderr,"accept failure.5\n"); //fprintf(stderr,"accept failure.5\n");
return state.DoS(0, error("AcceptToMemoryPool: not enough fees %s, %d < %d",hash.ToString(), nFees, txMinFee),REJECT_INSUFFICIENTFEE, "insufficient fee"); return state.DoS(0, error("AcceptToMemoryPool: not enough fees %s, %d < %d",hash.ToString(), nFees, txMinFee),REJECT_INSUFFICIENTFEE, "insufficient fee");
} }
} }
@@ -1614,7 +1618,7 @@ bool ReadBlockFromDisk(int32_t height,CBlock& block, const CDiskBlockPos& pos)
if (!(CheckEquihashSolution(&block, Params()) && CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus()))) if (!(CheckEquihashSolution(&block, Params()) && CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus())))
{ {
int32_t i; for (i=0; i<33; i++) int32_t i; for (i=0; i<33; i++)
printf("%02x",pubkey33[i]); fprintf(stderr,"%02x",pubkey33[i]);
fprintf(stderr," warning unexpected diff at ht.%d\n",height); fprintf(stderr," warning unexpected diff at ht.%d\n",height);
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
@@ -1849,7 +1853,7 @@ void Misbehaving(NodeId pnode, int howmuch)
return; return;
state->nMisbehavior += howmuch; state->nMisbehavior += howmuch;
int banscore = GetArg("-banscore", 100); int banscore = GetArg("-banscore", 101);
if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore) if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
{ {
LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior); LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
@@ -2427,6 +2431,7 @@ static int64_t nTimeTotal = 0;
bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck) bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
//fprintf(stderr,"connectblock ht.%d\n",(int32_t)pindex->nHeight); //fprintf(stderr,"connectblock ht.%d\n",(int32_t)pindex->nHeight);
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
bool fExpensiveChecks = true; bool fExpensiveChecks = true;
@@ -3080,21 +3085,21 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
} }
if ( KOMODO_REWIND != 0 ) if ( KOMODO_REWIND != 0 )
{ {
CBlockIndex *tipindex;
fprintf(stderr,">>>>>>>>>>> rewind start ht.%d -> KOMODO_REWIND.%d\n",chainActive.Tip()->nHeight,KOMODO_REWIND); fprintf(stderr,">>>>>>>>>>> rewind start ht.%d -> KOMODO_REWIND.%d\n",chainActive.Tip()->nHeight,KOMODO_REWIND);
while ( KOMODO_REWIND > 0 && chainActive.Tip()->nHeight > KOMODO_REWIND ) while ( KOMODO_REWIND > 0 && (tipindex= chainActive.Tip()) != 0 && tipindex->nHeight > KOMODO_REWIND )
{ {
fprintf(stderr,"%d ",(int32_t)chainActive.Tip()->nHeight); fBlocksDisconnected = true;
fprintf(stderr,"%d ",(int32_t)tipindex->nHeight);
InvalidateBlock(state,tipindex);
if ( !DisconnectTip(state) ) if ( !DisconnectTip(state) )
{
InvalidateBlock(state,chainActive.Tip());
break; break;
} }
}
fprintf(stderr,"reached rewind.%d, best to do: ./komodo-cli -ac_name=%s stop\n",KOMODO_REWIND,ASSETCHAINS_SYMBOL); fprintf(stderr,"reached rewind.%d, best to do: ./komodo-cli -ac_name=%s stop\n",KOMODO_REWIND,ASSETCHAINS_SYMBOL);
sleep(60); sleep(20);
fprintf(stderr,"resuming normal operations\n"); fprintf(stderr,"resuming normal operations\n");
KOMODO_REWIND = 0; KOMODO_REWIND = 0;
return(true); //return(true);
} }
// Build list of new blocks to connect. // Build list of new blocks to connect.
std::vector<CBlockIndex*> vpindexToConnect; std::vector<CBlockIndex*> vpindexToConnect;
@@ -3475,7 +3480,6 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
bool CheckBlockHeader(int32_t height,CBlockIndex *pindex, const CBlockHeader& blockhdr, CValidationState& state, bool fCheckPOW) bool CheckBlockHeader(int32_t height,CBlockIndex *pindex, const CBlockHeader& blockhdr, CValidationState& state, bool fCheckPOW)
{ {
uint8_t pubkey33[33];
// Check timestamp // Check timestamp
if ( 0 ) if ( 0 )
{ {
@@ -3516,6 +3520,36 @@ bool CheckBlockHeader(int32_t height,CBlockIndex *pindex, const CBlockHeader& bl
int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime); int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime);
int32_t komodo_reverify_blockcheck(CValidationState& state,int32_t height,CBlockIndex *pindex)
{
static int32_t oneshot;
CBlockIndex *tipindex; int32_t rewindtarget;
if ( oneshot == 0 && IsInitialBlockDownload() == 0 && (tipindex= chainActive.Tip()) != 0 )
{
// if 200 blocks behind longestchain and no blocks for 2 hours
if ( KOMODO_LONGESTCHAIN > height+200 )
{
if ( GetAdjustedTime() > tipindex->nTime+3600*2 )
{
fprintf(stderr,"tip.%d longest.%d newblock.%d lag.%d blocktime.%u\n",tipindex->nHeight,KOMODO_LONGESTCHAIN,height,(int32_t)(GetAdjustedTime() - tipindex->nTime),tipindex->nTime);
rewindtarget = tipindex->nHeight - 11;
fprintf(stderr,"rewindtarget <- %d\n",rewindtarget);
oneshot = 1;
while ( rewindtarget > 0 && (tipindex= chainActive.Tip()) != 0 && tipindex->nHeight > rewindtarget )
{
fprintf(stderr,"%d ",(int32_t)tipindex->nHeight);
InvalidateBlock(state,tipindex);
if ( !DisconnectTip(state) )
break;
}
tipindex = chainActive.Tip();
fprintf(stderr,"rewind done to %d\n",tipindex!=0?tipindex->nHeight:-1);
}
}
}
return(0);
}
bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidationState& state, bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidationState& state,
libzcash::ProofVerifier& verifier, libzcash::ProofVerifier& verifier,
bool fCheckPOW, bool fCheckMerkleRoot) bool fCheckPOW, bool fCheckMerkleRoot)
@@ -3527,10 +3561,13 @@ bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidat
// redundant with the call in AcceptBlockHeader. // redundant with the call in AcceptBlockHeader.
if (!CheckBlockHeader(height,pindex,block,state,fCheckPOW)) if (!CheckBlockHeader(height,pindex,block,state,fCheckPOW))
return false; return false;
//komodo_index2pubkey33(pubkey33,pindex,height);
komodo_block2pubkey33(pubkey33,(CBlock *)&block); komodo_block2pubkey33(pubkey33,(CBlock *)&block);
if ( fCheckPOW && !CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus()) ) if ( fCheckPOW && !CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus()) )
return state.DoS(50, error("CheckBlock(): proof of work failed"),REJECT_INVALID, "high-hash"); {
komodo_reverify_blockcheck(state,height,pindex);
return state.DoS(33, error("CheckBlock(): proof of work failed"),REJECT_INVALID, "high-hash");
}
// Check the merkle root. // Check the merkle root.
if (fCheckMerkleRoot) { if (fCheckMerkleRoot) {
bool mutated; bool mutated;
@@ -3704,7 +3741,10 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
if (ppindex) if (ppindex)
*ppindex = pindex; *ppindex = pindex;
if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK ) if ( pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK )
{
komodo_reverify_blockcheck(state,pindex->nHeight,pindex);
return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
}
if ( pindex != 0 && IsInitialBlockDownload() == 0 ) // jl777 debug test if ( pindex != 0 && IsInitialBlockDownload() == 0 ) // jl777 debug test
{ {
if (!CheckBlockHeader(pindex->nHeight,pindex, block, state)) if (!CheckBlockHeader(pindex->nHeight,pindex, block, state))
@@ -3734,11 +3774,10 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
if (!ContextualCheckBlockHeader(block, state, pindexPrev)) if (!ContextualCheckBlockHeader(block, state, pindexPrev))
{ {
pindex->nStatus |= BLOCK_FAILED_MASK; pindex->nStatus |= BLOCK_FAILED_MASK;
fprintf(stderr,"known block.%d failing ContextualCheckBlockHeader\n",(int32_t)pindex->nHeight); //fprintf(stderr,"known block.%d failing ContextualCheckBlockHeader\n",(int32_t)pindex->nHeight);
return false; return false;
} }
} }
return true; return true;
} }
@@ -3776,7 +3815,7 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
return false; return false;
if ( pindex == 0 ) if ( pindex == 0 )
{ {
fprintf(stderr,"AcceptBlock error null pindex\n"); //fprintf(stderr,"AcceptBlock error null pindex\n");
return false; return false;
} }
// Try to process all requested blocks that we don't have, but only // Try to process all requested blocks that we don't have, but only
@@ -3863,9 +3902,12 @@ bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBloc
LOCK(cs_main); LOCK(cs_main);
bool fRequested = MarkBlockAsReceived(pblock->GetHash()); bool fRequested = MarkBlockAsReceived(pblock->GetHash());
fRequested |= fForceProcessing; fRequested |= fForceProcessing;
if (!checked) { if (!checked)
{
if ( pfrom != 0 ) if ( pfrom != 0 )
{
Misbehaving(pfrom->GetId(), 1); Misbehaving(pfrom->GetId(), 1);
}
return error("%s: CheckBlock FAILED", __func__); return error("%s: CheckBlock FAILED", __func__);
} }
@@ -4040,6 +4082,7 @@ bool CheckDiskSpace(uint64_t nAdditionalBytes)
return true; return true;
} }
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
{ {
static int32_t didinit[1000]; long fsize,fpos; int32_t incr = 16*1024*1024; static int32_t didinit[1000]; long fsize,fpos; int32_t incr = 16*1024*1024;
@@ -4117,6 +4160,8 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
return pindexNew; return pindexNew;
} }
//void komodo_pindex_init(CBlockIndex *pindex,int32_t height);
bool static LoadBlockIndexDB() bool static LoadBlockIndexDB()
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
@@ -4132,6 +4177,7 @@ bool static LoadBlockIndexDB()
{ {
CBlockIndex* pindex = item.second; CBlockIndex* pindex = item.second;
vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex)); vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
//komodo_pindex_init(pindex,(int32_t)pindex->nHeight);
} }
sort(vSortedByHeight.begin(), vSortedByHeight.end()); sort(vSortedByHeight.begin(), vSortedByHeight.end());
BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight) BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
@@ -4180,6 +4226,7 @@ bool static LoadBlockIndexDB()
pindex->BuildSkip(); pindex->BuildSkip();
if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex))) if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
pindexBestHeader = pindex; pindexBestHeader = pindex;
//komodo_pindex_init(pindex,(int32_t)pindex->nHeight);
} }
// Load block file info // Load block file info
@@ -4208,6 +4255,7 @@ bool static LoadBlockIndexDB()
if (pindex->nStatus & BLOCK_HAVE_DATA) { if (pindex->nStatus & BLOCK_HAVE_DATA) {
setBlkDataFiles.insert(pindex->nFile); setBlkDataFiles.insert(pindex->nFile);
} }
//komodo_pindex_init(pindex,(int32_t)pindex->nHeight);
} }
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
{ {
@@ -4243,6 +4291,7 @@ bool static LoadBlockIndexDB()
if (pindex->pprev) { if (pindex->pprev) {
pindex->pprev->hashAnchorEnd = pindex->hashAnchor; pindex->pprev->hashAnchorEnd = pindex->hashAnchor;
} }
//komodo_pindex_init(pindex,(int32_t)pindex->nHeight);
} }
// Load pointer to end of best chain // Load pointer to end of best chain
@@ -4458,6 +4507,12 @@ bool RewindBlockIndex(const CChainParams& params)
pindexIter->nSequenceId = 0; pindexIter->nSequenceId = 0;
// Make sure it gets written // Make sure it gets written
setDirtyBlockIndex.insert(pindexIter); setDirtyBlockIndex.insert(pindexIter);
if (pindexIter == pindexBestInvalid)
{
fprintf(stderr,"Reset invalid block marker if it was pointing to this block\n");
pindexBestInvalid = NULL;
}
// Update indices // Update indices
setBlockIndexCandidates.erase(pindexIter); setBlockIndexCandidates.erase(pindexIter);
auto ret = mapBlocksUnlinked.equal_range(pindexIter->pprev); auto ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
@@ -5699,7 +5754,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
} }
if (!AcceptBlockHeader(header, state, &pindexLast)) { if (!AcceptBlockHeader(header, state, &pindexLast)) {
int nDoS; int nDoS;
if (state.IsInvalid(nDoS)) { if (state.IsInvalid(nDoS))
{
if (nDoS > 0) if (nDoS > 0)
Misbehaving(pfrom->GetId(), nDoS/nDoS); Misbehaving(pfrom->GetId(), nDoS/nDoS);
return error("invalid header received"); return error("invalid header received");