diff --git a/src/cc/import.cpp b/src/cc/import.cpp index b250bb7bf..4d61b4bf8 100644 --- a/src/cc/import.cpp +++ b/src/cc/import.cpp @@ -25,7 +25,66 @@ * * This method should control every parameter of the ImportCoin transaction, since it has no signature * to protect it from malleability. + + ##### 0xffffffff is a special CCid for single chain/dual daemon imports */ + +extern std::string ASSETCHAINS_SELFIMPORT; +extern uint16_t ASSETCHAINS_CODAPORT,ASSETCHAINS_BEAMPORT; + +int32_t GetSelfimportProof(TxProof &proof,CTransaction burnTx,uint256 hash) // find burnTx with hash from "other" daemon +{ + if ( ASSETCHAINS_SELFIMPORT == "BEAM" ) + { + // confirm via ASSETCHAINS_BEAMPORT that burnTx/hash is a valid BEAM burn + return(-1); + } + else if ( ASSETCHAINS_SELFIMPORT == "CODA" ) + { + // confirm via ASSETCHAINS_CODAPORT that burnTx/hash is a valid CODA burn + return(-1); + } + else if ( ASSETCHAINS_SELFIMPORT == "PUBKEY" ) + { + // make sure vin0 is signed by ASSETCHAINS_OVERRIDE_PUBKEY33 + return(0); + } + else if ( ASSETCHAINS_SELFIMPORT == "GATEWAY" ) + { + // external coin is the assetchains symbol in the burnTx OP_RETURN + return(-1); + } + else return(-1); + return(0); +} + +// use proof from the above functions to validate the import + +int32_t CheckBEAMimport(TxProof proof,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) +{ + // 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) +{ + // 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) +{ + // if burnTx has ASSETCHAINS_PUBKEY vin, it is valid return(0); + return(0); + return(-1); +} + bool Eval::ImportCoin(const std::vector params, const CTransaction &importTx, unsigned int nIn) { if (importTx.vout.size() < 2) @@ -52,12 +111,6 @@ bool Eval::ImportCoin(const std::vector params, const CTransaction &imp if (!UnmarshalBurnTx(burnTx, targetSymbol, &targetCcid, payoutsHash)) return Invalid("invalid-burn-tx"); - if (targetCcid != GetAssetchainsCC() || targetSymbol != GetAssetchainsSymbol()) - return Invalid("importcoin-wrong-chain"); - - if (targetCcid < KOMODO_FIRSTFUNGIBLEID) - return Invalid("chain-not-fungible"); - // check burn amount { uint64_t burnAmount = burnTx.vout.back().nValue; @@ -74,13 +127,41 @@ bool Eval::ImportCoin(const std::vector params, const CTransaction &imp if (payoutsHash != SerializeHash(payouts)) return Invalid("wrong-payouts"); + if (targetCcid < KOMODO_FIRSTFUNGIBLEID) + return Invalid("chain-not-fungible"); + // Check proof confirms existance of burnTx + if ( targetCcid != 0xffffffff ) { + if (targetCcid != GetAssetchainsCC() || targetSymbol != GetAssetchainsSymbol()) + return Invalid("importcoin-wrong-chain"); uint256 target = proof.second.Exec(burnTx.GetHash()); if (!CheckMoMoM(proof.first, target)) return Invalid("momom-check-fail"); } - + else if ( ASSETCHAINS_SELFIMPORT == targetSymbol || ASSETCHAINS_SELFIMPORT == "GATEWAY" ) // various selfchain imports + { + if ( GetAssetchainsSymbol() == "BEAM" ) + { + if ( CheckBEAMimport(proof,burnTx,payouts) < 0 ) + return Invalid("BEAM-import-failure"); + } + else if ( GetAssetchainsSymbol() == "CODA" ) + { + if ( CheckCODAimport(proof,burnTx,payouts) < 0 ) + return Invalid("CODA-import-failure"); + } + else if ( GetAssetchainsSymbol() == "PUBKEY" ) + { + if ( CheckPUBKEYimport(proof,burnTx,payouts) < 0 ) + return Invalid("PUBKEY-import-failure"); + } + else + { + if ( CheckGATEWAYimport(GetAssetchainsSymbol(),proof,burnTx,payouts) < 0 ) + return Invalid("GATEWAY-import-failure"); + } + } return Valid(); } diff --git a/src/crosschain.cpp b/src/crosschain.cpp index e9444c607..8dc5657b8 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -284,13 +284,13 @@ bool CheckMoMoM(uint256 kmdNotarisationHash, uint256 momom) * in: txid * out: pair */ -TxProof GetAssetchainProof(uint256 hash) + +TxProof GetAssetchainProof(uint256 hash,CTransaction burnTx) { int nIndex; CBlockIndex* blockIndex; Notarisation nota; std::vector branch; - { uint256 blockHash; CTransaction tx; diff --git a/src/crosschain.h b/src/crosschain.h index 1fbd7603a..57e4a79d0 100644 --- a/src/crosschain.h +++ b/src/crosschain.h @@ -5,7 +5,7 @@ /* On assetchain */ -TxProof GetAssetchainProof(uint256 hash); +TxProof GetAssetchainProof(uint256 hash,CTransaction burnTx); /* On KMD */ uint256 CalculateProofRoot(const char* symbol, uint32_t targetCCid, int kmdHeight, diff --git a/src/importcoin.cpp b/src/importcoin.cpp index d36943b5d..e6b5a166f 100644 --- a/src/importcoin.cpp +++ b/src/importcoin.cpp @@ -10,7 +10,7 @@ CTransaction MakeImportCoinTransaction(const TxProof proof, const CTransaction burnTx, const std::vector payouts) { std::vector payload = E_MARSHAL(ss << EVAL_IMPORTCOIN); - CMutableTransaction mtx; + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); mtx.vin.push_back(CTxIn(COutPoint(burnTx.GetHash(), 10e8), CScript() << payload)); mtx.vout = payouts; auto importData = E_MARSHAL(ss << proof; ss << burnTx); diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 3e9b84db1..bd28d39f3 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -47,12 +47,12 @@ int COINBASE_MATURITY = _COINBASE_MATURITY;//100; int32_t KOMODO_MININGTHREADS = -1,IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,KOMODO_CHOSEN_ONE,ASSETCHAINS_SEED,KOMODO_ON_DEMAND,KOMODO_EXTERNAL_NOTARIES,KOMODO_PASSPORT_INITDONE,KOMODO_PAX,KOMODO_EXCHANGEWALLET,KOMODO_REWIND,KOMODO_CONNECTING = -1,KOMODO_DEALERNODE,KOMODO_EXTRASATOSHI,ASSETCHAINS_FOUNDERS; int32_t KOMODO_INSYNC,KOMODO_LASTMINED,prevKOMODO_LASTMINED,KOMODO_CCACTIVATE,JUMBLR_PAUSE = 1; -std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY,ASSETCHAINS_SCRIPTPUB; +std::string NOTARY_PUBKEY,ASSETCHAINS_NOTARIES,ASSETCHAINS_OVERRIDE_PUBKEY,DONATION_PUBKEY,ASSETCHAINS_SCRIPTPUB,ASSETCHAINS_SELFIMPORT; uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEYHASH[20],ASSETCHAINS_PUBLIC,ASSETCHAINS_PRIVATE,ASSETCHAINS_TXPOW; bool VERUS_MINTBLOCKS; char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN],ASSETCHAINS_USERPASS[4096]; -uint16_t ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT; +uint16_t ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT,ASSETCHAINS_BEAMPORT,ASSETCHAINS_CODAPORT; uint32_t ASSETCHAIN_INIT,ASSETCHAINS_CC,KOMODO_STOPAT,KOMODO_DPOWCONFS = 1; uint32_t ASSETCHAINS_MAGIC = 2387029918; int64_t ASSETCHAINS_GENESISTXVAL = 5000000000; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 47b0e1755..5e25898b5 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1773,6 +1773,42 @@ void komodo_args(char *argv0) ASSETCHAINS_COMMISSION = GetArg("-ac_perc",0); ASSETCHAINS_OVERRIDE_PUBKEY = GetArg("-ac_pubkey",""); ASSETCHAINS_SCRIPTPUB = GetArg("-ac_script",""); + ASSETCHAINS_BEAMPORT = GetArg("-ac_beam",0); + ASSETCHAINS_CODAPORT = GetArg("-ac_coda",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); + } + else if ( ASSETCHAINS_SELFIMPORT.size() > 0 && ASSETCHAINS_SELFIMPORT != "GATEWAY" ) + { + fprintf(stderr,"invalid -ac_import type\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); if ( (ASSETCHAINS_STAKED= GetArg("-ac_staked",0)) > 100 ) @@ -1807,11 +1843,11 @@ void komodo_args(char *argv0) ASSETCHAINS_COMMISSION = 53846154; // maps to 35% printf("ASSETCHAINS_COMMISSION defaulted to 35%% when founders reward active\n"); } - else + /*else if ( ASSETCHAINS_SELFIMPORT.size() == 0 ) { - ASSETCHAINS_OVERRIDE_PUBKEY.clear(); + //ASSETCHAINS_OVERRIDE_PUBKEY.clear(); printf("-ac_perc must be set with -ac_pubkey\n"); - } + }*/ } } else @@ -1827,7 +1863,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 ) + 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 ) { 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; @@ -1889,6 +1925,14 @@ void komodo_args(char *argv0) //extralen += iguana_rwnum(1,&extraptr[extralen],(int32_t)ASSETCHAINS_SCRIPTPUB.size(),(void *)ASSETCHAINS_SCRIPTPUB.c_str()); fprintf(stderr,"append ac_script %s\n",ASSETCHAINS_SCRIPTPUB.c_str()); } + if ( ASSETCHAINS_SELFIMPORT.size() > 0 ) + { + memcpy(&extraptr[extralen],(char *)ASSETCHAINS_SELFIMPORT.c_str(),ASSETCHAINS_SELFIMPORT.size()); + for (i=0; i 0 ) + { + throw runtime_error("self-import chains cant be fungible"); + /*if ( ASSETCHAINS_SELFIMPORT == targetSymbol || ASSETCHAINS_SELFIMPORT == "GATEWAY" ) + { + ccid = 0xffffffff; + } // else maybe clusters of self-import chains can be supported?*/ + } + CTxOut burnOut = MakeBurnOutput(burnAmount, ccid, targetSymbol, tx.vout); UniValue ret(UniValue::VOBJ); ret.push_back(Pair("payouts", HexStr(E_MARSHAL(ss << tx.vout)))); tx.vout.clear(); @@ -229,7 +237,7 @@ UniValue migrate_createimporttransaction(const UniValue& params, bool fHelp) throw runtime_error("Couldn't parse payouts"); uint256 txid = burnTx.GetHash(); - TxProof proof = GetAssetchainProof(burnTx.GetHash()); + TxProof proof = GetAssetchainProof(burnTx.GetHash(),burnTx); CTransaction importTx = MakeImportCoinTransaction(proof, burnTx, payouts); return HexStr(E_MARSHAL(ss << importTx)); @@ -255,6 +263,29 @@ UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp) return HexStr(E_MARSHAL(ss << importTx)); } +#ifdef selfimport +UniValue selfimport(const UniValue& params, bool fHelp) +{ + TxProof proof; CTransaction importTx,burnTx; CTxOut burnOut; uint64_t burnAmount; uint256 blockHash; + if ( ASSETCHAINS_SELFIMPORT.size() == 0 ) + throw runtime_error("selfimport only works on -ac_import chains"); + if (fHelp || params.size() != 2) + throw runtime_error("selfimport txid burnamount\n\n" + "creates signed selfimport transaction from txid"); + //txid = + //burnAmount = + + if ( GetTransaction(txid,burnTx,hashBlock,false) == 0 ) + throw runtime_error("selfimport couldnt find txid"); + if ( GetSelfimportProof(proof,burnTx,txid) < 0 ) + throw std::runtime_error("Failed validating selfimport"); + + burnOut = MakeBurnOutput(burnAmount,0xffffffff,ASSETCHAINS_SELFIMPORT,burnTx.vout); + importTx = MakeImportCoinTransaction(proof,burnTx,payouts); + importTx.vout.clear(); + importTx.vout.push_back(burnOut); +} +#endif UniValue getNotarisationsForBlock(const UniValue& params, bool fHelp) {