From 79a3b761c234a140b622e24e8307ce0c65627e52 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 4 Aug 2018 22:25:00 -1100 Subject: [PATCH 1/7] Change regrets address prefix --- src/chainparams.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index d48d32c93..6903fe54e 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -568,9 +568,12 @@ public: 0 }; // These prefixes are the same as the testnet prefixes - base58Prefixes[PUBKEY_ADDRESS] = {0x1D,0x25}; - base58Prefixes[SCRIPT_ADDRESS] = {0x1C,0xBA}; - base58Prefixes[SECRET_KEY] = {0xEF}; + base58Prefixes[PUBKEY_ADDRESS] = std::vector(1,60); + base58Prefixes[SCRIPT_ADDRESS] = std::vector(1,85); + base58Prefixes[SECRET_KEY] = std::vector(1,188); + //base58Prefixes[PUBKEY_ADDRESS] = {0x1D,0x25}; + //base58Prefixes[SCRIPT_ADDRESS] = {0x1C,0xBA}; + //base58Prefixes[SECRET_KEY] = {0xEF}; // do not rely on these BIP32 prefixes; they are not specified and may change base58Prefixes[EXT_PUBLIC_KEY] = {0x04,0x35,0x87,0xCF}; base58Prefixes[EXT_SECRET_KEY] = {0x04,0x35,0x83,0x94}; From 6deb8c09eb42c1f626edcf68cf20d9e58c87d792 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 20:43:35 -1100 Subject: [PATCH 2/7] Disable FSM, auction, lotto and add some error checks --- src/cc/CCassetstx.cpp | 5 +---- src/cc/auction.cpp | 1 + src/cc/faucet.cpp | 9 +++++---- src/cc/fsm.cpp | 1 + src/cc/lotto.cpp | 1 + src/cc/rewards.cpp | 2 +- src/main.h | 3 ++- src/wallet/rpcwallet.cpp | 2 ++ 8 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 51668eda8..7d55b85ff 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -15,9 +15,6 @@ #include "CCassets.h" -// need allassets -// find asset - int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs) { char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t j,vout,n = 0; @@ -35,7 +32,7 @@ int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK continue; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsAssetvout(price,origpubkey,vintx,vout,assetid)) > 0 ) + if ( (nValue= IsAssetvout(price,origpubkey,vintx,vout,assetid)) > 0 && myIsutxo_spentinmempool(txid,vout) == 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index cf3ae601a..7d8512ede 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -73,6 +73,7 @@ bool AuctionExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransactio bool AuctionValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + return(false); // reject any auction CC for now numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 51d7f2f6f..570f1539a 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -126,20 +126,21 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t n = 0; + char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t vout,n = 0; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; - // prevent dup + vout = (int32_t)it->first.index); + // no need to prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 1000000 ) + if ( (nValue= IsFaucetvout(cp,vintx,vout) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 ) { if ( total != 0 && maxinputs != 0 ) - mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); + mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = it->second.satoshis; totalinputs += nValue; n++; diff --git a/src/cc/fsm.cpp b/src/cc/fsm.cpp index 201535992..e3e112bf6 100644 --- a/src/cc/fsm.cpp +++ b/src/cc/fsm.cpp @@ -73,6 +73,7 @@ bool FSMExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &t bool FSMValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + return(false); // reject any FSM CC for now numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; diff --git a/src/cc/lotto.cpp b/src/cc/lotto.cpp index 43367ab70..0ed223c25 100644 --- a/src/cc/lotto.cpp +++ b/src/cc/lotto.cpp @@ -73,6 +73,7 @@ bool LottoExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction bool LottoValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval; + return(false); // reject any lotto CC for now numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index a0f07aba1..afa025851 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -282,7 +282,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr break; if ( j != mtx.vin.size() ) continue; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myIsutxo_spentinmempool(txid,vout) == 0 ) { if ( (funcid= DecodeRewardsOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) { diff --git a/src/main.h b/src/main.h index 56b439fc0..dfea318cb 100644 --- a/src/main.h +++ b/src/main.h @@ -103,9 +103,10 @@ static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60; static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111; //static const bool DEFAULT_ADDRESSINDEX = false; +//static const bool DEFAULT_SPENTINDEX = false; #define DEFAULT_ADDRESSINDEX (GetArg("-ac_cc",0) != 0) +#define DEFAULT_SPENTINDEX (GetArg("-ac_cc",0) != 0) static const bool DEFAULT_TIMESTAMPINDEX = false; -static const bool DEFAULT_SPENTINDEX = false; static const unsigned int DEFAULT_DB_MAX_OPEN_FILES = 1000; static const bool DEFAULT_DB_COMPRESSION = true; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 295460618..c2e486a05 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4837,6 +4837,8 @@ int32_t ensure_CCrequirements() return(-1); else if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) == 0 ) return(-1); + else if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) == 0 ) + return(-1); else return(0); } From 8a78bd9d8c9dde5b1c8d0d9be904107b894e77c1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 20:47:51 -1100 Subject: [PATCH 3/7] Test --- src/cc/faucet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 570f1539a..a786edd2e 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -133,7 +133,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; - vout = (int32_t)it->first.index); + vout = (int32_t)it->first.index; // no need to prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { From b24e0ff44ef43c39462498a2ee51c43a7a59e906 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 20:49:12 -1100 Subject: [PATCH 4/7] ) --- src/cc/faucet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index a786edd2e..b8b3b62cb 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -137,7 +137,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu // no need to prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsFaucetvout(cp,vintx,vout) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 ) + if ( (nValue= IsFaucetvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); From 718255b3fdf835774de32a41decea1eb555b0bae Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 23:36:00 -1100 Subject: [PATCH 5/7] smart utxo selection without press release to coin telegraph --- src/cc/CCinclude.h | 7 ++++ src/cc/CCtx.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++--- src/komodo_kv.h | 4 +- 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 0ab0bf874..a59a2325c 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -33,6 +33,13 @@ union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; typedef union _bits256 bits256; +struct CC_utxo +{ + uint256 txid; + int64_t nValue; + int32_t vout; +}; + struct CCcontract_info { uint256 prevtxid; diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index bea26e4a2..171ebf132 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -201,36 +201,124 @@ uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout) 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,uint64_t value) +{ + int32_t i,abovei,belowi; int64_t above,below,gap,atx_value; + abovei = belowi = -1; + for (above=below=i=0; i value ) + { + gap = (atx_value - value); + if ( above == 0 || gap < above ) + { + above = gap; + abovei = i; + } + } + else + { + gap = (value - atx_value); + if ( below == 0 || gap < below ) + { + below = gap; + belowi = i; + } + } + //printf("value %.8f gap %.8f abovei.%d %.8f belowi.%d %.8f\n",dstr(value),dstr(gap),abovei,dstr(above),belowi,dstr(below)); + } + *aboveip = abovei; + *abovep = above; + *belowip = belowi; + *belowp = below; + //printf("above.%d below.%d\n",abovei,belowi); + if ( abovei >= 0 && belowi >= 0 ) + { + if ( above < (below >> 1) ) + return(abovei); + else return(belowi); + } + else if ( abovei >= 0 ) + return(abovei); + else return(belowi); + //return(abovei >= 0 && above < (below>>1) ? abovei : belowi); +} + uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs) { - int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid,hashBlock; std::vector vecOutputs; CTransaction tx; + int32_t abovei,belowi,ind,vout,i,n = 0,maxutxos=1024; int64_t above,below; uint64_t remains,nValue,totalinputs = 0; uint256 txid,hashBlock; std::vector vecOutputs; CTransaction tx; struct CC_utxo *utxos,*up; #ifdef ENABLE_WALLET const CKeyStore& keystore = *pwalletMain; assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); + utxos = calloc(maxutxos,sizeof(*utxos)); BOOST_FOREACH(const COutput& out, vecOutputs) { if ( out.fSpendable != 0 ) { txid = out.tx->GetHash(); vout = out.i; - for (j=0; jtxid = txid; + up->nValue = out.tx->vout[out.i].nValue; + up->vout = vout; + /*mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = out.tx->vout[out.i].nValue; totalinputs += nValue; n++; if ( totalinputs >= total || n >= maxinputs ) + break;*/ + if ( n >= maxutxos ) break; } } } + remains = total; + for (i=0; i= 0 ) + ind = abovei; + else ind = belowi; + if ( ind < 0 ) + { + printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,numunspents,dstr(remains),dstr(amount),abovei,belowi,ind); + free(utxos); + return(0); + } + up = utxos[ind]; + utxos[ind] = utxos[--n]; + memset(&utxos[n],0,sizeof(utxos[n])); + mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); + totalinputs += up->nValue; + remains -= up->nValue; + if ( totalinputs >= total || (i+1) >= maxinputs ) + break; + } + free(utxos); if ( totalinputs >= total ) return(totalinputs); #endif diff --git a/src/komodo_kv.h b/src/komodo_kv.h index ee0496aae..2f4eadea4 100644 --- a/src/komodo_kv.h +++ b/src/komodo_kv.h @@ -88,7 +88,7 @@ int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp if ( (retval= ptr->valuesize) > 0 ) memcpy(value,ptr->value,retval); } - } else fprintf(stderr,"couldnt find (%s)\n",(char *)key); + } //else fprintf(stderr,"couldnt find (%s)\n",(char *)key); portable_mutex_unlock(&KOMODO_KV_mutex); if ( retval < 0 ) { @@ -172,7 +172,7 @@ void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) memcpy(ptr->key,key,keylen); newflag = 1; HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); - fprintf(stderr,"KV add.(%s) (%s)\n",ptr->key,valueptr); + //fprintf(stderr,"KV add.(%s) (%s)\n",ptr->key,valueptr); } if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) { From 6facc0ea0631f1bad87f68b01a5f2fd2fe4e0803 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 23:39:16 -1100 Subject: [PATCH 6/7] Syntax --- src/cc/CCtx.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 171ebf132..b3cbfda88 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -260,7 +260,7 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in assert(pwalletMain != NULL); LOCK2(cs_main, pwalletMain->cs_wallet); pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); - utxos = calloc(maxutxos,sizeof(*utxos)); + utxos = (struct CC_utxo *)calloc(maxutxos,sizeof(*utxos)); BOOST_FOREACH(const COutput& out, vecOutputs) { if ( out.fSpendable != 0 ) @@ -296,7 +296,7 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in abovei = belowi = -1; if ( CC_vinselect(&abovei,&above,&belowi,&below,utxos,n,remains) < 0 ) { - printf("error finding unspent i.%d of %d, %.8f vs %.8f\n",i,numunspents,dstr(remains),dstr(amount)); + printf("error finding unspent i.%d of %d, %.8f vs %.8f\n",i,n,(double)remains/COIN,(double)total/COIN); free(utxos); return(0); } @@ -305,11 +305,11 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in else ind = belowi; if ( ind < 0 ) { - printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,numunspents,dstr(remains),dstr(amount),abovei,belowi,ind); + printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,numunspents,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); free(utxos); return(0); } - up = utxos[ind]; + up = &utxos[ind]; utxos[ind] = utxos[--n]; memset(&utxos[n],0,sizeof(utxos[n])); mtx.vin.push_back(CTxIn(up->txid,up->vout,CScript())); From eea3a3cdb15bca18866ec0d009e8d921b1376458 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 5 Aug 2018 23:39:57 -1100 Subject: [PATCH 7/7] N --- src/cc/CCtx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index b3cbfda88..666132590 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -305,7 +305,7 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in else ind = belowi; if ( ind < 0 ) { - printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,numunspents,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); + printf("error finding unspent i.%d of %d, %.8f vs %.8f, abovei.%d belowi.%d ind.%d\n",i,n,(double)remains/COIN,(double)total/COIN,abovei,belowi,ind); free(utxos); return(0); }