From 093487f6f0242a850f955493e6d75f839bf89f58 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 04:09:51 -1100 Subject: [PATCH 1/7] Fix not enough rewards case --- src/cc/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index efb47fdb3..3674ac42b 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -631,7 +631,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 { if ( reward > txfee ) { - if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) > 0 ) + if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) >= reward+txfee ) { if ( inputs >= (reward + 2*txfee) ) CCchange = (inputs - (reward + txfee)); From 8b10fb48e0fd8b03ce35dcbb1983a7f59d573c3e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 04:32:42 -1100 Subject: [PATCH 2/7] Handle partial but not enough rewards funding --- src/cc/rewards.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 3674ac42b..696867596 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -262,9 +262,9 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t else return(true); } 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"); + return eval->Invalid("unlock tx vout.0 is normal output"); + else if ( numvouts != 3 ) + return eval->Invalid("unlock tx wrong number of 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 ) @@ -277,6 +277,8 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return false; else if ( tx.vout[1].nValue > amount+reward ) return eval->Invalid("unlock tx vout.1 isnt amount+reward"); + else if ( tx.vout[2].nValue > 0 ) + return eval->Invalid("unlock tx vout.2 isnt 0"); preventCCvouts = 1; break; } @@ -587,7 +589,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret,scriptPubKey,ignore; uint256 hashBlock; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; int64_t funding,reward=0,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx,firstmtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret,scriptPubKey,ignore; uint256 hashBlock; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; int64_t funding,reward=0,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; @@ -631,6 +633,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 { if ( reward > txfee ) { + firstmtx = mtx; if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) >= reward+txfee ) { if ( inputs >= (reward + 2*txfee) ) @@ -642,10 +645,10 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 } else { - mtx.vout.push_back(CTxOut(amount-txfee,scriptPubKey)); + firstmtx.vout.push_back(CTxOut(amount-txfee,scriptPubKey)); //CCerror = "cant find enough rewards inputs"; fprintf(stderr,"not enough rewards funds to payout %.8f, recover mode tx\n",(double)(reward+txfee)/COIN); - return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,firstmtx,mypk,txfee,opret)); } } else From de70190c2a310584fabeec4136e418c13c363265 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 04:40:23 -1100 Subject: [PATCH 3/7] Add opret --- src/cc/rewards.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index 696867596..e34b8908f 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -589,7 +589,7 @@ std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 locktxid) { - CMutableTransaction mtx,firstmtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript opret,scriptPubKey,ignore; uint256 hashBlock; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; int64_t funding,reward=0,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; + CMutableTransaction mtx,firstmtx; CTransaction tx; char coinaddr[64]; CPubKey mypk,rewardspk; CScript scriptPubKey,ignore; uint256 hashBlock; uint64_t sbits,APR,minseconds,maxseconds,mindeposit; int64_t funding,reward=0,amount=0,inputs,CCchange=0; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_REWARDS); if ( txfee == 0 ) txfee = 10000; @@ -648,7 +648,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2 firstmtx.vout.push_back(CTxOut(amount-txfee,scriptPubKey)); //CCerror = "cant find enough rewards inputs"; fprintf(stderr,"not enough rewards funds to payout %.8f, recover mode tx\n",(double)(reward+txfee)/COIN); - return(FinalizeCCTx(-1LL,cp,firstmtx,mypk,txfee,opret)); + return(FinalizeCCTx(-1LL,cp,firstmtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); } } else From eb69e5cee62fe65e3dd32996f6a0c554d8e4fea2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 04:54:03 -1100 Subject: [PATCH 4/7] Windows rag --- src/cryptoconditions/src/secp256k1.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cryptoconditions/src/secp256k1.c b/src/cryptoconditions/src/secp256k1.c index c1eb82765..6b616705b 100644 --- a/src/cryptoconditions/src/secp256k1.c +++ b/src/cryptoconditions/src/secp256k1.c @@ -46,6 +46,12 @@ void lockSign() { fclose(fp); #endif if (read != 32) { +#ifdef _WIN32 + int32_t i; + for (i=0; i<32; i++) + ((uint8_t *)ent)[i] = rand(); + fprintf(stderr,"@libscott plz make this a high entropy for windows\n"); +#endif fprintf(stderr, "Could not read 32 bytes entropy from system\n"); exit(1); } From 9f545d8eb3febdbdb5595415f2a82635ca4fd301 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 04:55:02 -1100 Subject: [PATCH 5/7] Fix --- src/cryptoconditions/src/secp256k1.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cryptoconditions/src/secp256k1.c b/src/cryptoconditions/src/secp256k1.c index 6b616705b..b3e198945 100644 --- a/src/cryptoconditions/src/secp256k1.c +++ b/src/cryptoconditions/src/secp256k1.c @@ -51,9 +51,10 @@ void lockSign() { for (i=0; i<32; i++) ((uint8_t *)ent)[i] = rand(); fprintf(stderr,"@libscott plz make this a high entropy for windows\n"); -#endif +#else fprintf(stderr, "Could not read 32 bytes entropy from system\n"); exit(1); +#endif } if (!secp256k1_context_randomize(ec_ctx_sign, ent)) { fprintf(stderr, "Could not randomize secp256k1 context\n"); From 96e1505ec8737bb501b946af2d3cc7648e4a4a38 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 05:00:51 -1100 Subject: [PATCH 6/7] Allow weaker rand() for in memory values --- src/cryptoconditions/src/secp256k1.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/cryptoconditions/src/secp256k1.c b/src/cryptoconditions/src/secp256k1.c index b3e198945..a95895fa4 100644 --- a/src/cryptoconditions/src/secp256k1.c +++ b/src/cryptoconditions/src/secp256k1.c @@ -45,16 +45,11 @@ void lockSign() { int read = (int) fread(&ent, 1, 32, fp); fclose(fp); #endif - if (read != 32) { -#ifdef _WIN32 + if (read != 32) + { int32_t i; for (i=0; i<32; i++) ((uint8_t *)ent)[i] = rand(); - fprintf(stderr,"@libscott plz make this a high entropy for windows\n"); -#else - fprintf(stderr, "Could not read 32 bytes entropy from system\n"); - exit(1); -#endif } if (!secp256k1_context_randomize(ec_ctx_sign, ent)) { fprintf(stderr, "Could not randomize secp256k1 context\n"); From 799f664f5f2a6b68d9a4ced0a3bfdc27a6b6240a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 18 Aug 2018 05:05:47 -1100 Subject: [PATCH 7/7] Fix --- src/cc/rewards.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/rewards.cpp b/src/cc/rewards.cpp index e34b8908f..61bcf5ca3 100644 --- a/src/cc/rewards.cpp +++ b/src/cc/rewards.cpp @@ -251,7 +251,7 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t if ( (*cp->ismyvin)(tx.vin[i].scriptSig) == 0 ) return eval->Invalid("unexpected normal vin for unlock"); } - if ( numvouts == 1 && numvins == 1 ) + if ( numvouts == 2 && numvins == 1 ) { if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 ) return eval->Invalid("unlock recover tx vout.0 is not normal output"); @@ -259,6 +259,8 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t return eval->Invalid("unlock recover tx vout.0 mismatched scriptPubKey"); else if ( tx.vout[0].nValue > vinTx.vout[0].nValue ) return eval->Invalid("unlock recover tx vout.0 mismatched amounts"); + else if ( tx.vout[1].nValue > 0 ) + return eval->Invalid("unlock recover tx vout.1 nonz amount"); else return(true); } if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 )