diff --git a/src/cc/CCChannels.h b/src/cc/CCChannels.h index c628cc918..30e1aeb78 100644 --- a/src/cc/CCChannels.h +++ b/src/cc/CCChannels.h @@ -18,8 +18,10 @@ #define CC_CHANNELS_H #include "CCinclude.h" +#define CHANNELS_MAXPAYMENTS 1000 bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); +std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int32_t payment); // CCcustom UniValue ChannelsInfo(); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 184466fa4..673b30f8e 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -92,7 +92,10 @@ int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex); CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); // CCutils +void endiancpy(uint8_t *dest,uint8_t *src,int32_t len); +uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv); CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk); +CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2); CC *MakeCCcond1(uint8_t evalcode,CPubKey pk); CC* GetCryptoCondition(CScript const& scriptSig); bool IsCCInput(CScript const& scriptSig); @@ -104,6 +107,7 @@ char *pubkey33_str(char *dest,uint8_t *pubkey33); uint256 Parseuint256(char *hexstr); CPubKey pubkey2pk(std::vector pubkey); bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk); +bool GetCCaddress1of2(struct CCcontract_info *cp,char *destaddr,CPubKey pk,CPubKey pk2); bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,int64_t nValue); bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t numvins,int32_t preventCCvouts,int32_t numvouts); bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); diff --git a/src/cc/CClotto.h b/src/cc/CClotto.h index 2b33769c5..784441c57 100644 --- a/src/cc/CClotto.h +++ b/src/cc/CClotto.h @@ -20,7 +20,6 @@ #include "CCinclude.h" #define EVAL_LOTTO 0xe9 -uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv); bool LottoValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 50f5f9697..3cbc04799 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -19,6 +19,16 @@ CCutils has low level functions that are universally useful for all contracts. */ +void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) +{ + int32_t i,j=0; +#if defined(WORDS_BIGENDIAN) + for (i=31; i>=0; i--) + dest[j++] = src[i]; +#else + memcpy(dest,src,len); +#endif +} CC *MakeCCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2) { @@ -48,6 +58,15 @@ CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk) return(vout); } +CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2) +{ + CTxOut vout; + CC *payoutCond = MakeCCcond1of2(evalcode,pk,pk2); + vout = CTxOut(nValue,CCPubKey(payoutCond)); + cc_free(payoutCond); + return(vout); +} + CC* GetCryptoCondition(CScript const& scriptSig) { auto pc = scriptSig.begin(); @@ -167,6 +186,18 @@ bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk) return(destaddr[0] != 0); } +bool GetCCaddress1of2(struct CCcontract_info *cp,char *destaddr,CPubKey pk,CPubKey pk2) +{ + CC *payoutCond; + destaddr[0] = 0; + if ( (payoutCond= MakeCCcond1of2(cp->evalcode,pk,pk2)) != 0 ) + { + Getscriptaddress(destaddr,CCPubKey(payoutCond)); + cc_free(payoutCond); + } + return(destaddr[0] != 0); +} + bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,int64_t nValue) { char destaddr[64]; diff --git a/src/cc/channels.cpp b/src/cc/channels.cpp index d3a26de39..972c8dc92 100644 --- a/src/cc/channels.cpp +++ b/src/cc/channels.cpp @@ -35,6 +35,14 @@ For efficiency we want to allow batch spend with multiple secrets to claim a single total + Second iteration: + As implementing it, some efficieny gains to be made with a slightly different approach. + Instead of separate secrets for each amount, a hashchain will be used, each releasing the same amount + + To spend, the prior value in the hash chain is published, or can publish N deep. validation takes N hashes. + + Also, in order to be able to track open channels, a tag is needed to be sent and better to send to a normal CC address for a pubkey to isolate the transactions for channel opens. + */ // start of consensus code @@ -129,6 +137,13 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & // helper functions for rpc calls in rpcwallet.cpp +CScript EncodeChannelsOpRet(uint8_t funcid,int32_t numpayments,int32_t payment,uint256 hashchain) +{ + CScript opret; uint8_t evalcode = EVAL_CHANNELS; + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << numpayments << payment << hashchain); + return(opret); +} + int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,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 vout,n = 0; @@ -157,58 +172,33 @@ int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP return(totalinputs); } -std::string ChannelsGet(uint64_t txfee,int64_t nValue) +std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int32_t payment) { - CMutableTransaction mtx,tmpmtx; CPubKey mypk,Channelspk; int64_t inputs,CCchange=0; struct CCcontract_info *cp,C; std::string rawhex; uint32_t j; int32_t i,len; uint8_t buf[32768]; bits256 hash; - cp = CCinit(&C,EVAL_CHANNELS); - if ( txfee == 0 ) - txfee = 10000; - Channelspk = GetUnspendable(cp,0); - mypk = pubkey2pk(Mypubkey()); - if ( (inputs= AddChannelsInputs(cp,mtx,Channelspk,nValue+txfee,60)) > 0 ) + CMutableTransaction mtx; uint8_t hash[32],hashdest[32]; uint64_t funds; int32_t i; uint256 hashchain,entropy,hentropy; CPubKey mypk; CScript opret; struct CCcontract_info *cp,C; + if ( numpayments <= 0 || payment <= 0 || numpayments > CHANNELS_MAXPAYMENTS ) { - if ( inputs > nValue ) - CCchange = (inputs - nValue - txfee); - if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CCchange,Channelspk)); - mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - fprintf(stderr,"start at %u\n",(uint32_t)time(NULL)); - j = rand() & 0xfffffff; - for (i=0; i<1000000; i++,j++) - { - tmpmtx = mtx; - rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_CHANNELS << (uint8_t)'G' << j)); - if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 ) - { - len >>= 1; - decode_hex(buf,len,(char *)rawhex.c_str()); - hash = bits256_doublesha256(0,buf,len); - if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 ) - { - fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL)); - return(rawhex); - } - //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]); - } - } - fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL)); + CCerror = strprintf(stderr,"invalid ChannelsFund param numpayments.%d max.%d payment.%d\n",numpayments,CHANNELS_MAXPAYMENTS,payment); + fprintf(stderr,"%s\n",CCerror.c_str()); return(""); - } else fprintf(stderr,"cant find Channels inputs\n"); - return(""); -} - -std::string ChannelsFund(uint64_t txfee,int64_t funds) -{ - CMutableTransaction mtx; CPubKey mypk,Channelspk; CScript opret; struct CCcontract_info *cp,C; + } cp = CCinit(&C,EVAL_CHANNELS); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); - Channelspk = GetUnspendable(cp,0); - if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) + funds = numpayments * payment; + if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,funds,Channelspk)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + endiancpy(hash,&hentropy,32); + for (i=0; i=0; i--) - dest[j++] = src[i]; -#else - memcpy(dest,src,len); -#endif -} - CPubKey DiceFundingPk(CScript scriptPubKey) { CPubKey pk; uint8_t *ptr,*dest; int32_t i; diff --git a/src/deprecation.cpp b/src/deprecation.cpp index 73ff8046d..fa8b7c521 100644 --- a/src/deprecation.cpp +++ b/src/deprecation.cpp @@ -31,7 +31,7 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) { if (blocksToDeprecation == 0 || forceLogging) { auto msg = strprintf(_("This version has been deprecated as of block height %d."), DEPRECATION_HEIGHT) + " " + - _("You should upgrade to the latest version of Zcash."); + _("You should upgrade to the latest version of Komodo."); if (!disableDeprecation) { msg += " " + strprintf(_("To disable deprecation for this version, set %s%s."), "-disabledeprecation=", CLIENT_VERSION_STR); @@ -48,11 +48,11 @@ void EnforceNodeDeprecation(int nHeight, bool forceLogging) { if (disableDeprecation) { msg = strprintf(_("This version will be deprecated at block height %d."), DEPRECATION_HEIGHT) + " " + - _("You should upgrade to the latest version of Zcash."); + _("You should upgrade to the latest version of Komodo."); } else { msg = strprintf(_("This version will be deprecated at block height %d, and will automatically shut down."), DEPRECATION_HEIGHT) + " " + - _("You should upgrade to the latest version of Zcash.") + " " + + _("You should upgrade to the latest version of Komodo.") + " " + strprintf(_("To disable deprecation for this version, set %s%s."), "-disabledeprecation=", CLIENT_VERSION_STR); } diff --git a/src/init.cpp b/src/init.cpp index 18a1d38e6..1105cda31 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1590,7 +1590,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) InitWarning(msg); } else if (nLoadWalletRet == DB_TOO_NEW) - strErrors << _("Error loading wallet.dat: Wallet requires newer version of Zcash") << "\n"; + strErrors << _("Error loading wallet.dat: Wallet requires newer version of Komodo") << "\n"; else if (nLoadWalletRet == DB_NEED_REWRITE) { strErrors << _("Wallet needed to be rewritten: restart Zcash to complete") << "\n"; diff --git a/src/main.cpp b/src/main.cpp index bc409f635..315b80d42 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4186,8 +4186,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C else if ( ASSETCHAINS_STAKED != 0 && (i == (block.vtx.size() - 1)) && komodo_isPoS((CBlock *)&block) != 0 ) continue; Tx = tx; - if ( myAddtomempool(Tx) == false ) // can happen with out of order tx in block on resync - //if ( AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL) == false ) + if ( myAddtomempool(Tx) == false ) // happens with out of order tx in block on resync rejects++; } if ( rejects == 0 || rejects == lastrejects ) diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 7fc30a45d..cdff9aa86 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -375,7 +375,8 @@ static const CRPCCommand vRPCCommands[] = { "MofN", "mofnaddress", &mofnaddress, true }, /* Channels */ - { "channels", "channelsaddress", &channelsaddress, true }, + { "channels", "channelsaddress", &channelsaddress, true }, + { "channels", "channelsopen", &channelsopen, true }, /* Oracles */ { "oracles", "oraclesaddress", &oraclesaddress, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index adda67b19..ff2a8e4e0 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -229,6 +229,7 @@ extern UniValue pegsaddress(const UniValue& params, bool fHelp); extern UniValue triggersaddress(const UniValue& params, bool fHelp); extern UniValue paymentsaddress(const UniValue& params, bool fHelp); extern UniValue gatewaysaddress(const UniValue& params, bool fHelp); +extern UniValue channelsopen(const UniValue& params, bool fHelp); //extern UniValue tokenswapask(const UniValue& params, bool fHelp); //extern UniValue tokenfillswap(const UniValue& params, bool fHelp); diff --git a/src/sendalert.cpp b/src/sendalert.cpp index 3b7b53734..218465994 100644 --- a/src/sendalert.cpp +++ b/src/sendalert.cpp @@ -88,7 +88,7 @@ void ThreadSendAlert() // 4000 or higher will put the RPC into safe mode alert.nPriority = 4000; alert.strComment = ""; - alert.strStatusBar = "Your client version 1.0.10 has degraded networking behavior. Please update to the most recent version of Zcash (1.0.10-1 or later)."; + alert.strStatusBar = "Your client version 1.0.10 has degraded networking behavior. Please update to the most recent version of Komodo (1.0.10-1 or later)."; alert.strRPCError = alert.strStatusBar; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c69ab53be..40376b572 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4852,6 +4852,7 @@ int32_t ensure_CCrequirements() #include "../cc/CCfsm.h" #include "../cc/CCauction.h" #include "../cc/CClotto.h" +#include "../cc/CCchannels.h" UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { @@ -4882,6 +4883,26 @@ UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector destpubkey; CPubKey pk,pk2; char destaddr[64]; + cp = CCinit(&C,EVAL_CHANNELS); + if ( fHelp || params.size() != 1 ) + throw runtime_error("channelsaddress destpubkey\n"); + 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"); + destpubkey = ParseHex(params[0].get_str().c_str()); + pk = pubkey2pk(Mypubkey()); + pk2 = pubkey2pk(destpubkey); + result = CCaddress(cp,(char *)"Channels",pubkey); + result.push_back(Pair("otherpubkey", params[0].get_str())); + GetCCaddress1of2(cp,destaddr,pk,pk2); + result.push_back(Pair("sendaddr",destaddr)); + GetCCaddress1of2(cp,destaddr,pk2,pk); + result.push_back(Pair("recvaddr",destaddr)); + return(result); +} + UniValue oraclesaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; @@ -4960,19 +4981,6 @@ UniValue gatewaysaddress(const UniValue& params, bool fHelp) return(CCaddress(cp,(char *)"Gateways",pubkey)); } -UniValue channelsaddress(const UniValue& params, bool fHelp) -{ - struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_CHANNELS); - if ( fHelp || params.size() > 1 ) - throw runtime_error("channelsaddress [pubkey]\n"); - 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"); - if ( params.size() == 1 ) - pubkey = ParseHex(params[0].get_str().c_str()); - return(CCaddress(cp,(char *)"Channels",pubkey)); -} - UniValue mofnaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; @@ -5079,6 +5087,27 @@ UniValue tokenaddress(const UniValue& params, bool fHelp) return(CCaddress(cp,(char *)"Assets",pubkey)); } +UniValue channelsopen(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char destaddr[64]; int32_t numpayments,payment; std::vector destpub; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_CHANNELS); + if ( fHelp || params.size() != 3 ) + throw runtime_error("channelsopen destpubkey numpayments payment\n"); + 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"); + LOCK(cs_main); + destpub = ParseHex(params[0].get_str().c_str()); + numpayments = atoi(params[1].get_str().c_str()); + payment = atoi(params[2].get_str().c_str()); + hex = ChannelOpen(0,pubkey2pk(destpub),numpayments,payment); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else ERR_RESULT("couldnt create channelsopen transaction"); + return(result); +} + UniValue rewardscreatefunding(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; int64_t funds,APR,minseconds,maxseconds,mindeposit; std::string hex;