Handle early block prices

This commit is contained in:
jl777
2019-04-03 07:00:43 -11:00
parent 5579964235
commit 9b565ea675

View File

@@ -1188,7 +1188,7 @@ UniValue prices(const UniValue& params, bool fHelp)
if ( fHelp || params.size() != 1 ) if ( fHelp || params.size() != 1 )
throw runtime_error("prices maxsamples\n"); throw runtime_error("prices maxsamples\n");
LOCK(cs_main); LOCK(cs_main);
UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,num,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1;
if ( ASSETCHAINS_CBOPRET == 0 ) if ( ASSETCHAINS_CBOPRET == 0 )
throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices");
@@ -1225,6 +1225,7 @@ UniValue prices(const UniValue& params, bool fHelp)
} else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found");
} }
} }
num = i;
ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i));
UniValue timestamps(UniValue::VARR); UniValue timestamps(UniValue::VARR);
for (i=0; i<maxsamples; i++) for (i=0; i<maxsamples; i++)
@@ -1240,22 +1241,35 @@ UniValue prices(const UniValue& params, bool fHelp)
if ( (str= komodo_pricename(name,j)) != 0 ) if ( (str= komodo_pricename(name,j)) != 0 )
{ {
item.push_back(Pair("name",str)); item.push_back(Pair("name",str));
for (i=0; i<maxsamples+daywindow+smoothwidth; i++) if ( num >= width )
{ {
offset = j*width + i; for (i=0; i<maxsamples+daywindow+smoothwidth&&i<num; i++)
rngval = (rngval*11109 + 13849); {
if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow,prices2,smoothwidth)) < 0 ) offset = j*width + i;
throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); rngval = (rngval*11109 + 13849);
if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow,prices2,smoothwidth)) < 0 )
throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price");
}
for (i=0; i<maxsamples&&i<num; i++)
{
offset = j*width + i;
smoothed = komodo_pricesmoothed(&correlated[i],daywindow,correlated2,smoothwidth);
UniValue parr(UniValue::VARR);
parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j)));
parr.push_back(ValueFromAmount(correlated[i]));
parr.push_back(ValueFromAmount(smoothed));
p.push_back(parr);
}
} }
for (i=0; i<maxsamples; i++) else
{ {
offset = j*width + i; for (i=0; i<maxsamples&&i<num; i++)
smoothed = komodo_pricesmoothed(&correlated[i],daywindow,correlated2,smoothwidth); {
UniValue parr(UniValue::VARR); offset = j*width + i;
parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j))); UniValue parr(UniValue::VARR);
parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j)));
parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr);
p.push_back(parr); }
} }
item.push_back(Pair("prices",p)); item.push_back(Pair("prices",p));
} else item.push_back(Pair("name","error")); } else item.push_back(Pair("name","error"));