Skip to content

Commit 5884558

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt transaction table files
1 parent 3cab2ce commit 5884558

16 files changed

+410
-210
lines changed

src/interface/node.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class NodeImpl : public Node
6060
void initLogging() override { InitLogging(); }
6161
void initParameterInteraction() override { InitParameterInteraction(); }
6262
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
63+
uint32_t getLogCategories() override { return ::logCategories; }
6364
bool baseInitialize() override
6465
{
6566
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
@@ -227,6 +228,11 @@ class NodeImpl : public Node
227228
std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
228229
void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) override { RPCSetTimerInterfaceIfUnset(iface); }
229230
void rpcUnsetTimerInterface(RPCTimerInterface* iface) override { RPCUnsetTimerInterface(iface); }
231+
bool getUnspentOutput(const COutPoint& output, Coin& coin) override
232+
{
233+
LOCK(::cs_main);
234+
return ::pcoinsTip->GetCoin(output, coin);
235+
}
230236
std::vector<std::unique_ptr<Wallet>> getWallets() override
231237
{
232238
#ifdef ENABLE_WALLET

src/interface/node.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class CCoinControl;
2323
class CFeeRate;
2424
class CNodeStats;
25+
class Coin;
2526
class RPCTimerInterface;
2627
class UniValue;
2728
class proxyType;
@@ -66,6 +67,9 @@ class Node
6667
//! Get warnings.
6768
virtual std::string getWarnings(const std::string& type) = 0;
6869

70+
// Get log flags.
71+
virtual uint32_t getLogCategories() = 0;
72+
6973
//! Initialize app dependencies.
7074
virtual bool baseInitialize() = 0;
7175

@@ -184,6 +188,9 @@ class Node
184188
//! Unset RPC timer interface.
185189
virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
186190

191+
//! Get unspent outputs associated with a transaction.
192+
virtual bool getUnspentOutput(const COutPoint& output, Coin& coin) = 0;
193+
187194
//! Return interfaces for accessing wallets (if any).
188195
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
189196

src/interface/wallet.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <script/standard.h>
1616
#include <support/allocators/secure.h>
1717
#include <sync.h>
18+
#include <timedata.h>
1819
#include <ui_interface.h>
1920
#include <uint256.h>
2021
#include <validation.h>
@@ -54,6 +55,54 @@ class PendingWalletTxImpl : public PendingWalletTx
5455
CReserveKey m_key;
5556
};
5657

