Browse Source

Delete more CCs #381

duke
Duke 3 months ago
parent
commit
ca169d9300
  1. 93
      src/cc/CCinclude.h
  2. 40
      src/cc/CCtx.cpp
  3. 11
      src/cc/CCutils.cpp
  4. 205
      src/hush_nSPV_fullnode.h

93
src/cc/CCinclude.h

@ -215,7 +215,6 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector<std::pair<CAddr
int32_t CCgetspenttxid(uint256 &spenttxid,int32_t &vini,int32_t &height,uint256 txid,int32_t vout);
/// @private
void CCclearvars(struct CCcontract_info *cp);
UniValue CClib(struct CCcontract_info *cp,char *method,char *jsonstr);
UniValue CClib_info(struct CCcontract_info *cp);
@ -290,21 +289,6 @@ CPubKey CCtxidaddr(char *txidaddr,uint256 txid);
/// @return public key for the created address
CPubKey CCCustomtxidaddr(char *txidaddr,uint256 txid,uint8_t taddr,uint8_t prefix,uint8_t prefix2);
// TODO: description
/// @private
bool GetCCParams(Eval* eval, const CTransaction &tx, uint32_t nIn,
CTransaction &txOut, std::vector<std::vector<unsigned char>> &preConditions, std::vector<std::vector<unsigned char>> &params);
/// \cond INTERNAL
int64_t OraclePrice(int32_t height,uint256 reforacletxid,char *markeraddr,char *format);
uint256 OracleMerkle(int32_t height,uint256 reforacletxid,char *format,std::vector<struct oracle_merklepair>publishers);
uint256 OraclesBatontxid(uint256 oracletxid,CPubKey pk);
uint8_t DecodeOraclesCreateOpRet(const CScript &scriptPubKey,std::string &name,std::string &description,std::string &format);
uint8_t DecodeOraclesOpRet(const CScript &scriptPubKey,uint256 &oracletxid,CPubKey &pk,int64_t &num);
uint8_t DecodeOraclesData(const CScript &scriptPubKey,uint256 &oracletxid,uint256 &batontxid,CPubKey &pk,std::vector <uint8_t>&data);
int32_t oracle_format(uint256 *hashp,int64_t *valp,char *str,uint8_t fmt,uint8_t *data,int32_t offset,int32_t datalen);
/// \endcond
/// Adds token inputs to transaction object. If tokenid is a non-fungible token then the function will set additionalTokensEvalcode2 variable in the cp object to the eval code from NFT data to spend NFT outputs properly
/// @param cp CCcontract_info structure
/// @param mtx mutable transaction object
@ -340,71 +324,6 @@ bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
/// @private
int32_t payments_parsehexdata(std::vector<uint8_t> &hexdata,cJSON *item,int32_t len);
/// Makes opreturn scriptPubKey for token creation transaction. Normally this function is called internally by the tokencreate rpc. You might need to call this function to create a customized token.
/// The total opreturn length should not exceed 10001 byte
/// @param funcid should be set to 'c' character
/// @param origpubkey token creator pubkey as byte array
/// @param name token name (no more than 32 char)
/// @param description token description (no more than 4096 char)
/// @param vopretNonfungible NFT data, could be empty. If not empty, NFT will be created, the first byte if the NFT data should be set to the eval code of the contract validating this NFT data
/// @returns scriptPubKey with OP_RETURN script
CScript EncodeTokenCreateOpRet(uint8_t funcid, std::vector<uint8_t> origpubkey, std::string name, std::string description, vscript_t vopretNonfungible);
/// Makes opreturn scriptPubKey for token creation transaction. Normally this function is called internally by the tokencreate rpc. You might need to call it to create a customized token.
/// The total opreturn length should not exceed 10001 byte
/// @param funcid should be set to 'c' character
/// @param origpubkey token creator pubkey as byte array
/// @param name token name (no more than 32 char)
/// @param description token description (no more than 4096 char)
/// @param oprets vector of pairs of additional data added to the token opret. The first element in the pair is opretid enum, the second is the data as byte array
/// @returns scriptPubKey with OP_RETURN script
/// @see opretid
CScript EncodeTokenCreateOpRet(uint8_t funcid, std::vector<uint8_t> origpubkey, std::string name, std::string description, std::vector<std::pair<uint8_t, vscript_t>> oprets);
/// Makes opreturn scriptPubKey for token transaction. Normally this function is called internally by the token rpcs. You might call this function if your module should create a customized token.
/// The total opreturn length should not exceed 10001 byte
/// @param tokenid id of the token
/// @param voutPubkeys vector of pubkeys used to make the token vout in the same transaction that the created opreturn is for, the pubkeys are used for token vout validation
/// @param opretWithId a pair of additional opreturn data added to the token opret. Could be empty. The first element in the pair is opretid enum, the second is the data as byte array
/// @returns scriptPubKey with OP_RETURN script
CScript EncodeTokenOpRet(uint256 tokenid, std::vector<CPubKey> voutPubkeys, std::pair<uint8_t, vscript_t> opretWithId);
/// An overload to make opreturn scriptPubKey for token transaction. Normally this function is called internally by the token rpcs. You might call this function if your module should create a customized token.
/// The total opreturn length should not exceed 10001 byte
/// @param tokenid id of the token
/// @param voutPubkeys vector of pubkeys used to make the token vout in the same transaction that the created opreturn is for, the pubkeys are used for token vout validation
/// @param oprets vector of pairs of additional opreturn data added to the token opret. Could be empty. The first element in the pair is opretid enum, the second is the data as byte array
/// @returns scriptPubKey with OP_RETURN script
CScript EncodeTokenOpRet(uint256 tokenid, std::vector<CPubKey> voutPubkeys, std::vector<std::pair<uint8_t, vscript_t>> oprets);
/// Decodes opreturn scriptPubKey of token creation transaction. Normally this function is called internally by the token rpcs. You might call this function if your module should create a customized token.
/// @param scriptPubKey OP_RETURN script to decode
/// @param[out] origpubkey creator public key as a byte array
/// @param[out] name token name
/// @param[out] description token description
/// @returns funcid ('c') or NULL if errors
uint8_t DecodeTokenCreateOpRet(const CScript &scriptPubKey, std::vector<uint8_t> &origpubkey, std::string &name, std::string &description);
/// Overload that decodes opreturn scriptPubKey of token creation transaction and also returns additional data blobs.
/// Normally this function is called internally by the token rpcs. You might want to call this function if your module should create a customized token.
/// @param scriptPubKey OP_RETURN script to decode
/// @param[out] origpubkey creator public key as a byte array
/// @param[out] name token name
/// @param[out] description token description
/// @param[out] oprets vector of pairs of additional opreturn data added to the token opret. Could be empty if not set. The first element in the pair is opretid enum, the second is the data as byte array
/// @returns funcid ('c') or NULL if errors
uint8_t DecodeTokenCreateOpRet(const CScript &scriptPubKey, std::vector<uint8_t> &origpubkey, std::string &name, std::string &description, std::vector<std::pair<uint8_t, vscript_t>> &oprets);
/// Decodes opreturn scriptPubKey of token transaction, also returns additional data blobs.
/// Normally this function is called internally by different token rpc. You might want to call if your module created a customized token.
/// @param scriptPubKey OP_RETURN script to decode
/// @param[out] evalCodeTokens should be EVAL_TOKENS
/// @param[out] tokenid id of token
/// @param[out] voutPubkeys vector of token output validation pubkeys from the opreturn
/// @param[out] oprets vector of pairs of additional opreturn data added to the token opret. Could be empty if not set. The first element in the pair is opretid enum, the second is the data as byte array
/// @returns funcid ('c' if creation tx or 't' if token transfer tx) or NULL if errors
uint8_t DecodeTokenOpRet(const CScript scriptPubKey, uint8_t &evalCodeTokens, uint256 &tokenid, std::vector<CPubKey> &voutPubkeys, std::vector<std::pair<uint8_t, vscript_t>> &oprets);
/// @private
int64_t AddCClibtxfee(struct CCcontract_info *cp, CMutableTransaction &mtx, CPubKey pk);
@ -649,18 +568,6 @@ std::vector<uint8_t> Mypubkey();
/// @see Mypubkey
bool Myprivkey(uint8_t myprivkey[]);
/// Returns duration in seconds and number of blocks since the block where a transaction resides till the chain tip
/// @param[out] numblocks number of blocks from the block where the transaction with txid resides
/// @param txid id of the transaction that is queried of
/// @return duration in seconds since the block where the transaction with txid resides
int64_t CCduration(int32_t &numblocks,uint256 txid);
/// @private
uint256 CCOraclesReverseScan(char const *logcategory,uint256 &txid,int32_t height,uint256 reforacletxid,uint256 batontxid);
/// @private
int32_t CCCointxidExists(char const *logcategory,uint256 cointxid);
/// @private
uint256 BitcoinGetProofMerkleRoot(const std::vector<uint8_t> &proofData, std::vector<uint256> &txids);

