From cc5e546304d58bbfd4791b20a1f31ed25d72cc9d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 9 Jul 2019 23:47:29 -1100 Subject: [PATCH 1/4] -print --- src/komodo_nSPV_superlite.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index f8dd7c9af..90bae8bec 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -323,7 +323,7 @@ UniValue NSPV_ntzsproof_json(struct NSPV_ntzsproofresp *ptr) result.push_back(Pair("numhdrs",(int64_t)ptr->common.numhdrs)); result.push_back(Pair("headers",NSPV_headers_json(ptr->common.hdrs,ptr->common.numhdrs,ptr->common.prevht))); result.push_back(Pair("lastpeer",NSPV_lastpeer)); - fprintf(stderr,"ntzs_proof %s %d, %s %d\n",ptr->prevtxid.GetHex().c_str(),ptr->common.prevht,ptr->nexttxid.GetHex().c_str(),ptr->common.nextht); + //fprintf(stderr,"ntzs_proof %s %d, %s %d\n",ptr->prevtxid.GetHex().c_str(),ptr->common.prevht,ptr->nexttxid.GetHex().c_str(),ptr->common.nextht); return(result); } From f7a7b53bf2cb88060e9a5b47ff8616808f68f886 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Jul 2019 00:09:45 -1100 Subject: [PATCH 2/4] Rearrange --- src/komodo_nSPV_superlite.h | 120 +++--------------------------------- src/komodo_nSPV_wallet.h | 107 ++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 111 deletions(-) diff --git a/src/komodo_nSPV_superlite.h b/src/komodo_nSPV_superlite.h index 90bae8bec..8b3d2a762 100644 --- a/src/komodo_nSPV_superlite.h +++ b/src/komodo_nSPV_superlite.h @@ -113,7 +113,7 @@ CNode *NSPV_req(CNode *pnode,uint8_t *msg,int32_t len,uint64_t mask,int32_t ind) if ( pnode == 0 ) { memset(pnodes,0,sizeof(pnodes)); - //LOCK(cs_vNodes); + LOCK(cs_vNodes); n = 0; BOOST_FOREACH(CNode *ptr,vNodes) { @@ -426,6 +426,8 @@ UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag) { UniValue result(UniValue::VOBJ); uint8_t msg[64]; int32_t i,iter,slen,len = 0; //fprintf(stderr,"utxos %s NSPV addr %s\n",coinaddr,NSPV_address.c_str()); + if ( NSPV_utxosresult.nodeheight >= NSPV_inforesult.height && strcmp(coinaddr,NSPV_utxosresult.coinaddr) == 0 && CCflag == NSPV_utxosresult.CCflag ) + return(NSPV_utxosresp_json(&NSPV_utxosresult)); NSPV_utxosresp_purge(&NSPV_utxosresult); if ( bitcoin_base58decode(msg,coinaddr) != 25 ) { @@ -456,8 +458,8 @@ UniValue NSPV_addressutxos(char *coinaddr,int32_t CCflag) UniValue NSPV_notarizations(int32_t height) { uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsresp N; - //if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) - // return(NSPV_ntzs_json(&NSPV_ntzsresult)); + if ( NSPV_ntzsresult.prevntz.height <= height && NSPV_ntzsresult.nextntz.height >= height ) + return(NSPV_ntzs_json(&NSPV_ntzsresult)); NSPV_ntzsresp_purge(&NSPV_ntzsresult); msg[len++] = NSPV_NTZS; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); @@ -478,6 +480,8 @@ UniValue NSPV_notarizations(int32_t height) UniValue NSPV_txidhdrsproof(uint256 prevtxid,uint256 nexttxid) { uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_ntzsproofresp H; + if ( NSPV_ntzsproofresult.prevtxid == prevtxid && NSPV_ntzsproofresult.nexttxid == nexttxid ) + return(NSPV_ntzsproof_json(&NSPV_ntzsproofresult)); NSPV_ntzsproofresp_purge(&NSPV_ntzsproofresult); msg[len++] = NSPV_NTZSPROOF; len += iguana_rwbignum(1,&msg[len],sizeof(prevtxid),(uint8_t *)&prevtxid); @@ -509,8 +513,8 @@ UniValue NSPV_hdrsproof(int32_t prevht,int32_t nextht) UniValue NSPV_txproof(int32_t vout,uint256 txid,int32_t height) { uint8_t msg[64]; int32_t i,iter,len = 0; struct NSPV_txproof P; - //if ( NSPV_txproofresult.txid == txid && NSPV_txproofresult.height == height ) - // return(NSPV_txproof_json(&NSPV_txproofresult)); + if ( NSPV_txproofresult.txid == txid ) + return(NSPV_txproof_json(&NSPV_txproofresult)); NSPV_txproof_purge(&NSPV_txproofresult); msg[len++] = NSPV_TXPROOF; len += iguana_rwnum(1,&msg[len],sizeof(height),&height); @@ -589,111 +593,5 @@ UniValue NSPV_broadcast(char *hex) return(NSPV_broadcast_json(&B,txid)); } -int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) -{ - int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; - if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) - { - fprintf(stderr,"next.%d prev.%d -> %d vs %d\n",ptr->common.nextht,ptr->common.prevht,ptr->common.nextht-ptr->common.prevht+1,ptr->common.numhdrs); - return(-2); - } - else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) - return(-3); - else if ( tx.GetHash() != ptr->nexttxid ) - return(-4); - else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) - return(-5); - else if ( height != ptr->common.nextht ) - return(-6); - else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) - return(-7); - for (i=ptr->common.numhdrs-1; i>0; i--) - { - blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); - if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) - return(-i-13); - } - if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-8); - else if ( tx.GetHash() != ptr->prevtxid ) - return(-9); - else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) - return(-10); - else if ( height != ptr->common.prevht ) - return(-11); - else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) - return(-12); - return(0); -} - -int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx,int64_t extradata,uint32_t tiptime,int64_t &rewardsum) -{ - int32_t i,offset,retval = 0; int64_t rewards = 0; uint32_t nLockTime; std::vector proof; - for (i=0; i<3; i++) - { - NSPV_txproof(vout,txid,height); - if ( NSPV_txproofresult.txlen != 0 ) - break; - sleep(1); - } - if ( NSPV_txproofresult.txid != txid ) - { - fprintf(stderr,"txproof error %s != %s\n",NSPV_txproofresult.txid.GetHex().c_str(),txid.GetHex().c_str()); - return(-1); - } - else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) - retval = -2000; - else if ( skipvalidation == 0 && NSPV_txproofresult.unspentvalue <= 0 ) - retval = -2001; - else if ( ASSETCHAINS_SYMBOL[0] == 0 && extradata >= 0 && tiptime != 0 ) - { - rewards = komodo_interestnew(height,tx.vout[vout].nValue,tx.nLockTime,tiptime); - if ( rewards != extradata ) - { - fprintf(stderr,"extradata %.8f vs rewards %.8f\n",dstr(extradata),dstr(rewards)); - } - rewardsum += rewards; - } - - if ( skipvalidation == 0 ) - { - if ( NSPV_txproofresult.txprooflen > 0 ) - { - proof.resize(NSPV_txproofresult.txprooflen); - memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); - } - fprintf(stderr,"call NSPV_notarizations\n"); - NSPV_notarizations(height); // gets the prev and next notarizations - if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) - { - fprintf(stderr,"issue manual bracket\n"); - NSPV_notarizations(height-1); - NSPV_notarizations(height+1); - NSPV_notarizations(height); // gets the prev and next notarizations - } - if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) - { - fprintf(stderr,">>>>> gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); - offset = (height - NSPV_ntzsresult.prevntz.height); - if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) - { - fprintf(stderr,"call NSPV_txidhdrsproof %s %s\n",NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.txid.GetHex().c_str()); - NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); - usleep(10000); - if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) - { - std::vector txids; uint256 proofroot; - proofroot = BitcoinGetProofMerkleRoot(proof,txids); - if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) - { - fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); - retval = -2003; - } - } - } else retval = -2002; - } else retval = -2004; - } - return(retval); -} #endif // KOMODO_NSPVSUPERLITE_H diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index afe162880..cffdc8381 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,6 +20,113 @@ // nSPV wallet uses superlite functions (and some komodod built in functions) to implement nSPV_spend extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); +int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) +{ + int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) + { + fprintf(stderr,"next.%d prev.%d -> %d vs %d\n",ptr->common.nextht,ptr->common.prevht,ptr->common.nextht-ptr->common.prevht+1,ptr->common.numhdrs); + return(-2); + } + else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) + return(-3); + else if ( tx.GetHash() != ptr->nexttxid ) + return(-4); + else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) + return(-5); + else if ( height != ptr->common.nextht ) + return(-6); + else if ( NSPV_hdrhash(&ptr->common.hdrs[ptr->common.numhdrs-1]) != blockhash ) + return(-7); + for (i=ptr->common.numhdrs-1; i>0; i--) + { + blockhash = NSPV_hdrhash(&ptr->common.hdrs[i-1]); + if ( blockhash != ptr->common.hdrs[i].hashPrevBlock ) + return(-i-13); + } + if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) + return(-8); + else if ( tx.GetHash() != ptr->prevtxid ) + return(-9); + else if ( NSPV_notarizationextract(1,&height,&blockhash,&desttxid,tx) < 0 ) + return(-10); + else if ( height != ptr->common.prevht ) + return(-11); + else if ( NSPV_hdrhash(&ptr->common.hdrs[0]) != blockhash ) + return(-12); + return(0); +} + +int32_t NSPV_gettransaction(int32_t skipvalidation,int32_t vout,uint256 txid,int32_t height,CTransaction &tx,int64_t extradata,uint32_t tiptime,int64_t &rewardsum) +{ + int32_t i,offset,retval = 0; int64_t rewards = 0; uint32_t nLockTime; std::vector proof; + for (i=0; i<3; i++) + { + NSPV_txproof(vout,txid,height); + if ( NSPV_txproofresult.txlen != 0 ) + break; + sleep(1); + } + if ( NSPV_txproofresult.txid != txid ) + { + fprintf(stderr,"txproof error %s != %s\n",NSPV_txproofresult.txid.GetHex().c_str(),txid.GetHex().c_str()); + return(-1); + } + else if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 || NSPV_txproofresult.txlen <= 0 ) + retval = -2000; + else if ( skipvalidation == 0 && NSPV_txproofresult.unspentvalue <= 0 ) + retval = -2001; + else if ( ASSETCHAINS_SYMBOL[0] == 0 && extradata >= 0 && tiptime != 0 ) + { + rewards = komodo_interestnew(height,tx.vout[vout].nValue,tx.nLockTime,tiptime); + if ( rewards != extradata ) + { + fprintf(stderr,"extradata %.8f vs rewards %.8f\n",dstr(extradata),dstr(rewards)); + } + rewardsum += rewards; + } + + if ( skipvalidation == 0 ) + { + if ( NSPV_txproofresult.txprooflen > 0 ) + { + proof.resize(NSPV_txproofresult.txprooflen); + memcpy(&proof[0],NSPV_txproofresult.txproof,NSPV_txproofresult.txprooflen); + } + fprintf(stderr,"call NSPV_notarizations\n"); + NSPV_notarizations(height); // gets the prev and next notarizations + if ( NSPV_inforesult.notarization.height >= height && (NSPV_ntzsresult.prevntz.height == 0 || NSPV_ntzsresult.prevntz.height >= NSPV_ntzsresult.nextntz.height) ) + { + fprintf(stderr,"issue manual bracket\n"); + NSPV_notarizations(height-1); + NSPV_notarizations(height+1); + NSPV_notarizations(height); // gets the prev and next notarizations + } + if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) + { + fprintf(stderr,">>>>> gettx ht.%d prev.%d next.%d\n",height,NSPV_ntzsresult.prevntz.height, NSPV_ntzsresult.nextntz.height); + offset = (height - NSPV_ntzsresult.prevntz.height); + if ( offset >= 0 && height <= NSPV_ntzsresult.nextntz.height ) + { + fprintf(stderr,"call NSPV_txidhdrsproof %s %s\n",NSPV_ntzsresult.prevntz.txid.GetHex().c_str(),NSPV_ntzsresult.nextntz.txid.GetHex().c_str()); + NSPV_txidhdrsproof(NSPV_ntzsresult.prevntz.txid,NSPV_ntzsresult.nextntz.txid); + usleep(10000); + if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) + { + std::vector txids; uint256 proofroot; + proofroot = BitcoinGetProofMerkleRoot(proof,txids); + if ( proofroot != NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot ) + { + fprintf(stderr,"prooflen.%d proofroot.%s vs %s\n",NSPV_txproofresult.txprooflen,proofroot.GetHex().c_str(),NSPV_ntzsproofresult.common.hdrs[offset].hashMerkleRoot.GetHex().c_str()); + retval = -2003; + } + } + } else retval = -2002; + } else retval = -2004; + } + return(retval); +} + int32_t NSPV_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct NSPV_utxoresp utxos[],int32_t numunspents,int64_t value) { int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; From 982357098aa3283a632e7075d8861ef450606d8c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Jul 2019 00:12:00 -1100 Subject: [PATCH 3/4] Always add rewards validation for KMD --- src/komodo_nSPV_wallet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index cffdc8381..ea25b8983 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -373,7 +373,7 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a return(result); } hex = NSPV_signtx(rewardsum,interestsum,retcodes,mtx,txfee,opret,used); - if ( interestsum != 0 || rewardsum != 0 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 ) { result.push_back(Pair("rewards",dstr(interestsum))); result.push_back(Pair("validated",dstr(rewardsum))); From 879f38c131a9fc1e375d2370e548e064c2550f89 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 10 Jul 2019 00:14:25 -1100 Subject: [PATCH 4/4] AmountFromValue --- src/komodo_nSPV_wallet.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index ea25b8983..f43c2b424 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -375,8 +375,8 @@ UniValue NSPV_spend(char *srcaddr,char *destaddr,int64_t satoshis) // what its a hex = NSPV_signtx(rewardsum,interestsum,retcodes,mtx,txfee,opret,used); if ( ASSETCHAINS_SYMBOL[0] == 0 ) { - result.push_back(Pair("rewards",dstr(interestsum))); - result.push_back(Pair("validated",dstr(rewardsum))); + result.push_back(Pair("rewards",AmountFromValue(interestsum))); + result.push_back(Pair("validated",AmountFromValue(rewardsum))); } if ( hex.size() > 0 ) {