58+
//! Construct wallet tx struct.
59+
WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
60+
{
61+
WalletTx result;
62+
result.tx = wtx.tx;
63+
result.txin_is_mine.reserve(wtx.tx->vin.size());
64+
for (const auto& txin : wtx.tx->vin) {
65+
result.txin_is_mine.emplace_back(wallet.IsMine(txin));
66+
}
67+
result.txout_is_mine.reserve(wtx.tx->vout.size());
68+
result.txout_address.reserve(wtx.tx->vout.size());
69+
result.txout_address_is_mine.reserve(wtx.tx->vout.size());
70+
for (const auto& txout : wtx.tx->vout) {
71+
result.txout_is_mine.emplace_back(wallet.IsMine(txout));
72+
result.txout_address.emplace_back();
73+
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
74+
IsMine(wallet, result.txout_address.back()) :
75+
ISMINE_NO);
76+
}
77+
result.credit = wtx.GetCredit(ISMINE_ALL);
78+
result.debit = wtx.GetDebit(ISMINE_ALL);
79+
result.change = wtx.GetChange();
80+
result.time = wtx.GetTxTime();
81+
result.value_map = wtx.mapValue;
82+
result.is_coinbase = wtx.IsCoinBase();
83+
return result;
84+
}
85+
86+
//! Construct wallet tx status struct.
87+
WalletTxStatus MakeWalletTxStatus(const CWalletTx& wtx)
88+
{
89+
WalletTxStatus result;
90+
auto mi = ::mapBlockIndex.find(wtx.hashBlock);
91+
CBlockIndex* block = mi != ::mapBlockIndex.end() ? mi->second : nullptr;
92+
result.block_height = (block ? block->nHeight : std::numeric_limits<int>::max()),
93+
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
94+
result.depth_in_main_chain = wtx.GetDepthInMainChain();
95+
result.request_count = wtx.GetRequestCount();
96+
result.time_received = wtx.nTimeReceived;
97+
result.lock_time = wtx.tx->nLockTime;
98+
result.is_final = CheckFinalTx(*wtx.tx);
99+
result.is_trusted = wtx.IsTrusted();
100+
result.is_abandoned = wtx.isAbandoned();
101+
result.is_coinbase = wtx.IsCoinBase();
102+
result.is_in_main_chain = wtx.IsInMainChain();
103+
return result;
104+
}
105+
57106
//! Construct wallet TxOut struct.
58107
WalletTxOut MakeWalletTxOut(CWallet& wallet, const CWalletTx& wtx, int n, int depth)
59108
{
@@ -205,6 +254,75 @@ class WalletImpl : public Wallet
205254
return feebumper::CommitTransaction(&m_wallet, txid, std::move(mtx), errors, bumped_txid) ==
206255
feebumper::Result::OK;
207256
}
257+
CTransactionRef getTx(const uint256& txid) override
258+
{
259+
LOCK2(::cs_main, m_wallet.cs_wallet);
260+
auto mi = m_wallet.mapWallet.find(txid);
261+
if (mi != m_wallet.mapWallet.end()) {
262+
return mi->second.tx;
263+
}
264+
return {};
265+
}
266+
WalletTx getWalletTx(const uint256& txid) override
267+
{
268+
LOCK2(::cs_main, m_wallet.cs_wallet);
269+
auto mi = m_wallet.mapWallet.find(txid);
270+
if (mi != m_wallet.mapWallet.end()) {
271+
return MakeWalletTx(m_wallet, mi->second);
272+
}
273+
return {};
274+
}
275+
std::vector<WalletTx> getWalletTxs() override
276+
{
277+
LOCK2(::cs_main, m_wallet.cs_wallet);
278+
std::vector<WalletTx> result;
279+
result.reserve(m_wallet.mapWallet.size());
280+
for (const auto& entry : m_wallet.mapWallet) {
281+
result.emplace_back(MakeWalletTx(m_wallet, entry.second));
282+
}
283+
return result;
284+
}
285+
bool tryGetTxStatus(const uint256& txid,
286+
interface::WalletTxStatus& tx_status,
287+
int& num_blocks,
288+
int64_t& adjusted_time) override
289+
{
290+
TRY_LOCK(::cs_main, locked_chain);
291+
if (!locked_chain) {
292+
return false;
293+
}
294+
TRY_LOCK(m_wallet.cs_wallet, locked_wallet);
295+
if (!locked_wallet) {
296+
return false;
297+
}
298+
auto mi = m_wallet.mapWallet.find(txid);
299+
if (mi == m_wallet.mapWallet.end()) {
300+
return false;
301+
}
302+
num_blocks = ::chainActive.Height();
303+
adjusted_time = GetAdjustedTime();
304+
tx_status = MakeWalletTxStatus(mi->second);
305+
return true;
306+
}
307+
WalletTx getWalletTxDetails(const uint256& txid,
308+
WalletTxStatus& tx_status,
309+
WalletOrderForm& order_form,
310+
bool& in_mempool,
311+
int& num_blocks,
312+
int64_t& adjusted_time) override
313+
{
314+
LOCK2(::cs_main, m_wallet.cs_wallet);
315+
auto mi = m_wallet.mapWallet.find(txid);
316+
if (mi != m_wallet.mapWallet.end()) {
317+
num_blocks = ::chainActive.Height();
318+
adjusted_time = GetAdjustedTime();
319+
in_mempool = mi->second.InMempool();
320+
order_form = mi->second.vOrderForm;
321+
tx_status = MakeWalletTxStatus(mi->second);
322+
return MakeWalletTx(m_wallet, mi->second);
323+
}
324+
return {};
325+
}
208326
WalletBalances getBalances() override
209327
{
210328
WalletBalances result;
@@ -236,6 +354,26 @@ class WalletImpl : public Wallet
236354
{
237355
return m_wallet.GetAvailableBalance(&coin_control);
238356
}
357+
isminetype txinIsMine(const CTxIn& txin) override
358+
{
359+
LOCK2(::cs_main, m_wallet.cs_wallet);
360+
return m_wallet.IsMine(txin);
361+
}
362+
isminetype txoutIsMine(const CTxOut& txout) override
363+
{
364+
LOCK2(::cs_main, m_wallet.cs_wallet);
365+
return m_wallet.IsMine(txout);
366+
}
367+
CAmount getDebit(const CTxIn& txin, isminefilter filter) override
368+
{
369+
LOCK2(::cs_main, m_wallet.cs_wallet);
370+
return m_wallet.GetDebit(txin, filter);
371+
}
372+
CAmount getCredit(const CTxOut& txout, isminefilter filter) override
373+
{
374+
LOCK2(::cs_main, m_wallet.cs_wallet);
375+
return m_wallet.GetCredit(txout, filter);
376+
}
239377
CoinsList listCoins() override
240378
{
241379
LOCK2(::cs_main, m_wallet.cs_wallet);

src/interface/wallet.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class Handler;
3333
class PendingWalletTx;
3434
struct WalletAddress;
3535
struct WalletBalances;
36+
struct WalletTx;
3637
struct WalletTxOut;
38+
struct WalletTxStatus;
3739

3840
using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
3941
using WalletValueMap = std::map<std::string, std::string>;
@@ -158,6 +160,29 @@ class Wallet
158160
std::vector<std::string>& errors,
159161
uint256& bumped_txid) = 0;
160162

163+
//! Get a transaction.
164+
virtual CTransactionRef getTx(const uint256& txid) = 0;
165+
166+
//! Get transaction information.
167+
virtual WalletTx getWalletTx(const uint256& txid) = 0;
168+
169+
//! Get list of all wallet transactions.
170+
virtual std::vector<WalletTx> getWalletTxs() = 0;
171+
172+
//! Try to get updated status for a particular transaction, if possible without blocking.
173+
virtual bool tryGetTxStatus(const uint256& txid,
174+
WalletTxStatus& tx_status,
175+
int& num_blocks,
176+
int64_t& adjusted_time) = 0;
177+
178+
//! Get transaction details.
179+
virtual WalletTx getWalletTxDetails(const uint256& txid,
180+
WalletTxStatus& tx_status,
181+
WalletOrderForm& order_form,
182+
bool& in_mempool,
183+
int& num_blocks,
184+
int64_t& adjusted_time) = 0;
185+
161186
//! Get balances.
162187
virtual WalletBalances getBalances() = 0;
163188

@@ -170,6 +195,18 @@ class Wallet
170195
//! Get available balance.
171196
virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
172197

198+
//! Return whether transaction input belongs to wallet.
199+
virtual isminetype txinIsMine(const CTxIn& txin) = 0;
200+
201+
//! Return whether transaction output belongs to wallet.
202+
virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
203+
204+
//! Return debit amount if transaction input belongs to wallet.
205+
virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
206+
207+
//! Return credit amount if transaction input belongs to wallet.
208+
virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
209+
173210
//! Return AvailableCoins + LockedCoins grouped by wallet address.
174211
//! (put change in one group with wallet address)
175212
using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
@@ -265,6 +302,38 @@ struct WalletBalances
265302
}
266303
};
267304

