Merge pull request #1402 from jl777/FSM

FSM
This commit is contained in:
jl777
2019-04-11 19:04:48 -11:00
committed by GitHub
9 changed files with 129 additions and 52 deletions

View File

@@ -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)

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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<boost::filesystem::path> vImportFiles;

View File

@@ -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<PRICES_DAYWINDOW; i++)
// fprintf(stderr,"%u ",rawprices[i*rawskip]);
//fprintf(stderr,"ind.%d\n",ind);
for (iter=0; iter<PRICES_DAYWINDOW; iter++)
{
correlation = 0;
@@ -2518,10 +2521,37 @@ 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_priceave(int64_t *correlated,int32_t cskip)
static int cmp_llu(const void *a, const void*b)
{
int32_t i; int64_t sum=0,nonzprice,price;
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 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<PRICES_DAYWINDOW; i++)
@@ -2535,56 +2565,80 @@ int64_t komodo_priceave(int64_t *correlated,int32_t cskip)
{
if ( (price= correlated[i*cskip]) != 0 )
nonzprice = price;
buf[PRICES_DAYWINDOW+i] = nonzprice;
sum += nonzprice;
if ( i == PRICES_DAYWINDOW/2 )
halfave = (sum / (PRICES_DAYWINDOW/2));
else if ( i == PRICES_DAYWINDOW/3 )
thirdave = (sum / (PRICES_DAYWINDOW/3));
else if ( i == PRICES_DAYWINDOW/4 )
fourthave = (sum / (PRICES_DAYWINDOW/4));
}
memcpy(buf,&buf[PRICES_DAYWINDOW],PRICES_DAYWINDOW*sizeof(*buf));
price = sum / PRICES_DAYWINDOW;
return(price);
if ( halfave == price )
return(price);
else if ( halfave > price ) // rising prices
sort64(buf,PRICES_DAYWINDOW);
else revsort64(buf,PRICES_DAYWINDOW);
decayprice = buf[0];
for (i=0; i<PRICES_DAYWINDOW; i++)
{
decayprice = ((decayprice * 97) + (buf[i] * 3)) / 100;
//fprintf(stderr,"%.4f ",(double)buf[i]/COIN);
}
fprintf(stderr,"%ssort half %.8f %.8f %.8f %.8f %.8f %.8f -> %.8f\n",halfave<price?"rev":"",(double)price/COIN,(double)halfave/COIN,(double)thirdave/COIN,(double)fourthave/COIN,(double)decayprice/COIN,(double)buf[PRICES_DAYWINDOW-1]/COIN,(double)(price*7 + halfave*5 + thirdave*3 + fourthave*2 + decayprice + buf[PRICES_DAYWINDOW-1])/(19*COIN));
return((price*7 + halfave*5 + thirdave*3 + fourthave*2 + decayprice + buf[PRICES_DAYWINDOW-1]) / 19);
}
void komodo_pricesinit()
{
int32_t i;
int32_t i,createflag = 0;
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), createflag = 1;
for (i=0; i<KOMODO_MAXPRICES; i++)
{
boost::filesystem::create_directories(pricesdir);
for (i=0; i<KOMODO_MAXPRICES; i++)
if ( komodo_pricename(PRICES[i].symbol,i) == 0 )
break;
if ( i == 0 )
strcpy(PRICES[i].symbol,"rawprices");
pricefname = pricesdir / PRICES[i].symbol;
PRICES[i].fp = fopen(pricefname.string().c_str(), createflag != 0 ? "wb+" : "rb+");
if ( createflag != 0 )
{
if ( komodo_pricename(PRICES[i].symbol,i) == 0 )
break;
if ( i == 0 )
strcpy(PRICES[i].symbol,"rawprices");
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);
fflush(PRICES[i].fp);
}
if ( i > 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<numprices; i++)
// fprintf(stderr,"%u ",rawprices[i]);
//for (ind=0; ind<numprices; ind++)
// fprintf(stderr,"%u ",rawprices[ind]);
//fprintf(stderr,"numprices.%d\n",numprices);
if ( PRICES[0].fp != 0 )
{
@@ -2592,7 +2646,7 @@ void komodo_pricesupdate(int32_t height,CBlock *pblock)
if ( fwrite(rawprices,sizeof(uint32_t),numprices,PRICES[0].fp) != numprices )
fprintf(stderr,"error writing rawprices for ht.%d\n",height);
else fflush(PRICES[0].fp);
if ( height > 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");
}

View File

@@ -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,&notarizedhash,&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);

