diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index b65fcf1a9..4e010d28e 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -64,7 +64,7 @@ extern uint64_t ASSETCHAINS_CBOPRET; void komodo_passport_iteration(); uint64_t komodo_interestsum(); int32_t komodo_longestchain(); -void komodo_cbopretupdate(); +void komodo_cbopretupdate(int32_t forceflag); void WaitForShutdown(boost::thread_group* threadGroup) { @@ -89,7 +89,7 @@ void WaitForShutdown(boost::thread_group* threadGroup) //komodo_interestsum(); //komodo_longestchain(); if ( ASSETCHAINS_CBOPRET != 0 ) - komodo_cbopretupdate(); + komodo_cbopretupdate(0); for (i=0; i<=ASSETCHAINS_BLOCKTIME/5; i++) { fShutdown = ShutdownRequested(); diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a59e47a04..6035cf82f 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1551,6 +1551,15 @@ 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_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR +#define KOMODO_LOCALPRICE_CACHESIZE 7 +uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; + +void komodo_PriceCache_shift() +{ + int32_t i; + for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>=0; i--) + memcpy(PriceCache[i],PriceCache[i-1],sizeof(PriceCache[i])); +} // komodo_heightpricebits() extracts the price data in the coinbase for nHeight int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) @@ -1574,20 +1583,6 @@ int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) return(-1); } -int32_t komodo_prices(uint32_t *prices,uint32_t *correlated,uint32_t *smoothed,int32_t height) -{ - int32_t i,n; - n = komodo_heightpricebits(prices,height); - for (i=0; i vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,prevtime,maxflag,lag,lag2,lag3,n; uint32_t now = (uint32_t)time(NULL); + std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { GetOpReturnData(scriptPubKey,vopret); @@ -1778,16 +1773,48 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( localbits[i] == 0 ) localbits[i] = prevbits[i]; } - for (i=1; i 0 && localbits[i] < prevbits[i] ) - return(-1); - else if ( maxflag < 0 && localbits[i] > prevbits[i] ) - return(-1); + if ( (maxflag= maxflags[i]) != 0 ) + { + // make sure local price is moving in right direction + fprintf(stderr,"maxflag.%d i.%d localbits.%u vs pricebits.%u prevbits.%u\n",maxflag,i,localbits[i],pricebits[i],prevbits[i]); + if ( maxflag > 0 && localbits[i] < prevbits[i] ) + { + if ( iter == 0 ) + break; + // second iteration checks recent prices to see if within local volatility + for (j=0; j= prevbits[i] ) + { + fprintf(stderr,"within recent localprices[%d] %u >= %u\n",j,PriceCache[j][i],prevbits[i]); + continue; + } + break; + } + else if ( maxflag < 0 && localbits[i] > prevbits[i] ) + { + if ( iter == 0 ) + break; + for (j=0; j 333 ) - ASSETCHAINS_CBOPRET = 7; +if ( komodo_nextheight() > 333 ) // for debug only! + ASSETCHAINS_CBOPRET = 7; size = PRICES_SIZEBIT0; if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) size += sizeof(forexprices); @@ -2076,31 +2104,48 @@ void komodo_cbopretupdate() if ( Mineropret.size() < size ) Mineropret.resize(size); size = PRICES_SIZEBIT0; - if ( now > lastbtc+120 && get_btcusd(pricebits) == 0 ) + if ( (forceflag != 0 || now > lastbtc+120) && get_btcusd(pricebits) == 0 ) { - memcpy(Mineropret.data(),pricebits,PRICES_SIZEBIT0); - lastbtc = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + memcpy(PriceCache[0],pricebits,PRICES_SIZEBIT0); + flags |= 1; } if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) { - if ( now > lasttime+3600*5 || forexprices[0] == 0 ) + if ( now > lasttime+3600*5 || forexprices[0] == 0 ) // cant assume timestamp is valid for forex price as it is a daily weekday changing thing anyway. { get_dailyfx(forexprices); - memcpy(&Mineropret.data()[size],forexprices,sizeof(forexprices)); - lasttime = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + flags |= 2; + memcpy(&PriceCache[0][size],forexprices,sizeof(forexprices)); } size += sizeof(forexprices); } if ( (ASSETCHAINS_CBOPRET & 4) != 0 ) { - if ( now > lastcrypto+100 ) + if ( forceflag != 0 || flags != 0 ) { get_cryptoprices(cryptoprices,Cryptos,(int32_t)(sizeof(Cryptos)/sizeof(*Cryptos))); - memcpy(&Mineropret.data()[size],cryptoprices,sizeof(cryptoprices)); - lastcrypto = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + memcpy(&PriceCache[0][size],cryptoprices,sizeof(cryptoprices)); + flags |= 4; // very rarely we can see flags == 6 case } size += sizeof(cryptoprices); } + if ( flags != 0 ) + { + now = (uint32_t)time(NULL); + if ( (flags & 1) != 0 ) + lastbtc = now; + if ( (flags & 2) != 0 ) + lasttime = now; + if ( (flags & 4) != 0 ) + lastcrypto = now; + memcpy(Mineropret.data(),PriceCache[0],size); + } //int32_t i; for (i=0; i sizeof(heights)/sizeof(*heights) ) - maxsamples = sizeof(heights)/sizeof(*heights); nextheight = komodo_nextheight(); UniValue a(UniValue::VARR); + if ( daywindow < 7 ) + daywindow = 7; prices = (uint32_t *)calloc(sizeof(*prices),maxsamples+daywindow); correlated = (uint32_t *)calloc(sizeof(*correlated),maxsamples+daywindow); smoothed = (uint32_t *)calloc(sizeof(*smoothed),maxsamples+daywindow); @@ -1198,7 +1201,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); else { - if ( (n= komodo_prices(rawprices,ht)) > 0 ) + if ( (n= komodo_heightpricebits(rawprices,ht)) > 0 ) { if ( firstn == -1 ) firstn = n; @@ -1212,42 +1215,45 @@ UniValue prices(const UniValue& params, bool fHelp) } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } } + for (i=0; i