diff --git a/src/cc/dilithium.c b/src/cc/dilithium.c index 7ba522f0f..087dfd9f5 100644 --- a/src/cc/dilithium.c +++ b/src/cc/dilithium.c @@ -2717,8 +2717,9 @@ int64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack, *tshake; static int cmp_llu(const void *a, const void*b) { if(*(int64_t *)a < *(int64_t *)b) return -1; - if(*(int64_t *)a > *(int64_t *)b) return 1; - return 0; + else if(*(int64_t *)a > *(int64_t *)b) return 1; + else if ( (uint64_t)a < (uint64_t)b ) return -1; + else return 1; } static int64_t median(int64_t *l, size_t llen) diff --git a/src/cryptoconditions/src/asn/constr_SET_OF.c b/src/cryptoconditions/src/asn/constr_SET_OF.c index 2dbc6e518..25e80cc30 100644 --- a/src/cryptoconditions/src/asn/constr_SET_OF.c +++ b/src/cryptoconditions/src/asn/constr_SET_OF.c @@ -301,6 +301,9 @@ static int _el_buf_cmp(const void *ap, const void *bp) { ret = -1; else if(a->length > b->length) ret = 1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + ret = -1; + else ret = 1; } return ret; diff --git a/src/cryptoconditions/src/threshold.c b/src/cryptoconditions/src/threshold.c index 82f0e1b0a..7fc801897 100644 --- a/src/cryptoconditions/src/threshold.c +++ b/src/cryptoconditions/src/threshold.c @@ -35,8 +35,15 @@ static uint32_t thresholdSubtypes(const CC *cond) { } -static int cmpCostDesc(const void *a, const void *b) { - return (int) ( *(unsigned long*)b - *(unsigned long*)a ); +static int cmpCostDesc(const void *a, const void *b) +{ + int retval; + retval = (int) ( *(unsigned long*)b - *(unsigned long*)a ); + if ( retval != 0 ) + return(retval); + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } @@ -79,7 +86,9 @@ static int cmpConditionBin(const void *a, const void *b) { if (ret == 0) return r0.encoded < r1.encoded ? -1 : 1; - return 0; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } diff --git a/src/init.cpp b/src/init.cpp index 24b32a720..042d1d9fd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1886,7 +1886,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) uiInterface.InitMessage(_("Activating best chain...")); // scan for better chains in the block chain database, that are not yet connected in the active best chain CValidationState state; - if ( !ActivateBestChain(state)) + if ( !ActivateBestChain(true,state)) strErrors << "Failed to connect best block"; } std::vector vImportFiles; diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index ce89cea35..6bd03ccfb 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2353,9 +2353,12 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t firstprice,price,sum,den,mult,refprice,lowprice,highprice; if ( PRICES_DAYWINDOW < 2 || ind >= KOMODO_MAXPRICES ) return(-1); - mult = PriceMult[ind]; + mult = komodo_pricemult(ind); if ( nonzprices != 0 ) memset(nonzprices,0,sizeof(*nonzprices)*PRICES_DAYWINDOW); + //for (i=0; i *(int64_t *)b) return 1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); +} + +static int64_t sort64(int64_t *l, int32_t llen) +{ + qsort(l,llen,sizeof(uint64_t),cmp_llu); +} + +static int revcmp_llu(const void *a, const void*b) +{ + if(*(int64_t *)a < *(int64_t *)b) return 1; + else if(*(int64_t *)a > *(int64_t *)b) return -1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); +} + +static int64_t revsort64(int64_t *l, int32_t llen) +{ + qsort(l,llen,sizeof(uint64_t),revcmp_llu); +} + +int64_t komodo_priceave(int64_t *buf,int64_t *correlated,int32_t cskip) +{ + int32_t i,dir=0; int64_t sum=0,nonzprice,price,halfave,thirdave,fourthave,decayprice; if ( PRICES_DAYWINDOW < 2 ) return(0); for (i=0; i price ) // rising prices + sort64(buf,PRICES_DAYWINDOW); + else revsort64(buf,PRICES_DAYWINDOW); + decayprice = buf[0]; + for (i=0; i %.8f\n",halfave 0 && PRICES[0].fp != 0 ) - { - fseek(PRICES[0].fp,(2*PRICES_DAYWINDOW+PRICES_SMOOTHWIDTH) * sizeof(uint32_t) * i,SEEK_SET); - fputc(0,PRICES[0].fp); - fflush(PRICES[0].fp); - } + } + if ( i > 0 && PRICES[0].fp != 0 && createflag != 0 ) + { + 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; static uint32_t *ptr32; static int64_t *ptr64; + static int numprices; static uint32_t *ptr32; static int64_t *ptr64,*tmpbuf; 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); + width = PRICES_DAYWINDOW;//(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); + tmpbuf = (int64_t *)calloc(sizeof(int64_t),2*PRICES_DAYWINDOW); + fprintf(stderr,"prices update: numprices.%d %p %p\n",numprices,ptr32,ptr64); } if ( _komodo_heightpricebits(&seed,rawprices,pblock) == numprices ) { - //for (i=0; i width ) + if ( height > PRICES_DAYWINDOW ) { 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 ) @@ -2610,19 +2664,20 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock) 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 + else if ( height > PRICES_DAYWINDOW*2 ) { 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 ( fread(ptr64,sizeof(int64_t),PRICES_DAYWINDOW*3-1,PRICES[ind].fp) == PRICES_DAYWINDOW*3-1 ) { - if ( (smoothed= komodo_priceave(&ptr64[PRICES_DAYWINDOW*3-1],-3)) > 0 ) + if ( (smoothed= komodo_priceave(tmpbuf,&ptr64[PRICES_DAYWINDOW*3-2],-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); + if ( ind == 36 ) + fprintf(stderr,"(%.8f %.8f) ",(double)ptr64[PRICES_DAYWINDOW*3-2]/COIN,(double)smoothed/COIN); fflush(PRICES[ind].fp); } } else fprintf(stderr,"error price_smoothed ht.%d ind.%d\n",height,ind); @@ -2632,10 +2687,9 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock) } fprintf(stderr,"height.%d\n",height); } else fprintf(stderr,"error reading rawprices for ht.%d\n",height); - } - } + } else fprintf(stderr,"height.%d <= width.%d\n",height,width); + } else fprintf(stderr,"null PRICES[0].fp\n"); } else fprintf(stderr,"numprices mismatch\n"); - } diff --git a/src/main.cpp b/src/main.cpp index 0ebc5c892..f1e586337 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4231,7 +4231,7 @@ static void PruneBlockIndexCandidates() { * Try to make some progress towards making pindexMostWork the active block. * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork. */ -static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { +static bool ActivateBestChainStep(bool fSkipdpow, CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { AssertLockHeld(cs_main); bool fInvalidFound = false; const CBlockIndex *pindexOldTip = chainActive.Tip(); @@ -4241,7 +4241,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo // stay on the same chain tip! int32_t notarizedht,prevMoMheight; uint256 notarizedhash,txid; notarizedht = komodo_notarized_height(&prevMoMheight,¬arizedhash,&txid); - if ( pindexFork != 0 && pindexFork->GetHeight() < notarizedht ) + if ( !fSkipdpow && pindexFork != 0 && pindexFork->GetHeight() < notarizedht ) { fprintf(stderr,"pindexFork->GetHeight().%d is < notarizedht %d, so ignore it\n",(int32_t)pindexFork->GetHeight(),notarizedht); return state.DoS(100, error("ActivateBestChainStep(): pindexFork->GetHeight().%d is < notarizedht %d, so ignore it",(int32_t)pindexFork->GetHeight(),notarizedht), @@ -4366,7 +4366,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo * or an activated best chain. pblock is either NULL or a pointer to a block * that is already loaded (to avoid loading it again from disk). */ -bool ActivateBestChain(CValidationState &state, CBlock *pblock) { +bool ActivateBestChain(bool fSkipdpow, CValidationState &state, CBlock *pblock) { CBlockIndex *pindexNewTip = NULL; CBlockIndex *pindexMostWork = NULL; const CChainParams& chainParams = Params(); @@ -4382,7 +4382,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) { if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip()) return true; - if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL)) + if (!ActivateBestChainStep(fSkipdpow, state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL)) return false; pindexNewTip = chainActive.Tip(); fInitialDownload = IsInitialBlockDownload(); @@ -5542,7 +5542,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo //else fprintf(stderr,"added block %s %p\n",pindex->GetBlockHash().ToString().c_str(),pindex->pprev); } - if (futureblock == 0 && !ActivateBestChain(state, pblock)) + if (futureblock == 0 && !ActivateBestChain(false, state, pblock)) return error("%s: ActivateBestChain failed", __func__); //fprintf(stderr,"finished ProcessBlock %d\n",(int32_t)chainActive.LastTip()->GetHeight()); @@ -6319,7 +6319,7 @@ bool InitBlockIndex() { return error("LoadBlockIndex(): couldnt add to block index"); if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) return error("LoadBlockIndex(): genesis block not accepted"); - if (!ActivateBestChain(state, &block)) + if (!ActivateBestChain(true, state, &block)) return error("LoadBlockIndex(): genesis block cannot be activated"); // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); diff --git a/src/main.h b/src/main.h index 0cdb0b7a8..e5f042c31 100644 --- a/src/main.h +++ b/src/main.h @@ -252,7 +252,7 @@ std::string GetWarnings(const std::string& strFor); /** Retrieve a transaction (from memory pool, or from disk, if possible) */ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false); /** Find the best known block, and make it the tip of the block chain */ -bool ActivateBestChain(CValidationState &state, CBlock *pblock = NULL); +bool ActivateBestChain(bool fSkipdpow, CValidationState &state, CBlock *pblock = NULL); CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams); /** diff --git a/src/pow/tromp/equi.h b/src/pow/tromp/equi.h index f6d8803c2..84e566e1a 100644 --- a/src/pow/tromp/equi.h +++ b/src/pow/tromp/equi.h @@ -75,8 +75,14 @@ int verifyrec(const crypto_generichash_blake2b_state *ctx, u32 *indices, uchar * } int compu32(const void *pa, const void *pb) { + int32_t retval; u32 a = *(u32 *)pa, b = *(u32 *)pb; - return a