From 91b6694c71b4c5ebaa2caef34eaf854552fd6bc5 Mon Sep 17 00:00:00 2001 From: blackjok3r Date: Wed, 2 Jan 2019 23:04:26 +0800 Subject: [PATCH] Fix some bugs? Add getimports RPC. --- src/notaries_staked.cpp | 7 ++- src/rpc/crosschain.cpp | 105 ++++++++++++++++++++++++++++++++++++++++ src/rpc/server.cpp | 1 + src/rpc/server.h | 1 + src/wallet/walletdb.cpp | 6 +-- 5 files changed, 115 insertions(+), 5 deletions(-) diff --git a/src/notaries_staked.cpp b/src/notaries_staked.cpp index e53553b23..8cce14a0b 100644 --- a/src/notaries_staked.cpp +++ b/src/notaries_staked.cpp @@ -11,14 +11,19 @@ extern pthread_mutex_t staked_mutex; extern uint8_t NOTARY_PUBKEY33[33],NUM_NOTARIES; int8_t is_STAKED(const char *chain_name) { - int STAKED = 0; + static int8_t STAKED,doneinit; + if (doneinit == 1) + return(STAKED); if ( (strcmp(chain_name, "LABS") == 0) || (strncmp(chain_name, "LABS", 4) == 0) ) STAKED = 1; else if ( (strcmp(chain_name, "LAB") == 0) || (strncmp(chain_name, "LAB", 3) == 0) ) STAKED = 2; else if ( (strcmp(chain_name, "CFEK") == 0) || (strncmp(chain_name, "CFEK", 4) == 0) ) STAKED = 3; + else + STAKED = 0; //fprintf(stderr, "This chain is: %s which is: %d\n", chain_name,STAKED); + doneinit = 1; return(STAKED); }; diff --git a/src/rpc/crosschain.cpp b/src/rpc/crosschain.cpp index 98267aae9..3950e48a1 100644 --- a/src/rpc/crosschain.cpp +++ b/src/rpc/crosschain.cpp @@ -307,3 +307,108 @@ UniValue scanNotarisationsDB(const UniValue& params, bool fHelp) out.pushKV("opreturn", HexStr(E_MARSHAL(ss << nota.second))); return out; } + +UniValue getimports(const UniValue& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw runtime_error( + "getmigrates \"hash|height\"\n" + "\n\n" + "\nResult:\n" + "{\n" + " \"imports\" : [ (json array)\n" + " \"transactionid\" : { (json object)\n" + " \"value\" : (numeric)\n" + " \"address\" : (string)\n" + " \"export\" { (json object)\n" + " \"txid\" : (string)\n" + " \"value\" : (numeric)\n" + " \"chain\" : (string)\n" //TODO! + " }\n" + " }" + " ]\n" + " \"TotalImported\" : (numeric)\n" + " \"time\" : (numeric)\n" + "}\n" + "\nExamples:\n" + + HelpExampleCli("getmigrates", "\"00000000febc373a1da2bd9f887b105ad79ddc26ac26c2b28652d64e5207c5b5\"") + + HelpExampleRpc("getmigrates", "\"00000000febc373a1da2bd9f887b105ad79ddc26ac26c2b28652d64e5207c5b5\"") + + HelpExampleCli("getmigrates", "12800") + + HelpExampleRpc("getmigrates", "12800") + ); + + LOCK(cs_main); + + std::string strHash = params[0].get_str(); + + // If height is supplied, find the hash + if (strHash.size() < (2 * sizeof(uint256))) { + // std::stoi allows characters, whereas we want to be strict + regex r("[[:digit:]]+"); + if (!regex_match(strHash, r)) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block height parameter"); + } + + int nHeight = -1; + try { + nHeight = std::stoi(strHash); + } + catch (const std::exception &e) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block height parameter"); + } + + if (nHeight < 0 || nHeight > chainActive.Height()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); + } + strHash = chainActive[nHeight]->GetBlockHash().GetHex(); + } + + uint256 hash(uint256S(strHash)); + + if (mapBlockIndex.count(hash) == 0) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); + + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[hash]; + + if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) + throw JSONRPCError(RPC_INTERNAL_ERROR, "Block not available (pruned data)"); + + if(!ReadBlockFromDisk(block, pblockindex,1)) + throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); + + UniValue result(UniValue::VOBJ); + CAmount TotalImported; + UniValue imports(UniValue::VARR); + BOOST_FOREACH(const CTransaction&tx, block.vtx) + { + if(tx.IsCoinImport()) + { + UniValue objTx(UniValue::VOBJ); + objTx.push_back(Pair("txid",tx.GetHash().ToString())); + TxProof proof; + CTransaction burnTx; + std::vector payouts; + TotalImported += tx.vout[1].nValue; + printf("nvalue.%li TotalImported.%li\n",tx.vout[1].nValue,TotalImported); + objTx.push_back(Pair("amount", ValueFromAmount(tx.vout[1].nValue))); + CTxDestination importaddress; + if (ExtractDestination(tx.vout[1].scriptPubKey, importaddress)) + { + objTx.push_back(Pair("address", CBitcoinAddress(importaddress).ToString())); + } + UniValue objBurnTx(UniValue::VOBJ); + if (UnmarshalImportTx(tx, proof, burnTx, payouts)) { + objBurnTx.push_back(Pair("txid", burnTx.GetHash().ToString())); + objBurnTx.push_back(Pair("amount", ValueFromAmount(burnTx.vout.size() ? burnTx.vout.back().nValue : 0))); + // TODO: add source chain, using new data in burn OP_RETURN from upsteam. + } + objTx.push_back(Pair("export", objBurnTx)); + imports.push_back(objTx); + } + } + result.push_back(Pair("imports", imports)); + result.push_back(Pair("TotalImported", ValueFromAmount(TotalImported))); + result.push_back(Pair("time", block.GetBlockTime())); + return result; +} diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 800c01423..d1c4d56b9 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -333,6 +333,7 @@ static const CRPCCommand vRPCCommands[] = { "crosschain", "crosschainproof", &crosschainproof, true }, { "crosschain", "getNotarisationsForBlock", &getNotarisationsForBlock, true }, { "crosschain", "scanNotarisationsDB", &scanNotarisationsDB, true }, + { "crosschain", "getimports", &getimports, true }, { "crosschain", "migrate_converttoexport", &migrate_converttoexport, true }, { "crosschain", "migrate_createimporttransaction", &migrate_createimporttransaction, true }, { "crosschain", "migrate_completeimporttransaction", &migrate_completeimporttransaction, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index a49355c88..b1f94701e 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -426,6 +426,7 @@ extern UniValue assetchainproof(const UniValue& params, bool fHelp); extern UniValue crosschainproof(const UniValue& params, bool fHelp); extern UniValue getNotarisationsForBlock(const UniValue& params, bool fHelp); extern UniValue scanNotarisationsDB(const UniValue& params, bool fHelp); +extern UniValue getimports(const UniValue& params, bool fHelp); extern UniValue migrate_converttoexport(const UniValue& params, bool fHelp); extern UniValue migrate_createimporttransaction(const UniValue& params, bool fHelp); extern UniValue migrate_completeimporttransaction(const UniValue& params, bool fHelp); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 3bfb40648..a230910f3 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -859,7 +859,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, static bool IsKeyType(string strType) { - return (strType== "key" || strType == "wkey" || + return (strType == "key" || strType == "wkey" || strType == "hdseed" || strType == "chdseed" || strType == "zkey" || strType == "czkey" || strType == "sapzkey" || strType == "csapzkey" || @@ -922,9 +922,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet) { // Rescan if there is a bad transaction record.. //SoftSetBoolArg("-rescan", true); - uint256 hash; - ssKey >> hash; - fprintf(stderr, "TX corrupted hash: %s\n", hash.ToString().c_str()); + fprintf(stderr, "TX corrupted.. aborted rescan!\n"); } } }