Merge branch 'FSM' of https://github.com/jl777/komodo into jl777-FSM

merge
This commit is contained in:
blackjok3r
2018-11-29 23:22:32 +08:00
566 changed files with 42751 additions and 10240 deletions

View File

@@ -245,9 +245,12 @@ bool mySenddicetransaction(std::string res,uint256 entropyused,int32_t entropyvo
{
RelayTransaction(tx);
fprintf(stderr,"rebroadcast.%c and clear [%d] and broadcast entropyused.%s bettxid.%s -> %s\n",funcid,i,entropyused.GetHex().c_str(),bettxid.GetHex().c_str(),tx.GetHash().GetHex().c_str());
if ( ptr->rawtx.empty() == 0 )
ptr->rawtx.clear();
ptr->txid = zeroid;
if ( ptr != 0 )
{
if ( ptr->rawtx.empty() == 0 )
ptr->rawtx.clear();
ptr->txid = zeroid;
}
//fprintf(stderr,"error adding funcid.%c E.%s bet.%s -> %s to mempool, probably Disable replacement feature size.%d\n",funcid,entropyused.GetHex().c_str(),bettxid.GetHex().c_str(),tx.GetHash().GetHex().c_str(),(int32_t)ptr->rawtx.size());
}
} else fprintf(stderr,"error duplicate entropyused different bettxid\n");
@@ -540,10 +543,9 @@ CPubKey DiceFundingPk(CScript scriptPubKey)
CPubKey pk; uint8_t *ptr,*dest; int32_t i;
if ( scriptPubKey.size() == 35 )
{
ptr = (uint8_t *)scriptPubKey.data();
dest = (uint8_t *)pk.begin();
for (i=0; i<33; i++)
dest[i] = ptr[i+1];
dest[i] = scriptPubKey[i+1];
} else fprintf(stderr,"DiceFundingPk invalid size.%d\n",(int32_t)scriptPubKey.size());
return(pk);
}
@@ -831,7 +833,7 @@ bool DiceVerifyTimeout(CTransaction &betTx,int32_t timeoutblocks)
return(numblocks >= timeoutblocks);
}
bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx, uint32_t nIn)
{
uint256 txid,fundingtxid,vinfundingtxid,vinhentropy,vinproof,hashBlock,hash,proof,entropy; int64_t minbet,maxbet,maxodds,timeoutblocks,odds,winnings; uint64_t vinsbits,refsbits=0,sbits,amount,inputs,outputs,txfee=10000; int32_t numvins,entropyvout,numvouts,preventCCvins,preventCCvouts,i,iswin; uint8_t funcid; CScript fundingPubKey; CTransaction fundingTx,vinTx,vinofvinTx; char CCaddr[64];
numvins = tx.vin.size();
@@ -917,8 +919,8 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
fprintf(stderr,"%s != %s betTx.%s\n",addr0,addr1,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[vinTx.vin[0].prevout.n].scriptPubKey.data();
ptr1 = (uint8_t *)fundingPubKey.data();
ptr0 = (uint8_t *)&vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey[0];
ptr1 = (uint8_t *)&fundingPubKey[0];
for (i=0; i<vinofvinTx.vout[vinTx.vin[0].prevout.n].scriptPubKey.size(); i++)
fprintf(stderr,"%02x",ptr0[i]);
fprintf(stderr," script vs ");
@@ -972,7 +974,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
return eval->Invalid("vout[0] != inputs-txfee for loss");
else if ( tx.vout[2].scriptPubKey != fundingPubKey )
{
if ( tx.vout[2].scriptPubKey.size() == 0 || ((uint8_t *)tx.vout[2].scriptPubKey.data())[0] != 0x6a )
if ( tx.vout[2].scriptPubKey.size() == 0 || tx.vout[2].scriptPubKey[0] != 0x6a )
return eval->Invalid("vout[2] not send to fundingPubKey for loss");
}
iswin = -1;
@@ -996,7 +998,7 @@ bool DiceValidate(struct CCcontract_info *cp,Eval *eval,const CTransaction &tx)
}
else if ( tx.vout[3].scriptPubKey != fundingPubKey )
{
if ( tx.vout[3].scriptPubKey.size() == 0 || ((uint8_t *)tx.vout[3].scriptPubKey.data())[0] != 0x6a )
if ( tx.vout[3].scriptPubKey.size() == 0 || tx.vout[3].scriptPubKey[0] != 0x6a )
return eval->Invalid("vout[3] not send to fundingPubKey for win/timeout");
}
iswin = (funcid == 'W');
@@ -1162,8 +1164,8 @@ int64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbit
Getscriptaddress(addr1,fundingPubKey);
if ( strcmp(addr0,addr1) != 0 )
{
ptr0 = (uint8_t *)vinTx.vout[1].scriptPubKey.data();
ptr1 = (uint8_t *)fundingPubKey.data();
ptr0 = (uint8_t *)&vinTx.vout[1].scriptPubKey[0];
ptr1 = (uint8_t *)&fundingPubKey[0];
for (i=0; i<vinTx.vout[1].scriptPubKey.size(); i++)
fprintf(stderr,"%02x",ptr0[i]);
fprintf(stderr," script vs ");
@@ -1187,13 +1189,12 @@ int64_t DicePlanFunds(uint64_t &entropyval,uint256 &entropytxid,uint64_t refsbit
else
{
uint8_t *ptr0,*ptr1; int32_t i; char str[65];
ptr0 = (uint8_t *)tx.vout[1].scriptPubKey.data();
ptr1 = (uint8_t *)fundingPubKey.data();
const CScript &s0 = tx.vout[1].scriptPubKey;
for (i=0; i<tx.vout[1].scriptPubKey.size(); i++)
fprintf(stderr,"%02x",ptr0[i]);
fprintf(stderr,"%02x",s0[i]);
fprintf(stderr," script vs ");
for (i=0; i<fundingPubKey.size(); i++)
fprintf(stderr,"%02x",ptr1[i]);
fprintf(stderr,"%02x",fundingPubKey[i]);
fprintf(stderr," (%c) tx vin.%d fundingPubKey mismatch %s\n",funcid,tx.vin[0].prevout.n,uint256_str(str,tx.vin[0].prevout.hash));
}
}
@@ -1334,7 +1335,8 @@ 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;
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
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 || maxodds > 9999 || timeoutblocks < 0 || timeoutblocks > 1440 )
{
CCerror = "invalid parameter error";
@@ -1368,7 +1370,8 @@ 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;
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
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 )
{
CCerror = "amount must be positive";
@@ -1383,13 +1386,11 @@ std::string DiceAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,int6
if ( 0 )
{
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,"%02x",scriptPubKey[i]);
fprintf(stderr," script vs ");
for (i=0; i<35; i++)
fprintf(stderr,"%02x",ptr1[i]);
fprintf(stderr,"%02x",fundingPubKey[i]);
fprintf(stderr," funding\n");
}
if ( scriptPubKey == fundingPubKey )
@@ -1413,7 +1414,8 @@ 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,entropyval2; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropytxid2,entropy,hentropy; struct CCcontract_info *cp,C;
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
CScript fundingPubKey; CPubKey mypk,dicepk; uint64_t sbits,entropyval,entropyval2; int64_t funding,minbet,maxbet,maxodds,timeoutblocks; uint256 entropytxid,entropytxid2,entropy,hentropy; struct CCcontract_info *cp,C;
if ( bet < 0 )
{
CCerror = "bet must be positive";
@@ -1472,7 +1474,8 @@ std::string DiceBet(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t bet
std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyvout,int32_t *resultp,uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettxid,int32_t winlosetimeout,uint256 vin0txid,int32_t vin0vout)
{
CMutableTransaction mtx; CScript scriptPubKey,fundingPubKey; CTransaction oldbetTx,betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy,oldbettxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs=0,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; int32_t oldentropyvout,retval=0,iswin=0; uint64_t entropyval,sbits;
CMutableTransaction savemtx,mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
CScript scriptPubKey,fundingPubKey; CTransaction oldbetTx,betTx,entropyTx; uint256 hentropyproof,entropytxid,hashBlock,bettorentropy,entropy,hentropy,oldbettxid; CPubKey mypk,dicepk,fundingpk; struct CCcontract_info *cp,C; int64_t inputs=0,CCchange=0,odds,fundsneeded,minbet,maxbet,maxodds,timeoutblocks; int32_t oldentropyvout,retval=0,iswin=0; uint64_t entropyval,sbits;
entropyused = zeroid;
*resultp = 0;
funcid = 0;
@@ -1483,9 +1486,9 @@ std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyv
return("");
}
fundingpk = DiceFundingPk(fundingPubKey);
scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG;
if ( winlosetimeout != 0 ) // must be dealernode
{
scriptPubKey = CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG;
if ( scriptPubKey != fundingPubKey )
{
//fprintf(stderr,"only dice fund creator can submit winner or loser\n");
@@ -1506,7 +1509,7 @@ std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyv
{
if ( vin0txid == zeroid || vin0vout < 0 )
{
if ( AddNormalinputs(mtx,mypk,2*txfee,1) == 0 ) // must be a single vin!!
if ( AddNormalinputs(mtx,mypk,2*txfee,3) == 0 ) // must be a single vin!!
{
CCerror = "no txfee inputs for win/lose";
fprintf(stderr,"%s\n", CCerror.c_str() );
@@ -1587,6 +1590,7 @@ std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyv
}
CCchange = betTx.vout[0].nValue + betTx.vout[1].nValue;
fundsneeded = txfee + (odds+1)*betTx.vout[1].nValue;
savemtx = mtx;
if ( CCchange >= fundsneeded )
CCchange -= fundsneeded;
else if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,1,sbits,fundingtxid)) >= fundsneeded )
@@ -1596,6 +1600,7 @@ std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyv
}
else
{
mtx = savemtx;
if ( (inputs= AddDiceInputs(cp,mtx,dicepk,fundsneeded,60,sbits,fundingtxid)) > 0 )
{
if ( inputs > fundsneeded )
@@ -1621,6 +1626,15 @@ std::string DiceBetFinish(uint8_t &funcid,uint256 &entropyused,int32_t &entropyv
//fprintf(stderr,"make tx.%c\n",funcid);
if ( funcid == 'L' || funcid == 'W' ) // dealernode only
hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash,mtx.vin[0].prevout.n,1);
else
{
if ( scriptPubKey != betTx.vout[2].scriptPubKey )
{
CCerror = strprintf("can only finish your own bettxid\n");
fprintf(stderr,"%s\n", CCerror.c_str() );
return("");
}
}
*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));
@@ -1715,6 +1729,12 @@ double DiceStatus(uint64_t txfee,char *planstr,uint256 fundingtxid,uint256 bettx
GetCCaddress(cp,coinaddr,dicepk);
if ( bettxid == zeroid ) // scan
{
if ( fundingpk != mypk )
{
CCerror = "Diceinit error in status, non-dealer must provide bettxid";
fprintf(stderr,"%s\n", CCerror.c_str() );
return(0.);
}
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
SetCCunspents(unspentOutputs,coinaddr);
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)