From 64b54a40b94075bf7bff9aea8a9105598afed84a Mon Sep 17 00:00:00 2001 From: - <-> Date: Fri, 27 Dec 2024 20:44:10 +0100 Subject: [PATCH] enotes changes --- src/wallet/api/CMakeLists.txt | 8 +- src/wallet/api/coins.cpp | 185 ---------------------------------- src/wallet/api/coins.h | 40 -------- src/wallet/api/coins_info.cpp | 122 ---------------------- src/wallet/api/coins_info.h | 71 ------------- src/wallet/api/wallet.cpp | 52 +++++++++- src/wallet/api/wallet.h | 7 +- src/wallet/api/wallet2_api.h | 68 +++++-------- 8 files changed, 80 insertions(+), 473 deletions(-) delete mode 100644 src/wallet/api/coins.cpp delete mode 100644 src/wallet/api/coins.h delete mode 100644 src/wallet/api/coins_info.cpp delete mode 100644 src/wallet/api/coins_info.h diff --git a/src/wallet/api/CMakeLists.txt b/src/wallet/api/CMakeLists.txt index bb740e2ac..af7948d8a 100644 --- a/src/wallet/api/CMakeLists.txt +++ b/src/wallet/api/CMakeLists.txt @@ -40,9 +40,7 @@ set(wallet_api_sources address_book.cpp subaddress.cpp subaddress_account.cpp - unsigned_transaction.cpp - coins.cpp - coins_info.cpp) + unsigned_transaction.cpp) set(wallet_api_headers wallet2_api.h) @@ -57,9 +55,7 @@ set(wallet_api_private_headers address_book.h subaddress.h subaddress_account.h - unsigned_transaction.h - coins.h - coins_info.h) + unsigned_transaction.h) monero_private_headers(wallet_api ${wallet_api_private_headers}) diff --git a/src/wallet/api/coins.cpp b/src/wallet/api/coins.cpp deleted file mode 100644 index 2321c638d..000000000 --- a/src/wallet/api/coins.cpp +++ /dev/null @@ -1,185 +0,0 @@ -#include "coins.h" -#include "coins_info.h" -#include "wallet.h" -#include "crypto/hash.h" -#include "wallet/wallet2.h" -#include "common_defines.h" - -#include -#include - -using namespace epee; - -namespace Monero { - -Coins::~Coins() = default; - -CoinsImpl::CoinsImpl(WalletImpl *wallet) - : m_wallet(wallet) {} - -CoinsImpl::~CoinsImpl() -{ - for (auto t : m_rows) - delete t; -} - -int CoinsImpl::count() const -{ - boost::shared_lock lock(m_rowsMutex); - int result = m_rows.size(); - return result; -} - -CoinsInfo *CoinsImpl::coin(int index) const -{ - boost::shared_lock lock(m_rowsMutex); - // sanity check - if (index < 0) - return nullptr; - auto index_ = static_cast(index); - return index_ < m_rows.size() ? m_rows[index_] : nullptr; -} - -std::vector CoinsImpl::getAll() const -{ - boost::shared_lock lock(m_rowsMutex); - return m_rows; -} - - -void CoinsImpl::refresh() -{ - LOG_PRINT_L2("Refreshing coins"); - - boost::unique_lock lock(m_rowsMutex); - boost::shared_lock transfers_lock(m_wallet->m_wallet->m_transfers_mutex); - - // delete old outputs; - for (auto t : m_rows) - delete t; - m_rows.clear(); - - for (size_t i = 0; i < m_wallet->m_wallet->get_num_transfer_details(); ++i) - { - const tools::wallet2::transfer_details &td = m_wallet->m_wallet->get_transfer_details(i); - - auto ci = new CoinsInfoImpl(); - ci->m_blockHeight = td.m_block_height; - ci->m_hash = string_tools::pod_to_hex(td.m_txid); - ci->m_internalOutputIndex = td.m_internal_output_index; - ci->m_globalOutputIndex = td.m_global_output_index; - ci->m_spent = td.m_spent; - ci->m_frozen = td.m_frozen; - ci->m_spentHeight = td.m_spent_height; - ci->m_amount = td.m_amount; - ci->m_rct = td.m_rct; - ci->m_keyImageKnown = td.m_key_image_known; - ci->m_pkIndex = td.m_pk_index; - ci->m_subaddrIndex = td.m_subaddr_index.minor; - ci->m_subaddrAccount = td.m_subaddr_index.major; - ci->m_address = m_wallet->m_wallet->get_subaddress_as_str(td.m_subaddr_index); // todo: this is expensive, cache maybe? - ci->m_addressLabel = m_wallet->m_wallet->get_subaddress_label(td.m_subaddr_index); - ci->m_keyImage = string_tools::pod_to_hex(td.m_key_image); - ci->m_unlockTime = td.m_tx.unlock_time; - ci->m_unlocked = m_wallet->m_wallet->is_transfer_unlocked(td); - ci->m_pubKey = string_tools::pod_to_hex(td.get_public_key()); - ci->m_coinbase = td.m_tx.vin.size() == 1 && td.m_tx.vin[0].type() == typeid(cryptonote::txin_gen); - ci->m_description = m_wallet->m_wallet->get_tx_note(td.m_txid); - - m_rows.push_back(ci); - } -} - -void CoinsImpl::setFrozen(std::string public_key) -{ - crypto::public_key pk; - if (!epee::string_tools::hex_to_pod(public_key, pk)) - { - LOG_ERROR("Invalid public key: " << public_key); - return; - } - - try - { - m_wallet->m_wallet->freeze(pk); - refresh(); - } - catch (const std::exception& e) - { - LOG_ERROR("setFrozen: " << e.what()); - } -} - -void CoinsImpl::setFrozen(int index) -{ - try - { - m_wallet->m_wallet->freeze(index); - refresh(); - } - catch (const std::exception& e) - { - LOG_ERROR("setLabel: " << e.what()); - } -} - -void CoinsImpl::thaw(std::string public_key) -{ - crypto::public_key pk; - if (!epee::string_tools::hex_to_pod(public_key, pk)) - { - LOG_ERROR("Invalid public key: " << public_key); - return; - } - - try - { - m_wallet->m_wallet->thaw(pk); - refresh(); - } - catch (const std::exception& e) - { - LOG_ERROR("thaw: " << e.what()); - } -} - -void CoinsImpl::thaw(int index) -{ - try - { - m_wallet->m_wallet->thaw(index); - refresh(); - } - catch (const std::exception& e) - { - LOG_ERROR("thaw: " << e.what()); - } -} - -bool CoinsImpl::isTransferUnlocked(uint64_t unlockTime, uint64_t blockHeight) { - return m_wallet->m_wallet->is_transfer_unlocked(unlockTime, blockHeight); -} - -void CoinsImpl::setDescription(const std::string &public_key, const std::string &description) -{ - crypto::public_key pk; - if (!epee::string_tools::hex_to_pod(public_key, pk)) - { - LOG_ERROR("Invalid public key: " << public_key); - return; - } - - try - { - const size_t index = m_wallet->m_wallet->get_transfer_details(pk); - const tools::wallet2::transfer_details &td = m_wallet->m_wallet->get_transfer_details(index); - m_wallet->m_wallet->set_tx_note(td.m_txid, description); - refresh(); - } - catch (const std::exception& e) - { - LOG_ERROR("setDescription: " << e.what()); - } -} - -} // namespace diff --git a/src/wallet/api/coins.h b/src/wallet/api/coins.h deleted file mode 100644 index b7a0a8642..000000000 --- a/src/wallet/api/coins.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef FEATHER_COINS_H -#define FEATHER_COINS_H - -#include "wallet/api/wallet2_api.h" -#include "wallet/wallet2.h" - -namespace Monero { - -class WalletImpl; - -class CoinsImpl : public Coins -{ -public: - explicit CoinsImpl(WalletImpl * wallet); - ~CoinsImpl() override; - int count() const override; - CoinsInfo * coin(int index) const override; - std::vector getAll() const override; - void refresh() override; - - void setFrozen(std::string public_key) override; - void setFrozen(int index) override; - void thaw(std::string public_key) override; - void thaw(int index) override; - - bool isTransferUnlocked(uint64_t unlockTime, uint64_t blockHeight) override; - - void setDescription(const std::string &public_key, const std::string &description) override; - -private: - WalletImpl *m_wallet; - std::vector m_rows; - mutable boost::shared_mutex m_rowsMutex; -}; - -} - -namespace Bitmonero = Monero; - -#endif //FEATHER_COINS_H diff --git a/src/wallet/api/coins_info.cpp b/src/wallet/api/coins_info.cpp deleted file mode 100644 index 5f2c4e1e4..000000000 --- a/src/wallet/api/coins_info.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include "coins_info.h" - -using namespace std; - -namespace Monero { - -CoinsInfo::~CoinsInfo() = default; - -CoinsInfoImpl::CoinsInfoImpl() - : m_blockHeight(0) - , m_internalOutputIndex(0) - , m_globalOutputIndex(0) - , m_spent(false) - , m_frozen(false) - , m_spentHeight(0) - , m_amount(0) - , m_rct(false) - , m_keyImageKnown(false) - , m_pkIndex(0) - , m_subaddrAccount(0) - , m_subaddrIndex(0) - , m_unlockTime(0) - , m_unlocked(false) -{ - -} - -CoinsInfoImpl::~CoinsInfoImpl() = default; - -uint64_t CoinsInfoImpl::blockHeight() const -{ - return m_blockHeight; -} - -string CoinsInfoImpl::hash() const -{ - return m_hash; -} - -size_t CoinsInfoImpl::internalOutputIndex() const { - return m_internalOutputIndex; -} - -uint64_t CoinsInfoImpl::globalOutputIndex() const -{ - return m_globalOutputIndex; -} - -bool CoinsInfoImpl::spent() const -{ - return m_spent; -} - -bool CoinsInfoImpl::frozen() const -{ - return m_frozen; -} - -uint64_t CoinsInfoImpl::spentHeight() const -{ - return m_spentHeight; -} - -uint64_t CoinsInfoImpl::amount() const -{ - return m_amount; -} - -bool CoinsInfoImpl::rct() const { - return m_rct; -} - -bool CoinsInfoImpl::keyImageKnown() const { - return m_keyImageKnown; -} - -size_t CoinsInfoImpl::pkIndex() const { - return m_pkIndex; -} - -uint32_t CoinsInfoImpl::subaddrIndex() const { - return m_subaddrIndex; -} - -uint32_t CoinsInfoImpl::subaddrAccount() const { - return m_subaddrAccount; -} - -string CoinsInfoImpl::address() const { - return m_address; -} - -string CoinsInfoImpl::addressLabel() const { - return m_addressLabel; -} - -string CoinsInfoImpl::keyImage() const { - return m_keyImage; -} - -uint64_t CoinsInfoImpl::unlockTime() const { - return m_unlockTime; -} - -bool CoinsInfoImpl::unlocked() const { - return m_unlocked; -} - -string CoinsInfoImpl::pubKey() const { - return m_pubKey; -} - -bool CoinsInfoImpl::coinbase() const { - return m_coinbase; -} - -string CoinsInfoImpl::description() const { - return m_description; -} -} // namespace - -namespace Bitmonero = Monero; diff --git a/src/wallet/api/coins_info.h b/src/wallet/api/coins_info.h deleted file mode 100644 index c43e45abd..000000000 --- a/src/wallet/api/coins_info.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef FEATHER_COINS_INFO_H -#define FEATHER_COINS_INFO_H - -#include "wallet/api/wallet2_api.h" -#include -#include - -namespace Monero { - -class CoinsImpl; - -class CoinsInfoImpl : public CoinsInfo -{ -public: - CoinsInfoImpl(); - ~CoinsInfoImpl(); - - virtual uint64_t blockHeight() const override; - virtual std::string hash() const override; - virtual size_t internalOutputIndex() const override; - virtual uint64_t globalOutputIndex() const override; - virtual bool spent() const override; - virtual bool frozen() const override; - virtual uint64_t spentHeight() const override; - virtual uint64_t amount() const override; - virtual bool rct() const override; - virtual bool keyImageKnown() const override; - virtual size_t pkIndex() const override; - virtual uint32_t subaddrIndex() const override; - virtual uint32_t subaddrAccount() const override; - virtual std::string address() const override; - virtual std::string addressLabel() const override; - virtual std::string keyImage() const override; - virtual uint64_t unlockTime() const override; - virtual bool unlocked() const override; - virtual std::string pubKey() const override; - virtual bool coinbase() const override; - virtual std::string description() const override; - -private: - uint64_t m_blockHeight; - std::string m_hash; - size_t m_internalOutputIndex; - uint64_t m_globalOutputIndex; - bool m_spent; - bool m_frozen; - uint64_t m_spentHeight; - uint64_t m_amount; - bool m_rct; - bool m_keyImageKnown; - size_t m_pkIndex; - uint32_t m_subaddrIndex; - uint32_t m_subaddrAccount; - std::string m_address; - std::string m_addressLabel; - std::string m_keyImage; - uint64_t m_unlockTime; - bool m_unlocked; - std::string m_pubKey; - bool m_coinbase; - std::string m_description; - - friend class CoinsImpl; - -}; - -} // namespace - -namespace Bitmonero = Monero; - -#endif //FEATHER_COINS_INFO_H diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 7f0f2e028..5ffc6b378 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -35,7 +35,6 @@ #include "transaction_history.h" #include "address_book.h" #include "subaddress.h" -#include "coins.h" #include "subaddress_account.h" #include "common_defines.h" #include "common/util.h" @@ -48,6 +47,7 @@ #include #include +#include using namespace std; using namespace cryptonote; @@ -436,7 +436,6 @@ WalletImpl::WalletImpl(NetworkType nettype, uint64_t kdf_rounds) m_refreshEnabled = false; m_addressBook.reset(new AddressBookImpl(this)); m_subaddress.reset(new SubaddressImpl(this)); - m_coins.reset(new CoinsImpl(this)); m_subaddressAccount.reset(new SubaddressAccountImpl(this)); @@ -1886,9 +1885,54 @@ AddressBook *WalletImpl::addressBook() return m_addressBook.get(); } -Coins *WalletImpl::coins() +std::vector WalletImpl::enotes() { - return m_coins.get(); + LOG_PRINT_L2("Refreshing coins"); + + boost::shared_lock transfers_lock(m_wallet->m_transfers_mutex); + + std::vector enotes; + + for (size_t i = 0; i < m_wallet->get_num_transfer_details(); ++i) + { + const tools::wallet2::transfer_details &td = m_wallet->get_transfer_details(i); + + Enote enote; + enote.idx = i; + enote.blockHeight = td.m_block_height; + enote.hash = epee::string_tools::pod_to_hex(td.m_txid); + enote.internalOutputIndex = td.m_internal_output_index; + enote.globalOutputIndex = td.m_global_output_index; + enote.spent = td.m_spent; + enote.frozen = td.m_frozen; + enote.spentHeight = td.m_spent_height; + enote.amount = td.m_amount; + enote.rct = td.m_rct; + enote.keyImageKnown = td.m_key_image_known; + enote.pkIndex = td.m_pk_index; + enote.subaddrIndex = td.m_subaddr_index.minor; + enote.subaddrAccount = td.m_subaddr_index.major; + enote.address = m_wallet->get_subaddress_as_str(td.m_subaddr_index); // todo: this is expensive, cache maybe? + enote.addressLabel = m_wallet->get_subaddress_label(td.m_subaddr_index); + enote.keyImage = epee::string_tools::pod_to_hex(td.m_key_image); + enote.unlockTime = td.m_tx.unlock_time; + enote.unlocked = m_wallet->is_transfer_unlocked(td); + enote.pubKey = epee::string_tools::pod_to_hex(td.get_public_key()); + enote.coinbase = td.m_tx.vin.size() == 1 && td.m_tx.vin[0].type() == typeid(cryptonote::txin_gen); + enote.description = m_wallet->get_tx_note(td.m_txid); + + enotes.push_back(enote); + } + + return enotes; +} + +void WalletImpl::freeze(size_t idx) { + m_wallet->freeze(idx); +} + +void WalletImpl::thaw(size_t idx) { + m_wallet->thaw(idx); } Subaddress *WalletImpl::subaddress() diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 3a426e889..7d9c972db 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -46,7 +46,6 @@ class PendingTransactionImpl; class UnsignedTransactionImpl; class AddressBookImpl; class SubaddressImpl; -class CoinsImpl; class SubaddressAccountImpl; struct Wallet2CallbackImpl; @@ -189,7 +188,9 @@ public: PendingTransaction::Priority priority) const override; virtual TransactionHistory * history() override; virtual AddressBook * addressBook() override; - virtual Coins * coins() override; + virtual std::vector enotes() override; + virtual void freeze(size_t idx) override; + virtual void thaw(size_t idx) override; virtual Subaddress * subaddress() override; virtual SubaddressAccount * subaddressAccount() override; virtual void setListener(WalletListener * l) override; @@ -260,7 +261,6 @@ private: friend class TransactionHistoryImpl; friend struct Wallet2CallbackImpl; friend class AddressBookImpl; - friend class CoinsImpl; friend class SubaddressImpl; friend class SubaddressAccountImpl; @@ -273,7 +273,6 @@ private: std::unique_ptr m_wallet2Callback; std::unique_ptr m_addressBook; std::unique_ptr m_subaddress; - std::unique_ptr m_coins; std::unique_ptr m_subaddressAccount; // multi-threaded refresh stuff diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h index 942b17d74..225c79fa5 100644 --- a/src/wallet/api/wallet2_api.h +++ b/src/wallet/api/wallet2_api.h @@ -262,48 +262,32 @@ struct AddressBook }; /** - * @brief The CoinsInfo - interface for displaying coins information + * @brief Enote - enote (utxo) information */ -struct CoinsInfo +struct Enote { - virtual ~CoinsInfo() = 0; - - virtual uint64_t blockHeight() const = 0; - virtual std::string hash() const = 0; - virtual size_t internalOutputIndex() const = 0; - virtual uint64_t globalOutputIndex() const = 0; - virtual bool spent() const = 0; - virtual bool frozen() const = 0; - virtual uint64_t spentHeight() const = 0; - virtual uint64_t amount() const = 0; - virtual bool rct() const = 0; - virtual bool keyImageKnown() const = 0; - virtual size_t pkIndex() const = 0; - virtual uint32_t subaddrIndex() const = 0; - virtual uint32_t subaddrAccount() const = 0; - virtual std::string address() const = 0; - virtual std::string addressLabel() const = 0; - virtual std::string keyImage() const = 0; - virtual uint64_t unlockTime() const = 0; - virtual bool unlocked() const = 0; - virtual std::string pubKey() const = 0; - virtual bool coinbase() const = 0; - virtual std::string description() const = 0; -}; - -struct Coins -{ - virtual ~Coins() = 0; - virtual int count() const = 0; - virtual CoinsInfo * coin(int index) const = 0; - virtual std::vector getAll() const = 0; - virtual void refresh() = 0; - virtual void setFrozen(std::string public_key) = 0; - virtual void setFrozen(int index) = 0; - virtual void thaw(std::string public_key) = 0; - virtual void thaw(int index) = 0; - virtual bool isTransferUnlocked(uint64_t unlockTime, uint64_t blockHeight) = 0; - virtual void setDescription(const std::string &public_key, const std::string &description) = 0; + size_t idx; + uint64_t blockHeight; + std::string hash; + size_t internalOutputIndex; + uint64_t globalOutputIndex; + bool spent; + bool frozen; + uint64_t spentHeight; + uint64_t amount; + bool rct; + bool keyImageKnown; + size_t pkIndex; + uint32_t subaddrIndex; + uint32_t subaddrAccount; + std::string address; + std::string addressLabel; + std::string keyImage; + uint64_t unlockTime; + bool unlocked; + std::string pubKey; + bool coinbase; + std::string description; }; struct SubaddressRow { @@ -989,7 +973,9 @@ struct Wallet virtual TransactionHistory * history() = 0; virtual AddressBook * addressBook() = 0; - virtual Coins * coins() = 0; + virtual std::vector enotes() = 0; + virtual void freeze(size_t idx) = 0; + virtual void thaw(size_t idx) = 0; virtual Subaddress * subaddress() = 0; virtual SubaddressAccount * subaddressAccount() = 0; virtual void setListener(WalletListener *) = 0;