From 3350160a6c497d156275b2b534c0b4577047a24a Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 18 Jul 2018 16:02:53 +0000 Subject: [PATCH 1/5] Fix bug where finding index entries of other types, such as timestampindex, stopped our iterator --- src/txdb.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index cb0358403..5608cbb07 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -411,6 +411,7 @@ extern UniValue CBlockTreeDB::Snapshot() pcursor->SeekToLast(); int64_t startingHeight = chainActive.Height(); + //fprintf(stderr, "starting_height=%li\n", startingHeight); while (pcursor->Valid()) { @@ -423,7 +424,8 @@ extern UniValue CBlockTreeDB::Snapshot() ssKey >> chType; ssKey >> indexKey; - if ( chType == DB_ADDRESSUNSPENTINDEX ) + //fprintf(stderr, "chType=%d\n", chType); + if (chType == DB_ADDRESSUNSPENTINDEX) { try { leveldb::Slice slValue = pcursor->value(); @@ -435,7 +437,7 @@ extern UniValue CBlockTreeDB::Snapshot() std::map ::iterator pos = addressAmounts.find(address); if (pos == addressAmounts.end()) { // insert new address + utxo amount - //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); addressAmounts[address] = nValue; totalAddresses++; } else { @@ -446,9 +448,10 @@ extern UniValue CBlockTreeDB::Snapshot() //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); total += nValue; } catch (const std::exception& e) { - return error("failed to get address index value"); + // this should not happen normally, but maybe if an index is corrupt + fprintf(stderr, "failed to get address index value\n"); } - } else { break; } + } } catch (const std::exception& e) { fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); break; @@ -477,9 +480,11 @@ extern UniValue CBlockTreeDB::Snapshot() addressesSorted.push_back(obj); } - result.push_back(make_pair("addresses", addressesSorted)); - result.push_back(make_pair("total", total / COIN )); - result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses )); + if (totalAddresses > 0) { + result.push_back(make_pair("addresses", addressesSorted)); + result.push_back(make_pair("total", total / COIN )); + result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses )); + } // Total number of addresses in this snaphot result.push_back(make_pair("total_addresses", totalAddresses)); // The snapshot began at this block height From 0ce374b7c7e781f82f4955e4c97df79ade05325c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 18 Jul 2018 05:04:15 -1100 Subject: [PATCH 2/5] Remove height triggers and add PoWtarget in getblocktemplate --- src/komodo_bitcoind.h | 25 +++++++++++++++---------- src/rpcmining.cpp | 11 ++++++++++- src/wallet/rpcwallet.cpp | 10 ++++++---- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 5564a79a5..56213206e 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1076,11 +1076,11 @@ int32_t komodo_isrealtime(int32_t *kmdheightp) int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t cmptime,int32_t dispflag) { - if ( KOMODO_REWIND == 0 && (ASSETCHAINS_SYMBOL[0] == 0 || ASSETCHAINS_STAKED != 0) && (int64_t)tx.nLockTime >= LOCKTIME_THRESHOLD ) //1473793441 ) + if ( KOMODO_REWIND == 0 && ASSETCHAINS_SYMBOL[0] == 0 && (int64_t)tx.nLockTime >= LOCKTIME_THRESHOLD ) //1473793441 ) { - if ( txheight > 246748 || ASSETCHAINS_STAKED != 0 ) + if ( txheight > 246748 ) { - if ( txheight < 247205 && ASSETCHAINS_STAKED == 0 ) + if ( txheight < 247205 ) cmptime -= 16000; if ( (int64_t)tx.nLockTime < cmptime-KOMODO_MAXMEMPOOLTIME ) { @@ -1198,7 +1198,7 @@ uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeight,uint256 txid,int32_t vout,uint32_t blocktime,uint32_t prevtime,char *destaddr) { - bool fNegative,fOverflow; uint8_t hashbuf[256]; char address[64]; bits256 addrhash; arith_uint256 hashval,mindiff,ratio,coinage256; uint256 hash,pasthash; int32_t diff=0,segid,minage,i,iter=0; uint32_t txtime,winner = 0 ; uint64_t value,coinage; + bool fNegative,fOverflow; uint8_t hashbuf[256]; char address[64]; bits256 addrhash; arith_uint256 hashval,mindiff,ratio,coinage256; uint256 hash,pasthash; int32_t diff=0,segid,minage,i,iter=0; uint32_t txtime,segid32,winner = 0 ; uint64_t value,coinage; txtime = komodo_txtime2(&value,txid,vout,address); if ( validateflag == 0 ) { @@ -1221,13 +1221,16 @@ uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeigh ratio = (mindiff / bnTarget); if ( (minage= nHeight*3) > 6000 ) // about 100 blocks minage = 6000; - vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address)); + komodo_segids(hashbuf,nHeight-101,100); + segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); + segid = ((nHeight + segid32) & 0x3f); + /*vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address)); segid = ((nHeight + addrhash.uints[0]) & 0x3f); komodo_segids(hashbuf,nHeight-101,100); memcpy(&hashbuf[100],&addrhash,sizeof(addrhash)); memcpy(&hashbuf[100+sizeof(addrhash)],&txid,sizeof(txid)); memcpy(&hashbuf[100+sizeof(addrhash)+sizeof(txid)],&vout,sizeof(vout)); - vcalc_sha256(0,(uint8_t *)&hash,hashbuf,100 + (int32_t)sizeof(uint256)*2 + sizeof(vout)); + vcalc_sha256(0,(uint8_t *)&hash,hashbuf,100 + (int32_t)sizeof(uint256)*2 + sizeof(vout));*/ for (iter=0; iter<180; iter++) { if ( blocktime+iter+segid*2 < txtime+minage ) @@ -1243,12 +1246,14 @@ uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeigh if ( iter > 0 ) diff += segid*2; coinage = (value * diff); - if ( nHeight >= 2500 && blocktime+iter+segid*2 > prevtime+180 ) - coinage *= ((blocktime+iter+segid*2) - (prevtime+60)); + if ( blocktime+iter+segid*2 > prevtime+480 ) + coinage *= ((blocktime+iter+segid*2) - (prevtime+400)); + //if ( nHeight >= 2500 && blocktime+iter+segid*2 > prevtime+180 ) + // coinage *= ((blocktime+iter+segid*2) - (prevtime+60)); coinage256 = arith_uint256(coinage+1); hashval = ratio * (UintToArith256(hash) / coinage256); - if ( nHeight >= 900 && nHeight < 916 ) - hashval = (hashval / coinage256); + //if ( nHeight >= 900 && nHeight < 916 ) + // hashval = (hashval / coinage256); if ( hashval <= bnTarget ) { winner = 1; diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 47fbeec30..8e0723d6d 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -438,6 +438,7 @@ static UniValue BIP22ValidationResult(const CValidationState& state) UniValue getblocktemplate(const UniValue& params, bool fHelp) { + extern uint64_t ASSETCHAINS_STAKED; if (fHelp || params.size() > 1) throw runtime_error( "getblocktemplate ( \"jsonrequestobject\" )\n" @@ -727,7 +728,15 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue)); } result.push_back(Pair("longpollid", chainActive.LastTip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast))); - result.push_back(Pair("target", hashTarget.GetHex())); + if ( ASSETCHAINS_STAKED != 0 ) + { + arith_uint256 POWtarget; int32_t PosPerc; + POWtarget = komodo_PoWtarget(&PoSperc,hashTarget,(int32_t)(pindexPrev->nHeight+1),ASSETCHAINS_STAKED); + result.push_back(Pair("target", POWtarget.GetHex())); + 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())); result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1)); result.push_back(Pair("mutable", aMutable)); result.push_back(Pair("noncerange", "00000000ffffffff")); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 02cea5bef..176491b8b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4636,12 +4636,14 @@ arith_uint256 _komodo_eligible(struct komodo_staking *kp,arith_uint256 ratio,uin if ( iter > 0 ) diff += segid*2; coinage = ((uint64_t)kp->nValue/COIN * diff); - if ( nHeight >= 2500 && blocktime+iter+segid*2 > prevtime+180 ) - coinage *= ((blocktime+iter+segid*2) - (prevtime+60)); + if ( blocktime+iter+segid*2 > prevtime+480 ) + coinage *= ((blocktime+iter+segid*2) - (prevtime+400)); + //if ( nHeight >= 2500 && blocktime+iter+segid*2 > prevtime+180 ) + // coinage *= ((blocktime+iter+segid*2) - (prevtime+60)); coinage256 = arith_uint256(coinage+1); hashval = ratio * (kp->hashval / coinage256); - if ( nHeight >= 900 && nHeight < 916 ) - hashval = (hashval / coinage256); + //if ( nHeight >= 900 && nHeight < 916 ) + // hashval = (hashval / coinage256); return(hashval); } From a9e904d9dd7d4a63d0a8983e54bdb91baf62755f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 18 Jul 2018 05:08:55 -1100 Subject: [PATCH 3/5] Fix --- src/rpcmining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 8e0723d6d..55955f6e6 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -730,7 +730,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp) result.push_back(Pair("longpollid", chainActive.LastTip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast))); if ( ASSETCHAINS_STAKED != 0 ) { - arith_uint256 POWtarget; int32_t PosPerc; + arith_uint256 POWtarget; int32_t PoSperc; POWtarget = komodo_PoWtarget(&PoSperc,hashTarget,(int32_t)(pindexPrev->nHeight+1),ASSETCHAINS_STAKED); result.push_back(Pair("target", POWtarget.GetHex())); result.push_back(Pair("PoSperc", (int64_t)PoSperc)); From 4ed779073db32e311ca1536989a092f629b9cddd Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 18 Jul 2018 05:10:17 -1100 Subject: [PATCH 4/5] Test --- src/rpcmining.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 55955f6e6..4c4d2eed5 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -32,6 +32,8 @@ using namespace std; +extern uint64_t ASSETCHAINS_STAKED; +arith_uint256 komodo_PoWtarget(int32_t *percPoSp,arith_uint256 target,int32_t height,int32_t goalperc); /** * Return average network hashes per second based on the last 'lookup' blocks, @@ -438,7 +440,6 @@ static UniValue BIP22ValidationResult(const CValidationState& state) UniValue getblocktemplate(const UniValue& params, bool fHelp) { - extern uint64_t ASSETCHAINS_STAKED; if (fHelp || params.size() > 1) throw runtime_error( "getblocktemplate ( \"jsonrequestobject\" )\n" From a4d41984f67bcc7472469b564de92b8ab1505eca Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 18 Jul 2018 19:44:16 +0000 Subject: [PATCH 5/5] Modify how we iterate and deal with the end of iteration, since iter->Valid seems to always be true --- src/txdb.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index 5608cbb07..af6cd0fbf 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -402,61 +402,59 @@ bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &addr extern UniValue CBlockTreeDB::Snapshot() { char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address; - boost::scoped_ptr pcursor(NewIterator()); + boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; UniValue result(UniValue::VOBJ); result.push_back(Pair("start_time", time(NULL))); - //pcursor->SeekToFirst(); - pcursor->SeekToLast(); - int64_t startingHeight = chainActive.Height(); - //fprintf(stderr, "starting_height=%li\n", startingHeight); - - while (pcursor->Valid()) + fprintf(stderr, "Starting snapshot at height %li\n", startingHeight); + for (iter->SeekToLast(); iter->Valid(); iter->Prev()) { boost::this_thread::interruption_point(); try { - leveldb::Slice slKey = pcursor->key(); + leveldb::Slice slKey = iter->key(); CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); CAddressIndexIteratorKey indexKey; - ssKey >> chType; - ssKey >> indexKey; + + ssKey >> chType; + ssKey >> indexKey; //fprintf(stderr, "chType=%d\n", chType); if (chType == DB_ADDRESSUNSPENTINDEX) { try { - leveldb::Slice slValue = pcursor->value(); + leveldb::Slice slValue = iter->value(); CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); CAmount nValue; ssValue >> nValue; getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); + std::map ::iterator pos = addressAmounts.find(address); if (pos == addressAmounts.end()) { // insert new address + utxo amount - fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); addressAmounts[address] = nValue; totalAddresses++; } else { // update unspent tally for this address + //fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue); addressAmounts[address] += nValue; } - //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); total += nValue; + } catch (const std::exception& e) { - // this should not happen normally, but maybe if an index is corrupt - fprintf(stderr, "failed to get address index value\n"); + fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what()); + break; } - } + } } catch (const std::exception& e) { - fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); + fprintf(stderr, "DONE reading index entries\n"); break; } - pcursor->Prev(); } UniValue addresses(UniValue::VARR);