Merge pull request #577 from jl777/dev

Dev
This commit is contained in:
jl777
2018-04-15 22:15:38 +03:00
committed by GitHub
3 changed files with 1233 additions and 1164 deletions

View File

@@ -199,7 +199,7 @@ const char *Notaries_elected1[][2] =
int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp) int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp)
{ {
static uint8_t elected_pubkeys0[64][33],elected_pubkeys1[64][33],did0,did1; static uint8_t elected_pubkeys0[64][33],elected_pubkeys1[64][33],did0,did1,n0,n1;
int32_t i,htind,n; uint64_t mask = 0; struct knotary_entry *kp,*tmp; int32_t i,htind,n; uint64_t mask = 0; struct knotary_entry *kp,*tmp;
if ( timestamp == 0 && ASSETCHAINS_SYMBOL[0] != 0 ) if ( timestamp == 0 && ASSETCHAINS_SYMBOL[0] != 0 )
timestamp = komodo_heightstamp(height); timestamp = komodo_heightstamp(height);
@@ -211,29 +211,30 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestam
{ {
if ( did0 == 0 ) if ( did0 == 0 )
{ {
n = (int32_t)(sizeof(Notaries_elected0)/sizeof(*Notaries_elected0)); n0 = (int32_t)(sizeof(Notaries_elected0)/sizeof(*Notaries_elected0));
for (i=0; i<n; i++) for (i=0; i<n0; i++)
decode_hex(elected_pubkeys0[i],33,(char *)Notaries_elected0[i][1]); decode_hex(elected_pubkeys0[i],33,(char *)Notaries_elected0[i][1]);
did0 = 1; did0 = 1;
} }
memcpy(pubkeys,elected_pubkeys0,n * 33); memcpy(pubkeys,elected_pubkeys0,n0 * 33);
//if ( ASSETCHAINS_SYMBOL[0] != 0 ) //if ( ASSETCHAINS_SYMBOL[0] != 0 )
//fprintf(stderr,"%s height.%d t.%u elected.%d notaries\n",ASSETCHAINS_SYMBOL,height,timestamp,n); //fprintf(stderr,"%s height.%d t.%u elected.%d notaries\n",ASSETCHAINS_SYMBOL,height,timestamp,n0);
return(n0);
} }
else //if ( (timestamp != 0 && timestamp <= KOMODO_NOTARIES_TIMESTAMP2) || height <= KOMODO_NOTARIES_HEIGHT2 ) else //if ( (timestamp != 0 && timestamp <= KOMODO_NOTARIES_TIMESTAMP2) || height <= KOMODO_NOTARIES_HEIGHT2 )
{ {
if ( did1 == 0 ) if ( did1 == 0 )
{ {
n = (int32_t)(sizeof(Notaries_elected1)/sizeof(*Notaries_elected1)); n1 = (int32_t)(sizeof(Notaries_elected1)/sizeof(*Notaries_elected1));
for (i=0; i<n; i++) for (i=0; i<n1; i++)
decode_hex(elected_pubkeys1[i],33,(char *)Notaries_elected1[i][1]); decode_hex(elected_pubkeys1[i],33,(char *)Notaries_elected1[i][1]);
if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
fprintf(stderr,"%s height.%d t.%u elected.%d notaries2\n",ASSETCHAINS_SYMBOL,height,timestamp,n); fprintf(stderr,"%s height.%d t.%u elected.%d notaries2\n",ASSETCHAINS_SYMBOL,height,timestamp,n1);
did1 = 1; did1 = 1;
} }
memcpy(pubkeys,elected_pubkeys1,n * 33); memcpy(pubkeys,elected_pubkeys1,n1 * 33);
return(n1);
} }
return(n);
} }
htind = height / KOMODO_ELECTION_GAP; htind = height / KOMODO_ELECTION_GAP;
if ( htind >= KOMODO_MAXBLOCKS / KOMODO_ELECTION_GAP ) if ( htind >= KOMODO_MAXBLOCKS / KOMODO_ELECTION_GAP )

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,38 @@ 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 ( 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
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 +3563,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 +3743,15 @@ 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);
if ( KOMODO_LONGESTCHAIN != 0 && pindex->nHeight > KOMODO_LONGESTCHAIN-100 )
return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate"); 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 if ( pindex != 0 && IsInitialBlockDownload() == 0 ) // jl777 debug test
{ {
if (!CheckBlockHeader(pindex->nHeight,pindex, block, state)) if (!CheckBlockHeader(pindex->nHeight,pindex, block, state))
@@ -3734,11 +3781,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 +3822,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 +3909,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 +4089,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 +4167,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 +4184,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 +4233,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 +4262,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 +4298,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 +4514,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 +5761,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");

View File

@@ -165,6 +165,7 @@ UniValue getpeerinfo(const UniValue& params, bool fHelp)
return ret; return ret;
} }
int32_t KOMODO_LONGESTCHAIN;
int32_t komodo_longestchain() int32_t komodo_longestchain()
{ {
int32_t ht,n=0,num=0,maxheight=0,height = 0; int32_t ht,n=0,num=0,maxheight=0,height = 0;
@@ -191,8 +192,12 @@ int32_t komodo_longestchain()
height = ht; height = ht;
} }
if ( num > (n >> 1) ) if ( num > (n >> 1) )
{
KOMODO_LONGESTCHAIN = height;
return(height); return(height);
else return(0); }
KOMODO_LONGESTCHAIN = 0;
return(0);
} }
UniValue addnode(const UniValue& params, bool fHelp) UniValue addnode(const UniValue& params, bool fHelp)