View File

@@ -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);
/**

View File

@@ -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<b ? -1 : a==b ? 0 : +1;
retval = a<b ? -1 : a==b ? 0 : +1;
if ( retval != 0 )
return(retval);
else if ( (uint64_t)pa < (uint64_t)pb ) // jl777 prevent nondeterminism
return(-1);
else return(1);
}
bool duped(proof prf) {

View File

@@ -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_priceave(int64_t *correlated,int32_t cskip);
int64_t komodo_priceave(int64_t *tmpbuf,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; 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; int64_t *tmpbuf;
width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH;
komodo_heightpricebits(&seed,rawprices,firstheight + numblocks - 1);
if ( firstheight < width )
@@ -1210,8 +1210,10 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,
if ( (pricedata[i*3+1]= komodo_pricecorrelated(rngval,ind,(uint32_t *)&pricedata[i*3],6,0,PRICES_SMOOTHWIDTH)) < 0 )
return(-3);
}
tmpbuf = (int64_t *)calloc(sizeof(int64_t),2*PRICES_DAYWINDOW);
for (i=0; i<numblocks; i++)
pricedata[i*3+2] = komodo_priceave(&pricedata[i*3+1],3);
pricedata[i*3+2] = komodo_priceave(tmpbuf,&pricedata[i*3+1],3);
free(tmpbuf);
return(0);
}
@@ -1220,7 +1222,7 @@ UniValue prices(const UniValue& params, bool fHelp)
if ( fHelp || params.size() != 1 )
throw runtime_error("prices maxsamples\n");
LOCK(cs_main);
UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[1440*6],*prices; uint32_t i,width,j,numpricefeeds=-1,n,numsamples,nextheight,offset,ht;
UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t *tmpbuf,smoothed,*correlated; char name[64],*str; uint32_t rawprices[1440*6],*prices; uint32_t i,width,j,numpricefeeds=-1,n,numsamples,nextheight,offset,ht;
if ( ASSETCHAINS_CBOPRET == 0 )
throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices");
@@ -1281,10 +1283,11 @@ UniValue prices(const UniValue& params, bool fHelp)
if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],1,0,PRICES_SMOOTHWIDTH)) < 0 )
throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price");
}
tmpbuf = (int64_t *)calloc(sizeof(int64_t),2*PRICES_DAYWINDOW);
for (i=0; i<maxsamples&&i<numsamples; i++)
{
offset = j*width + i;
smoothed = komodo_priceave(&correlated[i],1);
smoothed = komodo_priceave(tmpbuf,&correlated[i],1);
UniValue parr(UniValue::VARR);
parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j)));
parr.push_back(ValueFromAmount(correlated[i]));
@@ -1292,6 +1295,7 @@ UniValue prices(const UniValue& params, bool fHelp)
// compare to alternate method
p.push_back(parr);
}
free(tmpbuf);
}
else
{
@@ -1875,7 +1879,7 @@ UniValue invalidateblock(const UniValue& params, bool fHelp)
}
if (state.IsValid()) {
ActivateBestChain(state);
ActivateBestChain(true,state);
}
if (!state.IsValid()) {
@@ -1914,7 +1918,7 @@ UniValue reconsiderblock(const UniValue& params, bool fHelp)
}
if (state.IsValid()) {
ActivateBestChain(state);
ActivateBestChain(true,state);
}
if (!state.IsValid()) {