From 8138fad2cca32237ce6a259c3e94d069a8cebd86 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:39:37 -1100 Subject: [PATCH 001/233] Add entropy --- src/cc/dice.cpp | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0e6669964..a988ba1d2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -26,6 +26,25 @@ 2. and 3. can be done in mempool */ +uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) +{ + int32_t i; uint8_t tmpseed,txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256,tmpseed; + hentropy = zeriod; + ed25519_create_keypair(txidpub,txidpriv,txidseed); + Myprivkey((uint8_t *)&tmp256); + tmpseed = tmp256.GetHash(); + ed25519_create_keypair(mypub,mypriv,(uint8_t *)tmpseed); + ed25519_key_exchange(ssecret,txidpub,mypriv); + ed25519_key_exchange(ssecret2,mypub,txidpriv); + if ( memcmp(ssecret,ssecret2,32) == 0 ) + { + memcpy(&tmp256,ssecret,32); + entropy = tmp256.GetHash(); + hentropy = entropy.GetHash(); + } + return(hentropy); +} + uint64_t DiceCalc(uint64_t amount,uint256 txid,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) { /*uint64_t duration,reward = 0; @@ -61,14 +80,14 @@ uint8_t DecodeDiceFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,int64 return(0); } -CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid) +CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint256 hash) { CScript opret; uint8_t evalcode = EVAL_DICE; - opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid); + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid << hash); return(opret); } -uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid) +uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) { std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,forfeitblocks; GetOpReturnData(scriptPubKey, vopret); @@ -81,11 +100,12 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits { if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> forfeitblocks) != 0 ) { + hash = zeroid; fundingtxid = txid; return('F'); } else fprintf(stderr,"unmarshal error for F\n"); } - else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid) != 0 ) + else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) { if ( e == EVAL_DICE && (f == 'L' || f == 'U' || f == 'A') ) return(f); @@ -393,7 +413,7 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); @@ -413,8 +433,9 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 sbits = stringbits(planstr); if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('A',sbits,fundingtxid))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('A',sbits,fundingtxid,hentropy))); } else fprintf(stderr,"cant find enough inputs\n"); fprintf(stderr,"cant find fundingtxid\n"); return(0); @@ -422,7 +443,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropy,hentropy; struct CCcontract_info *cp,C; if ( bet < 0 || odds < 1 ) { fprintf(stderr,"negative parameter error\n"); @@ -448,9 +469,10 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet { if ( AddNormalinputs(mtx,mypk,bet+2*txfee,64) > 0 ) { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('L',sbits,fundingtxid))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('L',sbits,fundingtxid,hentropy))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)bet/COIN); } fprintf(stderr,"cant find dice inputs\n"); @@ -459,7 +481,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; @@ -500,9 +522,12 @@ std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 if ( inputs >= (reward + 2*txfee) ) CCchange = (inputs - (reward + txfee)); fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN); + if ( houseflag != 0 ) + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + else hentropy = zeroid; mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey)); - return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeDiceOpRet('U',sbits,fundingtxid))); + return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeDiceOpRet('U',sbits,fundingtxid,hentropy))); } fprintf(stderr,"cant find enough dice inputs\n"); } From 5db44affffa0d18e10c672bc96b6dd98ed726d0d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:44:08 -1100 Subject: [PATCH 002/233] Test --- src/cc/dice.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a988ba1d2..cc8916813 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -26,21 +26,22 @@ 2. and 3. can be done in mempool */ -uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) +void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); + +uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU { - int32_t i; uint8_t tmpseed,txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256,tmpseed; + int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256,tmpseed; hentropy = zeriod; ed25519_create_keypair(txidpub,txidpriv,txidseed); - Myprivkey((uint8_t *)&tmp256); - tmpseed = tmp256.GetHash(); - ed25519_create_keypair(mypub,mypriv,(uint8_t *)tmpseed); + Myprivkey(tmp256); + vcalc_sha256(0,tmpseed,tmp256,32); + ed25519_create_keypair(mypub,mypriv,tmpseed); ed25519_key_exchange(ssecret,txidpub,mypriv); ed25519_key_exchange(ssecret2,mypub,txidpriv); if ( memcmp(ssecret,ssecret2,32) == 0 ) { - memcpy(&tmp256,ssecret,32); - entropy = tmp256.GetHash(); - hentropy = entropy.GetHash(); + vcalc_sha256(0,(uint8_t *)&entropy,ssecret,32); + vcalc_sha256(0,(uint8_t *)&hentropy,(uint8_t *)&entropy,32); } return(hentropy); } @@ -481,6 +482,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { + int32_t houseflag = 1; CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_DICE); if ( txfee == 0 ) From 72362bbe12e2104c4b0b8d54c13d0bfcfb270977 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:47:43 -1100 Subject: [PATCH 003/233] Test --- src/cc/dice.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index cc8916813..ecd3df4ec 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -27,11 +27,13 @@ */ void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); +void ed25519_create_keypair(uint8_t *,uint8_t *,uint8_t *); +void ed25519_key_exchange(uint8_t *,uint8_t *,uint8_t *); uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU { - int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256,tmpseed; - hentropy = zeriod; + int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256; + memset(&hentropy,0,32); ed25519_create_keypair(txidpub,txidpriv,txidseed); Myprivkey(tmp256); vcalc_sha256(0,tmpseed,tmp256,32); @@ -101,7 +103,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits { if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> forfeitblocks) != 0 ) { - hash = zeroid; + memset(&hash,0,32); fundingtxid = txid; return('F'); } else fprintf(stderr,"unmarshal error for F\n"); @@ -165,7 +167,7 @@ bool DiceExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransaction & bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid,hashBlock; int64_t minbet,maxbet,maxodds,forfeitblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,forfeitblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -174,7 +176,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) else { txid = tx.GetHash(); - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) return eval->Invalid("cant find fundingtxid"); @@ -246,7 +248,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) // 'L' vs 'F' and 'A' uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; + char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -262,7 +264,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) @@ -287,7 +289,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract uint64_t DicePlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -297,7 +299,7 @@ uint64_t DicePlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,u vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { From 2e124d5618a1f4fa956019f29d98c7c22f861202 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:49:00 -1100 Subject: [PATCH 004/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ecd3df4ec..6e5da5a24 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -32,7 +32,7 @@ void ed25519_key_exchange(uint8_t *,uint8_t *,uint8_t *); uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU { - int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy,tmp256; + int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy; memset(&hentropy,0,32); ed25519_create_keypair(txidpub,txidpriv,txidseed); Myprivkey(tmp256); From ff96162112d3ad816a8decb3957697c90e2b7cac Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:49:56 -1100 Subject: [PATCH 005/233] Txidseed --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6e5da5a24..c913e1071 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -34,7 +34,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little end { int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy; memset(&hentropy,0,32); - ed25519_create_keypair(txidpub,txidpriv,txidseed); + ed25519_create_keypair(txidpub,txidpriv,(uint8_t *)txidseed); Myprivkey(tmp256); vcalc_sha256(0,tmpseed,tmp256,32); ed25519_create_keypair(mypub,mypriv,tmpseed); From f41b35abe442242bc837359e116862c386fa55ab Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 21:50:35 -1100 Subject: [PATCH 006/233] & --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c913e1071..ab6e9bdf7 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -30,11 +30,11 @@ void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uin void ed25519_create_keypair(uint8_t *,uint8_t *,uint8_t *); void ed25519_key_exchange(uint8_t *,uint8_t *,uint8_t *); -uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU +uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU and max 1 vout per txid used { int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy; memset(&hentropy,0,32); - ed25519_create_keypair(txidpub,txidpriv,(uint8_t *)txidseed); + ed25519_create_keypair(txidpub,txidpriv,(uint8_t *)&txidseed); Myprivkey(tmp256); vcalc_sha256(0,tmpseed,tmp256,32); ed25519_create_keypair(mypub,mypriv,tmpseed); From d176d2df4fbbce0b44ebf54c14db195b3a2576a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:10:31 -1100 Subject: [PATCH 007/233] Use my curve25519 and sha256 --- src/cc/CCinclude.h | 8 ++++++++ src/cc/dice.cpp | 30 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index b7186e317..1af6ef4d1 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -30,6 +30,8 @@ #define SMALLVAL 0.000000000000001 +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 CCcontract_info { @@ -79,4 +81,10 @@ void SetCCtxids(std::vector > &addressIndex uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout); +// curve25519 and sha256 +bits256 curve25519_shared(bits256 privkey,bits256 otherpub); +bits256 curve25519_basepoint9(); +bits256 curve25519(bits256 mysecret,bits256 basepoint); +void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); + #endif diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ab6e9bdf7..b54caaa24 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -26,25 +26,25 @@ 2. and 3. can be done in mempool */ -void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); -void ed25519_create_keypair(uint8_t *,uint8_t *,uint8_t *); -void ed25519_key_exchange(uint8_t *,uint8_t *,uint8_t *); - -uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU and max 1 vout per txid used +uint256 DiceHashEntropy(uint256 &entropy,uint256 txidpriv) // assumes little endian CPU and max 1 vout per txid used { - int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy; + int32_t i; bits256 tmp256,txidpub,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; memset(&hentropy,0,32); - ed25519_create_keypair(txidpub,txidpriv,(uint8_t *)&txidseed); - Myprivkey(tmp256); - vcalc_sha256(0,tmpseed,tmp256,32); - ed25519_create_keypair(mypub,mypriv,tmpseed); - ed25519_key_exchange(ssecret,txidpub,mypriv); - ed25519_key_exchange(ssecret2,mypub,txidpriv); - if ( memcmp(ssecret,ssecret2,32) == 0 ) + txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; + txidpub = curve25519(txidpriv,curve25519_basepoint9()); + + Myprivkey(tmp256.bytes); + vcalc_sha256(0,mypriv.bytes,tmp256.bytes,32); + mypriv.bytes[0] &= 0xf8, mypriv.bytes[31] &= 0x7f, mypriv.bytes[31] |= 0x40; + mypub = curve25519(mypriv,curve25519_basepoint9()); + + ssecret = curve25519_shared(txidpub,mypriv); + ssecret2 = curve25519_shared(mypub,txidpriv); + if ( memcmp(ssecret.bytes,ssecret2.bytes,32) == 0 ) { - vcalc_sha256(0,(uint8_t *)&entropy,ssecret,32); + vcalc_sha256(0,(uint8_t *)&entropy,ssecret.bytes,32); vcalc_sha256(0,(uint8_t *)&hentropy,(uint8_t *)&entropy,32); - } + } else fprintf(stderr,"shared secrets dont match\n"); return(hentropy); } From ae748eaf3f7fc584b825c130c2dfd080b6b9aa9c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:19:18 -1100 Subject: [PATCH 008/233] Endian fix --- src/cc/dice.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b54caaa24..d3be492d4 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -26,10 +26,24 @@ 2. and 3. can be done in mempool */ -uint256 DiceHashEntropy(uint256 &entropy,uint256 txidpriv) // assumes little endian CPU and max 1 vout per txid used +#include "../endian.h" + +void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) { - int32_t i; bits256 tmp256,txidpub,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; + 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 +} + +uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // assumes little endian and max 1 vout per txid used +{ + int32_t i; uint8_t _entropy[32],_hentropy[32]; bits256 tmp256,txidpub,txidpriv,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; memset(&hentropy,0,32); + endiancpy(txidpriv.bytes,&txidpriv,32); txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; txidpub = curve25519(txidpriv,curve25519_basepoint9()); @@ -42,8 +56,10 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 txidpriv) // assumes little end ssecret2 = curve25519_shared(mypub,txidpriv); if ( memcmp(ssecret.bytes,ssecret2.bytes,32) == 0 ) { - vcalc_sha256(0,(uint8_t *)&entropy,ssecret.bytes,32); - vcalc_sha256(0,(uint8_t *)&hentropy,(uint8_t *)&entropy,32); + vcalc_sha256(0,(uint8_t *)&_entropy,ssecret.bytes,32); + vcalc_sha256(0,(uint8_t *)&_hentropy,_entropy,32); + endiancpy(&entropy,_entropy,32); + endiancpy(&hentropy,_hentropy,32); } else fprintf(stderr,"shared secrets dont match\n"); return(hentropy); } From dfe4c146b9487f4a04a4659b10e782994504a7a2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:20:06 -1100 Subject: [PATCH 009/233] (uint8_t *) --- src/cc/dice.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d3be492d4..286c11b68 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -39,11 +39,11 @@ void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) #endif } -uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // assumes little endian and max 1 vout per txid used +uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per txid used { int32_t i; uint8_t _entropy[32],_hentropy[32]; bits256 tmp256,txidpub,txidpriv,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; memset(&hentropy,0,32); - endiancpy(txidpriv.bytes,&txidpriv,32); + endiancpy(txidpriv.bytes,(uint8_t *)&txidpriv,32); txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; txidpub = curve25519(txidpriv,curve25519_basepoint9()); @@ -58,8 +58,8 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // assumes little en { vcalc_sha256(0,(uint8_t *)&_entropy,ssecret.bytes,32); vcalc_sha256(0,(uint8_t *)&_hentropy,_entropy,32); - endiancpy(&entropy,_entropy,32); - endiancpy(&hentropy,_hentropy,32); + endiancpy((uint8_t *)&entropy,_entropy,32); + endiancpy((uint8_t *)&hentropy,_hentropy,32); } else fprintf(stderr,"shared secrets dont match\n"); return(hentropy); } From 840a96ca995ee5636d75faccb7eedd5f4d523217 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:24:20 -1100 Subject: [PATCH 010/233] Test --- src/cc/dice.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 286c11b68..85efe34fa 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -60,7 +60,16 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx vcalc_sha256(0,(uint8_t *)&_hentropy,_entropy,32); endiancpy((uint8_t *)&entropy,_entropy,32); endiancpy((uint8_t *)&hentropy,_hentropy,32); - } else fprintf(stderr,"shared secrets dont match\n"); + } + else + { + for (i=0; i<32; i++) + fprintf(stderr,"%02x",ssecret[i]); + fprintf(stderr," ssecret\n"); + for (i=0; i<32; i++) + fprintf(stderr,"%02x",ssecrets[i]); + fprintf(stderr," ssecret2 dont match\n"); + } return(hentropy); } From 774b8b44eae6db19f644e133c8c94e6eba0de9de Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:25:38 -1100 Subject: [PATCH 011/233] .bytes --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 85efe34fa..15d2999e4 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -64,10 +64,10 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx else { for (i=0; i<32; i++) - fprintf(stderr,"%02x",ssecret[i]); + fprintf(stderr,"%02x",ssecret.bytes[i]); fprintf(stderr," ssecret\n"); for (i=0; i<32; i++) - fprintf(stderr,"%02x",ssecrets[i]); + fprintf(stderr,"%02x",ssecrets.bytes[i]); fprintf(stderr," ssecret2 dont match\n"); } return(hentropy); From ac9816bb2562239be3fd0ae1e0da7405bdc3df58 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:26:15 -1100 Subject: [PATCH 012/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 15d2999e4..70311610f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -67,7 +67,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr,"%02x",ssecret.bytes[i]); fprintf(stderr," ssecret\n"); for (i=0; i<32; i++) - fprintf(stderr,"%02x",ssecrets.bytes[i]); + fprintf(stderr,"%02x",ssecret2.bytes[i]); fprintf(stderr," ssecret2 dont match\n"); } return(hentropy); From e0e655fec8928cfcc9e4e92e0b3252d3942b96f9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:28:09 -1100 Subject: [PATCH 013/233] _ --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 70311610f..f3cf2da61 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -43,7 +43,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx { int32_t i; uint8_t _entropy[32],_hentropy[32]; bits256 tmp256,txidpub,txidpriv,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; memset(&hentropy,0,32); - endiancpy(txidpriv.bytes,(uint8_t *)&txidpriv,32); + endiancpy(txidpriv.bytes,(uint8_t *)&_txidpriv,32); txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; txidpub = curve25519(txidpriv,curve25519_basepoint9()); From 17bc6201b13cb4101b0acc7bd936df4729b2c69b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:31:49 -1100 Subject: [PATCH 014/233] +print --- src/cc/dice.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f3cf2da61..9bf610a87 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -35,7 +35,7 @@ void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) for (i=31; i>=0; i--) dest[j++] = src[i]; #else - memcpy(dest,src,len); + memcpy(dest,src,len); #endif } @@ -48,9 +48,18 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx txidpub = curve25519(txidpriv,curve25519_basepoint9()); Myprivkey(tmp256.bytes); + for (i=0; i<32; i++) + fprintf(stderr,"%02x",tmp256.bytes[i]); + fprintf(stderr," tmp256\n"); vcalc_sha256(0,mypriv.bytes,tmp256.bytes,32); mypriv.bytes[0] &= 0xf8, mypriv.bytes[31] &= 0x7f, mypriv.bytes[31] |= 0x40; + for (i=0; i<32; i++) + fprintf(stderr,"%02x",mypriv.bytes[i]); + fprintf(stderr," mypriv\n"); mypub = curve25519(mypriv,curve25519_basepoint9()); + for (i=0; i<32; i++) + fprintf(stderr,"%02x",mypub.bytes[i]); + fprintf(stderr," mypub\n"); ssecret = curve25519_shared(txidpub,mypriv); ssecret2 = curve25519_shared(mypub,txidpriv); From c83be946a6aa352173e41f033639cf64d3d95506 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:36:05 -1100 Subject: [PATCH 015/233] Curve25519 --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9bf610a87..21f0147ac 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -61,8 +61,8 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr,"%02x",mypub.bytes[i]); fprintf(stderr," mypub\n"); - ssecret = curve25519_shared(txidpub,mypriv); - ssecret2 = curve25519_shared(mypub,txidpriv); + ssecret = curve25519(txidpub,mypriv); + ssecret2 = curve25519(mypub,txidpriv); if ( memcmp(ssecret.bytes,ssecret2.bytes,32) == 0 ) { vcalc_sha256(0,(uint8_t *)&_entropy,ssecret.bytes,32); From 91f217719f3213ef98cfca33f5cf367e7aabf600 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:38:34 -1100 Subject: [PATCH 016/233] Flip order --- src/cc/dice.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 21f0147ac..1aa058070 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -45,7 +45,13 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx memset(&hentropy,0,32); endiancpy(txidpriv.bytes,(uint8_t *)&_txidpriv,32); txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; + for (i=0; i<32; i++) + fprintf(stderr,"%02x",txidpriv.bytes[i]); + fprintf(stderr," txidpriv\n"); txidpub = curve25519(txidpriv,curve25519_basepoint9()); + for (i=0; i<32; i++) + fprintf(stderr,"%02x",txidpub.bytes[i]); + fprintf(stderr," txidpub\n"); Myprivkey(tmp256.bytes); for (i=0; i<32; i++) @@ -61,8 +67,8 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr,"%02x",mypub.bytes[i]); fprintf(stderr," mypub\n"); - ssecret = curve25519(txidpub,mypriv); - ssecret2 = curve25519(mypub,txidpriv); + ssecret = curve25519(mypriv,txidpub); + ssecret2 = curve25519(txidpriv,mypub); if ( memcmp(ssecret.bytes,ssecret2.bytes,32) == 0 ) { vcalc_sha256(0,(uint8_t *)&_entropy,ssecret.bytes,32); From 494b64e58ee20820bbfaa8889149a0918f4689f5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 22:40:38 -1100 Subject: [PATCH 017/233] -print --- src/cc/dice.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1aa058070..e7e2cc5e1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -45,27 +45,12 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx memset(&hentropy,0,32); endiancpy(txidpriv.bytes,(uint8_t *)&_txidpriv,32); txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; - for (i=0; i<32; i++) - fprintf(stderr,"%02x",txidpriv.bytes[i]); - fprintf(stderr," txidpriv\n"); txidpub = curve25519(txidpriv,curve25519_basepoint9()); - for (i=0; i<32; i++) - fprintf(stderr,"%02x",txidpub.bytes[i]); - fprintf(stderr," txidpub\n"); Myprivkey(tmp256.bytes); - for (i=0; i<32; i++) - fprintf(stderr,"%02x",tmp256.bytes[i]); - fprintf(stderr," tmp256\n"); vcalc_sha256(0,mypriv.bytes,tmp256.bytes,32); mypriv.bytes[0] &= 0xf8, mypriv.bytes[31] &= 0x7f, mypriv.bytes[31] |= 0x40; - for (i=0; i<32; i++) - fprintf(stderr,"%02x",mypriv.bytes[i]); - fprintf(stderr," mypriv\n"); mypub = curve25519(mypriv,curve25519_basepoint9()); - for (i=0; i<32; i++) - fprintf(stderr,"%02x",mypub.bytes[i]); - fprintf(stderr," mypub\n"); ssecret = curve25519(mypriv,txidpub); ssecret2 = curve25519(txidpriv,mypub); From 04cf9f607229e58246a9566d677a84cbc5de81e9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 23:25:31 -1100 Subject: [PATCH 018/233] dicebet --- src/cc/dice.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e7e2cc5e1..5ff29d368 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -135,7 +135,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) { - if ( e == EVAL_DICE && (f == 'L' || f == 'U' || f == 'A') ) + if ( e == EVAL_DICE && (f == 'B' || f == 'U' || f == 'A') ) return(f); else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } @@ -224,12 +224,12 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'A' sbits fundingtxid return eval->Invalid("unexpected DiceValidate for addfunding"); break; - case 'L': + case 'B': //vins.*: normal inputs //vout.0: CC vout for locked funds //vout.1: normal output to unlock address //vout.2: change - //vout.n-1: opreturn 'L' sbits fundingtxid + //vout.n-1: opreturn 'B' sbits fundingtxid return eval->Invalid("unexpected DiceValidate for lock"); break; case 'U': @@ -270,7 +270,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return(true); } -// 'L' vs 'F' and 'A' +// 'B' vs 'F' and 'A' uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; @@ -294,7 +294,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) continue; - else if ( fundsflag == 0 && (funcid != 'L' || tx.vout.size() < 4) ) + else if ( fundsflag == 0 && (funcid != 'B' || tx.vout.size() < 4) ) continue; if ( total != 0 && maxinputs != 0 ) { @@ -312,12 +312,13 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract return(totalinputs); } -uint64_t DicePlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) +uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout,first=0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); + entropyval = 0; for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { txid = it->first.txhash; @@ -329,7 +330,15 @@ uint64_t DicePlanFunds(uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,u if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) + { totalinputs += nValue; + if ( first == 0 && funcid == 'A' ) + { + entropytxid = txid; + entropyval = tx.vout[0].nValue; + first = 1; + } + } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } //else fprintf(stderr,"else case\n"); } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); @@ -471,7 +480,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropy,hentropy; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; if ( bet < 0 || odds < 1 ) { fprintf(stderr,"negative parameter error\n"); @@ -493,17 +502,21 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"Dice plan %s bet %.8f < minbet %.8f\n",planstr,(double)bet/COIN,(double)minbet/COIN); return(0); } - if ( (funding= DicePlanFunds(sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee ) + if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) { if ( AddNormalinputs(mtx,mypk,bet+2*txfee,64) > 0 ) { + mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('L',sbits,fundingtxid,hentropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,hentropy))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)bet/COIN); } - fprintf(stderr,"cant find dice inputs\n"); + if ( entropyval == 0 && funding != 0 ) + fprintf(stderr,"cant find dice entropy inputs\n"); + else fprintf(stderr,"cant find dice inputs\n"); return(0); } From 0d4cfd0b93b3735bba37da1c6d711d251134c28a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 29 Jul 2018 23:33:37 -1100 Subject: [PATCH 019/233] Test --- src/cc/dice.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 5ff29d368..1b708f705 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -219,18 +219,18 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) break; case 'A': //vins.*: normal inputs - //vout.0: CC vout for funding - //vout.1: normal change + //vout.0: CC vout for locked entropy funds + //vout.1: tag to owner address for entropy funds + //vout.2: normal change //vout.n-1: opreturn 'A' sbits fundingtxid return eval->Invalid("unexpected DiceValidate for addfunding"); break; case 'B': //vins.*: normal inputs - //vout.0: CC vout for locked funds + //vout.0: CC vout for locked entropy //vout.1: normal output to unlock address //vout.2: change //vout.n-1: opreturn 'B' sbits fundingtxid - return eval->Invalid("unexpected DiceValidate for lock"); break; case 'U': //vin.0: locked funds CC vout.0 from lock @@ -468,10 +468,11 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 return(0); } sbits = stringbits(planstr); - if ( AddNormalinputs(mtx,mypk,amount+txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('A',sbits,fundingtxid,hentropy))); } else fprintf(stderr,"cant find enough inputs\n"); fprintf(stderr,"cant find fundingtxid\n"); @@ -504,9 +505,9 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet } if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) { + mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); if ( AddNormalinputs(mtx,mypk,bet+2*txfee,64) > 0 ) { - mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); From 28b947b84eb69877067f9f63185e250290d67922 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:14:36 -1100 Subject: [PATCH 020/233] Test --- src/cc/dice.cpp | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1b708f705..24249300b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -135,7 +135,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) { - if ( e == EVAL_DICE && (f == 'B' || f == 'U' || f == 'A') ) + if ( e == EVAL_DICE && (f == 'B' || f == 'U' || f == 'E') ) return(f); else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } @@ -217,24 +217,30 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'F' sbits APR minseconds maxseconds mindeposit return eval->Invalid("unexpected DiceValidate for createfunding"); break; - case 'A': + case 'E': //vins.*: normal inputs //vout.0: CC vout for locked entropy funds //vout.1: tag to owner address for entropy funds //vout.2: normal change - //vout.n-1: opreturn 'A' sbits fundingtxid - return eval->Invalid("unexpected DiceValidate for addfunding"); + //vout.n-1: opreturn 'E' sbits fundingtxid hentropy + return eval->Invalid("unexpected DiceValidate for addfunding entropy"); break; case 'B': - //vins.*: normal inputs + //vin.0: entropy txid from house + //vins.1+: normal inputs //vout.0: CC vout for locked entropy - //vout.1: normal output to unlock address - //vout.2: change - //vout.n-1: opreturn 'B' sbits fundingtxid + //vout.1: CC vout for locked bet + //vout.2: tag for bettor's address + //vout.3: change + //vout.n-1: opreturn 'B' sbits fundingtxid entropy + // get house hentropy and its vin0.prevtxid, cmp vout1 to owner address + // if owneraddress is me, then validate hentropy and submit outcome tx + fprintf(stderr,"got bet txid\n"); + return eval->Invalid("dont confirm bet during debug"); break; case 'U': //vin.0: locked funds CC vout.0 from lock - //vin.1+: funding CC vout.0 from 'F' and 'A' and 'U' + //vin.1+: funding CC vout.0 from 'F' and 'E' and 'U' //vout.0: funding CC change //vout.1: normal output to unlock address //vout.n-1: opreturn 'U' sbits fundingtxid @@ -270,7 +276,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return(true); } -// 'B' vs 'F' and 'A' +// 'B' vs 'F' and 'E' uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; @@ -292,7 +298,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); - if ( fundsflag != 0 && funcid != 'F' && funcid != 'A' && funcid != 'U' ) + if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'U' ) continue; else if ( fundsflag == 0 && (funcid != 'B' || tx.vout.size() < 4) ) continue; @@ -332,7 +338,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) { totalinputs += nValue; - if ( first == 0 && funcid == 'A' ) + if ( first == 0 && funcid == 'E' ) { entropytxid = txid; entropyval = tx.vout[0].nValue; @@ -473,7 +479,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('A',sbits,fundingtxid,hentropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy))); } else fprintf(stderr,"cant find enough inputs\n"); fprintf(stderr,"cant find fundingtxid\n"); return(0); @@ -512,7 +518,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,hentropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)bet/COIN); } if ( entropyval == 0 && funding != 0 ) From 35d43add2942146e301af9f2d25cdc7fe2ea76e1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:33:58 -1100 Subject: [PATCH 021/233] Test --- src/cc/dice.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 24249300b..43b19ca6a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -338,6 +338,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) { totalinputs += nValue; + fprintf(stderr,"add %.8f\n",(double)nValue/COIN); if ( first == 0 && funcid == 'E' ) { entropytxid = txid; @@ -346,7 +347,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); - } //else fprintf(stderr,"else case\n"); + } else fprintf(stderr,"else case funcid %d\n",funcid); } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } From 1c87477b45b6161524400f261fd4c1090d30abec Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:54:08 -1100 Subject: [PATCH 022/233] Check for bet tx is mine --- src/cc/dice.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 43b19ca6a..edc25076a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -190,6 +190,14 @@ bool DiceExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransaction & else return(true); } +bool DiceIsmine(const CScript scriptPubKey) +{ + char destaddr[64],myaddr[64]; + Getscriptaddress(destaddr,scriptPubKey); + Getscriptaddress(myaddr,CScript() << Mypubkey() << OP_CHECKSIG) + return(strcmp(destaddr,myaddr) == 0); +} + bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,forfeitblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; @@ -236,6 +244,13 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) // get house hentropy and its vin0.prevtxid, cmp vout1 to owner address // if owneraddress is me, then validate hentropy and submit outcome tx fprintf(stderr,"got bet txid\n"); + if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("always should find vin.0, but didnt"); + else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 ) + { + fprintf(stderr,"I am house entropy %.8f\n",(double)vinTx.vout[0].nValue/COIN); + + } return eval->Invalid("dont confirm bet during debug"); break; case 'U': @@ -513,7 +528,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) { mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); - if ( AddNormalinputs(mtx,mypk,bet+2*txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,bet+2*txfee,60) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); From ca05cbaa40330a0f66e5d575dbb56c5924a988dc Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:54:55 -1100 Subject: [PATCH 023/233] ; --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index edc25076a..bd7c4d3be 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -194,7 +194,7 @@ bool DiceIsmine(const CScript scriptPubKey) { char destaddr[64],myaddr[64]; Getscriptaddress(destaddr,scriptPubKey); - Getscriptaddress(myaddr,CScript() << Mypubkey() << OP_CHECKSIG) + Getscriptaddress(myaddr,CScript() << Mypubkey() << OP_CHECKSIG); return(strcmp(destaddr,myaddr) == 0); } From 69d725fe7a58b3c4edb6e57b7811814df458ed4f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:59:14 -1100 Subject: [PATCH 024/233] Test --- src/cc/dice.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index bd7c4d3be..dfc3074e5 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -246,10 +246,13 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 ) + else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { - fprintf(stderr,"I am house entropy %.8f\n",(double)vinTx.vout[0].nValue/COIN); - + uint64_t vinbits; uint256 vinfundingtxid,hentropy; char str[65]; + if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) + { + fprintf(stderr,"I am house entropy %.8f hentropy.(%s)\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy)); + } } return eval->Invalid("dont confirm bet during debug"); break; From da297d0598e126f29079e287ea0c026e67ff0dd6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 00:59:58 -1100 Subject: [PATCH 025/233] Fix --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index dfc3074e5..03c6eaf5c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -248,8 +248,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return eval->Invalid("always should find vin.0, but didnt"); else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { - uint64_t vinbits; uint256 vinfundingtxid,hentropy; char str[65]; - if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) + uint64_t vinsbits; uint256 vinfundingtxid,hentropy; char str[65]; + if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { fprintf(stderr,"I am house entropy %.8f hentropy.(%s)\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy)); } From c299a00ff78ce1c44fc657dafa00def9e8761c22 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:03:12 -1100 Subject: [PATCH 026/233] hentropy2 --- src/cc/dice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 03c6eaf5c..b8b901159 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -248,10 +248,11 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return eval->Invalid("always should find vin.0, but didnt"); else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { - uint64_t vinsbits; uint256 vinfundingtxid,hentropy; char str[65]; + uint64_t vinsbits; uint256 vinfundingtxid,hentropy,hentropy2,entropy; char str[65],str2[65]; if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { - fprintf(stderr,"I am house entropy %.8f hentropy.(%s)\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy)); + hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevhash); + fprintf(stderr,"I am house entropy %.8f hentropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy),uint256_str(str,hentropy2)); } } return eval->Invalid("dont confirm bet during debug"); From 93235ec9207a7bd86d5f3e079fb91b056f051a10 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:04:33 -1100 Subject: [PATCH 027/233] prevout.hash --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b8b901159..f65f290d2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -251,7 +251,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) uint64_t vinsbits; uint256 vinfundingtxid,hentropy,hentropy2,entropy; char str[65],str2[65]; if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { - hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevhash); + hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); fprintf(stderr,"I am house entropy %.8f hentropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy),uint256_str(str,hentropy2)); } } From a806f2fcdb17a88834c5972480a83c4ad62c9067 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:06:56 -1100 Subject: [PATCH 028/233] Test --- src/cc/dice.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f65f290d2..6ffaeddeb 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -252,7 +252,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); - fprintf(stderr,"I am house entropy %.8f hentropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,hentropy),uint256_str(str,hentropy2)); + if ( hentropy == hentropy2 ) + { + fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash)); + } } } return eval->Invalid("dont confirm bet during debug"); From 1702dceff370bfcefeecc52d70e1fd978c96f889 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:13:05 -1100 Subject: [PATCH 029/233] CCaddress fixes --- src/wallet/rpcwallet.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 0bc73e9f1..aaa0f2a8e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4850,12 +4850,12 @@ int32_t ensure_CCrequirements() UniValue CCaddress(struct CCcontract_info *cp,char *name,std::vector &pubkey) { - UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64],marker[64]; + UniValue result(UniValue::VOBJ); ; char destaddr[64],str[64]; result.push_back(Pair("result", "success")); sprintf(str,"%sCCaddress",name); - sprintf(marker,"%smarker",name); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) - result.push_back(Pair(str,destaddr)); + result.push_back(Pair(str,cp->unspendableCCaddr)); + sprintf(str,"%smarker",name); + result.push_back(Pair(str,cp->normaladdr)); if ( pubkey.size() == 33 ) { if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) From 98c6e2e1342bdc3c3debcbaf3bdd52609f5cbe42 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:21:56 -1100 Subject: [PATCH 030/233] Test --- src/cc/dice.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6ffaeddeb..728aabb80 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -73,18 +73,12 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx return(hentropy); } -uint64_t DiceCalc(uint64_t amount,uint256 txid,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) +uint64_t DiceCalc(int64_t amount,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks,uint256 houseentropy,uint256 bettorentropy) { - /*uint64_t duration,reward = 0; - if ( (duration= CCduration(txid)) < minseconds ) - { + if ( odds < 10000 ) return(0); - //duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); - } else if ( duration > maxseconds ) - maxseconds = duration; - reward = (((amount * APR) / COIN) * duration) / (365*24*3600LL * 100); - fprintf(stderr,"amount %.8f %.8f %llu -> duration.%llu reward %.8f\n",(double)amount/COIN,((double)amount * APR)/COIN,(long long)((amount * APR) / (COIN * 365*24*3600)),(long long)duration,(double)reward/COIN); - return(reward);*/ + else odds -= 10000; + fprintf(stderr,"bet %.8f at %d odds\n",(double)amount/COIN,(int32_t)odds); return(0); } @@ -238,7 +232,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vins.1+: normal inputs //vout.0: CC vout for locked entropy //vout.1: CC vout for locked bet - //vout.2: tag for bettor's address + //vout.2: tag for bettor's address (txfee + odds) //vout.3: change //vout.n-1: opreturn 'B' sbits fundingtxid entropy // get house hentropy and its vin0.prevtxid, cmp vout1 to owner address @@ -254,6 +248,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) { + winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,forfeitblocks,entropy,hash); fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash)); } } @@ -286,7 +281,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) else if ( tx.vout[1].scriptPubKey != vinTx.vout[1].scriptPubKey ) return eval->Invalid("unlock tx vout.1 mismatched scriptPubKey"); amount = vinTx.vout[0].nValue; - reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks); + reward = 0; + //reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks); if ( tx.vout[1].nValue > amount+reward ) return eval->Invalid("unlock tx vout.1 isnt amount+reward"); preventCCvouts = 1; @@ -535,12 +531,12 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) { mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); - if ( AddNormalinputs(mtx,mypk,bet+2*txfee,60) > 0 ) + if ( AddNormalinputs(mtx,mypk,bet+2*txfee+odds,60) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(txfee+odds,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)bet/COIN); } @@ -587,7 +583,8 @@ std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 return(0); } } - if ( amount > 0 && (reward= DiceCalc(amount,mtx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks)) > txfee && scriptPubKey.size() > 0 ) + reward = 0;//DiceCalc(amount,mtx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks); + if ( amount > 0 && reward > txfee && scriptPubKey.size() > 0 ) { if ( (inputs= AddDiceInputs(ignore,1,cp,mtx,dicepk,reward+txfee,30)) > 0 ) { From 77add0c17ea1c1bca5091088abd9344d49e7e135 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:22:37 -1100 Subject: [PATCH 031/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 728aabb80..af0fdafe1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -242,7 +242,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return eval->Invalid("always should find vin.0, but didnt"); else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { - uint64_t vinsbits; uint256 vinfundingtxid,hentropy,hentropy2,entropy; char str[65],str2[65]; + uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; char str[65],str2[65]; if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); From be7a410a67bd495a3b020c7a448d21894948d219 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:35:06 -1100 Subject: [PATCH 032/233] Test --- src/cc/dice.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index af0fdafe1..0bb93bcf9 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -73,12 +73,29 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx return(hentropy); } -uint64_t DiceCalc(int64_t amount,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks,uint256 houseentropy,uint256 bettorentropy) +uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks,uint256 houseentropy,uint256 bettorentropy) { + uint8_t buf[64],_house[32],_bettor[32]; arith_uint256 house,bettor; char str[65],str2[65]; if ( odds < 10000 ) return(0); else odds -= 10000; - fprintf(stderr,"bet %.8f at %d odds\n",(double)amount/COIN,(int32_t)odds); + if ( bet < minbet || bet > maxbet || odds > maxodds ) + { + fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); + return(0); + } + endiancpy(buf,(uint8_t *)&houseentropy,32); + endiancpy(&buf[32],(uint8_t *)&bettorentropy,32); + vcalc_sha256(0,(uint8_t *)&_house,buf,64); + endiancpy(&house,_house,32); + + endiancpy(buf,(uint8_t *)&bettorentropy,32); + endiancpy(&buf[32],(uint8_t *)&houseentropy,32); + vcalc_sha256(0,(uint8_t *)&_house,buf,64); + endiancpy(&bettor,_bettor,32); + + + fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,house),uint256_str(str2,bettor)); return(0); } @@ -249,7 +266,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,forfeitblocks,entropy,hash); - fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash)); + fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); } } } @@ -523,9 +540,9 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); return(0); } - if ( bet < minbet ) + if ( bet < minbet || bet > maxbet || odds > maxodds ) { - fprintf(stderr,"Dice plan %s bet %.8f < minbet %.8f\n",planstr,(double)bet/COIN,(double)minbet/COIN); + fprintf(stderr,"Dice plan %s illegal bet %.8f: minbet %.8f maxbet %.8f or odds %d vs max.%d\n",planstr,(double)bet/COIN,(double)minbet/COIN,(double)maxbet/COIN,(int32_t)odds,(int32_t)maxodds); return(0); } if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) From 208f0e7abc7884ecde5b2f69159b985b49591fec Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:36:17 -1100 Subject: [PATCH 033/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0bb93bcf9..986f6f715 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -87,12 +87,12 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t endiancpy(buf,(uint8_t *)&houseentropy,32); endiancpy(&buf[32],(uint8_t *)&bettorentropy,32); vcalc_sha256(0,(uint8_t *)&_house,buf,64); - endiancpy(&house,_house,32); + endiancpy((uint8_t *)&house,_house,32); endiancpy(buf,(uint8_t *)&bettorentropy,32); endiancpy(&buf[32],(uint8_t *)&houseentropy,32); vcalc_sha256(0,(uint8_t *)&_house,buf,64); - endiancpy(&bettor,_bettor,32); + endiancpy((uint8_t *)&bettor,_bettor,32); fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,house),uint256_str(str2,bettor)); From 2616416c1b7eda43d90d31a5b9f8518b1e35f8bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:37:45 -1100 Subject: [PATCH 034/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 986f6f715..0d19c0b83 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -95,7 +95,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t endiancpy((uint8_t *)&bettor,_bettor,32); - fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,house),uint256_str(str2,bettor)); + fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,(uint256 *)&house),uint256_str(str2,(uint256 *)&bettor)); return(0); } From 8a2eeb6b2b515130ee5706688d9861af5330046c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:38:42 -1100 Subject: [PATCH 035/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0d19c0b83..cf7d62a20 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -95,7 +95,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t endiancpy((uint8_t *)&bettor,_bettor,32); - fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,(uint256 *)&house),uint256_str(str2,(uint256 *)&bettor)); + fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); return(0); } From b52f7685db892e494ebc7039c2c7ec6b4732af78 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:42:52 -1100 Subject: [PATCH 036/233] Adjust bettor /= odds --- src/cc/dice.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index cf7d62a20..4c74c6057 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -75,7 +75,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks,uint256 houseentropy,uint256 bettorentropy) { - uint8_t buf[64],_house[32],_bettor[32]; arith_uint256 house,bettor; char str[65],str2[65]; + uint8_t buf[64],_house[32],_bettor[32]; uint64_t winnings; arith_uint256 house,bettor; char str[65],str2[65]; if ( odds < 10000 ) return(0); else odds -= 10000; @@ -93,8 +93,11 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t endiancpy(&buf[32],(uint8_t *)&houseentropy,32); vcalc_sha256(0,(uint8_t *)&_house,buf,64); endiancpy((uint8_t *)&bettor,_bettor,32); - - + if ( odds > 1 ) + bettor = (bettor / arith_uint256(odds)); + if ( bettor >= house ) + winnings = bet * odds; + else winnings = 0; fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); return(0); } From 5d3d3a8712ad5b3a7542231cb6c29326c597a953 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:51:32 -1100 Subject: [PATCH 037/233] test --- src/cc/CCdice.h | 2 ++ src/cc/dice.cpp | 76 +++++++++++++++++++++++++++++++++++++++- src/wallet/rpcwallet.cpp | 38 ++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 23adb0ffb..236a8652b 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,6 +24,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); +std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); +std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 4c74c6057..6d113b54a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -269,12 +269,24 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,forfeitblocks,entropy,hash); - fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); + //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); + if ( winnings == 0 ) + { + // queue 'L' losing tx + } + else + { + // queue 'W' winning tx + } } } } return eval->Invalid("dont confirm bet during debug"); break; + case 'L': + break; + case 'W': + break; case 'U': //vin.0: locked funds CC vout.0 from lock //vin.1+: funding CC vout.0 from 'F' and 'E' and 'U' @@ -524,6 +536,68 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 return(0); } +std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + sbits = stringbits(planstr); + if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) + { + fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + return(0); + } + sbits = stringbits(planstr); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + +std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + cp = CCinit(&C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + sbits = stringbits(planstr); + if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) + { + fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + return(0); + } + sbits = stringbits(planstr); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index aaa0f2a8e..97ade0db9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5169,6 +5169,44 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } +UniValue dicewinner(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("dicewinner name fundingtxid bettxid\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"); + name = (char *)params[0].get_str().c_str(); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + bettxid = Parseuint256((char *)params[2].get_str().c_str()); + hex = DiceWinner(0,name,fundingtxid,bettxid); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + return(result); +} + +UniValue diceloser(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("diceloser name fundingtxid bettxid\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"); + name = (char *)params[0].get_str().c_str(); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + bettxid = Parseuint256((char *)params[2].get_str().c_str()); + hex = DiceLoser(0,name,fundingtxid,bettxid); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + return(result); +} + UniValue dicelist(const UniValue& params, bool fHelp) { uint256 tokenid; From 2317fb0aabf11007550d6970564826942b59dd4e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:52:25 -1100 Subject: [PATCH 038/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6d113b54a..774d27006 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -538,7 +538,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); @@ -569,7 +569,7 @@ std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); From 117f64ab825104006c968ee02b22b0dce0d21d66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:53:51 -1100 Subject: [PATCH 039/233] Dice winner and dice loser --- src/rpcserver.cpp | 2 ++ src/rpcserver.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 580a050c3..9e911d210 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -373,6 +373,8 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicefund", &dicefund, true }, { "dice", "diceaddfunds", &diceaddfunds, true }, { "dice", "dicebet", &dicebet, true }, + { "dice", "dicewinner", &dicewinner, true }, + { "dice", "diceloser", &diceloser, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ diff --git a/src/rpcserver.h b/src/rpcserver.h index 4da7c73c0..4423df345 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -239,6 +239,8 @@ extern UniValue dicelist(const UniValue& params, bool fHelp); extern UniValue diceinfo(const UniValue& params, bool fHelp); extern UniValue diceaddfunds(const UniValue& params, bool fHelp); extern UniValue dicebet(const UniValue& params, bool fHelp); +extern UniValue dicewinner(const UniValue& params, bool fHelp); +extern UniValue diceloser(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); extern UniValue ponziaddress(const UniValue& params, bool fHelp); extern UniValue auctionaddress(const UniValue& params, bool fHelp); From 2c42e02c628b31e8e9a80bd03d2d203f9544236b Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 01:54:24 -1100 Subject: [PATCH 040/233] Test --- src/wallet/rpcwallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 97ade0db9..370ca9abe 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5171,7 +5171,7 @@ UniValue dicebet(const UniValue& params, bool fHelp) UniValue dicewinner(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; if ( fHelp || params.size() != 3 ) throw runtime_error("dicewinner name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5190,7 +5190,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) UniValue diceloser(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; if ( fHelp || params.size() != 3 ) throw runtime_error("diceloser name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) From 05177cfb4eddf6197cd462e8dfbe69ba6556aabc Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 02:08:32 -1100 Subject: [PATCH 041/233] Diceinit fund --- src/cc/dice.cpp | 82 +++++++++++++--------------------------- src/wallet/rpcwallet.cpp | 2 +- 2 files changed, 27 insertions(+), 57 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 774d27006..363760859 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -476,15 +476,10 @@ UniValue DiceList() return(result); } -std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) +struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubkey &dicepk,uint64_t &sbits) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; - if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || forfeitblocks < 0 || forfeitblocks > 1440 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - cp = CCinit(&C,EVAL_DICE); + int64_t a,b,c,d; + cp = CCinit(C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -495,6 +490,19 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t fprintf(stderr,"Dice plan (%s) already exists\n",planstr); return(0); } + return(cp); +} + +std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) +{ + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || forfeitblocks < 0 || forfeitblocks > 1440 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + return(0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,dicepk)); @@ -507,24 +515,14 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - cp = CCinit(&C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - dicepk = GetUnspendable(cp,0); - sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) - { - fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); - } - sbits = stringbits(planstr); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); @@ -538,24 +536,14 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; uint64_t amount = 0; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - cp = CCinit(&C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - dicepk = GetUnspendable(cp,0); - sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) - { - fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); - } - sbits = stringbits(planstr); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); @@ -569,24 +557,14 @@ std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; int64_t a,b,c,d; struct CCcontract_info *cp,C; uint64_t amount = 0; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - cp = CCinit(&C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - dicepk = GetUnspendable(cp,0); - sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) == 0 ) - { - fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); - } - sbits = stringbits(planstr); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); @@ -606,12 +584,8 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"negative parameter error\n"); return(0); } - cp = CCinit(&C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - dicepk = GetUnspendable(cp,0); - sbits = stringbits(planstr); + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + return(0); if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,forfeitblocks) == 0 ) { fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); @@ -644,12 +618,8 @@ std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 { int32_t houseflag = 1; CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - dicepk = GetUnspendable(cp,0); - mypk = pubkey2pk(Mypubkey()); - sbits = stringbits(planstr); + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + return(0); if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,forfeitblocks) == 0 ) { fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 370ca9abe..90d63a83b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5203,7 +5203,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) { result.push_back(Pair("result", "success")); result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + } else result.push_back(Pair("error", "couldnt create diceloser transaction")); return(result); } From f1dd27eb25af6df57f7b901a68a1c8fd53012aa0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 02:09:57 -1100 Subject: [PATCH 042/233] K --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 363760859..33fc73888 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -476,7 +476,7 @@ UniValue DiceList() return(result); } -struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubkey &dicepk,uint64_t &sbits) +struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits) { int64_t a,b,c,d; cp = CCinit(C,EVAL_DICE); From 730f13dc063710211ffb840032c43a8159bcd4f5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 02:10:37 -1100 Subject: [PATCH 043/233] struct CCcontract_info *cp --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 33fc73888..439ded4ef 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -478,7 +478,7 @@ UniValue DiceList() struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits) { - int64_t a,b,c,d; + int64_t a,b,c,d; struct CCcontract_info *cp; cp = CCinit(C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; From 544593c6ab9a2e5eebff0c010467e97474e8ec01 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 02:14:52 -1100 Subject: [PATCH 044/233] Dice refund --- src/cc/CCdice.h | 1 + src/cc/dice.cpp | 21 +++++++++++++++++++++ src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + src/wallet/rpcwallet.cpp | 19 +++++++++++++++++++ 5 files changed, 43 insertions(+) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 236a8652b..79bd31d1c 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -25,6 +25,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); +std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 439ded4ef..e1c3dc1eb 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -576,6 +576,27 @@ std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 b return(0); } +std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + return(0); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 9e911d210..9790af78b 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -375,6 +375,7 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicebet", &dicebet, true }, { "dice", "dicewinner", &dicewinner, true }, { "dice", "diceloser", &diceloser, true }, + { "dice", "dicerefund", &dicerefund, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ diff --git a/src/rpcserver.h b/src/rpcserver.h index 4423df345..b7aa32044 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -241,6 +241,7 @@ extern UniValue diceaddfunds(const UniValue& params, bool fHelp); extern UniValue dicebet(const UniValue& params, bool fHelp); extern UniValue dicewinner(const UniValue& params, bool fHelp); extern UniValue diceloser(const UniValue& params, bool fHelp); +extern UniValue dicerefund(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); extern UniValue ponziaddress(const UniValue& params, bool fHelp); extern UniValue auctionaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 90d63a83b..629b9409e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5207,6 +5207,25 @@ UniValue diceloser(const UniValue& params, bool fHelp) return(result); } +UniValue dicerefund(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; + if ( fHelp || params.size() != 3 ) + throw runtime_error("dicerefund name fundingtxid bettxid\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"); + name = (char *)params[0].get_str().c_str(); + fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); + bettxid = Parseuint256((char *)params[2].get_str().c_str()); + hex = DiceRefund(0,name,fundingtxid,bettxid); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create diceloser transaction")); + return(result); +} + UniValue dicelist(const UniValue& params, bool fHelp) { uint256 tokenid; From 9025093e5a36345f8d28ba547df7f9d584df83f1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:18:11 -1100 Subject: [PATCH 045/233] Relax vout constraint --- src/cc/CCdice.h | 2 +- src/cc/assets.cpp | 2 +- src/cc/dice.cpp | 61 +++++++++++++++++++++------------------- src/wallet/rpcwallet.cpp | 8 +++--- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 79bd31d1c..e539d301b 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -27,7 +27,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); -std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks); +std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); UniValue DiceList(); diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 1deb330f9..764724086 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -261,7 +261,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] if ( remaining_price == 0 ) return eval->Invalid("illegal null remaining_price for selloffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,inputs) == 0 ) + else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); preventCCvouts = 1; diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e1c3dc1eb..7003a15a1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -18,12 +18,15 @@ /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. - 1. CC vout locks "house" funds with hash(entropy) + half of shared secret + 1. CC vout locks "house" funds with hash(entropy) 2. bettor submits bet, with entropy, odds, houseid and sends combined amount into another CC vout. - 3. house account sends funds to winner with proof of entropy - 4. if timeout, bettor wins funds + 3. house account sends funds to winner/loser with proof of entropy + 4. if timeout, bettor gets refund 2. and 3. can be done in mempool + + The house commits to an entropy value by including the hash of the entropy value in the 'E' transaction. + */ #include "../endian.h" @@ -73,7 +76,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx return(hentropy); } -uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks,uint256 houseentropy,uint256 bettorentropy) +uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 houseentropy,uint256 bettorentropy) { uint8_t buf[64],_house[32],_bettor[32]; uint64_t winnings; arith_uint256 house,bettor; char str[65],str2[65]; if ( odds < 10000 ) @@ -102,19 +105,19 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t return(0); } -CScript EncodeDiceFundingOpRet(uint8_t funcid,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) +CScript EncodeDiceFundingOpRet(uint8_t funcid,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { CScript opret; uint8_t evalcode = EVAL_DICE; - opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'F' << sbits << minbet << maxbet << maxodds << forfeitblocks); + opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'F' << sbits << minbet << maxbet << maxodds << timeoutblocks); return(opret); } -uint8_t DecodeDiceFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &forfeitblocks) +uint8_t DecodeDiceFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { std::vector vopret; uint8_t *script,e,f; GetOpReturnData(scriptPubKey, vopret); script = (uint8_t *)vopret.data(); - if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> forfeitblocks) != 0 ) + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> timeoutblocks) != 0 ) { if ( e == EVAL_DICE && f == 'F' ) return(f); @@ -131,7 +134,7 @@ CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint25 uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) { - std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,forfeitblocks; + std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,timeoutblocks; GetOpReturnData(scriptPubKey, vopret); if ( vopret.size() > 2 ) { @@ -140,7 +143,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits { if ( script[1] == 'F' ) { - if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> forfeitblocks) != 0 ) + if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> minbet; ss >> maxbet; ss >> maxodds; ss >> timeoutblocks) != 0 ) { memset(&hash,0,32); fundingtxid = txid; @@ -214,7 +217,7 @@ bool DiceIsmine(const CScript scriptPubKey) bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,forfeitblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -227,7 +230,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) return eval->Invalid("cant find fundingtxid"); - else if ( fundingTx.vout.size() > 0 && DecodeDiceFundingOpRet(fundingTx.vout[fundingTx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,forfeitblocks) != 'F' ) + else if ( fundingTx.vout.size() > 0 && DecodeDiceFundingOpRet(fundingTx.vout[fundingTx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) != 'F' ) return eval->Invalid("fundingTx not valid"); switch ( funcid ) { @@ -268,7 +271,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) { - winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,forfeitblocks,entropy,hash); + winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,hash); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { @@ -314,7 +317,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return eval->Invalid("unlock tx vout.1 mismatched scriptPubKey"); amount = vinTx.vout[0].nValue; reward = 0; - //reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks); + //reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,timeoutblocks); if ( tx.vout[1].nValue > amount+reward ) return eval->Invalid("unlock tx vout.1 isnt amount+reward"); preventCCvouts = 1; @@ -404,7 +407,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi return(totalinputs); } -bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &forfeitblocks) +bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { char CCaddr[64]; uint64_t sbits; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; @@ -416,7 +419,7 @@ bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk, txid = it->first.txhash; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) { - if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,forfeitblocks) == 'F' ) + if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' ) { if ( sbits == refsbits ) return(true); @@ -428,14 +431,14 @@ bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk, UniValue DiceInfo(uint256 diceid) { - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t minbet,maxbet,maxodds,forfeitblocks; uint64_t sbits; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char str[67],numstr[65]; if ( GetTransaction(diceid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find fundingtxid\n"); result.push_back(Pair("error","cant find fundingtxid")); return(result); } - if ( vintx.vout.size() > 0 && DecodeDiceFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,forfeitblocks) == 0 ) + if ( vintx.vout.size() > 0 && DecodeDiceFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 0 ) { fprintf(stderr,"fundingtxid isnt dice creation txid\n"); result.push_back(Pair("error","fundingtxid isnt dice creation txid")); @@ -451,7 +454,7 @@ UniValue DiceInfo(uint256 diceid) sprintf(numstr,"%.8f",(double)maxbet/COIN); result.push_back(Pair("maxbet",numstr)); result.push_back(Pair("maxodds",maxodds)); - result.push_back(Pair("forfeitblocks",forfeitblocks)); + result.push_back(Pair("timeoutblocks",timeoutblocks)); sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); result.push_back(Pair("funding",numstr)); return(result); @@ -459,7 +462,7 @@ UniValue DiceInfo(uint256 diceid) UniValue DiceList() { - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; uint64_t sbits; int64_t minbet,maxbet,maxodds,forfeitblocks; char str[65]; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint256 txid,hashBlock; CTransaction vintx; uint64_t sbits; int64_t minbet,maxbet,maxodds,timeoutblocks; char str[65]; cp = CCinit(&C,EVAL_DICE); SetCCtxids(addressIndex,cp->normaladdr); for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) @@ -467,7 +470,7 @@ UniValue DiceList() txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( vintx.vout.size() > 0 && DecodeDiceFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,forfeitblocks) != 0 ) + if ( vintx.vout.size() > 0 && DecodeDiceFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) != 0 ) { result.push_back(uint256_str(str,txid)); } @@ -493,10 +496,10 @@ struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_ return(cp); } -std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t forfeitblocks) +std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; - if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || forfeitblocks < 0 || forfeitblocks > 1440 ) + if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); return(0); @@ -507,7 +510,7 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(dicepk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceFundingOpRet('F',sbits,minbet,maxbet,maxodds,forfeitblocks))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceFundingOpRet('F',sbits,minbet,maxbet,maxodds,timeoutblocks))); } fprintf(stderr,"cant find enough inputs\n"); return(0); @@ -599,7 +602,7 @@ std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,forfeitblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; if ( bet < 0 || odds < 1 ) { fprintf(stderr,"negative parameter error\n"); @@ -607,7 +610,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet } if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); - if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,forfeitblocks) == 0 ) + if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) == 0 ) { fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); return(0); @@ -638,10 +641,10 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { int32_t houseflag = 1; - CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,forfeitblocks; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,timeoutblocks; struct CCcontract_info *cp,C; if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); - if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,forfeitblocks) == 0 ) + if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) == 0 ) { fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); return(0); @@ -668,7 +671,7 @@ std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 return(0); } } - reward = 0;//DiceCalc(amount,mtx.vin[0].prevout.hash,minbet,maxbet,maxodds,forfeitblocks); + reward = 0;//DiceCalc(amount,mtx.vin[0].prevout.hash,minbet,maxbet,maxodds,timeoutblocks); if ( amount > 0 && reward > txfee && scriptPubKey.size() > 0 ) { if ( (inputs= AddDiceInputs(ignore,1,cp,mtx,dicepk,reward+txfee,30)) > 0 ) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 629b9409e..4d20ffb1e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5110,9 +5110,9 @@ UniValue faucetget(const UniValue& params, bool fHelp) UniValue dicefund(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); int64_t funds,minbet,maxbet,maxodds,forfeitblocks; std::string hex; char *name; + UniValue result(UniValue::VOBJ); int64_t funds,minbet,maxbet,maxodds,timeoutblocks; std::string hex; char *name; if ( fHelp || params.size() != 6 ) - throw runtime_error("dicefund name funds minbet maxbet maxodds forfeitblocks\n"); + throw runtime_error("dicefund name funds minbet maxbet maxodds timeoutblocks\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"); name = (char *)params[0].get_str().c_str(); @@ -5120,8 +5120,8 @@ UniValue dicefund(const UniValue& params, bool fHelp) minbet = atof(params[2].get_str().c_str()) * COIN; maxbet = atof(params[3].get_str().c_str()) * COIN; maxodds = atol(params[4].get_str().c_str()); - forfeitblocks = atol(params[5].get_str().c_str()); - hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,forfeitblocks); + timeoutblocks = atol(params[5].get_str().c_str()); + hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 4fe61a92572b64ca65998c048b570befe5a1c5d8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:21:38 -1100 Subject: [PATCH 046/233] Test --- src/cc/assets.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 764724086..e09cc0569 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -252,7 +252,6 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx break; case 's': // selloffer - case 'e': // exchange //vin.0: normal input //vin.1+: valid CC output for sale //vout.0: vin.1 assetoshis output to CC to unspendable @@ -266,7 +265,21 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); preventCCvouts = 1; break; - + case 'e': // exchange + //vin.0: normal input + //vin.1+: valid CC output for sale + //vout.0: vin.1 assetoshis output to CC to unspendable + //vout.1: normal output for change (if any) + //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey] + //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] + if ( remaining_price == 0 ) + return eval->Invalid("illegal null remaining_price for exchangeoffer"); + else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + return eval->Invalid("mismatched vout0 AssetsCCaddr for exchangeoffer"); + //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); + preventCCvouts = 2; + break; + case 'x': // cancel //vin.0: normal input //vin.1: unspendable.(vout.0 from exchange or selloffer) sellTx/exchangeTx.vout[0] inputTx From ee360983eb03729e28ed6fe13c19163eec989546 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:23:26 -1100 Subject: [PATCH 047/233] Test --- src/cc/CCutils.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 632b243bc..b6ef778a6 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -193,7 +193,10 @@ bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t n for (i=preventCCvouts; iInvalid("invalid CC vout"); + } } } return(true); From add613688e4d0b99f9094020d2dd08e0fb8a674d Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:28:09 -1100 Subject: [PATCH 048/233] Constrain sell offer CC touts --- src/cc/assets.cpp | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index e09cc0569..f03dadb66 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -252,34 +252,29 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx break; case 's': // selloffer + case 'e': // selloffer //vin.0: normal input //vin.1+: valid CC output for sale //vout.0: vin.1 assetoshis output to CC to unspendable - //vout.1: normal output for change (if any) + //vout.1: CC output for change (if any) + //vout.2: normal output for change (if any) //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey] //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] + preventCCvouts = 1; if ( remaining_price == 0 ) return eval->Invalid("illegal null remaining_price for selloffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + preventCCvouts++; + if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) + return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); + else if ( tx.vout[0].nValue+tx.vout[1].nValue != inputs ) + return eval->Invalid("mismatched vout0+vout1 total for selloffer"); + } else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,inputs) == 0 ) return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer"); //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); - preventCCvouts = 1; break; - case 'e': // exchange - //vin.0: normal input - //vin.1+: valid CC output for sale - //vout.0: vin.1 assetoshis output to CC to unspendable - //vout.1: normal output for change (if any) - //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey] - //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey] - if ( remaining_price == 0 ) - return eval->Invalid("illegal null remaining_price for exchangeoffer"); - else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 ) - return eval->Invalid("mismatched vout0 AssetsCCaddr for exchangeoffer"); - //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price); - preventCCvouts = 2; - break; - + case 'x': // cancel //vin.0: normal input //vin.1: unspendable.(vout.0 from exchange or selloffer) sellTx/exchangeTx.vout[0] inputTx From 753a6f9278e21422fd776898a2a2f251985de805 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:48:48 -1100 Subject: [PATCH 049/233] Test --- src/cc/dice.cpp | 67 +++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 7003a15a1..17a2fd7db 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -27,6 +27,11 @@ The house commits to an entropy value by including the hash of the entropy value in the 'E' transaction. + To bet, one of these 'E' transactions is used as the first input and its hashed entropy is combined with the unhashed entropy attached to the bet 'B' transaction. + + The house node monitors the 'B' transactions and if it sees one of its own, it creates either a winner 'W' or loser 'L' transaction, with proof of hash of entropy. + + In the even the house node doesnt respond before timeoutblocks, then anybody (including bettor) can undo the bet with funds going back to the house and bettor */ #include "../endian.h" @@ -132,10 +137,10 @@ CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint25 return(opret); } -uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) +uint8_t DecodeDiceOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) { std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,timeoutblocks; - GetOpReturnData(scriptPubKey, vopret); + GetOpReturnData(scriptPubKey,vopret); if ( vopret.size() > 2 ) { script = (uint8_t *)vopret.data(); @@ -215,9 +220,38 @@ bool DiceIsmine(const CScript scriptPubKey) return(strcmp(destaddr,myaddr) == 0); } +int32_t DiceIsWinner(CTransaction tx,CTransaction vinTx,uint256 bettorentropy,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblock) +{ + uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; + //char str[65],str2[65]; + if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) + { + if ( DecodeDiceOpRet(vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) + { + hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); + if ( hentropy == hentropy2 ) + { + winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,bettorentropy); + //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); + if ( winnings == 0 ) + { + // queue 'L' losing tx + return(-1); + } + else + { + // queue 'W' winning tx + return(1); + } + } + } + } + return(0); +} + bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -226,7 +260,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) else { txid = tx.GetHash(); - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) return eval->Invalid("cant find fundingtxid"); @@ -263,26 +297,9 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - else if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) + if ( (iswin= DiceIsWinner(tx,vinTx,hash,minbet,maxbet,maxodds,timeoutblock)) != 0 ) { - uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; char str[65],str2[65]; - if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) - { - hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); - if ( hentropy == hentropy2 ) - { - winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,hash); - //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); - if ( winnings == 0 ) - { - // queue 'L' losing tx - } - else - { - // queue 'W' winning tx - } - } - } + fprintf(stderr,"DiceIsWinner.%d\n",iswin); } return eval->Invalid("dont confirm bet during debug"); break; @@ -348,7 +365,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'U' ) @@ -384,7 +401,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { From 8581f98a19fad99195f9fd5a22ceba426e1d9094 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:50:56 -1100 Subject: [PATCH 050/233] DiceIswinner --- src/cc/dice.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 17a2fd7db..71d705d7e 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -137,7 +137,7 @@ CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint25 return(opret); } -uint8_t DecodeDiceOpRet(const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) +uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) { std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,timeoutblocks; GetOpReturnData(scriptPubKey,vopret); @@ -220,13 +220,13 @@ bool DiceIsmine(const CScript scriptPubKey) return(strcmp(destaddr,myaddr) == 0); } -int32_t DiceIsWinner(CTransaction tx,CTransaction vinTx,uint256 bettorentropy,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblock) +int32_t DiceIsWinner(uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblock) { uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; //char str[65],str2[65]; if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { - if ( DecodeDiceOpRet(vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) + if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) @@ -260,7 +260,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) else { txid = tx.GetHash(); - if ( (funcid= DecodeDiceOpRet(tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) return eval->Invalid("cant find fundingtxid"); @@ -297,7 +297,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - if ( (iswin= DiceIsWinner(tx,vinTx,hash,minbet,maxbet,maxodds,timeoutblock)) != 0 ) + if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblock)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } @@ -365,7 +365,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'U' ) @@ -401,7 +401,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { From f8dd0a16c59af53a36933b6d9a14fcdb42e90c66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:53:03 -1100 Subject: [PATCH 051/233] Timeoutblocks --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 71d705d7e..6a1081c10 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -220,7 +220,7 @@ bool DiceIsmine(const CScript scriptPubKey) return(strcmp(destaddr,myaddr) == 0); } -int32_t DiceIsWinner(uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblock) +int32_t DiceIsWinner(uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) { uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; //char str[65],str2[65]; @@ -297,7 +297,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblock)) != 0 ) + if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblock,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } From 6235079e37cb372c3d366067106a18987fb3fada Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:54:16 -1100 Subject: [PATCH 052/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6a1081c10..c2a28ef1c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -297,7 +297,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblock,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } From c6835cfe0aed155df2c81abac47e677b3e743004 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 05:57:11 -1100 Subject: [PATCH 053/233] Test --- src/cc/dice.cpp | 69 ++++++------------------------------------------- 1 file changed, 8 insertions(+), 61 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c2a28ef1c..06a85caac 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -496,7 +496,7 @@ UniValue DiceList() return(result); } -struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits) +struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits) { int64_t a,b,c,d; struct CCcontract_info *cp; cp = CCinit(C,EVAL_DICE); @@ -505,7 +505,7 @@ struct CCcontract_info *Diceinit(struct CCcontract_info *C,char *planstr,uint64_ mypk = pubkey2pk(Mypubkey()); dicepk = GetUnspendable(cp,0); sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) != 0 ) + if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); return(0); @@ -521,7 +521,7 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(0,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { @@ -541,7 +541,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -562,7 +562,7 @@ std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -583,7 +583,7 @@ std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 b fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -604,7 +604,7 @@ std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -625,7 +625,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) return(0); if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) == 0 ) { @@ -655,57 +655,4 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) -{ - int32_t houseflag = 1; - CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,dicepk; CScript opret,scriptPubKey,ignore; uint256 hashBlock,entropy,hentropy; uint64_t funding,sbits,reward=0,amount=0,inputs,CCchange=0; int64_t minbet,maxbet,maxodds,timeoutblocks; struct CCcontract_info *cp,C; - if ( (cp= Diceinit(&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) - return(0); - if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) == 0 ) - { - fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); - return(0); - } - // need to deal with finding the right utxos - if ( locktxid == zeroid ) - amount = AddDiceInputs(scriptPubKey,0,cp,mtx,dicepk,(1LL << 30),1); - else - { - GetCCaddress(cp,coinaddr,dicepk); - if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 ) - { - fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr); - return(0); - } - if ( GetTransaction(locktxid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) - { - scriptPubKey = tx.vout[1].scriptPubKey; - mtx.vin.push_back(CTxIn(locktxid,0,CScript())); - } - else - { - fprintf(stderr,"%s no normal vout.1 in locktxid\n",coinaddr); - return(0); - } - } - reward = 0;//DiceCalc(amount,mtx.vin[0].prevout.hash,minbet,maxbet,maxodds,timeoutblocks); - if ( amount > 0 && reward > txfee && scriptPubKey.size() > 0 ) - { - if ( (inputs= AddDiceInputs(ignore,1,cp,mtx,dicepk,reward+txfee,30)) > 0 ) - { - if ( inputs >= (reward + 2*txfee) ) - CCchange = (inputs - (reward + txfee)); - fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN); - if ( houseflag != 0 ) - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - else hentropy = zeroid; - mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); - mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey)); - return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeDiceOpRet('U',sbits,fundingtxid,hentropy))); - } - fprintf(stderr,"cant find enough dice inputs\n"); - } - fprintf(stderr,"amount %.8f -> reward %.8f\n",(double)amount/COIN,(double)reward/COIN); - return(0); -} From 7347a801b740241919fca86efd0e9cefb74a9c01 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 06:06:55 -1100 Subject: [PATCH 054/233] ,minbet,maxbet,maxodds,timeoutblocks --- src/cc/CCutils.cpp | 10 ++-- src/cc/dice.cpp | 141 +++++++++++++++++++++------------------------ 2 files changed, 70 insertions(+), 81 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index b6ef778a6..daaa73ade 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -255,17 +255,17 @@ CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv) bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) { - CTransaction createTx; uint256 txid,assetid,assetid2,hashBlock; uint8_t funcid; int32_t i,n; uint64_t amount; std::vector origpubkey; - txid = ctx.GetHash(); - if ( txid == cp->prevtxid ) - return(true); + CTransaction createTx; uint256 assetid,assetid2,hashBlock; uint8_t funcid; int32_t i,n; uint64_t amount; std::vector origpubkey; + //txid = ctx.GetHash(); + //if ( txid == cp->prevtxid ) + // return(true); if ( paramsNull.size() != 0 ) // Don't expect params return eval->Invalid("Cannot have params"); else if ( ctx.vout.size() == 0 ) return eval->Invalid("no-vouts"); else if ( (*cp->validate)(cp,eval,ctx) != 0 ) { - cp->prevtxid = txid; + //cp->prevtxid = txid; return(true); } return(false); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 06a85caac..65532e19b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -496,16 +496,16 @@ UniValue DiceList() return(result); } -struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits) +struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { - int64_t a,b,c,d; struct CCcontract_info *cp; + struct CCcontract_info *cp; cp = CCinit(C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); dicepk = GetUnspendable(cp,0); sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,a,b,c,d) != cmpflag ) + if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); return(0); @@ -515,13 +515,13 @@ struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; int63_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(0,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(0,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { @@ -535,13 +535,13 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -554,69 +554,6 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 return(0); } -std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) -{ - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; - if ( amount < 0 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) - return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); - return(0); -} - -std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) -{ - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; - if ( amount < 0 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) - return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); - return(0); -} - -std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) -{ - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; uint64_t amount = 0; - if ( amount < 0 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) - return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); - return(0); -} - std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; @@ -625,13 +562,8 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits)) == 0 ) + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); - if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) == 0 ) - { - fprintf(stderr,"Dice plan %s doesnt exist\n",planstr); - return(0); - } if ( bet < minbet || bet > maxbet || odds > maxodds ) { fprintf(stderr,"Dice plan %s illegal bet %.8f: minbet %.8f maxbet %.8f or odds %d vs max.%d\n",planstr,(double)bet/COIN,(double)minbet/COIN,(double)maxbet/COIN,(int32_t)odds,(int32_t)maxodds); @@ -655,4 +587,61 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } +std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + return(0); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + +std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + return(0); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} + +std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; + if ( amount < 0 ) + { + fprintf(stderr,"negative parameter error\n"); + return(0); + } + if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + return(0); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); + } else fprintf(stderr,"cant find enough inputs\n"); + fprintf(stderr,"cant find fundingtxid\n"); + return(0); +} From e8c99f79db54d14ee99f154117f035aa0cf75002 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 06:07:55 -1100 Subject: [PATCH 055/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 65532e19b..83a0ea135 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -515,7 +515,7 @@ struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; int63_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; int64_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); From a03bb7faac9b7de16858cb5f0840b6955547d9af Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 06:45:21 -1100 Subject: [PATCH 056/233] winloserefund --- src/cc/CCdice.h | 4 +- src/cc/dice.cpp | 106 +++++++++++++++++++++++++-------------- src/wallet/rpcwallet.cpp | 6 +-- 3 files changed, 72 insertions(+), 44 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index e539d301b..115dfa2c0 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,9 +24,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); -std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); -std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); -std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); +std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 83a0ea135..e3de48f24 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -166,6 +166,14 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits return(0); } +uint256 DiceGetEntropy(CTransaction tx,uint8_t reffuncid) +{ + uint256 hash,fundingtxid; uint64_t sbits; int32_t numvouts; + if ( (numvouts= tx.vout.size()) > 0 && DecodeDiceOpRet(tx.GetHash(),tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash) == reffuncid ) + return(hash); + else return(zeroid); +} + uint64_t IsDicevout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; @@ -424,7 +432,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi return(totalinputs); } -bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) +bool DicePlanExists(uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { char CCaddr[64]; uint64_t sbits; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; @@ -434,18 +442,43 @@ bool DicePlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk, { //int height = it->first.blockHeight; txid = it->first.txhash; + if ( fundingtxid != zeroid && txid != fundingtxid ) + continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) { if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' ) { if ( sbits == refsbits ) + { + fundingtxid = txid; return(true); + } } } } return(false); } +struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) +{ + struct CCcontract_info *cp; uint256 fundingtxid; + cp = CCinit(C,EVAL_DICE); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + dicepk = GetUnspendable(cp,0); + sbits = stringbits(planstr); + if ( reffundingtxid == zeroid ) + cmpflag = 0; + else cmpflag = 1; + if ( DicePlanExists(fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) + { + fprintf(stderr,"Dice plan (%s) already exists\n",planstr); + return(0); + } + return(cp); +} + UniValue DiceInfo(uint256 diceid) { UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char str[67],numstr[65]; @@ -496,32 +529,15 @@ UniValue DiceList() return(result); } -struct CCcontract_info *Diceinit(int32_t cmpflag,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) -{ - struct CCcontract_info *cp; - cp = CCinit(C,EVAL_DICE); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - dicepk = GetUnspendable(cp,0); - sbits = stringbits(planstr); - if ( DicePlanExists(cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) - { - fprintf(stderr,"Dice plan (%s) already exists\n",planstr); - return(0); - } - return(cp); -} - std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; int64_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; int64_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(0,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) + if ( (cp= Diceinit(zeroid,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { @@ -535,13 +551,13 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -556,13 +572,13 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { - CMutableTransaction mtx; CPubKey mypk,dicepk; CScript opret; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CPubKey mypk,dicepk; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; if ( bet < 0 || odds < 1 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( bet < minbet || bet > maxbet || odds > maxodds ) { @@ -587,31 +603,45 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceWinner(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount = 0; uint8_t funcid; + if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); + bettorentropy = DiceGetEntropy(betTx,'B'); + if ( (iswin= DiceIsWinner(bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + { + if ( iswin == winloserefund ) + { + if ( iswin == 0 ) + funcid = 'R'; + else if ( iswin > 0 ) + funcid = 'W'; + else funcid = 'L'; + fprintf(stderr,"iswin.%d matches\n",iswin); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,zeroid))); + } else fprintf(stderr,"cant find enough inputs\n"); + } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winloserefund); + } else return(0); + } return(0); } std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { @@ -626,13 +656,13 @@ std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 b std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; CScript opret; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; + CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(1,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4d20ffb1e..6e818e2fd 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5179,7 +5179,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinner(0,name,fundingtxid,bettxid); + hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5198,7 +5198,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceLoser(0,name,fundingtxid,bettxid); + hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,-1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5217,7 +5217,7 @@ UniValue dicerefund(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceRefund(0,name,fundingtxid,bettxid); + hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,0); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From 39d9f2762c2a6978e1961e92a33529c643b99652 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 06:46:46 -1100 Subject: [PATCH 057/233] Fix --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e3de48f24..9da3d40e2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -461,7 +461,7 @@ bool DicePlanExists(uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t ref struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { - struct CCcontract_info *cp; uint256 fundingtxid; + struct CCcontract_info *cp; uint256 fundingtxid; int32_t cmpflag; cp = CCinit(C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; @@ -605,7 +605,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund) { - CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount = 0; uint8_t funcid; + CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount = 0; uint8_t funcid; int32_t iswin; if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) From e0e985ee17f5c27f7d40caf4f2c623fbf31a5cff Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 06:49:23 -1100 Subject: [PATCH 058/233] Enable dice bet --- src/cc/dice.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9da3d40e2..f84cfab9d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -159,7 +159,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits { if ( e == EVAL_DICE && (f == 'B' || f == 'U' || f == 'E') ) return(f); - else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); + //else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } } else fprintf(stderr,"script[0] %02x != EVAL_DICE\n",script[0]); } else fprintf(stderr,"not enough opret.[%d]\n",(int32_t)vopret.size()); @@ -302,14 +302,13 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'B' sbits fundingtxid entropy // get house hentropy and its vin0.prevtxid, cmp vout1 to owner address // if owneraddress is me, then validate hentropy and submit outcome tx - fprintf(stderr,"got bet txid\n"); if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } - return eval->Invalid("dont confirm bet during debug"); + //return eval->Invalid("dont confirm bet during debug"); break; case 'L': break; @@ -426,7 +425,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case funcid %d\n",funcid); - } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); + } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } return(totalinputs); From 7eb604110e8e4d2f7c52cbc288563c6d727ee6ac Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 07:56:27 -1100 Subject: [PATCH 059/233] Initial dice --- src/cc/dice.cpp | 206 ++++++++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 77 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f84cfab9d..0e9313a8d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -32,6 +32,59 @@ The house node monitors the 'B' transactions and if it sees one of its own, it creates either a winner 'W' or loser 'L' transaction, with proof of hash of entropy. In the even the house node doesnt respond before timeoutblocks, then anybody (including bettor) can undo the bet with funds going back to the house and bettor + + In order for people to play dice, someone (anyone) needs to create a funded dice plan and addfunding with enough utxo to allow players to find one. + +createfunding: + vins.*: normal inputs + vout.0: CC vout for funding + vout.1: normal marker vout for easy searching + vout.2: normal change + vout.n-1: opreturn 'F' sbits minbet maxbet maxodds timeoutblocks + +addfunding (entropy): + vins.*: normal inputs + vout.0: CC vout for locked entropy funds + vout.1: tag to owner address for entropy funds + vout.2: normal change + vout.n-1: opreturn 'E' sbits fundingtxid hentropy + +bet: + vin.0: entropy txid from house + vins.1+: normal inputs + vout.0: CC vout for locked entropy + vout.1: CC vout for locked bet + vout.2: tag for bettor's address (txfee + odds) + vout.3: change + vout.n-1: opreturn 'B' sbits fundingtxid entropy + +winner: + vin.0: betTx CC vout.0 entropy from bet + vin.1: betTx CC vout.1 bet amount from bet + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vout.0: funding CC change to entropy owner + vout.1: tag to owner address for entropy funds + vout.2: normal output to bettor's address + vout.n-1: opreturn 'W' sbits fundingtxid hentropy + +loser: + vin.0: betTx CC vout.0 entropy from bet + vin.1: betTx CC vout.1 bet amount from bet + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vout.0: funding CC to entropy owner + vout.1: tag to owner address for entropy funds + vout.n-1: opreturn 'L' sbits fundingtxid hentropy + +refund: + vin.0: betTx CC vout.0 entropy from bet + vin.1: betTx CC vout.1 bet amount from bet + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vout.0: funding CC vin.0 to entropy owner + vout.1: tag to owner address for entropy funds + vout.2: normal vin.1 to bettor's address + vout.n-1: opreturn 'R' sbits fundingtxid hentropy + + */ #include "../endian.h" @@ -157,7 +210,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) { - if ( e == EVAL_DICE && (f == 'B' || f == 'U' || f == 'E') ) + if ( e == EVAL_DICE && (f == 'B' || f == 'W' || f == 'L' || f == 'R' || f == 'E') ) return(f); //else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } @@ -228,13 +281,13 @@ bool DiceIsmine(const CScript scriptPubKey) return(strcmp(destaddr,myaddr) == 0); } -int32_t DiceIsWinner(uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) +int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) { - uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; + uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; uint8_t funcid; //char str[65],str2[65]; - if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) + if ( (mustbeme == 0 || DiceIsmine(vinTx.vout[1].scriptPubKey) != 0) && vinTx.vout.size() > 0 ) { - if ( DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy) == 'E' && sbits == vinsbits && fundingtxid == vinfundingtxid ) + if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'R') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) @@ -281,7 +334,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.0: CC vout for funding //vout.1: normal marker vout for easy searching //vout.2: normal change - //vout.n-1: opreturn 'F' sbits APR minseconds maxseconds mindeposit + //vout.n-1: opreturn 'F' sbits minbet maxbet maxodds timeoutblocks return eval->Invalid("unexpected DiceValidate for createfunding"); break; case 'E': @@ -300,27 +353,40 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.2: tag for bettor's address (txfee + odds) //vout.3: change //vout.n-1: opreturn 'B' sbits fundingtxid entropy - // get house hentropy and its vin0.prevtxid, cmp vout1 to owner address - // if owneraddress is me, then validate hentropy and submit outcome tx if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - if ( (iswin= DiceIsWinner(txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(1,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } //return eval->Invalid("dont confirm bet during debug"); break; case 'L': + //vin.0: betTx CC vout.0 entropy from bet + //vin.1: betTx CC vout.1 bet amount from bet + //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + //vout.0: funding CC to entropy owner + //vout.1: tag to owner address for entropy funds + //vout.n-1: opreturn 'L' sbits fundingtxid hentropy break; case 'W': + //vin.0: betTx CC vout.0 entropy from bet + //vin.1: betTx CC vout.1 bet amount from bet + //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + //vout.0: funding CC change to entropy owner + //vout.1: tag to owner address for entropy funds + //vout.2: normal output to bettor's address + //vout.n-1: opreturn 'W' sbits fundingtxid hentropy break; - case 'U': - //vin.0: locked funds CC vout.0 from lock - //vin.1+: funding CC vout.0 from 'F' and 'E' and 'U' - //vout.0: funding CC change - //vout.1: normal output to unlock address - //vout.n-1: opreturn 'U' sbits fundingtxid - for (i=0; iismyvin)(tx.vin[i].scriptSig) == 0 ) return eval->Invalid("unexpected normal vin for unlock"); @@ -344,7 +410,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,timeoutblocks); if ( tx.vout[1].nValue > amount+reward ) return eval->Invalid("unlock tx vout.1 isnt amount+reward"); - preventCCvouts = 1; + preventCCvouts = 1;*/ break; } } @@ -353,7 +419,6 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return(true); } -// 'B' vs 'F' and 'E' uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; @@ -375,9 +440,9 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); - if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'U' ) + if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'W' && funcid != 'L' && funcid != 'R' ) continue; - else if ( fundsflag == 0 && (funcid != 'B' || tx.vout.size() < 4) ) + else if ( fundsflag == 0 && funcid != 'B' ) continue; if ( total != 0 && maxinputs != 0 ) { @@ -412,11 +477,11 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { - if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) + if ( funcid != 'B' && refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) { totalinputs += nValue; fprintf(stderr,"add %.8f\n",(double)nValue/COIN); - if ( first == 0 && funcid == 'E' ) + if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'R') ) { entropytxid = txid; entropyval = tx.vout[0].nValue; @@ -584,7 +649,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet fprintf(stderr,"Dice plan %s illegal bet %.8f: minbet %.8f maxbet %.8f or odds %d vs max.%d\n",planstr,(double)bet/COIN,(double)minbet/COIN,(double)maxbet/COIN,(int32_t)odds,(int32_t)maxodds); return(0); } - if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= bet*odds+txfee && entropyval != 0 ) + if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= 2*bet*odds+txfee && entropyval != 0 ) { mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); if ( AddNormalinputs(mtx,mypk,bet+2*txfee+odds,60) > 0 ) @@ -604,73 +669,60 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund) { - CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount = 0; uint8_t funcid; int32_t iswin; + CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded; uint8_t funcid; int32_t iswin; uint64_t entropyval; if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); - if ( (iswin= DiceIsWinner(bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(0,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { if ( iswin == winloserefund ) { - if ( iswin == 0 ) - funcid = 'R'; - else if ( iswin > 0 ) - funcid = 'W'; - else funcid = 'L'; fprintf(stderr,"iswin.%d matches\n",iswin); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + mtx.vin.push_back(CTxIn(bettxid,0,CScript())); + mtx.vin.push_back(CTxIn(bettxid,1,CScript())); + if ( iswin == 0 ) { - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,zeroid))); - } else fprintf(stderr,"cant find enough inputs\n"); + funcid = 'R'; + fprintf(stderr,"refunds are not supported yet\n"); + } + else if ( iswin > 0 ) + { + funcid = 'W'; + odds = (betTx.vout[2].nValue - txfee); + if ( odds < 1 || odds > maxodds ) + { + fprintf(stderr,"illegal odds.%d vs maxodds.%d\n",(int32_t)odds,(int32_t)maxodds); + return(0); + } + CCchange = betTx.vout[0].nValue; + fundsneeded = 2*txfee + (odds-1)*betTx.vout[1].nValue; + if ( (inputs= AddDiceInputs(scriptPubKey,1,cp,mtx,dicepk,fundsneeded,60)) > 0 ) + { + if ( inputs > fundsneeded+txfee ) + CCchange += (inputs - (fundsneeded+txfee)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); + mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); + mtx.vout.push_back(CTxOut(odds * betTx.vout[1].nValue,betTx.vout[2].scriptPubKey)); + } + else + { + fprintf(stderr,"not enough inputs for %.8f\n",(double)fundsneeded/COIN); + return(0); + } + } + else + { + funcid = 'L'; + mtx.vout.push_back(MakeCC1vout(cp->evalcode,betTx.vout[0].nValue + betTx.vout[1].nValue - txfee,dicepk)); + mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); + } + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winloserefund); } else return(0); } return(0); } -std::string DiceLoser(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) -{ - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; - if ( amount < 0 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) - return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); - return(0); -} - -std::string DiceRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) -{ - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t amount = 0; - if ( amount < 0 ) - { - fprintf(stderr,"negative parameter error\n"); - return(0); - } - if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) - return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) - { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('W',sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); - return(0); -} - From 0fd2f876962a0a55d5a81d1b959bbdf5ab7950fe Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 07:57:29 -1100 Subject: [PATCH 060/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0e9313a8d..330447284 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -669,7 +669,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund) { - CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded; uint8_t funcid; int32_t iswin; uint64_t entropyval; + CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) From 0240997499b1e06e957adcc7f8282f2a144efc90 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 08:13:29 -1100 Subject: [PATCH 061/233] Dice refund -> timeout --- src/cc/CCdice.h | 2 +- src/cc/dice.cpp | 36 ++++++++++++++++++------------------ src/rpcserver.cpp | 2 +- src/rpcserver.h | 2 +- src/wallet/rpcwallet.cpp | 12 ++++++------ 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 115dfa2c0..33f187391 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,7 +24,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); -std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund); +std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 330447284..236ea37b2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -61,7 +61,7 @@ bet: winner: vin.0: betTx CC vout.0 entropy from bet vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC change to entropy owner vout.1: tag to owner address for entropy funds vout.2: normal output to bettor's address @@ -70,19 +70,19 @@ winner: loser: vin.0: betTx CC vout.0 entropy from bet vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC to entropy owner vout.1: tag to owner address for entropy funds vout.n-1: opreturn 'L' sbits fundingtxid hentropy -refund: +timeout: vin.0: betTx CC vout.0 entropy from bet vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC vin.0 to entropy owner vout.1: tag to owner address for entropy funds vout.2: normal vin.1 to bettor's address - vout.n-1: opreturn 'R' sbits fundingtxid hentropy + vout.n-1: opreturn 'T' sbits fundingtxid hentropy */ @@ -210,7 +210,7 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits } else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) { - if ( e == EVAL_DICE && (f == 'B' || f == 'W' || f == 'L' || f == 'R' || f == 'E') ) + if ( e == EVAL_DICE && (f == 'B' || f == 'W' || f == 'L' || f == 'T' || f == 'E') ) return(f); //else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } @@ -287,7 +287,7 @@ int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction //char str[65],str2[65]; if ( (mustbeme == 0 || DiceIsmine(vinTx.vout[1].scriptPubKey) != 0) && vinTx.vout.size() > 0 ) { - if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'R') && sbits == vinsbits && fundingtxid == vinfundingtxid ) + if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) @@ -364,7 +364,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) case 'L': //vin.0: betTx CC vout.0 entropy from bet //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' //vout.0: funding CC to entropy owner //vout.1: tag to owner address for entropy funds //vout.n-1: opreturn 'L' sbits fundingtxid hentropy @@ -372,20 +372,20 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) case 'W': //vin.0: betTx CC vout.0 entropy from bet //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' //vout.0: funding CC change to entropy owner //vout.1: tag to owner address for entropy funds //vout.2: normal output to bettor's address //vout.n-1: opreturn 'W' sbits fundingtxid hentropy break; - case 'R': + case 'T': //vin.0: betTx CC vout.0 entropy from bet //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'R' + //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' //vout.0: funding CC vin.0 to entropy owner //vout.1: tag to owner address for entropy funds //vout.2: normal vin.1 to bettor's address - //vout.n-1: opreturn 'R' sbits fundingtxid hentropy + //vout.n-1: opreturn 'T' sbits fundingtxid hentropy /*for (i=0; iismyvin)(tx.vin[i].scriptSig) == 0 ) @@ -440,7 +440,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); - if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'W' && funcid != 'L' && funcid != 'R' ) + if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'W' && funcid != 'L' && funcid != 'T' ) continue; else if ( fundsflag == 0 && funcid != 'B' ) continue; @@ -481,7 +481,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { totalinputs += nValue; fprintf(stderr,"add %.8f\n",(double)nValue/COIN); - if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'R') ) + if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') ) { entropytxid = txid; entropyval = tx.vout[0].nValue; @@ -667,7 +667,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winloserefund) +std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) @@ -677,15 +677,15 @@ std::string DiceWinLoseRefund(uint64_t txfee,char *planstr,uint256 fundingtxid,u bettorentropy = DiceGetEntropy(betTx,'B'); if ( (iswin= DiceIsWinner(0,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { - if ( iswin == winloserefund ) + if ( iswin == winlosetimeout ) { fprintf(stderr,"iswin.%d matches\n",iswin); mtx.vin.push_back(CTxIn(bettxid,0,CScript())); mtx.vin.push_back(CTxIn(bettxid,1,CScript())); if ( iswin == 0 ) { - funcid = 'R'; - fprintf(stderr,"refunds are not supported yet\n"); + funcid = 'T'; + fprintf(stderr,"timeout refunds are not supported yet\n"); } else if ( iswin > 0 ) { diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 9790af78b..58799bc98 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -375,7 +375,7 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicebet", &dicebet, true }, { "dice", "dicewinner", &dicewinner, true }, { "dice", "diceloser", &diceloser, true }, - { "dice", "dicerefund", &dicerefund, true }, + { "dice", "dicetimeout", &dicetimeout, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ diff --git a/src/rpcserver.h b/src/rpcserver.h index b7aa32044..786c377d8 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -241,7 +241,7 @@ extern UniValue diceaddfunds(const UniValue& params, bool fHelp); extern UniValue dicebet(const UniValue& params, bool fHelp); extern UniValue dicewinner(const UniValue& params, bool fHelp); extern UniValue diceloser(const UniValue& params, bool fHelp); -extern UniValue dicerefund(const UniValue& params, bool fHelp); +extern UniValue dicetimeout(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); extern UniValue ponziaddress(const UniValue& params, bool fHelp); extern UniValue auctionaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6e818e2fd..54de29217 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5179,7 +5179,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,1); + hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5198,7 +5198,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,-1); + hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,-1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5207,22 +5207,22 @@ UniValue diceloser(const UniValue& params, bool fHelp) return(result); } -UniValue dicerefund(const UniValue& params, bool fHelp) +UniValue dicetimeout(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; if ( fHelp || params.size() != 3 ) - throw runtime_error("dicerefund name fundingtxid bettxid\n"); + throw runtime_error("dicetimeout name fundingtxid bettxid\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"); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseRefund(0,name,fundingtxid,bettxid,0); + hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,0); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create diceloser transaction")); + } else result.push_back(Pair("error", "couldnt create dicetimeout transaction")); return(result); } From e0ea3b3eb9075f49e66ea74144269cd0bdd1a218 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 08:14:42 -1100 Subject: [PATCH 062/233] Fix --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 236ea37b2..22f2d36d3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -720,7 +720,7 @@ std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid, } hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy))); - } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winloserefund); + } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return(0); } return(0); From afef48c0bfa7cda9ec52e4a6bb7e912b07affcdc Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 08:21:57 -1100 Subject: [PATCH 063/233] Hentropy proof --- src/cc/dice.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 22f2d36d3..a2d818344 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,6 +15,8 @@ #include "CCdice.h" +// timeout, validate, autoqueue + /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. @@ -183,10 +185,10 @@ uint8_t DecodeDiceFundingOpRet(const CScript &scriptPubKey,uint64_t &sbits,int64 return(0); } -CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint256 hash) +CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint256 hash,uint256 proof) { CScript opret; uint8_t evalcode = EVAL_DICE; - opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid << hash); + opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << sbits << fundingtxid << hash << proof); return(opret); } @@ -628,7 +630,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy,zeroid))); } else fprintf(stderr,"cant find enough inputs\n"); fprintf(stderr,"cant find fundingtxid\n"); return(0); @@ -658,7 +660,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet mtx.vout.push_back(MakeCC1vout(cp->evalcode,entropyval,dicepk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,bet,dicepk)); mtx.vout.push_back(CTxOut(txfee+odds,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('B',sbits,fundingtxid,entropy,zeroid))); } else fprintf(stderr,"cant find enough inputs %.8f note enough for %.8f\n",(double)funding/COIN,(double)bet/COIN); } if ( entropyval == 0 && funding != 0 ) @@ -669,12 +671,13 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; + CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); + // need to set hentropyproof if ( (iswin= DiceIsWinner(0,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { if ( iswin == winlosetimeout ) @@ -719,7 +722,7 @@ std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid, mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); } hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy))); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return(0); } From cf8725c3b5c3539047e056e766c595929feca7c7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 09:20:51 -1100 Subject: [PATCH 064/233] Faucet test --- src/cc/dice.cpp | 2 +- src/cc/faucet.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a2d818344..a0fc68a68 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// timeout, validate, autoqueue +// hentropy_proof, timeout, validate, autoqueue /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 3891a3f9e..de06a1612 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -136,7 +136,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 10000 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); From 03660ecce67598ba254fc5cf832161ce0d420188 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 09:22:39 -1100 Subject: [PATCH 065/233] Min vin 0.01 --- 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 de06a1612..9359a4eb7 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -136,7 +136,7 @@ uint64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPu // prevent dup if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 10000 ) + if ( (nValue= IsFaucetvout(cp,vintx,(int32_t)it->first.index)) > 1000000 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); From c2342f7b2424cd2430250e70f6f978577ebc887c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 09:25:51 -1100 Subject: [PATCH 066/233] Add spam prevention --- src/cc/dice.cpp | 2 ++ src/cc/rewards.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a0fc68a68..a1527fec2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -431,6 +431,8 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract { txid = it->first.txhash; vout = (int32_t)it->first.index; + if ( it->second.satoshis < 10000 ) + continue; fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; jfirst.txhash; vout = (int32_t)it->first.index; + if ( it->second.satoshis < 10000 ) + continue; fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; j Date: Mon, 30 Jul 2018 09:31:28 -1100 Subject: [PATCH 067/233] Bigger spam prevention --- src/cc/auction.cpp | 2 ++ src/cc/dice.cpp | 2 +- src/cc/lotto.cpp | 2 ++ src/cc/ponzi.cpp | 2 ++ src/cc/rewards.cpp | 2 +- 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cc/auction.cpp b/src/cc/auction.cpp index 1bcf2ecd8..cf3ae601a 100644 --- a/src/cc/auction.cpp +++ b/src/cc/auction.cpp @@ -127,6 +127,8 @@ uint64_t AddAuctionInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP { txid = it->first.txhash; // prevent dup + if ( it->second.satoshis < 1000000 ) + continue; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsAuctionvout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a1527fec2..2240e5e57 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -431,7 +431,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract { txid = it->first.txhash; vout = (int32_t)it->first.index; - if ( it->second.satoshis < 10000 ) + if ( it->second.satoshis < 1000000 ) continue; fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; jfirst.txhash; // prevent dup + if ( it->second.satoshis < 1000000 ) + continue; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsLottovout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/ponzi.cpp b/src/cc/ponzi.cpp index c29f63ebc..d39f087dd 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/ponzi.cpp @@ -127,6 +127,8 @@ uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub { txid = it->first.txhash; // prevent dup + if ( it->second.satoshis < 1000000 ) + continue; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { if ( (nValue= IsPonzivout(cp,vintx,(int32_t)it->first.index)) > 0 ) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 6b562df8a..2067c6ebf 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -273,7 +273,7 @@ uint64_t AddRewardsInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontr { txid = it->first.txhash; vout = (int32_t)it->first.index; - if ( it->second.satoshis < 10000 ) + if ( it->second.satoshis < 1000000 ) continue; fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; j Date: Mon, 30 Jul 2018 09:38:41 -1100 Subject: [PATCH 068/233] Prevent skip Relaying wtx on non-KMD --- src/wallet/wallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5100a2470..70375e663 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2117,7 +2117,7 @@ std::vector CWallet::ResendWalletTransactionsBefore(int64_t nTime) // Don't rebroadcast if newer than nTime: if (wtx.nTimeReceived > nTime) continue; - //if ( ASSETCHAINS_SYMBOL[0] == 0 ) + if ( ASSETCHAINS_SYMBOL[0] == 0 ) { if ( wtx.nLockTime >= LOCKTIME_THRESHOLD && wtx.nLockTime < now-KOMODO_MAXMEMPOOLTIME ) { From 876bf0e56fd45d1543b11d585cd129cf074eb41e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 10:02:12 -1100 Subject: [PATCH 069/233] Need to be house address to finalize bet --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 2240e5e57..73d8e8c2c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -680,7 +680,7 @@ std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid, { bettorentropy = DiceGetEntropy(betTx,'B'); // need to set hentropyproof - if ( (iswin= DiceIsWinner(0,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(1,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { if ( iswin == winlosetimeout ) { From dd042a0fd3802706415ba4d7f21d855cc075a040 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:23:15 -1100 Subject: [PATCH 070/233] +print --- src/cc/dice.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 73d8e8c2c..66133d3ea 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -477,6 +477,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { + char str[65],str2[65]; if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) @@ -493,7 +494,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); - } else fprintf(stderr,"else case funcid %d\n",funcid); + } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid),); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } From 76152f3317154afa706149fecbd7b19aa77dc144 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:23:48 -1100 Subject: [PATCH 071/233] -, --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 66133d3ea..a13572d0c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -494,7 +494,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); - } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid),); + } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } From e8afffa928e935d6ab1eb70562a59c14780f9b4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:32:12 -1100 Subject: [PATCH 072/233] Test --- src/cc/dice.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a13572d0c..43cf57b49 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -540,6 +540,7 @@ struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info * if ( reffundingtxid == zeroid ) cmpflag = 0; else cmpflag = 1; + fundingtxid = zeriod; if ( DicePlanExists(fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); From 3cf03a67ac8a986f0325a0665d4709db58c4ffd7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:32:46 -1100 Subject: [PATCH 073/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 43cf57b49..b743dd2e6 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -540,7 +540,7 @@ struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info * if ( reffundingtxid == zeroid ) cmpflag = 0; else cmpflag = 1; - fundingtxid = zeriod; + fundingtxid = zeroid; if ( DicePlanExists(fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); From 237dae63ee189d52d0177b369ffb5fb3546cc522 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:41:48 -1100 Subject: [PATCH 074/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b743dd2e6..2aa2b9b4d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// hentropy_proof, timeout, validate, autoqueue +// hentropy_proof, timeout, validate, autoqueue, compare address to match house /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. @@ -495,7 +495,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); - } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); + } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } return(totalinputs); From 54519f6916fa43a0c6d70270694c404d831c3a2c Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:45:02 -1100 Subject: [PATCH 075/233] test --- src/cc/dice.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 2aa2b9b4d..cfec73584 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -192,7 +192,7 @@ CScript EncodeDiceOpRet(uint8_t funcid,uint64_t sbits,uint256 fundingtxid,uint25 return(opret); } -uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash) +uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits,uint256 &fundingtxid,uint256 &hash,uint256 &proof) { std::vector vopret; uint8_t *script,e,f,funcid; int64_t minbet,maxbet,maxodds,timeoutblocks; GetOpReturnData(scriptPubKey,vopret); @@ -210,11 +210,11 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits return('F'); } else fprintf(stderr,"unmarshal error for F\n"); } - else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash) != 0 ) + else if ( E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> sbits; ss >> fundingtxid; ss >> hash; ss >> proof) != 0 ) { if ( e == EVAL_DICE && (f == 'B' || f == 'W' || f == 'L' || f == 'T' || f == 'E') ) return(f); - //else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); + else fprintf(stderr,"mismatched e.%02x f.(%c)\n",e,f); } } else fprintf(stderr,"script[0] %02x != EVAL_DICE\n",script[0]); } else fprintf(stderr,"not enough opret.[%d]\n",(int32_t)vopret.size()); @@ -223,8 +223,8 @@ uint8_t DecodeDiceOpRet(uint256 txid,const CScript &scriptPubKey,uint64_t &sbits uint256 DiceGetEntropy(CTransaction tx,uint8_t reffuncid) { - uint256 hash,fundingtxid; uint64_t sbits; int32_t numvouts; - if ( (numvouts= tx.vout.size()) > 0 && DecodeDiceOpRet(tx.GetHash(),tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash) == reffuncid ) + uint256 hash,fundingtxid,proof; uint64_t sbits; int32_t numvouts; + if ( (numvouts= tx.vout.size()) > 0 && DecodeDiceOpRet(tx.GetHash(),tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash,proof) == reffuncid ) return(hash); else return(zeroid); } @@ -285,11 +285,11 @@ bool DiceIsmine(const CScript scriptPubKey) int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) { - uint64_t vinsbits,winnings; uint256 vinfundingtxid,hentropy,hentropy2,entropy; uint8_t funcid; + uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2,entropy; uint8_t funcid; //char str[65],str2[65]; if ( (mustbeme == 0 || DiceIsmine(vinTx.vout[1].scriptPubKey) != 0) && vinTx.vout.size() > 0 ) { - if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') && sbits == vinsbits && fundingtxid == vinfundingtxid ) + if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) @@ -314,7 +314,7 @@ int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid,hashBlock,hash; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock,hash,proof; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -323,7 +323,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) else { txid = tx.GetHash(); - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[numvouts-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { if ( eval->GetTxUnconfirmed(fundingtxid,fundingTx,hashBlock) == 0 ) return eval->Invalid("cant find fundingtxid"); @@ -423,7 +423,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { - char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; + char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,proof,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -441,7 +441,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract continue; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'W' && funcid != 'L' && funcid != 'T' ) @@ -466,7 +466,7 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,hashBlock,fundingtxid; CTransaction tx; int32_t vout,first=0; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CTransaction tx; int32_t vout,first=0; uint8_t funcid; std::vector > unspentOutputs; GetCCaddress(cp,coinaddr,pk); SetCCunspents(unspentOutputs,coinaddr); @@ -478,7 +478,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { char str[65],str2[65]; - if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash)) != 0 ) + if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { From 06811319cd3df82897871ee425545ff1fe312cd1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 19:46:06 -1100 Subject: [PATCH 076/233] -print --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index cfec73584..d4f908c29 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -495,7 +495,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); - } else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); + } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } return(totalinputs); From f516779cdc22e3454e4651b652729a88926c8c29 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 22:53:46 -1100 Subject: [PATCH 077/233] Info funding to return total funding --- src/cc/dice.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d4f908c29..9425fcee3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -551,7 +551,7 @@ struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info * UniValue DiceInfo(uint256 diceid) { - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); CPubKey dicepk; uint256 hashBlock,entropytxid; CTransaction vintx; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,funding,entropyval; char str[67],numstr[65]; struct CCcontract_info *cp,C; if ( GetTransaction(diceid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find fundingtxid\n"); @@ -575,7 +575,10 @@ UniValue DiceInfo(uint256 diceid) result.push_back(Pair("maxbet",numstr)); result.push_back(Pair("maxodds",maxodds)); result.push_back(Pair("timeoutblocks",timeoutblocks)); - sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); + cp = CCinit(&C,EVAL_DICE); + dicepk = GetUnspendable(cp,0); + funding = DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid); + sprintf(numstr,"%.8f",(double)funding/COIN); result.push_back(Pair("funding",numstr)); return(result); } From 688d3ac45de98dc72aad00cd3705d31d83aeb9f4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 22:55:05 -1100 Subject: [PATCH 078/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9425fcee3..154f07062 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -577,7 +577,7 @@ UniValue DiceInfo(uint256 diceid) result.push_back(Pair("timeoutblocks",timeoutblocks)); cp = CCinit(&C,EVAL_DICE); dicepk = GetUnspendable(cp,0); - funding = DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid); + funding = DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,diceid); sprintf(numstr,"%.8f",(double)funding/COIN); result.push_back(Pair("funding",numstr)); return(result); From 94ada5ec58f3d411c32c308df4788180b1e9f5e8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 23:16:47 -1100 Subject: [PATCH 079/233] Rewards funding <- use actual total --- src/cc/rewards.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 2067c6ebf..88c92198a 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -357,7 +357,7 @@ bool RewardsPlanExists(struct CCcontract_info *cp,uint64_t refsbits,CPubKey rewa UniValue RewardsInfo(uint256 rewardsid) { - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; uint64_t APR,minseconds,maxseconds,mindeposit,sbits; char str[67],numstr[65]; + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; uint64_t APR,minseconds,maxseconds,mindeposit,sbits,funding; CPubKey rewardspk; struct CCcontract_info *cp,C; char str[67],numstr[65]; if ( GetTransaction(rewardsid,vintx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find fundingtxid\n"); @@ -381,7 +381,10 @@ UniValue RewardsInfo(uint256 rewardsid) result.push_back(Pair("maxseconds",maxseconds)); sprintf(numstr,"%.8f",(double)mindeposit/COIN); result.push_back(Pair("mindeposit",numstr)); - sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); + cp = CCinit(&C,EVAL_REWARDS); + rewardspk = GetUnspendable(cp,0); + funding = RewardsPlanFunds(sbits,cp,rewardspk,rewardsid); + sprintf(numstr,"%.8f",(double)funding/COIN); result.push_back(Pair("funding",numstr)); return(result); } From 4608d17092e9ee8f2d1f1eec476c32ff7984f613 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 23:24:21 -1100 Subject: [PATCH 080/233] Faucet info --- src/cc/CCfaucet.h | 1 + src/cc/faucet.cpp | 13 +++++++++++++ src/wallet/rpcwallet.cpp | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/cc/CCfaucet.h b/src/cc/CCfaucet.h index b478165b5..b875bd133 100644 --- a/src/cc/CCfaucet.h +++ b/src/cc/CCfaucet.h @@ -26,5 +26,6 @@ bool FaucetValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx // CCcustom std::string FaucetFund(uint64_t txfee,uint64_t funds); std::string FaucetGet(uint64_t txfee); +UniValue FaucetInfo(); #endif diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index 9359a4eb7..f171257dc 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -187,4 +187,17 @@ std::string FaucetFund(uint64_t txfee,uint64_t funds) return(0); } +UniValue FaucetInfo() +{ + UniValue result(UniValue::VOBJ); + CMutableTransaction mtx; CPubKey faucetpk; struct CCcontract_info *cp,C; uint64_t funding; + result.push_back(Pair("result","success")); + result.push_back(Pair("name","Faucet")); + cp = CCinit(&C,EVAL_FAUCET); + faucetpk = GetUnspendable(cp,0); + funding = AddFaucetInputs(cp,mtx,faucetpk,0,0); + sprintf(numstr,"%.8f",(double)funding/COIN); + result.push_back(Pair("funding",numstr)); + return(result); +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 54de29217..50c298e97 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5075,6 +5075,16 @@ UniValue rewardsinfo(const UniValue& params, bool fHelp) return(RewardsInfo(fundingtxid)); } +UniValue faucetinfo(const UniValue& params, bool fHelp) +{ + uint256 fundingtxid; + if ( fHelp || params.size() != 0 ) + throw runtime_error("faucetinfo\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"); + return(FaucetInfo()); +} + UniValue faucetfund(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); uint64_t funds; std::string hex; From 096f8ed830f5f7c2c0aff310922b957d20ab33b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 30 Jul 2018 23:25:46 -1100 Subject: [PATCH 081/233] Faucet info --- src/cc/faucet.cpp | 2 +- src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/faucet.cpp b/src/cc/faucet.cpp index f171257dc..51d7f2f6f 100644 --- a/src/cc/faucet.cpp +++ b/src/cc/faucet.cpp @@ -189,7 +189,7 @@ std::string FaucetFund(uint64_t txfee,uint64_t funds) UniValue FaucetInfo() { - UniValue result(UniValue::VOBJ); + UniValue result(UniValue::VOBJ); char numstr[64]; CMutableTransaction mtx; CPubKey faucetpk; struct CCcontract_info *cp,C; uint64_t funding; result.push_back(Pair("result","success")); result.push_back(Pair("name","Faucet")); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 58799bc98..011d1d16e 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -363,6 +363,7 @@ static const CRPCCommand vRPCCommands[] = { "rewards", "rewardsaddress", &rewardsaddress, true }, /* faucet */ + { "faucet", "faucetinfo", &faucetinfo, true }, { "faucet", "faucetfund", &faucetfund, true }, { "faucet", "faucetget", &faucetget, true }, { "faucet", "faucetaddress", &faucetaddress, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 786c377d8..80d09b735 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -226,6 +226,7 @@ extern UniValue tokenfillswap(const UniValue& params, bool fHelp); extern UniValue faucetfund(const UniValue& params, bool fHelp); extern UniValue faucetget(const UniValue& params, bool fHelp); extern UniValue faucetaddress(const UniValue& params, bool fHelp); +extern UniValue faucetinfo(const UniValue& params, bool fHelp); extern UniValue rewardsinfo(const UniValue& params, bool fHelp); extern UniValue rewardslist(const UniValue& params, bool fHelp); extern UniValue rewardsaddress(const UniValue& params, bool fHelp); From aec296a50acc7166cc8d7f034103cde700799953 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:17:05 -1100 Subject: [PATCH 082/233] Add error checks --- src/cc/CCinclude.h | 2 +- src/cc/dice.cpp | 75 ++++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 1af6ef4d1..0be8a7e98 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -49,7 +49,7 @@ extern CWallet* pwalletMain; #endif bool GetAddressUnspent(uint160 addressHash, int type,std::vector > &unspentOutputs); -static uint256 zeroid; +static const uint256 zeroid; // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 154f07062..1c20df379 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -283,11 +283,11 @@ bool DiceIsmine(const CScript scriptPubKey) return(strcmp(destaddr,myaddr) == 0); } -int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) +int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction vinTx,uint256 bettorentropy,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks,uint256 fundingtxid) { - uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2,entropy; uint8_t funcid; + uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; - if ( (mustbeme == 0 || DiceIsmine(vinTx.vout[1].scriptPubKey) != 0) && vinTx.vout.size() > 0 ) + if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) { if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { @@ -314,7 +314,7 @@ int32_t DiceIsWinner(int32_t mustbeme,uint256 txid,CTransaction tx,CTransaction bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) { - uint256 txid,fundingtxid,hashBlock,hash,proof; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + uint256 txid,fundingtxid,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -357,7 +357,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'B' sbits fundingtxid entropy if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt"); - if ( (iswin= DiceIsWinner(1,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); } @@ -501,12 +501,24 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi return(totalinputs); } -bool DicePlanExists(uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) +bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t refsbits,CPubKey dicepk,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { char CCaddr[64]; uint64_t sbits; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; GetCCaddress(cp,CCaddr,dicepk); SetCCtxids(txids,CCaddr); + if ( fundingtxid != zeroid ) // avoid scan unless creating new funding plan + { + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) + { + if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' && sbits == refsbits ) + { + fundingPubKey = tx.vout[1].scriptPubKey; + return(true); + } + } + return(false); + } for (std::vector >::const_iterator it=txids.begin(); it!=txids.end(); it++) { //int height = it->first.blockHeight; @@ -519,6 +531,7 @@ bool DicePlanExists(uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t ref { if ( sbits == refsbits ) { + fundingPubKey = tx.vout[1].scriptPubKey; fundingtxid = txid; return(true); } @@ -528,7 +541,7 @@ bool DicePlanExists(uint256 &fundingtxid,struct CCcontract_info *cp,uint64_t ref return(false); } -struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) +struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { struct CCcontract_info *cp; uint256 fundingtxid; int32_t cmpflag; cp = CCinit(C,EVAL_DICE); @@ -541,7 +554,7 @@ struct CCcontract_info *Diceinit(uint256 reffundingtxid,struct CCcontract_info * cmpflag = 0; else cmpflag = 1; fundingtxid = zeroid; - if ( DicePlanExists(fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) + if ( DicePlanExists(fundingPubKey,fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); return(0); @@ -604,13 +617,14 @@ UniValue DiceList() std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) { - CMutableTransaction mtx; CPubKey mypk,dicepk; int64_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; + CMutableTransaction mtx; uint256 zero; CScript fundingPubKey; CPubKey mypk,dicepk; int64_t a,b,c,d; uint64_t sbits; struct CCcontract_info *cp,C; if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(zeroid,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) + memset(&zero,0,sizeof(zero)); + if ( (cp= Diceinit(fundingPubKey,zero,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) return(0); if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) { @@ -624,34 +638,37 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount) { - CMutableTransaction mtx; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; + CMutableTransaction mtx; CScript fundingPubKey,scriptPubKey; uint256 entropy,hentropy; CPubKey mypk,dicepk; uint64_t sbits; struct CCcontract_info *cp,C; int64_t minbet,maxbet,maxodds,timeoutblocks; if ( amount < 0 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + if ( scriptPubKey == fundingPubKey ) { - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy,zeroid))); - } else fprintf(stderr,"cant find enough inputs\n"); - fprintf(stderr,"cant find fundingtxid\n"); + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + { + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); + mtx.vout.push_back(CTxOut(txfee,scriptPubKey)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy,zeroid))); + } else fprintf(stderr,"cant find enough inputs\n"); + } else fprintf(stderr,"only fund creator can add more funds (entropy)\n"); return(0); } std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds) { - CMutableTransaction mtx; CPubKey mypk,dicepk; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; + CMutableTransaction mtx; CScript fundingPubKey; CPubKey mypk,dicepk; uint64_t sbits,entropyval; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropy,hentropy; struct CCcontract_info *cp,C; if ( bet < 0 || odds < 1 ) { fprintf(stderr,"negative parameter error\n"); return(0); } - if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( bet < minbet || bet > maxbet || odds > maxodds ) { @@ -678,14 +695,22 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; - if ( (cp= Diceinit(fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; + if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); + if ( winlosetimeout != 0 ) + { + scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + if ( scriptPubKey != fundingPubKey ) + { + fprintf(stderr,"only dice fund creator can submit winner or loser\n"); + return(0); + } + } if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); - // need to set hentropyproof - if ( (iswin= DiceIsWinner(1,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { if ( iswin == winlosetimeout ) { From 121e371d569be0e906da1e1435f3f30d452e47c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:20:12 -1100 Subject: [PATCH 083/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1c20df379..d06fa70c3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -695,7 +695,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CScript scriptPubKey; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; + CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( winlosetimeout != 0 ) From 32e3be8738049ff1e4ac85cf53eca0ad464671c5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:27:18 -1100 Subject: [PATCH 084/233] Change fund create marker out --- src/cc/dice.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d06fa70c3..2d32db9f7 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -40,8 +40,9 @@ createfunding: vins.*: normal inputs vout.0: CC vout for funding - vout.1: normal marker vout for easy searching - vout.2: normal change + vout.1: owner vout + vout.2: dice marker address vout for easy searching + vout.3: normal change vout.n-1: opreturn 'F' sbits minbet maxbet maxodds timeoutblocks addfunding (entropy): @@ -493,7 +494,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi first = 1; } } - else fprintf(stderr,"refsbits.%llx sbits.%llx nValue %.8f\n",(long long)refsbits,(long long)sbits,(double)nValue/COIN); + else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } @@ -626,9 +627,10 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t memset(&zero,0,sizeof(zero)); if ( (cp= Diceinit(fundingPubKey,zero,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) return(0); - if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,funds+3*txfee,64) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,dicepk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(dicepk)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceFundingOpRet('F',sbits,minbet,maxbet,maxodds,timeoutblocks))); } From 3c3451b8b63552e8f45ebcab139b2f4bb898cadd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:30:49 -1100 Subject: [PATCH 085/233] Optimize dice plan scan --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 2d32db9f7..d093f9332 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -507,7 +507,7 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac char CCaddr[64]; uint64_t sbits; uint256 txid,hashBlock; CTransaction tx; std::vector > txids; GetCCaddress(cp,CCaddr,dicepk); - SetCCtxids(txids,CCaddr); + SetCCtxids(txids,cp->normaladdr); if ( fundingtxid != zeroid ) // avoid scan unless creating new funding plan { if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) From 792cef0675fed4ee52f7082b1e758946484662e7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:36:50 -1100 Subject: [PATCH 086/233] Test --- src/cc/dice.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d093f9332..086fd5084 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -649,6 +649,17 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + { + uint8_t *ptr0,*ptr1; int32_t i; + ptr0 = (uint8_t *)scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; i<35; i++) + fprintf(stderr,"%02x",ptr0[i]); + fprintf(stderr," script vs "); + for (i=0; i<35; i++) + fprintf(stderr,"%02x",ptr1[i]); + fprintf(stderr," funding\n"); + } if ( scriptPubKey == fundingPubKey ) { if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) From 28500c3051f25ed020aa29d11de4932a57a6e808 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:40:26 -1100 Subject: [PATCH 087/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 086fd5084..f52292f7e 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -544,7 +544,7 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { - struct CCcontract_info *cp; uint256 fundingtxid; int32_t cmpflag; + struct CCcontract_info *cp; int32_t cmpflag; cp = CCinit(C,EVAL_DICE); if ( txfee == 0 ) txfee = 10000; @@ -555,7 +555,7 @@ struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,st cmpflag = 0; else cmpflag = 1; fundingtxid = zeroid; - if ( DicePlanExists(fundingPubKey,fundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) + if ( DicePlanExists(fundingPubKey,reffundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); return(0); From 6e26918746b4b5442ab88b6fccab545de2100b6c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:42:10 -1100 Subject: [PATCH 088/233] Test --- src/cc/dice.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f52292f7e..b0c849580 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -554,7 +554,6 @@ struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,st if ( reffundingtxid == zeroid ) cmpflag = 0; else cmpflag = 1; - fundingtxid = zeroid; if ( DicePlanExists(fundingPubKey,reffundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { fprintf(stderr,"Dice plan (%s) already exists\n",planstr); From 97b0bc3aeb3da96ccd4356a95ca71a13fdba3ed8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:44:52 -1100 Subject: [PATCH 089/233] Cmpflag --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b0c849580..c25c08f6c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -556,7 +556,7 @@ struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,st else cmpflag = 1; if ( DicePlanExists(fundingPubKey,reffundingtxid,cp,sbits,dicepk,minbet,maxbet,maxodds,timeoutblocks) != cmpflag ) { - fprintf(stderr,"Dice plan (%s) already exists\n",planstr); + fprintf(stderr,"Dice plan (%s) already exists cmpflag.%d\n",planstr,cmpflag); return(0); } return(cp); From 83da810cb371e379c5bbb3605d235aab1c29b296 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:46:47 -1100 Subject: [PATCH 090/233] Get fundingtxid --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c25c08f6c..004a97b5c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -510,7 +510,7 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac SetCCtxids(txids,cp->normaladdr); if ( fundingtxid != zeroid ) // avoid scan unless creating new funding plan { - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) + if ( GetTransaction(fundingtxid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,CCaddr,0) != 0 ) { if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' && sbits == refsbits ) { From 46a40047f341e063ceecd79b0ac55990a90c3862 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 00:50:19 -1100 Subject: [PATCH 091/233] & --- src/cc/dice.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 004a97b5c..02fb9c285 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -515,6 +515,7 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' && sbits == refsbits ) { fundingPubKey = tx.vout[1].scriptPubKey; + fprintf(stderr,"set fundingPubKey[%d]\n",(int32_t)fundingPubKey.size()); return(true); } } @@ -542,7 +543,7 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac return(false); } -struct CCcontract_info *Diceinit(CScript fundingPubKey,uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) +struct CCcontract_info *Diceinit(CScript &fundingPubKey,uint256 reffundingtxid,struct CCcontract_info *C,char *planstr,uint64_t &txfee,CPubKey &mypk,CPubKey &dicepk,uint64_t &sbits,int64_t &minbet,int64_t &maxbet,int64_t &maxodds,int64_t &timeoutblocks) { struct CCcontract_info *cp; int32_t cmpflag; cp = CCinit(C,EVAL_DICE); From 7377f6eaa7890dd4059a6ddf6be3273f987c3e9b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:09:22 -1100 Subject: [PATCH 092/233] DiceQueue --- src/cc/dice.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 02fb9c285..99ea821ad 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// hentropy_proof, timeout, validate, autoqueue, compare address to match house +// timeout, validate, autoqueue /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. @@ -92,6 +92,57 @@ timeout: #include "../endian.h" +static uint256 bidtxids[128]; + +struct dicefinish_info +{ + uint256 fundingtxid,bidtxid; + uint64_t sbits; + int32_t iswin; +}; + +void *dicefinish(void *_ptr) +{ + char str[65],str2[65],name[32]; int32_t i,duplicate=0; struct dicefinish_info *ptr; + ptr = (struct dicefinish_info *)_ptr; + sleep(1); + for (i=0; ibidtxid ) + { + duplicate = 1; + break; + } + if ( duplicate == 0 ) + { + for (i=0; ibidtxid; + break; + } + if ( i == sizeof(bidtxids)/sizeof(*bidtxids) ) + bidtxids[rand() % i] = ptr->bidtxid; + } + unstringbits(name,ptr->sbits); + fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%d\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bidtxid)); + free(ptr); + return(0); +} + +void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bidtxid) +{ + struct dicefinish_info *ptr = calloc(1,sizeof(*ptr)); + ptr->fundingtxid = fundingtxid; + ptr->bidtxid = bidtxid; + ptr->sbits = sbits; + ptr->iswin = iswin; + if ( ptr != 0 && pthread_create((pthread_t *)malloc(sizeof(pthread_t)),NULL,dicefinish,(void *)ptr) != 0 ) + { + fprintf(stderr,"DiceQueue.%d\n",iswin); + } // small memory leak per DiceQueue + return(0); +} + void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) { int32_t i,j=0; @@ -340,7 +391,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'F' sbits minbet maxbet maxodds timeoutblocks return eval->Invalid("unexpected DiceValidate for createfunding"); break; - case 'E': + case 'E': // check sig of vin to match fundingtxid //vins.*: normal inputs //vout.0: CC vout for locked entropy funds //vout.1: tag to owner address for entropy funds @@ -361,6 +412,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); + DiceQueue(iswin,sbits,fundingtxid,txid); } //return eval->Invalid("dont confirm bet during debug"); break; @@ -515,7 +567,6 @@ bool DicePlanExists(CScript &fundingPubKey,uint256 &fundingtxid,struct CCcontrac if ( DecodeDiceFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) == 'F' && sbits == refsbits ) { fundingPubKey = tx.vout[1].scriptPubKey; - fprintf(stderr,"set fundingPubKey[%d]\n",(int32_t)fundingPubKey.size()); return(true); } } @@ -649,6 +700,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + if ( 0 ) { uint8_t *ptr0,*ptr1; int32_t i; ptr0 = (uint8_t *)scriptPubKey.data(); @@ -666,7 +718,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); - mtx.vout.push_back(CTxOut(txfee,scriptPubKey)); + mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet('E',sbits,fundingtxid,hentropy,zeroid))); } else fprintf(stderr,"cant find enough inputs\n"); } else fprintf(stderr,"only fund creator can add more funds (entropy)\n"); From 6c2381533671c8dd1490f5c58dc10d298e6cb6fe Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:11:37 -1100 Subject: [PATCH 093/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 99ea821ad..22cba6cf3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// timeout, validate, autoqueue +// timeout, validate /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. @@ -124,7 +124,7 @@ void *dicefinish(void *_ptr) bidtxids[rand() % i] = ptr->bidtxid; } unstringbits(name,ptr->sbits); - fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%d\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bidtxid)); + fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bidtxid)); free(ptr); return(0); } From e8f9de07d1cad8708b640e56ae08aedaf1f71310 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:12:02 -1100 Subject: [PATCH 094/233] Test --- src/cc/dice.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 22cba6cf3..1d9dfd5bd 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -131,7 +131,7 @@ void *dicefinish(void *_ptr) void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bidtxid) { - struct dicefinish_info *ptr = calloc(1,sizeof(*ptr)); + struct dicefinish_info *ptr = (struct dicefinish_info *)calloc(1,sizeof(*ptr)); ptr->fundingtxid = fundingtxid; ptr->bidtxid = bidtxid; ptr->sbits = sbits; @@ -140,7 +140,6 @@ void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bidtxid) { fprintf(stderr,"DiceQueue.%d\n",iswin); } // small memory leak per DiceQueue - return(0); } void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) From b47b17439bc76c9f5845c9fec02f480c011d81ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:19:36 -1100 Subject: [PATCH 095/233] Add locks --- src/wallet/rpcwallet.cpp | 47 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 50c298e97..aa16727b7 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4966,7 +4966,9 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp) throw runtime_error("rewardscreatefunding name amount APR mindays maxdays mindeposit\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"); - // default to OOT params + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); + // default to OOT params APR = 5 * COIN; minseconds = maxseconds = 60 * 3600 * 24; mindeposit = 100 * COIN; @@ -5002,6 +5004,8 @@ UniValue rewardslock(const UniValue& params, bool fHelp) throw runtime_error("rewardslock name fundingtxid amount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; @@ -5021,6 +5025,8 @@ UniValue rewardsaddfunding(const UniValue& params, bool fHelp) throw runtime_error("rewardsaddfunding name fundingtxid amount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; @@ -5040,6 +5046,8 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp) throw runtime_error("rewardsunlock name fundingtxid [txid]\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); if ( params.size() > 2 ) @@ -5092,6 +5100,8 @@ UniValue faucetfund(const UniValue& params, bool fHelp) throw runtime_error("faucetfund amount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); funds = atof(params[0].get_str().c_str()) * COIN; hex = FaucetFund(0,funds); if ( hex.size() > 0 ) @@ -5109,6 +5119,8 @@ UniValue faucetget(const UniValue& params, bool fHelp) throw runtime_error("faucetget\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); hex = FaucetGet(0); if ( hex.size() > 0 ) { @@ -5125,6 +5137,8 @@ UniValue dicefund(const UniValue& params, bool fHelp) throw runtime_error("dicefund name funds minbet maxbet maxodds timeoutblocks\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); funds = atof(params[1].get_str().c_str()) * COIN; minbet = atof(params[2].get_str().c_str()) * COIN; @@ -5147,6 +5161,8 @@ UniValue diceaddfunds(const UniValue& params, bool fHelp) throw runtime_error("diceaddfunds name fundingtxid amount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; @@ -5166,6 +5182,8 @@ UniValue dicebet(const UniValue& params, bool fHelp) throw runtime_error("dicebet name fundingtxid amount odds\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); amount = atof(params[2].get_str().c_str()) * COIN; @@ -5186,6 +5204,8 @@ UniValue dicewinner(const UniValue& params, bool fHelp) throw runtime_error("dicewinner name fundingtxid bettxid\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); @@ -5205,6 +5225,8 @@ UniValue diceloser(const UniValue& params, bool fHelp) throw runtime_error("diceloser name fundingtxid bettxid\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); @@ -5224,6 +5246,8 @@ UniValue dicetimeout(const UniValue& params, bool fHelp) throw runtime_error("dicetimeout name fundingtxid bettxid\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); @@ -5299,6 +5323,7 @@ UniValue tokenbalance(const UniValue& params, bool fHelp) throw runtime_error("tokenbalance tokenid [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"); + LOCK(cs_main); tokenid = Parseuint256((char *)params[0].get_str().c_str()); if ( params.size() == 2 ) pubkey = ParseHex(params[1].get_str().c_str()); @@ -5319,6 +5344,8 @@ UniValue tokencreate(const UniValue& params, bool fHelp) throw runtime_error("tokencreate name supply description\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); name = params[0].get_str(); supply = atof(params[1].get_str().c_str()) * COIN; if ( params.size() == 3 ) @@ -5339,6 +5366,8 @@ UniValue tokentransfer(const UniValue& params, bool fHelp) throw runtime_error("tokentransfer tokenid destpubkey amount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); std::vector pubkey(ParseHex(params[1].get_str().c_str())); amount = atol(params[2].get_str().c_str()); @@ -5358,6 +5387,8 @@ UniValue tokenbid(const UniValue& params, bool fHelp) throw runtime_error("tokenbid numtokens tokenid price\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); numtokens = atoi(params[0].get_str().c_str()); tokenid = Parseuint256((char *)params[1].get_str().c_str()); price = atof(params[2].get_str().c_str()); @@ -5378,6 +5409,8 @@ UniValue tokencancelbid(const UniValue& params, bool fHelp) throw runtime_error("tokencancelbid tokenid bidtxid\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); bidtxid = Parseuint256((char *)params[1].get_str().c_str()); hex = CancelBuyOffer(0,tokenid,bidtxid); @@ -5396,6 +5429,8 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp) throw runtime_error("tokenfillbid tokenid bidtxid fillamount\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); bidtxid = Parseuint256((char *)params[1].get_str().c_str()); fillamount = atol(params[2].get_str().c_str()); @@ -5415,6 +5450,8 @@ UniValue tokenask(const UniValue& params, bool fHelp) throw runtime_error("tokenask numtokens tokenid price\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); numtokens = atoi(params[0].get_str().c_str()); tokenid = Parseuint256((char *)params[1].get_str().c_str()); price = atof(params[2].get_str().c_str()); @@ -5436,6 +5473,8 @@ UniValue tokenswapask(const UniValue& params, bool fHelp) throw runtime_error("tokenswapask numtokens tokenid otherid price\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); numtokens = atoi(params[0].get_str().c_str()); tokenid = Parseuint256((char *)params[1].get_str().c_str()); otherid = Parseuint256((char *)params[2].get_str().c_str()); @@ -5457,6 +5496,8 @@ UniValue tokencancelask(const UniValue& params, bool fHelp) throw runtime_error("tokencancelask tokenid asktxid\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); asktxid = Parseuint256((char *)params[1].get_str().c_str()); hex = CancelSell(0,tokenid,asktxid); @@ -5476,6 +5517,8 @@ UniValue tokenfillask(const UniValue& params, bool fHelp) throw runtime_error("tokenfillask tokenid asktxid fillunits\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); asktxid = Parseuint256((char *)params[1].get_str().c_str()); fillunits = atol(params[2].get_str().c_str()); @@ -5496,6 +5539,8 @@ UniValue tokenfillswap(const UniValue& params, bool fHelp) throw runtime_error("tokenfillswap tokenid otherid asktxid fillunits\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); tokenid = Parseuint256((char *)params[0].get_str().c_str()); otherid = Parseuint256((char *)params[1].get_str().c_str()); asktxid = Parseuint256((char *)params[2].get_str().c_str()); From 49f7caa268bee3f330d7445202c6b984b0fd07bb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:25:02 -1100 Subject: [PATCH 096/233] Unlock processnewblock --- src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 79182bad8..5371bed51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4551,11 +4551,11 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo auto verifier = libzcash::ProofVerifier::Disabled(); hash = pblock->GetHash(); //fprintf(stderr,"ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight); + if ( chainActive.LastTip() != 0 ) + komodo_currentheight_set(chainActive.LastTip()->nHeight); + checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); { LOCK(cs_main); - if ( chainActive.LastTip() != 0 ) - komodo_currentheight_set(chainActive.LastTip()->nHeight); - checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); bool fRequested = MarkBlockAsReceived(hash); fRequested |= fForceProcessing; if ( checked != 0 && komodo_checkPOW(0,pblock,height) < 0 ) //from_miner && ASSETCHAINS_STAKED == 0 From a839f3a384060d8b016dac4cb551c5715d47865d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:30:21 -1100 Subject: [PATCH 097/233] Test --- src/cc/dice.cpp | 2 +- src/main.cpp | 6 +++--- src/miner.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1d9dfd5bd..f905c77b4 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -411,7 +411,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); - DiceQueue(iswin,sbits,fundingtxid,txid); + //DiceQueue(iswin,sbits,fundingtxid,txid); } //return eval->Invalid("dont confirm bet during debug"); break; diff --git a/src/main.cpp b/src/main.cpp index 5371bed51..79182bad8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4551,11 +4551,11 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo auto verifier = libzcash::ProofVerifier::Disabled(); hash = pblock->GetHash(); //fprintf(stderr,"ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight); - if ( chainActive.LastTip() != 0 ) - komodo_currentheight_set(chainActive.LastTip()->nHeight); - checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); { LOCK(cs_main); + if ( chainActive.LastTip() != 0 ) + komodo_currentheight_set(chainActive.LastTip()->nHeight); + checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0); bool fRequested = MarkBlockAsReceived(hash); fRequested |= fForceProcessing; if ( checked != 0 && komodo_checkPOW(0,pblock,height) < 0 ) //from_miner && ASSETCHAINS_STAKED == 0 diff --git a/src/miner.cpp b/src/miner.cpp index fb2eb2d05..97034e72d 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1018,7 +1018,7 @@ void static BitcoinMiner() fprintf(stderr," mined %s block %d!\n",ASSETCHAINS_SYMBOL,Mining_height); } CValidationState state; - if ( !TestBlockValidity(state,B, chainActive.LastTip(), true, false)) + if ( ASSETCHAINS_CC == 0 && !TestBlockValidity(state,B, chainActive.LastTip(), true, false)) { h = UintToArith256(B.GetHash()); for (z=31; z>=0; z--) From ee90d3d7c80181c447adc10d03bc2f1821433801 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:35:25 -1100 Subject: [PATCH 098/233] Test --- src/cc/CCutils.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index daaa73ade..c4e5be3fb 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -259,15 +259,18 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param //txid = ctx.GetHash(); //if ( txid == cp->prevtxid ) // return(true); + fprintf(stderr,"process CC %02x\n",cp->evalcode); if ( paramsNull.size() != 0 ) // Don't expect params return eval->Invalid("Cannot have params"); else if ( ctx.vout.size() == 0 ) return eval->Invalid("no-vouts"); else if ( (*cp->validate)(cp,eval,ctx) != 0 ) { + fprintf(stderr,"done CC %02x\n",cp->evalcode); //cp->prevtxid = txid; return(true); } + fprintf(stderr,"invalid CC %02x\n",cp->evalcode); return(false); } From 63600b1d9b2f98c1d9d31c025388f57d971435c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:43:12 -1100 Subject: [PATCH 099/233] Use CC mutex --- src/cc/eval.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/eval.cpp b/src/cc/eval.cpp index f157b6197..d685c82cd 100644 --- a/src/cc/eval.cpp +++ b/src/cc/eval.cpp @@ -30,12 +30,15 @@ Eval* EVAL_TEST = 0; struct CCcontract_info CCinfos[0x100]; +extern pthread_mutex_t KOMODO_CC_mutex; bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn) { EvalRef eval; + pthread_mutex_lock(&KOMODO_CC_mutex); bool out = eval->Dispatch(cond, tx, nIn); - //fprintf(stderr,"out %d vs %d isValid\n",(int32_t)out,(int32_t)eval->state.IsValid()); + pthread_mutex_unlock(&KOMODO_CC_mutex); + //fprintf(stderr,"out %d vs %d isValid\n",(int32_t)out,(int32_t)eval->state.IsValid()); assert(eval->state.IsValid() == out); if (eval->state.IsValid()) return true; From fb89b4411dfcc15f63b3d84d4475ed1c30bda103 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:52:05 -1100 Subject: [PATCH 100/233] Test --- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 2 +- src/cc/dice.cpp | 2 +- src/cc/eval.cpp | 1 - src/cc/rewards.cpp | 1 + 5 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 0be8a7e98..29bdcf900 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -50,6 +50,7 @@ extern CWallet* pwalletMain; bool GetAddressUnspent(uint160 addressHash, int type,std::vector > &unspentOutputs); static const uint256 zeroid; +bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index c4e5be3fb..622a50fe8 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -277,7 +277,7 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param int64_t CCduration(uint256 txid) { CTransaction tx; uint256 hashBlock; uint32_t txtime=0; char str[65]; CBlockIndex *pindex; int64_t duration = 0; - if ( GetTransaction(txid,tx,hashBlock,false) == 0 ) + if ( myGetTransaction(txid,tx,hashBlock) == 0 ) { fprintf(stderr,"CCduration cant find duration txid %s\n",uint256_str(str,txid)); return(0); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f905c77b4..1d9dfd5bd 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -411,7 +411,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"DiceIsWinner.%d\n",iswin); - //DiceQueue(iswin,sbits,fundingtxid,txid); + DiceQueue(iswin,sbits,fundingtxid,txid); } //return eval->Invalid("dont confirm bet during debug"); break; diff --git a/src/cc/eval.cpp b/src/cc/eval.cpp index d685c82cd..95f5c33de 100644 --- a/src/cc/eval.cpp +++ b/src/cc/eval.cpp @@ -98,7 +98,6 @@ bool Eval::GetSpendsConfirmed(uint256 hash, std::vector &spends) c bool Eval::GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const { - bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); // there is a LOCK(cs_main) in the normal GetTransaction(), which leads to deadlocks //bool fAllowSlow = false; // Don't allow slow //return GetTransaction(hash, txOut, hashBlock, fAllowSlow); diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 88c92198a..c2f6dcd27 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -72,6 +72,7 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minsecon fprintf(stderr,"minseconds %llu maxseconds %llu\n",(long long)minseconds,(long long)maxseconds); if ( (duration= CCduration(txid)) < minseconds ) { + fprintf(stderr,"duration %llu < minseconds %llu\n",(long long)duration,(long long)minseconds); return(0); //duration = (uint32_t)time(NULL) - (1532713903 - 3600 * 24); } else if ( duration > maxseconds ) From 7adbb3e3c1b4b7e2dd5e125d25b9c6afe5d9fb68 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 03:56:20 -1100 Subject: [PATCH 101/233] Fix mining deadlock --- src/cc/CCutils.cpp | 6 +++--- src/miner.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 622a50fe8..84be0dbf3 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -259,18 +259,18 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param //txid = ctx.GetHash(); //if ( txid == cp->prevtxid ) // return(true); - fprintf(stderr,"process CC %02x\n",cp->evalcode); + //fprintf(stderr,"process CC %02x\n",cp->evalcode); if ( paramsNull.size() != 0 ) // Don't expect params return eval->Invalid("Cannot have params"); else if ( ctx.vout.size() == 0 ) return eval->Invalid("no-vouts"); else if ( (*cp->validate)(cp,eval,ctx) != 0 ) { - fprintf(stderr,"done CC %02x\n",cp->evalcode); + //fprintf(stderr,"done CC %02x\n",cp->evalcode); //cp->prevtxid = txid; return(true); } - fprintf(stderr,"invalid CC %02x\n",cp->evalcode); + //fprintf(stderr,"invalid CC %02x\n",cp->evalcode); return(false); } diff --git a/src/miner.cpp b/src/miner.cpp index 97034e72d..fb2eb2d05 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1018,7 +1018,7 @@ void static BitcoinMiner() fprintf(stderr," mined %s block %d!\n",ASSETCHAINS_SYMBOL,Mining_height); } CValidationState state; - if ( ASSETCHAINS_CC == 0 && !TestBlockValidity(state,B, chainActive.LastTip(), true, false)) + if ( !TestBlockValidity(state,B, chainActive.LastTip(), true, false)) { h = UintToArith256(B.GetHash()); for (z=31; z>=0; z--) From d4c504f04b09ad808fc2dbf00e07089640171999 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 04:39:57 -1100 Subject: [PATCH 102/233] Test --- src/cc/dice.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1d9dfd5bd..31aab4401 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -212,8 +212,8 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t if ( bettor >= house ) winnings = bet * odds; else winnings = 0; - fprintf(stderr,"bet %.8f at odds %d:1 %s vs %s\n",(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); - return(0); + fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1 %s vs %s\n",(double)winnings/COIN,(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); + return(winnings); } CScript EncodeDiceFundingOpRet(uint8_t funcid,uint64_t sbits,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks) @@ -544,9 +544,8 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi entropyval = tx.vout[0].nValue; first = 1; } - } - else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); - } else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); + } //else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); + } //else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } From 3528fb201d70709cedfb345687a83340f228f0ab Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 04:48:05 -1100 Subject: [PATCH 103/233] Test --- src/cc/dice.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 31aab4401..cb591078d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -103,7 +103,7 @@ struct dicefinish_info void *dicefinish(void *_ptr) { - char str[65],str2[65],name[32]; int32_t i,duplicate=0; struct dicefinish_info *ptr; + char str[65],str2[65],name[32]; std::string res; int32_t i,duplicate=0; struct dicefinish_info *ptr; ptr = (struct dicefinish_info *)_ptr; sleep(1); for (i=0; isbits); fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bidtxid)); + if ( duplicate == 0 ) + { + res = DiceWinLoseTimeout(0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + fprintf(stderr,"result.(%s)\n",res.c_str()); + } free(ptr); return(0); } From e787eaee4b59179a2e95545dc73f94ddf062169a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 04:50:01 -1100 Subject: [PATCH 104/233] Test --- src/cc/dice.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index cb591078d..77d47f44c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -92,11 +92,11 @@ timeout: #include "../endian.h" -static uint256 bidtxids[128]; +static uint256 bettxids[128]; struct dicefinish_info { - uint256 fundingtxid,bidtxid; + uint256 fundingtxid,bettxid; uint64_t sbits; int32_t iswin; }; @@ -106,25 +106,25 @@ void *dicefinish(void *_ptr) char str[65],str2[65],name[32]; std::string res; int32_t i,duplicate=0; struct dicefinish_info *ptr; ptr = (struct dicefinish_info *)_ptr; sleep(1); - for (i=0; ibidtxid ) + for (i=0; ibettxid ) { duplicate = 1; break; } if ( duplicate == 0 ) { - for (i=0; ibidtxid; + bettxids[i] = ptr->bettxid; break; } - if ( i == sizeof(bidtxids)/sizeof(*bidtxids) ) - bidtxids[rand() % i] = ptr->bidtxid; + if ( i == sizeof(bettxids)/sizeof(*bettxids) ) + bettxids[rand() % i] = ptr->bettxid; } unstringbits(name,ptr->sbits); - fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bidtxid)); + fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { res = DiceWinLoseTimeout(0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); @@ -134,11 +134,11 @@ void *dicefinish(void *_ptr) return(0); } -void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bidtxid) +void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bettxid) { struct dicefinish_info *ptr = (struct dicefinish_info *)calloc(1,sizeof(*ptr)); ptr->fundingtxid = fundingtxid; - ptr->bidtxid = bidtxid; + ptr->bettxid = bettxid; ptr->sbits = sbits; ptr->iswin = iswin; if ( ptr != 0 && pthread_create((pthread_t *)malloc(sizeof(pthread_t)),NULL,dicefinish,(void *)ptr) != 0 ) From a88b9554e3c2d324acf5a7b4c84e405c0ad3d56c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 04:58:22 -1100 Subject: [PATCH 105/233] Relay win/loss --- src/cc/CCinclude.h | 1 + src/cc/dice.cpp | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 29bdcf900..717fd2da6 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -51,6 +51,7 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vectorfundingtxid,ptr->bettxid,ptr->iswin); - fprintf(stderr,"result.(%s)\n",res.c_str()); + if ( is_hexstr((char *)res.c_str(),0) > 64 ) + { + CTransaction tx; uint256 txid; char str[65]; + LOCK(cs_main); + if ( DecodeHexTx(tx,res) != 0 ) + { + txid = tx.GetHash(); + RelayTransaction(tx); + fprintf(stderr,"result.(%s)\n",uint256_str(str,txid)); + } + } else fprintf(stderr,"result.(%s)\n",res.c_str()); } free(ptr); return(0); From 8dc5d27941ab5e62d003390ec072ea94535b3524 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:03:38 -1100 Subject: [PATCH 106/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ec3c3b0eb..3a16ea7ba 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -136,9 +136,9 @@ void *dicefinish(void *_ptr) { txid = tx.GetHash(); RelayTransaction(tx); - fprintf(stderr,"result.(%s)\n",uint256_str(str,txid)); + fprintf(stderr,"%s\nresult.(%s)\n",res.c_str(),uint256_str(str,txid)); } - } else fprintf(stderr,"result.(%s)\n",res.c_str()); + } else fprintf(stderr,"non-hex result.(%s)\n",res.c_str()); } free(ptr); return(0); From fde0fe57dc1907f7c1ee1487d8932818903d93b9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:32:30 -1100 Subject: [PATCH 107/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 3a16ea7ba..d5756990f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -128,7 +128,7 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { res = DiceWinLoseTimeout(0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( is_hexstr((char *)res.c_str(),0) > 64 ) + if ( res != 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { CTransaction tx; uint256 txid; char str[65]; LOCK(cs_main); From 260c79c3cc05455f525e9891b725f240e65f208f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:33:02 -1100 Subject: [PATCH 108/233] Fix --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d5756990f..68ef87362 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -128,7 +128,7 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { res = DiceWinLoseTimeout(0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( res != 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + if ( res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { CTransaction tx; uint256 txid; char str[65]; LOCK(cs_main); From cfdc3770651d3d1763693fc67f3735742f5b3228 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:36:16 -1100 Subject: [PATCH 109/233] Test --- src/cc/CCdice.h | 2 +- src/cc/dice.cpp | 10 ++++++---- src/wallet/rpcwallet.cpp | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 33f187391..8ecd7addd 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,7 +24,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); -std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); +std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 68ef87362..8e4b2ba73 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -127,10 +127,10 @@ void *dicefinish(void *_ptr) fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - res = DiceWinLoseTimeout(0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + CTransaction tx; uint256 txid; char str[65]; int32_t result; + res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + if ( result != 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { - CTransaction tx; uint256 txid; char str[65]; LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { @@ -771,9 +771,10 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) +std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; + *resultp = 0; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); if ( winlosetimeout != 0 ) @@ -832,6 +833,7 @@ std::string DiceWinLoseTimeout(uint64_t txfee,char *planstr,uint256 fundingtxid, mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); } hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + *resultp = 1; return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return(0); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index aa16727b7..9b7b31ff2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5199,7 +5199,7 @@ UniValue dicebet(const UniValue& params, bool fHelp) UniValue dicewinner(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; if ( fHelp || params.size() != 3 ) throw runtime_error("dicewinner name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5209,7 +5209,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,1); + hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5220,7 +5220,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) UniValue diceloser(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; if ( fHelp || params.size() != 3 ) throw runtime_error("diceloser name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5230,7 +5230,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,-1); + hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,-1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5241,7 +5241,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) UniValue dicetimeout(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; if ( fHelp || params.size() != 3 ) throw runtime_error("dicetimeout name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5251,7 +5251,7 @@ UniValue dicetimeout(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(0,name,fundingtxid,bettxid,0); + hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,0); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From ec48f0c36159ec3ea09ecf5b6bb998ad2da1e0b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:37:35 -1100 Subject: [PATCH 110/233] Test --- src/wallet/rpcwallet.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9b7b31ff2..b2094adce 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5199,7 +5199,7 @@ UniValue dicebet(const UniValue& params, bool fHelp) UniValue dicewinner(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; if ( fHelp || params.size() != 3 ) throw runtime_error("dicewinner name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5209,7 +5209,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,1); + hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5220,7 +5220,7 @@ UniValue dicewinner(const UniValue& params, bool fHelp) UniValue diceloser(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; if ( fHelp || params.size() != 3 ) throw runtime_error("diceloser name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5230,7 +5230,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,-1); + hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,-1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5241,7 +5241,7 @@ UniValue diceloser(const UniValue& params, bool fHelp) UniValue dicetimeout(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t result; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; if ( fHelp || params.size() != 3 ) throw runtime_error("dicetimeout name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5251,7 +5251,7 @@ UniValue dicetimeout(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&result,0,name,fundingtxid,bettxid,0); + hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,0); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From f4d2c1a3ef80039e4279a420fe312adaaccbeefc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:40:47 -1100 Subject: [PATCH 111/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 8e4b2ba73..227957621 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -129,7 +129,7 @@ void *dicefinish(void *_ptr) { CTransaction tx; uint256 txid; char str[65]; int32_t result; res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) @@ -138,7 +138,7 @@ void *dicefinish(void *_ptr) RelayTransaction(tx); fprintf(stderr,"%s\nresult.(%s)\n",res.c_str(),uint256_str(str,txid)); } - } else fprintf(stderr,"non-hex result.(%s)\n",res.c_str()); + } } free(ptr); return(0); From 113adfd46d81f670fb350a60392a4292b68c73c5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 05:43:38 -1100 Subject: [PATCH 112/233] Test --- src/cc/dice.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 227957621..a4030390d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -776,14 +776,14 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; *resultp = 0; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) - return(0); + return("0"); if ( winlosetimeout != 0 ) { scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( scriptPubKey != fundingPubKey ) { fprintf(stderr,"only dice fund creator can submit winner or loser\n"); - return(0); + return("0"); } } if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) @@ -808,7 +808,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin if ( odds < 1 || odds > maxodds ) { fprintf(stderr,"illegal odds.%d vs maxodds.%d\n",(int32_t)odds,(int32_t)maxodds); - return(0); + return("0"); } CCchange = betTx.vout[0].nValue; fundsneeded = 2*txfee + (odds-1)*betTx.vout[1].nValue; @@ -823,7 +823,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin else { fprintf(stderr,"not enough inputs for %.8f\n",(double)fundsneeded/COIN); - return(0); + return("0"); } } else @@ -836,8 +836,8 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin *resultp = 1; return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); - } else return(0); + } else return("0"); } - return(0); + return("0"); } From c8624d4024a261a1b9f182118f527aa23d61de82 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 06:34:34 -1100 Subject: [PATCH 113/233] Bet --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a4030390d..612911451 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -124,7 +124,7 @@ void *dicefinish(void *_ptr) bettxids[rand() % i] = ptr->bettxid; } unstringbits(name,ptr->sbits); - fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bid.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); + fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { CTransaction tx; uint256 txid; char str[65]; int32_t result; From 7137a0223ed01687708eb4fdd44429e8993f71de Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 19:19:33 -1100 Subject: [PATCH 114/233] FSM skeleton --- src/Makefile.am | 2 +- src/cc/CCcustom.cpp | 30 ++++++++--------- src/cc/{CCponzi.h => CCfsm.h} | 13 ++++---- src/cc/dice.cpp | 2 +- src/cc/eval.h | 2 +- src/cc/{ponzi.cpp => fsm.cpp} | 61 +++++++++++++++++++---------------- src/rpcserver.cpp | 7 ++-- src/rpcserver.h | 5 ++- src/wallet/rpcwallet.cpp | 51 ++++++++++++++++++++++++++--- 9 files changed, 113 insertions(+), 60 deletions(-) rename src/cc/{CCponzi.h => CCfsm.h} (79%) rename src/cc/{ponzi.cpp => fsm.cpp} (74%) diff --git a/src/Makefile.am b/src/Makefile.am index a44839781..8b10c06e0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -266,7 +266,7 @@ libbitcoin_server_a_SOURCES = \ cc/rewards.cpp \ cc/dice.cpp \ cc/lotto.cpp \ - cc/ponzi.cpp \ + cc/fsm.cpp \ cc/auction.cpp \ cc/betprotocol.cpp \ chain.cpp \ diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 2b1c83b20..9bf1d5449 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -20,7 +20,7 @@ #include "CCdice.h" #include "CCauction.h" #include "CClotto.h" -#include "CCponzi.h" +#include "CCfsm.h" /* CCcustom has most of the functions that need to be extended to create a new CC contract. @@ -106,13 +106,13 @@ uint8_t LottoCCpriv[32] = { 0xb4, 0xac, 0xc2, 0xd9, 0x67, 0x34, 0xd7, 0x58, 0x80 #undef FUNCNAME #undef EVALCODE -// Ponzi -#define FUNCNAME IsPonziInput -#define EVALCODE EVAL_PONZI -const char *PonziCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; -const char *PonziNormaladdr = "RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; -char PonziCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; -uint8_t PonziCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6, 0x34, 0xe1, 0x21, 0xb8, 0x02, 0xb9, 0x4b, 0x12, 0x37, 0x8f, 0xa0, 0x86, 0x23, 0x50, 0xb2, 0x5f, 0xe4, 0xe7, 0x36, 0x0f, 0xda, 0xae, 0xfc }; +// Finite State Machine +#define FUNCNAME IsFSMInput +#define EVALCODE EVAL_FSM +const char *FSMCCaddr = "RUKTbLBeKgHkm3Ss4hKZP3ikuLW1xx7B2x"; +const char *FSMNormaladdr = "RWSHRbxnJYLvDjpcQ2i8MekgP6h2ctTKaj"; +char FSMCChexstr[67] = { "039b52d294b413b07f3643c1a28c5467901a76562d8b39a785910ae0a0f3043810" }; +uint8_t FSMCCpriv[32] = { 0x11, 0xe1, 0xea, 0x3e, 0xdb, 0x36, 0xf0, 0xa8, 0xc6, 0x34, 0xe1, 0x21, 0xb8, 0x02, 0xb9, 0x4b, 0x12, 0x37, 0x8f, 0xa0, 0x86, 0x23, 0x50, 0xb2, 0x5f, 0xe4, 0xe7, 0x36, 0x0f, 0xda, 0xae, 0xfc }; #include "CCcustom.inc" #undef FUNCNAME #undef EVALCODE @@ -173,13 +173,13 @@ struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode) cp->validate = LottoValidate; cp->ismyvin = IsLottoInput; break; - case EVAL_PONZI: - strcpy(cp->unspendableCCaddr,PonziCCaddr); - strcpy(cp->normaladdr,PonziNormaladdr); - strcpy(cp->CChexstr,PonziCChexstr); - memcpy(cp->CCpriv,PonziCCpriv,32); - cp->validate = PonziValidate; - cp->ismyvin = IsPonziInput; + case EVAL_FSM: + strcpy(cp->unspendableCCaddr,FSMCCaddr); + strcpy(cp->normaladdr,FSMNormaladdr); + strcpy(cp->CChexstr,FSMCChexstr); + memcpy(cp->CCpriv,FSMCCpriv,32); + cp->validate = FSMValidate; + cp->ismyvin = IsFSMInput; break; case EVAL_AUCTION: strcpy(cp->unspendableCCaddr,AuctionCCaddr); diff --git a/src/cc/CCponzi.h b/src/cc/CCfsm.h similarity index 79% rename from src/cc/CCponzi.h rename to src/cc/CCfsm.h index eb28737f7..bf9d48a98 100644 --- a/src/cc/CCponzi.h +++ b/src/cc/CCfsm.h @@ -14,16 +14,17 @@ ******************************************************************************/ -#ifndef CC_PONZI_H -#define CC_PONZI_H +#ifndef CC_FSM_H +#define CC_FSM_H #include "CCinclude.h" -#define EVAL_PONZI 0xe7 +#define EVAL_FSM 0xe7 -bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); +bool FSMValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -std::string PonziBuy(uint64_t txfee,uint64_t amount); -std::string PonziClaim(uint64_t txfee); +std::string FSMlist(uint64_t txfee); +std::string FSMinfo(uint64_t txfee,uint256 fsmtxid); +std::string FSMcreate(uint64_t txfee,std::string name,std::string states); #endif diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 612911451..b0fb3dc21 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// timeout, validate +// timeout, validate, verify win/loss can be used for entropy /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. diff --git a/src/cc/eval.h b/src/cc/eval.h index 9d69c5fd5..77c592a16 100644 --- a/src/cc/eval.h +++ b/src/cc/eval.h @@ -44,7 +44,7 @@ EVAL(EVAL_FAUCET, 0xe4) \ EVAL(EVAL_REWARDS, 0xe5) \ EVAL(EVAL_DICE, 0xe6) \ - EVAL(EVAL_PONZI, 0xe7) \ + EVAL(EVAL_FSM, 0xe7) \ EVAL(EVAL_AUCTION, 0xe8) \ EVAL(EVAL_LOTTO, 0xe9) diff --git a/src/cc/ponzi.cpp b/src/cc/fsm.cpp similarity index 74% rename from src/cc/ponzi.cpp rename to src/cc/fsm.cpp index d39f087dd..8ec54a56b 100644 --- a/src/cc/ponzi.cpp +++ b/src/cc/fsm.cpp @@ -13,7 +13,7 @@ * * ******************************************************************************/ -#include "CCponzi.h" +#include "CCfsm.h" #include "../txmempool.h" /* @@ -21,7 +21,7 @@ // start of consensus code -uint64_t IsPonzivout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) +uint64_t IsFSMvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) { char destaddr[64]; if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) @@ -32,7 +32,7 @@ uint64_t IsPonzivout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v return(0); } -bool PonziExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) +bool FSMExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) { static uint256 zerohash; CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; @@ -50,8 +50,8 @@ bool PonziExactAmounts(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 ponzi from mempool"); - if ( (assetoshis= IsPonzivout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) + return eval->Invalid("cant FSM from mempool"); + if ( (assetoshis= IsFSMvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) inputs += assetoshis; } } @@ -59,7 +59,7 @@ bool PonziExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction for (i=0; iInvalid("illegal normal vini"); } } //fprintf(stderr,"check amounts\n"); - if ( PonziExactAmounts(cp,eval,tx,1,10000) == false ) + if ( FSMExactAmounts(cp,eval,tx,1,10000) == false ) { - fprintf(stderr,"ponziget invalid amount\n"); + fprintf(stderr,"fsmget invalid amount\n"); return false; } else { preventCCvouts = 1; - if ( IsPonzivout(cp,tx,0) != 0 ) + if ( IsFSMvout(cp,tx,0) != 0 ) { preventCCvouts++; i = 1; } else i = 0; if ( tx.vout[i].nValue != COIN ) - return eval->Invalid("invalid ponzi output"); + return eval->Invalid("invalid fsm output"); retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); if ( retval != 0 ) - fprintf(stderr,"ponziget validated\n"); - else fprintf(stderr,"ponziget invalid\n"); + fprintf(stderr,"fsmget validated\n"); + else fprintf(stderr,"fsmget invalid\n"); return(retval); } } @@ -117,7 +117,7 @@ bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) // helper functions for rpc calls in rpcwallet.cpp -uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +uint64_t AddFSMInputs(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; std::vector > unspentOutputs; @@ -131,7 +131,7 @@ uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub continue; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( (nValue= IsPonzivout(cp,vintx,(int32_t)it->first.index)) > 0 ) + if ( (nValue= IsFSMvout(cp,vintx,(int32_t)it->first.index)) > 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript())); @@ -146,37 +146,42 @@ uint64_t AddPonziInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub return(totalinputs); } -std::string PonziBuy(uint64_t txfee,uint64_t amount) +std::string FSMlist(uint64_t txfee) { - CMutableTransaction mtx; CPubKey mypk,ponzipk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_PONZI); + return(0); +} + +std::string FSMcreate(uint64_t txfee,std::string name,std::string states) +{ + CMutableTransaction mtx; CPubKey mypk,fsmpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FSM); if ( txfee == 0 ) txfee = 10000; - ponzipk = GetUnspendable(cp,0); + fsmpk = GetUnspendable(cp,0); mypk = pubkey2pk(Mypubkey()); - if ( (inputs= AddPonziInputs(cp,mtx,ponzipk,nValue+txfee,60)) > 0 ) + if ( (inputs= AddFSMInputs(cp,mtx,fsmpk,nValue+txfee,60)) > 0 ) { if ( inputs > nValue ) CCchange = (inputs - nValue - txfee); if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,CCchange,ponzipk)); + mtx.vout.push_back(MakeCC1vout(EVAL_FSM,CCchange,fsmpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); - } else fprintf(stderr,"cant find ponzi inputs\n"); + } else fprintf(stderr,"cant find fsm inputs\n"); return(0); } -std::string PonziClaim(uint64_t txfee) +std::string FSMinfo(uint64_t txfee,uint256 fsmtxid) { - CMutableTransaction mtx; CPubKey mypk,ponzipk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_PONZI); + CMutableTransaction mtx; CPubKey mypk,fsmpk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_FSM); if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); - ponzipk = GetUnspendable(cp,0); + fsmpk = GetUnspendable(cp,0); if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) { - mtx.vout.push_back(MakeCC1vout(EVAL_PONZI,funds,ponzipk)); + mtx.vout.push_back(MakeCC1vout(EVAL_FSM,funds,fsmpk)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } return(0); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 011d1d16e..3e92ab7b8 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -350,8 +350,11 @@ static const CRPCCommand vRPCCommands[] = /* lotto */ { "lotto", "lottoaddress", &lottoaddress, true }, - /* ponzi */ - { "ponzi", "ponziaddress", &ponziaddress, true }, + /* fsm */ + { "FSM", "FSMaddress", &FSMaddress, true }, + { "FSMcreate", "FSMcreate", &FSMcreate, true }, + { "FSMlist", "FSMlist", &FSMlist, true }, + { "FSMinfo", "FSMinfo", &FSMinfo, true }, /* rewards */ { "rewards", "rewardslist", &rewardslist, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 80d09b735..3e25fc4cc 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -244,7 +244,10 @@ extern UniValue dicewinner(const UniValue& params, bool fHelp); extern UniValue diceloser(const UniValue& params, bool fHelp); extern UniValue dicetimeout(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); -extern UniValue ponziaddress(const UniValue& params, bool fHelp); +extern UniValue FSMaddress(const UniValue& params, bool fHelp); +extern UniValue FSMcreate(const UniValue& params, bool fHelp); +extern UniValue FSMlist(const UniValue& params, bool fHelp); +extern UniValue FSMinfo(const UniValue& params, bool fHelp); extern UniValue auctionaddress(const UniValue& params, bool fHelp); extern UniValue getnewaddress(const UniValue& params, bool fHelp); // in rpcwallet.cpp diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b2094adce..c36e9f38b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4844,7 +4844,7 @@ int32_t ensure_CCrequirements() #include "../cc/CCassets.h" #include "../cc/CCrewards.h" #include "../cc/CCdice.h" -#include "../cc/CCponzi.h" +#include "../cc/CCfsm.h" #include "../cc/CCauction.h" #include "../cc/CClotto.h" @@ -4881,17 +4881,17 @@ UniValue lottoaddress(const UniValue& params, bool fHelp) return(CCaddress(cp,(char *)"Lotto",pubkey)); } -UniValue ponziaddress(const UniValue& params, bool fHelp) +UniValue FSMaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_PONZI); + cp = CCinit(&C,EVAL_FSM); if ( fHelp || params.size() > 1 ) - throw runtime_error("ponziaddress [pubkey]\n"); + throw runtime_error("FSMaddress [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 *)"Ponzi",pubkey)); + return(CCaddress(cp,(char *)"FSM",pubkey)); } UniValue auctionaddress(const UniValue& params, bool fHelp) @@ -5083,6 +5083,47 @@ UniValue rewardsinfo(const UniValue& params, bool fHelp) return(RewardsInfo(fundingtxid)); } +UniValue FSMcreate(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); std::string name,states,hex; + if ( fHelp || params.size() != 2 ) + throw runtime_error("FSMcreate name states\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"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); + name = params[0].get_str(); + states = params[1].get_str(); + hex = FSMcreate(0,name,states); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else result.push_back(Pair("error", "couldnt create FSM transaction")); + return(result); +} + +UniValue FSMlist(const UniValue& params, bool fHelp) +{ + uint256 tokenid; + if ( fHelp || params.size() > 0 ) + throw runtime_error("FSMlist\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"); + return(FSMList()); +} + +UniValue FSMinfo(const UniValue& params, bool fHelp) +{ + uint256 FSMtxid; + if ( fHelp || params.size() != 1 ) + throw runtime_error("FSMinfo fundingtxid\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"); + fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); + return(FSMInfo(FSMtxid)); +} + UniValue faucetinfo(const UniValue& params, bool fHelp) { uint256 fundingtxid; From d9cf43a5567fd9fad54cd03e6632e1e545b51e29 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 19:31:58 -1100 Subject: [PATCH 115/233] Test --- src/cc/CCfsm.h | 6 +++--- src/cc/fsm.cpp | 6 +++--- src/wallet/rpcwallet.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cc/CCfsm.h b/src/cc/CCfsm.h index bf9d48a98..7e6056d2e 100644 --- a/src/cc/CCfsm.h +++ b/src/cc/CCfsm.h @@ -23,8 +23,8 @@ bool FSMValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -std::string FSMlist(uint64_t txfee); -std::string FSMinfo(uint64_t txfee,uint256 fsmtxid); -std::string FSMcreate(uint64_t txfee,std::string name,std::string states); +std::string FSMList(uint64_t txfee); +std::string FSMInfo(uint64_t txfee,uint256 fsmtxid); +std::string FSMCreate(uint64_t txfee,std::string name,std::string states); #endif diff --git a/src/cc/fsm.cpp b/src/cc/fsm.cpp index 8ec54a56b..2f7c0629a 100644 --- a/src/cc/fsm.cpp +++ b/src/cc/fsm.cpp @@ -146,12 +146,12 @@ uint64_t AddFSMInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKe return(totalinputs); } -std::string FSMlist(uint64_t txfee) +std::string FSMList(uint64_t txfee) { return(0); } -std::string FSMcreate(uint64_t txfee,std::string name,std::string states) +std::string FSMCreate(uint64_t txfee,std::string name,std::string states) { CMutableTransaction mtx; CPubKey mypk,fsmpk; CScript opret; uint64_t inputs,CCchange=0,nValue=COIN; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_FSM); @@ -171,7 +171,7 @@ std::string FSMcreate(uint64_t txfee,std::string name,std::string states) return(0); } -std::string FSMinfo(uint64_t txfee,uint256 fsmtxid) +std::string FSMInfo(uint64_t txfee,uint256 fsmtxid) { CMutableTransaction mtx; CPubKey mypk,fsmpk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_FSM); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c36e9f38b..b403c6ac5 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5094,7 +5094,7 @@ UniValue FSMcreate(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); name = params[0].get_str(); states = params[1].get_str(); - hex = FSMcreate(0,name,states); + hex = FSMCreate(0,name,states); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5120,7 +5120,7 @@ UniValue FSMinfo(const UniValue& params, bool fHelp) throw runtime_error("FSMinfo fundingtxid\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"); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); + FSMtxid = Parseuint256((char *)params[0].get_str().c_str()); return(FSMInfo(FSMtxid)); } From 3f53fb0b5b9a85bb8ef1a4d1b0b50b73af8db4b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 19:34:06 -1100 Subject: [PATCH 116/233] Test --- src/cc/CCfsm.h | 4 ++-- src/cc/fsm.cpp | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cc/CCfsm.h b/src/cc/CCfsm.h index 7e6056d2e..c6ea17024 100644 --- a/src/cc/CCfsm.h +++ b/src/cc/CCfsm.h @@ -23,8 +23,8 @@ bool FSMValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); -std::string FSMList(uint64_t txfee); -std::string FSMInfo(uint64_t txfee,uint256 fsmtxid); +std::string FSMList(); +std::string FSMInfo(uint256 fsmtxid); std::string FSMCreate(uint64_t txfee,std::string name,std::string states); #endif diff --git a/src/cc/fsm.cpp b/src/cc/fsm.cpp index 2f7c0629a..d3bfe0caf 100644 --- a/src/cc/fsm.cpp +++ b/src/cc/fsm.cpp @@ -146,7 +146,7 @@ uint64_t AddFSMInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKe return(totalinputs); } -std::string FSMList(uint64_t txfee) +std::string FSMList() { return(0); } @@ -171,12 +171,10 @@ std::string FSMCreate(uint64_t txfee,std::string name,std::string states) return(0); } -std::string FSMInfo(uint64_t txfee,uint256 fsmtxid) +std::string FSMInfo(uint256 fsmtxid) { CMutableTransaction mtx; CPubKey mypk,fsmpk; uint64_t funds = 0; CScript opret; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_FSM); - if ( txfee == 0 ) - txfee = 10000; mypk = pubkey2pk(Mypubkey()); fsmpk = GetUnspendable(cp,0); if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) From 1b2632f5ae5f03f8f953571e3335fea45e904cbb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 19:34:51 -1100 Subject: [PATCH 117/233] Stub --- src/cc/fsm.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/cc/fsm.cpp b/src/cc/fsm.cpp index d3bfe0caf..201535992 100644 --- a/src/cc/fsm.cpp +++ b/src/cc/fsm.cpp @@ -177,11 +177,6 @@ std::string FSMInfo(uint256 fsmtxid) cp = CCinit(&C,EVAL_FSM); mypk = pubkey2pk(Mypubkey()); fsmpk = GetUnspendable(cp,0); - if ( AddNormalinputs(mtx,mypk,txfee,64) > 0 ) - { - mtx.vout.push_back(MakeCC1vout(EVAL_FSM,funds,fsmpk)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); - } return(0); } From 4d2b732331aeeecbdcd2ea34f8349d50e5836526 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 21:49:21 -1100 Subject: [PATCH 118/233] Dice validation --- src/cc/CCutils.cpp | 1 + src/cc/dice.cpp | 240 +++++++++++++++++++++++++-------------------- 2 files changed, 134 insertions(+), 107 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 84be0dbf3..fff113d30 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -256,6 +256,7 @@ CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv) bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector paramsNull,const CTransaction &ctx, unsigned int nIn) { CTransaction createTx; uint256 assetid,assetid2,hashBlock; uint8_t funcid; int32_t i,n; uint64_t amount; std::vector origpubkey; + // there is a chance CC tx is valid in mempool, but invalid when in block, so we cant filter duplicate requests. if any of the vins are spent, for example //txid = ctx.GetHash(); //if ( txid == cp->prevtxid ) // return(true); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b0fb3dc21..532e77434 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -53,7 +53,7 @@ addfunding (entropy): vout.n-1: opreturn 'E' sbits fundingtxid hentropy bet: - vin.0: entropy txid from house + vin.0: entropy txid from house (must validate vin0 of 'E') vins.1+: normal inputs vout.0: CC vout for locked entropy vout.1: CC vout for locked bet @@ -61,32 +61,20 @@ bet: vout.3: change vout.n-1: opreturn 'B' sbits fundingtxid entropy -winner: - vin.0: betTx CC vout.0 entropy from bet - vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' - vout.0: funding CC change to entropy owner - vout.1: tag to owner address for entropy funds - vout.2: normal output to bettor's address - vout.n-1: opreturn 'W' sbits fundingtxid hentropy - loser: vin.0: betTx CC vout.0 entropy from bet vin.1: betTx CC vout.1 bet amount from bet vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC to entropy owner vout.1: tag to owner address for entropy funds - vout.n-1: opreturn 'L' sbits fundingtxid hentropy + vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof + +winner: + same as loser, but vout.2 is winnings + vout.n-1: opreturn 'W' sbits fundingtxid hentropy proof timeout: - vin.0: betTx CC vout.0 entropy from bet - vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' - vout.0: funding CC vin.0 to entropy owner - vout.1: tag to owner address for entropy funds - vout.2: normal vin.1 to bettor's address - vout.n-1: opreturn 'T' sbits fundingtxid hentropy - + same as winner, just without hentropy or proof */ @@ -306,10 +294,9 @@ uint64_t IsDicevout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) return(0); } -bool DiceExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx,uint64_t txfee) +int64_t DiceAmounts(uint64_t &inputs,uint64_t &outputs,struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) { - static uint256 zerohash; - CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis; + CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t assetoshis; numvins = tx.vin.size(); numvouts = tx.vout.size(); for (i=0; iInvalid("always should find vin, but didnt"); else { - if ( hashBlock == zerohash ) - return eval->Invalid("cant dice from mempool"); if ( (assetoshis= IsDicevout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) inputs += assetoshis; } @@ -333,12 +318,7 @@ bool DiceExactAmounts(struct CCcontract_info *cp,Eval *eval,const CTransaction & if ( (assetoshis= IsDicevout(cp,tx,i)) != 0 ) outputs += assetoshis; } - if ( inputs != outputs+txfee ) - { - fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs); - return eval->Invalid("mismatched inputs != outputs + txfee"); - } - else return(true); + return(inputs - outputs); } bool DiceIsmine(const CScript scriptPubKey) @@ -353,9 +333,9 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction { uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; - if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 ) + if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') && sbits == vinsbits && fundingtxid == vinfundingtxid ) + if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) @@ -378,9 +358,14 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction return(0); } -bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) +bool DiceVerifyTimeout(CTransaction &betTx,int32_t timeoutblocks) { - uint256 txid,fundingtxid,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits,amount,reward,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript scriptPubKey; CTransaction fundingTx,vinTx; + return(false); +} + +bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) +{ + uint256 txid,fundingtxid,vinfundingtxid,vinhentropy,vinproof,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks,odds,winnings; uint64_t vinsbits,sbits,amount,inputs,outputs,txfee=10000; int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript fundingPubKey; CTransaction fundingTx,vinTx,vinofvinTx; char CCaddr[64]; numvins = tx.vin.size(); numvouts = tx.vout.size(); preventCCvins = preventCCvouts = -1; @@ -395,6 +380,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return eval->Invalid("cant find fundingtxid"); else if ( fundingTx.vout.size() > 0 && DecodeDiceFundingOpRet(fundingTx.vout[fundingTx.vout.size()-1].scriptPubKey,sbits,minbet,maxbet,maxodds,timeoutblocks) != 'F' ) return eval->Invalid("fundingTx not valid"); + fundingPubKey = fundingTx.vout[1].scriptPubKey; switch ( funcid ) { case 'F': @@ -405,7 +391,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.n-1: opreturn 'F' sbits minbet maxbet maxodds timeoutblocks return eval->Invalid("unexpected DiceValidate for createfunding"); break; - case 'E': // check sig of vin to match fundingtxid + case 'E': // check sig of vin to match fundingtxid in the 'B' tx //vins.*: normal inputs //vout.0: CC vout for locked entropy funds //vout.1: tag to owner address for entropy funds @@ -421,65 +407,89 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) //vout.2: tag for bettor's address (txfee + odds) //vout.3: change //vout.n-1: opreturn 'B' sbits fundingtxid entropy - if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) - return eval->Invalid("always should find vin.0, but didnt"); + preventCCvouts = 2; + preventCCvins = 1; + if ( IsCCInput(tx.vin[0].scriptSig) == 0 ) + return eval->Invalid("vin.0 is normal for bet"); + else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) + return eval->Invalid("vout.0 is normal for bet"); + else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) + return eval->Invalid("vout.1 is normal for bet"); + else if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) + return eval->Invalid("always should find vin.0, but didnt for bet"); + else if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) + return eval->Invalid("entropy tx not fundingPubKey for bet"); + else if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,vinTx.vout[tx.vin[0].prevout.n].nValue) == 0 ) + return eval->Invalid("vout[0] != entropy nValue for bet"); + else if ( ConstrainVout(tx.vout[1],1,cp->unspendableCCaddr,0) == 0 ) + return eval->Invalid("vout[1] constrain violation for bet"); + else if ( tx.vout[2].nValue >= txfee+maxodds || tx.vout[2].nValue < txfee ) + return eval->Invalid("vout[2] nValue violation for bet"); + else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 ) + return eval->Invalid("always should find vinofvin.0, but didnt for bet"); + else if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + return eval->Invalid("vin0 of entropy tx not fundingPubKey for bet"); + else if ( vinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) + return eval->Invalid("vout.0 nValue != entropy nValue for bet"); if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { - fprintf(stderr,"DiceIsWinner.%d\n",iswin); + // will only happen for fundingPubKey DiceQueue(iswin,sbits,fundingtxid,txid); } - //return eval->Invalid("dont confirm bet during debug"); break; case 'L': - //vin.0: betTx CC vout.0 entropy from bet - //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' - //vout.0: funding CC to entropy owner - //vout.1: tag to owner address for entropy funds - //vout.n-1: opreturn 'L' sbits fundingtxid hentropy - break; case 'W': - //vin.0: betTx CC vout.0 entropy from bet - //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' - //vout.0: funding CC change to entropy owner - //vout.1: tag to owner address for entropy funds - //vout.2: normal output to bettor's address - //vout.n-1: opreturn 'W' sbits fundingtxid hentropy - break; case 'T': //vin.0: betTx CC vout.0 entropy from bet //vin.1: betTx CC vout.1 bet amount from bet //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' - //vout.0: funding CC vin.0 to entropy owner //vout.1: tag to owner address for entropy funds - //vout.2: normal vin.1 to bettor's address - //vout.n-1: opreturn 'T' sbits fundingtxid hentropy - /*for (i=0; iismyvin)(tx.vin[i].scriptSig) == 0 ) - return eval->Invalid("unexpected normal vin for unlock"); - } - if ( DiceExactAmounts(cp,eval,tx,txfee+tx.vout[1].nValue) == 0 ) - return false; + preventCCvouts = 1; + DiceAmounts(inputs,outputs,cp,eval,tx); + if ( IsCCInput(tx.vin[0].scriptSig) == 0 || IsCCInput(tx.vin[1].scriptSig) == 0 ) + return eval->Invalid("vin0 or vin1 normal vin for bet"); + else if ( tx.vin[0].prevout.hash != tx.vin[1].prevout.hash ) + return eval->Invalid("vin0 != vin1 prevout.hash for bet"); else if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) - return eval->Invalid("always should find vin.0, but didnt"); - else if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("lock tx vout.0 is normal output"); - else if ( tx.vout.size() < 3 ) - return eval->Invalid("unlock tx not enough vouts"); - else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("unlock tx vout.0 is normal output"); - else if ( tx.vout[1].scriptPubKey.IsPayToCryptoCondition() != 0 ) - return eval->Invalid("unlock tx vout.1 is CC output"); - else if ( tx.vout[1].scriptPubKey != vinTx.vout[1].scriptPubKey ) - return eval->Invalid("unlock tx vout.1 mismatched scriptPubKey"); - amount = vinTx.vout[0].nValue; - reward = 0; - //reward = DiceCalc(amount,tx.vin[0].prevout.hash,minbet,maxbet,maxodds,timeoutblocks); - if ( tx.vout[1].nValue > amount+reward ) - return eval->Invalid("unlock tx vout.1 isnt amount+reward"); - preventCCvouts = 1;*/ + return eval->Invalid("always should find vin.0, but didnt for wlt"); + else if ( vinTx.vout.size() < 3 || DecodeDiceOpRet(tx.vin[0].prevout.hash,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,vinhentropy,vinproof) != 'B' ) + return eval->Invalid("not betTx for vin0/1 for wlt"); + else if ( sbits != vinsbits || fundingtxid != vinfundingtxid ) + return eval->Invalid("sbits or fundingtxid mismatch for wlt"); + else if ( fundingPubKey != tx.vout[1].scriptPubKey ) + return eval->Invalid("tx.vout[1] != fundingPubKey for wlt"); + if ( funcid == 'L' ) + { + //vout.0: funding CC to entropy owner + //vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof + if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,inputs-txfee) == 0 ) + return eval->Invalid("vout[0] != inputs-txfee for loss"); + iswin = -1; + } + else + { + //vout.0: funding CC change to entropy owner + //vout.2: normal output to bettor's address + //vout.n-1: opreturn 'W' sbits fundingtxid hentropy proof + odds = vinTx.vout[2].nValue - txfee; + if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 ) + return eval->Invalid("vout[0] != inputs-txfee for win/timeout"); + else if ( tx.vout[2].scriptPubKey != vinTx.vout[2].scriptPubKey ) + return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); + else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) + return eval->Invalid("vout[2] payut mismatch for win/timeout"); + else if ( input != output+tx.vout[2].nValue-txfee ) + return eval->Invalid("CC funds mismatch for win/timeout"); + iswin = (funcid == 'W'); + } + if ( iswin != 0 ) + { + winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,hash); + if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) + return eval->Invalid("DiceCalc mismatch for win/loss"); + } + else if ( DiceVerifyTimeout(vinTx,timeoutblocks) == 0 ) + return eval->Invalid("invalid timeout claim for timeout"); break; } } @@ -488,7 +498,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx) return(true); } -uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) +uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs) { char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 txid,hash,proof,hashBlock,fundingtxid; CTransaction tx; int32_t j,vout,n = 0; uint8_t funcid; std::vector > unspentOutputs; @@ -511,31 +521,27 @@ uint64_t AddDiceInputs(CScript &scriptPubKey,int32_t fundsflag,struct CCcontract if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); - if ( fundsflag != 0 && funcid != 'F' && funcid != 'E' && funcid != 'W' && funcid != 'L' && funcid != 'T' ) - continue; - else if ( fundsflag == 0 && funcid != 'B' ) - continue; - if ( total != 0 && maxinputs != 0 ) + if ( funcid == 'F' || funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T' ) { - if ( fundsflag == 0 ) - scriptPubKey = tx.vout[1].scriptPubKey; - mtx.vin.push_back(CTxIn(txid,vout,CScript())); + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,vout,CScript())); + totalinputs += it->second.satoshis; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; } - totalinputs += it->second.satoshis; - n++; - if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) - break; } else fprintf(stderr,"null funcid\n"); } } return(totalinputs); } -uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbits,struct CCcontract_info *cp,CPubKey pk,uint256 reffundingtxid) +uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbits,struct CCcontract_info *cp,CPubKey dicepk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CTransaction tx; int32_t vout,first=0; uint8_t funcid; + char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CScript fundingPubKey; CTransaction tx,vinTx; int32_t vout,first=0,n=0; uint8_t funcid; std::vector > unspentOutputs; - GetCCaddress(cp,coinaddr,pk); + fundingPubKey = CScript() << ParseHex(HexStr(dicepk)) << OP_CHECKSIG; + GetCCaddress(cp,coinaddr,dicepk); SetCCunspents(unspentOutputs,coinaddr); entropyval = 0; for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) @@ -544,17 +550,26 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi vout = (int32_t)it->first.index; if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - char str[65],str2[65]; + //char str[65],str2[65]; if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { if ( (funcid == 'F' && reffundingtxid == txid) || reffundingtxid == fundingtxid ) { - if ( funcid != 'B' && refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 0 ) + if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 10000 && (funcid == 'F' || funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') ) { + if ( funcid != 'F' && funcid != 'T' ) + n++; totalinputs += nValue; - fprintf(stderr,"add %.8f\n",(double)nValue/COIN); - if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') ) + if ( first == 0 && fundingPubKey == tx.vout[1].scriptPubKey && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) { + if ( funcid == 'E' ) + { + if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + { + fprintf(stderr,"entropy vin[0] != fundingPubKey\n"); + continue; + } + } entropytxid = txid; entropyval = tx.vout[0].nValue; first = 1; @@ -564,6 +579,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } } + fprintf(stderr,"numentropy tx %d: %.8f\n",n,(double)totalinputs/COIN); return(totalinputs); } @@ -773,7 +789,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin; uint64_t entropyval,sbits; + CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return("0"); @@ -789,7 +805,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); - if ( (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) + if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { if ( iswin == winlosetimeout ) { @@ -799,9 +815,18 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin if ( iswin == 0 ) { funcid = 'T'; - fprintf(stderr,"timeout refunds are not supported yet\n"); + if ( DiceVerifyTimeout(betTx,timeoutblocks) == 0 ) // hasnt timed out yet + { + fprintf(stderr,"timeout is not supported yet\n"); + return(0); + } + else + { + hentropy = hentropyproof = zeroid; + iswin = 1; + } } - else if ( iswin > 0 ) + if ( iswin > 0 ) { funcid = 'W'; odds = (betTx.vout[2].nValue - txfee); @@ -812,7 +837,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin } CCchange = betTx.vout[0].nValue; fundsneeded = 2*txfee + (odds-1)*betTx.vout[1].nValue; - if ( (inputs= AddDiceInputs(scriptPubKey,1,cp,mtx,dicepk,fundsneeded,60)) > 0 ) + if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,60)) > 0 ) { if ( inputs > fundsneeded+txfee ) CCchange += (inputs - (fundsneeded+txfee)); @@ -832,7 +857,8 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin mtx.vout.push_back(MakeCC1vout(cp->evalcode,betTx.vout[0].nValue + betTx.vout[1].nValue - txfee,dicepk)); mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); } - hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); + if ( winlosetimeout != 0 ) + hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); *resultp = 1; return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); From aa78e86048b521716a8711fe22601de2a74763c0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 21:50:24 -1100 Subject: [PATCH 119/233] Fix --- src/cc/dice.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 532e77434..07a63ef5d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -478,7 +478,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( input != output+tx.vout[2].nValue-txfee ) + else if ( inputs != output+tx.vout[2].nValue-txfee ) return eval->Invalid("CC funds mismatch for win/timeout"); iswin = (funcid == 'W'); } @@ -520,7 +520,6 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK { if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { - fprintf(stderr,"fundsflag.%d (%c) %.8f %.8f\n",fundsflag,funcid,(double)tx.vout[vout].nValue/COIN,(double)it->second.satoshis/COIN); if ( funcid == 'F' || funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T' ) { if ( total != 0 && maxinputs != 0 ) From 4e12f16ccf015f4833d51fa26def91d791c60020 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 21:50:57 -1100 Subject: [PATCH 120/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 07a63ef5d..5e4e1fde3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -478,7 +478,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( inputs != output+tx.vout[2].nValue-txfee ) + else if ( inputs != outputs+tx.vout[2].nValue-txfee ) return eval->Invalid("CC funds mismatch for win/timeout"); iswin = (funcid == 'W'); } From 14debc6dfe39650ab174e340c55c55a9d99b5164 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 21:54:49 -1100 Subject: [PATCH 121/233] Test --- src/cc/dice.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 5e4e1fde3..533e2c123 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -557,21 +557,27 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( refsbits == sbits && (nValue= IsDicevout(cp,tx,vout)) > 10000 && (funcid == 'F' || funcid == 'E' || funcid == 'W' || funcid == 'L' || funcid == 'T') ) { if ( funcid != 'F' && funcid != 'T' ) - n++; - totalinputs += nValue; - if ( first == 0 && fundingPubKey == tx.vout[1].scriptPubKey && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) { - if ( funcid == 'E' ) + n++; + fprintf(stderr,"(%c %.8f) ",funcid,(double)nValue/COIN); + } + totalinputs += nValue; + if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) + { + if ( fundingPubKey == tx.vout[1].scriptPubKey ) { - if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + if ( funcid == 'E' ) { - fprintf(stderr,"entropy vin[0] != fundingPubKey\n"); - continue; + if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + { + fprintf(stderr,"entropy vin[0] != fundingPubKey\n"); + continue; + } } - } - entropytxid = txid; - entropyval = tx.vout[0].nValue; - first = 1; + entropytxid = txid; + entropyval = tx.vout[0].nValue; + first = 1; + } else fprintf(stderr,"(%c) fundingPubKey mismatch\n",funcid); } } //else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); } //else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); From 04a4df805007cba19680381db64bd24bfc54d68a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:01:48 -1100 Subject: [PATCH 122/233] Test --- src/cc/dice.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 533e2c123..ceb9b409a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -568,7 +568,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( funcid == 'E' ) { - if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[1].scriptPubKey != fundingPubKey || tx.vin[0].prevout.n != 0 ) { fprintf(stderr,"entropy vin[0] != fundingPubKey\n"); continue; @@ -577,7 +577,19 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi entropytxid = txid; entropyval = tx.vout[0].nValue; first = 1; - } else fprintf(stderr,"(%c) fundingPubKey mismatch\n",funcid); + } + else + { + uint8_t *ptr0,*ptr1; int32_t i; + ptr0 = (uint8_t *)tx.vout[1].scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; i fundsneeded+txfee ) CCchange += (inputs - (fundsneeded+txfee)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); - mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); + mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); mtx.vout.push_back(CTxOut(odds * betTx.vout[1].nValue,betTx.vout[2].scriptPubKey)); } else @@ -860,7 +872,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin { funcid = 'L'; mtx.vout.push_back(MakeCC1vout(cp->evalcode,betTx.vout[0].nValue + betTx.vout[1].nValue - txfee,dicepk)); - mtx.vout.push_back(CTxOut(txfee,entropyTx.vout[1].scriptPubKey)); + mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); } if ( winlosetimeout != 0 ) hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); From d3cc1ec118c72d39fc9079b06a59232804a7e7d4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:06:26 -1100 Subject: [PATCH 123/233] Tsp --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ceb9b409a..e643ee9e1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -746,7 +746,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(0); scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; - if ( 0 ) + if ( 1 ) { uint8_t *ptr0,*ptr1; int32_t i; ptr0 = (uint8_t *)scriptPubKey.data(); From a1b43551c9d1709b5d239ab2aa2ae5a856bdcccc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:12:04 -1100 Subject: [PATCH 124/233] Fix fundingPubKey --- src/cc/dice.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e643ee9e1..d6ede2d52 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -539,7 +539,10 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CScript fundingPubKey; CTransaction tx,vinTx; int32_t vout,first=0,n=0; uint8_t funcid; std::vector > unspentOutputs; - fundingPubKey = CScript() << ParseHex(HexStr(dicepk)) << OP_CHECKSIG; + if ( GetTransaction(reffundingtxid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) != 0 ) + { + fundingPubKey = tx.vout[1].scriptPubKey; + } else return(0); GetCCaddress(cp,coinaddr,dicepk); SetCCunspents(unspentOutputs,coinaddr); entropyval = 0; From 385f6db046425e27d2a088f3596151f60c2b5b5d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:13:50 -1100 Subject: [PATCH 125/233] Test --- src/cc/dice.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d6ede2d52..a91df06b8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -573,7 +573,15 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[1].scriptPubKey != fundingPubKey || tx.vin[0].prevout.n != 0 ) { - fprintf(stderr,"entropy vin[0] != fundingPubKey\n"); + uint8_t *ptr0,*ptr1; int32_t i; + ptr0 = (uint8_t *)tx.vout[1].scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; i Date: Tue, 31 Jul 2018 22:15:30 -1100 Subject: [PATCH 126/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a91df06b8..54eb65711 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -574,14 +574,14 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[1].scriptPubKey != fundingPubKey || tx.vin[0].prevout.n != 0 ) { uint8_t *ptr0,*ptr1; int32_t i; - ptr0 = (uint8_t *)tx.vout[1].scriptPubKey.data(); + ptr0 = (uint8_t *)vinTx.vout[1].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); for (i=0; i Date: Tue, 31 Jul 2018 22:18:26 -1100 Subject: [PATCH 127/233] Test --- src/cc/dice.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 54eb65711..e7ab4153e 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -571,7 +571,12 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( funcid == 'E' ) { - if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout[1].scriptPubKey != fundingPubKey || tx.vin[0].prevout.n != 0 ) + if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.size() < 2 ) + { + fprintf(stderr,"cant find entropy tx or vin0prev %d\n",tx.vin[0].prevout.n); + continue; + } + if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; ptr0 = (uint8_t *)vinTx.vout[1].scriptPubKey.data(); From b13e77bb0833ab6abe9fc63d8ddf4c927accb73e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:19:01 -1100 Subject: [PATCH 128/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e7ab4153e..0ad1c1578 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -571,7 +571,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( funcid == 'E' ) { - if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.size() < 2 ) + if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout.size() < 2 ) { fprintf(stderr,"cant find entropy tx or vin0prev %d\n",tx.vin[0].prevout.n); continue; From e9c753662f73092d97d8726f1b865999357304c1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:30:02 -1100 Subject: [PATCH 129/233] +print --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0ad1c1578..298b8a68c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -537,7 +537,7 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbits,struct CCcontract_info *cp,CPubKey dicepk,uint256 reffundingtxid) { - char coinaddr[64]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CScript fundingPubKey; CTransaction tx,vinTx; int32_t vout,first=0,n=0; uint8_t funcid; + char coinaddr[64],str[65]; uint64_t sbits,nValue,totalinputs = 0; uint256 hash,txid,proof,hashBlock,fundingtxid; CScript fundingPubKey; CTransaction tx,vinTx; int32_t vout,first=0,n=0; uint8_t funcid; std::vector > unspentOutputs; if ( GetTransaction(reffundingtxid,tx,hashBlock,false) != 0 && tx.vout.size() > 1 && ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) != 0 ) { @@ -573,7 +573,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout.size() < 2 ) { - fprintf(stderr,"cant find entropy tx or vin0prev %d\n",tx.vin[0].prevout.n); + fprintf(stderr,"cant find entropy vin0 %s or vin0prev %d\n",uint256_str(str,tx.vin[0].prevout.hash),tx.vin[0].prevout.n); continue; } if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) From 4b4113ddd0fdaf5578641ad332ddb8324b31890f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:34:48 -1100 Subject: [PATCH 130/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 298b8a68c..c91748a41 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -571,9 +571,9 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( funcid == 'E' ) { - if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 || vinTx.vout.size() < 2 ) + if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 ) { - fprintf(stderr,"cant find entropy vin0 %s or vin0prev %d\n",uint256_str(str,tx.vin[0].prevout.hash),tx.vin[0].prevout.n); + fprintf(stderr,"cant find entropy vin0 %s or vin0prev %d vouts[%d]\n",uint256_str(str,tx.vin[0].prevout.hash),tx.vin[0].prevout.n,(int32_t)vinTx.vout.size()); continue; } if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) From f7af30aece4db3f4a975a14c0f621a3d40701e8d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:36:29 -1100 Subject: [PATCH 131/233] tx.vin[0].prevout.n --- src/cc/dice.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c91748a41..9ef53abd0 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -571,17 +571,17 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( funcid == 'E' ) { - if ( tx.vin[0].prevout.n != 0 || GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 ) + if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 ) { fprintf(stderr,"cant find entropy vin0 %s or vin0prev %d vouts[%d]\n",uint256_str(str,tx.vin[0].prevout.hash),tx.vin[0].prevout.n,(int32_t)vinTx.vout.size()); continue; } - if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) + if ( vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; - ptr0 = (uint8_t *)vinTx.vout[1].scriptPubKey.data(); + ptr0 = (uint8_t *)vinTx.vout[tx.vin[0].prevout.n].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; i Date: Tue, 31 Jul 2018 22:40:45 -1100 Subject: [PATCH 132/233] Test --- src/cc/dice.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9ef53abd0..a366c1313 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -299,6 +299,7 @@ int64_t DiceAmounts(uint64_t &inputs,uint64_t &outputs,struct CCcontract_info *c CTransaction vinTx; uint256 hashBlock; int32_t i,numvins,numvouts; uint64_t assetoshis; numvins = tx.vin.size(); numvouts = tx.vout.size(); + inputs = outputs = 0; for (i=0; iismyvin)(tx.vin[i].scriptSig) != 0 ) From 53f6ead6e3f611afb932138f0d324b19dac89028 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:47:03 -1100 Subject: [PATCH 133/233] Test --- src/cc/dice.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a366c1313..6b926b3b1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -485,7 +485,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) } if ( iswin != 0 ) { - winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,hash); + entropy = DiceGetEntropy(vinTx,'B'); + winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) return eval->Invalid("DiceCalc mismatch for win/loss"); } From 8306464319e168f780c8cd25dd0ef50cdbf767bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:49:09 -1100 Subject: [PATCH 134/233] Patch regtest segfault --- src/komodo_bitcoind.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index 3901c4ee7..89e79ccbd 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -676,7 +676,7 @@ int32_t komodo_block2height(CBlock *block) if ( height2 >= 0 ) return(height2); } - if ( block->vtx[0].vin.size() > 0 ) + if ( block != 0 && block->vtx[0].vin.size() > 0 ) { #ifdef KOMODO_ZCASH ptr = (uint8_t *)block->vtx[0].vin[0].scriptSig.data(); From aa760cb89278bc2b6a8e842964890b59c896c537 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:53:00 -1100 Subject: [PATCH 135/233] Test --- src/cc/dice.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6b926b3b1..06d0f9bf8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -201,6 +201,8 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); return(0); } + fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uin256_str(str2,bettorentropy)); + endiancpy(buf,(uint8_t *)&houseentropy,32); endiancpy(&buf[32],(uint8_t *)&bettorentropy,32); vcalc_sha256(0,(uint8_t *)&_house,buf,64); @@ -485,7 +487,9 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) } if ( iswin != 0 ) { + char str[65],str2[65]; entropy = DiceGetEntropy(vinTx,'B'); + fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uin256_str(str2,entropy)); winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) return eval->Invalid("DiceCalc mismatch for win/loss"); From b97fa94e72092da1feaf0bb73e8cf14e3c6b027f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:54:59 -1100 Subject: [PATCH 136/233] Test --- src/cc/dice.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 06d0f9bf8..865985b25 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -887,6 +887,8 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin if ( winlosetimeout != 0 ) hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); *resultp = 1; + char str[65],str2[65]; + fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uin256_str(str2,bettorentropy)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return("0"); From 0cab9ce2f9585d6d595cf5ee478da9521e8a81e7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 22:59:24 -1100 Subject: [PATCH 137/233] Test --- src/cc/dice.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 865985b25..00085278d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -187,6 +187,8 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr,"%02x",ssecret2.bytes[i]); fprintf(stderr," ssecret2 dont match\n"); } + char str[65],str2[65]; + fprintf(stderr,"generated hentropy.%s <- entropy.%s\n",uint256_str(str,hentropy),uint256_str(str2,entropy)); return(hentropy); } @@ -201,7 +203,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); return(0); } - fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uin256_str(str2,bettorentropy)); + fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uint256_str(str2,bettorentropy)); endiancpy(buf,(uint8_t *)&houseentropy,32); endiancpy(&buf[32],(uint8_t *)&bettorentropy,32); @@ -489,7 +491,9 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) { char str[65],str2[65]; entropy = DiceGetEntropy(vinTx,'B'); - fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uin256_str(str2,entropy)); + vcalc_sha256(0,(uint8_t *)&hash,entropy,32); + fprintf(stderr,"calculated hentropy.%s\n",uint256_str(str,hash)); + fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uint256_str(str2,entropy)); winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) return eval->Invalid("DiceCalc mismatch for win/loss"); @@ -888,7 +892,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); *resultp = 1; char str[65],str2[65]; - fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uin256_str(str2,bettorentropy)); + fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return("0"); From e7479ef8aa4eb6a442a171eef1576a875398048f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 23:01:08 -1100 Subject: [PATCH 138/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 00085278d..eebe46667 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -491,7 +491,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) { char str[65],str2[65]; entropy = DiceGetEntropy(vinTx,'B'); - vcalc_sha256(0,(uint8_t *)&hash,entropy,32); + vcalc_sha256(0,(uint8_t *)&hash,(uint8_t *)&proof,32); fprintf(stderr,"calculated hentropy.%s\n",uint256_str(str,hash)); fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uint256_str(str2,entropy)); winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); From 36dcc2e6e26652fef0b18f4fca1d3badfa13b6a1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 23:24:15 -1100 Subject: [PATCH 139/233] Test --- src/cc/dice.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index eebe46667..33d37e1d1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -188,7 +188,7 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr," ssecret2 dont match\n"); } char str[65],str2[65]; - fprintf(stderr,"generated hentropy.%s <- entropy.%s\n",uint256_str(str,hentropy),uint256_str(str2,entropy)); + fprintf(stderr,"generated house hentropy.%s <- entropy.%s\n",uint256_str(str,hentropy),uint256_str(str2,entropy)); return(hentropy); } @@ -203,6 +203,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); return(0); } + house = bettor = zeroid; fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uint256_str(str2,bettorentropy)); endiancpy(buf,(uint8_t *)&houseentropy,32); @@ -212,7 +213,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t endiancpy(buf,(uint8_t *)&bettorentropy,32); endiancpy(&buf[32],(uint8_t *)&houseentropy,32); - vcalc_sha256(0,(uint8_t *)&_house,buf,64); + vcalc_sha256(0,(uint8_t *)&_bettor,buf,64); endiancpy((uint8_t *)&bettor,_bettor,32); if ( odds > 1 ) bettor = (bettor / arith_uint256(odds)); @@ -492,7 +493,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) char str[65],str2[65]; entropy = DiceGetEntropy(vinTx,'B'); vcalc_sha256(0,(uint8_t *)&hash,(uint8_t *)&proof,32); - fprintf(stderr,"calculated hentropy.%s\n",uint256_str(str,hash)); + fprintf(stderr,"calculated house hentropy.%s\n",uint256_str(str,hash)); fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uint256_str(str2,entropy)); winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) From aee90e8209ff4f1cbfc3917206c5bd2964e1d820 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 23:25:01 -1100 Subject: [PATCH 140/233] Test --- src/cc/dice.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 33d37e1d1..f807c59e5 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -203,7 +203,6 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); return(0); } - house = bettor = zeroid; fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uint256_str(str2,bettorentropy)); endiancpy(buf,(uint8_t *)&houseentropy,32); From d2c1a2f29cb9df84231ed4225a4a06a7272d50a3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 23:48:05 -1100 Subject: [PATCH 141/233] +print --- src/cc/dice.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f807c59e5..bbea093d3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -484,7 +484,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); else if ( inputs != outputs+tx.vout[2].nValue-txfee ) + { + fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f - txfee\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); + } iswin = (funcid == 'W'); } if ( iswin != 0 ) From 3454564bc7bdb05eaa4bd1e298eace79d12340bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 31 Jul 2018 23:56:31 -1100 Subject: [PATCH 142/233] Test --- src/cc/dice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index bbea093d3..ede88f730 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -483,7 +483,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( inputs != outputs+tx.vout[2].nValue-txfee ) + else if ( inputs != (outputs + tx.vout[2].nValue + 2*txfee) ) { fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f - txfee\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); @@ -873,8 +873,8 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin fundsneeded = 2*txfee + (odds-1)*betTx.vout[1].nValue; if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,60)) > 0 ) { - if ( inputs > fundsneeded+txfee ) - CCchange += (inputs - (fundsneeded+txfee)); + if ( inputs > fundsneeded ) + CCchange += (inputs - fundsneeded); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); mtx.vout.push_back(CTxOut(odds * betTx.vout[1].nValue,betTx.vout[2].scriptPubKey)); From ef979a505d9b412ee33556e3a45dafc4ae003017 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 00:09:57 -1100 Subject: [PATCH 143/233] -print --- src/cc/dice.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ede88f730..2919cbb4b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -141,7 +141,7 @@ void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bettxid) ptr->iswin = iswin; if ( ptr != 0 && pthread_create((pthread_t *)malloc(sizeof(pthread_t)),NULL,dicefinish,(void *)ptr) != 0 ) { - fprintf(stderr,"DiceQueue.%d\n",iswin); + //fprintf(stderr,"DiceQueue.%d\n",iswin); } // small memory leak per DiceQueue } @@ -187,8 +187,8 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx fprintf(stderr,"%02x",ssecret2.bytes[i]); fprintf(stderr," ssecret2 dont match\n"); } - char str[65],str2[65]; - fprintf(stderr,"generated house hentropy.%s <- entropy.%s\n",uint256_str(str,hentropy),uint256_str(str2,entropy)); + //char str[65],str2[65]; + //fprintf(stderr,"generated house hentropy.%s <- entropy.%s\n",uint256_str(str,hentropy),uint256_str(str2,entropy)); return(hentropy); } @@ -203,7 +203,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t fprintf(stderr,"bet size violation %.8f\n",(double)bet/COIN); return(0); } - fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uint256_str(str2,bettorentropy)); + //fprintf(stderr,"calc house entropy %s vs bettor %s\n",uint256_str(str,houseentropy),uint256_str(str2,bettorentropy)); endiancpy(buf,(uint8_t *)&houseentropy,32); endiancpy(&buf[32],(uint8_t *)&bettorentropy,32); @@ -492,11 +492,11 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) } if ( iswin != 0 ) { - char str[65],str2[65]; + //char str[65],str2[65]; entropy = DiceGetEntropy(vinTx,'B'); vcalc_sha256(0,(uint8_t *)&hash,(uint8_t *)&proof,32); - fprintf(stderr,"calculated house hentropy.%s\n",uint256_str(str,hash)); - fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uint256_str(str2,entropy)); + //fprintf(stderr,"calculated house hentropy.%s\n",uint256_str(str,hash)); + //fprintf(stderr,"verify house entropy %s vs bettor %s\n",uint256_str(str,proof),uint256_str(str2,entropy)); winnings = DiceCalc(vinTx.vout[1].nValue,vinTx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,proof,entropy); if ( (winnings == 0 && iswin > 0) || (winnings > 0 && iswin < 0) ) return eval->Invalid("DiceCalc mismatch for win/loss"); @@ -523,7 +523,7 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK vout = (int32_t)it->first.index; if ( it->second.satoshis < 1000000 ) continue; - fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); + //fprintf(stderr,"(%s) %s/v%d %.8f\n",coinaddr,uint256_str(str,txid),vout,(double)it->second.satoshis/COIN); for (j=0; j Date: Wed, 1 Aug 2018 00:21:48 -1100 Subject: [PATCH 144/233] Test --- src/cc/dice.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 2919cbb4b..f0c60ba3c 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -433,7 +433,18 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); else if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + { + uint8_t *ptr0,*ptr1; int32_t i; + ptr0 = (uint8_t *)vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; iInvalid("vin0 of entropy tx not fundingPubKey for bet"); + } else if ( vinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) return eval->Invalid("vout.0 nValue != entropy nValue for bet"); if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) From 7f63070bc9c613ebc3d895216452b639a4cd2c9c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 00:26:31 -1100 Subject: [PATCH 145/233] Test --- src/cc/dice.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f0c60ba3c..bc405e05a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -430,20 +430,20 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[1] constrain violation for bet"); else if ( tx.vout[2].nValue >= txfee+maxodds || tx.vout[2].nValue < txfee ) return eval->Invalid("vout[2] nValue violation for bet"); - else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 ) + else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() > 1 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); - else if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + else if ( vinofvinTx.vout[1].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; - ptr0 = (uint8_t *)vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey.data(); + ptr0 = (uint8_t *)vinofvinTx.vout[1].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; iInvalid("vin0 of entropy tx not fundingPubKey for bet"); + return eval->Invalid("vin1 of entropy tx not fundingPubKey for bet"); } else if ( vinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) return eval->Invalid("vout.0 nValue != entropy nValue for bet"); @@ -600,12 +600,12 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi fprintf(stderr,"cant find entropy vin0 %s or vin0prev %d vouts[%d]\n",uint256_str(str,tx.vin[0].prevout.hash),tx.vin[0].prevout.n,(int32_t)vinTx.vout.size()); continue; } - if ( vinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + if ( vinTx.vout[1].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; - ptr0 = (uint8_t *)vinTx.vout[tx.vin[0].prevout.n].scriptPubKey.data(); + ptr0 = (uint8_t *)vinTx.vout[1].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; i Date: Wed, 1 Aug 2018 00:28:27 -1100 Subject: [PATCH 146/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index bc405e05a..d4877b84f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -430,7 +430,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[1] constrain violation for bet"); else if ( tx.vout[2].nValue >= txfee+maxodds || tx.vout[2].nValue < txfee ) return eval->Invalid("vout[2] nValue violation for bet"); - else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() > 1 ) + else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() < 2 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); else if ( vinofvinTx.vout[1].scriptPubKey != fundingPubKey ) { From 7a241adbe66e7dc83214070a475683e8197c92a6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 00:33:26 -1100 Subject: [PATCH 147/233] +print --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d4877b84f..d9c54fbc8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -434,7 +434,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); else if ( vinofvinTx.vout[1].scriptPubKey != fundingPubKey ) { - uint8_t *ptr0,*ptr1; int32_t i; + uint8_t *ptr0,*ptr1; int32_t i; char str[65]; ptr0 = (uint8_t *)vinofvinTx.vout[1].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); for (i=0; iInvalid("vin1 of entropy tx not fundingPubKey for bet"); } else if ( vinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) From 502dad46790373c1c1262393e3a73db6a5f1986c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 00:43:34 -1100 Subject: [PATCH 148/233] Test --- src/cc/dice.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d9c54fbc8..67e298dd5 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -430,14 +430,17 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[1] constrain violation for bet"); else if ( tx.vout[2].nValue >= txfee+maxodds || tx.vout[2].nValue < txfee ) return eval->Invalid("vout[2] nValue violation for bet"); - else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() < 2 ) + else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() < 1 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); - else if ( vinofvinTx.vout[1].scriptPubKey != fundingPubKey ) + else if ( vinofvinTx.vout[0].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; char str[65]; - ptr0 = (uint8_t *)vinofvinTx.vout[1].scriptPubKey.data(); + fprintf(stderr,"bidTx.%s\n",uint256_str(str,txid)); + fprintf(stderr,"entropyTx.%s v%d\n",uint256_str(str,tx.vin[0].prevout.hash),(int32_t)tx.vin[0].prevout.n); + fprintf(stderr,"entropyTx vin0 %s v%d\n",uint256_str(str,vinTx.vin[0].prevout.hash),(int32_t)vinTx.vin[0].prevout.n); + ptr0 = (uint8_t *)vinofvinTx.vout[0].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; i Date: Wed, 1 Aug 2018 00:58:40 -1100 Subject: [PATCH 149/233] Test --- src/cc/dice.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 67e298dd5..52d67a601 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -432,24 +432,27 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] nValue violation for bet"); else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() < 1 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); - else if ( vinofvinTx.vout[0].scriptPubKey != fundingPubKey ) + else if ( vinTx.vin[0].prevout.hash != fundingtxid ) { - uint8_t *ptr0,*ptr1; int32_t i; char str[65]; - fprintf(stderr,"bidTx.%s\n",uint256_str(str,txid)); - fprintf(stderr,"entropyTx.%s v%d\n",uint256_str(str,tx.vin[0].prevout.hash),(int32_t)tx.vin[0].prevout.n); - fprintf(stderr,"entropyTx vin0 %s v%d\n",uint256_str(str,vinTx.vin[0].prevout.hash),(int32_t)vinTx.vin[0].prevout.n); - ptr0 = (uint8_t *)vinofvinTx.vout[0].scriptPubKey.data(); - ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; iInvalid("vin1 of entropy tx not fundingPubKey for bet"); + if ( vinofvinTx.vout[0].scriptPubKey != fundingPubKey ) + { + uint8_t *ptr0,*ptr1; int32_t i; char str[65]; + fprintf(stderr,"bidTx.%s\n",uint256_str(str,txid)); + fprintf(stderr,"entropyTx.%s v%d\n",uint256_str(str,tx.vin[0].prevout.hash),(int32_t)tx.vin[0].prevout.n); + fprintf(stderr,"entropyTx vin0 %s v%d\n",uint256_str(str,vinTx.vin[0].prevout.hash),(int32_t)vinTx.vin[0].prevout.n); + ptr0 = (uint8_t *)vinofvinTx.vout[0].scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; iInvalid("vin1 of entropy tx not fundingPubKey for bet"); + } + else if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) + return eval->Invalid("vout.0 nValue != entropy nValue for bet"); } - else if ( vinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) - return eval->Invalid("vout.0 nValue != entropy nValue for bet"); if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { // will only happen for fundingPubKey From 8e071986946e506d5ecadf474450dd810af67785 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 01:05:17 -1100 Subject: [PATCH 150/233] Test --- src/cc/dice.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 52d67a601..cea386fe4 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -448,10 +448,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) for (i=0; iInvalid("vin1 of entropy tx not fundingPubKey for bet"); + //return eval->Invalid("vin1 of entropy tx not fundingPubKey for bet"); } - else if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].nValue != tx.vout[0].nValue ) - return eval->Invalid("vout.0 nValue != entropy nValue for bet"); } if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { From c066bd7bde337e7427dea2154096531e2dbfeec1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 01:28:50 -1100 Subject: [PATCH 151/233] Test --- src/cc/dice.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index cea386fe4..3f70036aa 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -62,9 +62,10 @@ bet: vout.n-1: opreturn 'B' sbits fundingtxid entropy loser: - vin.0: betTx CC vout.0 entropy from bet - vin.1: betTx CC vout.1 bet amount from bet - vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' + vin.0: normal input + vin.1: betTx CC vout.0 entropy from bet + vin.2: betTx CC vout.1 bet amount from bet + vin.3+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC to entropy owner vout.1: tag to owner address for entropy funds vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof @@ -466,13 +467,13 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) //vout.1: tag to owner address for entropy funds preventCCvouts = 1; DiceAmounts(inputs,outputs,cp,eval,tx); - if ( IsCCInput(tx.vin[0].scriptSig) == 0 || IsCCInput(tx.vin[1].scriptSig) == 0 ) + if ( IsCCInput(tx.vin[1].scriptSig) == 0 || IsCCInput(tx.vin[2].scriptSig) == 0 ) return eval->Invalid("vin0 or vin1 normal vin for bet"); - else if ( tx.vin[0].prevout.hash != tx.vin[1].prevout.hash ) + else if ( tx.vin[1].prevout.hash != tx.vin[2].prevout.hash ) return eval->Invalid("vin0 != vin1 prevout.hash for bet"); - else if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 ) + else if ( eval->GetTxUnconfirmed(tx.vin[1].prevout.hash,vinTx,hashBlock) == 0 ) return eval->Invalid("always should find vin.0, but didnt for wlt"); - else if ( vinTx.vout.size() < 3 || DecodeDiceOpRet(tx.vin[0].prevout.hash,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,vinhentropy,vinproof) != 'B' ) + else if ( vinTx.vout.size() < 3 || DecodeDiceOpRet(tx.vin[1].prevout.hash,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,vinhentropy,vinproof) != 'B' ) return eval->Invalid("not betTx for vin0/1 for wlt"); else if ( sbits != vinsbits || fundingtxid != vinfundingtxid ) return eval->Invalid("sbits or fundingtxid mismatch for wlt"); @@ -482,7 +483,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) { //vout.0: funding CC to entropy owner //vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof - if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,inputs-txfee) == 0 ) + if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,inputs) == 0 ) return eval->Invalid("vout[0] != inputs-txfee for loss"); iswin = -1; } @@ -498,7 +499,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( inputs != (outputs + tx.vout[2].nValue + 2*txfee) ) + else if ( inputs != (outputs + tx.vout[2].nValue + txfee) ) { fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f - txfee\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); @@ -851,6 +852,11 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin return("0"); } } + if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 ) + { + fprintf(stderr,"no txfee inputs for win/lose\n"); + return("0"); + } if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); @@ -885,7 +891,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin return("0"); } CCchange = betTx.vout[0].nValue; - fundsneeded = 2*txfee + (odds-1)*betTx.vout[1].nValue; + fundsneeded = txfee + (odds-1)*betTx.vout[1].nValue; if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,60)) > 0 ) { if ( inputs > fundsneeded ) @@ -903,7 +909,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin else { funcid = 'L'; - mtx.vout.push_back(MakeCC1vout(cp->evalcode,betTx.vout[0].nValue + betTx.vout[1].nValue - txfee,dicepk)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,betTx.vout[0].nValue + betTx.vout[1].nValue,dicepk)); mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); } if ( winlosetimeout != 0 ) From 313ee6d78d642d50b0f29c49404749bc49603b1d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 01:31:46 -1100 Subject: [PATCH 152/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 3f70036aa..d26b9296b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -449,7 +449,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) for (i=0; iInvalid("vin1 of entropy tx not fundingPubKey for bet"); + return eval->Invalid("vin1 of entropy tx not fundingPubKey for bet"); } } if ( (iswin= DiceIsWinner(entropy,txid,tx,vinTx,hash,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) From 6afd491b76bc867888c34ab50e6ce7707d46cd02 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 01:47:45 -1100 Subject: [PATCH 153/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d26b9296b..c6801f98d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -607,7 +607,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi } if ( vinTx.vout[0].scriptPubKey != fundingPubKey ) { - uint8_t *ptr0,*ptr1; int32_t i; + uint8_t *ptr0,*ptr1; int32_t i; char str[65]; ptr0 = (uint8_t *)vinTx.vout[0].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); for (i=0; i Date: Wed, 1 Aug 2018 01:50:18 -1100 Subject: [PATCH 154/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c6801f98d..9cd0cb2a6 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -598,7 +598,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { if ( fundingPubKey == tx.vout[1].scriptPubKey ) { - if ( funcid == 'E' ) + if ( funcid == 'E' && fundingtxid != tx.vin[0].prevout.hash ) { if ( GetTransaction(tx.vin[0].prevout.hash,vinTx,hashBlock,false) == 0 ) { From 214e2c4cf19f653ad15563c166b5a983c24b110b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 01:59:37 -1100 Subject: [PATCH 155/233] Fix asset swap vout2 --- src/cc/CCassetstx.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 2a9e376c0..22ca71c66 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -461,7 +461,9 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt CCchange = (inputs - paid_nValue); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,orig_assetoshis - received_assetoshis,GetUnspendable(cp,0))); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,received_assetoshis,mypk)); - mtx.vout.push_back(CTxOut(paid_nValue,CScript() << origpubkey << OP_CHECKSIG)); + if ( assetid2 != zeroid ) + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,paid_nValue,origpubkey)); + else mtx.vout.push_back(CTxOut(paid_nValue,CScript() << origpubkey << OP_CHECKSIG)); if ( CCchange != 0 ) mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey))); From f3ad9d4865dd382687f23eceebf1518fb4c0ae22 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 02:33:52 -1100 Subject: [PATCH 156/233] odds+1 --- src/cc/dice.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9cd0cb2a6..50a69f6cb 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -94,7 +94,7 @@ void *dicefinish(void *_ptr) { char str[65],str2[65],name[32]; std::string res; int32_t i,duplicate=0; struct dicefinish_info *ptr; ptr = (struct dicefinish_info *)_ptr; - sleep(1); + sleep(3); // wait for bettxid to be in mempool for (i=0; ibettxid ) { @@ -218,7 +218,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t if ( odds > 1 ) bettor = (bettor / arith_uint256(odds)); if ( bettor >= house ) - winnings = bet * odds; + winnings = bet * (odds+1); else winnings = 0; fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1 %s vs %s\n",(double)winnings/COIN,(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); return(winnings); @@ -497,7 +497,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[0] != inputs-txfee for win/timeout"); else if ( tx.vout[2].scriptPubKey != vinTx.vout[2].scriptPubKey ) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); - else if ( tx.vout[2].nValue != odds*vinTx.vout[1].nValue ) + else if ( tx.vout[2].nValue != (odds+1)*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); else if ( inputs != (outputs + tx.vout[2].nValue + txfee) ) { @@ -891,14 +891,14 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin return("0"); } CCchange = betTx.vout[0].nValue; - fundsneeded = txfee + (odds-1)*betTx.vout[1].nValue; + fundsneeded = txfee + odds*betTx.vout[1].nValue; if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,60)) > 0 ) { if ( inputs > fundsneeded ) CCchange += (inputs - fundsneeded); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,dicepk)); mtx.vout.push_back(CTxOut(txfee,fundingPubKey)); - mtx.vout.push_back(CTxOut(odds * betTx.vout[1].nValue,betTx.vout[2].scriptPubKey)); + mtx.vout.push_back(CTxOut((odds+1) * betTx.vout[1].nValue,betTx.vout[2].scriptPubKey)); } else { From 00005381de230cabf6faf46811181e23ff9f502d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 02:50:30 -1100 Subject: [PATCH 157/233] Win/loss change -> funding pub key --- src/cc/dice.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 50a69f6cb..c71190b7e 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -68,10 +68,12 @@ loser: vin.3+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' vout.0: funding CC to entropy owner vout.1: tag to owner address for entropy funds + vout.2: change to fundingpk vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof winner: same as loser, but vout.2 is winnings + vout.3: change to fundingpk vout.n-1: opreturn 'W' sbits fundingtxid hentropy proof timeout: @@ -157,6 +159,20 @@ void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) #endif } +CPubKey DiceFundingPk(CScript scriptPubKey) +{ + CPubKey pk; uint8_t *ptr,*dest; + if ( scriptPubKey.size() == 35 ) + { + pk.resize(33); + ptr = (uint8_t *)scriptPubkey.data(); + dest = (uint8_t *)pk.data(); + for (i=0; i<33; i++) + dest[i] = ptr[i+1]; + } else fprintf(stderr,"DiceFundingPk invalid size.%d\n",(int32_t)scriptPubKey.size()); + return(pk); +} + uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per txid used { int32_t i; uint8_t _entropy[32],_hentropy[32]; bits256 tmp256,txidpub,txidpriv,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; @@ -485,6 +501,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) //vout.n-1: opreturn 'L' sbits fundingtxid hentropy proof if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,inputs) == 0 ) return eval->Invalid("vout[0] != inputs-txfee for loss"); + else if ( tx.vout[2].scriptPubKey != fundingPubKey ) + return eval->Invalid("vout[2] not send to fundingPubKey for loss"); iswin = -1; } else @@ -504,6 +522,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f - txfee\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); } + else if ( tx.vout[3].scriptPubKey != fundingPubKey ) + return eval->Invalid("vout[3] not send to fundingPubKey for win/timeout"); iswin = (funcid == 'W'); } if ( iswin != 0 ) @@ -839,10 +859,11 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; + CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return("0"); + fundingpk = DiceFundingPk(fundingPubKey); if ( winlosetimeout != 0 ) { scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; @@ -917,7 +938,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin *resultp = 1; //char str[65],str2[65]; //fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); + return(FinalizeCCTx(0,cp,mtx,fundingpk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else return("0"); } From 7aee2b87effaa150be93f320b9865a66d7f46667 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 02:53:17 -1100 Subject: [PATCH 158/233] Test --- src/cc/dice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c71190b7e..4d94212f3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -161,12 +161,12 @@ void endiancpy(uint8_t *dest,uint8_t *src,int32_t len) CPubKey DiceFundingPk(CScript scriptPubKey) { - CPubKey pk; uint8_t *ptr,*dest; + CPubKey pk; uint8_t *ptr,*dest; int32_t i; if ( scriptPubKey.size() == 35 ) { pk.resize(33); - ptr = (uint8_t *)scriptPubkey.data(); - dest = (uint8_t *)pk.data(); + ptr = (uint8_t *)scriptPubKey.data(); + dest = (uint8_t *)pk.begin(); for (i=0; i<33; i++) dest[i] = ptr[i+1]; } else fprintf(stderr,"DiceFundingPk invalid size.%d\n",(int32_t)scriptPubKey.size()); From 58439b8e3718eff5ef5d8b781cd6324f4dfe278f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 02:54:31 -1100 Subject: [PATCH 159/233] Test --- src/cc/dice.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 4d94212f3..7c2b8a89b 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -164,7 +164,6 @@ CPubKey DiceFundingPk(CScript scriptPubKey) CPubKey pk; uint8_t *ptr,*dest; int32_t i; if ( scriptPubKey.size() == 35 ) { - pk.resize(33); ptr = (uint8_t *)scriptPubKey.data(); dest = (uint8_t *)pk.begin(); for (i=0; i<33; i++) From 3f01899adfd6ab673610dba028672b489c4352b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 03:05:48 -1100 Subject: [PATCH 160/233] Test --- src/cc/dice.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 7c2b8a89b..d20f95451 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -642,6 +642,18 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi entropyval = tx.vout[0].nValue; first = 1; } + else + { + uint8_t *ptr0,*ptr1; int32_t i; char str[65]; + ptr0 = (uint8_t *)tx.vout[1].scriptPubKey.data(); + ptr1 = (uint8_t *)fundingPubKey.data(); + for (i=0; i Date: Wed, 1 Aug 2018 03:10:43 -1100 Subject: [PATCH 161/233] Test --- src/cc/dice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index d20f95451..3e77f9bb0 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -615,6 +615,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi totalinputs += nValue; if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) { + fprintf(stderr,"check first\n"); if ( fundingPubKey == tx.vout[1].scriptPubKey ) { if ( funcid == 'E' && fundingtxid != tx.vin[0].prevout.hash ) @@ -637,7 +638,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi fprintf(stderr," (%c) entropy vin.%d fundingPubKey mismatch %s\n",funcid,tx.vin[0].prevout.n,uint256_str(str,tx.vin[0].prevout.hash)); continue; } - } + } else fprintf(stderr,"not E or is funding\n"); entropytxid = txid; entropyval = tx.vout[0].nValue; first = 1; @@ -655,7 +656,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi fprintf(stderr," (%c) tx vin.%d fundingPubKey mismatch %s\n",funcid,tx.vin[0].prevout.n,uint256_str(str,tx.vin[0].prevout.hash)); } } - } //else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); + } else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); } //else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } From c8d1932aacb66dfca96f3d3cfb3a4819be831901 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 03:52:24 -1100 Subject: [PATCH 162/233] Handle no change cases --- src/cc/dice.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 3e77f9bb0..a7dcde9ab 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -501,7 +501,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,inputs) == 0 ) return eval->Invalid("vout[0] != inputs-txfee for loss"); else if ( tx.vout[2].scriptPubKey != fundingPubKey ) - return eval->Invalid("vout[2] not send to fundingPubKey for loss"); + { + if ( tx.vout[2].scriptPubKey.size() == 0 || ((uint8_t *)tx.vout[2].scriptPubKey.data())[0] != 0x6a ) + return eval->Invalid("vout[2] not send to fundingPubKey for loss"); + } iswin = -1; } else @@ -522,7 +525,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("CC funds mismatch for win/timeout"); } else if ( tx.vout[3].scriptPubKey != fundingPubKey ) - return eval->Invalid("vout[3] not send to fundingPubKey for win/timeout"); + { + if ( tx.vout[3].scriptPubKey.size() == 0 || ((uint8_t *)tx.vout[3].scriptPubKey.data())[0] != 0x6a ) + return eval->Invalid("vout[3] not send to fundingPubKey for win/timeout"); + } iswin = (funcid == 'W'); } if ( iswin != 0 ) From af42329cb0e9f8e83c17ee2cdc6329f89ea9c4dc Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 04:05:10 -1100 Subject: [PATCH 163/233] Test --- src/cc/dice.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a7dcde9ab..78108dea8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -450,15 +450,15 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); else if ( vinTx.vin[0].prevout.hash != fundingtxid ) { - if ( vinofvinTx.vout[0].scriptPubKey != fundingPubKey ) + if ( vinofvinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; char str[65]; fprintf(stderr,"bidTx.%s\n",uint256_str(str,txid)); fprintf(stderr,"entropyTx.%s v%d\n",uint256_str(str,tx.vin[0].prevout.hash),(int32_t)tx.vin[0].prevout.n); fprintf(stderr,"entropyTx vin0 %s v%d\n",uint256_str(str,vinTx.vin[0].prevout.hash),(int32_t)vinTx.vin[0].prevout.n); - ptr0 = (uint8_t *)vinofvinTx.vout[0].scriptPubKey.data(); + ptr0 = (uint8_t *)vinofvinTx.vout[tx.vin[0].prevout.n].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; i Date: Wed, 1 Aug 2018 04:09:58 -1100 Subject: [PATCH 164/233] Test --- src/cc/dice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 78108dea8..dc0ecdec0 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -450,15 +450,15 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("always should find vinofvin.0, but didnt for bet"); else if ( vinTx.vin[0].prevout.hash != fundingtxid ) { - if ( vinofvinTx.vout[tx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) + if ( vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey != fundingPubKey ) { uint8_t *ptr0,*ptr1; int32_t i; char str[65]; fprintf(stderr,"bidTx.%s\n",uint256_str(str,txid)); fprintf(stderr,"entropyTx.%s v%d\n",uint256_str(str,tx.vin[0].prevout.hash),(int32_t)tx.vin[0].prevout.n); fprintf(stderr,"entropyTx vin0 %s v%d\n",uint256_str(str,vinTx.vin[0].prevout.hash),(int32_t)vinTx.vin[0].prevout.n); - ptr0 = (uint8_t *)vinofvinTx.vout[tx.vin[0].prevout.n].scriptPubKey.data(); + ptr0 = (uint8_t *)vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey.data(); ptr1 = (uint8_t *)fundingPubKey.data(); - for (i=0; i Date: Wed, 1 Aug 2018 04:20:21 -1100 Subject: [PATCH 165/233] Add autofill to mempool --- src/cc/dice.cpp | 6 ++++++ src/rpcrawtransaction.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index dc0ecdec0..d7b956d76 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -126,6 +126,12 @@ void *dicefinish(void *_ptr) if ( DecodeHexTx(tx,res) != 0 ) { txid = tx.GetHash(); + if ( mempool.exists(tx) == 0 ) + { + CValidationState state; + bool fMissingInputs,fOverrideFees = false; + AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees); + } RelayTransaction(tx); fprintf(stderr,"%s\nresult.(%s)\n",res.c_str(),uint256_str(str,txid)); } diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 125be1d73..fa4118294 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -1137,7 +1137,7 @@ UniValue sendrawtransaction(const UniValue& params, bool fHelp) } } else if (fHaveChain) { throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain"); - } + } RelayTransaction(tx); return hashTx.GetHex(); From b46dc9da560233bee5c52b055a2a46d75f8e8a89 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 04:22:56 -1100 Subject: [PATCH 166/233] bool myAddtomempool(CTransaction &tx) --- src/cc/CCinclude.h | 1 + src/cc/dice.cpp | 7 +------ src/main.cpp | 8 ++++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 717fd2da6..64987a236 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -52,6 +52,7 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector Date: Wed, 1 Aug 2018 04:26:15 -1100 Subject: [PATCH 167/233] Mempool lookup --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e3cd2590b..5dc9ad4a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1605,8 +1605,8 @@ bool GetAddressUnspent(uint160 addressHash, int type, bool myAddtomempool(CTransaction &tx) { - CValidationState state; bool fMissingInputs,fOverrideFees = false; - if ( mempool.exists(tx) == 0 ) + CValidationState state; CTransaction Ltx; bool fMissingInputs,fOverrideFees = false; + if ( mempool.lookup(tx.GetHash(),Ltx) == 0 ) return(AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)); else return(false); } From 2ac06413e1621f967bc7c0a8800d1cda782173b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 04:39:42 -1100 Subject: [PATCH 168/233] Iterate relay until in mempool --- src/cc/dice.cpp | 17 ++++++++++++----- src/main.cpp | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 685ddd9fc..5c62a0240 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -15,7 +15,7 @@ #include "CCdice.h" -// timeout, validate, verify win/loss can be used for entropy +// timeout /* in order to implement a dice game, we need a source of entropy, reasonably fast completion time and a way to manage the utxos. @@ -122,13 +122,20 @@ void *dicefinish(void *_ptr) res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { - LOCK(cs_main); + //LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { txid = tx.GetHash(); - myAddtomempool(tx); - RelayTransaction(tx); fprintf(stderr,"%s\nresult.(%s)\n",res.c_str(),uint256_str(str,txid)); + for (i=0; i<10; i++) + { + if ( myAddtomempool(tx) == 0 ) + { + RelayTransaction(tx); + fprintf(stderr,"Relay transaction\n"); + sleep(1); + } else break; + } } } } @@ -645,7 +652,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi fprintf(stderr," (%c) entropy vin.%d fundingPubKey mismatch %s\n",funcid,tx.vin[0].prevout.n,uint256_str(str,tx.vin[0].prevout.hash)); continue; } - } else fprintf(stderr,"not E or is funding\n"); + } //else fprintf(stderr,"not E or is funding\n"); entropytxid = txid; entropyval = tx.vout[0].nValue; first = 1; diff --git a/src/main.cpp b/src/main.cpp index 5dc9ad4a7..2ec680009 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1608,7 +1608,7 @@ bool myAddtomempool(CTransaction &tx) CValidationState state; CTransaction Ltx; bool fMissingInputs,fOverrideFees = false; if ( mempool.lookup(tx.GetHash(),Ltx) == 0 ) return(AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !fOverrideFees)); - else return(false); + else return(true); } bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) From d45da59952cee5c1cf4757afc34968f4420e1a71 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 04:58:05 -1100 Subject: [PATCH 169/233] Iterate dicewinner/loser --- src/cc/dice.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 5c62a0240..765bb0007 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -119,24 +119,34 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { CTransaction tx; uint256 txid; char str[65]; int32_t result; - res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + for (i=0; i<10; i++) { - //LOCK(cs_main); - if ( DecodeHexTx(tx,res) != 0 ) + res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { - txid = tx.GetHash(); - fprintf(stderr,"%s\nresult.(%s)\n",res.c_str(),uint256_str(str,txid)); - for (i=0; i<10; i++) + //LOCK(cs_main); + if ( DecodeHexTx(tx,res) != 0 ) { + txid = tx.GetHash(); + fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); if ( myAddtomempool(tx) == 0 ) { RelayTransaction(tx); fprintf(stderr,"Relay transaction\n"); - sleep(1); - } else break; + if ( myAddtomempool(tx) != 0 ) + { + fprintf(stderr,"result tx in mempool\n",uint256_str(str,txid)); + break; + } + } + else + { + fprintf(stderr,"result tx in mempool\n",uint256_str(str,txid)); + break; + } } - } + } else fprintf(stderr,"error decoding result tx\n"); + sleep(1); } } free(ptr); From 84467192b978d26d51ce33acba23aa6fe5f37a8e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 04:58:58 -1100 Subject: [PATCH 170/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 765bb0007..953e3ecb8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -135,13 +135,13 @@ void *dicefinish(void *_ptr) fprintf(stderr,"Relay transaction\n"); if ( myAddtomempool(tx) != 0 ) { - fprintf(stderr,"result tx in mempool\n",uint256_str(str,txid)); + fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); break; } } else { - fprintf(stderr,"result tx in mempool\n",uint256_str(str,txid)); + fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); break; } } From 8012ad483cf5df245cacfbb6f3bf23922d45c3e5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 05:11:16 -1100 Subject: [PATCH 171/233] tst --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 953e3ecb8..74e8d33a4 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -537,9 +537,9 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != (odds+1)*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( inputs != (outputs + tx.vout[2].nValue + txfee) ) + else if ( inputs != (outputs + tx.vout[2].nValue) ) { - fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f - txfee\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); + fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); } else if ( tx.vout[3].scriptPubKey != fundingPubKey ) From 60fbf4f32519625d44d00a90489ddef5124ee1f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 05:17:33 -1100 Subject: [PATCH 172/233] Test --- src/cc/dice.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 74e8d33a4..672c93a0d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -122,13 +122,14 @@ void *dicefinish(void *_ptr) for (i=0; i<10; i++) { res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + txid = tx.GetHash(); + if ( i == 0 ) + fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { //LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { - txid = tx.GetHash(); - fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); if ( myAddtomempool(tx) == 0 ) { RelayTransaction(tx); @@ -146,7 +147,7 @@ void *dicefinish(void *_ptr) } } } else fprintf(stderr,"error decoding result tx\n"); - sleep(1); + sleep(3); } } free(ptr); From ca2e06860472b97fd0c61a7bd3c990e0a8b54395 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 05:24:19 -1100 Subject: [PATCH 173/233] Test --- src/cc/dice.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 672c93a0d..a2544fc44 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -122,18 +122,18 @@ void *dicefinish(void *_ptr) for (i=0; i<10; i++) { res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - txid = tx.GetHash(); - if ( i == 0 ) - fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { //LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { + txid = tx.GetHash(); + if ( i == 0 ) + fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); if ( myAddtomempool(tx) == 0 ) { RelayTransaction(tx); - fprintf(stderr,"Relay transaction\n"); + fprintf(stderr,"Relay transaction.%d\n",i); if ( myAddtomempool(tx) != 0 ) { fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); From c6485d87ec7f86ab4c3b892a9923a638db7f5ba0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 05:44:57 -1100 Subject: [PATCH 174/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a2544fc44..faed5f0de 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -538,9 +538,9 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[2] scriptPubKey mismatch for win/timeout"); else if ( tx.vout[2].nValue != (odds+1)*vinTx.vout[1].nValue ) return eval->Invalid("vout[2] payut mismatch for win/timeout"); - else if ( inputs != (outputs + tx.vout[2].nValue) ) + else if ( inputs != (outputs + tx.vout[2].nValue) && inputs != (outputs + tx.vout[2].nValue+txfee) ) { - fprintf(stderr,"inputs %.8f != outputs %.8f + 2 %.8f\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); + fprintf(stderr,"inputs %.8f != outputs %.8f + %.8f\n",(double)inputs/COIN,(double)outputs/COIN,(double)tx.vout[2].nValue/COIN); return eval->Invalid("CC funds mismatch for win/timeout"); } else if ( tx.vout[3].scriptPubKey != fundingPubKey ) From 0700947997831a00b09b2d8a21bb086f358ec1c8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 05:55:16 -1100 Subject: [PATCH 175/233] 0 --- src/cc/dice.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index faed5f0de..1077c5d8a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -899,7 +899,10 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + { + fprintf(stderr,"Diceinit error\n"); return("0"); + } fundingpk = DiceFundingPk(fundingPubKey); if ( winlosetimeout != 0 ) { From 48b3eb8536558844215476a51d5c791cc54f4979 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:02:34 -1100 Subject: [PATCH 176/233] Test --- src/cc/dice.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1077c5d8a..0bd5f66e6 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -392,9 +392,9 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction // queue 'W' winning tx return(1); } - } - } - } + } else fprintf(stderr,"hentropy != hentropy2\n"); + } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)refbits,fundingtxid == vinfundingtxid); + } else fprintf(stderr,"notmine or not CC\n"); return(0); } @@ -980,7 +980,12 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin //fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); return(FinalizeCCTx(0,cp,mtx,fundingpk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); - } else return("0"); + } + else + { + fprintf(stderr,"iswin.%d winlosetimeout.%d\n",iswin,winlosetimeout); + return("0"); + } } return("0"); } From dfbd6ae11705e20e923acfa7fcd8715d8f0ff31c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:04:20 -1100 Subject: [PATCH 177/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0bd5f66e6..df905ef26 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -393,7 +393,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction return(1); } } else fprintf(stderr,"hentropy != hentropy2\n"); - } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)refbits,fundingtxid == vinfundingtxid); + } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)refsbits,fundingtxid == vinfundingtxid); } else fprintf(stderr,"notmine or not CC\n"); return(0); } From 19be1d65d5d04649d77bc80af310bf9e10ff3e88 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:05:11 -1100 Subject: [PATCH 178/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index df905ef26..b77e58015 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -393,7 +393,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction return(1); } } else fprintf(stderr,"hentropy != hentropy2\n"); - } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)refsbits,fundingtxid == vinfundingtxid); + } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)vinsbits,fundingtxid == vinfundingtxid); } else fprintf(stderr,"notmine or not CC\n"); return(0); } From 7d8ed58c34e5c9bd08634007a0380c82f3af2eba Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:16:43 -1100 Subject: [PATCH 179/233] Test --- src/cc/CCtx.cpp | 2 +- src/cc/dice.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index c20a579f1..c559f100e 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -82,7 +82,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran } else fprintf(stderr,"FinalizeCCTx couldnt find %s\n",mtx.vin[i].prevout.hash.ToString().c_str()); } nmask = (1LL << n) - 1; - if ( (mask & nmask) != (CCmask & nmask) ) + if ( 0 && (mask & nmask) != (CCmask & nmask) ) fprintf(stderr,"mask.%llx vs CCmask.%llx %llx %llx %llx\n",(long long)(mask & nmask),(long long)(CCmask & nmask),(long long)mask,(long long)CCmask,(long long)nmask); if ( totalinputs >= totaloutputs+2*txfee ) { diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b77e58015..e37ac4266 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -118,7 +118,16 @@ void *dicefinish(void *_ptr) fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - CTransaction tx; uint256 txid; char str[65]; int32_t result; + CTransaction tx; uint256 txid,hashBlock; char str[65]; int32_t result; + for (i=0; i<10; i++) + { + if ( myGetTransaction(ptr->bettxid,tx,hashBlock) == 0 || hashBlock == zeroid ) + { + fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); + sleep(10); + } else break; + } + fprintf(stderr,"bettxid %s confirmed\n",uint256_str(str,ptr->bettxid)); for (i=0; i<10; i++) { res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); From 9dff11caf1dc64ae57954e0e9549421b5893f9cf Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:18:14 -1100 Subject: [PATCH 180/233] Test --- src/cc/dice.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index e37ac4266..3005fabc8 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -119,6 +119,15 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { CTransaction tx; uint256 txid,hashBlock; char str[65]; int32_t result; + res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + { + if ( DecodeHexTx(tx,res) != 0 ) + { + myAddtomempool(tx) + RelayTransaction(tx); + } + } for (i=0; i<10; i++) { if ( myGetTransaction(ptr->bettxid,tx,hashBlock) == 0 || hashBlock == zeroid ) From dcea6b0274f1affb2112379a82049ef8eb57a13a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:18:47 -1100 Subject: [PATCH 181/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 3005fabc8..ebd8e4aff 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -124,7 +124,7 @@ void *dicefinish(void *_ptr) { if ( DecodeHexTx(tx,res) != 0 ) { - myAddtomempool(tx) + myAddtomempool(tx); RelayTransaction(tx); } } From 5ce44e362e27ad127118a549bc5747ba73f44ba7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:40:14 -1100 Subject: [PATCH 182/233] Skip coinable for normal inputs --- src/cc/CCtx.cpp | 17 ++++++++++------- src/cc/dice.cpp | 7 ++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index c559f100e..b59dc020b 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -203,7 +203,7 @@ uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout) 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; std::vector vecOutputs; + int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid,hashBlock; std::vector vecOutputs; #ifdef ENABLE_WALLET const CKeyStore& keystore = *pwalletMain; assert(pwalletMain != NULL); @@ -220,12 +220,15 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in break; if ( j != mtx.vin.size() ) continue; - 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 ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.IsCoinBase() == 0 ) + { + 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 ( totalinputs >= total ) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ebd8e4aff..47070f3a3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -513,9 +513,10 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) case 'L': case 'W': case 'T': - //vin.0: betTx CC vout.0 entropy from bet - //vin.1: betTx CC vout.1 bet amount from bet - //vin.2+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' + //vin.0: normal input + //vin.1: betTx CC vout.0 entropy from bet + //vin.2: betTx CC vout.1 bet amount from bet + //vin.3+: funding CC vout.0 from 'F', 'E', 'W', 'L' or 'T' //vout.1: tag to owner address for entropy funds preventCCvouts = 1; DiceAmounts(inputs,outputs,cp,eval,tx); From 72d0ba39b4e34523f9fe1a14a02780b8a04773db Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:41:22 -1100 Subject: [PATCH 183/233] Test --- 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 b59dc020b..b0fb5e78e 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -203,7 +203,7 @@ uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout) 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; + int32_t vout,j,n = 0; uint64_t nValue,totalinputs = 0; uint256 txid,hashBlock; std::vector vecOutputs; CTransaction tx; #ifdef ENABLE_WALLET const CKeyStore& keystore = *pwalletMain; assert(pwalletMain != NULL); From dbd332755d0aee31ddb02b6d7e15c37e9fe9e7d0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 06:56:46 -1100 Subject: [PATCH 184/233] Test --- src/cc/dice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 47070f3a3..c6da4218a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -122,6 +122,7 @@ void *dicefinish(void *_ptr) res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { + LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { myAddtomempool(tx); @@ -142,7 +143,7 @@ void *dicefinish(void *_ptr) res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { - //LOCK(cs_main); + LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { txid = tx.GetHash(); @@ -412,7 +413,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction } } else fprintf(stderr,"hentropy != hentropy2\n"); } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)vinsbits,fundingtxid == vinfundingtxid); - } else fprintf(stderr,"notmine or not CC\n"); + } //else fprintf(stderr,"notmine or not CC\n"); return(0); } From c88babeb6f93db0b99714850a27924591884e86f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:08:42 -1100 Subject: [PATCH 185/233] +print --- src/cc/assets.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index f03dadb66..2088d9b73 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -349,7 +349,10 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 ) return eval->Invalid("vout2 doesnt go to origpubkey fillex"); else if ( inputs != tx.vout[2].nValue+tx.vout[3].nValue ) + { + fprintf(stderr,"inputs %.8f != %.8f + %.8f\n",(double)inputs/COIN,(double)tx.vout[2].nValue/COIN,(double)tx.vout[3].nValue/COIN); return eval->Invalid("asset inputs doesnt match vout2+3 fillex"); + } } else if ( ConstrainVout(tx.vout[2],1,CCaddr,inputs) == 0 ) return eval->Invalid("vout2 doesnt match inputs fillex"); From d0fad7b177afb85e30674d8f93533d7566099c9c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:20:54 -1100 Subject: [PATCH 186/233] Test --- src/cc/dice.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c6da4218a..1c5384351 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -118,27 +118,27 @@ void *dicefinish(void *_ptr) fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - CTransaction tx; uint256 txid,hashBlock; char str[65]; int32_t result; + CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { - myAddtomempool(tx); - RelayTransaction(tx); + for (i=0; i<10; i++) + { + myAddtomempool(tx); + RelayTransaction(tx); + if ( myGetTransaction(ptr->bettxid,bettx,hashBlock) == 0 || hashBlock == zeroid ) + { + fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); + sleep(10); + } else break; + } } } - for (i=0; i<10; i++) - { - if ( myGetTransaction(ptr->bettxid,tx,hashBlock) == 0 || hashBlock == zeroid ) - { - fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); - sleep(10); - } else break; - } fprintf(stderr,"bettxid %s confirmed\n",uint256_str(str,ptr->bettxid)); - for (i=0; i<10; i++) + for (i=0; i<60; i++) { res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) @@ -166,7 +166,7 @@ void *dicefinish(void *_ptr) } } } else fprintf(stderr,"error decoding result tx\n"); - sleep(3); + sleep(10); } } free(ptr); From 0648474d44a4c24576aab9a960d6e399f6ba78ff Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:24:13 -1100 Subject: [PATCH 187/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1c5384351..9188818a5 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -122,7 +122,7 @@ void *dicefinish(void *_ptr) res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { - LOCK(cs_main); + //LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { for (i=0; i<10; i++) From 59ec101fb8c24514babfaa19560b6202ce1309dd Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:43:22 -1100 Subject: [PATCH 188/233] -print --- src/cc/dice.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 9188818a5..b4a9e7b3e 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -131,7 +131,7 @@ void *dicefinish(void *_ptr) RelayTransaction(tx); if ( myGetTransaction(ptr->bettxid,bettx,hashBlock) == 0 || hashBlock == zeroid ) { - fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); + //fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); sleep(10); } else break; } @@ -152,7 +152,7 @@ void *dicefinish(void *_ptr) if ( myAddtomempool(tx) == 0 ) { RelayTransaction(tx); - fprintf(stderr,"Relay transaction.%d\n",i); + //fprintf(stderr,"Relay transaction.%d\n",i); if ( myAddtomempool(tx) != 0 ) { fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); @@ -273,7 +273,6 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t if ( bettor >= house ) winnings = bet * (odds+1); else winnings = 0; - fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1 %s vs %s\n",(double)winnings/COIN,(double)bet/COIN,(int32_t)odds,uint256_str(str,*(uint256 *)&house),uint256_str(str2,*(uint256 *)&bettor)); return(winnings); } @@ -399,7 +398,8 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) { - winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,bettorentropy); + winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks); + fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-txfee)); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { From e4b41c11ae456aebbec45a26471613d26957d6ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:44:11 -1100 Subject: [PATCH 189/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b4a9e7b3e..19af43966 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -399,7 +399,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks); - fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-txfee)); + fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-10000)); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { From 4a8278877731c29cfbedcb89449e503c17f4010a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:45:59 -1100 Subject: [PATCH 190/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 19af43966..0d45ab926 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -398,8 +398,8 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); if ( hentropy == hentropy2 ) { - winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks); - fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-10000)); + winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,bettorentropy); + fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-txfee)); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { From d83da823e01802825f2bba7be3e4f11136a45676 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 07:46:43 -1100 Subject: [PATCH 191/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0d45ab926..66d8dd9e2 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -399,7 +399,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,bettorentropy); - fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-txfee)); + fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-10000)); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { From b0a85aec3f6633ecc012478ba245c45a6ff820d5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 1 Aug 2018 22:42:04 -1100 Subject: [PATCH 192/233] Streamline dice queue --- src/cc/dice.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 66d8dd9e2..1151bee37 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -122,6 +122,16 @@ void *dicefinish(void *_ptr) res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { + if ( DecodeHexTx(tx,res) != 0 ) + { + LOCK(cs_main); + if ( myAddtomempool(tx) != 0 ) + { + RelayTransaction(tx); + fprintf(stderr,"added to mempool and broadcast\n"); + } else fprintf(stderr,"error adding to mempool\n"); + } else fprintf(stderr,"error decoding hex\n"); + /* //LOCK(cs_main); if ( DecodeHexTx(tx,res) != 0 ) { @@ -135,9 +145,9 @@ void *dicefinish(void *_ptr) sleep(10); } else break; } - } + }*/ } - fprintf(stderr,"bettxid %s confirmed\n",uint256_str(str,ptr->bettxid)); + /*fprintf(stderr,"bettxid %s confirmed\n",uint256_str(str,ptr->bettxid)); for (i=0; i<60; i++) { res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); @@ -167,7 +177,7 @@ void *dicefinish(void *_ptr) } } else fprintf(stderr,"error decoding result tx\n"); sleep(10); - } + }*/ } free(ptr); return(0); From 1f9a4106c5301fe4b4d23b32982fc55e0933a554 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:04:29 -1100 Subject: [PATCH 193/233] myGettxout --- src/cc/CCinclude.h | 1 + src/cc/CCtx.cpp | 3 ++- src/main.cpp | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 64987a236..43e10ab22 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -53,6 +53,7 @@ static const uint256 zeroid; bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); int32_t is_hexstr(char *str,int32_t n); bool myAddtomempool(CTransaction &tx); +uint64_t myGettxout(uint256 hash,int32_t n); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index b0fb5e78e..5863197c5 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -220,7 +220,8 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in break; if ( j != mtx.vin.size() ) continue; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.IsCoinBase() == 0 ) + if ( myGettxout(txid,vout) == out.tx->vout[out.i].nValue ) + //if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.IsCoinBase() == 0 ) { mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = out.tx->vout[out.i].nValue; diff --git a/src/main.cpp b/src/main.cpp index 2ec680009..d604b4eec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1603,6 +1603,19 @@ bool GetAddressUnspent(uint160 addressHash, int type, return true; } +uint64_t myGettxout(uint256 hash,int32_t n) +{ + CCoins coins; + LOCK(cs_main); + LOCK(mempool.cs); + CCoinsViewMemPool view(pcoinsTip, mempool); + if (!view.GetCoins(hash, coins)) + return(0); + if ( n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull() ) + return(0); + else return(coins.vout[n].nValue); +} + bool myAddtomempool(CTransaction &tx) { CValidationState state; CTransaction Ltx; bool fMissingInputs,fOverrideFees = false; From 63605f0e4ebba711768d1d69a99c8b6bd4eda32a Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:06:50 -1100 Subject: [PATCH 194/233] Test --- src/main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d604b4eec..7634226f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1606,8 +1606,7 @@ bool GetAddressUnspent(uint160 addressHash, int type, uint64_t myGettxout(uint256 hash,int32_t n) { CCoins coins; - LOCK(cs_main); - LOCK(mempool.cs); + LOCK2(cs_main,mempool.cs); CCoinsViewMemPool view(pcoinsTip, mempool); if (!view.GetCoins(hash, coins)) return(0); From 654c77c8a36f1349282504af31f6e6733dc509ed Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:20:52 -1100 Subject: [PATCH 195/233] Check mygettxout --- src/cc/dice.cpp | 65 ++++++++++++------------------------------------- src/main.cpp | 4 +-- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 1151bee37..978843eea 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -131,53 +131,7 @@ void *dicefinish(void *_ptr) fprintf(stderr,"added to mempool and broadcast\n"); } else fprintf(stderr,"error adding to mempool\n"); } else fprintf(stderr,"error decoding hex\n"); - /* - //LOCK(cs_main); - if ( DecodeHexTx(tx,res) != 0 ) - { - for (i=0; i<10; i++) - { - myAddtomempool(tx); - RelayTransaction(tx); - if ( myGetTransaction(ptr->bettxid,bettx,hashBlock) == 0 || hashBlock == zeroid ) - { - //fprintf(stderr,"bettxid.(%s) not confirmed yet\n",uint256_str(str,ptr->bettxid)); - sleep(10); - } else break; - } - }*/ } - /*fprintf(stderr,"bettxid %s confirmed\n",uint256_str(str,ptr->bettxid)); - for (i=0; i<60; i++) - { - res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) - { - LOCK(cs_main); - if ( DecodeHexTx(tx,res) != 0 ) - { - txid = tx.GetHash(); - if ( i == 0 ) - fprintf(stderr,"iter.%d %s\nresult.(%s)\n",i,res.c_str(),uint256_str(str,txid)); - if ( myAddtomempool(tx) == 0 ) - { - RelayTransaction(tx); - //fprintf(stderr,"Relay transaction.%d\n",i); - if ( myAddtomempool(tx) != 0 ) - { - fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); - break; - } - } - else - { - fprintf(stderr,"result tx %s in mempool\n",uint256_str(str,txid)); - break; - } - } - } else fprintf(stderr,"error decoding result tx\n"); - sleep(10); - }*/ } free(ptr); return(0); @@ -429,6 +383,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction bool DiceVerifyTimeout(CTransaction &betTx,int32_t timeoutblocks) { + fprintf(stderr,"DiceVerifyTimeout needs to be implemented\n"); return(false); } @@ -619,7 +574,7 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK 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 && myGettxout(txid,vout) != 0 ) { if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { @@ -653,7 +608,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { txid = it->first.txhash; vout = (int32_t)it->first.index; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myGettxout(txid,vout) > 0 ) { //char str[65],str2[65]; if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) @@ -844,7 +799,7 @@ std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t memset(&zero,0,sizeof(zero)); if ( (cp= Diceinit(fundingPubKey,zero,&C,planstr,txfee,mypk,dicepk,sbits,a,b,c,d)) == 0 ) return(0); - if ( AddNormalinputs(mtx,mypk,funds+3*txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,funds+3*txfee,60) > 0 ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,funds,dicepk)); mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); @@ -880,7 +835,7 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6 } if ( scriptPubKey == fundingPubKey ) { - if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 ) + if ( AddNormalinputs(mtx,mypk,amount+2*txfee,60) > 0 ) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,dicepk)); @@ -908,6 +863,11 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet } if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= 2*bet*odds+txfee && entropyval != 0 ) { + if ( myGettxout(entropytxid,0) == 0 ) + { + fprintf(stderr,"entropy txid is spent\n"); + return(0); + } mtx.vin.push_back(CTxIn(entropytxid,0,CScript())); if ( AddNormalinputs(mtx,mypk,bet+2*txfee+odds,60) > 0 ) { @@ -955,6 +915,11 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin { if ( iswin == winlosetimeout ) { + if ( myGettxout(bettxid,0) == 0 || myGettxout(betxid,1) == 0 ) + { + fprintf(stderr,"bettxid already spent\n"); + return(0); + } //fprintf(stderr,"iswin.%d matches\n",iswin); mtx.vin.push_back(CTxIn(bettxid,0,CScript())); mtx.vin.push_back(CTxIn(bettxid,1,CScript())); diff --git a/src/main.cpp b/src/main.cpp index 7634226f4..312cc8782 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1606,11 +1606,11 @@ bool GetAddressUnspent(uint160 addressHash, int type, uint64_t myGettxout(uint256 hash,int32_t n) { CCoins coins; - LOCK2(cs_main,mempool.cs); + //LOCK2(cs_main,mempool.cs); CCoinsViewMemPool view(pcoinsTip, mempool); if (!view.GetCoins(hash, coins)) return(0); - if ( n<0 || (unsigned int)n>=coins.vout.size() || coins.vout[n].IsNull() ) + if ( n < 0 || (unsigned int)n >= coins.vout.size() || coins.vout[n].IsNull() ) return(0); else return(coins.vout[n].nValue); } From c51da953ad25d509bd191d372f1475fa3309ce7b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:23:53 -1100 Subject: [PATCH 196/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 978843eea..a06c16064 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -915,7 +915,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin { if ( iswin == winlosetimeout ) { - if ( myGettxout(bettxid,0) == 0 || myGettxout(betxid,1) == 0 ) + if ( myGettxout(bettxid,0) == 0 || myGettxout(bettxid,1) == 0 ) { fprintf(stderr,"bettxid already spent\n"); return(0); From b5af6943130704bae2ae4c3ab78d615d3fb5148d Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:39:38 -1100 Subject: [PATCH 197/233] Test --- src/cc/CCtx.cpp | 1 - src/cc/dice.cpp | 7 +++++++ src/main.cpp | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 5863197c5..dbb477817 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -221,7 +221,6 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in if ( j != mtx.vin.size() ) continue; if ( myGettxout(txid,vout) == out.tx->vout[out.i].nValue ) - //if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.IsCoinBase() == 0 ) { mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = out.tx->vout[out.i].nValue; diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a06c16064..b7e0dd14a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -119,6 +119,13 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; + for (i=0; i<10; i++) + { + if ( myGettxout(ptr->bettxid,0) != 0 ) + break; + fprintf(stderr,"."); + sleep(3); + } res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { diff --git a/src/main.cpp b/src/main.cpp index 312cc8782..274d86c07 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1606,7 +1606,7 @@ bool GetAddressUnspent(uint160 addressHash, int type, uint64_t myGettxout(uint256 hash,int32_t n) { CCoins coins; - //LOCK2(cs_main,mempool.cs); + LOCK2(cs_main,mempool.cs); CCoinsViewMemPool view(pcoinsTip, mempool); if (!view.GetCoins(hash, coins)) return(0); From f2e6ae729414b6e0deb00db043fc1da35de84998 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 00:50:07 -1100 Subject: [PATCH 198/233] Print --- src/cc/dice.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b7e0dd14a..bf0bb24b7 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -121,7 +121,7 @@ void *dicefinish(void *_ptr) CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; for (i=0; i<10; i++) { - if ( myGettxout(ptr->bettxid,0) != 0 ) + if ( myGettxout(ptr->bettxid,0) != 0 ) // need to wait for mempool to be updated break; fprintf(stderr,"."); sleep(3); @@ -131,6 +131,7 @@ void *dicefinish(void *_ptr) { if ( DecodeHexTx(tx,res) != 0 ) { + fprintf(stderr,"iswin.%d %s\n%s\n",ptr->iswin,res.c_str(),uint256_str(str,tx.GetHash())); LOCK(cs_main); if ( myAddtomempool(tx) != 0 ) { From 079be98a2ed5e6e761f237b362662804019f049b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 01:10:04 -1100 Subject: [PATCH 199/233] myIsutxo_spentinmempool --- src/cc/CCinclude.h | 3 ++- src/cc/CCtx.cpp | 2 +- src/cc/dice.cpp | 15 ++++----------- src/main.cpp | 4 ++-- src/rpcblockchain.cpp | 20 ++++++++++++++++++++ 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 43e10ab22..312d44a06 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -53,7 +53,8 @@ static const uint256 zeroid; bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock); int32_t is_hexstr(char *str,int32_t n); bool myAddtomempool(CTransaction &tx); -uint64_t myGettxout(uint256 hash,int32_t n); +//uint64_t myGettxout(uint256 hash,int32_t n); +bool myIsutxo_spentinmempool(uint256 txid,int32_t vout); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index dbb477817..66a5d4388 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -220,7 +220,7 @@ uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,in break; if ( j != mtx.vin.size() ) continue; - if ( myGettxout(txid,vout) == out.tx->vout[out.i].nValue ) + if ( myIsutxo_spentinmempool(txid,vout) == 0 ) { mtx.vin.push_back(CTxIn(txid,vout,CScript())); nValue = out.tx->vout[out.i].nValue; diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index bf0bb24b7..220916535 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -119,13 +119,6 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; - for (i=0; i<10; i++) - { - if ( myGettxout(ptr->bettxid,0) != 0 ) // need to wait for mempool to be updated - break; - fprintf(stderr,"."); - sleep(3); - } res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { @@ -582,7 +575,7 @@ uint64_t AddDiceInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK break; if ( j != mtx.vin.size() ) continue; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myGettxout(txid,vout) != 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= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) { @@ -616,7 +609,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi { txid = it->first.txhash; vout = (int32_t)it->first.index; - if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myGettxout(txid,vout) > 0 ) + if ( GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 && myIsutxo_spentinmempool(txid,vout) == 0 ) { //char str[65],str2[65]; if ( (funcid= DecodeDiceOpRet(txid,tx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof)) != 0 ) @@ -871,7 +864,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet } if ( (funding= DicePlanFunds(entropyval,entropytxid,sbits,cp,dicepk,fundingtxid)) >= 2*bet*odds+txfee && entropyval != 0 ) { - if ( myGettxout(entropytxid,0) == 0 ) + if ( myIsutxo_spentinmempool(entropytxid,0) != 0 ) { fprintf(stderr,"entropy txid is spent\n"); return(0); @@ -923,7 +916,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin { if ( iswin == winlosetimeout ) { - if ( myGettxout(bettxid,0) == 0 || myGettxout(bettxid,1) == 0 ) + if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) { fprintf(stderr,"bettxid already spent\n"); return(0); diff --git a/src/main.cpp b/src/main.cpp index 274d86c07..b42f85ce7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1603,7 +1603,7 @@ bool GetAddressUnspent(uint160 addressHash, int type, return true; } -uint64_t myGettxout(uint256 hash,int32_t n) +/*uint64_t myGettxout(uint256 hash,int32_t n) { CCoins coins; LOCK2(cs_main,mempool.cs); @@ -1613,7 +1613,7 @@ uint64_t myGettxout(uint256 hash,int32_t n) if ( n < 0 || (unsigned int)n >= coins.vout.size() || coins.vout[n].IsNull() ) return(0); else return(coins.vout[n].nValue); -} +}*/ bool myAddtomempool(CTransaction &tx) { diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 2e5ed0be8..bedb84383 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -346,6 +346,26 @@ UniValue getdifficulty(const UniValue& params, bool fHelp) return GetNetworkDifficulty(); } +bool myIsutxo_spentinmempool(uint256 txid,int32_t vout) +{ + char *uint256_str(char *str,uint256); + char str[65]; + LOCK(mempool.cs); + BOOST_FOREACH(const CTxMemPoolEntry &e,mempool.mapTx) + { + const CTransaction &tx = e.GetTx(); + const uint256 &hash = tx.GetHash(); + BOOST_FOREACH(const CTxIn &txin,tx.vin) + { + fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n); + if ( txin.prevout.n == vout && txin.prevout.hash == txid ) + return(true); + } + fprintf(stderr,"are vins for %s\n",uint256_str(str,hash)); + } + return(false); +} + UniValue mempoolToJSON(bool fVerbose = false) { if (fVerbose) From 7e9887593d70a099fff24a6546fc35fd70a8c518 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:05:37 -1100 Subject: [PATCH 200/233] Dice status --- src/cc/CCdice.h | 3 +- src/cc/CCinclude.h | 2 + src/cc/dice.cpp | 89 +++++++++++++++++++++++++++++++-------- src/rpcblockchain.cpp | 7 ++- src/rpcrawtransaction.cpp | 13 ++++++ src/rpcserver.cpp | 5 +-- src/rpcserver.h | 5 +-- src/wallet/rpcwallet.cpp | 46 ++++++-------------- 8 files changed, 108 insertions(+), 62 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 8ecd7addd..007cb7ec9 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -24,7 +24,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); -std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); +std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); +std::string DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 312d44a06..cf98251be 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -55,6 +55,8 @@ int32_t is_hexstr(char *str,int32_t n); bool myAddtomempool(CTransaction &tx); //uint64_t myGettxout(uint256 hash,int32_t n); bool myIsutxo_spentinmempool(uint256 txid,int32_t vout); +int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout); +bool mySendrawtransaction(std:string res); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 220916535..b158bd49a 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -92,9 +92,29 @@ struct dicefinish_info int32_t iswin; }; +bool mySendrawtransaction(std:string res) +{ + CTransaction tx; char str[65]; + if ( res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) + { + if ( DecodeHexTx(tx,res) != 0 ) + { + fprintf(stderr,"%s\n%s\n",res.c_str(),uint256_str(str,tx.GetHash())); + LOCK(cs_main); + if ( myAddtomempool(tx) != 0 ) + { + RelayTransaction(tx); + fprintf(stderr,"added to mempool and broadcast\n"); + return(true); + } else fprintf(stderr,"error adding to mempool\n"); + } else fprintf(stderr,"error decoding hex\n"); + } + return(false); +} + void *dicefinish(void *_ptr) { - char str[65],str2[65],name[32]; std::string res; int32_t i,duplicate=0; struct dicefinish_info *ptr; + char str[65],str2[65],name[32]; std::string res; int32_t i,result,duplicate=0; struct dicefinish_info *ptr; ptr = (struct dicefinish_info *)_ptr; sleep(3); // wait for bettxid to be in mempool for (i=0; ibettxid; } unstringbits(name,ptr->sbits); - fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); + //fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - CTransaction tx,bettx; uint256 txid,hashBlock; char str[65]; int32_t result; res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 && res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) - { - if ( DecodeHexTx(tx,res) != 0 ) - { - fprintf(stderr,"iswin.%d %s\n%s\n",ptr->iswin,res.c_str(),uint256_str(str,tx.GetHash())); - LOCK(cs_main); - if ( myAddtomempool(tx) != 0 ) - { - RelayTransaction(tx); - fprintf(stderr,"added to mempool and broadcast\n"); - } else fprintf(stderr,"error adding to mempool\n"); - } else fprintf(stderr,"error decoding hex\n"); - } + if ( result != 0 ) + mySendrawtransaction(res); } free(ptr); return(0); @@ -621,7 +629,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi if ( funcid != 'F' && funcid != 'T' ) { n++; - fprintf(stderr,"(%c %.8f) ",funcid,(double)nValue/COIN); + fprintf(stderr,"%s.(%c %.8f) ",uint256_str(str,txid),funcid,(double)nValue/COIN); } totalinputs += nValue; if ( first == 0 && (funcid == 'E' || funcid == 'W' || funcid == 'L') ) @@ -885,7 +893,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet return(0); } -std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) +std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; @@ -914,6 +922,7 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin bettorentropy = DiceGetEntropy(betTx,'B'); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { + winlosetimeout = iswin; if ( iswin == winlosetimeout ) { if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) @@ -986,3 +995,47 @@ std::string DiceWinLoseTimeout(int32_t *resultp,uint64_t txfee,char *planstr,uin return("0"); } +double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) +{ + CScript fundingPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; + if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) + { + fprintf(stderr,"Diceinit error\n"); + return("0"); + } + fundingpk = DiceFundingPk(fundingPubKey); + scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + if ( bettxid == zeroid ) // scan + { + + } + else + { + if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) + { + if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() > 2 ) + { + if ( spenttx.vout[2].scriptPubKey == fundingPubKey ) + return(0.); + else return((double)spenttx.vout[2].nValue/COIN); + } else return(0.); + } + else if ( scriptPubKey == fundingPubKey ) + res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,1); + else res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,0); + if ( result != 0 ) + mySendrawtransaction(res); + sleep(1); + if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) + { + if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() >= 2 ) + { + if ( spenttx.vout[2].scriptPubKey == fundingPubKey || ((uint8_t *)spenttx.vout[2].scriptPubKey.data())[0] == 0x6a ) + return(0.); + else return((double)spenttx.vout[2].nValue/COIN); + } else return(0.); + } + fprintf(stderr,"didnt find dicefinish tx\n"); + } + return(0.); +} diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index bedb84383..b76698917 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -348,8 +348,7 @@ UniValue getdifficulty(const UniValue& params, bool fHelp) bool myIsutxo_spentinmempool(uint256 txid,int32_t vout) { - char *uint256_str(char *str,uint256); - char str[65]; + //char *uint256_str(char *str,uint256); char str[65]; LOCK(mempool.cs); BOOST_FOREACH(const CTxMemPoolEntry &e,mempool.mapTx) { @@ -357,11 +356,11 @@ bool myIsutxo_spentinmempool(uint256 txid,int32_t vout) const uint256 &hash = tx.GetHash(); BOOST_FOREACH(const CTxIn &txin,tx.vin) { - fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n); + //fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n); if ( txin.prevout.n == vout && txin.prevout.hash == txid ) return(true); } - fprintf(stderr,"are vins for %s\n",uint256_str(str,hash)); + //fprintf(stderr,"are vins for %s\n",uint256_str(str,hash)); } return(false); } diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index fa4118294..4c7488d02 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -119,6 +119,19 @@ UniValue TxJoinSplitToJSON(const CTransaction& tx) { uint64_t komodo_accrued_interest(int32_t *txheightp,uint32_t *locktimep,uint256 hash,int32_t n,int32_t checkheight,uint64_t checkvalue,int32_t tipheight); +int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout) +{ + CSpentIndexValue spentInfo; CSpentIndexKey spentKey(txid,vout); + if ( GetSpentIndex(spentKey,spentInfo) ) + { + spenttxid = spentInfo.txid.GetHex(); + return((int32_t)spentInfo.inputIndex)); + // out.push_back(Pair("spentHeight", spentInfo.blockHeight)); + } + memset(&spenttxid,0,sizeof(spenttxid)); + return(-1); +} + void TxToJSONExpanded(const CTransaction& tx, const uint256 hashBlock, UniValue& entry, int nHeight = 0, int nConfirmations = 0, int nBlockTime = 0) { uint256 txid = tx.GetHash(); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 3e92ab7b8..7b410babd 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -377,9 +377,8 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicefund", &dicefund, true }, { "dice", "diceaddfunds", &diceaddfunds, true }, { "dice", "dicebet", &dicebet, true }, - { "dice", "dicewinner", &dicewinner, true }, - { "dice", "diceloser", &diceloser, true }, - { "dice", "dicetimeout", &dicetimeout, true }, + { "dice", "dicefinish", &dicewinner, true }, + { "dice", "dicestatus", &diceloser, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ diff --git a/src/rpcserver.h b/src/rpcserver.h index 3e25fc4cc..4b5dcdc2c 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -240,9 +240,8 @@ extern UniValue dicelist(const UniValue& params, bool fHelp); extern UniValue diceinfo(const UniValue& params, bool fHelp); extern UniValue diceaddfunds(const UniValue& params, bool fHelp); extern UniValue dicebet(const UniValue& params, bool fHelp); -extern UniValue dicewinner(const UniValue& params, bool fHelp); -extern UniValue diceloser(const UniValue& params, bool fHelp); -extern UniValue dicetimeout(const UniValue& params, bool fHelp); +extern UniValue dicefinish(const UniValue& params, bool fHelp); +extern UniValue dicestatus(const UniValue& params, bool fHelp); extern UniValue lottoaddress(const UniValue& params, bool fHelp); extern UniValue FSMaddress(const UniValue& params, bool fHelp); extern UniValue FSMcreate(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b403c6ac5..58d790275 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5238,11 +5238,11 @@ UniValue dicebet(const UniValue& params, bool fHelp) return(result); } -UniValue dicewinner(const UniValue& params, bool fHelp) +UniValue dicefinish(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; if ( fHelp || params.size() != 3 ) - throw runtime_error("dicewinner name fundingtxid bettxid\n"); + throw runtime_error("dicefinish name fundingtxid bettxid\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"); const CKeyStore& keystore = *pwalletMain; @@ -5250,20 +5250,20 @@ UniValue dicewinner(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,1); + hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create dicewinner transaction")); + } else result.push_back(Pair("error", "couldnt create dicefinish transaction")); return(result); } -UniValue diceloser(const UniValue& params, bool fHelp) +UniValue dicestatus(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; int32_t r; double winnings; if ( fHelp || params.size() != 3 ) - throw runtime_error("diceloser name fundingtxid bettxid\n"); + throw runtime_error("dicestatus name fundingtxid bettxid\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"); const CKeyStore& keystore = *pwalletMain; @@ -5271,33 +5271,13 @@ UniValue diceloser(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,-1); - if ( hex.size() > 0 ) + winnings = DiceStatus(&r,0,name,fundingtxid,bettxid); + result.push_back(Pair("result", "success")); + if ( winnings != 0. ) { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create diceloser transaction")); - return(result); -} - -UniValue dicetimeout(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; - if ( fHelp || params.size() != 3 ) - throw runtime_error("dicetimeout name fundingtxid bettxid\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"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - name = (char *)params[0].get_str().c_str(); - fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); - bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = DiceWinLoseTimeout(&r,0,name,fundingtxid,bettxid,0); - if ( hex.size() > 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } else result.push_back(Pair("error", "couldnt create dicetimeout transaction")); + result.push_back(Pair("status", "win")); + result.push_back(Pair("won", winnings)); + } else result.push_back(Pair("status", "loss")); return(result); } From 4404f47258ccc04ec920f0e40cad1121077e37b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:06:33 -1100 Subject: [PATCH 201/233] Test --- src/cc/CCinclude.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index cf98251be..f10027c97 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -56,7 +56,7 @@ bool myAddtomempool(CTransaction &tx); //uint64_t myGettxout(uint256 hash,int32_t n); bool myIsutxo_spentinmempool(uint256 txid,int32_t vout); int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout); -bool mySendrawtransaction(std:string res); +bool mySendrawtransaction(std::string res); // CCcustom CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); From b3c09a21f932f05cb683ada6c49e47a6e8d778ca Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:08:05 -1100 Subject: [PATCH 202/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b158bd49a..f8c8fe553 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -92,7 +92,7 @@ struct dicefinish_info int32_t iswin; }; -bool mySendrawtransaction(std:string res) +bool mySendrawtransaction(std::string res) { CTransaction tx; char str[65]; if ( res.empty() == 0 && res.size() > 64 && is_hexstr((char *)res.c_str(),0) > 64 ) From 6113aef25adced9f457e76c69afcc7597876e628 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:09:13 -1100 Subject: [PATCH 203/233] Test --- src/cc/dice.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f8c8fe553..8caad69d6 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -997,14 +997,13 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CScript fundingPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; + CScript fundingPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); - return("0"); + return(0.); } fundingpk = DiceFundingPk(fundingPubKey); - scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( bettxid == zeroid ) // scan { From f5d3217a8de1ee62901999faa6f760ea92d1ca62 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:12:45 -1100 Subject: [PATCH 204/233] Test --- src/cc/CCdice.h | 2 +- src/cc/dice.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/CCdice.h b/src/cc/CCdice.h index 007cb7ec9..16b2f6136 100644 --- a/src/cc/CCdice.h +++ b/src/cc/CCdice.h @@ -25,7 +25,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx); std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet,int32_t odds); std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout); -std::string DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); +double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid); std::string DiceCreateFunding(uint64_t txfee,char *planstr,int64_t funds,int64_t minbet,int64_t maxbet,int64_t maxodds,int64_t timeoutblocks); std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t amount); UniValue DiceInfo(uint256 diceid); diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 8caad69d6..8bd0b4ed0 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -138,7 +138,7 @@ void *dicefinish(void *_ptr) //fprintf(stderr,"duplicate.%d dicefinish.%d %s funding.%s bet.%s\n",duplicate,ptr->iswin,name,uint256_str(str,ptr->fundingtxid),uint256_str(str2,ptr->bettxid)); if ( duplicate == 0 ) { - res = DiceWinLoseTimeout(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); + res = DiceBetFinish(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); if ( result != 0 ) mySendrawtransaction(res); } @@ -997,13 +997,14 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CScript fundingPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; + CScript fundingPubKey,scriptPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); return(0.); } fundingpk = DiceFundingPk(fundingPubKey); + scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( bettxid == zeroid ) // scan { From 9726ead472c5b86e48ba3c91a8bdba7ed3a08476 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:15:36 -1100 Subject: [PATCH 205/233] Test --- src/rpcrawtransaction.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 4c7488d02..f77d9be94 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -124,8 +124,8 @@ int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout) CSpentIndexValue spentInfo; CSpentIndexKey spentKey(txid,vout); if ( GetSpentIndex(spentKey,spentInfo) ) { - spenttxid = spentInfo.txid.GetHex(); - return((int32_t)spentInfo.inputIndex)); + spenttxid = spentInfo.txid; + return((int32_t)spentInfo.inputIndex); // out.push_back(Pair("spentHeight", spentInfo.blockHeight)); } memset(&spenttxid,0,sizeof(spenttxid)); From cdd6d559d7bb169899cbe4d2646ca62e56fbb98b Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:16:47 -1100 Subject: [PATCH 206/233] Test --- src/rpcserver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 7b410babd..06fdc433b 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -377,8 +377,8 @@ static const CRPCCommand vRPCCommands[] = { "dice", "dicefund", &dicefund, true }, { "dice", "diceaddfunds", &diceaddfunds, true }, { "dice", "dicebet", &dicebet, true }, - { "dice", "dicefinish", &dicewinner, true }, - { "dice", "dicestatus", &diceloser, true }, + { "dice", "dicefinish", &dicefinish, true }, + { "dice", "dicestatus", &dicestatus, true }, { "dice", "diceaddress", &diceaddress, true }, /* tokens */ From 4e525e48e98cccd9dfce4e90db2582a830793421 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:19:48 -1100 Subject: [PATCH 207/233] Test --- src/wallet/rpcwallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 58d790275..4942cfb8b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5261,7 +5261,7 @@ UniValue dicefinish(const UniValue& params, bool fHelp) UniValue dicestatus(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; int32_t r; double winnings; + UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; double winnings; if ( fHelp || params.size() != 3 ) throw runtime_error("dicestatus name fundingtxid bettxid\n"); if ( ensure_CCrequirements() < 0 ) @@ -5271,7 +5271,7 @@ UniValue dicestatus(const UniValue& params, bool fHelp) name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); - winnings = DiceStatus(&r,0,name,fundingtxid,bettxid); + winnings = DiceStatus(0,name,fundingtxid,bettxid); result.push_back(Pair("result", "success")); if ( winnings != 0. ) { From 92b48feececa7fdcb8af2b60fc294032674330a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:28:45 -1100 Subject: [PATCH 208/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 8bd0b4ed0..0b86921cc 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -928,7 +928,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) { fprintf(stderr,"bettxid already spent\n"); - return(0); + return("0"); } //fprintf(stderr,"iswin.%d matches\n",iswin); mtx.vin.push_back(CTxIn(bettxid,0,CScript())); @@ -939,7 +939,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( DiceVerifyTimeout(betTx,timeoutblocks) == 0 ) // hasnt timed out yet { fprintf(stderr,"timeout is not supported yet\n"); - return(0); + return("0"); } else { From cd2a5d0af0c6a81c153f1d7b5362a7c94135fefe Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:33:03 -1100 Subject: [PATCH 209/233] Test --- src/cc/dice.cpp | 1 + src/wallet/rpcwallet.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0b86921cc..a02fc514f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -897,6 +897,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 { CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; + char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid)); if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4942cfb8b..d81160deb 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5248,6 +5248,7 @@ UniValue dicefinish(const UniValue& params, bool fHelp) const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); +fprintf(stderr,"name %s\n",name); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); From 598e88cec5a1eb79cecffc2b7a4300d90295e5c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:39:53 -1100 Subject: [PATCH 210/233] Test --- src/cc/CCtx.cpp | 4 ++-- src/cc/dice.cpp | 5 +++-- src/wallet/rpcwallet.cpp | 1 - 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cc/CCtx.cpp b/src/cc/CCtx.cpp index 66a5d4388..bea26e4a2 100644 --- a/src/cc/CCtx.cpp +++ b/src/cc/CCtx.cpp @@ -52,7 +52,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran if ( (n= mtx.vin.size()) > 64 ) { fprintf(stderr,"FinalizeCCTx: %d is too many vins\n",n); - return(0); + return("0"); } Myprivkey(myprivkey); unspendablepk = GetUnspendable(cp,unspendablepriv); @@ -147,7 +147,7 @@ std::string FinalizeCCTx(uint64_t CCmask,struct CCcontract_info *cp,CMutableTran std::string strHex = EncodeHexTx(mtx); if ( strHex.size() > 0 ) return(strHex); - else return(0); + else return("0"); } void SetCCunspents(std::vector > &unspentOutputs,char *coinaddr) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a02fc514f..a3877c8b1 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -982,8 +982,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( winlosetimeout != 0 ) hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); *resultp = 1; - //char str[65],str2[65]; - //fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); + char str[65],str2[65]; + fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); return(FinalizeCCTx(0,cp,mtx,fundingpk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } @@ -993,6 +993,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 return("0"); } } + fprintf(stderr,"couldnt find bettx or entropytx\n"); return("0"); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index d81160deb..4942cfb8b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5248,7 +5248,6 @@ UniValue dicefinish(const UniValue& params, bool fHelp) const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); -fprintf(stderr,"name %s\n",name); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); bettxid = Parseuint256((char *)params[2].get_str().c_str()); hex = DiceBetFinish(&r,0,name,fundingtxid,bettxid,1); From dbf213821f274acbc35553c83ae618bd1f0a5466 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:44:34 -1100 Subject: [PATCH 211/233] Test --- src/cc/dice.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a3877c8b1..6de0e80dd 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -912,15 +912,18 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fprintf(stderr,"only dice fund creator can submit winner or loser\n"); return("0"); } + fprintf(stderr,"ismine dice plan\n"); } if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 ) { fprintf(stderr,"no txfee inputs for win/lose\n"); return("0"); } + fprintf(stderr,"added inputs\n"); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); + fprintf(stderr,"got bettor entropy\n"); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { winlosetimeout = iswin; @@ -931,7 +934,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fprintf(stderr,"bettxid already spent\n"); return("0"); } - //fprintf(stderr,"iswin.%d matches\n",iswin); + fprintf(stderr,"iswin.%d matches\n",iswin); mtx.vin.push_back(CTxIn(bettxid,0,CScript())); mtx.vin.push_back(CTxIn(bettxid,1,CScript())); if ( iswin == 0 ) From 507520445b12e5f44cf6d07c36abfceabaa448ba Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:47:06 -1100 Subject: [PATCH 212/233] Test --- src/cc/dice.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 6de0e80dd..8fdf83615 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -366,6 +366,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction //char str[65],str2[65]; if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { + fprintf(stderr,"inside diceiswinner\n"); if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); @@ -926,6 +927,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fprintf(stderr,"got bettor entropy\n"); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { + fprintf(stderr,"inside if/else\n"); winlosetimeout = iswin; if ( iswin == winlosetimeout ) { From 846c9eb9bfe06654001e74603c600235134488d2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:50:46 -1100 Subject: [PATCH 213/233] Test --- src/cc/dice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 8fdf83615..b529f1453 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -364,6 +364,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction { uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; + fprintf(stderr,"dice is winner\n");. if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { fprintf(stderr,"inside diceiswinner\n"); @@ -387,7 +388,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction } } else fprintf(stderr,"hentropy != hentropy2\n"); } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)vinsbits,fundingtxid == vinfundingtxid); - } //else fprintf(stderr,"notmine or not CC\n"); + } else fprintf(stderr,"notmine or not CC\n"); return(0); } @@ -924,7 +925,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); - fprintf(stderr,"got bettor entropy\n"); + fprintf(stderr,"got bettor entropy winlosetimeout.%d\n",winlosetimeout); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { fprintf(stderr,"inside if/else\n"); From 6555bf3aa41c050f622a8aa7da2703b75ac87aab Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:51:42 -1100 Subject: [PATCH 214/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b529f1453..01bdb0151 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -364,7 +364,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction { uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; - fprintf(stderr,"dice is winner\n");. + fprintf(stderr,"dice is winner\n"); if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { fprintf(stderr,"inside diceiswinner\n"); From 9cd014a9734c8ad2c829233637cc2c4b746f70fe Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:55:02 -1100 Subject: [PATCH 215/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 01bdb0151..5e58e3df0 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -364,8 +364,8 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction { uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; - fprintf(stderr,"dice is winner\n"); - if ( DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout.size() > 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) + fprintf(stderr,"dice is winner vout[%d]\n",(int32_t)vinTx.vout.size()); + if ( vinTx.vout.size() > 1 && DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { fprintf(stderr,"inside diceiswinner\n"); if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L') && sbits == vinsbits && fundingtxid == vinfundingtxid ) From 1fadc7992c7b3ddcbbf85a76be336a364587dec5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 03:57:55 -1100 Subject: [PATCH 216/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 5e58e3df0..ae55116ef 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -677,7 +677,7 @@ uint64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbi fprintf(stderr," (%c) tx vin.%d fundingPubKey mismatch %s\n",funcid,tx.vin[0].prevout.n,uint256_str(str,tx.vin[0].prevout.hash)); } } - } else fprintf(stderr,"%c refsbits.%llx sbits.%llx nValue %.8f\n",funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); + } else fprintf(stderr,"%s %c refsbits.%llx sbits.%llx nValue %.8f\n",uint256_str(str,txid),funcid,(long long)refsbits,(long long)sbits,(double)nValue/COIN); } //else fprintf(stderr,"else case funcid (%c) %d %s vs %s\n",funcid,funcid,uint256_str(str,reffundingtxid),uint256_str(str2,fundingtxid)); } //else fprintf(stderr,"funcid.%d %c skipped %.8f\n",funcid,funcid,(double)tx.vout[vout].nValue/COIN); } From d74d791ae02cef8a9f5cae9058669ad73227d0b1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:07:15 -1100 Subject: [PATCH 217/233] Test --- src/cc/dice.cpp | 44 +++++++++++++++++++--------------------- src/wallet/rpcwallet.cpp | 11 ++++++---- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ae55116ef..499aab65f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -139,7 +139,7 @@ void *dicefinish(void *_ptr) if ( duplicate == 0 ) { res = DiceBetFinish(&result,0,name,ptr->fundingtxid,ptr->bettxid,ptr->iswin); - if ( result != 0 ) + if ( result > 0 ) mySendrawtransaction(res); } free(ptr); @@ -364,10 +364,8 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction { uint64_t vinsbits,winnings; uint256 vinproof,vinfundingtxid,hentropy,hentropy2; uint8_t funcid; //char str[65],str2[65]; - fprintf(stderr,"dice is winner vout[%d]\n",(int32_t)vinTx.vout.size()); if ( vinTx.vout.size() > 1 && DiceIsmine(vinTx.vout[1].scriptPubKey) != 0 && vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - fprintf(stderr,"inside diceiswinner\n"); if ( ((funcid= DecodeDiceOpRet(txid,vinTx.vout[vinTx.vout.size()-1].scriptPubKey,vinsbits,vinfundingtxid,hentropy,vinproof)) == 'E' || funcid == 'W' || funcid == 'L') && sbits == vinsbits && fundingtxid == vinfundingtxid ) { hentropy2 = DiceHashEntropy(entropy,vinTx.vin[0].prevout.hash); @@ -388,7 +386,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction } } else fprintf(stderr,"hentropy != hentropy2\n"); } else fprintf(stderr,"funcid.%c sbits %llx vs %llx cmp.%d\n",funcid,(long long)sbits,(long long)vinsbits,fundingtxid == vinfundingtxid); - } else fprintf(stderr,"notmine or not CC\n"); + } //else fprintf(stderr,"notmine or not CC\n"); return(0); } @@ -899,7 +897,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 { CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; - char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid)); + //char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid)); if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); @@ -914,21 +912,17 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fprintf(stderr,"only dice fund creator can submit winner or loser\n"); return("0"); } - fprintf(stderr,"ismine dice plan\n"); } if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 ) { fprintf(stderr,"no txfee inputs for win/lose\n"); return("0"); } - fprintf(stderr,"added inputs\n"); if ( GetTransaction(bettxid,betTx,hashBlock,false) != 0 && GetTransaction(betTx.vin[0].prevout.hash,entropyTx,hashBlock,false) != 0 ) { bettorentropy = DiceGetEntropy(betTx,'B'); - fprintf(stderr,"got bettor entropy winlosetimeout.%d\n",winlosetimeout); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { - fprintf(stderr,"inside if/else\n"); winlosetimeout = iswin; if ( iswin == winlosetimeout ) { @@ -937,7 +931,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fprintf(stderr,"bettxid already spent\n"); return("0"); } - fprintf(stderr,"iswin.%d matches\n",iswin); + //fprintf(stderr,"iswin.%d matches\n",iswin); mtx.vin.push_back(CTxIn(bettxid,0,CScript())); mtx.vin.push_back(CTxIn(bettxid,1,CScript())); if ( iswin == 0 ) @@ -988,17 +982,19 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( winlosetimeout != 0 ) hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash); *resultp = 1; - char str[65],str2[65]; - fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); + //char str[65],str2[65]; + //fprintf(stderr,"iswin.%d house entropy %s vs bettor %s\n",iswin,uint256_str(str,hentropyproof),uint256_str(str2,bettorentropy)); return(FinalizeCCTx(0,cp,mtx,fundingpk,txfee,EncodeDiceOpRet(funcid,sbits,fundingtxid,hentropy,hentropyproof))); } else fprintf(stderr,"iswin.%d does not match.%d\n",iswin,winlosetimeout); } else { + *resultp = -1; fprintf(stderr,"iswin.%d winlosetimeout.%d\n",iswin,winlosetimeout); return("0"); } } + *resultp = -1; fprintf(stderr,"couldnt find bettx or entropytx\n"); return("0"); } @@ -1031,19 +1027,21 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx else if ( scriptPubKey == fundingPubKey ) res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,1); else res = DiceBetFinish(&result,txfee,planstr,fundingtxid,bettxid,0); - if ( result != 0 ) - mySendrawtransaction(res); - sleep(1); - if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) + if ( result > 0 ) { - if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() >= 2 ) + mySendrawtransaction(res); + sleep(1); + if ( (vout= myIsutxo_spent(spenttxid,bettxid,1)) >= 0 ) { - if ( spenttx.vout[2].scriptPubKey == fundingPubKey || ((uint8_t *)spenttx.vout[2].scriptPubKey.data())[0] == 0x6a ) - return(0.); - else return((double)spenttx.vout[2].nValue/COIN); - } else return(0.); - } - fprintf(stderr,"didnt find dicefinish tx\n"); + if ( GetTransaction(spenttxid,spenttx,hashBlock,false) != 0 && spenttx.vout.size() >= 2 ) + { + if ( spenttx.vout[2].scriptPubKey == fundingPubKey || ((uint8_t *)spenttx.vout[2].scriptPubKey.data())[0] == 0x6a ) + return(0.); + else return((double)spenttx.vout[2].nValue/COIN); + } else return(0.); + } + fprintf(stderr,"didnt find dicefinish tx\n"); + } else return(-1.); } return(0.); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4942cfb8b..b12c335a7 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5273,11 +5273,14 @@ UniValue dicestatus(const UniValue& params, bool fHelp) bettxid = Parseuint256((char *)params[2].get_str().c_str()); winnings = DiceStatus(0,name,fundingtxid,bettxid); result.push_back(Pair("result", "success")); - if ( winnings != 0. ) + if ( winnings >= 0. ) { - result.push_back(Pair("status", "win")); - result.push_back(Pair("won", winnings)); - } else result.push_back(Pair("status", "loss")); + if ( winnings > 0. ) + { + result.push_back(Pair("status", "win")); + result.push_back(Pair("won", winnings)); + } else result.push_back(Pair("status", "loss")); + } else result.push_back(Pair("status", "invalid bet txid")); return(result); } From 510d6d5c5e86e794ccad9cae829038e3bfd9cd7f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:21:32 -1100 Subject: [PATCH 218/233] Status --- src/cc/dice.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 499aab65f..f89c17001 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1001,7 +1001,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CScript fundingPubKey,scriptPubKey; CTransaction spenttx; uint256 hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char resultstr[64]; std::string res; + CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); @@ -1009,9 +1009,25 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx } fundingpk = DiceFundingPk(fundingPubKey); scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; + GetCCaddress(cp,coinaddr,dicepk); if ( bettxid == zeroid ) // scan { - + std::vector > unspentOutputs; + SetCCunspents(unspentOutputs,coinaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + vout = (int32_t)it->first.index; + if ( GetTransaction(txid,betTx,hashBlock,false) != 0 && betTx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) + { + if ( DecodeDiceOpRet(txid,betTx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof) == 'B' ) + { + res = DiceBetFinish(&result,txfee,planstr,fundingtxid,txid,1); + if ( result > 0 ) + mySendrawtransaction(res); + } + } + } } else { From 50854c787b6598350c8b9fde8fcf7c2736e110bd Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:23:36 -1100 Subject: [PATCH 219/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index f89c17001..15742c280 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1020,7 +1020,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx vout = (int32_t)it->first.index; if ( GetTransaction(txid,betTx,hashBlock,false) != 0 && betTx.vout[vout].scriptPubKey.IsPayToCryptoCondition() != 0 ) { - if ( DecodeDiceOpRet(txid,betTx.vout[tx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof) == 'B' ) + if ( DecodeDiceOpRet(txid,betTx.vout[betTx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof) == 'B' ) { res = DiceBetFinish(&result,txfee,planstr,fundingtxid,txid,1); if ( result > 0 ) From 75dbd478191ef550743337c659ffa7b3fcbb4473 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:26:06 -1100 Subject: [PATCH 220/233] Status zero --- src/wallet/rpcwallet.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b12c335a7..2fe16d504 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5262,7 +5262,7 @@ UniValue dicefinish(const UniValue& params, bool fHelp) UniValue dicestatus(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; double winnings; - if ( fHelp || params.size() != 3 ) + if ( fHelp || params.size() == 2 || params.size() == 3 ) throw runtime_error("dicestatus name fundingtxid bettxid\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"); @@ -5270,7 +5270,9 @@ UniValue dicestatus(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); name = (char *)params[0].get_str().c_str(); fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); - bettxid = Parseuint256((char *)params[2].get_str().c_str()); + memset(&bettxid,0,sizeof(bettxid)); + if ( params.size() == 3 ) + bettxid = Parseuint256((char *)params[2].get_str().c_str()); winnings = DiceStatus(0,name,fundingtxid,bettxid); result.push_back(Pair("result", "success")); if ( winnings >= 0. ) From 7b44d4c12315e03938dc4079a2e2a36b66738641 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:27:46 -1100 Subject: [PATCH 221/233] Test --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2fe16d504..0de4a4ae8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5262,7 +5262,7 @@ UniValue dicefinish(const UniValue& params, bool fHelp) UniValue dicestatus(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; double winnings; - if ( fHelp || params.size() == 2 || params.size() == 3 ) + if ( fHelp || (params.size() != 2 && params.size() != 3) ) throw runtime_error("dicestatus name fundingtxid bettxid\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"); From 096cfeb8a32b3e65466a39f831cabcc2f6e78a91 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:33:18 -1100 Subject: [PATCH 222/233] Test --- src/cc/dice.cpp | 6 +++++- src/wallet/rpcwallet.cpp | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 15742c280..dd5beae98 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1001,7 +1001,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; + CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout,n=0; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); @@ -1024,10 +1024,14 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx { res = DiceBetFinish(&result,txfee,planstr,fundingtxid,txid,1); if ( result > 0 ) + { mySendrawtransaction(res); + n++; + } } } } + return(n); } else { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 0de4a4ae8..07100e53e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5279,8 +5279,16 @@ UniValue dicestatus(const UniValue& params, bool fHelp) { if ( winnings > 0. ) { - result.push_back(Pair("status", "win")); - result.push_back(Pair("won", winnings)); + if ( params.size() == 3 ) + { + result.push_back(Pair("status", "win")); + result.push_back(Pair("won", winnings)); + } + else + { + result.push_back(Pair("status", "finalized")); + result.push_back(Pair("n", (int64_t)winnings)); + } } else result.push_back(Pair("status", "loss")); } else result.push_back(Pair("status", "invalid bet txid")); return(result); From 3bb6e2335ea9661ef70cb2afc3b34c1669fc7730 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 04:40:20 -1100 Subject: [PATCH 223/233] Test --- src/wallet/rpcwallet.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 07100e53e..295460618 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5289,7 +5289,13 @@ UniValue dicestatus(const UniValue& params, bool fHelp) result.push_back(Pair("status", "finalized")); result.push_back(Pair("n", (int64_t)winnings)); } - } else result.push_back(Pair("status", "loss")); + } + else + { + if ( params.size() == 3 ) + result.push_back(Pair("status", "loss")); + else result.push_back(Pair("status", "no pending bets")); + } } else result.push_back(Pair("status", "invalid bet txid")); return(result); } From 221972777d864670004023fca6577f4cbf572f50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 05:13:18 -1100 Subject: [PATCH 224/233] Test --- src/cc/dice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index dd5beae98..8e7dd63f5 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -372,7 +372,7 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction if ( hentropy == hentropy2 ) { winnings = DiceCalc(tx.vout[1].nValue,tx.vout[2].nValue,minbet,maxbet,maxodds,timeoutblocks,entropy,bettorentropy); - fprintf(stderr,"winnings %.8f bet %.8f at odds %d:1\n",(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-10000)); + char str[65]; fprintf(stderr,"%s winnings %.8f bet %.8f at odds %d:1\n",uint256_str(str,tx.GetHash()),(double)winnings/COIN,(double)tx.vout[1].nValue/COIN,(int32_t)(tx.vout[2].nValue-10000)); //fprintf(stderr,"I am house entropy %.8f entropy.(%s) vs %s -> winnings %.8f\n",(double)vinTx.vout[0].nValue/COIN,uint256_str(str,entropy),uint256_str(str2,hash),(double)winnings/COIN); if ( winnings == 0 ) { @@ -1022,7 +1022,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx { if ( DecodeDiceOpRet(txid,betTx.vout[betTx.vout.size()-1].scriptPubKey,sbits,fundingtxid,hash,proof) == 'B' ) { - res = DiceBetFinish(&result,txfee,planstr,fundingtxid,txid,1); + res = DiceBetFinish(&result,txfee,planstr,fundingtxid,txid,scriptPubKey == fundingPubKey); if ( result > 0 ) { mySendrawtransaction(res); From d4c70c4f7ae678d79b38fce67a6e0fda1310ced4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 05:24:47 -1100 Subject: [PATCH 225/233] Funding plan creator to auto create entropy tx on dice status --- src/cc/dice.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 8e7dd63f5..ad74fcfef 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1031,6 +1031,14 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx } } } + if ( scriptPubKey == fundingPubKey ) + { + for (i=0; i<=n; i++) + { + res = DiceAddfunding(txfee,planstr,fundingtxid,COIN); + mySendrawtransaction(res); + } + } return(n); } else From fba740ea43452174e59939fdb2e9ceedbff6f0c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 05:30:37 -1100 Subject: [PATCH 226/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ad74fcfef..a213dc9db 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1001,7 +1001,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid) { - CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t result,vout,n=0; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; + CScript fundingPubKey,scriptPubKey; CTransaction spenttx,betTx; uint256 hash,proof,txid,hashBlock,spenttxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int32_t i,result,vout,n=0; int64_t minbet,maxbet,maxodds,timeoutblocks; uint64_t sbits; char coinaddr[64]; std::string res; if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) { fprintf(stderr,"Diceinit error\n"); From 8132e08781f0abb2fd9625569ac9f40c6801f9fd Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 21:33:06 -1100 Subject: [PATCH 227/233] Add back join split in decoderawtransaction --- src/cc/dice.cpp | 1 + src/rpcrawtransaction.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index a213dc9db..68c3e6cc7 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -1036,6 +1036,7 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx for (i=0; i<=n; i++) { res = DiceAddfunding(txfee,planstr,fundingtxid,COIN); + fprintf(stderr,"ENTROPY tx:\n"); mySendrawtransaction(res); } } diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index f77d9be94..798b991d7 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -280,6 +280,8 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) vout.push_back(out); } entry.push_back(Pair("vout", vout)); + UniValue vjoinsplit = TxJoinSplitToJSON(tx); + entry.push_back(Pair("vjoinsplit", vjoinsplit)); if (!hashBlock.IsNull()) { entry.push_back(Pair("blockhash", hashBlock.GetHex())); From da07d84b84a4ba6c08433ed91d4bdbd05de37bd3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 23:02:51 -1100 Subject: [PATCH 228/233] Implement dice timeout --- src/cc/CCinclude.h | 2 +- src/cc/CCutils.cpp | 16 +++++++++------- src/cc/dice.cpp | 6 ++++-- src/cc/rewards.cpp | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index f10027c97..0ab0bf874 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -79,7 +79,7 @@ bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t n bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey); std::vector Mypubkey(); bool Myprivkey(uint8_t myprivkey[]); -int64_t CCduration(uint256 txid); +int64_t CCduration(int32_t &numblocks,uint256 txid); // CCtx std::string FinalizeCCTx(uint64_t skipmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index fff113d30..240e5b7e0 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -275,9 +275,10 @@ bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector param return(false); } -int64_t CCduration(uint256 txid) +int64_t CCduration(int32_t &numblocks,uint256 txid) { - CTransaction tx; uint256 hashBlock; uint32_t txtime=0; char str[65]; CBlockIndex *pindex; int64_t duration = 0; + CTransaction tx; uint256 hashBlock; uint32_t txheight,txtime=0; char str[65]; CBlockIndex *pindex; int64_t duration = 0; + numblocks = 0; if ( myGetTransaction(txid,tx,hashBlock) == 0 ) { fprintf(stderr,"CCduration cant find duration txid %s\n",uint256_str(str,txid)); @@ -288,18 +289,19 @@ int64_t CCduration(uint256 txid) fprintf(stderr,"CCduration no hashBlock for txid %s\n",uint256_str(str,txid)); return(0); } - else if ( (pindex= mapBlockIndex[hashBlock]) == 0 || (txtime= pindex->nTime) == 0 ) + else if ( (pindex= mapBlockIndex[hashBlock]) == 0 || (txtime= pindex->nTime) == 0 || (txheight= pindex->nHeight) <= 0 ) { - fprintf(stderr,"CCduration no txtime %u %p for txid %s\n",txtime,pindex,uint256_str(str,txid)); + fprintf(stderr,"CCduration no txtime %u or txheight.%d %p for txid %s\n",txtime,txheight,pindex,uint256_str(str,txid)); return(0); } - else if ( (pindex= chainActive.LastTip()) == 0 || pindex->nTime < txtime ) + else if ( (pindex= chainActive.LastTip()) == 0 || pindex->nTime < txtime || pindex->nHeight <= txheight ) { - fprintf(stderr,"CCduration backwards timestamps %u %u for txid %s\n",(uint32_t)pindex->nTime,txtime,uint256_str(str,txid)); + fprintf(stderr,"CCduration backwards timestamps %u %u for txid %s hts.(%d %d)\n",(uint32_t)pindex->nTime,txtime,uint256_str(str,txid),txheight,(int32_t)pindex->nHeight); return(0); } + numblocks = (pindex->nHeight - txheight); duration = (pindex->nTime - txtime); - fprintf(stderr,"duration %d (%u - %u)\n",(int32_t)duration,(uint32_t)pindex->nTime,txtime); + fprintf(stderr,"duration %d (%u - %u) numblocks %d (%d - %d)\n",(int32_t)duration,(uint32_t)pindex->nTime,txtime,numblocks,pindex->nHeight,txheight); return(duration); } diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 68c3e6cc7..37a6959fc 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -392,8 +392,10 @@ int32_t DiceIsWinner(uint256 &entropy,uint256 txid,CTransaction tx,CTransaction bool DiceVerifyTimeout(CTransaction &betTx,int32_t timeoutblocks) { - fprintf(stderr,"DiceVerifyTimeout needs to be implemented\n"); - return(false); + int32_t numblocks; + if ( CCduration(numblocks,betTx.GetHash()) <= 0 ) + return(false); + return(numblocks >= timeoutblocks); } bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index c2f6dcd27..a0f07aba1 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -68,9 +68,9 @@ uint64_t RewardsCalc(uint64_t amount,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) { - uint64_t duration,reward = 0; + int32_t numblocks; uint64_t duration,reward = 0; fprintf(stderr,"minseconds %llu maxseconds %llu\n",(long long)minseconds,(long long)maxseconds); - if ( (duration= CCduration(txid)) < minseconds ) + if ( (duration= CCduration(numblocks,txid)) < minseconds ) { fprintf(stderr,"duration %llu < minseconds %llu\n",(long long)duration,(long long)minseconds); return(0); From 8e3c66caf9e03fead4d33051dfd924db02afc3f4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 23:09:42 -1100 Subject: [PATCH 229/233] Test --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 37a6959fc..c46851919 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -912,7 +912,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 if ( scriptPubKey != fundingPubKey ) { fprintf(stderr,"only dice fund creator can submit winner or loser\n"); - return("0"); + winlosetimeout = 0; } } if ( AddNormalinputs(mtx,mypk,txfee,1) == 0 ) From b0becd6e7c764c0e7cec3f12551b326a62f2599f Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 2 Aug 2018 23:32:03 -1100 Subject: [PATCH 230/233] Test --- src/cc/dice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c46851919..02eb99a58 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -911,7 +911,7 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( scriptPubKey != fundingPubKey ) { - fprintf(stderr,"only dice fund creator can submit winner or loser\n"); + //fprintf(stderr,"only dice fund creator can submit winner or loser\n"); winlosetimeout = 0; } } @@ -925,7 +925,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 bettorentropy = DiceGetEntropy(betTx,'B'); if ( winlosetimeout == 0 || (iswin= DiceIsWinner(hentropyproof,bettxid,betTx,entropyTx,bettorentropy,sbits,minbet,maxbet,maxodds,timeoutblocks,fundingtxid)) != 0 ) { - winlosetimeout = iswin; + if ( winlosetimeout != 0 ) + winlosetimeout = iswin; if ( iswin == winlosetimeout ) { if ( myIsutxo_spentinmempool(bettxid,0) != 0 || myIsutxo_spentinmempool(bettxid,1) != 0 ) @@ -941,7 +942,6 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 funcid = 'T'; if ( DiceVerifyTimeout(betTx,timeoutblocks) == 0 ) // hasnt timed out yet { - fprintf(stderr,"timeout is not supported yet\n"); return("0"); } else From ac8b2c68d25390f2b1aea4618167947ad31deace Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 3 Aug 2018 00:09:53 -1100 Subject: [PATCH 231/233] Disable assets swap --- src/cc/CCassetstx.cpp | 8 ++++++++ src/cc/assets.cpp | 5 +++-- src/rpcserver.cpp | 4 ++-- src/rpcserver.h | 4 ++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 22ca71c66..51668eda8 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -305,6 +305,8 @@ std::string CreateSell(int64_t txfee,int64_t askamount,uint256 assetid,int64_t p std::string CreateSwap(int64_t txfee,int64_t askamount,uint256 assetid,uint256 assetid2,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; uint64_t mask; int64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + fprintf(stderr,"asset swaps disabled\n"); + return(0); if ( askamount < 0 || pricetotal < 0 ) { fprintf(stderr,"negative askamount %lld, askamount %lld\n",(long long)pricetotal,(long long)askamount); @@ -429,6 +431,12 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); return(0); } + if ( assetid2 != zeroid ) + { + fprintf(stderr,"asset swaps disabled\n"); + return(0); + } + cp = CCinit(&C,EVAL_ASSETS); if ( txfee == 0 ) txfee = 10000; diff --git a/src/cc/assets.cpp b/src/cc/assets.cpp index 2088d9b73..8adecb3e4 100644 --- a/src/cc/assets.cpp +++ b/src/cc/assets.cpp @@ -250,9 +250,9 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx } fprintf(stderr,"fillbuy validated\n"); break; - - case 's': // selloffer case 'e': // selloffer + break; // disable swaps + case 's': // selloffer //vin.0: normal input //vin.1+: valid CC output for sale //vout.0: vin.1 assetoshis output to CC to unspendable @@ -323,6 +323,7 @@ bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx fprintf(stderr,"fill validated\n"); break; case 'E': // fillexchange + break; // disable asset swaps //vin.0: normal input //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0] //vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 06fdc433b..41228e71a 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -393,10 +393,10 @@ static const CRPCCommand vRPCCommands[] = { "tokens", "tokencancelbid", &tokencancelbid, true }, { "tokens", "tokenfillbid", &tokenfillbid, true }, { "tokens", "tokenask", &tokenask, true }, - { "tokens", "tokenswapask", &tokenswapask, true }, + //{ "tokens", "tokenswapask", &tokenswapask, true }, { "tokens", "tokencancelask", &tokencancelask, true }, { "tokens", "tokenfillask", &tokenfillask, true }, - { "tokens", "tokenfillswap", &tokenfillswap, true }, + //{ "tokens", "tokenfillswap", &tokenfillswap, true }, /* Address index */ { "addressindex", "getaddressmempool", &getaddressmempool, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 4b5dcdc2c..f88f1e571 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -221,8 +221,8 @@ extern UniValue tokenfillbid(const UniValue& params, bool fHelp); extern UniValue tokenask(const UniValue& params, bool fHelp); extern UniValue tokencancelask(const UniValue& params, bool fHelp); extern UniValue tokenfillask(const UniValue& params, bool fHelp); -extern UniValue tokenswapask(const UniValue& params, bool fHelp); -extern UniValue tokenfillswap(const UniValue& params, bool fHelp); +//extern UniValue tokenswapask(const UniValue& params, bool fHelp); +//extern UniValue tokenfillswap(const UniValue& params, bool fHelp); extern UniValue faucetfund(const UniValue& params, bool fHelp); extern UniValue faucetget(const UniValue& params, bool fHelp); extern UniValue faucetaddress(const UniValue& params, bool fHelp); From ebe7e27ada2b5d17cda14007c262a2323df62342 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 3 Aug 2018 01:51:05 -1100 Subject: [PATCH 232/233] Fix timeout claim --- src/cc/dice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 02eb99a58..5d9aa328d 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -897,7 +897,7 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout) { - CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid; int32_t iswin=0; uint64_t entropyval,sbits; + CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; uint8_t funcid=0; int32_t iswin=0; uint64_t entropyval,sbits; *resultp = 0; //char str[65]; fprintf(stderr,"DiceBetFinish.%s %s\n",planstr,uint256_str(str,bettxid)); if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) @@ -952,7 +952,8 @@ std::string DiceBetFinish(int32_t *resultp,uint64_t txfee,char *planstr,uint256 } if ( iswin > 0 ) { - funcid = 'W'; + if ( funcid != 'T' ) + funcid = 'W'; odds = (betTx.vout[2].nValue - txfee); if ( odds < 1 || odds > maxodds ) { From 99c090d95e338367216646ef17ef18d61a0434ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 3 Aug 2018 03:07:01 -1100 Subject: [PATCH 233/233] Fix edge case for maxodds --- src/cc/dice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 5d9aa328d..c5e8ecf88 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -458,7 +458,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx) return eval->Invalid("vout[0] != entropy nValue for bet"); else if ( ConstrainVout(tx.vout[1],1,cp->unspendableCCaddr,0) == 0 ) return eval->Invalid("vout[1] constrain violation for bet"); - else if ( tx.vout[2].nValue >= txfee+maxodds || tx.vout[2].nValue < txfee ) + else if ( tx.vout[2].nValue > txfee+maxodds || tx.vout[2].nValue < txfee ) return eval->Invalid("vout[2] nValue violation for bet"); else if ( eval->GetTxUnconfirmed(vinTx.vin[0].prevout.hash,vinofvinTx,hashBlock) == 0 || vinofvinTx.vout.size() < 1 ) return eval->Invalid("always should find vinofvin.0, but didnt for bet");