Merge pull request #1603 from jl777/jl777

Jl777
This commit is contained in:
jl777
2019-07-10 00:15:21 -11:00
committed by GitHub
2 changed files with 120 additions and 115 deletions

View File

@@ -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)
{
@@ -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);
}
@@ -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<uint8_t> 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<uint256> 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

View File

@@ -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<uint8_t> 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<uint256> 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;
@@ -266,10 +373,10 @@ 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)));
result.push_back(Pair("rewards",AmountFromValue(interestsum)));
result.push_back(Pair("validated",AmountFromValue(rewardsum)));
}
if ( hex.size() > 0 )
{