diff --git a/src/komodo.h b/src/komodo.h index 8e1c051f5..c47d51d26 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -181,6 +181,7 @@ const char *Notaries[64][2] = int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,NOTARIZED_HEIGHT,Num_nutxos; std::string NOTARY_PUBKEY; +uint8_t NOTARY_PUBKEY33[33]; uint256 NOTARIZED_HASH,NOTARIZED_BTCHASH; struct nutxo_entry { uint256 txhash; uint64_t voutmask; int32_t notaryid; }; struct nutxo_entry NUTXOS[10000]; @@ -205,6 +206,7 @@ int32_t komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numno #endif if ( fp == 0 ) { + decode_hex(NOTARY_PUBKEY33,33,NOTARY_PUBKEY.ToString().c_str()); if ( (fp= fopen(fname,"rb+")) != 0 ) { while ( (func= fgetc(fp)) != EOF ) @@ -513,7 +515,19 @@ void komodo_disconnect(CBlockIndex *pindex,CBlock& block) komodo_stateupdate(-pindex->nHeight,0,0,0,zero,0,0); } -int32_t komodo_blockindexcheck(CBlockIndex *pindex,uint32_t *nBitsp) +void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex) +{ + +} + +void komodo_block2pubkey33(uint8_t *pubkey33,CBlock& block) +{ + uint8_t *ptr; + ptr = block.vtx[0].vout[0].scriptPubKey.data(); + memcpy(pubkey33,ptr+1,33); +} + +/*int32_t komodo_blockindexcheck(CBlockIndex *pindex,uint32_t *nBitsp) { // 1 -> valid notary block, change nBits to KOMODO_MINDIFF_NBITS // -1 -> invalid, ie, prior to notarized block @@ -560,6 +574,6 @@ int32_t komodo_blockhdrcheck(CBlockHeader& blockhdr,uint32_t *nBitsp) int32_t komodo_blockcheck(CBlock& block,uint32_t *nBitsp) { return(komodo_blockhdrcheck(block,nBitsp)); -} +}*/ #endif diff --git a/src/main.cpp b/src/main.cpp index aafdf5733..b0a7431f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1278,7 +1278,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock if (pindexSlow) { CBlock block; - if (ReadBlockFromDisk(block, pindexSlow,0)) { + if (ReadBlockFromDisk(block, pindexSlow)) { BOOST_FOREACH(const CTransaction &tx, block.vtx) { if (tx.GetHash() == hash) { txOut = tx; @@ -1333,9 +1333,9 @@ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::M return true; } -bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos,int32_t skipkomodo) +bool ReadBlockFromDisk(int32_t height,CBlock& block, const CDiskBlockPos& pos) { - int32_t retval=0; uint32_t nBits; + uint8_t pubkey33[33]; block.SetNull(); // Open history file to read @@ -1350,22 +1350,16 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos,int32_t skipkomod catch (const std::exception& e) { return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString()); } - // Check the header - nBits = block.nBits; - if ( skipkomodo != 0 || (retval= komodo_blockcheck(block,&nBits)) == 0 ) - { - if (!(CheckEquihashSolution(&block, Params()) && CheckProofOfWork(block.GetHash(), nBits, Params().GetConsensus()))) - return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); - } - else if ( retval < 0 ) - return(false); + komodo_block2pubkey33(pubkey33,block); + if (!(CheckEquihashSolution(&block, Params()) && CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus()))) + return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); return true; } -bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex,int32_t skipkomodo) +bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex) { - if (!ReadBlockFromDisk(block, pindex->GetBlockPos(),skipkomodo)) + if (!ReadBlockFromDisk(pindex->nHeight,block, pindex->GetBlockPos())) return false; if (block.GetHash() != pindex->GetBlockHash()) return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s", @@ -2405,7 +2399,7 @@ bool static DisconnectTip(CValidationState &state) { mempool.check(pcoinsTip); // Read block from disk. CBlock block; - if (!ReadBlockFromDisk(block, pindexDelete,0)) + if (!ReadBlockFromDisk(block, pindexDelete)) return AbortNode(state, "Failed to read block"); // Apply the block atomically to the chain state. uint256 anchorBeforeDisconnect = pcoinsTip->GetBestAnchor(); @@ -2468,7 +2462,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * int64_t nTime1 = GetTimeMicros(); CBlock block; if (!pblock) { - if (!ReadBlockFromDisk(block, pindexNew,0)) + if (!ReadBlockFromDisk(block, pindexNew)) return AbortNode(state, "Failed to read block"); pblock = █ } @@ -2956,25 +2950,20 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne return true; } -bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW) +bool CheckBlockHeader(int32_t height,const CBlockHeader& block, CValidationState& state, bool fCheckPOW) { - int32_t retval; uint32_t nBits; + uint8_t pubkey33[33]; // Check timestamp if (block.GetBlockTime() > GetAdjustedTime() + 60) return state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),REJECT_INVALID, "time-too-new"); - nBits = block.nBits; - if ( (retval= komodo_blockhdrcheck(*(CBlockHeader *)&block,&nBits)) == 0 ) - { - // Check Equihash solution is valid - if ( fCheckPOW && !CheckEquihashSolution(&block, Params()) ) - return state.DoS(100, error("CheckBlockHeader(): Equihash solution invalid"),REJECT_INVALID, "invalid-solution"); - - // Check proof of work matches claimed amount - if ( fCheckPOW && !CheckProofOfWork(block.GetHash(), nBits, Params().GetConsensus()) ) - return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),REJECT_INVALID, "high-hash"); - } - else if ( retval < 0 ) // komodo rejects block, ie. prior to notarized blockhash - return(false); + // Check Equihash solution is valid + if ( fCheckPOW && !CheckEquihashSolution(&block, Params()) ) + return state.DoS(100, error("CheckBlockHeader(): Equihash solution invalid"),REJECT_INVALID, "invalid-solution"); + + // Check proof of work matches claimed amount + komodo_block2pubkey33(pubkey33,block); + if ( fCheckPOW && !CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus()) ) + return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),REJECT_INVALID, "high-hash"); return true; } @@ -3606,7 +3595,7 @@ bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth break; CBlock block; // check level 0: read from disk - if (!ReadBlockFromDisk(block, pindex,0)) + if (!ReadBlockFromDisk(block, pindex)) return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); // check level 1: verify block validity if (nCheckLevel >= 1 && !CheckBlock(block, state)) @@ -3646,7 +3635,7 @@ bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50)))); pindex = chainActive.Next(pindex); CBlock block; - if (!ReadBlockFromDisk(block, pindex,0)) + if (!ReadBlockFromDisk(block, pindex)) return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); if (!ConnectBlock(block, state, pindex, coins)) return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); @@ -3819,7 +3808,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp) std::pair::iterator, std::multimap::iterator> range = mapBlocksUnknownParent.equal_range(head); while (range.first != range.second) { std::multimap::iterator it = range.first; - if (ReadBlockFromDisk(block, it->second,0)) + if (ReadBlockFromDisk(block, it->second)) { LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(), head.ToString()); @@ -4178,7 +4167,7 @@ void static ProcessGetData(CNode* pfrom) { // Send block from disk CBlock block; - if (!ReadBlockFromDisk(block, (*mi).second,0)) + if (!ReadBlockFromDisk(block, (*mi).second)) assert(!"cannot load block from disk"); if (inv.type == MSG_BLOCK) pfrom->PushMessage("block", block); diff --git a/src/main.h b/src/main.h index 0b8d71db0..aec92435c 100644 --- a/src/main.h +++ b/src/main.h @@ -393,8 +393,8 @@ public: /** Functions for disk access for blocks */ bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart); -bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos,int32_t skipkomodo); -bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex,int32_t skipkomodo); +bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos); +bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex); /** Functions for validating blocks and updating the block tree */ diff --git a/src/pow.cpp b/src/pow.cpp index a382a3eea..7a8d2c96c 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -106,7 +106,7 @@ bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& param return true; } -bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) +bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params& params) { bool fNegative; bool fOverflow; diff --git a/src/pow.h b/src/pow.h index 30d0f5e63..3edec74ac 100644 --- a/src/pow.h +++ b/src/pow.h @@ -25,7 +25,7 @@ unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams&); /** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */ -bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&); +bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params&); arith_uint256 GetBlockProof(const CBlockIndex& block); /** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */ diff --git a/src/rest.cpp b/src/rest.cpp index c21b48484..7c238d506 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -200,7 +200,7 @@ static bool rest_block(AcceptedConnection* conn, if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) throw RESTERR(HTTP_NOT_FOUND, hashStr + " not available (pruned data)"); - if (!ReadBlockFromDisk(block, pblockindex,0)) + if (!ReadBlockFromDisk(block, pblockindex)) throw RESTERR(HTTP_NOT_FOUND, hashStr + " not found"); } diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 7f7d21b78..4c92979b2 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -326,7 +326,7 @@ Value getblock(const Array& params, bool fHelp) if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)"); - if(!ReadBlockFromDisk(block, pblockindex,0)) + if(!ReadBlockFromDisk(block, pblockindex)) throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); if (!fVerbose) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 1e22bec1c..dd78a55b1 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -115,7 +115,7 @@ Value getgenerate(const Array& params, bool fHelp) return GetBoolArg("-gen", false); } -int32_t komodo_blockcheck(CBlock& block,uint32_t *nBitsp); +extern uint8_t NOTARY_PUBKEY33[33]; Value generate(const Array& params, bool fHelp) { @@ -193,15 +193,8 @@ Value generate(const Array& params, bool fHelp) std::function)> validBlock = [&pblock](std::vector soln) { - int32_t retval; uint32_t nBits; pblock->nSolution = soln; - nBits = pblock->nBits; - if ( (retval= komodo_blockcheck(*pblock,&nBits)) == 0 ) - { - return CheckProofOfWork(pblock->GetHash(), nBits, Params().GetConsensus()); - } else if ( retval < 0 ) // komodo rejects, ie. prior to notarized blockhash - return(false); - return true; + return CheckProofOfWork(nHeight,NOTARY_PUBKEY33,pblock->GetHash(), pblock->nBits, Params().GetConsensus()); }; if (EhBasicSolveUncancellable(n, k, curr_state, validBlock)) goto endloop; diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 2a767616d..5cca9df9d 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -303,7 +303,7 @@ Value gettxoutproof(const Array& params, bool fHelp) } CBlock block; - if(!ReadBlockFromDisk(block, pblockindex,0)) + if(!ReadBlockFromDisk(block, pblockindex)) throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); unsigned int ntxFound = 0; diff --git a/src/txdb.cpp b/src/txdb.cpp index 27cda25d4..ea34d8b77 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -306,15 +306,10 @@ bool CBlockTreeDB::LoadBlockIndexGuts() pindexNew->nSolution = diskindex.nSolution; pindexNew->nStatus = diskindex.nStatus; pindexNew->nTx = diskindex.nTx; - - int32_t retval; uint32_t nBits; - nBits = pindexNew->nBits; - if ( (retval= komodo_blockindexcheck(pindexNew,&nBits)) == 0 ) - { - if (!CheckProofOfWork(pindexNew->GetBlockHash(), nBits, Params().GetConsensus())) - return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString()); - } else if ( retval < 0 ) // komodo rejects, ie. prior to notarized blockhash - return(false); + uint8_t pubkey33[33]; + komodo_index2pubkey33(pubkey33,pindexNew); + if (!CheckProofOfWork(pindexNew->height,pubkey33,pindexNew->GetBlockHash(), pindexNew->nBits, Params().GetConsensus())) + return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString()); pcursor->Next(); } else { break; // if shutdown requested or finished loading block index diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 239b8976c..1a865a966 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -668,7 +668,7 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, const CBlock* pblock {pblockIn}; CBlock block; if (!pblock) { - ReadBlockFromDisk(block, pindex,0); + ReadBlockFromDisk(block, pindex); pblock = █ } @@ -1651,7 +1651,7 @@ void CWallet::WitnessNoteCommitment(std::vector commitments, while (pindex) { CBlock block; - ReadBlockFromDisk(block, pindex,0); + ReadBlockFromDisk(block, pindex); BOOST_FOREACH(const CTransaction& tx, block.vtx) { @@ -1727,7 +1727,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate) ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100)))); CBlock block; - ReadBlockFromDisk(block, pindex,0); + ReadBlockFromDisk(block, pindex); BOOST_FOREACH(CTransaction& tx, block.vtx) { if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))