From 2993a76b3050cd885a9f58f614264f9b665eb941 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Apr 2019 20:39:55 -1100 Subject: [PATCH 01/15] komodo_pricesinit --- src/komodo_defs.h | 1 + src/komodo_gateway.h | 16 ++++++++++++++++ src/komodo_utils.h | 1 + 3 files changed, 18 insertions(+) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 9377ea276..31dde4a24 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -85,5 +85,6 @@ int tx_height( const uint256 &hash ); extern std::vector vWhiteListAddress; void komodo_netevent(std::vector payload); int32_t komodo_priceind(char *symbol); +void komodo_pricesinit(); #endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9afc9845c..2098d30c2 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2526,3 +2526,19 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice return(price); } +void komodo_pricesinit() +{ + FILE *fp; char symbol[65]; int32_t i; + boost::filesystem::path pricefname,pricesdir = GetDataDir() / "prices"; + if (!boost::filesystem::exists(pricesdir)) + { + boost::filesystem::create_directories(pricesdir); + for (i=0; i Date: Wed, 10 Apr 2019 20:53:00 -1100 Subject: [PATCH 02/15] Make fp for each file --- src/komodo_gateway.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 2098d30c2..5ac8ea540 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1549,10 +1549,11 @@ void komodo_passport_iteration() } extern std::vector Mineropret; // opreturn data set by the data gathering code -#define PRICES_MAXCHANGE (COIN / 100) // maximum acceptable change, set at 1% +#define PRICES_ERRORRATE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR #define KOMODO_LOCALPRICE_CACHESIZE 13 #define KOMODO_MAXPRICES 2048 +#define PRICES_SMOOTHWIDTH 1 #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) @@ -1696,10 +1697,10 @@ CScript komodo_mineropret(int32_t nHeight) { memcpy(pricebits,Mineropret.data(),Mineropret.size()); memset(maxflags,0,sizeof(maxflags)); - if ( komodo_pricecmp(0,n,maxflags,pricebits,prevbits,PRICES_MAXCHANGE) < 0 ) + if ( komodo_pricecmp(0,n,maxflags,pricebits,prevbits,PRICES_ERRORRATE) < 0 ) { // if the new prices are outside tolerance, update Mineropret with clamped prices - komodo_priceclamp(n,pricebits,prevbits,PRICES_MAXCHANGE); + komodo_priceclamp(n,pricebits,prevbits,PRICES_ERRORRATE); //fprintf(stderr,"update Mineropret to clamped prices\n"); memcpy(Mineropret.data(),pricebits,Mineropret.size()); } @@ -1788,7 +1789,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( pricebits[i] == 0 ) pricebits[i] = prevbits[i]; } - if ( komodo_pricecmp(nHeight,n,maxflags,pricebits,prevbits,PRICES_MAXCHANGE) < 0 ) + if ( komodo_pricecmp(nHeight,n,maxflags,pricebits,prevbits,PRICES_ERRORRATE) < 0 ) { for (i=1; i Date: Wed, 10 Apr 2019 20:59:20 -1100 Subject: [PATCH 03/15] Fputc --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 5ac8ea540..5f9a2b8b4 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2541,7 +2541,7 @@ void komodo_pricesinit() pricefname = pricesdir / symbol; fp = fopen(pricefname.string().c_str(), "wb+"); fseek(fp,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(int64_t) * 3,SEEK_SET); - fputc(fp,0); + fputc(0,fp); fflush(fp); } } From 169bffa3e4231ab06172df5cd6e0892f2e98d248 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Apr 2019 21:06:56 -1100 Subject: [PATCH 04/15] +print --- src/komodo_gateway.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 5f9a2b8b4..f5ee16f7b 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2530,7 +2530,8 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice void komodo_pricesinit() { FILE *fp; char symbol[65]; int32_t i; - boost::filesystem::path pricefname,pricesdir = GetDataDir() / ASSETCHAINS_SYMBOL / "prices"; + boost::filesystem::path pricefname,pricesdir = GetDataDir() / "prices"; + fprintf(stderr,"pricesinit (%s)\n",pricesdir.string().c_str()); if (!boost::filesystem::exists(pricesdir)) { boost::filesystem::create_directories(pricesdir); From 027662268df016a645de71a6c5ff21dd3db4da4c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Apr 2019 21:11:27 -1100 Subject: [PATCH 05/15] Move pricesinit --- src/bitcoind.cpp | 2 ++ src/komodo_utils.h | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 4e010d28e..68e0da8b3 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -70,6 +70,8 @@ void WaitForShutdown(boost::thread_group* threadGroup) { int32_t i; bool fShutdown = ShutdownRequested(); // Tell the main threads to shutdown. + if ( ASSETCHAINS_CBOPRET != 0 ) + komodo_pricesinit(); while (!fShutdown) { //fprintf(stderr,"call passport iteration\n"); diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 700615ee1..043d7282e 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -2082,7 +2082,6 @@ void komodo_args(char *argv0) extralen += symbol.size(); } } - komodo_pricesinit(); komodo_cbopretupdate(1); // will set Mineropret fprintf(stderr,"This blockchain uses data produced from CoinDesk Bitcoin Price Index\n"); } From 8f5feded98166b02b00a18e488a31c5a0bdc1884 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Apr 2019 22:46:42 -1100 Subject: [PATCH 06/15] fp0 --- src/komodo_gateway.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index f5ee16f7b..e128043ff 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2529,7 +2529,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice void komodo_pricesinit() { - FILE *fp; char symbol[65]; int32_t i; + FILE *fp,*fp0 = 0; char symbol[65]; int32_t i; boost::filesystem::path pricefname,pricesdir = GetDataDir() / "prices"; fprintf(stderr,"pricesinit (%s)\n",pricesdir.string().c_str()); if (!boost::filesystem::exists(pricesdir)) @@ -2539,11 +2539,21 @@ void komodo_pricesinit() { if ( komodo_pricename(symbol,i) == 0 ) break; + if ( i == 0 ) + strcpy(symbol,"rawprices"); pricefname = pricesdir / symbol; fp = fopen(pricefname.string().c_str(), "wb+"); + if ( i == 0 ) + fp0 = fp; fseek(fp,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(int64_t) * 3,SEEK_SET); fputc(0,fp); fflush(fp); } + if ( fp0 != 0 && i > 0 ) + { + fseek(fp0,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(uint32_t) * i,SEEK_SET); + fputc(0,fp0); + fflush(fp0); + } } } From 6178eb87c47e643f73f7918ed120e162c342ae91 Mon Sep 17 00:00:00 2001 From: blackjok3r Date: Thu, 11 Apr 2019 18:13:39 +0800 Subject: [PATCH 07/15] fix dpow --- src/komodo.h | 4 ++-- src/main.cpp | 57 ++++++++++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/komodo.h b/src/komodo.h index 3ceb29cc3..15c1e5c02 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -691,7 +691,7 @@ int32_t komodo_voutupdate(bool fJustCheck,int32_t *isratificationp,int32_t notar else { komodo_rwccdata(ASSETCHAINS_SYMBOL,1,&ccdata,&MoMoMdata); - if ( !fJustCheck && matched != 0 ) + if ( matched != 0 ) printf("[%s] matched.%d VALID (%s) MoM.%s [%d] CCid.%u\n",ASSETCHAINS_SYMBOL,matched,ccdata.symbol,MoM.ToString().c_str(),MoMdepth&0xffff,(MoMdepth>>16)&0xffff); } if ( MoMoMdata.pairs != 0 ) @@ -747,7 +747,7 @@ int32_t komodo_voutupdate(bool fJustCheck,int32_t *isratificationp,int32_t notar } else if ( matched != 0 && i == 0 && j == 1 && opretlen == 149 ) { - if ( !fJustCheck && notaryid >= 0 && notaryid < 64 ) + if ( notaryid >= 0 && notaryid < 64 ) komodo_paxpricefeed(height,&scriptbuf[len],opretlen); } else if ( matched != 0 ) diff --git a/src/main.cpp b/src/main.cpp index faa9e6a9c..47b089ca8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4239,37 +4239,36 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo // our genesis block. In practice this (probably) won't happen because of checks elsewhere. auto reorgLength = pindexOldTip ? pindexOldTip->GetHeight() - (pindexFork ? pindexFork->GetHeight() : -1) : 0; assert(MAX_REORG_LENGTH > 0);//, "We must be able to reorg some distance"); + + int32_t notarizedht,prevMoMheight; uint256 notarizedhash,txid; + notarizedht = komodo_notarized_height(&prevMoMheight,¬arizedhash,&txid); + if ( pindexFork->GetHeight() < notarizedht ) + { + fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); + pindexFork = pindexOldTip; + } + if (reorgLength > MAX_REORG_LENGTH) { - int32_t notarizedht,prevMoMheight; uint256 notarizedhash,txid; - notarizedht = komodo_notarized_height(&prevMoMheight,¬arizedhash,&txid); - if ( pindexFork->GetHeight() < notarizedht ) - { - fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); - pindexFork = pindexOldTip; - } - else - { - auto msg = strprintf(_( - "A block chain reorganization has been detected that would roll back %d blocks! " - "This is larger than the maximum of %d blocks, and so the node is shutting down for your safety." - ), reorgLength, MAX_REORG_LENGTH) + "\n\n" + - _("Reorganization details") + ":\n" + - "- " + strprintf(_("Current tip: %s, height %d, work %s\nstake %s"), - pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex(), - pindexOldTip->chainPower.chainStake.GetHex()) + "\n" + - "- " + strprintf(_("New tip: %s, height %d, work %s\nstake %s"), - pindexMostWork->phashBlock->GetHex(), pindexMostWork->GetHeight(), pindexMostWork->chainPower.chainWork.GetHex(), - pindexMostWork->chainPower.chainStake.GetHex()) + "\n" + - "- " + strprintf(_("Fork point: %s %s, height %d"), - ASSETCHAINS_SYMBOL,pindexFork->phashBlock->GetHex(), pindexFork->GetHeight()) + "\n\n" + - _("Please help, human!"); - LogPrintf("*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg,reorgLength+10); - fprintf(stderr,"*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg.c_str(),reorgLength+10); - uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR); - StartShutdown(); - return false; - } + auto msg = strprintf(_( + "A block chain reorganization has been detected that would roll back %d blocks! " + "This is larger than the maximum of %d blocks, and so the node is shutting down for your safety." + ), reorgLength, MAX_REORG_LENGTH) + "\n\n" + + _("Reorganization details") + ":\n" + + "- " + strprintf(_("Current tip: %s, height %d, work %s\nstake %s"), + pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex(), + pindexOldTip->chainPower.chainStake.GetHex()) + "\n" + + "- " + strprintf(_("New tip: %s, height %d, work %s\nstake %s"), + pindexMostWork->phashBlock->GetHex(), pindexMostWork->GetHeight(), pindexMostWork->chainPower.chainWork.GetHex(), + pindexMostWork->chainPower.chainStake.GetHex()) + "\n" + + "- " + strprintf(_("Fork point: %s %s, height %d"), + ASSETCHAINS_SYMBOL,pindexFork->phashBlock->GetHex(), pindexFork->GetHeight()) + "\n\n" + + _("Please help, human!"); + LogPrintf("*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg,reorgLength+10); + fprintf(stderr,"*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg.c_str(),reorgLength+10); + uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR); + StartShutdown(); + return false; } // Disconnect active blocks which are no longer in the best chain. From a766a16bb34ee2cb50fa657ab6cc2fa879a5db4b Mon Sep 17 00:00:00 2001 From: blackjok3r Date: Thu, 11 Apr 2019 19:13:37 +0800 Subject: [PATCH 08/15] fix better --- src/main.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 47b089ca8..3d155feb3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4233,22 +4233,22 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo const CBlockIndex *pindexOldTip = chainActive.Tip(); const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork); + // stop trying to reorg if the reorged chain is before last notarized height. + // stay on the same chain tip! + int32_t notarizedht,prevMoMheight; uint256 notarizedhash,txid; + notarizedht = komodo_notarized_height(&prevMoMheight,¬arizedhash,&txid); + if ( pindexFork->GetHeight() < notarizedht ) + { + fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); + return false; + } // - On ChainDB initialization, pindexOldTip will be null, so there are no removable blocks. // - If pindexMostWork is in a chain that doesn't have the same genesis block as our chain, // then pindexFork will be null, and we would need to remove the entire chain including // our genesis block. In practice this (probably) won't happen because of checks elsewhere. auto reorgLength = pindexOldTip ? pindexOldTip->GetHeight() - (pindexFork ? pindexFork->GetHeight() : -1) : 0; assert(MAX_REORG_LENGTH > 0);//, "We must be able to reorg some distance"); - - int32_t notarizedht,prevMoMheight; uint256 notarizedhash,txid; - notarizedht = komodo_notarized_height(&prevMoMheight,¬arizedhash,&txid); - if ( pindexFork->GetHeight() < notarizedht ) - { - fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); - pindexFork = pindexOldTip; - } - - if (reorgLength > MAX_REORG_LENGTH) + if ( reorgLength > MAX_REORG_LENGTH) { auto msg = strprintf(_( "A block chain reorganization has been detected that would roll back %d blocks! " From 0ac427ee97842283e3369278f61ab998f12dfd8e Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 00:28:35 -1100 Subject: [PATCH 09/15] Prices update --- src/komodo_defs.h | 3 ++ src/komodo_gateway.h | 92 +++++++++++++++++++++++++++++--------------- src/main.cpp | 2 + 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 31dde4a24..d1749def8 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -84,7 +84,10 @@ extern char NOTARYADDRS[64][64]; int tx_height( const uint256 &hash ); extern std::vector vWhiteListAddress; void komodo_netevent(std::vector payload); + +#define PRICES_SMOOTHWIDTH 1 int32_t komodo_priceind(char *symbol); void komodo_pricesinit(); +void komodo_pricesupdate(int32_t height,CBlock *pblock); #endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e128043ff..fac601fbc 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1548,6 +1548,7 @@ void komodo_passport_iteration() } } + extern std::vector Mineropret; // opreturn data set by the data gathering code #define PRICES_ERRORRATE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR @@ -1563,6 +1564,20 @@ const char *Forex[] = { "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" }; // must be in ECB list +struct komodo_extremeprice +{ + uint256 blockhash; + uint32_t pricebits,timestamp; + int32_t height; + int16_t dir,ind; +} ExtremePrice; + +struct komodo_priceinfo +{ + FILE *fp; + char symbol[64]; +} PRICES[KOMODO_MAXPRICES]; + uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][KOMODO_MAXPRICES];//4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; int64_t PriceMult[KOMODO_MAXPRICES]; int32_t komodo_cbopretsize(uint64_t flags); @@ -1575,26 +1590,33 @@ void komodo_PriceCache_shift() memcpy(PriceCache[0],Mineropret.data(),Mineropret.size()); } +int32_t _komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,CBlock *block) +{ + CTransaction tx; int32_t numvouts; std::vector vopret; + tx = block->vtx[0]; + numvouts = (int32_t)tx.vout.size(); + GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); + if ( vopret.size() >= PRICES_SIZEBIT0 ) + { + if ( seedp != 0 ) + memcpy(seedp,&block->hashMerkleRoot,sizeof(*seedp)); + memcpy(heightbits,vopret.data(),vopret.size()); + return((int32_t)(vopret.size()/sizeof(uint32_t))); + } + return(-1); +} + // komodo_heightpricebits() extracts the price data in the coinbase for nHeight int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight) { - CBlockIndex *pindex; CBlock block; CTransaction tx; int32_t numvouts; std::vector vopret; + CBlockIndex *pindex; CBlock block; if ( seedp != 0 ) *seedp = 0; if ( (pindex= komodo_chainactive(nHeight)) != 0 ) { if ( komodo_blockload(block,pindex) == 0 ) { - tx = block.vtx[0]; - numvouts = (int32_t)tx.vout.size(); - GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); - if ( vopret.size() >= PRICES_SIZEBIT0 ) - { - if ( seedp != 0 ) - memcpy(seedp,&pindex->hashMerkleRoot,sizeof(*seedp)); - memcpy(heightbits,vopret.data(),vopret.size()); - return((int32_t)(vopret.size()/sizeof(uint32_t))); - } + return(_komodo_heightpricebits(seedp,heightbits,&block)); } } fprintf(stderr,"couldnt get pricebits for %d\n",nHeight); @@ -1722,13 +1744,6 @@ CScript komodo_mineropret(int32_t nHeight) */ -struct komodo_extremeprice -{ - uint256 blockhash; - uint32_t pricebits,timestamp; - int32_t height; - int16_t dir,ind; -} ExtremePrice; void komodo_queuelocalprice(int32_t dir,int32_t height,uint32_t timestamp,uint256 blockhash,int32_t ind,uint32_t pricebits) { @@ -2529,7 +2544,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice void komodo_pricesinit() { - FILE *fp,*fp0 = 0; char symbol[65]; int32_t i; + int32_t i; boost::filesystem::path pricefname,pricesdir = GetDataDir() / "prices"; fprintf(stderr,"pricesinit (%s)\n",pricesdir.string().c_str()); if (!boost::filesystem::exists(pricesdir)) @@ -2537,23 +2552,38 @@ void komodo_pricesinit() boost::filesystem::create_directories(pricesdir); for (i=0; i 0 ) + if ( i > 0 && PRICES[0].fp != 0 ) { - fseek(fp0,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(uint32_t) * i,SEEK_SET); - fputc(0,fp0); - fflush(fp0); + fseek(PRICES[0].fp,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(uint32_t) * i,SEEK_SET); + fputc(0,PRICES[0].fp); + fflush(PRICES[0].fp); } } } + +void komodo_pricesupdate(int32_t height,CBlock *pblock) +{ + static int numprices; + int32_t i; uint64_t seed; uint32_t rawprices[KOMODO_MAXPRICES]; + if ( numprices == 0 ) + numprices = (int32_t)(komodo_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); + if ( _komodo_heightpricebits(&seed,rawprices,pblock) == numprices ) + { + for (i=0; iGetHeight(),pblock); if ( ASSETCHAINS_SAPLING <= 0 && pindexNew->nTime > KOMODO_SAPLING_ACTIVATION - 24*3600 ) komodo_activate_sapling(pindexNew); return true; From c8d0608674d460bc9542a774891748594401afbd Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 00:35:13 -1100 Subject: [PATCH 10/15] PRICES[i]. --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index fac601fbc..27eda9d5c 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2556,7 +2556,7 @@ void komodo_pricesinit() break; if ( i == 0 ) strcpy(PRICES[i].symbol,"rawprices"); - pricefname = pricesdir / symbol; + pricefname = pricesdir / PRICES[i].symbol; PRICES[i].fp = fopen(pricefname.string().c_str(), "wb+"); fseek(PRICES[i].fp,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(int64_t) * 3,SEEK_SET); fputc(0,PRICES[i].fp); From 348c7a970cc71d58055c6ed9d75af1de13e2a9c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 00:42:31 -1100 Subject: [PATCH 11/15] Update raw prices --- src/komodo_defs.h | 1 - src/komodo_gateway.h | 6 ++++++ src/main.cpp | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index d1749def8..ebfadee69 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -88,6 +88,5 @@ void komodo_netevent(std::vector payload); #define PRICES_SMOOTHWIDTH 1 int32_t komodo_priceind(char *symbol); void komodo_pricesinit(); -void komodo_pricesupdate(int32_t height,CBlock *pblock); #endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 27eda9d5c..625cdc6b5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2582,6 +2582,12 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock) for (i=0; i Date: Thu, 11 Apr 2019 02:48:20 -1100 Subject: [PATCH 12/15] Calc correlated and smoothed each block --- src/komodo_gateway.h | 66 +++++++++++++++++++++++++++++++++++------- src/rpc/blockchain.cpp | 23 ++++++++------- 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 625cdc6b5..837a82570 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2516,9 +2516,11 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) } else memcpy(dest,src,width*sizeof(*dest)); } -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprices,int32_t numprices) +// http://www.holoborodko.com/pavel/numerical-methods/noise-robust-smoothing-filter/ +//const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; + +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip) { - //const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; int32_t i; int64_t sum=0,nonzprice,price; if ( PRICES_DAYWINDOW < 2 ) return(0); @@ -2533,12 +2535,9 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *rawprice { if ( (price= correlated[i*cskip]) != 0 ) nonzprice = price; - //correlated2[i] = nonzprice / PRICES_DAYWINDOW; // reduce precision sum += nonzprice; } price = sum / PRICES_DAYWINDOW; - // improve smoothing with correlated2 processing - // price = smooth(correlated2,PRICES_DAYWINDOW,price/daywindow) * PRICES_DAYWINDOW; return(price); } @@ -2573,20 +2572,67 @@ void komodo_pricesinit() void komodo_pricesupdate(int32_t height,CBlock *pblock) { - static int numprices; - int32_t i; uint64_t seed; uint32_t rawprices[KOMODO_MAXPRICES]; + static int numprices; static uint32_t *ptr32,*ptr64; + int32_t ind,offset,width; int64_t correlated,smoothed; uint64_t seed,rngval; uint32_t rawprices[KOMODO_MAXPRICES],buf[4]; + width = (2*PRICES_DAYWINDOW + PRICES_SMOOTHWIDTH); if ( numprices == 0 ) + { numprices = (int32_t)(komodo_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); + ptr32 = (uint32_t *)calloc(sizeof(uint32_t),numprices * width); + ptr64 = (int64_t *)calloc(sizeof(int64_t),PRICES_DAYWINDOW*3); + } if ( _komodo_heightpricebits(&seed,rawprices,pblock) == numprices ) { - for (i=0; i width ) + { + fseek(PRICES[0].fp,(height-width+1) * numprices * sizeof(uint32_t),SEEK_SET); + if ( fread(ptr32,sizeof(uint32_t),width*numprices,PRICES[0].fp) == width*numprices ) + { + rngval = seed; + for (ind=1; ind 0 ) + { + fseek(PRICES[ind].fp,height * sizeof(int64_t) * 3,SEEK_SET); + buf[0] = rawprices[ind]; + buf[1] = rawprices[0]; // timestamp + memcpy(&buf[2],&correlated,sizeof(correlated)); + if ( fwrite(buf,1,sizeof(buf),PRICES[ind].fp) != sizeof(buf) ) + fprintf(stderr,"error fwrite buf for ht.%d ind.%d\n",height,ind); + else + { + fseek(PRICES[ind].fp,(height-PRICES_DAYWINDOW+1) * 3 * sizeof(int64_t),SEEK_SET); + if ( fread(ptr64,sizeof(int64_t),PRICES_DAYWINDOW*3,PRICES[ind].fp) == PRICES_DAYWINDOW*3 ) + { + if ( (smoothed= komodo_pricesmoothed(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 ) + { + fseek(PRICES[ind].fp,(height * 3 + 2) * sizeof(int64_t),SEEK_SET); + if ( fwrite(&smoothed,1,sizeof(smoothed),PRICES[ind].fp) != sizeof(smoothed) ) + fprintf(stderr,"error fwrite smoothed for ht.%d ind.%d\n",height,ind); + else + { + fprintf(stderr,"%.4f ",(double)smoothed/COIN); + fflush(PRICES[ind].fp); + } + } else fprintf(stderr,"error price_smoothed ht.%d ind.%d\n",height,ind); + } else fprintf(stderr,"error fread ptr64 for ht.%d ind.%d\n",height,ind); + } + } else fprintf(stderr,"error komodo_pricecorrelated for ht.%d ind.%d\n",height,ind); + } + fprintf(stderr,"height.%d\n",height); + } else fprintf(stderr,"error reading rawprices for ht.%d\n",height); + } } } else fprintf(stderr,"numprices mismatch\n"); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index fc9a71fe2..87f2658cd 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,7 +1176,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *correlated2,int32_t numprices); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip); int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t rawskip,uint32_t *nonzprices,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1185,7 +1185,7 @@ int64_t komodo_pricemult(int32_t ind); int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) { - int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; int64_t *correlated2; uint32_t rawprices[1440*6],*ptr; + int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; uint32_t rawprices[1440*6],*ptr; //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; @@ -1205,20 +1205,21 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, ptr[1] = rawprices[0]; // timestamp } rngval = seed; - correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); + //correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); for (i=0; i2; i++,ht--) @@ -1287,14 +1288,14 @@ UniValue prices(const UniValue& params, bool fHelp) { offset = j*width + i; rngval = (rngval*11109 + 13849); - correlated2[i] = prices[offset]; + //correlated2[i] = prices[offset]; if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],1,0,PRICES_SMOOTHWIDTH)) < 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); } for (i=0; i Date: Thu, 11 Apr 2019 02:49:33 -1100 Subject: [PATCH 13/15] Int64 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 837a82570..8bc639050 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2572,7 +2572,7 @@ void komodo_pricesinit() void komodo_pricesupdate(int32_t height,CBlock *pblock) { - static int numprices; static uint32_t *ptr32,*ptr64; + static int numprices; static uint32_t *ptr32; static int64_t *ptr64; int32_t ind,offset,width; int64_t correlated,smoothed; uint64_t seed,rngval; uint32_t rawprices[KOMODO_MAXPRICES],buf[4]; width = (2*PRICES_DAYWINDOW + PRICES_SMOOTHWIDTH); if ( numprices == 0 ) From 1862321861e745a1dfa3ed50d5366e299cbfc1ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 02:52:49 -1100 Subject: [PATCH 14/15] Priceave --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 19 +++---------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 8bc639050..ce89cea35 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2519,7 +2519,7 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) // http://www.holoborodko.com/pavel/numerical-methods/noise-robust-smoothing-filter/ //const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip) +int64_t komodo_priceave(int64_t *correlated,int32_t cskip) { int32_t i; int64_t sum=0,nonzprice,price; if ( PRICES_DAYWINDOW < 2 ) @@ -2615,7 +2615,7 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock) fseek(PRICES[ind].fp,(height-PRICES_DAYWINDOW+1) * 3 * sizeof(int64_t),SEEK_SET); if ( fread(ptr64,sizeof(int64_t),PRICES_DAYWINDOW*3,PRICES[ind].fp) == PRICES_DAYWINDOW*3 ) { - if ( (smoothed= komodo_pricesmoothed(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 ) + if ( (smoothed= komodo_priceave(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 ) { fseek(PRICES[ind].fp,(height * 3 + 2) * sizeof(int64_t),SEEK_SET); if ( fwrite(&smoothed,1,sizeof(smoothed),PRICES[ind].fp) != sizeof(smoothed) ) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 87f2658cd..07cb8a26d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,7 +1176,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip); +int64_t komodo_priceave(int64_t *correlated,int32_t cskip); int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t rawskip,uint32_t *nonzprices,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1186,8 +1186,6 @@ int64_t komodo_pricemult(int32_t ind); int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) { int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; uint32_t rawprices[1440*6],*ptr; - //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; - //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; komodo_heightpricebits(&seed,rawprices,firstheight + numblocks - 1); if ( firstheight < width ) @@ -1205,21 +1203,15 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, ptr[1] = rawprices[0]; // timestamp } rngval = seed; - //correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); for (i=0; i2; i++,ht--) { @@ -1288,14 +1278,13 @@ UniValue prices(const UniValue& params, bool fHelp) { offset = j*width + i; rngval = (rngval*11109 + 13849); - //correlated2[i] = prices[offset]; if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],1,0,PRICES_SMOOTHWIDTH)) < 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); } for (i=0; i Date: Thu, 11 Apr 2019 21:59:04 +0800 Subject: [PATCH 15/15] add node ban for pushing attacking chain --- src/main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3d155feb3..5964b33d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3904,7 +3904,8 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { if ( block.GetHash() == notarizedhash ) { fprintf(stderr,"DisconnectTip trying to disconnect notarized block at ht.%d\n",(int32_t)pindexDelete->GetHeight()); - return(false); + return state.DoS(100, error("AcceptBlock(): DisconnectTip trying to disconnect notarized blockht.%d",(int32_t)pindexDelete->GetHeight()), + REJECT_INVALID, "past-notarized-height"); } } // Apply the block atomically to the chain state. @@ -4240,7 +4241,8 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo if ( pindexFork->GetHeight() < notarizedht ) { fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); - return false; + return state.DoS(100, error("ActivateBestChainStep(): pindexFork->GetHeight().%d is < notarizedht %d, so ignore it",(int32_t)pindexFork->GetHeight(),notarizedht), + REJECT_INVALID, "past-notarized-height"); } // - On ChainDB initialization, pindexOldTip will be null, so there are no removable blocks. // - If pindexMostWork is in a chain that doesn't have the same genesis block as our chain,