Delete more CCs #381
This commit is contained in:
@@ -159,7 +159,7 @@ int32_t NSPV_getaddressutxos(struct NSPV_utxosresp *ptr,char *coinaddr,bool isCC
|
||||
{
|
||||
int64_t total = 0,interest=0; uint32_t locktime; int32_t ind=0,tipheight,maxlen,txheight,n = 0,len = 0;
|
||||
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
|
||||
SetCCunspents(unspentOutputs,coinaddr,isCC);
|
||||
//SetCCunspents(unspentOutputs,coinaddr,isCC);
|
||||
maxlen = MAX_BLOCK_SIZE(tipheight) - 512;
|
||||
maxlen /= sizeof(*ptr->utxos);
|
||||
strncpy(ptr->coinaddr,coinaddr,sizeof(ptr->coinaddr)-1);
|
||||
@@ -303,141 +303,11 @@ static std::map<uint8_t, class BaseCCChecker*> ccCheckerTable =
|
||||
{
|
||||
};
|
||||
|
||||
// implements SPV server's part, gets cc module utxos, filtered by evalcode, funcid and txid on opret, for the specified amount
|
||||
// if the amount param is 0 returns total available filtere utxo amount and returns no utxos
|
||||
// first char funcid in the string param is considered as the creation tx funcid so filtertxid is compared to the creation txid itself
|
||||
// for other funcids filtertxid is compared to the txid in opreturn
|
||||
int32_t NSPV_getccmoduleutxos(struct NSPV_utxosresp *ptr, char *coinaddr, int64_t amount, uint8_t evalcode, std::string funcids, uint256 filtertxid)
|
||||
{
|
||||
int64_t total = 0, totaladded = 0;
|
||||
uint32_t locktime;
|
||||
int32_t tipheight=0, len, maxlen;
|
||||
int32_t maxinputs = CC_MAXVINS;
|
||||
|
||||
std::vector<struct CC_utxo> utxoSelected;
|
||||
utxoSelected.reserve(CC_MAXVINS);
|
||||
|
||||
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
|
||||
SetCCunspents(unspentOutputs, coinaddr, true);
|
||||
|
||||
maxlen = MAX_BLOCK_SIZE(tipheight) - 512;
|
||||
//maxlen /= sizeof(*ptr->utxos); // TODO why was this? we need maxlen in bytes, don't we?
|
||||
|
||||
//ptr->numutxos = (uint16_t)unspentOutputs.size();
|
||||
//if (ptr->numutxos >= 0 && ptr->numutxos < maxlen)
|
||||
//{
|
||||
ptr->utxos = NULL;
|
||||
ptr->numutxos = 0;
|
||||
strncpy(ptr->coinaddr, coinaddr, sizeof(ptr->coinaddr) - 1);
|
||||
ptr->CCflag = 1;
|
||||
tipheight = chainActive.LastTip()->GetHeight();
|
||||
ptr->nodeheight = tipheight; // will be checked in libnspv
|
||||
//}
|
||||
|
||||
// select all appropriate utxos:
|
||||
std::cerr << __func__ << " " << "searching addr=" << coinaddr << std::endl;
|
||||
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it = unspentOutputs.begin(); it != unspentOutputs.end(); it++)
|
||||
{
|
||||
if (myIsutxo_spentinmempool(ignoretxid, ignorevin, it->first.txhash, (int32_t)it->first.index) == 0)
|
||||
{
|
||||
//const CCoins *pcoins = pcoinsTip->AccessCoins(it->first.txhash); <-- no opret in coins
|
||||
CTransaction tx;
|
||||
uint256 hashBlock;
|
||||
int32_t nvout = it->first.index;
|
||||
if (myGetTransaction(it->first.txhash, tx, hashBlock))
|
||||
{
|
||||
class BaseCCChecker *baseChecker = ccCheckerTable[evalcode];
|
||||
|
||||
// if a checker is set for evalcode use it otherwise use the default checker:
|
||||
if (baseChecker && baseChecker->checkCC(it->first.txhash, tx.vout, nvout, evalcode, funcids, filtertxid) || defaultCCChecker.checkCC(it->first.txhash, tx.vout, nvout, evalcode, funcids, filtertxid))
|
||||
{
|
||||
std::cerr << __func__ << " " << "filtered utxo with amount=" << tx.vout[nvout].nValue << std::endl;
|
||||
|
||||
struct CC_utxo utxo;
|
||||
utxo.txid = it->first.txhash;
|
||||
utxo.vout = (int32_t)it->first.index;
|
||||
utxo.nValue = it->second.satoshis;
|
||||
//utxo.height = it->second.blockHeight;
|
||||
utxoSelected.push_back(utxo);
|
||||
total += it->second.satoshis;
|
||||
}
|
||||
}
|
||||
else
|
||||
std::cerr << __func__ << " " << "ERROR: cant load tx for txid, please reindex" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (amount == 0) {
|
||||
// just return total value
|
||||
ptr->total = total;
|
||||
len = (int32_t)(sizeof(*ptr) - sizeof(ptr->utxos)/*subtract not serialized part of NSPV_utxoresp*/);
|
||||
return len;
|
||||
}
|
||||
|
||||
// pick optimal utxos for the requested amount
|
||||
CAmount remains = amount;
|
||||
std::vector<struct CC_utxo> utxoAdded;
|
||||
|
||||
while (utxoSelected.size() > 0)
|
||||
{
|
||||
int64_t below = 0, above = 0;
|
||||
int32_t abovei = -1, belowi = -1, ind = -1;
|
||||
|
||||
if (CC_vinselect(&abovei, &above, &belowi, &below, utxoSelected.data(), utxoSelected.size(), remains) < 0)
|
||||
{
|
||||
std::cerr << "error CC_vinselect" << " remains=" << remains << " amount=" << amount << " abovei=" << abovei << " belowi=" << belowi << " ind=" << " utxoSelected.size()=" << utxoSelected.size() << ind << std::endl;
|
||||
return 0;
|
||||
}
|
||||
if (abovei >= 0) // best is 'above'
|
||||
ind = abovei;
|
||||
else if (belowi >= 0) // second try is 'below'
|
||||
ind = belowi;
|
||||
else
|
||||
{
|
||||
std::cerr << "error finding unspent" << " remains=" << remains << " amount=" << amount << " abovei=" << abovei << " belowi=" << belowi << " ind=" << " utxoSelected.size()=" << utxoSelected.size() << ind << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
utxoAdded.push_back(utxoSelected[ind]);
|
||||
total += utxoSelected[ind].nValue;
|
||||
remains -= utxoSelected[ind].nValue;
|
||||
|
||||
// remove used utxo[ind]:
|
||||
utxoSelected[ind] = utxoSelected.back();
|
||||
utxoSelected.pop_back();
|
||||
|
||||
if (total >= amount) // found the requested amount
|
||||
break;
|
||||
if (utxoAdded.size() >= maxinputs) // reached maxinputs
|
||||
break;
|
||||
}
|
||||
ptr->numutxos = (uint16_t)utxoAdded.size();
|
||||
ptr->total = total;
|
||||
ptr->utxos = (NSPV_utxoresp*)calloc(ptr->numutxos, sizeof(ptr->utxos[0]));
|
||||
|
||||
for (uint16_t i = 0; i < ptr->numutxos; i++)
|
||||
{
|
||||
ptr->utxos[i].satoshis = utxoAdded[i].nValue;
|
||||
ptr->utxos[i].txid = utxoAdded[i].txid;
|
||||
ptr->utxos[i].vout = utxoAdded[i].vout;
|
||||
}
|
||||
|
||||
len = (int32_t)(sizeof(*ptr) - sizeof(ptr->utxos)/*subtract not serialized part of NSPV_utxoresp*/ + sizeof(*ptr->utxos)*ptr->numutxos);
|
||||
if (len < maxlen)
|
||||
return len; // good length
|
||||
else
|
||||
{
|
||||
NSPV_utxosresp_purge(ptr);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t NSPV_getaddresstxids(struct NSPV_txidsresp *ptr,char *coinaddr,bool isCC,int32_t skipcount,uint32_t filter)
|
||||
{
|
||||
int32_t maxlen,txheight,ind=0,n = 0,len = 0; CTransaction tx; uint256 hashBlock;
|
||||
std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
|
||||
SetCCtxids(txids,coinaddr,isCC);
|
||||
//SetCCtxids(txids,coinaddr,isCC);
|
||||
ptr->nodeheight = chainActive.LastTip()->GetHeight();
|
||||
maxlen = MAX_BLOCK_SIZE(ptr->nodeheight) - 512;
|
||||
maxlen /= sizeof(*ptr->txids);
|
||||
@@ -799,7 +669,7 @@ int32_t NSPV_gettxproof(struct NSPV_txproof *ptr,int32_t vout,uint256 txid,int32
|
||||
}
|
||||
}
|
||||
}
|
||||
ptr->unspentvalue = CCgettxout(txid,vout,1,1);
|
||||
// ptr->unspentvalue = CCgettxout(txid,vout,1,1);
|
||||
}
|
||||
return(sizeof(*ptr) - sizeof(ptr->tx) - sizeof(ptr->txproof) + ptr->txlen + ptr->txprooflen);
|
||||
}
|
||||
@@ -855,16 +725,6 @@ int32_t NSPV_getspentinfo(struct NSPV_spentinfo *ptr,uint256 txid,int32_t vout)
|
||||
ptr->vout = vout;
|
||||
ptr->spentvini = -1;
|
||||
len = (int32_t)(sizeof(*ptr) - sizeof(ptr->spent.tx) - sizeof(ptr->spent.txproof));
|
||||
if ( CCgetspenttxid(ptr->spent.txid,ptr->spentvini,ptr->spent.height,txid,vout) == 0 )
|
||||
{
|
||||
if ( NSPV_gettxproof(&ptr->spent,0,ptr->spent.txid,ptr->spent.height) > 0 )
|
||||
len += ptr->spent.txlen + ptr->spent.txprooflen;
|
||||
else
|
||||
{
|
||||
NSPV_txproof_purge(&ptr->spent);
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
@@ -1166,65 +1026,6 @@ void hush_nSPVreq(CNode *pfrom,std::vector<uint8_t> request) // received a reque
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (request[0] == NSPV_CCMODULEUTXOS) // get cc module utxos from coinaddr for the requested amount, evalcode, funcid list and txid
|
||||
{
|
||||
//fprintf(stderr,"utxos: %u > %u, ind.%d, len.%d\n",timestamp,pfrom->prevtimes[ind],ind,len);
|
||||
if (timestamp > pfrom->prevtimes[ind])
|
||||
{
|
||||
struct NSPV_utxosresp U;
|
||||
char coinaddr[64];
|
||||
int64_t amount;
|
||||
uint8_t evalcode;
|
||||
char funcids[27];
|
||||
uint256 filtertxid;
|
||||
bool errorFormat = false;
|
||||
const int32_t BITCOINADDRESSMINLEN = 20;
|
||||
|
||||
int32_t minreqlen = sizeof(uint8_t) + sizeof(uint8_t) + BITCOINADDRESSMINLEN + sizeof(amount) + sizeof(evalcode) + sizeof(uint8_t) + sizeof(filtertxid);
|
||||
int32_t maxreqlen = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(coinaddr)-1 + sizeof(amount) + sizeof(evalcode) + sizeof(uint8_t) + sizeof(funcids)-1 + sizeof(filtertxid);
|
||||
|
||||
if (len >= minreqlen && len <= maxreqlen)
|
||||
{
|
||||
n = 1;
|
||||
int32_t addrlen = request[n++];
|
||||
if (addrlen < sizeof(coinaddr))
|
||||
{
|
||||
memcpy(coinaddr, &request[n], addrlen);
|
||||
coinaddr[addrlen] = 0;
|
||||
n += addrlen;
|
||||
dragon_rwnum(0, &request[n], sizeof(amount), &amount);
|
||||
n += sizeof(amount);
|
||||
dragon_rwnum(0, &request[n], sizeof(evalcode), &evalcode);
|
||||
n += sizeof(evalcode);
|
||||
|
||||
int32_t funcidslen = request[n++];
|
||||
if (funcidslen < sizeof(funcids))
|
||||
{
|
||||
memcpy(funcids, &request[n], funcidslen);
|
||||
funcids[funcidslen] = 0;
|
||||
n += funcidslen;
|
||||
dragon_rwbignum(0, &request[n], sizeof(filtertxid), (uint8_t *)&filtertxid);
|
||||
std::cerr << __func__ << " " << "request addr=" << coinaddr << " amount=" << amount << " evalcode=" << (int)evalcode << " funcids=" << funcids << " filtertxid=" << filtertxid.GetHex() << std::endl;
|
||||
|
||||
memset(&U, 0, sizeof(U));
|
||||
if ((slen = NSPV_getccmoduleutxos(&U, coinaddr, amount, evalcode, funcids, filtertxid)) > 0)
|
||||
{
|
||||
std::cerr << __func__ << " " << "created utxos, slen=" << slen << std::endl;
|
||||
response.resize(1 + slen);
|
||||
response[0] = NSPV_CCMODULEUTXOSRESP;
|
||||
if (NSPV_rwutxosresp(1, &response[1], &U) == slen)
|
||||
{
|
||||
pfrom->PushMessage("nSPV", response);
|
||||
pfrom->prevtimes[ind] = timestamp;
|
||||
std::cerr << __func__ << " " << "returned nSPV response" << std::endl;
|
||||
}
|
||||
NSPV_utxosresp_purge(&U);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user