Merge pull request #1224 from jl777/jl777

Jl777
This commit is contained in:
jl777
2019-01-30 04:18:43 -11:00
committed by GitHub
11 changed files with 2442 additions and 52 deletions

View File

@@ -281,7 +281,7 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode)
sprintf(&cp->CChexstr[i*2],"%02x",pub33[i]);
cp->CChexstr[i*2] = 0;
GetCCaddress(cp,cp->unspendableCCaddr,pk);
printf("evalcode.%d initialized\n",evalcode);
//printf("evalcode.%d initialized\n",evalcode);
return(0);
}
}

View File

@@ -134,6 +134,7 @@ int32_t CCgetspenttxid(uint256 &spenttxid,int32_t &vini,int32_t &height,uint256
void CCclearvars(struct CCcontract_info *cp);
UniValue CClib(struct CCcontract_info *cp,char *method,cJSON *params);
UniValue CClib_info(struct CCcontract_info *cp);
CBlockIndex *komodo_blockindex(uint256 hash);
static const uint256 zeroid;
bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock);

View File

@@ -73,7 +73,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran
unspendablepk = GetUnspendable(cp, unspendablepriv);
GetCCaddress(cp, unspendable, unspendablepk);
othercond = MakeCCcond1(cp->evalcode, unspendablepk);
//printf("evalcode.%d (%s)\n",cp->evalcode,unspendable);
// tokens support:
// to spend from dual-eval mypk vout
@@ -177,7 +177,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran
{
privkey = unspendablepriv;
cond = othercond;
//fprintf(stderr,"FinalizeCCTx() matched unspendable CC addr.(%s)\n",unspendable);
//fprintf(stderr,"FinalizeCCTx(%d) matched unspendable CC addr.(%s)\n",cp->evalcode,unspendable);
}
else if (strcmp(destaddr, tokensunspendable) == 0)
{

View File

@@ -37,20 +37,22 @@ char *CClib_name() { return((char *)MYCCLIBNAME.c_str()); }
struct CClib_rpcinfo
{
char *CCname,*method,*help;
int32_t numrequiredargs,maxargs; // frontloaded with required
int32_t numrequiredargs,maxargs;
uint8_t funcid,evalcode;
}
CClib_methods[] =
{
{ (char *)"faucet2", (char *)"fund", (char *)"amount", 1, 1, 'F', EVAL_FAUCET2 },
{ (char *)"faucet2", (char *)"get", (char *)"<no args>", 0, 0, 'G', EVAL_FAUCET2 },
{ (char *)"sudoku", (char *)"gen", (char *)"amount", 1, 1, 'G', EVAL_SUDOKU },
{ (char *)"sudoku", (char *)"gen", (char *)"<no args>", 0, 0, 'G', EVAL_SUDOKU },
{ (char *)"sudoku", (char *)"txidinfo", (char *)"txid", 1, 1, 'T', EVAL_SUDOKU },
{ (char *)"sudoku", (char *)"pending", (char *)"<no args>", 0, 0, 'U', EVAL_SUDOKU },
{ (char *)"sudoku", (char *)"solution", (char *)"solution timestamps[]", 2, 2, 'S', EVAL_SUDOKU },
{ (char *)"sudoku", (char *)"solution", (char *)"txid solution timestamps[81]", 83, 83, 'S', EVAL_SUDOKU },
};
std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *params);
bool sudoku_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx);
UniValue sudoku_txidinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params);
UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params);
UniValue sudoku_solution(uint64_t txfee,struct CCcontract_info *cp,cJSON *params);
@@ -61,6 +63,7 @@ UniValue CClib_method(struct CCcontract_info *cp,char *method,cJSON *params)
UniValue result(UniValue::VOBJ); uint64_t txfee = 10000;
if ( cp->evalcode == EVAL_SUDOKU )
{
//printf("CClib_method params.%p\n",params);
if ( strcmp(method,"txidinfo") == 0 )
return(sudoku_txidinfo(txfee,cp,params));
else if ( strcmp(method,"gen") == 0 )
@@ -117,6 +120,7 @@ UniValue CClib_info(struct CCcontract_info *cp)
UniValue CClib(struct CCcontract_info *cp,char *method,cJSON *params)
{
UniValue result(UniValue::VOBJ); int32_t i; std::string rawtx;
//printf("CClib params.%p\n",params);
for (i=0; i<sizeof(CClib_methods)/sizeof(*CClib_methods); i++)
{
if ( cp->evalcode == CClib_methods[i].evalcode && strcmp(method,CClib_methods[i].method) == 0 )
@@ -137,12 +141,12 @@ UniValue CClib(struct CCcontract_info *cp,char *method,cJSON *params)
return(result);
}
int64_t IsCClibvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
int64_t IsCClibvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v,char *cmpaddr)
{
char destaddr[64];
if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
{
if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cmpaddr) == 0 )
return(tx.vout[v].nValue);
}
return(0);
@@ -167,7 +171,7 @@ bool CClibExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction
//fprintf(stderr,"vini.%d check hash and vout\n",i);
if ( hashBlock == zerohash )
return eval->Invalid("cant faucet2 from mempool");
if ( (assetoshis= IsCClibvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
if ( (assetoshis= IsCClibvout(cp,vinTx,tx.vin[i].prevout.n,cp->unspendableCCaddr)) != 0 )
inputs += assetoshis;
}
}
@@ -175,7 +179,7 @@ bool CClibExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction
for (i=0; i<numvouts; i++)
{
//fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
if ( (assetoshis= IsCClibvout(cp,tx,i)) != 0 )
if ( (assetoshis= IsCClibvout(cp,tx,i,cp->unspendableCCaddr)) != 0 )
outputs += assetoshis;
}
if ( inputs != outputs+FAUCET2SIZE+txfee )
@@ -190,6 +194,8 @@ bool CClib_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const C
{
int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
if ( cp->evalcode != EVAL_FAUCET2 )
return(sudoku_validate(cp,height,eval,tx));
numvins = tx.vin.size();
numvouts = tx.vout.size();
preventCCvins = preventCCvouts = -1;
@@ -214,7 +220,7 @@ bool CClib_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const C
else
{
preventCCvouts = 1;
if ( IsCClibvout(cp,tx,0) != 0 )
if ( IsCClibvout(cp,tx,0,cp->unspendableCCaddr) != 0 )
{
preventCCvouts++;
i = 1;
@@ -246,7 +252,7 @@ bool CClib_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const C
}
}
int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs,char *cmpaddr)
{
char coinaddr[64]; int64_t threshold,nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
@@ -257,13 +263,13 @@ int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK
{
txid = it->first.txhash;
vout = (int32_t)it->first.index;
//char str[65]; fprintf(stderr,"%s check %s/v%d %.8f vs %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN,(double)threshold/COIN);
if ( it->second.satoshis < threshold )
continue;
//char str[65]; fprintf(stderr,"check %s/v%d %.8f`\n",uint256_str(str,txid),vout,(double)it->second.satoshis/COIN);
// no need to prevent dup
if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
{
if ( (nValue= IsCClibvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
if ( (nValue= IsCClibvout(cp,vintx,vout,cmpaddr)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
{
if ( total != 0 && maxinputs != 0 )
mtx.vin.push_back(CTxIn(txid,vout,CScript()));
@@ -272,7 +278,7 @@ int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK
n++;
if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
break;
} else fprintf(stderr,"nValue too small or already spent in mempool\n");
} //else fprintf(stderr,"nValue %.8f too small or already spent in mempool\n",(double)nValue/COIN);
} else fprintf(stderr,"couldnt get tx\n");
}
return(totalinputs);
@@ -327,7 +333,7 @@ std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *para
return("");
cclibpk = GetUnspendable(cp,0);
mypk = pubkey2pk(Mypubkey());
if ( (inputs= AddCClibInputs(cp,mtx,cclibpk,nValue+txfee,60)) > 0 )
if ( (inputs= AddCClibInputs(cp,mtx,cclibpk,nValue+txfee,60,cp->unspendableCCaddr)) > 0 )
{
if ( inputs > nValue )
CCchange = (inputs - nValue - txfee);

View File

@@ -35,8 +35,8 @@
* *
******************************************************************************/
#ifndef cJSON__h
#define cJSON__h
#ifndef cJSON__ccih
#define cJSON__ccih
#include <stdio.h>
#include <stdlib.h>

File diff suppressed because it is too large Load Diff

View File

@@ -35,8 +35,8 @@
* *
******************************************************************************/
#ifndef cJSON__h
#define cJSON__h
#ifndef cJSON__cch
#define cJSON__cch
#ifdef __cplusplus
extern "C"

View File

@@ -1033,6 +1033,14 @@ int32_t komodo_MoM(int32_t *notarized_heightp,uint256 *MoMp,uint256 *kmdtxidp,in
return(depth);
}
CBlockIndex *komodo_blockindex(uint256 hash)
{
BlockMap::const_iterator it; CBlockIndex *pindex = 0;
if ( (it = mapBlockIndex.find(hash)) != mapBlockIndex.end() )
pindex = it->second;
return(pindex);
}
int32_t komodo_checkpoint(int32_t *notarized_heightp,int32_t nHeight,uint256 hash)
{
int32_t notarized_height,MoMdepth; uint256 MoM,notarized_hash,notarized_desttxid; CBlockIndex *notary,*pindex;

View File

@@ -173,7 +173,7 @@ int64_t komodo_current_supply(uint32_t nHeight)
else if ( decay == SATOSHIDEN )
{
int64_t lowestSubsidy, subsidyDifference, stepDifference, stepTriangle;
int64_t denominator, modulo;
int64_t denominator, modulo=1;
int32_t sign = 1;
if ( j == ASSETCHAINS_LASTERA )

View File

@@ -1047,7 +1047,7 @@ uint64_t komodo_block_prg(uint32_t nHeight)
{
int i;
uint8_t hashSrc[8];
uint64_t result, hashSrc64 = (uint64_t)ASSETCHAINS_MAGIC << 32 + nHeight;
uint64_t result=0, hashSrc64 = (uint64_t)ASSETCHAINS_MAGIC << 32 + nHeight;
bits256 hashResult;
for ( i = 0; i < sizeof(hashSrc); i++ )

View File

@@ -5220,6 +5220,7 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector<unsigned ch
result.push_back(Pair("result", "success"));
sprintf(str,"%sCCAddress",name);
result.push_back(Pair(str,cp->unspendableCCaddr));
result.push_back(Pair("CCbalance",ValueFromAmount(CCaddress_balance(cp->unspendableCCaddr))));
sprintf(str,"%sNormalAddress",name);
result.push_back(Pair(str,cp->normaladdr));
if (strcmp(name,"Gateways")==0) result.push_back(Pair("GatewaysPubkey","03ea9c062b9652d8eff34879b504eda0717895d27597aaeb60347d65eed96ccb40"));
@@ -5258,6 +5259,16 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector<unsigned ch
result.push_back(Pair(str,destaddr));
}
}
if ( GetCCaddress(cp,destaddr,pubkey2pk(Mypubkey())) != 0 )
{
result.push_back(Pair("myCCaddress",destaddr));
result.push_back(Pair("myCCbalance",ValueFromAmount(CCaddress_balance(destaddr))));
}
if ( Getscriptaddress(destaddr,(CScript() << Mypubkey() << OP_CHECKSIG)) != 0 )
{
result.push_back(Pair("myaddress",destaddr));
result.push_back(Pair("mybalance",ValueFromAmount(CCaddress_balance(destaddr))));
}
return(result);
}
@@ -5380,16 +5391,18 @@ UniValue cclib(const UniValue& params, bool fHelp)
if ( ensure_CCrequirements() < 0 )
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
method = (char *)params[0].get_str().c_str();
if ( params.size() >= 1 )
if ( params.size() >= 2 )
{
evalcode = atoi(params[1].get_str().c_str());
if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER )
{
printf("evalcode.%d vs (%d, %d)\n",evalcode,EVAL_FIRSTUSER,EVAL_LASTUSER);
//printf("evalcode.%d vs (%d, %d)\n",evalcode,EVAL_FIRSTUSER,EVAL_LASTUSER);
throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n");
}
if ( params.size() == 2 )
if ( params.size() == 3 )
{
jsonparams = cJSON_Parse(params[2].get_str().c_str());
}
}
cp = CCinit(&C,evalcode);
return(CClib(cp,method,jsonparams));