From 84eabd0d59a56b98e2bed897f8b8a6787750b386 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 05:43:18 -1100 Subject: [PATCH 01/35] Skip null privacy address --- src/komodo_bitcoind.h | 13 ++++++++++--- src/wallet/rpcwallet.cpp | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index da244e079..eb649be99 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1546,7 +1546,7 @@ int32_t komodo_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height) int64_t komodo_newcoins(int64_t *zfundsp,int32_t nHeight,CBlock *pblock) { - int32_t i,j,m,n,vout; uint8_t *script; uint256 txid,hashBlock; int64_t zfunds=0,vinsum=0,voutsum=0; + CTxDestination address; int32_t i,j,m,n,vout; uint8_t *script; uint256 txid,hashBlock; int64_t zfunds=0,vinsum=0,voutsum=0; n = pblock->vtx.size(); for (i=0; i 0 ) { for (j=0; j %s\n",dstr(),CBitcoinAddress(address).ToString().c_str()); + } script = (uint8_t *)tx.vout[j].scriptPubKey.data(); if ( script == 0 || script[0] != 0x6a ) - voutsum += tx.vout[j].nValue; + { + if ( ExtractDestination(tx.vout[j].scriptPubKey,address) != 0 && strcmp("RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY",CBitcoinAddress(address).ToString().c_str()) != 0 ) + voutsum += tx.vout[j].nValue; + } } } *zfundsp = zfunds; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6831585c4..07bf91c89 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4592,7 +4592,7 @@ int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { - set setAddress; int32_t i,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; + set setAddress; int32_t i,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); @@ -4643,11 +4643,17 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); if ( eligible > 0 ) { - if ( eligible != komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) + besttime = 0; + if ( eligible == komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) { - //fprintf(stderr,"tip.%d validation of winning blocktime failed %u -> eligible.%u\n",(uint32_t)tipindex->nHeight,*blocktimep,eligible); - } - else if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) + besttime = eligible; + eligible--; + if ( eligible < (uint32_t)tipindex->nTime-300 ) + break; + fprintf(stderr,"tip.%d validated winning blocktime %u -> eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,eligible); + } else continue; + eligible = besttime; + if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) { earliest = eligible; best_scriptPubKey = out.tx->vout[out.i].scriptPubKey; @@ -4655,7 +4661,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt decode_hex((uint8_t *)utxotxidp,32,(char *)out.tx->GetHash().GetHex().c_str()); *utxovoutp = out.i; *txtimep = (uint32_t)out.tx->nLockTime; - //fprintf(stderr,"ht.%d earliest.%u [%d] (%s) nValue %.8f locktime.%u\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep); + fprintf(stderr,"ht.%d earliest.%u [%d] (%s) nValue %.8f locktime.%u\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep); } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); From 27d76b740bb4b59e35335706c6d06b7f604de1d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 06:02:53 -1100 Subject: [PATCH 02/35] Syntax --- src/komodo_bitcoind.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index eb649be99..f328853b3 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1574,7 +1574,7 @@ int64_t komodo_newcoins(int64_t *zfundsp,int32_t nHeight,CBlock *pblock) { if ( ExtractDestination(tx.vout[j].scriptPubKey,address) != 0 && strcmp("RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY",CBitcoinAddress(address).ToString().c_str()) != 0 ) voutsum += tx.vout[j].nValue; - else printf("skip %.8f -> %s\n",dstr(),CBitcoinAddress(address).ToString().c_str()); + else printf("skip %.8f -> %s\n",dstr(tx.vout[j].nValue),CBitcoinAddress(address).ToString().c_str()); } script = (uint8_t *)tx.vout[j].scriptPubKey.data(); if ( script == 0 || script[0] != 0x6a ) From b388a2914475cda3196eb45218f99f610283b9eb Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 06:22:25 -1100 Subject: [PATCH 03/35] Add loop --- src/wallet/rpcwallet.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 07bf91c89..542666755 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4646,11 +4646,14 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt besttime = 0; if ( eligible == komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) { - besttime = eligible; - eligible--; - if ( eligible < (uint32_t)tipindex->nTime-300 ) - break; - fprintf(stderr,"tip.%d validated winning blocktime %u -> eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,eligible); + while ( eligible == komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) + { + besttime = eligible; + eligible--; + if ( eligible < (uint32_t)tipindex->nTime-300 ) + break; + fprintf(stderr,"tip.%d validated winning blocktime %u -> eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,eligible); + } } else continue; eligible = besttime; if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) From beb03773e4bd9c8550b3b48183ed2950ca7e1b02 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 06:37:07 -1100 Subject: [PATCH 04/35] Test --- src/wallet/rpcwallet.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 542666755..d5b01a495 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4592,7 +4592,7 @@ int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { - set setAddress; int32_t i,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; + set setAddress; int32_t i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); @@ -4640,6 +4640,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt CBlockIndex *tipindex; if ( (tipindex= chainActive.Tip()) != 0 ) { + m = 0; eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); if ( eligible > 0 ) { @@ -4652,7 +4653,8 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt eligible--; if ( eligible < (uint32_t)tipindex->nTime-300 ) break; - fprintf(stderr,"tip.%d validated winning blocktime %u -> eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,eligible); + m++; + //fprintf(stderr,"tip.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,(double)nValue/COIN,eligible); } } else continue; eligible = besttime; @@ -4664,7 +4666,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt decode_hex((uint8_t *)utxotxidp,32,(char *)out.tx->GetHash().GetHex().c_str()); *utxovoutp = out.i; *txtimep = (uint32_t)out.tx->nLockTime; - fprintf(stderr,"ht.%d earliest.%u [%d] (%s) nValue %.8f locktime.%u\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep); + fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),m,CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep); } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); From 424aeca00d8ff14e541be3d84a3ee478c0171aa7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 07:04:10 -1100 Subject: [PATCH 05/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d5b01a495..be1be1b28 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4651,7 +4651,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime-300 ) + if ( eligible < (uint32_t)tipindex->nTime+3 ) break; m++; //fprintf(stderr,"tip.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,(double)nValue/COIN,eligible); From 17ec046d90b8966caea92d8c67cb62d1f5051721 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 07:15:26 -1100 Subject: [PATCH 06/35] Test --- src/wallet/rpcwallet.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index be1be1b28..765b718a1 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4601,6 +4601,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt memset(utxovoutp,0,sizeof(*utxovoutp)); memset(utxosig,0,72); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); + fprintf(stderr,"Start scan of utxo for staking %u\n",(uint32_t)time(NULL)); BOOST_FOREACH(const COutput& out, vecOutputs) { if ( out.nDepth < nMinDepth || out.nDepth > nMaxDepth ) @@ -4702,6 +4703,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt *blocktimep = earliest; } } //else fprintf(stderr,"no earliest utxo for staking\n"); + fprintf(stderr,"end scan of utxo for staking %u\n",(uint32_t)time(NULL)); return(siglen); } From 4fc4e200964a77efdb62eec30682642f8c98c704 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 07:22:28 -1100 Subject: [PATCH 07/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 765b718a1..51b9edb8e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4652,7 +4652,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime+3 ) + //if ( eligible < (uint32_t)tipindex->nTime+3 ) break; m++; //fprintf(stderr,"tip.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,(double)nValue/COIN,eligible); From 55dd662212f77be38ad94625d0019fa4fd3a671f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 07:41:29 -1100 Subject: [PATCH 08/35] Optimize staking loop a bit --- src/wallet/rpcwallet.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 51b9edb8e..88759a6f4 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4592,7 +4592,7 @@ int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { - set setAddress; int32_t i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; + set setAddress; int32_t counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; CBlockIndex *tipindex; CTxDestination address; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); @@ -4601,15 +4601,26 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt memset(utxovoutp,0,sizeof(*utxovoutp)); memset(utxosig,0,72); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); + if ( (tipindex= chainActive.Tip()) == 0 ) + return(0); fprintf(stderr,"Start scan of utxo for staking %u\n",(uint32_t)time(NULL)); BOOST_FOREACH(const COutput& out, vecOutputs) { + counter++; + if ( chainActive.Tip() != tipindex ) + { + fprintf(stderr,"chain tip changed during staking loop t.%u counter.%d\n",(uint32_t)time(NULL),counter); + return(0); + } if ( out.nDepth < nMinDepth || out.nDepth > nMaxDepth ) { //fprintf(stderr,"komodo_staked invalid depth %d\n",(int32_t)out.nDepth); continue; } - if ( setAddress.size() ) + CAmount nValue = out.tx->vout[out.i].nValue; + if ( nValue < COIN || !out.fSpendable ) + continue; + /*if ( setAddress.size() ) { CTxDestination address; if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) @@ -4624,23 +4635,13 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } if ( IsMine(*pwalletMain, address) == 0 ) continue; - } - CAmount nValue = out.tx->vout[out.i].nValue; - if ( nValue < COIN || !out.fSpendable ) - continue; + }*/ const CScript& pk = out.tx->vout[out.i].scriptPubKey; //entry.push_back(Pair("generated", out.tx->IsCoinBase())); - CTxDestination address; - if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) - { - //entry.push_back(Pair("address", CBitcoinAddress(address).ToString())); - //if (pwalletMain->mapAddressBook.count(address)) - // entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name)); - } - //BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock()); - CBlockIndex *tipindex; - if ( (tipindex= chainActive.Tip()) != 0 ) + if ( ExtractDestination(out.tx->vout[out.i].scriptPubKey, address) != 0 ) { + if ( IsMine(*pwalletMain,address) == 0 ) + continue; m = 0; eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); if ( eligible > 0 ) @@ -4703,7 +4704,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt *blocktimep = earliest; } } //else fprintf(stderr,"no earliest utxo for staking\n"); - fprintf(stderr,"end scan of utxo for staking %u\n",(uint32_t)time(NULL)); + fprintf(stderr,"end scan of utxo for staking t.%u counter.%d\n",(uint32_t)time(NULL),counter); return(siglen); } From aa2ef3abfcbaba8090f930a9358a92c178909c59 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 08:05:00 -1100 Subject: [PATCH 09/35] Test --- src/wallet/rpcwallet.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 88759a6f4..9b7e17d4e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4668,7 +4668,9 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt decode_hex((uint8_t *)utxotxidp,32,(char *)out.tx->GetHash().GetHex().c_str()); *utxovoutp = out.i; *txtimep = (uint32_t)out.tx->nLockTime; - fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),m,CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep); + fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),m,CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep,counter); + if ( counter > 1000 ) + break; } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); From d04efc6ff574bfdd826cdd137bd0aec0d4393f5b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 08:30:38 -1100 Subject: [PATCH 10/35] Test --- src/wallet/rpcwallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9b7e17d4e..5073ab845 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4642,6 +4642,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { if ( IsMine(*pwalletMain,address) == 0 ) continue; +continue; m = 0; eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); if ( eligible > 0 ) From ac53109534eb6023a960ee2e01862ab2256dc623 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:29:29 -1100 Subject: [PATCH 11/35] Optimized staker --- src/komodo_bitcoind.h | 11 ++ src/wallet/rpcwallet.cpp | 212 +++++++++++++++++++++++++++------------ 2 files changed, 160 insertions(+), 63 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index f328853b3..58fc643b7 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1185,6 +1185,17 @@ int32_t komodo_segids(uint8_t *hashbuf,int32_t height,int32_t n) } } +uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 txid,int32_t vout) +{ + bits256 addrhash; uint8_t hashbuf[256]; + vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address)); + 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 *)hashp,hashbuf,100 + (int32_t)sizeof(uint256)*2 + sizeof(vout)); + return(addrhash.uints[0]); +} + 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; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 5073ab845..13ca693f5 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4513,6 +4513,7 @@ UniValue z_listoperationids(const UniValue& params, bool fHelp) int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex); extern std::string NOTARY_PUBKEY; uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeight,uint256 hash,int32_t n,uint32_t blocktime,uint32_t prevtime,char *destaddr); +int8_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 txid,int32_t vout); int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) { @@ -4590,10 +4591,86 @@ int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) return(siglen); } +struct komodo_staking +{ + char address[64]; + uint256 txid; + arith_uint256 hashval; + uint64_t nValue; + uint32_t segid32; + int32_t vout; + const CScript scriptPubKey; +}; + +struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,const CScript pk) +{ + uint256 hash; uint32_t segid32; struct komodo_staking *kp; + segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); + kp = &array[(*numlp)++]; + memset(*kp,0,sizeof(*kp)); + strcpy(kp->address,address); + kp->txid = txid; + kp->vout = vout; + kp->hashval = UintToArith256(hash); + kp->txtime = txtime; + kp->segid32 = segid32; + kp->nValue = nValue; + kp->scriptPubKey = pk; + return(array); +} + +arith_uint256 _komodo_eligible(struct komodo_staking *kp,arith_uint256 ratio,uint32_t blocktime,int32_t iter,int32_t minage,int32_t segid,int32_t nHeight,uint32_t prevtime) +{ + int32_t diff; uint64_t coinage; arith_uint256 coinage256,hashval; + diff = (iter + blocktime - kp->txtime - minage); + if ( diff < 0 ) + diff = 60; + else if ( diff > 3600*24*30 ) + diff = 3600*24*30; + 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)); + coinage256 = arith_uint256(coinage+1); + hashval = ratio * (kp->hashval / coinage256); + if ( nHeight >= 900 && nHeight < 916 ) + hashval = (hashval / coinage256); + return(hashval); +} + +uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komodo_staking *kp,int32_t nHeight,uint32_t blocktime,uint32_t prevtime,int32_t minage) +{ + int32_t maxiters = 180; + int32_t segid,iter,diff; uint64_t coinage; arith_uint256 hashval,coinage256; + segid = ((nHeight + kp->segid32) & 0x3f); + hashval = _komodo_eligible(kp,ratio,blocktime,maxiters,minage,segid,nHeight,prevtime); + if ( hashval <= bnTarget ) + { + for (iter=0; itertxtime+minage ) + continue; + hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime); + if ( hashval <= bnTarget ) + { + //fprintf(stderr,"winner blocktime.%u iter.%d segid.%d\n",blocktime,iter,segid); + blocktime += iter; + blocktime += segid * 2; + return(blocktime); + } + } + } + return(0); +} + int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { - set setAddress; int32_t counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; bool fNegative,fOverflow; CBlockIndex *tipindex; CTxDestination address; + static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime; + set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 ratio,mindiff,hashBlock,ratio; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); + mindiff.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); + ratio = (mindiff / bnTarget); assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); *utxovaluep = 0; @@ -4603,79 +4680,88 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); if ( (tipindex= chainActive.Tip()) == 0 ) return(0); - fprintf(stderr,"Start scan of utxo for staking %u\n",(uint32_t)time(NULL)); - BOOST_FOREACH(const COutput& out, vecOutputs) + nHeight = tipindex->nHeight + 1; + if ( (minage= nHeight*3) > 6000 ) // about 100 blocks + minage = 6000; + komodo_segids(hashbuf,nHeight-101,100); + fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); + if ( time(NULL) > lasttime+600 ) { - counter++; - if ( chainActive.Tip() != tipindex ) + if ( array != 0 ) { - fprintf(stderr,"chain tip changed during staking loop t.%u counter.%d\n",(uint32_t)time(NULL),counter); - return(0); + free(array); + array = 0; + maxkp = numkp = 0; } - if ( out.nDepth < nMinDepth || out.nDepth > nMaxDepth ) + BOOST_FOREACH(const COutput& out, vecOutputs) { - //fprintf(stderr,"komodo_staked invalid depth %d\n",(int32_t)out.nDepth); - continue; - } - CAmount nValue = out.tx->vout[out.i].nValue; - if ( nValue < COIN || !out.fSpendable ) - continue; - /*if ( setAddress.size() ) - { - CTxDestination address; - if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) + if ( (tipindex= chainActive.Tip()) == 0 || tipindex->nHeight+1 > nHeight ) { - fprintf(stderr,"komodo_staked ExtractDestination error\n"); + fprintf(stderr,"chain tip changed during staking loop t.%u counter.%d\n",(uint32_t)time(NULL),counter); + return(0); + } + counter++; + if ( out.nDepth < nMinDepth || out.nDepth > nMaxDepth ) + { + //fprintf(stderr,"komodo_staked invalid depth %d\n",(int32_t)out.nDepth); continue; } - if (!setAddress.count(address)) + CAmount nValue = out.tx->vout[out.i].nValue; + if ( nValue < COIN || !out.fSpendable ) + continue; + const CScript& pk = out.tx->vout[out.i].scriptPubKey; + if ( ExtractDestination(pk,address) != 0 ) { - fprintf(stderr,"komodo_staked setAddress.count error\n"); - continue; - } - if ( IsMine(*pwalletMain, address) == 0 ) - continue; - }*/ - const CScript& pk = out.tx->vout[out.i].scriptPubKey; - //entry.push_back(Pair("generated", out.tx->IsCoinBase())); - if ( ExtractDestination(out.tx->vout[out.i].scriptPubKey, address) != 0 ) - { - if ( IsMine(*pwalletMain,address) == 0 ) - continue; -continue; - m = 0; - eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); - if ( eligible > 0 ) - { - besttime = 0; - if ( eligible == komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) + if ( IsMine(*pwalletMain,address) == 0 ) + continue; + if ( GetTransaction(hash,tx,hashBlock,true) != 0 && (pindex= mapBlockIndex[hashBlock]) != 0 ) { - while ( eligible == komodo_stake(1,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,eligible,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()) ) - { - besttime = eligible; - eligible--; - //if ( eligible < (uint32_t)tipindex->nTime+3 ) - break; - m++; - //fprintf(stderr,"tip.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",(uint32_t)tipindex->nHeight,*blocktimep,(double)nValue/COIN,eligible); - } - } else continue; - eligible = besttime; - if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) - { - earliest = eligible; - best_scriptPubKey = out.tx->vout[out.i].scriptPubKey; - *utxovaluep = (uint64_t)nValue; - decode_hex((uint8_t *)utxotxidp,32,(char *)out.tx->GetHash().GetHex().c_str()); - *utxovoutp = out.i; - *txtimep = (uint32_t)out.tx->nLockTime; - fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d\n",(uint32_t)tipindex->nHeight+1,earliest,(int32_t)(earliest- *blocktimep),m,CBitcoinAddress(address).ToString().c_str(),(double)nValue/COIN,*txtimep,counter); - if ( counter > 1000 ) - break; + array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,pk); } - } //else fprintf(stderr,"utxo not eligible\n"); - } //else fprintf(stderr,"no tipindex\n"); + } + } + lasttime = (uint32_t)time(NULL); + fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); } + for (i=0; inTime+27,minage); + //eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); + if ( eligible > 0 ) + { + besttime = m = 0; + if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) + { + while ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) + { + besttime = eligible; + eligible--; + if ( eligible < (uint32_t)tipindex->nTime+3 ) + break; + m++; + fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)nValue/COIN,eligible); + } + } + else + { + fprintf(stderr,"ht.%d error validating winning blocktime %u -> %.8f eligible.%u test prior\n",nHeight,*blocktimep,(double)nValue/COIN,eligible); + continue; + } + eligible = besttime; + if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) + { + earliest = eligible; + best_scriptPubKey = kp->scriptPubKey; //out.tx->vout[out.i].scriptPubKey; + *utxovaluep = (uint64_t)kp->nValue; + //decode_hex((uint8_t *)utxotxidp,32,(char *)out.tx->GetHash().GetHex().c_str()); + decode_hex((uint8_t *)utxotxidp,32,(char *)kp->txid.GetHex().c_str()); + *utxovoutp = kp->vout; + *txtimep = kp->txtime;//(uint32_t)out.tx->nLockTime; + fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d\n",nHeight,earliest,(int32_t)(earliest- *blocktimep),m,kp->address,(double)kp->nValue/COIN,*txtimep,counter); + } + } //else fprintf(stderr,"utxo not eligible\n"); + } //else fprintf(stderr,"no tipindex\n"); if ( earliest != 0 ) { bool signSuccess; SignatureData sigdata; uint64_t txfee; uint8_t *ptr; uint256 revtxid,utxotxid; From 5649e3d39d0350598fa5bbeaf08c8a570fc7cb75 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:30:50 -1100 Subject: [PATCH 12/35] Test --- src/komodo_bitcoind.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 58fc643b7..fad483d3f 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1187,7 +1187,7 @@ int32_t komodo_segids(uint8_t *hashbuf,int32_t height,int32_t n) uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 txid,int32_t vout) { - bits256 addrhash; uint8_t hashbuf[256]; + bits256 addrhash; vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address)); memcpy(&hashbuf[100],&addrhash,sizeof(addrhash)); memcpy(&hashbuf[100+sizeof(addrhash)],&txid,sizeof(txid)); From 1273624d3e440bd9578f246b45531dd4661dd7c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:38:45 -1100 Subject: [PATCH 13/35] Test --- src/komodo_bitcoind.h | 2 +- src/wallet/rpcwallet.cpp | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index fad483d3f..d624dc72a 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -1187,7 +1187,7 @@ int32_t komodo_segids(uint8_t *hashbuf,int32_t height,int32_t n) uint32_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 txid,int32_t vout) { - bits256 addrhash; + bits256 addrhash; vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)address,(int32_t)strlen(address)); memcpy(&hashbuf[100],&addrhash,sizeof(addrhash)); memcpy(&hashbuf[100+sizeof(addrhash)],&txid,sizeof(txid)); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 13ca693f5..d9de578d5 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4514,6 +4514,7 @@ int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex); extern std::string NOTARY_PUBKEY; uint32_t komodo_stake(int32_t validateflag,arith_uint256 bnTarget,int32_t nHeight,uint256 hash,int32_t n,uint32_t blocktime,uint32_t prevtime,char *destaddr); int8_t komodo_stakehash(uint256 *hashp,char *address,uint8_t *hashbuf,uint256 txid,int32_t vout); +int32_t komodo_segids(uint8_t *hashbuf,int32_t height,int32_t n); int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33) { @@ -4597,17 +4598,22 @@ struct komodo_staking uint256 txid; arith_uint256 hashval; uint64_t nValue; - uint32_t segid32; + uint32_t segid32,txtime; int32_t vout; - const CScript scriptPubKey; + CScript scriptPubKey; }; -struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,const CScript pk) +struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk) { uint256 hash; uint32_t segid32; struct komodo_staking *kp; segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout); - kp = &array[(*numlp)++]; - memset(*kp,0,sizeof(*kp)); + if ( *numkp >= *maxkp ) + { + *maxkp += 1000; + array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp)); + } + kp = &array[(*numkp)++]; + memset(kp,0,sizeof(*kp)); strcpy(kp->address,address); kp->txid = txid; kp->vout = vout; @@ -4667,7 +4673,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime; - set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 ratio,mindiff,hashBlock,ratio; + set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); mindiff.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); ratio = (mindiff / bnTarget); @@ -4716,7 +4722,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt continue; if ( GetTransaction(hash,tx,hashBlock,true) != 0 && (pindex= mapBlockIndex[hashBlock]) != 0 ) { - array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,pk); + array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk); } } } From aecf58592c68a873893e4c8f476b6014d214f6d7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:41:08 -1100 Subject: [PATCH 14/35] Fix --- src/wallet/rpcwallet.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d9de578d5..3633c56e4 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4720,7 +4720,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { if ( IsMine(*pwalletMain,address) == 0 ) continue; - if ( GetTransaction(hash,tx,hashBlock,true) != 0 && (pindex= mapBlockIndex[hashBlock]) != 0 ) + if ( GetTransaction(out.tx->GetHash(),tx,hashBlock,true) != 0 && (pindex= mapBlockIndex[hashBlock]) != 0 ) { array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk); } @@ -4746,16 +4746,16 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( eligible < (uint32_t)tipindex->nTime+3 ) break; m++; - fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)nValue/COIN,eligible); + fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); } } else { - fprintf(stderr,"ht.%d error validating winning blocktime %u -> %.8f eligible.%u test prior\n",nHeight,*blocktimep,(double)nValue/COIN,eligible); + fprintf(stderr,"ht.%d error validating winning blocktime %u -> %.8f eligible.%u test prior\n",nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); continue; } eligible = besttime; - if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || nValue < *utxovaluep)) ) + if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp->nValue < *utxovaluep)) ) { earliest = eligible; best_scriptPubKey = kp->scriptPubKey; //out.tx->vout[out.i].scriptPubKey; From 76df25be7a6f964272eb4383b698375c95351726 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:51:01 -1100 Subject: [PATCH 15/35] Test --- src/wallet/rpcwallet.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 3633c56e4..c37e78889 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4732,8 +4732,9 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt for (i=0; inTime+27,minage); - //eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); + if ( (eligible= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) + continue; + eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); if ( eligible > 0 ) { besttime = m = 0; From ecae680bfa8f842849ca057233ea5973d7daec20 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 10:52:42 -1100 Subject: [PATCH 16/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c37e78889..babeabe6e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4734,7 +4734,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt kp = &array[i]; if ( (eligible= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) continue; - eligible = komodo_stake(0,bnTarget,(uint32_t)tipindex->nHeight+1,out.tx->GetHash(),out.i,0,(uint32_t)tipindex->nTime+27,(char *)CBitcoinAddress(address).ToString().c_str()); + eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+27,kp->address); if ( eligible > 0 ) { besttime = m = 0; From 011b0a24334378c2a71ef3a069efac111f3afeed Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 11:02:23 -1100 Subject: [PATCH 17/35] Test --- src/wallet/rpcwallet.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index babeabe6e..59516472b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4691,7 +4691,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt minage = 6000; komodo_segids(hashbuf,nHeight-101,100); fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - if ( time(NULL) > lasttime+600 ) + //if ( time(NULL) > lasttime+600 ) { if ( array != 0 ) { @@ -4747,7 +4747,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( eligible < (uint32_t)tipindex->nTime+3 ) break; m++; - fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); + //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); } } else @@ -4769,6 +4769,12 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); + if ( array != 0 ) + { + free(array); + array = 0; + maxkp = numkp = 0; + } if ( earliest != 0 ) { bool signSuccess; SignatureData sigdata; uint64_t txfee; uint8_t *ptr; uint256 revtxid,utxotxid; @@ -4800,7 +4806,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt *blocktimep = earliest; } } //else fprintf(stderr,"no earliest utxo for staking\n"); - fprintf(stderr,"end scan of utxo for staking t.%u counter.%d\n",(uint32_t)time(NULL),counter); + fprintf(stderr,"end scan of utxo for staking t.%u counter.%d numkp.%d\n",(uint32_t)time(NULL),counter,numkp); return(siglen); } From 518e079e9172d69e211ed783bb7873f36b1e0333 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 11:14:53 -1100 Subject: [PATCH 18/35] Test --- src/wallet/rpcwallet.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 59516472b..384c23001 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4740,11 +4740,12 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt besttime = m = 0; if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) { + fprintf(stderr,"ht.%d validated winning [%d] -> %.8f eligible.%u test prior\n",nHeight,(int32_t)(eligible - *blocktimep),(double)kp->nValue/COIN,eligible); while ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime+3 ) + if ( eligible < (uint32_t)tipindex->nTime-180 ) break; m++; //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); From 1d8f1b466afec494601820a696923f53ff68f6ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 11:18:36 -1100 Subject: [PATCH 19/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 384c23001..cc8f63040 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4740,7 +4740,6 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt besttime = m = 0; if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) { - fprintf(stderr,"ht.%d validated winning [%d] -> %.8f eligible.%u test prior\n",nHeight,(int32_t)(eligible - *blocktimep),(double)kp->nValue/COIN,eligible); while ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address) ) { besttime = eligible; @@ -4757,6 +4756,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt continue; } eligible = besttime; + fprintf(stderr,"ht.%d validated winning [%d] -> %.8f eligible.%u test prior\n",nHeight,(int32_t)(eligible - *blocktimep),(double)kp->nValue/COIN,eligible); if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp->nValue < *utxovaluep)) ) { earliest = eligible; From 817aedba536f3573c715a90b49c24a2d240a68a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 11:53:42 -1100 Subject: [PATCH 20/35] Test --- src/wallet/rpcwallet.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index cc8f63040..7c1b81af1 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4691,13 +4691,14 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt minage = 6000; komodo_segids(hashbuf,nHeight-101,100); fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - //if ( time(NULL) > lasttime+600 ) + if ( time(NULL) > lasttime+60 ) { if ( array != 0 ) { free(array); array = 0; maxkp = numkp = 0; + lasttime = 0; } BOOST_FOREACH(const COutput& out, vecOutputs) { @@ -4731,6 +4732,11 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } for (i=0; inHeight+1 > nHeight ) + { + fprintf(stderr,"chain tip changed during staking loop t.%u counter.%d\n",(uint32_t)time(NULL),counter); + return(0); + } kp = &array[i]; if ( (eligible= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) continue; @@ -4770,11 +4776,12 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); - if ( array != 0 ) + if ( 0 && array != 0 ) { free(array); array = 0; maxkp = numkp = 0; + lasttime = 0; } if ( earliest != 0 ) { From a87b6f5f629a2d55c2db677155e5347f6dcad7e5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 11:55:01 -1100 Subject: [PATCH 21/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 7c1b81af1..8a0d83d1f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4750,7 +4750,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime-180 ) + if ( eligible < (uint32_t)tipindex->nTime-60 ) break; m++; //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); From 45c4bfef6434afc0f351566691257b49a126cebc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:02:20 -1100 Subject: [PATCH 22/35] Test --- src/wallet/rpcwallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 8a0d83d1f..d068ac18d 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4691,7 +4691,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt minage = 6000; komodo_segids(hashbuf,nHeight-101,100); fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - if ( time(NULL) > lasttime+60 ) + //if ( time(NULL) > lasttime+60 ) { if ( array != 0 ) { @@ -4776,7 +4776,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); - if ( 0 && array != 0 ) + if ( array != 0 ) { free(array); array = 0; From d5c81c59bbc70ea5067c316cf34a73960cc181a8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:11:43 -1100 Subject: [PATCH 23/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d068ac18d..28166a6fa 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4678,7 +4678,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt mindiff.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); ratio = (mindiff / bnTarget); assert(pwalletMain != NULL); - LOCK2(cs_main, pwalletMain->cs_wallet); + //LOCK2(cs_main, pwalletMain->cs_wallet); *utxovaluep = 0; memset(utxotxidp,0,sizeof(*utxotxidp)); memset(utxovoutp,0,sizeof(*utxovoutp)); From b8b06863603f0f0b284731d107cb4efc0de73447 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:21:33 -1100 Subject: [PATCH 24/35] Test --- src/wallet/rpcwallet.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 28166a6fa..d11300c96 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4660,7 +4660,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { - //fprintf(stderr,"winner blocktime.%u iter.%d segid.%d\n",blocktime,iter,segid); + fprintf(stderr,"winner %.8f blocktime.%u iter.%d segid.%d\n",(double)kp->nValue/COIN,blocktime,iter,segid); blocktime += iter; blocktime += segid * 2; return(blocktime); @@ -4673,12 +4673,12 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime; - set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; + set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,eligible2,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); mindiff.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); ratio = (mindiff / bnTarget); assert(pwalletMain != NULL); - //LOCK2(cs_main, pwalletMain->cs_wallet); + LOCK2(cs_main, pwalletMain->cs_wallet); *utxovaluep = 0; memset(utxotxidp,0,sizeof(*utxotxidp)); memset(utxovoutp,0,sizeof(*utxovoutp)); @@ -4691,7 +4691,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt minage = 6000; komodo_segids(hashbuf,nHeight-101,100); fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - //if ( time(NULL) > lasttime+60 ) + if ( time(NULL) > lasttime+60 ) { if ( array != 0 ) { @@ -4730,7 +4730,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt lasttime = (uint32_t)time(NULL); fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); } - for (i=0; inHeight+1 > nHeight ) { @@ -4738,9 +4738,10 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt return(0); } kp = &array[i]; - if ( (eligible= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) + if ( (eligible2= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) continue; eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+27,kp->address); + fprintf(stderr,"i.%d %u vs %u\n",i,eligible2,eligible); if ( eligible > 0 ) { besttime = m = 0; From cfe64b2b487c991b8775a875949025bfb0ed0299 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:26:38 -1100 Subject: [PATCH 25/35] Test --- src/wallet/rpcwallet.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d11300c96..987e6db62 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4651,6 +4651,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod int32_t segid,iter,diff; uint64_t coinage; arith_uint256 hashval,coinage256; segid = ((nHeight + kp->segid32) & 0x3f); hashval = _komodo_eligible(kp,ratio,blocktime,maxiters,minage,segid,nHeight,prevtime); + fprintf(stderr,"b.%u minage.%d segid.%d ht.%d prev.%u\n",blocktime,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { for (iter=0; iter Date: Sat, 14 Jul 2018 12:35:26 -1100 Subject: [PATCH 26/35] Test --- src/wallet/rpcwallet.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 987e6db62..1977bb4df 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4645,20 +4645,24 @@ arith_uint256 _komodo_eligible(struct komodo_staking *kp,arith_uint256 ratio,uin return(hashval); } -uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komodo_staking *kp,int32_t nHeight,uint32_t blocktime,uint32_t prevtime,int32_t minage) +uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komodo_staking *kp,int32_t nHeight,uint32_t blocktime,uint32_t prevtime,int32_t minage,uint8_t *hashbuf) { - int32_t maxiters = 180; + int32_t maxiters = 180; uint256 hash; int32_t segid,iter,diff; uint64_t coinage; arith_uint256 hashval,coinage256; + komodo_stakehash(&hash,kp->address,hashbuf,kp->txid,kp->vout); + kp->hashval = UintToArith256(hash); segid = ((nHeight + kp->segid32) & 0x3f); hashval = _komodo_eligible(kp,ratio,blocktime,maxiters,minage,segid,nHeight,prevtime); - fprintf(stderr,"b.%u minage.%d segid.%d ht.%d prev.%u\n",blocktime,minage,segid,nHeight,prevtime); + for (int i=32; i>=0; i--) + fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]); + fprintf(stderr," b.%u minage.%d segid.%d ht.%d prev.%u\n",blocktime,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { for (iter=0; itertxtime+minage ) continue; - hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime); + hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime,hashbuf); if ( hashval <= bnTarget ) { fprintf(stderr,"winner %.8f blocktime.%u iter.%d segid.%d\n",(double)kp->nValue/COIN,blocktime,iter,segid); @@ -4739,7 +4743,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt return(0); } kp = &array[i]; - if ( (eligible2= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage)) == 0 ) + if ( (eligible2= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage,hashbuf)) == 0 ) continue; eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+27,kp->address); fprintf(stderr,"i.%d %u vs %u\n",i,eligible2,eligible); From 37d6294dbe692798fac29b536754a6cc8bf00425 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:36:44 -1100 Subject: [PATCH 27/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1977bb4df..5612f5f79 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4662,7 +4662,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod { if ( blocktime+iter+segid*2 < kp->txtime+minage ) continue; - hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime,hashbuf); + hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { fprintf(stderr,"winner %.8f blocktime.%u iter.%d segid.%d\n",(double)kp->nValue/COIN,blocktime,iter,segid); From d3ce166fb8b21972151ecb7673c8f9bbbd942194 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:41:30 -1100 Subject: [PATCH 28/35] Test --- src/wallet/rpcwallet.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 5612f5f79..824eb60b9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4653,9 +4653,9 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod kp->hashval = UintToArith256(hash); segid = ((nHeight + kp->segid32) & 0x3f); hashval = _komodo_eligible(kp,ratio,blocktime,maxiters,minage,segid,nHeight,prevtime); - for (int i=32; i>=0; i--) - fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]); - fprintf(stderr," b.%u minage.%d segid.%d ht.%d prev.%u\n",blocktime,minage,segid,nHeight,prevtime); + //for (int i=32; i>=0; i--) + // fprintf(stderr,"%02x",((uint8_t *)&hashval)[i]); + //fprintf(stderr," b.%u minage.%d segid.%d ht.%d prev.%u\n",blocktime,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { for (iter=0; iter 6000 ) // about 100 blocks minage = 6000; komodo_segids(hashbuf,nHeight-101,100); + if ( blocktime > tipindex->nTime+60 ) + blocktime = tipindex->nTime+60; fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - if ( time(NULL) > lasttime+60 ) + if ( time(NULL) > lasttime+100 ) { if ( array != 0 ) { @@ -4735,7 +4737,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt lasttime = (uint32_t)time(NULL); fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); } - for (i=0; inHeight+1 > nHeight ) { From 6e7dcf31067a4501dd6caf2643b58c1b5936ffe4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:42:51 -1100 Subject: [PATCH 29/35] Test --- src/wallet/rpcwallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 824eb60b9..a8575497f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4695,8 +4695,8 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( (minage= nHeight*3) > 6000 ) // about 100 blocks minage = 6000; komodo_segids(hashbuf,nHeight-101,100); - if ( blocktime > tipindex->nTime+60 ) - blocktime = tipindex->nTime+60; + if ( *blocktimep > tipindex->nTime+60 ) + *blocktimep = tipindex->nTime+60; fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); if ( time(NULL) > lasttime+100 ) { From 1973dfa0505aa51cf471bc67aac2919a00615e82 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 12:49:18 -1100 Subject: [PATCH 30/35] Test --- src/wallet/rpcwallet.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index a8575497f..330c5f253 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4665,7 +4665,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod hashval = _komodo_eligible(kp,ratio,blocktime,iter,minage,segid,nHeight,prevtime); if ( hashval <= bnTarget ) { - fprintf(stderr,"winner %.8f blocktime.%u iter.%d segid.%d\n",(double)kp->nValue/COIN,blocktime,iter,segid); + //fprintf(stderr,"winner %.8f blocktime.%u iter.%d segid.%d\n",(double)kp->nValue/COIN,blocktime,iter,segid); blocktime += iter; blocktime += segid * 2; return(blocktime); @@ -4698,7 +4698,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( *blocktimep > tipindex->nTime+60 ) *blocktimep = tipindex->nTime+60; fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); - if ( time(NULL) > lasttime+100 ) + if ( time(NULL) > lasttime+600 ) { if ( array != 0 ) { @@ -4748,7 +4748,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt if ( (eligible2= komodo_eligible(bnTarget,ratio,kp,nHeight,*blocktimep,(uint32_t)tipindex->nTime+27,minage,hashbuf)) == 0 ) continue; eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+27,kp->address); - fprintf(stderr,"i.%d %u vs %u\n",i,eligible2,eligible); + //fprintf(stderr,"i.%d %u vs %u\n",i,eligible2,eligible); if ( eligible > 0 ) { besttime = m = 0; From 2d4811152640e6e4413f811af0b02cc8605022b6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 13:02:35 -1100 Subject: [PATCH 31/35] -prints --- src/wallet/rpcwallet.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 330c5f253..c55a306ed 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4678,7 +4678,7 @@ uint32_t komodo_eligible(arith_uint256 bnTarget,arith_uint256 ratio,struct komod int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig) { static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime; - set setAddress; struct komodo_staking *kp; int32_t segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,eligible2,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; + set setAddress; struct komodo_staking *kp; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; vector vecOutputs; uint32_t besttime,eligible,eligible2,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock; bnTarget.SetCompact(nBits, &fNegative, &fOverflow); mindiff.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow); ratio = (mindiff / bnTarget); @@ -4697,7 +4697,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt komodo_segids(hashbuf,nHeight-101,100); if ( *blocktimep > tipindex->nTime+60 ) *blocktimep = tipindex->nTime+60; - fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); + //fprintf(stderr,"Start scan of utxo for staking %u ht.%d\n",(uint32_t)time(NULL),nHeight); if ( time(NULL) > lasttime+600 ) { if ( array != 0 ) @@ -4737,7 +4737,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt lasttime = (uint32_t)time(NULL); fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); } - for (i=0; inHeight+1 > nHeight ) { @@ -4770,7 +4770,8 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt continue; } eligible = besttime; - fprintf(stderr,"ht.%d validated winning [%d] -> %.8f eligible.%u test prior\n",nHeight,(int32_t)(eligible - *blocktimep),(double)kp->nValue/COIN,eligible); + winners++; + //fprintf(stderr,"ht.%d validated winning [%d] -> %.8f eligible.%u test prior\n",nHeight,(int32_t)(eligible - tipindex->nTime),(double)kp->nValue/COIN,eligible); if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp->nValue < *utxovaluep)) ) { earliest = eligible; @@ -4780,11 +4781,11 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt decode_hex((uint8_t *)utxotxidp,32,(char *)kp->txid.GetHex().c_str()); *utxovoutp = kp->vout; *txtimep = kp->txtime;//(uint32_t)out.tx->nLockTime; - fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d\n",nHeight,earliest,(int32_t)(earliest- *blocktimep),m,kp->address,(double)kp->nValue/COIN,*txtimep,counter); + fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d winnders.%d\n",nHeight,earliest,(int32_t)(earliest - tipindex->nTime),m,kp->address,(double)kp->nValue/COIN,*txtimep,counter,winners); } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); - if ( 0 && array != 0 ) + if ( numkp < 10000 && array != 0 ) { free(array); array = 0; @@ -4822,7 +4823,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt *blocktimep = earliest; } } //else fprintf(stderr,"no earliest utxo for staking\n"); - fprintf(stderr,"end scan of utxo for staking t.%u counter.%d numkp.%d\n",(uint32_t)time(NULL),counter,numkp); + fprintf(stderr,"end scan of utxo for staking t.%u counter.%d numkp.%d winners.%d\n",(uint32_t)time(NULL),counter,numkp,winners); return(siglen); } From 8cbf8d6a12fee8999c1a8b591e606b681e31b2f0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 13:12:08 -1100 Subject: [PATCH 32/35] Test --- src/wallet/rpcwallet.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c55a306ed..debec99b6 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4735,7 +4735,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt } } lasttime = (uint32_t)time(NULL); - fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); + //fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp); } for (i=winners=0; inTime-60 ) + if ( eligible < (uint32_t)tipindex->nTime+30 ) break; m++; //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); @@ -4823,7 +4823,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt *blocktimep = earliest; } } //else fprintf(stderr,"no earliest utxo for staking\n"); - fprintf(stderr,"end scan of utxo for staking t.%u counter.%d numkp.%d winners.%d\n",(uint32_t)time(NULL),counter,numkp,winners); + //fprintf(stderr,"end scan of utxo for staking t.%u counter.%d numkp.%d winners.%d\n",(uint32_t)time(NULL),counter,numkp,winners); return(siglen); } From 779b7b3a5b5a472bd0893807bbbf023fa4313360 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 13:16:18 -1100 Subject: [PATCH 33/35] Typo --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index debec99b6..23ba352dd 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4781,7 +4781,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt decode_hex((uint8_t *)utxotxidp,32,(char *)kp->txid.GetHex().c_str()); *utxovoutp = kp->vout; *txtimep = kp->txtime;//(uint32_t)out.tx->nLockTime; - fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d winnders.%d\n",nHeight,earliest,(int32_t)(earliest - tipindex->nTime),m,kp->address,(double)kp->nValue/COIN,*txtimep,counter,winners); + fprintf(stderr,"ht.%d earliest.%u [%d].%d (%s) nValue %.8f locktime.%u counter.%d winners.%d\n",nHeight,earliest,(int32_t)(earliest - tipindex->nTime),m,kp->address,(double)kp->nValue/COIN,*txtimep,counter,winners); } } //else fprintf(stderr,"utxo not eligible\n"); } //else fprintf(stderr,"no tipindex\n"); From 8afd5b3a0444e241dbcd885ffc300f580e60dcf3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 13:25:57 -1100 Subject: [PATCH 34/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 23ba352dd..383c84ef9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4758,7 +4758,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime+30 ) + if ( eligible < (uint32_t)tipindex->nTime+3 ) break; m++; //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible); From a3b8c12830810f7d8b81bf7bfe5296675d2aa550 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 14 Jul 2018 13:26:39 -1100 Subject: [PATCH 35/35] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 383c84ef9..c4374737f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4758,7 +4758,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt { besttime = eligible; eligible--; - if ( eligible < (uint32_t)tipindex->nTime+3 ) + if ( eligible < (uint32_t)tipindex->nTime-63 ) break; m++; //fprintf(stderr,"m.%d ht.%d validated winning blocktime %u -> %.8f eligible.%u test prior\n",m,nHeight,*blocktimep,(double)kp->nValue/COIN,eligible);