Skip to content

Commit

Permalink
Merge pull request #3886 from xdustinface/backport-11536
Browse files Browse the repository at this point in the history
backport: Deprecate accounts
  • Loading branch information
PastaPastaPasta authored Dec 30, 2020
2 parents a06eba3 + 96d3063 commit 9aaa230
Show file tree
Hide file tree
Showing 19 changed files with 1,038 additions and 599 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class WalletImpl : public Wallet
result.anonymized_balance = m_wallet.GetAnonymizedBalance();
result.have_watch_only = m_wallet.HaveWatchOnly();
if (result.have_watch_only) {
result.watch_only_balance = m_wallet.GetWatchOnlyBalance();
result.watch_only_balance = m_wallet.GetBalance(ISMINE_WATCH_ONLY);
result.unconfirmed_watch_only_balance = m_wallet.GetUnconfirmedWatchOnlyBalance();
result.immature_watch_only_balance = m_wallet.GetImmatureWatchOnlyBalance();
}
Expand Down
6 changes: 6 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getreceivedbyaddress", 2, "addlocked" },
{ "getreceivedbyaccount", 1, "minconf" },
{ "getreceivedbyaccount", 2, "addlocked" },
{ "getreceivedbylabel", 1, "minconf" },
{ "getreceivedbylabel", 2, "addlocked" },
{ "listaddressbalances", 0, "minamount" },
{ "listreceivedbyaddress", 0, "minconf" },
{ "listreceivedbyaddress", 1, "addlocked" },
Expand All @@ -56,6 +58,10 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listreceivedbyaccount", 1, "addlocked" },
{ "listreceivedbyaccount", 2, "include_empty" },
{ "listreceivedbyaccount", 3, "include_watchonly" },
{ "listreceivedbylabel", 0, "minconf" },
{ "listreceivedbylabel", 1, "addlocked" },
{ "listreceivedbylabel", 2, "include_empty" },
{ "listreceivedbylabel", 3, "include_watchonly" },
{ "getbalance", 1, "minconf" },
{ "getbalance", 2, "addlocked" },
{ "getbalance", 3, "include_watchonly" },
Expand Down
4 changes: 3 additions & 1 deletion src/rpc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enum RPCErrorCode
//! Wallet errors
RPC_WALLET_ERROR = -4, //!< Unspecified problem with wallet (key not found etc.)
RPC_WALLET_INSUFFICIENT_FUNDS = -6, //!< Not enough funds in wallet or account
RPC_WALLET_INVALID_ACCOUNT_NAME = -11, //!< Invalid account name
RPC_WALLET_INVALID_LABEL_NAME = -11, //!< Invalid label name
RPC_WALLET_KEYPOOL_RAN_OUT = -12, //!< Keypool ran out, call keypoolrefill first
RPC_WALLET_UNLOCK_NEEDED = -13, //!< Enter the wallet passphrase with walletpassphrase first
RPC_WALLET_PASSPHRASE_INCORRECT = -14, //!< The wallet passphrase entered was incorrect
Expand All @@ -87,6 +87,8 @@ enum RPCErrorCode
RPC_WALLET_NOT_SPECIFIED = -19, //!< No wallet specified (error when there are multiple wallets loaded)


//! Backwards compatible aliases
RPC_WALLET_INVALID_ACCOUNT_NAME = RPC_WALLET_INVALID_LABEL_NAME,
//! Unused reserved codes, kept around for backwards compatibility. Do not reuse.
RPC_FORBIDDEN_BY_SAFE_MODE = -2, //!< Server is in safe mode, and command is not allowed in safe mode
};
Expand Down
672 changes: 508 additions & 164 deletions src/wallet/rpcwallet.cpp

Large diffs are not rendered by default.

106 changes: 42 additions & 64 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,12 @@ bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmoun
return true;
}