40
src/cc/CCtx.cpp

@ -66,46 +66,6 @@ void SetCCunspents(std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValu
return;
}
void SetCCtxids(std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,char *coinaddr,bool ccflag)
{
return;
}
void SetCCtxids(std::vector<uint256> &txids,char *coinaddr,bool ccflag, uint8_t evalcode, uint256 filtertxid, uint8_t func)
{
return;
}
int64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout,int32_t CCflag)
{
return(0);
}
int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag)
{
return(-1);
}
int32_t CCgetspenttxid(uint256 &spenttxid,int32_t &vini,int32_t &height,uint256 txid,int32_t vout)
{
return(0);
}
int64_t CCaddress_balance(char *coinaddr,int32_t CCflag)
{
return 0;
}
int64_t CCfullsupply(uint256 tokenid)
{
return(0);
}
int64_t CCtoken_balance(char *coinaddr,uint256 reftokenid)
{
return 0;
}
int32_t CC_vinselect(int32_t *aboveip,int64_t *abovep,int32_t *belowip,int64_t *belowp,struct CC_utxo utxos[],int32_t numunspents,int64_t value)
{
int32_t i,abovei,belowi; int64_t above,below,gap,atx_value;

11
src/cc/CCutils.cpp

@ -280,12 +280,6 @@ CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv)
return(pubkey2pk(ParseHex(cp->CChexstr)));
}
void CCclearvars(struct CCcontract_info *cp)
{
cp->unspendableEvalcode2 = cp->unspendableEvalcode3 = 0;
cp->unspendableaddr2[0] = cp->unspendableaddr3[0] = 0;
}
int32_t NSPV_coinaddr_inmempool(char const *logcategory,char *coinaddr,uint8_t CCflag);
int32_t myIs_coinaddr_inmempoolvout(char const *logcategory,char *coinaddr)
@ -462,11 +456,6 @@ int64_t TotalPubkeyCCInputs(const CTransaction &tx, const CPubKey &pubkey)
return total;
}
bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector<uint8_t> paramsNull,const CTransaction &ctx, unsigned int nIn)
{
return(false);
}
extern struct CCcontract_info CCinfos[0x100];
extern std::string MYCCLIBNAME;
bool CClib_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx,unsigned int nIn);

205
src/hush_nSPV_fullnode.h

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

Loading…
Cancel
Save