From fb9c36521831a04ae94689923cab752c3d2f598a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 12 Apr 2018 11:44:28 +0300 Subject: [PATCH] Calculate MoMoM --- src/komodo.h | 6 +-- src/komodo_ccdata.h | 108 ++++++++++++++++++++++++++++++++++++++---- src/komodo_globals.h | 2 +- src/komodo_structs.h | 5 +- src/rpcblockchain.cpp | 13 +++++ src/rpcclient.cpp | 1 + src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + 8 files changed, 122 insertions(+), 15 deletions(-) diff --git a/src/komodo.h b/src/komodo.h index 059fb3da6..a261cf0db 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -357,6 +357,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar if ( didinit == 0 ) { portable_mutex_init(&KOMODO_KV_mutex); + portable_mutex_init(&KOMODO_CC_mutex); didinit = 1; } if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) @@ -640,14 +641,13 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr if ( ASSETCHAINS_SYMBOL[0] != 0 ) { // MoMoM, depth, numpairs, (notarization ht, MoMoM offset) - if ( len+48-opoffset <= opretlen && strcmp(ccdata.symbol,ASSETCHAINS_SYMBOL) == 0 ) + if ( len+44-opoffset <= opretlen && strcmp(ccdata.symbol,ASSETCHAINS_SYMBOL) == 0 ) { len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMstart); len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMend); len += iguana_rwbignum(0,&scriptbuf[len],sizeof(MoMoMdata.MoMoM),(uint8_t *)&MoMoMdata.MoMoM); - len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMdepth); len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.numpairs); - MoMoMdata.len += sizeof(MoMoMdata.MoMoM) + sizeof(uint32_t)*4; + MoMoMdata.len += sizeof(MoMoMdata.MoMoM) + sizeof(uint32_t)*3; if ( len+MoMoMdata.numpairs*8-opoffset == opretlen ) { MoMoMdata.pairs = (struct komodo_ccdatapair *)calloc(MoMoMdata.numpairs,sizeof(*MoMoMdata.pairs)); diff --git a/src/komodo_ccdata.h b/src/komodo_ccdata.h index 532569c6b..fc746bcf2 100644 --- a/src/komodo_ccdata.h +++ b/src/komodo_ccdata.h @@ -16,21 +16,109 @@ #ifndef H_KOMODOCCDATA_H #define H_KOMODOCCDATA_H +/* +struct komodo_ccdataMoM +{ + uint256 MoM; + int32_t MoMdepth,notarized_height,height,txi; +}; + +struct komodo_ccdatapair { int32_t notarization_height; uint32_t MoMoMoffset; }; + +struct komodo_ccdataMoMoM +{ + uint256 MoMoM; + int32_t MoMoMstarti,MoMoMendi,numpairs,len; + struct komodo_ccdatapair *pairs; +}; -/*struct komodo_ccdatapair { int32_t notarization_height; uint32_t MoMoMoffset; }; struct komodo_ccdata { - uint32_t CCid; - uint256 MoMoM,MoM; - int32_t MoMoMstart,MoMoMend,MoMoMdepth,numpairs,notarized_height,height,txi,len,MoMdepth; - struct komodo_ccdatapair *pairs; + struct komodo_ccdataMoM MoMdata; + uint32_t CCid,len,inMoMoM; char symbol[65]; }; */ +struct komodo_ccdata *CC_data; + +bits256 iguana_merkle(bits256 *tree,int32_t txn_count) +{ + int32_t i,n=0,prev; uint8_t serialized[sizeof(bits256) * 2]; + if ( txn_count == 1 ) + return(tree[0]); + prev = 0; + while ( txn_count > 1 ) + { + if ( (txn_count & 1) != 0 ) + tree[prev + txn_count] = tree[prev + txn_count-1], txn_count++; + n += txn_count; + for (i=0; i> 1)] = bits256_doublesha256(0,serialized,sizeof(serialized)); + } + prev = n; + txn_count >>= 1; + } + return(tree[n]); +} + +char *komodo_MoMoMdata(char *symbol,int32_t kmdheight,int32_t notarized_height) +{ + cJSON *retjson,*pairs,*item; struct komodo_ccdata *ccdata,*tmp; int32_t max,offset,starti,endi,kmdstarti=0; bits256 *tree,MoMoM; + starti = endi = offset = max = 0; + pairs = cJSON_CreateArray(); + portable_mutex_lock(&KOMODO_CC_mutex); + DL_FOREACH_SAFE(CC_data,ccdata,tmp) + { + if ( ccdata->MoMdata.height < kmdheight ) + { + if ( endi == 0 ) + endi = ccdata->MoMdata.height; + if ( strcmp(ccdata->symbol,symbol) == 0 ) + { + if (ccdata->MoMdata.notarized_height <= notarized_height ) + { + starti = ccdata->MoMdata.height + 1; + break; + } + item = cJSON_CreateArray(); + jaddinum(item,ccdata->MoMdata.notarized_height); + jaddinum(item,offset); + jaddi(pairs,item); + } + if ( offset >= max ) + { + max += 100; + tree = realloc(sizeof(*tree),max); + } + memcpy(&tree[offset++],&ccdata->MoMdata.MoM,sizeof(bits256)); + starti = ccdata->MoMdata.height; + } + } + portable_mutex_unlock(&KOMODO_CC_mutex); + retjson = cJSON_CreateObject(); + jaddnum(retjson,"kmdstarti",starti); + jaddnum(retjson,"kmdendi",endi); + if ( starti != 0 && endi != 0 && endi >= starti ) + { + if ( tree != 0 && offset > 0 ) + { + MoMoM = iguana_merkle(tree,offset); + jaddbits256(retjson,"MoMoM",MoMoM); + } + } + if ( tree != 0 ) + free(tree); + jadd(retjson,"offsets",pairs); + return(jprint(retjson,1)); +} + int32_t komodo_rwccdata(char *thischain,int32_t rwflag,struct komodo_ccdata *ccdata,struct komodo_ccdataMoMoM *MoMoMdata) { - bits256 hash; int32_t i; + bits256 hash; int32_t i; struct komodo_ccdata *ptr; if ( rwflag == 0 ) { @@ -40,9 +128,11 @@ int32_t komodo_rwccdata(char *thischain,int32_t rwflag,struct komodo_ccdata *ccd char str[65]; fprintf(stderr,"[%s] ccdata.%s id.%d notarized_ht.%d MoM.%s height.%d/t%d\n",ASSETCHAINS_SYMBOL,ccdata->symbol,ccdata->CCid,ccdata->MoMdata.notarized_height,bits256_str(str,hash),ccdata->MoMdata.height,ccdata->MoMdata.txi); if ( ASSETCHAINS_SYMBOL[0] == 0 ) { - // find/create entry for CCid - // if KMD, for all CCids, get range and calc MoMoM for RPC retrieval - // given MoM height for CCid chain, find the offset and MoMoM + ptr = calloc(1,sizeof(*ptr)); + *ptr = *ccdata; + portable_mutex_lock(&KOMODO_CC_mutex); + DL_PREPEND(CC_data,ptr); + portable_mutex_unlock(&KOMODO_CC_mutex); } else { diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 1f4872d51..4d9d53f07 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -62,4 +62,4 @@ char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t KMD_PORT = 7771,BITCOIND_PORT uint64_t PENDING_KOMODO_TX; struct komodo_kv *KOMODO_KV; -pthread_mutex_t KOMODO_KV_mutex; +pthread_mutex_t KOMODO_KV_mutex,KOMODO_CC_mutex; diff --git a/src/komodo_structs.h b/src/komodo_structs.h index 6527bebb7..caf6aeae1 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -100,14 +100,15 @@ struct komodo_ccdatapair { int32_t notarization_height; uint32_t MoMoMoffset; }; struct komodo_ccdataMoMoM { uint256 MoMoM; - int32_t MoMoMstart,MoMoMend,MoMoMdepth,numpairs,len; + int32_t MoMoMstarti,MoMoMendi,numpairs,len; struct komodo_ccdatapair *pairs; }; struct komodo_ccdata { + struct komodo_ccdata *next,*prev; struct komodo_ccdataMoM MoMdata; - uint32_t CCid,len; + uint32_t CCid,inMoMoM,len; char symbol[65]; }; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 010fb051c..5ddeaabf5 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -553,6 +553,7 @@ char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160, int32_t komodo_minerids(uint8_t *minerids,int32_t height,int32_t width); int32_t komodo_kvsearch(uint256 *refpubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); int32_t komodo_MoM(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight); +char *komodo_MoMoMdata(char *symbol,int32_t kmdheight,int32_t notarized_height); UniValue kvsearch(const UniValue& params, bool fHelp) { @@ -589,6 +590,18 @@ UniValue kvsearch(const UniValue& params, bool fHelp) return ret; } +UniValue MoMoMdata(const UniValue& params, bool fHelp) +{ + char *symbol; int32_t kmdheight; UniValue ret(UniValue::VOBJ); UniValue a(UniValue::VARR); + if ( fHelp || params.size() != 3 ) + throw runtime_error("height_MoM needs symbol kmdheight notarized_height\n"); + LOCK(cs_main); + symbol = (char *)params[0].get_str().c_str(); + kmdheight = atoi(params[1].get_str().c_str()); + notarized_height = atoi(params[2].get_str().c_str()); + return((UniValue)komodo_MoMoMdata(symbol,kmdheight,notarized_height)); +} + UniValue height_MoM(const UniValue& params, bool fHelp) { int32_t height,depth,notarized_height; uint256 MoM,kmdtxid; uint32_t timestamp = 0; UniValue ret(UniValue::VOBJ); UniValue a(UniValue::VARR); diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 6a83e8c19..475a8bf69 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -126,6 +126,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "paxpending", 0 }, { "notaries", 2 }, { "height_MoM", 1 }, + { "MoMoMdata", 3 }, { "txMoMproof", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 5d573f62b..dffe2d4d9 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -296,6 +296,7 @@ static const CRPCCommand vRPCCommands[] = { "blockchain", "paxpending", &paxpending, true }, { "blockchain", "paxprices", &paxprices, true }, { "blockchain", "notaries", ¬aries, true }, + { "blockchain", "MoMoMdata", &MoMoMdata, true }, { "blockchain", "height_MoM", &height_MoM, true }, { "blockchain", "txMoMproof", &txMoMproof, true }, { "blockchain", "minerids", &minerids, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index b4e6ed8fc..f3e0d5d89 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -304,6 +304,7 @@ extern UniValue z_validateaddress(const UniValue& params, bool fHelp); // in rpc extern UniValue z_getpaymentdisclosure(const UniValue& params, bool fHelp); // in rpcdisclosure.cpp extern UniValue z_validatepaymentdisclosure(const UniValue ¶ms, bool fHelp); // in rpcdisclosure.cpp +extern UniValue MoMoMdata(const UniValue& params, bool fHelp); extern UniValue height_MoM(const UniValue& params, bool fHelp); extern UniValue txMoMproof(const UniValue& params, bool fHelp); extern UniValue notaries(const UniValue& params, bool fHelp);