Skip to content

Commit

Permalink
Remove direct bitcoin calls from qt/coincontroldialog.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanofsky authored and gades committed Mar 8, 2022
1 parent b159a6f commit b0bb6a5
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 81 deletions.
28 changes: 28 additions & 0 deletions src/interface/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <net_processing.h>
#include <netaddress.h>
#include <netbase.h>
#include <policy/feerate.h>
#include <policy/fees.h>
#include <policy/policy.h>
#include <primitives/block.h>
#include <rpc/server.h>
#include <scheduler.h>
Expand All @@ -29,6 +32,7 @@
#include <config/cosanta-config.h>
#endif
#ifdef ENABLE_WALLET
#include <wallet/fees.h>
#include <wallet/wallet.h>
#define CHECK_WALLET(x) x
#else
Expand Down Expand Up @@ -194,7 +198,31 @@ class NodeImpl : public Node
}
bool getNetworkActive() override { return g_connman && g_connman->GetNetworkActive(); }
unsigned int getTxConfirmTarget() override { CHECK_WALLET(return ::nTxConfirmTarget); }
CAmount getRequiredFee(unsigned int tx_bytes) override { CHECK_WALLET(return GetRequiredFee(tx_bytes)); }
CAmount getMinimumFee(unsigned int tx_bytes,
const CCoinControl& coin_control,
int* returned_target,
FeeReason* reason) override
{
FeeCalculation fee_calc;
CAmount result;
CHECK_WALLET(result = GetMinimumFee(tx_bytes, coin_control, ::mempool, ::feeEstimator, &fee_calc));
if (returned_target) *returned_target = fee_calc.returnedTarget;
if (reason) *reason = fee_calc.reason;
return result;
}
CAmount getMaxTxFee() override { return ::maxTxFee; }
CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override
{
FeeCalculation fee_calc;
CFeeRate result = ::feeEstimator.estimateSmartFee(num_blocks, &fee_calc, conservative);
if (returned_target) {
*returned_target = fee_calc.returnedTarget;
}
return result;
}
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
CFeeRate getPayTxFee() override { CHECK_WALLET(return ::payTxFee); }
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
{
JSONRPCRequest req;
Expand Down
21 changes: 21 additions & 0 deletions src/interface/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
#include <tuple>
#include <vector>

class CCoinControl;
class CDeterministicMNList;
class CFeeRate;
class CNodeStats;
class RPCTimerInterface;
class UniValue;
class proxyType;
enum class FeeReason;
struct CNodeStateStats;

namespace interface {
Expand Down Expand Up @@ -149,9 +152,27 @@ class Node
//! Get tx confirm target.
virtual unsigned int getTxConfirmTarget() = 0;

//! Get required fee.
virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;

//! Get minimum fee.
virtual CAmount getMinimumFee(unsigned int tx_bytes,
const CCoinControl& coin_control,
int* returned_target,
FeeReason* reason) = 0;

//! Get max tx fee.
virtual CAmount getMaxTxFee() = 0;

//! Estimate smart fee.
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) = 0;

//! Get dust relay fee.
virtual CFeeRate getDustRelayFee() = 0;

//! Get pay tx fee.
virtual CFeeRate getPayTxFee() = 0;

//! Execute rpc command.
virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;

Expand Down
41 changes: 41 additions & 0 deletions src/interface/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ class PendingWalletTxImpl : public PendingWalletTx
CReserveKey m_key;
};

//! Construct wallet TxOut struct.
WalletTxOut MakeWalletTxOut(CWallet& wallet, const CWalletTx& wtx, int n, int depth)
{
WalletTxOut result;
result.txout = wtx.tx->vout[n];
result.time = wtx.GetTxTime();
result.depth_in_main_chain = depth;
result.is_spent = wallet.IsSpent(wtx.GetHash(), n);
return result;
}

class WalletImpl : public Wallet
{
public:
Expand Down Expand Up @@ -220,6 +231,36 @@ class WalletImpl : public Wallet
return m_wallet.GetAvailableBalance(&coin_control);
}
}
CoinsList listCoins() override
{
LOCK2(::cs_main, m_wallet.cs_wallet);
CoinsList result;
for (const auto& entry : m_wallet.ListCoins()) {
auto& group = result[entry.first];
for (const auto& coin : entry.second) {
group.emplace_back(
COutPoint(coin.tx->GetHash(), coin.i), MakeWalletTxOut(m_wallet, *coin.tx, coin.i, coin.nDepth));
}
}
return result;
}
std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override
{
LOCK2(::cs_main, m_wallet.cs_wallet);
std::vector<WalletTxOut> result;
result.reserve(outputs.size());
for (const auto& output : outputs) {
result.emplace_back();
auto it = m_wallet.mapWallet.find(output.hash);
if (it != m_wallet.mapWallet.end()) {
int depth = it->second.GetDepthInMainChain();
if (depth >= 0) {
result.back() = MakeWalletTxOut(m_wallet, it->second, output.n, depth);
}
}
}
return result;
}
bool hdEnabled() override { return m_wallet.IsHDEnabled(); }
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
{
Expand Down
19 changes: 19 additions & 0 deletions src/interface/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory>
#include <stdint.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand All @@ -30,6 +31,7 @@ namespace interface {
class Handler;
class PendingWalletTx;
struct WalletBalances;
struct WalletTxOut;

using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
using WalletValueMap = std::map<std::string, std::string>;
Expand Down Expand Up @@ -162,6 +164,14 @@ class Wallet
//! Get available balance.
virtual CAmount getAvailableBalance(const CCoinControl& coin_control, bool fAnonymized = false) = 0;

//! Return AvailableCoins + LockedCoins grouped by wallet address.
//! (put change in one group with wallet address)
using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
virtual CoinsList listCoins() = 0;

//! Return wallet transaction output information.
virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;

// Return whether HD enabled.
virtual bool hdEnabled() = 0;

Expand Down Expand Up @@ -235,6 +245,15 @@ struct WalletBalances
}
};

//! Wallet transaction output.
struct WalletTxOut
{
CTxOut txout;
int64_t time;
int depth_in_main_chain = -1;
bool is_spent = false;
};

//! Return implementation of Wallet interface. This function will be undefined
//! in builds where ENABLE_WALLET is false.
std::unique_ptr<Wallet> MakeWallet(CWallet& wallet);
Expand Down

0 comments on commit b0bb6a5

Please sign in to comment.