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

This commit is contained in:
blackjok3r
2019-02-18 19:09:54 +08:00
130 changed files with 45226 additions and 1915 deletions

View File

@@ -381,10 +381,11 @@ UniValue getdifficulty(const UniValue& params, bool fHelp)
return GetNetworkDifficulty();
}
bool myIsutxo_spentinmempool(uint256 txid,int32_t vout)
bool myIsutxo_spentinmempool(uint256 &spenttxid,int32_t &spentvini,uint256 txid,int32_t vout)
{
//char *uint256_str(char *str,uint256); char str[65];
//LOCK(mempool.cs);
int32_t vini = 0;
BOOST_FOREACH(const CTxMemPoolEntry &e,mempool.mapTx)
{
const CTransaction &tx = e.GetTx();
@@ -393,7 +394,12 @@ bool myIsutxo_spentinmempool(uint256 txid,int32_t vout)
{
//fprintf(stderr,"%s/v%d ",uint256_str(str,txin.prevout.hash),txin.prevout.n);
if ( txin.prevout.n == vout && txin.prevout.hash == txid )
{
spenttxid = hash;
spentvini = vini;
return(true);
}
vini++;
}
//fprintf(stderr,"are vins for %s\n",uint256_str(str,hash));
}

View File

@@ -34,21 +34,27 @@
#include "script/sign.h"
#include "script/standard.h"
#include "key_io.h"
#include <stdint.h>
#include <univalue.h>
#include <regex>
using namespace std;
extern std::string ASSETCHAINS_SELFIMPORT;
extern uint16_t ASSETCHAINS_CODAPORT, ASSETCHAINS_BEAMPORT;
int32_t komodo_MoM(int32_t *notarized_htp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight,uint256 *MoMoMp,int32_t *MoMoMoffsetp,int32_t *MoMoMdepthp,int32_t *kmdstartip,int32_t *kmdendip);
int32_t komodo_MoMoMdata(char *hexstr,int32_t hexsize,struct komodo_ccdataMoMoM *mdata,char *symbol,int32_t kmdheight,int32_t notarized_height);
struct komodo_ccdata_entry *komodo_allMoMs(int32_t *nump,uint256 *MoMoMp,int32_t kmdstarti,int32_t kmdendi);
uint256 komodo_calcMoM(int32_t height,int32_t MoMdepth);
extern std::string ASSETCHAINS_SELFIMPORT;
uint256 Parseuint256(char *hexstr);
int32_t GetSelfimportProof(std::string source,CMutableTransaction &mtx,CScript &scriptPubKey,TxProof &proof,uint64_t burnAmount,std::vector<uint8_t> rawtx,uint256 txid,std::vector<uint8_t> rawproof);
std::string MakeSelfImportSourceTx(CTxDestination &dest, int64_t amount, CMutableTransaction &mtx);
int32_t GetSelfimportProof(std::string source, CMutableTransaction &mtx, CScript &scriptPubKey, TxProof &proof, std::string rawsourcetx, int32_t &ivout, uint256 sourcetxid, uint64_t burnAmount);
std::string MakeGatewaysImportTx(uint64_t txfee, uint256 bindtxid, int32_t height, std::string refcoin, std::vector<uint8_t>proof, std::string rawburntx, int32_t ivout, uint256 burntxid);
UniValue assetchainproof(const UniValue& params, bool fHelp)
{
@@ -256,6 +262,7 @@ UniValue migrate_createimporttransaction(const UniValue& params, bool fHelp)
TxProof proof = GetAssetchainProof(burnTx.GetHash(),burnTx);
CTransaction importTx = MakeImportCoinTransaction(proof, burnTx, payouts);
return HexStr(E_MARSHAL(ss << importTx));
}
@@ -281,31 +288,122 @@ UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp)
UniValue selfimport(const UniValue& params, bool fHelp)
{
CMutableTransaction mtx;
std::string source; TxProof proof; CTransaction burnTx,tx; CTxOut burnOut; uint64_t burnAmount; uint256 txid,blockHash; std::vector<CTxOut> vouts; std::vector<uint8_t> rawtx,rawproof; CScript scriptPubKey;
UniValue result(UniValue::VOBJ);
CMutableTransaction sourceMtx, templateMtx;
std::string destaddr;
std::string source;
std::string rawsourcetx;
CTransaction burnTx;
CTxOut burnOut;
uint64_t burnAmount;
uint256 sourcetxid, blockHash;
std::vector<CTxOut> vouts;
std::vector<uint8_t> rawproof, rawproofEmpty;
int32_t ivout = 0;
CScript scriptPubKey;
TxProof proof;
if ( ASSETCHAINS_SELFIMPORT.size() == 0 )
throw runtime_error("selfimport only works on -ac_import chains");
if (fHelp || params.size() < 3 || params.size() > 5 )
throw runtime_error("selfimport rawtx txid burnamount [rawproof source]\n\n"
"creates signed selfimport transaction");
rawtx = ParseHex(params[0].get_str().c_str());
txid = Parseuint256((char *)params[1].get_str().c_str()); // allow for txid != hash(rawtx)
burnAmount = atof(params[2].get_str().c_str()) * COIN + 0.00000000499999;
source = ASSETCHAINS_SELFIMPORT;
if ( params.size() >= 4 )
if (fHelp || params.size() != 2)
throw runtime_error("selfimport destaddr amount\n"
//old: "selfimport rawsourcetx sourcetxid {nvout|\"find\"} amount \n"
//TODO: "or selfimport rawburntx burntxid {nvout|\"find\"} rawproof source bindtxid height} \n"
"\ncreates self import coin transaction");
/* OLD selfimport schema:
rawsourcetx = params[0].get_str();
sourcetxid = Parseuint256((char *)params[1].get_str().c_str()); // allow for txid != hash(rawtx)
int32_t ivout = -1;
if( params[2].get_str() != "find" ) {
if( !std::all_of(params[2].get_str().begin(), params[2].get_str().end(), ::isdigit) ) // check if not all chars are digit
throw std::runtime_error("incorrect nvout param");
ivout = atoi(params[2].get_str().c_str());
}
burnAmount = atof(params[3].get_str().c_str()) * COIN + 0.00000000499999; */
destaddr = params[0].get_str();
burnAmount = atof(params[1].get_str().c_str()) * COIN + 0.00000000499999;
source = ASSETCHAINS_SELFIMPORT; //defaults to -ac_import=... param
/* TODO for gateways:
if ( params.size() >= 5 )
{
rawproof = ParseHex(params[3].get_str().c_str());
if ( params.size() == 5 )
source = params[4].get_str();
rawproof = ParseHex(params[4].get_str().c_str());
if ( params.size() == 6 )
source = params[5].get_str();
} */
if (source == "BEAM")
{
if (ASSETCHAINS_BEAMPORT == 0)
return(-1);
// confirm via ASSETCHAINS_BEAMPORT that burnTx/hash is a valid BEAM burn
// return(0);
return -1;
}
if ( GetSelfimportProof(source,mtx,scriptPubKey,proof,burnAmount,rawtx,txid,rawproof) < 0 )
throw std::runtime_error("Failed validating selfimport");
vouts = mtx.vout;
burnOut = MakeBurnOutput(burnAmount,0xffffffff,ASSETCHAINS_SELFIMPORT,vouts,rawproof);
mtx.vout.clear();
mtx.vout.push_back(burnOut);
burnTx = mtx;
return HexStr(E_MARSHAL(ss << MakeImportCoinTransaction(proof,burnTx,vouts)));
else if (source == "CODA")
{
if (ASSETCHAINS_CODAPORT == 0)
return(-1);
// confirm via ASSETCHAINS_CODAPORT that burnTx/hash is a valid CODA burn
// return(0);
return -1;
}
else if (source == "PUBKEY")
{
CTxDestination dest = DecodeDestination(destaddr.c_str());
rawsourcetx = MakeSelfImportSourceTx(dest, burnAmount, sourceMtx);
sourcetxid = sourceMtx.GetHash();
// prepare self-import 'quasi-burn' tx and also create vout for import tx (in mtx.vout):
if (GetSelfimportProof(source, templateMtx, scriptPubKey, proof, rawsourcetx, ivout, sourcetxid, burnAmount) < 0)
throw std::runtime_error("Failed validating selfimport");
vouts = templateMtx.vout;
burnOut = MakeBurnOutput(burnAmount, 0xffffffff, ASSETCHAINS_SELFIMPORT, vouts, rawproofEmpty);
templateMtx.vout.clear();
templateMtx.vout.push_back(burnOut); // burn tx has only opret with vouts and optional proof
burnTx = templateMtx; // complete the creation of 'quasi-burn' tx
std::string hextx = HexStr(E_MARSHAL(ss << MakeImportCoinTransaction(proof, burnTx, vouts)));
CTxDestination address;
bool fValidAddress = ExtractDestination(scriptPubKey, address);
result.push_back(Pair("sourceTxHex", rawsourcetx));
result.push_back(Pair("importTxHex", hextx));
result.push_back(Pair("UsedRawtxVout", ivout)); // notify user about the used vout of rawtx
result.push_back(Pair("DestinationAddress", EncodeDestination(address))); // notify user about the address where the funds will be sent
return result;
}
else if (source == ASSETCHAINS_SELFIMPORT)
{
throw std::runtime_error("not implemented yet\n");
if (params.size() != 8)
throw runtime_error("use \'selfimport rawburntx burntxid nvout rawproof source bindtxid height\' to import from a coin chain\n");
uint256 bindtxid = Parseuint256((char *)params[6].get_str().c_str());
int32_t height = atoi((char *)params[7].get_str().c_str());
// source is external coin is the assetchains symbol in the burnTx OP_RETURN
// burnAmount, rawtx and rawproof should be enough for gatewaysdeposit equivalent
std::string hextx = MakeGatewaysImportTx(0, bindtxid, height, source, rawproof, rawsourcetx, ivout, sourcetxid);
result.push_back(Pair("hex", hextx));
result.push_back(Pair("UsedRawtxVout", ivout)); // notify user about the used vout of rawtx
}
return result;
}
UniValue getNotarisationsForBlock(const UniValue& params, bool fHelp)

