FSM skeleton
This commit is contained in:
@@ -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 \
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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; i<numvouts; i++)
|
||||
{
|
||||
//fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
|
||||
if ( (assetoshis= IsPonzivout(cp,tx,i)) != 0 )
|
||||
if ( (assetoshis= IsFSMvout(cp,tx,i)) != 0 )
|
||||
outputs += assetoshis;
|
||||
}
|
||||
if ( inputs != outputs+COIN+txfee )
|
||||
@@ -70,7 +70,7 @@ bool PonziExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction
|
||||
else return(true);
|
||||
}
|
||||
|
||||
bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
|
||||
bool FSMValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
|
||||
{
|
||||
int32_t numvins,numvouts,preventCCvins,preventCCvouts,i; bool retval;
|
||||
numvins = tx.vin.size();
|
||||
@@ -85,30 +85,30 @@ bool PonziValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
|
||||
{
|
||||
if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
|
||||
{
|
||||
fprintf(stderr,"ponziget invalid vini\n");
|
||||
fprintf(stderr,"fsmget invalid vini\n");
|
||||
return eval->Invalid("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<uint8_t> origpubkey; CTransaction vintx; int32_t n = 0;
|
||||
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > 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);
|
||||
@@ -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 },
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<unsigned char> 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;
|
||||
|
||||
Reference in New Issue
Block a user