Changeset View
Changeset View
Standalone View
Standalone View
src/interfaces/wallet.cpp
Show All 21 Lines | |||||
#include <wallet/rpcdump.h> | #include <wallet/rpcdump.h> | ||||
#include <wallet/rpcwallet.h> | #include <wallet/rpcwallet.h> | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
namespace interfaces { | namespace interfaces { | ||||
namespace { | namespace { | ||||
//! Construct wallet tx struct. | //! Construct wallet tx struct. | ||||
static WalletTx MakeWalletTx(interfaces::Chain::Lock &locked_chain, | static WalletTx MakeWalletTx(CWallet &wallet, const CWalletTx &wtx) { | ||||
CWallet &wallet, const CWalletTx &wtx) { | |||||
WalletTx result; | WalletTx result; | ||||
result.tx = wtx.tx; | result.tx = wtx.tx; | ||||
result.txin_is_mine.reserve(wtx.tx->vin.size()); | result.txin_is_mine.reserve(wtx.tx->vin.size()); | ||||
for (const auto &txin : wtx.tx->vin) { | for (const auto &txin : wtx.tx->vin) { | ||||
result.txin_is_mine.emplace_back(wallet.IsMine(txin)); | result.txin_is_mine.emplace_back(wallet.IsMine(txin)); | ||||
} | } | ||||
result.txout_is_mine.reserve(wtx.tx->vout.size()); | result.txout_is_mine.reserve(wtx.tx->vout.size()); | ||||
result.txout_address.reserve(wtx.tx->vout.size()); | result.txout_address.reserve(wtx.tx->vout.size()); | ||||
result.txout_address_is_mine.reserve(wtx.tx->vout.size()); | result.txout_address_is_mine.reserve(wtx.tx->vout.size()); | ||||
for (const auto &txout : wtx.tx->vout) { | for (const auto &txout : wtx.tx->vout) { | ||||
result.txout_is_mine.emplace_back(wallet.IsMine(txout)); | result.txout_is_mine.emplace_back(wallet.IsMine(txout)); | ||||
result.txout_address.emplace_back(); | result.txout_address.emplace_back(); | ||||
result.txout_address_is_mine.emplace_back( | result.txout_address_is_mine.emplace_back( | ||||
ExtractDestination(txout.scriptPubKey, | ExtractDestination(txout.scriptPubKey, | ||||
result.txout_address.back()) | result.txout_address.back()) | ||||
? wallet.IsMine(result.txout_address.back()) | ? wallet.IsMine(result.txout_address.back()) | ||||
: ISMINE_NO); | : ISMINE_NO); | ||||
} | } | ||||
result.credit = wtx.GetCredit(locked_chain, ISMINE_ALL); | result.credit = wtx.GetCredit(ISMINE_ALL); | ||||
result.debit = wtx.GetDebit(ISMINE_ALL); | result.debit = wtx.GetDebit(ISMINE_ALL); | ||||
result.change = wtx.GetChange(); | result.change = wtx.GetChange(); | ||||
result.time = wtx.GetTxTime(); | result.time = wtx.GetTxTime(); | ||||
result.value_map = wtx.mapValue; | result.value_map = wtx.mapValue; | ||||
result.is_coinbase = wtx.IsCoinBase(); | result.is_coinbase = wtx.IsCoinBase(); | ||||
return result; | return result; | ||||
} | } | ||||
//! Construct wallet tx status struct. | //! Construct wallet tx status struct. | ||||
static WalletTxStatus | static WalletTxStatus | ||||
MakeWalletTxStatus(interfaces::Chain::Lock &locked_chain, | MakeWalletTxStatus(interfaces::Chain::Lock &locked_chain, | ||||
const CWalletTx &wtx) { | const CWalletTx &wtx) { | ||||
WalletTxStatus result; | WalletTxStatus result; | ||||
result.block_height = | result.block_height = | ||||
locked_chain.getBlockHeight(wtx.m_confirm.hashBlock) | locked_chain.getBlockHeight(wtx.m_confirm.hashBlock) | ||||
.get_value_or(std::numeric_limits<int>::max()); | .get_value_or(std::numeric_limits<int>::max()); | ||||
result.blocks_to_maturity = wtx.GetBlocksToMaturity(locked_chain); | result.blocks_to_maturity = wtx.GetBlocksToMaturity(); | ||||
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain); | result.depth_in_main_chain = wtx.GetDepthInMainChain(); | ||||
result.time_received = wtx.nTimeReceived; | result.time_received = wtx.nTimeReceived; | ||||
result.lock_time = wtx.tx->nLockTime; | result.lock_time = wtx.tx->nLockTime; | ||||
TxValidationState state; | TxValidationState state; | ||||
result.is_final = | result.is_final = | ||||
locked_chain.contextualCheckTransactionForCurrentBlock( | locked_chain.contextualCheckTransactionForCurrentBlock( | ||||
Params().GetConsensus(), *wtx.tx, state); | Params().GetConsensus(), *wtx.tx, state); | ||||
result.is_trusted = wtx.IsTrusted(locked_chain); | result.is_trusted = wtx.IsTrusted(locked_chain); | ||||
result.is_abandoned = wtx.isAbandoned(); | result.is_abandoned = wtx.isAbandoned(); | ||||
result.is_coinbase = wtx.IsCoinBase(); | result.is_coinbase = wtx.IsCoinBase(); | ||||
result.is_in_main_chain = wtx.IsInMainChain(locked_chain); | result.is_in_main_chain = wtx.IsInMainChain(); | ||||
return result; | return result; | ||||
} | } | ||||
//! Construct wallet TxOut struct. | //! Construct wallet TxOut struct. | ||||
static WalletTxOut MakeWalletTxOut(interfaces::Chain::Lock &locked_chain, | static WalletTxOut MakeWalletTxOut(CWallet &wallet, const CWalletTx &wtx, | ||||
CWallet &wallet, const CWalletTx &wtx, | |||||
int n, int depth) | int n, int depth) | ||||
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) { | EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) { | ||||
WalletTxOut result; | WalletTxOut result; | ||||
result.txout = wtx.tx->vout[n]; | result.txout = wtx.tx->vout[n]; | ||||
result.time = wtx.GetTxTime(); | result.time = wtx.GetTxTime(); | ||||
result.depth_in_main_chain = depth; | result.depth_in_main_chain = depth; | ||||
result.is_spent = | result.is_spent = wallet.IsSpent(COutPoint(wtx.GetId(), n)); | ||||
wallet.IsSpent(locked_chain, COutPoint(wtx.GetId(), n)); | |||||
return result; | return result; | ||||
} | } | ||||
class WalletImpl : public Wallet { | class WalletImpl : public Wallet { | ||||
public: | public: | ||||
explicit WalletImpl(const std::shared_ptr<CWallet> &wallet) | explicit WalletImpl(const std::shared_ptr<CWallet> &wallet) | ||||
: m_wallet(wallet) {} | : m_wallet(wallet) {} | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | public: | ||||
std::move(order_form)); | std::move(order_form)); | ||||
} | } | ||||
bool transactionCanBeAbandoned(const TxId &txid) override { | bool transactionCanBeAbandoned(const TxId &txid) override { | ||||
return m_wallet->TransactionCanBeAbandoned(txid); | return m_wallet->TransactionCanBeAbandoned(txid); | ||||
} | } | ||||
bool abandonTransaction(const TxId &txid) override { | bool abandonTransaction(const TxId &txid) override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
return m_wallet->AbandonTransaction(*locked_chain, txid); | return m_wallet->AbandonTransaction(txid); | ||||
} | } | ||||
CTransactionRef getTx(const TxId &txid) override { | CTransactionRef getTx(const TxId &txid) override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
auto mi = m_wallet->mapWallet.find(txid); | auto mi = m_wallet->mapWallet.find(txid); | ||||
if (mi != m_wallet->mapWallet.end()) { | if (mi != m_wallet->mapWallet.end()) { | ||||
return mi->second.tx; | return mi->second.tx; | ||||
} | } | ||||
return {}; | return {}; | ||||
} | } | ||||
WalletTx getWalletTx(const TxId &txid) override { | WalletTx getWalletTx(const TxId &txid) override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
auto mi = m_wallet->mapWallet.find(txid); | auto mi = m_wallet->mapWallet.find(txid); | ||||
if (mi != m_wallet->mapWallet.end()) { | if (mi != m_wallet->mapWallet.end()) { | ||||
return MakeWalletTx(*locked_chain, *m_wallet, mi->second); | return MakeWalletTx(*m_wallet, mi->second); | ||||
} | } | ||||
return {}; | return {}; | ||||
} | } | ||||
std::vector<WalletTx> getWalletTxs() override { | std::vector<WalletTx> getWalletTxs() override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
std::vector<WalletTx> result; | std::vector<WalletTx> result; | ||||
result.reserve(m_wallet->mapWallet.size()); | result.reserve(m_wallet->mapWallet.size()); | ||||
for (const auto &entry : m_wallet->mapWallet) { | for (const auto &entry : m_wallet->mapWallet) { | ||||
result.emplace_back( | result.emplace_back(MakeWalletTx(*m_wallet, entry.second)); | ||||
MakeWalletTx(*locked_chain, *m_wallet, entry.second)); | |||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
bool tryGetTxStatus(const TxId &txid, | bool tryGetTxStatus(const TxId &txid, | ||||
interfaces::WalletTxStatus &tx_status, | interfaces::WalletTxStatus &tx_status, | ||||
int &num_blocks, int64_t &block_time) override { | int &num_blocks, int64_t &block_time) override { | ||||
auto locked_chain = m_wallet->chain().lock(true /* try_lock */); | auto locked_chain = m_wallet->chain().lock(true /* try_lock */); | ||||
if (!locked_chain) { | if (!locked_chain) { | ||||
Show All 24 Lines | public: | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
auto mi = m_wallet->mapWallet.find(txid); | auto mi = m_wallet->mapWallet.find(txid); | ||||
if (mi != m_wallet->mapWallet.end()) { | if (mi != m_wallet->mapWallet.end()) { | ||||
num_blocks = locked_chain->getHeight().value_or(-1); | num_blocks = locked_chain->getHeight().value_or(-1); | ||||
in_mempool = mi->second.InMempool(); | in_mempool = mi->second.InMempool(); | ||||
order_form = mi->second.vOrderForm; | order_form = mi->second.vOrderForm; | ||||
tx_status = MakeWalletTxStatus(*locked_chain, mi->second); | tx_status = MakeWalletTxStatus(*locked_chain, mi->second); | ||||
return MakeWalletTx(*locked_chain, *m_wallet, mi->second); | return MakeWalletTx(*m_wallet, mi->second); | ||||
} | } | ||||
return {}; | return {}; | ||||
} | } | ||||
WalletBalances getBalances() override { | WalletBalances getBalances() override { | ||||
const auto bal = m_wallet->GetBalance(); | const auto bal = m_wallet->GetBalance(); | ||||
WalletBalances result; | WalletBalances result; | ||||
result.balance = bal.m_mine_trusted; | result.balance = bal.m_mine_trusted; | ||||
result.unconfirmed_balance = bal.m_mine_untrusted_pending; | result.unconfirmed_balance = bal.m_mine_untrusted_pending; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | public: | ||||
CoinsList listCoins() override { | CoinsList listCoins() override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
CoinsList result; | CoinsList result; | ||||
for (const auto &entry : m_wallet->ListCoins(*locked_chain)) { | for (const auto &entry : m_wallet->ListCoins(*locked_chain)) { | ||||
auto &group = result[entry.first]; | auto &group = result[entry.first]; | ||||
for (const auto &coin : entry.second) { | for (const auto &coin : entry.second) { | ||||
group.emplace_back(COutPoint(coin.tx->GetId(), coin.i), | group.emplace_back(COutPoint(coin.tx->GetId(), coin.i), | ||||
MakeWalletTxOut(*locked_chain, *m_wallet, | MakeWalletTxOut(*m_wallet, *coin.tx, | ||||
*coin.tx, coin.i, | coin.i, coin.nDepth)); | ||||
coin.nDepth)); | |||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
std::vector<WalletTxOut> | std::vector<WalletTxOut> | ||||
getCoins(const std::vector<COutPoint> &outputs) override { | getCoins(const std::vector<COutPoint> &outputs) override { | ||||
auto locked_chain = m_wallet->chain().lock(); | auto locked_chain = m_wallet->chain().lock(); | ||||
LOCK(m_wallet->cs_wallet); | LOCK(m_wallet->cs_wallet); | ||||
std::vector<WalletTxOut> result; | std::vector<WalletTxOut> result; | ||||
result.reserve(outputs.size()); | result.reserve(outputs.size()); | ||||
for (const auto &output : outputs) { | for (const auto &output : outputs) { | ||||
result.emplace_back(); | result.emplace_back(); | ||||
auto it = m_wallet->mapWallet.find(output.GetTxId()); | auto it = m_wallet->mapWallet.find(output.GetTxId()); | ||||
if (it != m_wallet->mapWallet.end()) { | if (it != m_wallet->mapWallet.end()) { | ||||
int depth = it->second.GetDepthInMainChain(*locked_chain); | int depth = it->second.GetDepthInMainChain(); | ||||
if (depth >= 0) { | if (depth >= 0) { | ||||
result.back() = | result.back() = MakeWalletTxOut(*m_wallet, it->second, | ||||
MakeWalletTxOut(*locked_chain, *m_wallet, | output.GetN(), depth); | ||||
it->second, output.GetN(), depth); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
bool hdEnabled() override { return m_wallet->IsHDEnabled(); } | bool hdEnabled() override { return m_wallet->IsHDEnabled(); } | ||||
OutputType getDefaultAddressType() override { | OutputType getDefaultAddressType() override { | ||||
return m_wallet->m_default_address_type; | return m_wallet->m_default_address_type; | ||||
▲ Show 20 Lines • Show All 102 Lines • Show Last 20 Lines |