From 246919c32d86e0a2815420ec7d2f3d0bff059fe9 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/Makefile.am | 1 + src/init.cpp | 8 +++++++ src/wallet/rpcwallet.cpp | 39 ++-------------------------------- src/wallet/wallet.cpp | 46 +++++----------------------------------- src/wallet/wallet.h | 4 ---- src/wallet/walletdb.cpp | 32 ---------------------------- src/wallet/walletdb.h | 3 --- 7 files changed, 16 insertions(+), 117 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index f9cde36f07..88013cedff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -232,6 +232,7 @@ endif libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_wallet_a_SOURCES = \ + global/common.cpp \ wallet/crypter.cpp \ wallet/db.cpp \ wallet/rpcdump.cpp \ diff --git a/src/init.cpp b/src/init.cpp index eff25f8d46..fbe884818e 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" @@ -436,6 +437,7 @@ std::string HelpMessage(HelpMessageMode mode) 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("-bip9params=deployment:start:end", "Use given start/end times for specified BIP9 deployment (regtest-only)"); + strUsage += HelpMessageOpt("-assetdir=hexidstr:label", "For naming known assets"); } std::string debugCategories = "addrman, alert, bench, cmpctblock, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below if (mode == HMM_BITCOIN_QT) @@ -1077,6 +1079,12 @@ bool AppInitParameterInteraction() fEnableReplacement = (std::find(vstrReplacementModes.begin(), vstrReplacementModes.end(), "fee") != vstrReplacementModes.end()); } + try { + InitGlobalAssetDir(mapMultiArgs.count("-assetdir") > 0 ? mapMultiArgs.at("-assetdir") : std::vector()); + } catch (const std::exception& e) { + return InitError(strprintf("Error in -assetdir: %s\n", e.what())); + } + if (mapMultiArgs.count("-bip9params")) { // Allow overriding BIP9 parameters for testing if (!chainparams.MineBlocksOnDemand()) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 180d12962f..d607b51341 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -85,7 +85,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); @@ -3607,40 +3607,6 @@ UniValue claimpegin(const JSONRPCRequest& request) return finalTxn.GetHash().GetHex(); } -UniValue addassetlabel(const JSONRPCRequest& request) -{ - if (!EnsureWalletIsAvailable(request.fHelp)) - return NullUniValue; - - // TODO: Add basic protection against over-writing asset - if (request.fHelp || request.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(request.params, boost::assign::list_of(UniValue::VSTR)(UniValue::VSTR)); - - std::string id = request.params[0].get_str(); - std::string label = request.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 dumpassetlabels(const JSONRPCRequest& request) { if (!EnsureWalletIsAvailable(request.fHelp)) @@ -3649,7 +3615,7 @@ UniValue dumpassetlabels(const JSONRPCRequest& request) if (request.fHelp || request.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" + HelpExampleCli("generateasset", "\"my asset\" 10" ) + HelpExampleRpc("generateasset", "\"my asset\" 10" ) ); @@ -3679,7 +3645,6 @@ static const CRPCCommand commands[] = { "hidden", "resendwallettransactions", &resendwallettransactions, true, {} }, { "wallet", "abandontransaction", &abandontransaction, false, {"txid"} }, { "wallet", "addmultisigaddress", &addmultisigaddress, true, {"nrequired","keys","account"} }, - { "wallet", "addassetlabel", &addassetlabel, true , {} }, { "wallet", "addwitnessaddress", &addwitnessaddress, true, {"address"} }, { "wallet", "backupwallet", &backupwallet, true, {"destination"} }, { "wallet", "dumpblindingkey", &dumpblindingkey, true, {} }, diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 775d42b298..fc61b61b7d 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 "validation.h" @@ -92,11 +94,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() @@ -1407,17 +1405,6 @@ bool CWallet::IsHDEnabled() return !hdChain.masterKeyID.IsNull(); } -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; @@ -4017,9 +4004,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile) } } - // All wallets should understand native peg-in currency - walletInstance->SetAssetPair("bitcoin", BITCOINID); - walletInstance->SetBestChain(chainActive.GetLocator()); } else if (IsArgSet("-usehd")) { @@ -4335,19 +4319,13 @@ bool CMerkleTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& 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 @@ -4404,20 +4382,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()) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 179725aec2..baefb38af8 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -921,10 +921,6 @@ public: bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const; void SetBestChain(const CBlockLocator& loc) override; - 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 bf18a1ad14..e792367089 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -214,16 +214,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; @@ -577,28 +567,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 afe587c6c5..cf614f4a12 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -170,9 +170,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 LoadWallet(CWallet* pwallet); DBErrors FindWalletTx(CWallet* pwallet, std::vector& vTxHash, std::vector& vWtx); DBErrors ZapWalletTx(CWallet* pwallet, std::vector& vWtx);