From 199d8db0193216edb6181479ee97cb20d013eddc Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 19:02:44 -1100 Subject: [PATCH 01/10] Fix zawy attack --- src/komodo_bitcoind.h | 2 +- src/main.cpp | 2 +- src/miner.cpp | 8 +++----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 84a84935b..e2bc2b7f2 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1425,7 +1425,7 @@ arith_uint256 komodo_adaptivepow_target(int32_t height,arith_uint256 bnTarget,ui tipdiff = (nTime - tipindex->nTime); if ( tipdiff > 13*ASSETCHAINS_BLOCKTIME ) diff = tipdiff; - if ( diff >= 13 * ASSETCHAINS_BLOCKTIME ) + if ( diff >= 13 * ASSETCHAINS_BLOCKTIME && (height < 3000 || tipdiff > 2*ASSETCHAINS_BLOCKTIME) ) { mult = diff - 12 * ASSETCHAINS_BLOCKTIME; mult = (mult / ASSETCHAINS_BLOCKTIME) * ASSETCHAINS_BLOCKTIME + ASSETCHAINS_BLOCKTIME / 2; diff --git a/src/main.cpp b/src/main.cpp index c47ae67cf..d83f5f169 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5288,7 +5288,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } // Check timestamp against prev - if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) + if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) { return state.Invalid(error("%s: block's timestamp is too early", __func__), REJECT_INVALID, "time-too-old"); diff --git a/src/miner.cpp b/src/miner.cpp index 6d404161f..fe7ca068a 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -123,7 +123,7 @@ public: void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) { - pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1 + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); // Updating time can change work required on testnet: if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) { @@ -567,7 +567,7 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 nLastBlockTx = nBlockTx; nLastBlockSize = nBlockSize; - blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); //pblock->nTime = blocktime + 1; pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); @@ -596,8 +596,6 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 else { blocktime = GetAdjustedTime(); - //if ( blocktime > pindexPrev->GetMedianTimePast()+60 ) - // blocktime = pindexPrev->GetMedianTimePast() + 60; siglen = komodo_staked(txStaked, pblock->nBits, &blocktime, &txtime, &utxotxid, &utxovout, &utxovalue, utxosig); // if you skip this check it will create a block too far into the future and not pass ProcessBlock or AcceptBlock. // This has been moved from the mining loop to save CPU, and to also make ac_staked work with the verus miner. @@ -642,7 +640,7 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 txNew.vout[0].nValue = GetBlockSubsidy(nHeight,consensusParams) + nFees; //fprintf(stderr,"mine ht.%d with %.8f\n",nHeight,(double)txNew.vout[0].nValue/COIN); txNew.nExpiryHeight = 0; - txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); if ( ASSETCHAINS_SYMBOL[0] == 0 && IS_KOMODO_NOTARY != 0 && My_notaryid >= 0 ) txNew.vout[0].nValue += 5000; From 4f64b197d20152e387b66b528f18dcb57d009dee Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 19:32:41 -1100 Subject: [PATCH 02/10] Fix --- src/main.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d83f5f169..8505b974b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5288,10 +5288,21 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } // Check timestamp against prev - if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) + if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 1000 ) { - return state.Invalid(error("%s: block's timestamp is too early", __func__), - REJECT_INVALID, "time-too-old"); + if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() ) + { + return state.Invalid(error("%s: block's timestamp is too early", __func__), + REJECT_INVALID, "time-too-old"); + } + } + else + { + if ( block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) + { + return state.Invalid(error("%s: block's timestamp is too early2", __func__), + REJECT_INVALID, "time-too-old"); + } } // Check that timestamp is not too far in the future From 733949c4f96e9c56e8b94b72abaa1b8b22cf9bb8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 19:38:14 -1100 Subject: [PATCH 03/10] <2000 --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8505b974b..301c0c071 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5288,10 +5288,11 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } // Check timestamp against prev - if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 1000 ) + if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 2000 ) { if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() ) { + fprintf(stderr,"too early %u vs %u\n",(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); return state.Invalid(error("%s: block's timestamp is too early", __func__), REJECT_INVALID, "time-too-old"); } @@ -5300,6 +5301,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta { if ( block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) { + fprintf(stderr,"too early2 %u vs %u\n",(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); return state.Invalid(error("%s: block's timestamp is too early2", __func__), REJECT_INVALID, "time-too-old"); } From 81eb5548c65eec1f55ce2e842bee7443c01dc632 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 19:45:26 -1100 Subject: [PATCH 04/10] <3000 --- src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 301c0c071..14c8e05ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5288,11 +5288,11 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } // Check timestamp against prev - if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 2000 ) + if ( ASSETCHAINS_ADAPTIVEPOW == 0 || nHeight < 3000 ) { if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast() ) { - fprintf(stderr,"too early %u vs %u\n",(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); + fprintf(stderr,"ht.%d too early %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); return state.Invalid(error("%s: block's timestamp is too early", __func__), REJECT_INVALID, "time-too-old"); } @@ -5301,7 +5301,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta { if ( block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) { - fprintf(stderr,"too early2 %u vs %u\n",(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); + fprintf(stderr,"ht.%d too early2 %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); return state.Invalid(error("%s: block's timestamp is too early2", __func__), REJECT_INVALID, "time-too-old"); } From 75c034113ef15837b656ffbc3bb69d5be83ab50f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 21:20:58 -1100 Subject: [PATCH 05/10] Calculate correct target in getblocktemplate for adaptivepow --- src/rpc/mining.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 4814d34dd..479edff45 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -837,7 +837,10 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) result.push_back(Pair("PoSperc", (int64_t)PoSperc)); result.push_back(Pair("ac_staked", (int64_t)ASSETCHAINS_STAKED)); result.push_back(Pair("origtarget", hashTarget.GetHex())); - } else result.push_back(Pair("target", hashTarget.GetHex())); + } + else if ( ASSETCHAINS_ADAPTIVEPOW != 0 ) + result.push_back(Pair("target",komodo_adaptivepow_target((int32_t)(pindexPrev->GetHeight()+1),hashTarget,pblock->nTime))); + else result.push_back(Pair("target", hashTarget.GetHex())); result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1)); result.push_back(Pair("mutable", aMutable)); result.push_back(Pair("noncerange", "00000000ffffffff")); From a74bd537a4dacd4204416c5346d92b15d4738bd9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 31 Jul 2019 21:22:17 -1100 Subject: [PATCH 06/10] .GetHex() --- src/rpc/mining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 479edff45..e31dbd396 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -839,7 +839,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) result.push_back(Pair("origtarget", hashTarget.GetHex())); } else if ( ASSETCHAINS_ADAPTIVEPOW != 0 ) - result.push_back(Pair("target",komodo_adaptivepow_target((int32_t)(pindexPrev->GetHeight()+1),hashTarget,pblock->nTime))); + result.push_back(Pair("target",komodo_adaptivepow_target((int32_t)(pindexPrev->GetHeight()+1),hashTarget,pblock->nTime).GetHex())); else result.push_back(Pair("target", hashTarget.GetHex())); result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1)); result.push_back(Pair("mutable", aMutable)); From 1cefd54af14fc10ae2eead39814db825e5875627 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Aug 2019 00:38:59 -1100 Subject: [PATCH 07/10] Fix zawy attack, thanks zawy! --- src/main.cpp | 4 ++-- src/miner.cpp | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 14c8e05ac..aeab7cadc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5299,9 +5299,9 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta } else { - if ( block.GetBlockTime() <= pindexPrev->GetMedianTimePast() + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME ) + if ( block.GetBlockTime() <= pindexPrev->nTime ) { - fprintf(stderr,"ht.%d too early2 %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->GetMedianTimePast()); + fprintf(stderr,"ht.%d too early2 %u vs %u\n",(int32_t)nHeight,(uint32_t)block.GetBlockTime(),(uint32_t)pindexPrev->nTime); return state.Invalid(error("%s: block's timestamp is too early2", __func__), REJECT_INVALID, "time-too-old"); } diff --git a/src/miner.cpp b/src/miner.cpp index fe7ca068a..01a41f5b8 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -123,7 +123,9 @@ public: void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) { - pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1 + 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else pblock->nTime = std::max(pindexPrev->nTime+1, GetAdjustedTime()); // Updating time can change work required on testnet: if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) { @@ -567,7 +569,9 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 nLastBlockTx = nBlockTx; nLastBlockSize = nBlockSize; - blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else blocktime = 1 + std::max(pindexPrev->nTime+1, GetAdjustedTime()); //pblock->nTime = blocktime + 1; pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); @@ -640,7 +644,10 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 txNew.vout[0].nValue = GetBlockSubsidy(nHeight,consensusParams) + nFees; //fprintf(stderr,"mine ht.%d with %.8f\n",nHeight,(double)txNew.vout[0].nValue/COIN); txNew.nExpiryHeight = 0; - txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1+ 2*ASSETCHAINS_ADAPTIVEPOW*ASSETCHAINS_BLOCKTIME, GetAdjustedTime()); + if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) + txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + else txNew.nLockTime = std::max(pindexPrev->nTime+1, GetAdjustedTime()); + if ( ASSETCHAINS_SYMBOL[0] == 0 && IS_KOMODO_NOTARY != 0 && My_notaryid >= 0 ) txNew.vout[0].nValue += 5000; From 4aa44715aa4e5c39fc54323d3fc5f6a0af2380f1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Aug 2019 00:43:39 -1100 Subject: [PATCH 08/10] Syntax --- src/miner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 01a41f5b8..b7447500d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -125,7 +125,7 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, { if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - else pblock->nTime = std::max(pindexPrev->nTime+1, GetAdjustedTime()); + else pblock->nTime = std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); // Updating time can change work required on testnet: if (consensusParams.nPowAllowMinDifficultyBlocksAfterHeight != boost::none) { @@ -646,7 +646,7 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 txNew.nExpiryHeight = 0; if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) txNew.nLockTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - else txNew.nLockTime = std::max(pindexPrev->nTime+1, GetAdjustedTime()); + else txNew.nLockTime = std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); if ( ASSETCHAINS_SYMBOL[0] == 0 && IS_KOMODO_NOTARY != 0 && My_notaryid >= 0 ) From 41f752ff4bf1ba4d452a03b2c1e3fc5425076627 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Aug 2019 00:44:56 -1100 Subject: [PATCH 09/10] int64_t --- src/miner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/miner.cpp b/src/miner.cpp index b7447500d..d19faee7d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -571,7 +571,7 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32 nLastBlockSize = nBlockSize; if ( ASSETCHAINS_ADAPTIVEPOW == 0 ) blocktime = 1 + std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); - else blocktime = 1 + std::max(pindexPrev->nTime+1, GetAdjustedTime()); + else blocktime = 1 + std::max((int64_t)(pindexPrev->nTime+1), GetAdjustedTime()); //pblock->nTime = blocktime + 1; pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus()); From 716e089fab83c4f62c1c14faf4213cf8a8f7a619 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 1 Aug 2019 00:56:26 -1100 Subject: [PATCH 10/10] Fix AddCClibtxfee --- src/cc/cclib.cpp | 2 +- src/init.cpp | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index dddd59f59..4c182e721 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -542,7 +542,7 @@ int64_t AddCClibtxfee(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKe char coinaddr[64]; int64_t nValue,txfee = 10000; uint256 txid,hashBlock; CTransaction vintx; int32_t vout; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); - SetCCunspents(unspentOutputs,coinaddr,true); + SetCCunspents(unspentOutputs,coinaddr,false); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; diff --git a/src/init.cpp b/src/init.cpp index 51d6cb435..903ca8555 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1911,11 +1911,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) PruneAndFlush(); } } - if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) != 0 ) - nLocalServices |= NODE_ADDRINDEX; - if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) - nLocalServices |= NODE_SPENTINDEX; -fprintf(stderr,"nLocalServices %llx %d, %d\n",(long long)nLocalServices,GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)); + if ( KOMODO_NSPV > 0 ) + { + if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) != 0 ) + nLocalServices |= NODE_ADDRINDEX; + if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) != 0 ) + nLocalServices |= NODE_SPENTINDEX; + fprintf(stderr,"nLocalServices %llx %d, %d\n",(long long)nLocalServices,GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX),GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)); + } // ********************************************************* Step 10: import blocks if (mapArgs.count("-blocknotify"))