From e0903308239eb7b0624a1f185b1a027dea1c42a7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 1 Jan 2019 23:34:31 -1100 Subject: [PATCH] remove mutually exclusive constraint on selfimport -ac_beam and -ac_coda cannot be active at the same time Clusters can do import coin normally even with self-import chains. -ac_import=KMD (any bitcoinprotocol coin) will make a side chain (not implemented yet) Theoretically -ac_beam or -ac_coda can work with side chain (not implemented yet other than chain params) --- src/cc/import.cpp | 58 ++++++++++++++++++++++++++---------------- src/crosschain.cpp | 6 ++--- src/importcoin.cpp | 40 ++++++++++++++++++++++------- src/importcoin.h | 2 +- src/komodo_utils.h | 23 +++++++++-------- src/rpc/crosschain.cpp | 21 +++++++++------ 6 files changed, 95 insertions(+), 55 deletions(-) diff --git a/src/cc/import.cpp b/src/cc/import.cpp index 3162de1bd..807b12050 100644 --- a/src/cc/import.cpp +++ b/src/cc/import.cpp @@ -33,17 +33,23 @@ extern std::string ASSETCHAINS_SELFIMPORT; extern uint16_t ASSETCHAINS_CODAPORT,ASSETCHAINS_BEAMPORT; extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; -int32_t GetSelfimportProof(CMutableTransaction &mtx,CScript &scriptPubKey,TxProof &proof,uint64_t burnAmount,std::vector rawtx,uint256 txid,std::vector rawproof) // find burnTx with hash from "other" daemon +int32_t GetSelfimportProof(std::string source,CMutableTransaction &mtx,CScript &scriptPubKey,TxProof &proof,uint64_t burnAmount,std::vector rawtx,uint256 txid,std::vector &rawproof) // find burnTx with hash from "other" daemon { MerkleBranch newBranch; CMutableTransaction tmpmtx; CTransaction tx,vintx; uint256 blockHash; char destaddr[64],pkaddr[64]; tmpmtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),komodo_nextheight()); - if ( ASSETCHAINS_SELFIMPORT == "BEAM" ) + if ( source == "BEAM" ) { + if ( ASSETCHAINS_BEAMPORT == 0 ) + return(-1); // confirm via ASSETCHAINS_BEAMPORT that burnTx/hash is a valid BEAM burn + // return(0); } - else if ( ASSETCHAINS_SELFIMPORT == "CODA" ) + else if ( source == "CODA" ) { + if ( ASSETCHAINS_CODAPORT == 0 ) + return(-1); // confirm via ASSETCHAINS_CODAPORT that burnTx/hash is a valid CODA burn + // return(0); } else { @@ -61,7 +67,7 @@ int32_t GetSelfimportProof(CMutableTransaction &mtx,CScript &scriptPubKey,TxProo mtx.vout[0].scriptPubKey = scriptPubKey; if ( tx.GetHash() != txid ) return(-1); - if ( ASSETCHAINS_SELFIMPORT == "PUBKEY" ) + if ( source == "PUBKEY" ) { // make sure vin0 is signed by ASSETCHAINS_OVERRIDE_PUBKEY33 if ( myGetTransaction(tx.vin[0].prevout.hash,vintx,blockHash) == 0 ) @@ -77,9 +83,9 @@ int32_t GetSelfimportProof(CMutableTransaction &mtx,CScript &scriptPubKey,TxProo fprintf(stderr,"mismatched vin0[%d] -> %s vs %s\n",tx.vin[0].prevout.n,destaddr,pkaddr); } } - else + else if ( source == ASSETCHAINS_SELFIMPORT ) { - // ASSETCHAINS_SELFIMPORT and external coin is the assetchains symbol in the burnTx OP_RETURN + // source is external coin is the assetchains symbol in the burnTx OP_RETURN // burnAmount, rawtx and rawproof should be enough for gatewaysdeposit equivalent } } @@ -88,25 +94,26 @@ int32_t GetSelfimportProof(CMutableTransaction &mtx,CScript &scriptPubKey,TxProo // use proof from the above functions to validate the import -int32_t CheckBEAMimport(TxProof proof,CTransaction burnTx,std::vector payouts) +int32_t CheckBEAMimport(TxProof proof,std::vector rawproof,CTransaction burnTx,std::vector payouts) { // check with dual-BEAM daemon via ASSETCHAINS_BEAMPORT for validity of burnTx return(-1); } -int32_t CheckCODAimport(TxProof proof,CTransaction burnTx,std::vector payouts) +int32_t CheckCODAimport(TxProof proof,std::vector rawproof,CTransaction burnTx,std::vector payouts) { // check with dual-CODA daemon via ASSETCHAINS_CODAPORT for validity of burnTx return(-1); } -int32_t CheckGATEWAYimport(std::string coin,TxProof proof,CTransaction burnTx,std::vector payouts) +int32_t CheckGATEWAYimport(TxProof proof,std::vector rawproof,CTransaction burnTx,std::vector payouts) { + // ASSETCHAINS_SELFIMPORT is coin // check for valid burn from external coin blockchain and if valid return(0); return(-1); } -int32_t CheckPUBKEYimport(TxProof proof,CTransaction burnTx,std::vector payouts) +int32_t CheckPUBKEYimport(TxProof proof,std::vector rawproof,CTransaction burnTx,std::vector payouts) { // if burnTx has ASSETCHAINS_PUBKEY vin, it is valid return(0); fprintf(stderr,"proof txid.%s\n",proof.first.GetHex().c_str()); @@ -117,19 +124,18 @@ int32_t CheckPUBKEYimport(TxProof proof,CTransaction burnTx,std::vector bool Eval::ImportCoin(const std::vector params,const CTransaction &importTx,unsigned int nIn) { TxProof proof; CTransaction burnTx; std::vector payouts; uint64_t txfee = 10000; - uint32_t targetCcid; std::string targetSymbol; uint256 payoutsHash; - if (importTx.vout.size() < 2) + uint32_t targetCcid; std::string targetSymbol; uint256 payoutsHash; std::vector rawproof; + if ( importTx.vout.size() < 2 ) return Invalid("too-few-vouts"); // params if (!UnmarshalImportTx(importTx, proof, burnTx, payouts)) return Invalid("invalid-params"); - // Control all aspects of this transaction // It should not be at all malleable if (MakeImportCoinTransaction(proof, burnTx, payouts).GetHash() != importTx.GetHash()) return Invalid("non-canonical"); // burn params - if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCcid, payoutsHash)) + if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCcid, payoutsHash,rawproof)) return Invalid("invalid-burn-tx"); // check burn amount { @@ -156,26 +162,34 @@ bool Eval::ImportCoin(const std::vector params,const CTransaction &impo if (!CheckMoMoM(proof.first, target)) return Invalid("momom-check-fail"); } - else if ( ASSETCHAINS_SELFIMPORT == targetSymbol ) // various selfchain imports + else { - if ( ASSETCHAINS_SELFIMPORT == "BEAM" ) + if ( targetSymbol == "BEAM" ) { - if ( CheckBEAMimport(proof,burnTx,payouts) < 0 ) + if ( ASSETCHAINS_BEAMPORT == 0 ) + return Invalid("BEAM-import-without-port"); + else if ( CheckBEAMimport(proof,rawproof,burnTx,payouts) < 0 ) return Invalid("BEAM-import-failure"); } - else if ( ASSETCHAINS_SELFIMPORT == "CODA" ) + else if ( targetSymbol == "CODA" ) { - if ( CheckCODAimport(proof,burnTx,payouts) < 0 ) + if ( ASSETCHAINS_CODAPORT == 0 ) + return Invalid("CODA-import-without-port"); + else if ( CheckCODAimport(proof,rawproof,burnTx,payouts) < 0 ) return Invalid("CODA-import-failure"); } - else if ( ASSETCHAINS_SELFIMPORT == "PUBKEY" ) + else if ( targetSymbol == "PUBKEY" ) { - if ( CheckPUBKEYimport(proof,burnTx,payouts) < 0 ) + if ( ASSETCHAINS_SELFIMPORT != "PUBKEY" ) + return Invalid("PUBKEY-import-when-notPUBKEY"); + else if ( CheckPUBKEYimport(proof,rawproof,burnTx,payouts) < 0 ) return Invalid("PUBKEY-import-failure"); } else { - if ( CheckGATEWAYimport(targetSymbol,proof,burnTx,payouts) < 0 ) + if ( targetSymbol != ASSETCHAINS_SELFIMPORT ) + return Invalid("invalid-gateway-import-coin"); + else if ( CheckGATEWAYimport(proof,rawproof,burnTx,payouts) < 0 ) return Invalid("GATEWAY-import-failure"); } } diff --git a/src/crosschain.cpp b/src/crosschain.cpp index 447f907cc..8b6cb12a4 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -214,16 +214,14 @@ cont: */ void CompleteImportTransaction(CTransaction &importTx) { - TxProof proof; - CTransaction burnTx; - std::vector payouts; + TxProof proof; CTransaction burnTx; std::vector payouts; std::vector rawproof; if (!UnmarshalImportTx(importTx, proof, burnTx, payouts)) throw std::runtime_error("Couldn't parse importTx"); std::string targetSymbol; uint32_t targetCCid; uint256 payoutsHash; - if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCCid, payoutsHash)) + if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCCid, payoutsHash, rawproof)) throw std::runtime_error("Couldn't parse burnTx"); proof = GetCrossChainProof(burnTx.GetHash(), targetSymbol.data(), targetCCid, proof); diff --git a/src/importcoin.cpp b/src/importcoin.cpp index c72aea210..e479757dd 100644 --- a/src/importcoin.cpp +++ b/src/importcoin.cpp @@ -38,11 +38,22 @@ CTransaction MakeImportCoinTransaction(const TxProof proof, const CTransaction b } -CTxOut MakeBurnOutput(CAmount value, uint32_t targetCCid, std::string targetSymbol, const std::vector payouts) +CTxOut MakeBurnOutput(CAmount value, uint32_t targetCCid, std::string targetSymbol, const std::vector payouts,std::vector rawproof) { - std::vector opret = E_MARSHAL(ss << VARINT(targetCCid); - ss << targetSymbol; - ss << SerializeHash(payouts)); + std::vector opret; + if ( targetCCid != 0xffffffff ) + { + opret = E_MARSHAL(ss << VARINT(targetCCid); + ss << targetSymbol; + ss << SerializeHash(payouts)); + } + else + { + opret = E_MARSHAL(ss << VARINT(targetCCid); + ss << targetSymbol; + ss << SerializeHash(payouts); + ss << rawproof); + } return CTxOut(value, CScript() << OP_RETURN << opret); } @@ -60,14 +71,25 @@ bool UnmarshalImportTx(const CTransaction &importTx, TxProof &proof, CTransactio } -bool UnmarshalBurnTx(const CTransaction &burnTx, std::string &targetSymbol, uint32_t *targetCCid, uint256 &payoutsHash) +bool UnmarshalBurnTx(const CTransaction &burnTx, std::string &targetSymbol, uint32_t *targetCCid, uint256 &payoutsHash,std::vector&rawproof) { - std::vector burnOpret; + std::vector burnOpret; uint32_t ccid = 0; if (burnTx.vout.size() == 0) return false; GetOpReturnData(burnTx.vout.back().scriptPubKey, burnOpret); - return E_UNMARSHAL(burnOpret, ss >> VARINT(*targetCCid); - ss >> targetSymbol; - ss >> payoutsHash); + E_UNMARSHAL(burnOpret, ss >> VARINT(ccid)); + if ( ccid != 0xffffffff ) + { + return E_UNMARSHAL(burnOpret, ss >> VARINT(*targetCCid); + ss >> targetSymbol; + ss >> payoutsHash); + } + else + { + return E_UNMARSHAL(burnOpret, ss >> VARINT(*targetCCid); + ss >> targetSymbol; + ss >> payoutsHash; + ss >> rawproof); + } } diff --git a/src/importcoin.h b/src/importcoin.h index 53611cac9..3053eaf72 100644 --- a/src/importcoin.h +++ b/src/importcoin.h @@ -30,7 +30,7 @@ CTransaction MakeImportCoinTransaction(const TxProof proof, CTxOut MakeBurnOutput(CAmount value, uint32_t targetCCid, std::string targetSymbol, const std::vector payouts); -bool UnmarshalBurnTx(const CTransaction &burnTx, std::string &targetSymbol, uint32_t *targetCCid, uint256 &payoutsHash); +bool UnmarshalBurnTx(const CTransaction &burnTx, std::string &targetSymbol, uint32_t *targetCCid, uint256 &payoutsHash,std::vector &rawproof); bool UnmarshalImportTx(const CTransaction &importTx, TxProof &proof, CTransaction &burnTx, std::vector &payouts); diff --git a/src/komodo_utils.h b/src/komodo_utils.h index a8823e5b3..2437b0bf6 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1775,36 +1775,33 @@ void komodo_args(char *argv0) ASSETCHAINS_SCRIPTPUB = GetArg("-ac_script",""); ASSETCHAINS_BEAMPORT = GetArg("-ac_beam",0); ASSETCHAINS_CODAPORT = GetArg("-ac_coda",0); - + if ( ASSETCHAINS_BEAMPORT != 0 && ASSETCHAINS_CODAPORT != 0 ) + { + fprintf(stderr,"can only have one of -ac_beam or -ac_coda\n"); + exit(0); + } ASSETCHAINS_SELFIMPORT = GetArg("-ac_import",""); // BEAM, CODA, PUBKEY, GATEWAY if ( ASSETCHAINS_SELFIMPORT == "PUBKEY" ) { if ( strlen(ASSETCHAINS_OVERRIDE_PUBKEY.c_str()) != 66 ) { fprintf(stderr,"invalid -ac_pubkey for -ac_import=PUBKEY\n"); - ASSETCHAINS_SELFIMPORT = ""; exit(0); } } else if ( ASSETCHAINS_SELFIMPORT == "BEAM" && ASSETCHAINS_BEAMPORT == 0 ) { fprintf(stderr,"missing -ac_beam for BEAM rpcport\n"); - ASSETCHAINS_SELFIMPORT = ""; exit(0); } else if ( ASSETCHAINS_SELFIMPORT == "CODA" && ASSETCHAINS_CODAPORT == 0 ) { fprintf(stderr,"missing -ac_coda for CODA rpcport\n"); - ASSETCHAINS_SELFIMPORT = ""; exit(0); } - if ( ASSETCHAINS_SELFIMPORT.size() > 0 && ASSETCHAINS_CC >= KOMODO_FIRSTFUNGIBLEID ) - { - fprintf(stderr,"selfimport chains cant be in a fungible cluster\n"); - exit(0); - } - //ASSETCHAINS_FOUNDERS_PERIOD = GetArg("-ac_period",0); + // else it can be gateway coin + if ( (ASSETCHAINS_STAKED= GetArg("-ac_staked",0)) > 100 ) ASSETCHAINS_STAKED = 100; @@ -1857,7 +1854,7 @@ void komodo_args(char *argv0) printf("ASSETCHAINS_FOUNDERS needs an ASETCHAINS_OVERRIDE_PUBKEY\n"); } } - if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 || ASSETCHAINS_REWARD[0] != 0 || ASSETCHAINS_HALVING[0] != 0 || ASSETCHAINS_DECAY[0] != 0 || ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_PUBLIC != 0 || ASSETCHAINS_PRIVATE != 0 || ASSETCHAINS_TXPOW != 0 || ASSETCHAINS_FOUNDERS != 0 || ASSETCHAINS_SCRIPTPUB.size() > 1 || ASSETCHAINS_SELFIMPORT.size() > 0 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || ASSETCHAINS_TIMELOCKGTE != _ASSETCHAINS_TIMELOCKOFF|| ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH || ASSETCHAINS_LWMAPOS != 0 || ASSETCHAINS_LASTERA > 0 ) + if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 || ASSETCHAINS_REWARD[0] != 0 || ASSETCHAINS_HALVING[0] != 0 || ASSETCHAINS_DECAY[0] != 0 || ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_PUBLIC != 0 || ASSETCHAINS_PRIVATE != 0 || ASSETCHAINS_TXPOW != 0 || ASSETCHAINS_FOUNDERS != 0 || ASSETCHAINS_SCRIPTPUB.size() > 1 || ASSETCHAINS_SELFIMPORT.size() > 0 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || ASSETCHAINS_TIMELOCKGTE != _ASSETCHAINS_TIMELOCKOFF|| ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH || ASSETCHAINS_LWMAPOS != 0 || ASSETCHAINS_LASTERA > 0 || ASSETCHAINS_BEAMPORT != 0 || ASSETCHAINS_CODAPORT != 0 ) { fprintf(stderr,"perc %.4f%% ac_pub=[%02x%02x%02x...] acsize.%d\n",dstr(ASSETCHAINS_COMMISSION)*100,ASSETCHAINS_OVERRIDE_PUBKEY33[0],ASSETCHAINS_OVERRIDE_PUBKEY33[1],ASSETCHAINS_OVERRIDE_PUBKEY33[2],(int32_t)ASSETCHAINS_SCRIPTPUB.size()); extraptr = extrabuf; @@ -1927,6 +1924,10 @@ void komodo_args(char *argv0) fprintf(stderr," selfimport\n"); extralen += ASSETCHAINS_SELFIMPORT.size(); } + if ( ASSETCHAINS_BEAMPORT != 0 ) + extraptr[extralen++] = 'b'; + if ( ASSETCHAINS_CODAPORT != 0 ) + extraptr[extralen++] = 'c'; } addn = GetArg("-seednode",""); diff --git a/src/rpc/crosschain.cpp b/src/rpc/crosschain.cpp index 5f40f60fb..0c4703725 100644 --- a/src/rpc/crosschain.cpp +++ b/src/rpc/crosschain.cpp @@ -47,7 +47,7 @@ struct komodo_ccdata_entry *komodo_allMoMs(int32_t *nump,uint256 *MoMoMp,int32_t uint256 komodo_calcMoM(int32_t height,int32_t MoMdepth); extern std::string ASSETCHAINS_SELFIMPORT; uint256 Parseuint256(char *hexstr); -int32_t GetSelfimportProof(CMutableTransaction &mtx,CScript &scriptPubKey,TxProof &proof,uint64_t burnAmount,std::vector rawtx,uint256 txid,std::vector rawproof); +int32_t GetSelfimportProof(std::string source,CMutableTransaction &mtx,CScript &scriptPubKey,TxProof &proof,uint64_t burnAmount,std::vector rawtx,uint256 txid,std::vector rawproof); UniValue assetchainproof(const UniValue& params, bool fHelp) @@ -199,8 +199,8 @@ UniValue migrate_converttoexport(const UniValue& params, bool fHelp) if (burnAmount < needed) throw runtime_error("export_amount too small"); } - if ( ASSETCHAINS_SELFIMPORT.size() > 0 ) - throw runtime_error("self-import chains cant be fungible"); + //if ( ASSETCHAINS_SELFIMPORT.size() > 0 ) + // throw runtime_error("self-import chains cant be fungible"); CTxOut burnOut = MakeBurnOutput(burnAmount, ccid, targetSymbol, tx.vout); UniValue ret(UniValue::VOBJ); ret.push_back(Pair("payouts", HexStr(E_MARSHAL(ss << tx.vout)))); @@ -278,18 +278,23 @@ UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp) UniValue selfimport(const UniValue& params, bool fHelp) { CMutableTransaction mtx; - TxProof proof; CTransaction burnTx,tx; CTxOut burnOut; uint64_t burnAmount; uint256 txid,blockHash; std::vector vouts; std::vector rawtx,rawproof; CScript scriptPubKey; + std::string source; TxProof proof; CTransaction burnTx,tx; CTxOut burnOut; uint64_t burnAmount; uint256 txid,blockHash; std::vector vouts; std::vector rawtx,rawproof; CScript scriptPubKey; if ( ASSETCHAINS_SELFIMPORT.size() == 0 ) throw runtime_error("selfimport only works on -ac_import chains"); - if (fHelp || params.size() < 3 || params.size() > 4) - throw runtime_error("selfimport rawtx txid burnamount\n\n" + if (fHelp || params.size() < 3 || params.size() > 5 ) + throw runtime_error("selfimport rawtx txid burnamount [rawproof source]\n\n" "creates signed selfimport transaction from txid"); 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; - if ( params.size() == 4 ) + source = ASSETCHAINS_SELFIMPORT; + if ( params.size() >= 4 ) + { rawproof = ParseHex(params[3].get_str().c_str()); - if ( GetSelfimportProof(mtx,scriptPubKey,proof,burnAmount,rawtx,txid,rawproof) < 0 ) + if ( params.size() == 5 ) + source = params[4].get_str(); + } + 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);