diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 18f85a036..31756881f 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -374,6 +374,8 @@ void NSPV_broadcast_purge(struct NSPV_broadcastresp *ptr) memset(ptr,0,sizeof(*ptr)); } +// useful utility functions + uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) { bits256 _hash; uint256 hash; int32_t i; @@ -383,4 +385,29 @@ uint256 NSPV_doublesha256(uint8_t *data,int32_t datalen) return(hash); } +int32_t NSPV_txextract(CTransaction &tx,uint8_t *data,int32_t datalen) +{ + std::vector rawdata; + rawdata.resize(datalen); + memcpy(&rawdata[0],data,datalen); + if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) + return(0); + else return(-1); +} + +uint256 NSPV_opretextract(int32_t *heightp,uint256 *blockhashp,char *symbol,std::vector opret) +{ + uint256 desttxid; int32_t i; + iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); + for (i=0; i<32; i++) + ((uint8_t *)blockhashp)[i] = opret[4 + i]; + for (i=0; i<32; i++) + ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; + return(desttxid); +} + +int32_t NSPV_notarizationextract(int32_t *heightp,uint256 *blockhashp,uint256 *txidp,int32_t *txidhtp,uint256 *desttxidp,CTransaction tx) +{ + return(0); +} #endif // KOMODO_NSPV_H diff --git a/src/komodo_nSPV_fullnode.h b/src/komodo_nSPV_fullnode.h index b93e299f1..fb45bd4f5 100644 --- a/src/komodo_nSPV_fullnode.h +++ b/src/komodo_nSPV_fullnode.h @@ -35,27 +35,9 @@ uint256 NSPV_getnotarization_txid(int32_t *ntzheightp,int32_t height) return(txid); } -uint256 NSPV_extract_desttxid(int32_t *heightp,char *symbol,std::vector opret) -{ - uint256 desttxid; int32_t i; - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[i]); - //fprintf(stderr," blockhash, "); - //for (i=0; i<4; i++) - // fprintf(stderr,"%02x",opret[32+i]); - //fprintf(stderr," height, "); - iguana_rwnum(0,&opret[32],sizeof(*heightp),heightp); - //for (i=0; i<32; i++) - // fprintf(stderr,"%02x",opret[36+i]); - //fprintf(stderr," desttxid\n"); - for (i=0; i<32; i++) - ((uint8_t *)&desttxid)[i] = opret[4 + 32 + i]; - return(desttxid); -} - int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 desttxids[2],int32_t ntzheights[2],int32_t height) { - int32_t txidht; Notarisation nota; char *symbol; + int32_t txidht; Notarisation nota; char *symbol; uint256 bhash; symbol = (ASSETCHAINS_SYMBOL[0] == 0) ? (char *)"KMD" : ASSETCHAINS_SYMBOL; memset(txids,0,sizeof(*txids)*2); memset(desttxids,0,sizeof(*desttxids)*2); @@ -65,7 +47,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des return(-1); txids[0] = nota.first; txidhts[0] = txidht; - desttxids[0] = NSPV_extract_desttxid(&ntzheights[0],symbol,E_MARSHAL(ss << nota.second)); + desttxids[0] = NSPV_opretextract(&ntzheights[0],&bhash,symbol,E_MARSHAL(ss << nota.second)); //if ( height != 2668 ) // fprintf(stderr,"scan.%d -> %s txidht.%d ntzht.%d\n",height,desttxids[0].GetHex().c_str(),txidht,ntzheights[0]); if ( ntzheights[0] == height-1 ) // offset the +1 from caller @@ -80,7 +62,7 @@ int32_t komodo_notarized_bracket(uint256 txids[2],int32_t txidhts[2],uint256 des { txids[1] = nota.first; txidhts[1] = txidht; - desttxids[1] = NSPV_extract_desttxid(&ntzheights[1],symbol,E_MARSHAL(ss << nota.second)); + desttxids[1] = NSPV_opretextract(&ntzheights[1],&bhash,symbol,E_MARSHAL(ss << nota.second)); } return(0); } @@ -196,12 +178,9 @@ uint8_t *NSPV_getrawtx(uint256 &hashBlock,uint16_t *txlenp,uint256 txid) int32_t NSPV_sendrawtransaction(struct NSPV_broadcastresp *ptr,uint8_t *data,int32_t n) { - CTransaction tx; std::vector rawdata; + CTransaction tx; ptr->retcode = 0; - rawdata.resize(n); - memcpy(&rawdata[0],data,n); - fprintf(stderr,"sendraw.(%s)\n",HexStr(rawdata).c_str()); - if ( DecodeHexTx(tx,HexStr(rawdata)) != 0 ) + if ( NSPV_txextract(tx,data,n) == 0 ) { LOCK(cs_main); ptr->txid = tx.GetHash(); diff --git a/src/komodo_nSPV_wallet.h b/src/komodo_nSPV_wallet.h index 5c34bcb14..ac3b22963 100644 --- a/src/komodo_nSPV_wallet.h +++ b/src/komodo_nSPV_wallet.h @@ -20,70 +20,61 @@ // 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); - - -/*struct NSPV_ntzproofshared -{ - struct NSPV_equihdr *hdrs; - int32_t prevht,nextht,pad32; - uint16_t numhdrs,pad16; -}; - -struct NSPV_ntzsproofresp -{ - struct NSPV_ntzproofshared common; - uint256 prevtxid,nexttxid; - int32_t pad32,prevtxidht,nexttxidht; - uint16_t prevtxlen,nexttxlen; - uint8_t *prevntz,*nextntz; -};*/ - int32_t NSPV_validatehdrs(struct NSPV_ntzsproofresp *ptr) { - /* int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; - if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) + int32_t i,height,txidht; CTransaction tx; uint256 blockhash,txid,desttxid; + if ( (ptr->common.nextht-ptr->common.prevht+1) != ptr->common.numhdrs ) return(-1); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) + else if ( NSPV_txextract(tx,ptr->nextntz,ptr->nexttxlen) < 0 ) return(-2); - else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + else if ( tx.GetHash() != ptr->nexttxid ) return(-3); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + return(-4); + else if ( height != ptr->common.nextht ) + return(-5); + else if ( NSPV_doublesha256(&ptr->common.hdrs[ptr->common.numhdrs-1],sizeof(*ptr->common.hdrs)) != blockhash ) + return(-6); for (i=ptr->common.numhdrs-1; i>0; i--) { - // make sure the hash of i-1 matches the prevBlockhash of i + if ( NSPV_doublesha256(&ptr->common.hdrs[i-1],sizeof(*ptr->common.hdrs)) != ptr->common.hdrs.prevblockhash ) + return(-i-11); } - // verify prevntz is valid notarization - // verify blockhash of first hdr with prevntz value and height if ( NSPV_txextract(tx,ptr->prevntz,ptr->prevtxlen) < 0 ) - return(-1); - else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid) < 0 ) - return(-2);*/ + return(-6); + else if ( tx.GetHash() != ptr->prevtxid ) + return(-7); + else if ( NSPV_notarizationextract(&height,&blockhash,&txid,&txidht,&desttxid,tx) < 0 ) + return(-8); + else if ( height != ptr->common.prevht ) + return(-9); + else if ( NSPV_doublesha256(&ptr->common.hdrs[0],sizeof(*ptr->common.hdrs)) != blockhash ) + return(-10); return(0); } int32_t NSPV_gettransaction(uint256 txid,int32_t height,CTransaction &tx) { - char *txstr; int32_t retval = 0; + int32_t retval = 0; NSPV_txproof(txid,height); - if ( NSPV_txproofresult.txid != txid || NSPV_txproofresult.height != height ) + if ( NSPV_txproofresult.txid != txid ) return(-1); - txstr = (char *)malloc(NSPV_txproofresult.txlen*2 + 1); - init_hexbytes_noT(txstr,NSPV_txproofresult.tx,NSPV_txproofresult.txlen); - if ( !DecodeHexTx(tx,txstr) ) - retval = -1; + if ( NSPV_txextract(tx,NSPV_txproofresult.tx,NSPV_txproofresult.txlen) < 0 ) + retval = -20; else { NSPV_notarizations(height); // gets the prev and next notarizations if ( NSPV_ntzsresult.prevntz.height != 0 && NSPV_ntzsresult.prevntz.height <= NSPV_ntzsresult.nextntz.height ) { NSPV_hdrsproof(NSPV_ntzsresult.prevntz.height,NSPV_ntzsresult.nextntz.height); // validate the segment - if ( NSPV_validatehdrs(&NSPV_ntzsproofresult) == 0 ) + if ( (retval= NSPV_validatehdrs(&NSPV_ntzsproofresult)) == 0 ) { // merkle prove txproof to the merkleroot in the corresponding hdr } } else retval = -1; } - free(txstr); - return(retval); + fprintf(stderr,"NSPV_gettransaction retval would have been %d\n",retval); + return(0); } 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)