From 1e9d15c61c960f18b6ae9705c563c8e31e2ad128 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 31 Oct 2016 09:59:17 -0300 Subject: [PATCH] test --- src/komodo_notary.h | 63 ++++++++++++++++++++++++++++++------------- src/rpcblockchain.cpp | 44 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/komodo_notary.h b/src/komodo_notary.h index 0e5447087..d30942f0b 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -13,7 +13,7 @@ * * ******************************************************************************/ -const char *Notaries[][2] = +const char *Notaries_genesis[][2] = { { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, { "jl777_testB", "02ebfc784a4ba768aad88d44d1045d240d47b26e248cafaf1c5169a42d7a61d344" }, @@ -60,18 +60,6 @@ struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; struct knotaries_entry { int32_t height,numnotaries; struct knotary_entry *Notaries; } Pubkeys[10000]; struct notarized_checkpoint { uint256 notarized_hash,notarized_desttxid; int32_t nHeight,notarized_height; } *NPOINTS; int32_t NUM_NPOINTS; -int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) -{ - int32_t numnotaries,i,wt = 0; - numnotaries = Pubkeys[height / KOMODO_ELECTION_GAP].numnotaries; - for (i=0; i (numnotaries >> 1) || (wt > 7 && (signedmask & 3) != 0) ) - return(1); // N/2+1 || N/3 + devsig - else return(0); -} - void komodo_nutxoadd(int32_t height,int32_t notaryid,uint256 txhash,uint64_t voutmask,int32_t numvouts) { struct nutxo_entry *np; @@ -84,7 +72,7 @@ void komodo_nutxoadd(int32_t height,int32_t notaryid,uint256 txhash,uint64_t vou np->voutmask = voutmask; np->notaryid = notaryid; HASH_ADD_KEYPTR(hh,NUTXOS,&np->txhash,sizeof(np->txhash),np); - printf("Add NUTXO[%d] <- %s notaryid.%d t%u %s %llx\n",Num_nutxos,Notaries[notaryid][0],notaryid,komodo_txtime(txhash),txhash.ToString().c_str(),(long long)voutmask); + //printf("Add NUTXO[%d] <- %s notaryid.%d t%u %s %llx\n",Num_nutxos,Notaries[notaryid][0],notaryid,komodo_txtime(txhash),txhash.ToString().c_str(),(long long)voutmask); Num_nutxos++; pthread_mutex_unlock(&komodo_mutex); } @@ -101,6 +89,42 @@ int32_t komodo_nutxofind(int32_t height,uint256 txhash,int32_t vout) return(-1); } +int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) +{ + int32_t htind,numnotaries,i,wt = 0; + if ( (htind= KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP) == 1 ) + htind = 0; + numnotaries = Pubkeys[htind].numnotaries; + for (i=0; i (numnotaries >> 1) || (wt > 7 && (signedmask & 3) != 0) ) + return(1); + else return(0); +} + +int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height) +{ + int32_t i,htind,n; uint64_t mask = 0; struct knotary_entry *kp,*tmp; + if ( (htind= KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP) == 1 ) + htind = 0; + pthread_mutex_lock(&komodo_mutex); + n = Pubkeys[htind].num; + HASH_ITER(hh,Pubkeys[htind].Notaries,kp,tmp) + { + if ( kp->notaryid < n ) + { + mask |= (1LL << kp->notaryid); + memcpy(pubkeys[kp->notaryid],kp->pubkey,33); + } + } + pthread_mutex_unlock(&komodo_mutex); + if ( mask == ((1LL << n)-1) ) + return(n); + printf("error retrieving notaries ht.%d got mask.%llx for n.%d\n",height,(long long)mask,n); + return(-1); +} + void komodo_notarysinit(int32_t height,uint8_t pubkeys[64][33],int32_t num) { int32_t k,i,htind; struct knotary_entry *kp; struct knotaries_entry N; @@ -120,8 +144,7 @@ void komodo_notarysinit(int32_t height,uint8_t pubkeys[64][33],int32_t num) } } N.numnotaries = num; - htind = KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP; - if ( htind == 1 ) + if ( (htind= KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP) == 1 ) htind = 0; for (i=htind; i= sizeof(Pubkeys)/sizeof(*Pubkeys) ) return(-1); + if ( (htind= KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP) == 1 ) + htind = 0; pthread_mutex_lock(&komodo_mutex); - HASH_FIND(hh,Pubkeys[height/KOMODO_ELECTION_GAP].Notaries,pubkey33,33,kp); + HASH_FIND(hh,Pubkeys[htind].Notaries,pubkey33,33,kp); pthread_mutex_unlock(&komodo_mutex); if ( kp != 0 ) { - if ( (numnotaries= Pubkeys[height/KOMODO_ELECTION_GAP].numnotaries) > 0 ) + if ( (numnotaries= Pubkeys[htind].numnotaries) > 0 ) { *notaryidp = kp->notaryid; modval = ((height % numnotaries) == kp->notaryid); diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 607500c70..48a175401 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -384,6 +384,50 @@ uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uin uint32_t komodo_txtime(uint256 hash); uint64_t komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolume); int32_t komodo_paxprices(int32_t *heights,uint64_t *prices,int32_t max,char *base,char *rel); +int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); +char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); + +Value notaries(const Array& params, bool fHelp) +{ + if ( fHelp || params.size() != 1 ) + throw runtime_error("notaries height\n"); + LOCK(cs_main); + int32_t n,height = atoi(params[0].get_str().c_str()); + if ( height < 0 || height > chainActive.Height() ) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); + else + { + Array a; Object item; int32_t i,j,n,m; char *hexstr; std::string hex,addr; uint8_t pubkeys[64][33]; char btcaddr[64],kmdaddr[64],*ptr; + hex.resize(67); + if ( (n= komodo_notaries(pubkeys,height)) > 0 ) + { + for (i=0; i