From 8b26f1169c439e76ae4511cd3fcd90610b1fdfbe Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 04:28:41 -1100 Subject: [PATCH 01/21] KOMODO_CONNECTING --- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 1 + src/komodo_globals.h | 2 +- src/main.cpp | 4 +++- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 3954068e1..761e64bcd 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -28,6 +28,7 @@ #include #include +extern int32_t KOMODO_CONNECTING; #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; }; diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 8cb06ab25..073c2b508 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; int64_t amount; std::vector origpubkey; + fprintf(stderr,"KOMODO_CONNECTING.%d\n",KOMODO_CONNECTING); // 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 ) diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 86c168a85..5971e31cc 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -45,7 +45,7 @@ struct komodo_state KOMODO_STATES[34]; #define _COINBASE_MATURITY 100 int COINBASE_MATURITY = _COINBASE_MATURITY;//100; -int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND; +int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,KOMODO_CONNECTING; int32_t KOMODO_INSYNC,KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE = 1; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY; uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_PUBLIC,ASSETCHAINS_PRIVATE; diff --git a/src/main.cpp b/src/main.cpp index 6dcd6b13b..7a0b0a250 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,7 +56,7 @@ using namespace std; CCriticalSection cs_main; extern uint8_t NOTARY_PUBKEY33[33]; -extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN,KOMODO_INSYNC; +extern int32_t KOMODO_LOADINGBLOCKS,KOMODO_LONGESTCHAIN,KOMODO_INSYNC,KOMODO_CONNECTING; int32_t KOMODO_NEWBLOCKS; int32_t komodo_block2pubkey33(uint8_t *pubkey33,CBlock *block); void komodo_broadcast(CBlock *pblock,int32_t limit); @@ -3448,6 +3448,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * return AbortNode(state, "Failed to read block"); pblock = █ } + KOMODO_CONNECTING = (int32_t)pindexNew->nHeight; // Get the current commitment tree ZCIncrementalMerkleTree oldTree; assert(pcoinsTip->GetAnchorAt(pcoinsTip->GetBestAnchor(), oldTree)); @@ -3458,6 +3459,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock * { CCoinsViewCache view(pcoinsTip); bool rv = ConnectBlock(*pblock, state, pindexNew, view, false, true); + KOMODO_CONNECTING = -1; GetMainSignals().BlockChecked(*pblock, state); if (!rv) { if (state.IsInvalid()) From 0bfdfaef027b56abf4d7c74d55bedd9fa480a1f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 04:34:46 -1100 Subject: [PATCH 02/21] Set KOMODO_CONNECTING from accepttomempool --- src/main.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7a0b0a250..b36e5ad82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1263,7 +1263,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa if (pfMissingInputs) *pfMissingInputs = false; - int nextBlockHeight = chainActive.Height() + 1; + int flag=0,nextBlockHeight = chainActive.Height() + 1; auto consensusBranchId = CurrentEpochBranchId(nextBlockHeight, Params().GetConsensus()); // Node operator can choose to reject tx by number of transparent inputs @@ -1283,11 +1283,19 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa //fprintf(stderr,"AcceptToMemoryPool komodo_validate_interest failure\n"); return error("AcceptToMemoryPool: komodo_validate_interest failed"); } + if ( KOMODO_CONNECTING <= 0 && chainActive.LastTip() != 0 ) + { + flag = 1; + KOMODO_CONNECTING = (int32_t)chainActive.LastTip()->nHeight + 1; + } if (!CheckTransaction(tx, state, verifier)) { - + if ( flag != 0 ) + KOMODO_CONNECTING = -1; return error("AcceptToMemoryPool: CheckTransaction failed"); } + if ( flag != 0 ) + KOMODO_CONNECTING = -1; // DoS level set to 10 to be more forgiving. // Check transaction contextually against the set of consensus rules which apply in the next block to be mined. if (!ContextualCheckTransaction(tx, state, nextBlockHeight, 10)) From ad274d5be87bc4a5f1b1ca28d00e095cdf25e96e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 04:40:37 -1100 Subject: [PATCH 03/21] Test --- src/cc/CCutils.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 073c2b508..bdeb93481 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -256,6 +256,8 @@ 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; int64_t amount; std::vector origpubkey; + if ( KOMODO_CONNECTING < 0 ) // always comes back with > 0 for final confirmation + return(true); fprintf(stderr,"KOMODO_CONNECTING.%d\n",KOMODO_CONNECTING); // 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(); From cd88cde2da53696542ae9e5d41899f6acb2aeae0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 06:37:46 -1100 Subject: [PATCH 04/21] test --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index b36e5ad82..74037b65b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1286,7 +1286,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa if ( KOMODO_CONNECTING <= 0 && chainActive.LastTip() != 0 ) { flag = 1; - KOMODO_CONNECTING = (int32_t)chainActive.LastTip()->nHeight + 1; + KOMODO_CONNECTING = 100000000 + (int32_t)chainActive.LastTip()->nHeight + 1; } if (!CheckTransaction(tx, state, verifier)) { From cb5763f0096a84ae26c759772f7d361ff5cf0052 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 06:53:50 -1100 Subject: [PATCH 05/21] Test --- src/cc/CCutils.cpp | 2 +- src/main.cpp | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index bdeb93481..61bebab40 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -256,9 +256,9 @@ 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; int64_t amount; std::vector origpubkey; + fprintf(stderr,"KOMODO_CONNECTING.%d\n",KOMODO_CONNECTING); if ( KOMODO_CONNECTING < 0 ) // always comes back with > 0 for final confirmation return(true); - fprintf(stderr,"KOMODO_CONNECTING.%d\n",KOMODO_CONNECTING); // 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 ) diff --git a/src/main.cpp b/src/main.cpp index 74037b65b..299906a9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1283,26 +1283,17 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa //fprintf(stderr,"AcceptToMemoryPool komodo_validate_interest failure\n"); return error("AcceptToMemoryPool: komodo_validate_interest failed"); } - if ( KOMODO_CONNECTING <= 0 && chainActive.LastTip() != 0 ) - { - flag = 1; - KOMODO_CONNECTING = 100000000 + (int32_t)chainActive.LastTip()->nHeight + 1; - } if (!CheckTransaction(tx, state, verifier)) { - if ( flag != 0 ) - KOMODO_CONNECTING = -1; return error("AcceptToMemoryPool: CheckTransaction failed"); } - if ( flag != 0 ) - KOMODO_CONNECTING = -1; // DoS level set to 10 to be more forgiving. // Check transaction contextually against the set of consensus rules which apply in the next block to be mined. if (!ContextualCheckTransaction(tx, state, nextBlockHeight, 10)) { return error("AcceptToMemoryPool: ContextualCheckTransaction failed"); } - + // Coinbase is only valid in a block, not as a loose transaction if (tx.IsCoinBase()) { @@ -1535,12 +1526,21 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa // invalid blocks, however allowing such transactions into the mempool // can be exploited as a DoS attack. // XXX: is this neccesary for CryptoConditions? + if ( KOMODO_CONNECTING <= 0 && chainActive.LastTip() != 0 ) + { + flag = 1; + KOMODO_CONNECTING = 100000000 + (int32_t)chainActive.LastTip()->nHeight + 1; + } if (!ContextualCheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata, Params().GetConsensus(), consensusBranchId)) { + if ( flag != 0 ) + KOMODO_CONNECTING = -1; fprintf(stderr,"accept failure.10\n"); return error("AcceptToMemoryPool: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s", hash.ToString()); } - + if ( flag != 0 ) + KOMODO_CONNECTING = -1; + // Store transaction in memory if ( komodo_is_notarytx(tx) == 0 ) KOMODO_ON_DEMAND++; From a409a6e419652bc09e9c0a48261bf7874b0903d0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 07:02:12 -1100 Subject: [PATCH 06/21] Test --- src/komodo_globals.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 5971e31cc..2d64bc8fc 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -45,7 +45,7 @@ struct komodo_state KOMODO_STATES[34]; #define _COINBASE_MATURITY 100 int COINBASE_MATURITY = _COINBASE_MATURITY;//100; -int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,KOMODO_CONNECTING; +int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,KOMODO_CONNECTING = -1; int32_t KOMODO_INSYNC,KOMODO_LASTMINED,prevKOMODO_LASTMINED,JUMBLR_PAUSE = 1; std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY; uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_PUBLIC,ASSETCHAINS_PRIVATE; From 1f5025d995902cbced16844c12b25bfb928c85f5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 07:16:14 -1100 Subject: [PATCH 07/21] Handle out of order confirmed CC --- src/main.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 299906a9a..87e79eafc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4168,17 +4168,27 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C // Check transactions if ( ASSETCHAINS_CC != 0 ) // CC contracts might refer to transactions in the current block, from a CC spend within the same block and out of order { - CValidationState stateDummy; + CValidationState stateDummy; int32_t i,j,rejects=0,lastrejects=0; //fprintf(stderr,"put block's tx into mempool\n"); - for (int i = 0; i < block.vtx.size(); i++) + while ( 1 ) { - const CTransaction &tx = block.vtx[i]; - if (tx.IsCoinBase() != 0 ) - continue; - else if ( ASSETCHAINS_STAKED != 0 && (i == (block.vtx.size() - 1)) && komodo_isPoS((CBlock *)&block) != 0 ) - continue; - AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL); - } + for (i=0; i Date: Sun, 19 Aug 2018 07:19:25 -1100 Subject: [PATCH 08/21] Test --- src/cc/CCinclude.h | 2 +- src/cc/dice.cpp | 2 +- src/main.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 761e64bcd..c12389275 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -60,7 +60,7 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { if ( DecodeHexTx(tx,res) != 0 ) diff --git a/src/main.cpp b/src/main.cpp index 87e79eafc..2b239dcf3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1623,7 +1623,7 @@ bool GetAddressUnspent(uint160 addressHash, int type, else return(coins.vout[n].nValue); }*/ -bool myAddtomempool(CTransaction &tx) +bool myAddtomempool(const CTransaction &tx) { CValidationState state; CTransaction Ltx; bool fMissingInputs,fOverrideFees = false; if ( mempool.lookup(tx.GetHash(),Ltx) == 0 ) From 9a813a0441d30e1084fb3569b727caaf28eaaf5f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 07:21:20 -1100 Subject: [PATCH 09/21] Test --- src/cc/CCinclude.h | 2 +- src/cc/dice.cpp | 2 +- src/main.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index c12389275..761e64bcd 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -60,7 +60,7 @@ bool GetAddressUnspent(uint160 addressHash, int type,std::vector 64 && is_hexstr((char *)res.c_str(),0) > 64 ) { if ( DecodeHexTx(tx,res) != 0 ) diff --git a/src/main.cpp b/src/main.cpp index 2b239dcf3..a2ec5cdb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1623,7 +1623,7 @@ bool GetAddressUnspent(uint160 addressHash, int type, else return(coins.vout[n].nValue); }*/ -bool myAddtomempool(const CTransaction &tx) +bool myAddtomempool(CTransaction &tx) { CValidationState state; CTransaction Ltx; bool fMissingInputs,fOverrideFees = false; if ( mempool.lookup(tx.GetHash(),Ltx) == 0 ) @@ -4174,7 +4174,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C { for (i=0; i Date: Sun, 19 Aug 2018 07:56:44 -1100 Subject: [PATCH 10/21] Fix --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a2ec5cdb4..8bf4f269b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4174,7 +4174,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C { for (i=0; i Date: Sun, 19 Aug 2018 07:58:02 -1100 Subject: [PATCH 11/21] Syntax --- src/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8bf4f269b..fba7193d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4174,12 +4174,13 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C { for (i=0; i Date: Sun, 19 Aug 2018 08:42:57 -1100 Subject: [PATCH 12/21] Change mempool print --- src/cc/CCutils.cpp | 10 ++++++++-- src/main.cpp | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 61bebab40..54a9a0225 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -255,10 +255,16 @@ 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; int64_t amount; std::vector origpubkey; - fprintf(stderr,"KOMODO_CONNECTING.%d\n",KOMODO_CONNECTING); + CTransaction createTx; uint256 assetid,assetid2,hashBlock; uint8_t funcid; int32_t height,i,n,from_mempool = 0; int64_t amount; std::vector origpubkey; if ( KOMODO_CONNECTING < 0 ) // always comes back with > 0 for final confirmation return(true); + height = KOMODO_CONNECTING; + if ( (KOMODO_CONNECTING & (1<<30)) != 0 ) + { + from_mempool = 1; + height &= ((1<<30) - 1); + } + fprintf(stderr,"KOMODO_CONNECTING.%d mempool.%d\n",height,from_mempool); // 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 ) diff --git a/src/main.cpp b/src/main.cpp index fba7193d0..5c2e02e54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1529,7 +1529,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa if ( KOMODO_CONNECTING <= 0 && chainActive.LastTip() != 0 ) { flag = 1; - KOMODO_CONNECTING = 100000000 + (int32_t)chainActive.LastTip()->nHeight + 1; + KOMODO_CONNECTING = (1<<30) + (int32_t)chainActive.LastTip()->nHeight + 1; } if (!ContextualCheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata, Params().GetConsensus(), consensusBranchId)) { From 60a91e3bd4fc885d800b44cec8248c5f0539d11d Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 23:04:01 -1100 Subject: [PATCH 13/21] Change dice odds > 1 to match industry method Get the first 5 nibbles < 1 million, mod it by 10000 and compare against 1000/(odds+1) --- src/cc/dice.cpp | 47 ++++++++++++++++++++++++++++++++++++-------- src/komodo_gateway.h | 1 + 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index c2f055d8d..2616bd536 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -225,9 +225,14 @@ uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv) // max 1 vout per tx return(hentropy); } +int32_t dice_5nibbles(uint8_t *fivevals) +{ + return(((int32_t)fivevals[0]<<16) + ((int32_t)fivevals[1]<<12) + ((int32_t)fivevals[2]<<8) + ((int32_t)fivevals[3]<<4) + ((int32_t)fivevals[4])); +} + 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]; + uint8_t buf[64],_house[32],_bettor[32],_hash[32],hash[32],hash16[64]; uint64_t winnings; arith_uint256 hash,house,bettor; char str[65],str2[65]; int32_t i,modval; if ( odds < 10000 ) return(0); else odds -= 10000; @@ -247,11 +252,35 @@ 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 *)&_bettor,buf,64); endiancpy((uint8_t *)&bettor,_bettor,32); + winnings = 0; if ( odds > 1 ) - bettor = (bettor / arith_uint256(odds)); - if ( bettor >= house ) + { + //bettor = (bettor / arith_uint256(odds)); + endiancpy(buf,(uint8_t *)&house,32); + endiancpy(&buf[32],(uint8_t *)&bettor,32); + vcalc_sha256(0,(uint8_t *)&_hash,buf,64); + endiancpy(hash,_hash,32); + for (i=0; i<32; i++) + { + hash16[i<<1] = ((hash[i] >> 4) & 0x0f); + hash16[(i<<1) + 1] = (hash[i] & 0x0f); + } + modval = 0; + for (i=0; i<12; i++) + { + modval = dice_5nibbles(&hash16[i*5]); + if ( modval < 1000000 ) + { + modval %= 10000; + break; + } + } + fprintf(stderr,"modval %d vs %d\n",modval,10000/(odds+1)) + if ( modval < 10000/(odds+1) ) + winnings = bet * (odds+1); + } + else if ( bettor >= house ) winnings = bet * (odds+1); - else winnings = 0; return(winnings); } @@ -424,6 +453,8 @@ 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"); + if ( maxodds > 9999 ) + return eval->Invalid("maxodds too big"); fundingPubKey = fundingTx.vout[1].scriptPubKey; switch ( funcid ) { @@ -838,9 +869,9 @@ 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; 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 ) + if ( amount < 0 || maxodds < 1 || maxodds > 9999 ) { - fprintf(stderr,"negative parameter error\n"); + fprintf(stderr,"negative parameter or invalid maxodds error (limit 9999)\n"); return(""); } if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) @@ -874,9 +905,9 @@ 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; 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 ) + if ( bet < 0 || odds < 1 || odds > 9999 ) { - fprintf(stderr,"negative parameter error\n"); + fprintf(stderr,"negative parameter or odds too big error\n"); return(""); } if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6a8ef310b..20ece62d2 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -755,6 +755,7 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtim if ( height > 1 && checktoshis == 0 ) { checktoshis = ((uint64_t)GetBlockSubsidy(height, Params().GetConsensus()) - block.vtx[0].vout[0].nValue); + //checktoshis += txn_count * 0.001; // rely on higher level validations to prevent emitting more coins than actual txfees } if ( height >= 2 && (overflow != 0 || total > checktoshis || strangeout != 0) ) { From cd98443d65ebc2d09a17b8a6ad0dc95e61b7082a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 23:06:59 -1100 Subject: [PATCH 14/21] Syntax --- 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 2616bd536..0d7e168c3 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -232,7 +232,7 @@ int32_t dice_5nibbles(uint8_t *fivevals) 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],_hash[32],hash[32],hash16[64]; uint64_t winnings; arith_uint256 hash,house,bettor; char str[65],str2[65]; int32_t i,modval; + uint8_t buf[64],_house[32],_bettor[32],_hash[32],hash[32],hash16[64]; uint64_t winnings; arith_uint256 house,bettor; char str[65],str2[65]; int32_t i,modval; if ( odds < 10000 ) return(0); else odds -= 10000; @@ -275,7 +275,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t break; } } - fprintf(stderr,"modval %d vs %d\n",modval,10000/(odds+1)) + fprintf(stderr,"modval %d vs %d\n",modval,10000/(odds+1)); if ( modval < 10000/(odds+1) ) winnings = bet * (odds+1); } From 76b729d24639c0cd6c9ff26d023a5801053433a6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 23:08:11 -1100 Subject: [PATCH 15/21] Fix --- src/cc/dice.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 0d7e168c3..e8d0678ce 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -255,6 +255,8 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t winnings = 0; if ( odds > 1 ) { + if ( odds > 9999 ) // shouldnt happen + return(0); //bettor = (bettor / arith_uint256(odds)); endiancpy(buf,(uint8_t *)&house,32); endiancpy(&buf[32],(uint8_t *)&bettor,32); @@ -275,7 +277,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t break; } } - fprintf(stderr,"modval %d vs %d\n",modval,10000/(odds+1)); + fprintf(stderr,"modval %d vs %d\n",modval,(int32_t)(10000/(odds+1))); if ( modval < 10000/(odds+1) ) winnings = bet * (odds+1); } From d2b767f71df0310161439e8ec4ebc11e33891021 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 19 Aug 2018 23:10:22 -1100 Subject: [PATCH 16/21] Test --- src/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 5c2e02e54..51e53a742 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1535,7 +1535,6 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa { if ( flag != 0 ) KOMODO_CONNECTING = -1; - fprintf(stderr,"accept failure.10\n"); return error("AcceptToMemoryPool: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s", hash.ToString()); } if ( flag != 0 ) From bc3419f06d1ef275149940031b900f2c54d9c2be Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 20 Aug 2018 02:26:42 -1100 Subject: [PATCH 17/21] Revert --- 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 e8d0678ce..263b644ad 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -255,9 +255,14 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t winnings = 0; if ( odds > 1 ) { + { // old way + bettor = (bettor / arith_uint256(odds)); + if ( bettor >= house ) + winnings = bet * (odds+1); + return(winnings); + } if ( odds > 9999 ) // shouldnt happen return(0); - //bettor = (bettor / arith_uint256(odds)); endiancpy(buf,(uint8_t *)&house,32); endiancpy(&buf[32],(uint8_t *)&bettor,32); vcalc_sha256(0,(uint8_t *)&_hash,buf,64); From d930ed4f2e17faf5404051738e053c7bb3fdfc13 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 20 Aug 2018 02:27:11 -1100 Subject: [PATCH 18/21] Unrevert --- src/cc/dice.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 263b644ad..b3115ab8f 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -255,6 +255,7 @@ uint64_t DiceCalc(int64_t bet,int64_t odds,int64_t minbet,int64_t maxbet,int64_t winnings = 0; if ( odds > 1 ) { + if ( 0 ) { // old way bettor = (bettor / arith_uint256(odds)); if ( bettor >= house ) From 33f4e4925cdf9d048fc92c4748a94552af940dac Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 20 Aug 2018 08:32:00 -1100 Subject: [PATCH 19/21] Fix maxodds check --- src/cc/dice.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index b3115ab8f..03529b0ed 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -855,7 +855,7 @@ 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; 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 ) + if ( funds < 0 || minbet < 0 || maxbet < 0 || maxodds < 1 || maxodds > 9999 || timeoutblocks < 0 || timeoutblocks > 1440 ) { fprintf(stderr,"negative parameter error\n"); return(""); @@ -877,13 +877,18 @@ 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; 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 || maxodds < 1 || maxodds > 9999 ) + if ( amount < 0 || maxodds < 1 ) { - fprintf(stderr,"negative parameter or invalid maxodds error (limit 9999)\n"); + fprintf(stderr,"negative parameter\n"); return(""); } if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(""); + if ( maxodds > 9999 ) + { + fprintf(stderr,"maxodds.%llu error (limit 9999)\n",(long long)maxodds); + return(""); + } scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( 0 ) { From e222a19d4b9bb42a3cd0df7875b69c5fd968e003 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 20 Aug 2018 08:46:12 -1100 Subject: [PATCH 20/21] Fix maxodds check --- src/cc/dice.cpp | 7 +------ src/main.cpp | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index 03529b0ed..8da052c86 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -877,18 +877,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; 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 || maxodds < 1 ) + if ( amount < 0 ) { fprintf(stderr,"negative parameter\n"); return(""); } if ( (cp= Diceinit(fundingPubKey,fundingtxid,&C,planstr,txfee,mypk,dicepk,sbits,minbet,maxbet,maxodds,timeoutblocks)) == 0 ) return(""); - if ( maxodds > 9999 ) - { - fprintf(stderr,"maxodds.%llu error (limit 9999)\n",(long long)maxodds); - return(""); - } scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG; if ( 0 ) { diff --git a/src/main.cpp b/src/main.cpp index 51e53a742..47d5fb017 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4185,7 +4185,7 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C } if ( rejects == 0 || rejects == lastrejects ) break; - fprintf(stderr,"addtomempool for CC checking: n.%d rejects.%d last.%d\n",(int32_t)block.vtx.size(),rejects,lastrejects); + fprintf(stderr,"addtomempool ht.%d for CC checking: n.%d rejects.%d last.%d\n",height,(int32_t)block.vtx.size(),rejects,lastrejects); lastrejects = rejects; rejects = 0; } From 38b5d53f015a320b23853afc1d1f378495b20b36 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 20 Aug 2018 21:41:20 -1100 Subject: [PATCH 21/21] +print --- src/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 47d5fb017..548ce2011 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4184,7 +4184,11 @@ bool CheckBlock(int32_t *futureblockp,int32_t height,CBlockIndex *pindex,const C rejects++; } if ( rejects == 0 || rejects == lastrejects ) + { + if ( lastrejects != 0 ) + fprintf(stderr,"lastrejects.%d -> all tx in mempool\n",lastrejects); break; + } fprintf(stderr,"addtomempool ht.%d for CC checking: n.%d rejects.%d last.%d\n",height,(int32_t)block.vtx.size(),rejects,lastrejects); lastrejects = rejects; rejects = 0;