View File

@@ -30,6 +30,7 @@
#include "util.h"
#include "notaries_staked.h"
#include "cc/eval.h"
#include "cc/CCinclude.h"
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
#include "wallet/walletdb.h"
@@ -1426,17 +1427,17 @@ UniValue txnotarizedconfirmed(const UniValue& params, bool fHelp)
UniValue decodeccopret(const UniValue& params, bool fHelp)
{
CTransaction tx; uint256 txid,hashBlock;
std::vector<uint8_t> vopret; uint8_t *script;
UniValue result(UniValue::VOBJ);
CTransaction tx; uint256 tokenid,txid,hashblock;
std::vector<uint8_t> vopret,vOpretExtra; uint8_t *script,tokenevalcode;
UniValue result(UniValue::VOBJ),array(UniValue::VARR); std::vector<CPubKey> pubkeys;
if (fHelp || params.size() < 1 || params.size() > 1)
{
string msg = "decodeccopret hex\n"
string msg = "decodeccopret scriptPubKey\n"
"\nReturns eval code and function id for CC OP RETURN data.\n"
"\nArguments:\n"
"1. txid (string, required) Transaction id.\n"
"1. scriptPubKey (string, required) Hex of scriptPubKey with OP_RETURN data.\n"
"\nResult:\n"
"{\n"
@@ -1446,21 +1447,38 @@ UniValue decodeccopret(const UniValue& params, bool fHelp)
;
throw runtime_error(msg);
}
txid = uint256S((char *)params[0].get_str().c_str());
std::vector<unsigned char> hex(ParseHex(params[0].get_str()));
CScript scripthex(hex.begin(),hex.end());
if (DecodeTokenOpRet(scripthex,tokenevalcode,tokenid,pubkeys,vOpretExtra)!=0 && tokenevalcode==EVAL_TOKENS && vOpretExtra.size()>0)
{
LOCK(cs_main);
if (!GetTransaction(txid, tx, hashBlock, true))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
UniValue obj(UniValue::VOBJ);
GetOpReturnData(scripthex,vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 1)
{
char func[5];
sprintf(func,"%c",script[1]);
obj.push_back(Pair("eval_code", EvalToStr(script[0])));
obj.push_back(Pair("function", func));
}
else
{
obj.push_back(Pair("error", "invalid or no CC opret data for Token OP_RETURN"));
}
array.push_back(obj);
if (!E_UNMARSHAL(vOpretExtra, { ss >> vopret; })) return (0);
}
GetOpReturnData(tx.vout[tx.vout.size()-1].scriptPubKey,vopret);
else GetOpReturnData(scripthex,vopret);
script = (uint8_t *)vopret.data();
if ( vopret.size() > 1)
{
char func[5];
sprintf(func,"%c",script[1]);
char func[5]; UniValue obj(UniValue::VOBJ);
result.push_back(Pair("result", "success"));
result.push_back(Pair("eval_code", EvalToStr(script[0])));
result.push_back(Pair("function", func));
sprintf(func,"%c",script[1]);
obj.push_back(Pair("eval_code", EvalToStr(script[0])));
obj.push_back(Pair("function", func));
array.push_back(obj);
result.push_back(Pair("OpRets",array));
}
else
{

View File

@@ -473,6 +473,8 @@ static const CRPCCommand vRPCCommands[] =
// Gateways
{ "gateways", "gatewaysaddress", &gatewaysaddress, true },
{ "gateways", "gatewayslist", &gatewayslist, true },
{ "gateways", "gatewaysexternaladdress", &gatewaysexternaladdress, true },
{ "gateways", "gatewaysdumpprivkey", &gatewaysdumpprivkey, true },
{ "gateways", "gatewaysinfo", &gatewaysinfo, true },
{ "gateways", "gatewaysbind", &gatewaysbind, true },
{ "gateways", "gatewaysdeposit", &gatewaysdeposit, true },
@@ -481,9 +483,9 @@ static const CRPCCommand vRPCCommands[] =
{ "gateways", "gatewayspartialsign", &gatewayspartialsign, true },
{ "gateways", "gatewayscompletesigning", &gatewayscompletesigning, true },
{ "gateways", "gatewaysmarkdone", &gatewaysmarkdone, true },
{ "gateways", "gatewayspending", &gatewayspending, true },
{ "gateways", "gatewayspendingdeposits", &gatewayspendingdeposits, true },
{ "gateways", "gatewayspendingwithdraws", &gatewayspendingwithdraws, true },
{ "gateways", "gatewaysprocessed", &gatewaysprocessed, true },
{ "gateways", "gatewaysmultisig", &gatewaysmultisig, true },
// dice
{ "dice", "dicelist", &dicelist, true },
@@ -500,6 +502,7 @@ static const CRPCCommand vRPCCommands[] =
{ "tokens", "tokeninfo", &tokeninfo, true },
{ "tokens", "tokenlist", &tokenlist, true },
{ "tokens", "tokenorders", &tokenorders, true },
{ "tokens", "mytokenorders", &mytokenorders, true },
{ "tokens", "tokenaddress", &tokenaddress, true },
{ "tokens", "tokenbalance", &tokenbalance, true },
{ "tokens", "tokencreate", &tokencreate, true },
@@ -541,7 +544,10 @@ static const CRPCCommand vRPCCommands[] =
/* Not shown in help */
{ "hidden", "setmocktime", &setmocktime, true },
{ "hidden", "test_ac", &test_ac, true },
{ "hidden", "test_heirmarker", &test_heirmarker, true },
{ "hidden", "test_heirmarker", &test_heirmarker, true },
{ "hidden", "test_proof", &test_proof, true },
{ "hidden", "test_burntx", &test_burntx, true },
#ifdef ENABLE_WALLET
/* Wallet */

View File

@@ -241,6 +241,7 @@ extern UniValue coinsupply(const UniValue& params, bool fHelp);
extern UniValue tokeninfo(const UniValue& params, bool fHelp);
extern UniValue tokenlist(const UniValue& params, bool fHelp);
extern UniValue tokenorders(const UniValue& params, bool fHelp);
extern UniValue mytokenorders(const UniValue& params, bool fHelp);
extern UniValue tokenbalance(const UniValue& params, bool fHelp);
extern UniValue assetsaddress(const UniValue& params, bool fHelp);
extern UniValue tokenaddress(const UniValue& params, bool fHelp);
@@ -293,6 +294,8 @@ extern UniValue cclib(const UniValue& params, bool fHelp);
extern UniValue gatewaysaddress(const UniValue& params, bool fHelp);
extern UniValue gatewayslist(const UniValue& params, bool fHelp);
extern UniValue gatewaysinfo(const UniValue& params, bool fHelp);
extern UniValue gatewaysdumpprivkey(const UniValue& params, bool fHelp);
extern UniValue gatewaysexternaladdress(const UniValue& params, bool fHelp);
extern UniValue gatewaysbind(const UniValue& params, bool fHelp);
extern UniValue gatewaysdeposit(const UniValue& params, bool fHelp);
extern UniValue gatewaysclaim(const UniValue& params, bool fHelp);
@@ -300,9 +303,9 @@ extern UniValue gatewayswithdraw(const UniValue& params, bool fHelp);
extern UniValue gatewayspartialsign(const UniValue& params, bool fHelp);
extern UniValue gatewayscompletesigning(const UniValue& params, bool fHelp);
extern UniValue gatewaysmarkdone(const UniValue& params, bool fHelp);
extern UniValue gatewayspending(const UniValue& params, bool fHelp);
extern UniValue gatewayspendingdeposits(const UniValue& params, bool fHelp);
extern UniValue gatewayspendingwithdraws(const UniValue& params, bool fHelp);
extern UniValue gatewaysprocessed(const UniValue& params, bool fHelp);
extern UniValue gatewaysmultisig(const UniValue& params, bool fHelp);
extern UniValue channelslist(const UniValue& params, bool fHelp);
extern UniValue channelsinfo(const UniValue& params, bool fHelp);
extern UniValue channelsopen(const UniValue& params, bool fHelp);
@@ -478,5 +481,7 @@ extern UniValue paxwithdraw(const UniValue& params, bool fHelp);
// test rpc:
extern UniValue test_ac(const UniValue& params, bool fHelp);
extern UniValue test_heirmarker(const UniValue& params, bool fHelp);
extern UniValue test_burntx(const UniValue& params, bool fHelp);
extern UniValue test_proof(const UniValue& params, bool fHelp);
#endif // BITCOIN_RPCSERVER_H