305+
// Wallet transaction information.
306+
struct WalletTx
307+
{
308+
CTransactionRef tx;
309+
std::vector<isminetype> txin_is_mine;
310+
std::vector<isminetype> txout_is_mine;
311+
std::vector<CTxDestination> txout_address;
312+
std::vector<isminetype> txout_address_is_mine;
313+
CAmount credit;
314+
CAmount debit;
315+
CAmount change;
316+
int64_t time;
317+
std::map<std::string, std::string> value_map;
318+
bool is_coinbase;
319+
};
320+
321+
//! Updated transaction status.
322+
struct WalletTxStatus
323+
{
324+
int block_height;
325+
int blocks_to_maturity;
326+
int depth_in_main_chain;
327+
int request_count;
328+
unsigned int time_received;
329+
uint32_t lock_time;
330+
bool is_final;
331+
bool is_trusted;
332+
bool is_abandoned;
333+
bool is_coinbase;
334+
bool is_in_main_chain;
335+
};
336+
268337
//! Wallet transaction output.
269338
struct WalletTxOut
270339
{

src/qt/bitcoin.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
#ifdef ENABLE_WALLET
3939
#include <wallet/init.h>
40-
#include <wallet/wallet.h>
4140
#endif
4241
#include <walletinitinterface.h>
4342

@@ -466,9 +465,8 @@ void BitcoinApplication::initializeResult(bool success)
466465
#ifdef ENABLE_WALLET
467466
bool fFirstWallet = true;
468467
auto wallets = m_node.getWallets();
469-
auto cwallet = ::vpwallets.begin();
470468
for (auto& wallet : wallets) {
471-
WalletModel * const walletModel = new WalletModel(std::move(wallet), m_node, platformStyle, *cwallet++, optionsModel);
469+
WalletModel * const walletModel = new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel);
472470

473471
window->addWallet(walletModel);
474472
if (fFirstWallet) {

src/qt/recentrequeststablemodel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
#include <streams.h>
1313

1414

15-
RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
15+
RecentRequestsTableModel::RecentRequestsTableModel(WalletModel *parent) :
1616
QAbstractTableModel(parent), walletModel(parent)
1717
{
18-
Q_UNUSED(wallet);
1918
nReceiveRequestsMaxId = 0;
2019

2120
// Load entries from wallet

src/qt/recentrequeststablemodel.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <QStringList>
1212
#include <QDateTime>
1313

14-
class CWallet;
15-
1614
class RecentRequestEntry
1715
{
1816
public:
@@ -60,7 +58,7 @@ class RecentRequestsTableModel: public QAbstractTableModel
6058
Q_OBJECT
6159

6260
public:
63-
explicit RecentRequestsTableModel(CWallet *wallet, WalletModel *parent);
61+
explicit RecentRequestsTableModel(WalletModel *parent);
6462
~RecentRequestsTableModel();
6563

6664
enum ColumnIndex {

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void TestGUI()
179179
auto node = interface::MakeNode();
180180
OptionsModel optionsModel(*node);
181181
vpwallets.insert(vpwallets.begin(), &wallet);
182-
WalletModel walletModel(std::move(node->getWallets()[0]), *node, platformStyle.get(), &wallet, &optionsModel);
182+
WalletModel walletModel(std::move(node->getWallets()[0]), *node, platformStyle.get(), &optionsModel);
183183
vpwallets.erase(vpwallets.begin());
184184
sendCoinsDialog.setModel(&walletModel);
185185
transactionView.setModel(&walletModel);

0 commit comments

Comments
 (0)