From 3cd9298b087407311665116d444f10a9aaf6f0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Tim=C3=B3n?= Date: Wed, 15 Mar 2017 20:48:31 +0100 Subject: [PATCH] CA: Use CAssetsDir that inits from config instead of wallet - Don't maintain asset petnames in wallet. - RPC: Remove addassetlabel - Remove load functions in CWallet for asset labels. - "bitcoin" already hardcoded in CAssetsDir::InitFromStrings, remove from CWallet::InitLoadWallet - Remove CWallet::SetAssetPair --- src/init.cpp | 8 +++++ src/wallet/rpcwallet.cpp | 39 ++--------------------- src/wallet/test/rpc_wallet_tests.cpp | 9 ++++-- src/wallet/wallet.cpp | 46 +++------------------------- src/wallet/wallet.h | 6 ---- src/wallet/walletdb.cpp | 32 ------------------- src/wallet/walletdb.h | 3 -- 7 files changed, 21 insertions(+), 122 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 12957ef922..b377f1e396 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -17,6 +17,7 @@ #include "checkpoints.h" #include "compat/sanity.h" #include "consensus/validation.h" +#include "global/common.h" #include "httpserver.h" #include "httprpc.h" #include "key.h" @@ -414,6 +415,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-limitancestorsize=", strprintf("Do not accept transactions whose size with all in-mempool ancestors exceeds kilobytes (default: %u)", DEFAULT_ANCESTOR_SIZE_LIMIT)); strUsage += HelpMessageOpt("-limitdescendantcount=", strprintf("Do not accept transactions if any ancestor would have or more in-mempool descendants (default: %u)", DEFAULT_DESCENDANT_LIMIT)); strUsage += HelpMessageOpt("-limitdescendantsize=", strprintf("Do not accept transactions if any ancestor would have more than kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT)); + strUsage += HelpMessageOpt("-assetdir=hexidstr:label", "For naming known assets"); strUsage += HelpMessageOpt("-bip9params=deployment:start:end", "Use given start/end times for specified bip9 deployment (regtest-only)"); } string debugCategories = "addrman, alert, bench, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below @@ -998,6 +1000,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end()); } + try { + InitGlobalAssetDir(mapMultiArgs["-assetdir"]); + } catch (const std::exception& e) { + return InitError(strprintf("Error in -assetdir: %s\n", e.what())); + } + if (!mapMultiArgs["-bip9params"].empty()) { // Allow overriding bip9 parameters for testing if (!Params().MineBlocksOnDemand()) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 7e05383a6e..5f871e0a3e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -82,7 +82,7 @@ void EnsureWalletIsUnlocked() // Attaches labeled balance reports to UniValue obj with asset filter // "" displays *all* assets as VOBJ pairs, while named assets must have -// been entered via addassetlabel RPC command and are returns as VNUM. +// been entered via -assetdir configuration argument and are returned as VNUM. UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string strasset) { UniValue obj(UniValue::VOBJ); @@ -3178,40 +3178,6 @@ UniValue claimpegin(const UniValue& params, bool fHelp) return finalTxn.GetHash().GetHex(); } -UniValue addassetlabel(const UniValue& params, bool fHelp) -{ - if (!EnsureWalletIsAvailable(fHelp)) - return NullUniValue; - - // TODO: Add basic protection against over-writing asset - if (fHelp || params.size() != 2) - throw runtime_error( - "addassetlabel id label\n" - "\nAdd a label to a known asset ID. This label can be used in place of the ID for all asset-compatible RPC calls.\n" - "\nArguments:\n" - "1. \"id\" (string, required) Hex ID that will be given label.\n" - "2. \"label\" (string, required) Label that will be assigned to ID.\n" - "\nExamples:\n" - + HelpExampleCli("addassetlabel", "\"fa821b0be5e1387adbcb69dbb3ad33edb5e470831c7c938c4e7b344edbe8bb11\", \"ethereum\"") - + HelpExampleRpc("addassetlabel", "\"fa821b0be5e1387adbcb69dbb3ad33edb5e470831c7c938c4e7b344edbe8bb11\", \"ethereum\"") - ); - RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR)(UniValue::VSTR)); - - std::string id = params[0].get_str(); - std::string label = params[1].get_str(); - if (!IsHex(id) || id.size() != 64) - throw JSONRPCError(RPC_TYPE_ERROR, "Asset ID must be hex of length 64"); - if (label == "bitcoin" || label == "Bitcoin" || label == "btc") - throw JSONRPCError(RPC_TYPE_ERROR, "'bitcoin' label is protected"); - else if (label.size() > 32 || label.size() < 3) { - throw JSONRPCError(RPC_TYPE_ERROR, "Please pick a label between 3 and 32 characters."); - } - - pwalletMain->SetAssetPair(label, CAsset(uint256S(id))); - - return NullUniValue; -} - UniValue generateasset(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) @@ -3257,7 +3223,7 @@ UniValue dumpassetlabels(const UniValue& params, bool fHelp) if (fHelp || params.size() != 0) throw runtime_error( "dumpassetlabels\n" - "\nLists all known asset id/label pairs in this wallet. This list can be modified by `addassetlabel` command.\n" + "\nLists all known asset id/label pairs in this wallet. This list can be modified with `-assetdir` configuration argument.\n" ); UniValue obj(UniValue::VOBJ); for (CAsset as : pwalletMain->GetKnownAssets()) { @@ -3283,7 +3249,6 @@ static const CRPCCommand commands[] = { "rawtransactions", "fundrawtransaction", &fundrawtransaction, false }, { "hidden", "resendwallettransactions", &resendwallettransactions, true }, { "wallet", "abandontransaction", &abandontransaction, false }, - { "wallet", "addassetlabel", &addassetlabel, true }, { "wallet", "addmultisigaddress", &addmultisigaddress, true }, { "wallet", "addwitnessaddress", &addwitnessaddress, true }, { "wallet", "backupwallet", &backupwallet, true }, diff --git a/src/wallet/test/rpc_wallet_tests.cpp b/src/wallet/test/rpc_wallet_tests.cpp index 15a6f7bb33..7e68682d0f 100644 --- a/src/wallet/test/rpc_wallet_tests.cpp +++ b/src/wallet/test/rpc_wallet_tests.cpp @@ -5,7 +5,9 @@ #include "rpc/server.h" #include "rpc/client.h" +#include "assetsdir.h" #include "base58.h" +#include "global/common.h" #include "main.h" #include "wallet/wallet.h" @@ -22,6 +24,7 @@ extern UniValue createArgs(int nRequired, const char* address1 = NULL, const cha extern UniValue CallRPC(string args); extern CWallet* pwalletMain; +extern CAssetsDir _gAssetsDir; BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, WalletTestingSetup) @@ -64,6 +67,9 @@ BOOST_AUTO_TEST_CASE(rpc_addmultisig) BOOST_AUTO_TEST_CASE(rpc_wallet) { + // Need to set bitcoin asset for listreceivedby* tests + std::vector assetsToInit = {}; + _gAssetsDir.InitFromStrings(assetsToInit); // Test RPC calls for various wallet statistics UniValue r; CPubKey demoPubkey; @@ -74,9 +80,6 @@ BOOST_AUTO_TEST_CASE(rpc_wallet) { LOCK(pwalletMain->cs_wallet); - // Need to set bitcoin asset for listreceivedby* tests - pwalletMain->SetAssetPair("bitcoin", BITCOINID); - demoPubkey = pwalletMain->GenerateNewKey(); demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID())); string strPurpose = "receive"; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 85cf2ecbde..3a0c1d6824 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -5,6 +5,7 @@ #include "wallet/wallet.h" +#include "assetsdir.h" #include "base58.h" #include "checkpoints.h" #include "chain.h" @@ -12,6 +13,7 @@ #include "consensus/consensus.h" #include "consensus/validation.h" #include "crypto/hmac_sha256.h" +#include "global/common.h" #include "key.h" #include "keystore.h" #include "main.h" @@ -91,11 +93,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const std::vector CWallet::GetKnownAssets() const { - std::vector lAssets; - for (auto knownAsset : this->mapAssets) { - lAssets.push_back(knownAsset.second); - } - return lAssets; + return gAssetsDir.GetKnownAssets(); } CPubKey CWallet::GenerateNewKey() @@ -1234,17 +1232,6 @@ bool CWallet::SetHDChain(const CHDChain& chain, bool memonly) return true; } -bool CWallet::SetAssetPair(const std::string& label, const CAsset& id) -{ - LOCK(cs_wallet); - if (!CWalletDB(strWalletFile).WriteAssetLabelPair(id, label) || - !CWalletDB(strWalletFile).WriteLabelAssetPair(label, id)) - throw runtime_error(std::string(__func__) + ": writing asset pair failed"); - mapAssetLabels[id] = label; - mapAssets[label] = id; - return true; -} - int64_t CWalletTx::GetTxTime() const { int64_t n = nTimeSmart; @@ -3693,9 +3680,6 @@ bool CWallet::InitLoadWallet() return InitError(_("Cannot write default address") += "\n"); } - // All wallets should understand native peg-in currency - walletInstance->SetAssetPair("bitcoin", BITCOINID); - walletInstance->SetBestChain(chainActive.GetLocator()); } else if (mapArgs.count("-usehd")) { @@ -3953,19 +3937,13 @@ bool CMerkleTx::AcceptToMemoryPool(bool fLimitFree, CAmount nAbsurdFee) std::string CWallet::GetLabelFromAsset(const CAsset& id) const { LOCK(cs_wallet); - std::map::const_iterator it = mapAssetLabels.find(id); - if (it != mapAssetLabels.end()) - return it->second; - return ""; + return gAssetsDir.GetLabel(id); } CAsset CWallet::GetAssetFromLabel(const std::string& label) const { LOCK(cs_wallet); - std::map::const_iterator it = mapAssets.find(label); - if (it != mapAssets.end()) - return it->second; - return CAsset(); + return gAssetsDir.GetAsset(label); } CKey CWallet::GetBlindingKey(const CScript* script) const @@ -4022,20 +4000,6 @@ bool CWallet::AddSpecificBlindingKey(const CScriptID& scriptid, const uint256& k return CWalletDB(strWalletFile).WriteSpecificBlindingKey(scriptid, key); } -bool CWallet::LoadLabelAssetMapping(const std::string& label, const CAsset& id) -{ - AssertLockHeld(cs_wallet); - mapAssets[label] = id; - return true; -} - -bool CWallet::LoadAssetLabelMapping(const CAsset& id, const std::string& label) -{ - AssertLockHeld(cs_wallet); - mapAssetLabels[id] = label; - return true; -} - void CWallet::ComputeBlindingData(const CTxOut& output, const CTxOutWitness& witness, CAmount& amount, CPubKey& pubkey, uint256& blindingfactor, CAsset& asset, uint256& assetBlindingFactor) const { if (output.nValue.IsExplicit() && (output.nAsset.IsExplicit() || output.nAsset.IsAssetGeneration())) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index b4c56a3482..bcc615e7a2 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -615,8 +615,6 @@ private: /* the HD chain data model (external chain counters) */ CHDChain hdChain; - std::map mapAssetLabels; - std::map mapAssets; public: /* @@ -849,10 +847,6 @@ public: CAmountMap GetChange(const CWalletTx& tx) const; void SetBestChain(const CBlockLocator& loc); - bool SetAssetPair(const std::string& label, const CAsset& id); - bool LoadLabelAssetMapping(const std::string& label, const CAsset& id); - bool LoadAssetLabelMapping(const CAsset&, const std::string&); - DBErrors LoadWallet(bool& fFirstRunRet); DBErrors ZapWalletTx(std::vector& vWtx); DBErrors ZapSelectTx(std::vector& vHashIn, std::vector& vHashOut); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 2447d4bb7d..3fe461ec19 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -207,16 +207,6 @@ bool CWalletDB::WriteBlindingDerivationKey(const uint256& key) return Write(std::string("blindingderivationkey"), key); } -bool CWalletDB::WriteAssetLabelPair(const CAsset& asset, const std::string& label) -{ - return Write(make_pair(std::string("assetlabelmapping"), asset), label); -} - -bool CWalletDB::WriteLabelAssetPair(const std::string& label, const CAsset& asset) -{ - return Write(make_pair(std::string("labelassetmapping"), label), asset); -} - CAmount CWalletDB::GetAccountCreditDebit(const string& strAccount) { list entries; @@ -647,28 +637,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, return false; } } - else if (strType == "labelassetmapping") - { - string label; - ssKey >> label; - CAsset asset; - ssValue >> asset; - if (!pwallet->LoadLabelAssetMapping(label, asset)) { - strErr = "Error reading wallet database: LoadLabelAssetMapping failed"; - return false; - } - } - else if (strType == "assetlabelmapping") - { - CAsset asset; - ssKey >> asset; - string label; - ssValue >> label; - if (!pwallet->LoadAssetLabelMapping(asset, label)) { - strErr = "Error reading wallet database: LoadAssetLabelMapping failed"; - return false; - } - } } catch (...) { return false; diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 8e1ffc0a55..cfd8039147 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -171,9 +171,6 @@ public: bool WriteSpecificBlindingKey(const CScriptID& scriptid, const uint256& key); bool WriteBlindingDerivationKey(const uint256& key); - bool WriteAssetLabelPair(const CAsset& asset, const std::string& label); - bool WriteLabelAssetPair(const std::string& label, const CAsset& asset); - DBErrors ReorderTransactions(CWallet* pwallet); DBErrors LoadWallet(CWallet* pwallet); DBErrors FindWalletTx(CWallet* pwallet, std::vector& vTxHash, std::vector& vWtx);