bool CWallet::GetAccountDestination(CTxDestination &dest, std::string strAccount, bool bForceNew)
bool CWallet::GetLabelDestination(CTxDestination &dest, const std::string& label, bool bForceNew)
{
WalletBatch batch(*database);

CAccount account;
batch.ReadAccount(strAccount, account);
batch.ReadAccount(label, account);

if (!bForceNew) {
if (!account.vchPubKey.IsValid())
Expand All @@ -1071,8 +1071,8 @@ bool CWallet::GetAccountDestination(CTxDestination &dest, std::string strAccount
return false;

dest = account.vchPubKey.GetID();
SetAddressBook(dest, strAccount, "receive");
batch.WriteAccount(strAccount, account);
SetAddressBook(dest, label, "receive");
batch.WriteAccount(label, account);
} else {
dest = account.vchPubKey.GetID();
}
Expand Down Expand Up @@ -2333,7 +2333,7 @@ CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
return 0;
}

CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter) const
{
if (pwallet == nullptr)
return 0;
Expand All @@ -2342,8 +2342,20 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
if (IsCoinBase() && GetBlocksToMaturity() > 0)
return 0;

if (fUseCache && fAvailableCreditCached)
return nAvailableCreditCached;
CAmount* cache = nullptr;
bool* cache_used = nullptr;

if (filter == ISMINE_SPENDABLE) {
cache = &nAvailableCreditCached;
cache_used = &fAvailableCreditCached;
} else if (filter == ISMINE_WATCH_ONLY) {
cache = &nAvailableWatchCreditCached;
cache_used = &fAvailableWatchCreditCached;
}

if (fUseCache && cache_used && *cache_used) {
return *cache;
}

CAmount nCredit = 0;
uint256 hashTx = GetHash();
Expand All @@ -2352,14 +2364,16 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
if (!pwallet->IsSpent(hashTx, i))
{
const CTxOut &txout = tx->vout[i];
nCredit += pwallet->GetCredit(txout, ISMINE_SPENDABLE);
nCredit += pwallet->GetCredit(txout, filter);
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
}

nAvailableCreditCached = nCredit;
fAvailableCreditCached = true;
if (cache) {
*cache = nCredit;
*cache_used = true;
}
return nCredit;
}

Expand All @@ -2377,35 +2391,6 @@ CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const
return 0;
}

CAmount CWalletTx::GetAvailableWatchOnlyCredit(const bool fUseCache) const
{
if (pwallet == nullptr)
return 0;

// Must wait until coinbase is safely deep enough in the chain before valuing it
if (IsCoinBase() && GetBlocksToMaturity() > 0)
return 0;

if (fUseCache && fAvailableWatchCreditCached)
return nAvailableWatchCreditCached;

CAmount nCredit = 0;
for (unsigned int i = 0; i < tx->vout.size(); i++)
{
if (!pwallet->IsSpent(GetHash(), i))
{
const CTxOut &txout = tx->vout[i];
nCredit += pwallet->GetCredit(txout, ISMINE_WATCH_ONLY);
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
}

nAvailableWatchCreditCached = nCredit;
fAvailableWatchCreditCached = true;
return nCredit;
}

CAmount CWalletTx::GetAnonymizedCredit(const CCoinControl* coinControl) const
{
if (!pwallet)
Expand Down Expand Up @@ -2627,14 +2612,15 @@ std::unordered_set<const CWalletTx*, WalletTxHasher> CWallet::GetSpendableTXs()
return ret;
}

CAmount CWallet::GetBalance() const
CAmount CWallet::GetBalance(const isminefilter& filter, const int min_depth, const bool fAddLocked) const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableCredit();
if (pcoin->IsTrusted() && ((pcoin->GetDepthInMainChain() >= min_depth) || (fAddLocked && pcoin->IsLockedByInstantSend()))) {
nTotal += pcoin->GetAvailableCredit(true, filter);
}
}
}

Expand Down Expand Up @@ -2764,28 +2750,14 @@ CAmount CWallet::GetImmatureBalance() const
return nTotal;
}

CAmount CWallet::GetWatchOnlyBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableWatchOnlyCredit();
}
}

return nTotal;
}

CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && !pcoin->IsLockedByInstantSend() && pcoin->InMempool())
nTotal += pcoin->GetAvailableWatchOnlyCredit();
nTotal += pcoin->GetAvailableCredit(true, ISMINE_WATCH_ONLY);
}
}
return nTotal;
Expand Down Expand Up @@ -2828,7 +2800,7 @@ CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, cons
for (const CTxOut& out : wtx.tx->vout) {
if (outgoing && IsChange(out)) {
debit -= out.nValue;
} else if (IsMine(out) & filter && (depth >= minDepth || (fAddLocked && wtx.IsLockedByInstantSend())) && (!account || *account == GetAccountName(out.scriptPubKey))) {
} else if (IsMine(out) & filter && (depth >= minDepth || (fAddLocked && wtx.IsLockedByInstantSend())) && (!account || *account == GetLabelName(out.scriptPubKey))) {
balance += out.nValue;
}
}
Expand Down Expand Up @@ -4279,7 +4251,7 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
return WalletBatch(*database).EraseName(EncodeDestination(address));
}

const std::string& CWallet::GetAccountName(const CScript& scriptPubKey) const
const std::string& CWallet::GetLabelName(const CScript& scriptPubKey) const
{
CTxDestination address;
if (ExtractDestination(scriptPubKey, address) && !scriptPubKey.IsUnspendable()) {
Expand All @@ -4289,9 +4261,9 @@ const std::string& CWallet::GetAccountName(const CScript& scriptPubKey) const
}
}
// A scriptPubKey that doesn't have an entry in the address book is
// associated with the default account ("").
const static std::string DEFAULT_ACCOUNT_NAME;
return DEFAULT_ACCOUNT_NAME;
// associated with the default label ("").
const static std::string DEFAULT_LABEL_NAME;
return DEFAULT_LABEL_NAME;
}

/**
Expand Down Expand Up @@ -4671,20 +4643,26 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
return ret;
}

std::set<CTxDestination> CWallet::GetAccountAddresses(const std::string& strAccount) const
std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) const
{
LOCK(cs_wallet);
std::set<CTxDestination> result;
for (const std::pair<const CTxDestination, CAddressBookData>& item : mapAddressBook)
{
const CTxDestination& address = item.first;
const std::string& strName = item.second.name;
if (strName == strAccount)
if (strName == label)
result.insert(address);
}
return result;
}

void CWallet::DeleteLabel(const std::string& label)
{
WalletBatch batch(*database);
batch.EraseAccount(label);
}

bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool fInternalIn)
{
if (nIndex == -1)
Expand Down
17 changes: 8 additions & 9 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,8 @@ class CWalletTx : public CMerkleTx
CAmount GetDebit(const isminefilter& filter) const;
CAmount GetCredit(const isminefilter& filter) const;
CAmount GetImmatureCredit(bool fUseCache=true) const;
CAmount GetAvailableCredit(bool fUseCache=true) const;
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const;
CAmount GetImmatureWatchOnlyCredit(const bool fUseCache=true) const;
CAmount GetAvailableWatchOnlyCredit(const bool fUseCache=true) const;
CAmount GetChange() const;

CAmount GetAnonymizedCredit(const CCoinControl* coinControl = nullptr) const;
Expand Down Expand Up @@ -602,7 +601,7 @@ class CWalletKey
};

/**
* Internal transfers.
* DEPRECATED Internal transfers.
* Database key is acentry<account><counter>.
*/
class CAccountingEntry
Expand Down Expand Up @@ -1031,7 +1030,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
int64_t IncOrderPosNext(WalletBatch *batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
DBErrors ReorderTransactions();
bool AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment = "") EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool GetAccountDestination(CTxDestination &dest, std::string strAccount, bool bForceNew = false);
bool GetLabelDestination(CTxDestination &dest, const std::string& label, bool bForceNew = false);

void MarkDirty();
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose=true);
Expand All @@ -1047,10 +1046,9 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override;
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman);
CAmount GetBalance() const;
CAmount GetBalance(const isminefilter& filter=ISMINE_SPENDABLE, const int min_depth=0, bool fAddLocked = false) const;
CAmount GetUnconfirmedBalance() const;
CAmount GetImmatureBalance() const;
CAmount GetWatchOnlyBalance() const;
CAmount GetUnconfirmedWatchOnlyBalance() const;
CAmount GetImmatureWatchOnlyBalance() const;
CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const std::string* account, const bool fAddLocked) const;
Expand Down Expand Up @@ -1116,7 +1114,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
std::set<std::set<CTxDestination>> GetAddressGroupings() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
std::map<CTxDestination, CAmount> GetAddressBalances();

std::set<CTxDestination> GetAccountAddresses(const std::string& strAccount) const;
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const;
void DeleteLabel(const std::string& label);

isminetype IsMine(const CTxIn& txin) const;
/**
Expand Down Expand Up @@ -1147,7 +1146,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface

bool DelAddressBook(const CTxDestination& address);

const std::string& GetAccountName(const CScript& scriptPubKey) const;
const std::string& GetLabelName(const CScript& scriptPubKey) const;

void GetScriptForMining(std::shared_ptr<CReserveScript> &script);

Expand Down Expand Up @@ -1304,7 +1303,7 @@ class CReserveKey final : public CReserveScript


/**
* Account information.
* DEPRECATED Account information.
* Stored in wallet with key "acc"+string account name.
*/
class CAccount
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ bool WalletBatch::WriteAccount(const std::string& strAccount, const CAccount& ac
return WriteIC(std::make_pair(std::string("acc"), strAccount), account);
}

bool WalletBatch::EraseAccount(const std::string& strAccount)
{
return EraseIC(std::make_pair(std::string("acc"), strAccount));
}

bool WalletBatch::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
{
return WriteIC(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class WalletBatch
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
bool ReadAccount(const std::string& strAccount, CAccount& account);
bool WriteAccount(const std::string& strAccount, const CAccount& account);
bool EraseAccount(const std::string& strAccount);

bool ReadPrivateSendSalt(uint256& salt);
bool WritePrivateSendSalt(const uint256& salt);
Expand Down
Loading

0 comments on commit 9aaa230

Please sign in to comment.