Skip to content

Commit

Permalink
Merge #439: Add decoration for name renewals
Browse files Browse the repository at this point in the history
18a97b1 Namecoin / Qt: Decorate renewal transactions (Jeremy Rand)
a5ef5c5 Namecoin: Add WalletTx::name_debit (Jeremy Rand)
5080edb Namecoin: Add WalletTx::name_credit (Jeremy Rand)

Pull request description:

  Lays the groundwork for #434

ACKs for top commit:
  domob1812:
    ACK 18a97b1 with one nit.  Thanks!

Tree-SHA512: c952c57ab62787302c05ebe6a315a697df5495e5121662f28ac9fe07ee59e5a8e4ea739631809428ae4b9b32dddfc1905b1ecb8f295e4a218d621c16acc7dae0
  • Loading branch information
domob1812 committed Aug 10, 2021
2 parents 83baa3b + 18a97b1 commit 6e0d254
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <amount.h> // For CAmount
#include <interfaces/chain.h> // For ChainClient
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
#include <script/names.h> // For CNameScript
#include <script/standard.h> // For CTxDestination
#include <support/allocators/secure.h> // For SecureString
#include <util/message.h>
Expand Down Expand Up @@ -379,6 +380,8 @@ struct WalletTx
CAmount credit;
CAmount debit;
CAmount change;
std::optional<CNameScript> name_credit;
std::optional<CNameScript> name_debit;
int64_t time;
std::map<std::string, std::string> value_map;
bool is_coinbase;
Expand Down
33 changes: 12 additions & 21 deletions src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,25 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface

CAmount nChange = wtx.change;

// If we find a name script, we put it here.
bool foundNameOp = false;
CNameScript nameScript;
std::optional<CNameScript> nNameCredit = wtx.name_credit;
std::optional<CNameScript> nNameDebit = wtx.name_debit;

// TODO: Maybe move this loop into a convenience function.
for (const CTxOut& txout : wtx.tx->vout)
{
// check txout for nameop
const CNameScript maybeNameScript(txout.scriptPubKey);
if(maybeNameScript.isNameOp())
{
foundNameOp = true;
nameScript = maybeNameScript;

break;
}
}

if(foundNameOp)
if(nNameCredit)
{
// TODO: Use "Pre-Registration" / "Registration" / "Update" strings
std::string opName = GetOpName(nameScript.getNameOp());
std::string opName = GetOpName(nNameCredit.value().getNameOp());
std::string description = opName.substr(3);

// TODO: Use friendly names based on namespaces
if(nameScript.isAnyUpdate())
if(nNameCredit.value().isAnyUpdate())
{
description += " " + EncodeNameForMessage(nameScript.getOpName());
// Check if renewal (previous value is unchanged)
if(nNameDebit && nNameDebit.value().isAnyUpdate() && nNameDebit.value().getOpValue() == nNameCredit.value().getOpValue())
{
description = "NAME_RENEW";
}

description += " " + EncodeNameForMessage(nNameCredit.value().getOpName());
}

parts.append(TransactionRecord(hash, nTime, TransactionRecord::NameOp, description, -(nDebit - nChange), nCredit - nChange));
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
result.credit = wtx.GetCredit(ISMINE_ALL);
result.debit = wtx.GetDebit(ISMINE_ALL);
result.change = wtx.GetChange();
result.name_credit = wtx.GetNameCredit(ISMINE_ALL);
result.name_debit = wtx.GetNameDebit(ISMINE_ALL);
result.time = wtx.GetTxTime();
result.value_map = wtx.mapValue;
result.is_coinbase = wtx.IsCoinBase();
Expand Down
33 changes: 33 additions & 0 deletions src/wallet/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ CAmount CWallet::GetCredit(const CTransaction& tx, const isminefilter& filter) c
return nCredit;
}

std::optional<CNameScript> CWallet::GetNameCredit(const CTxOut& txout, const isminefilter& filter) const
{
CNameScript op(txout.scriptPubKey);
if (!op.isNameOp ())
return {};
LOCK(cs_wallet);
return ((IsMine(txout) & filter) ? std::optional<CNameScript>{op} : std::nullopt);
}

std::optional<CNameScript> CWallet::GetNameCredit(const CTransaction& tx, const isminefilter& filter) const
{
std::optional<CNameScript> nCredit;
for (const CTxOut& txout : tx.vout)
{
nCredit = GetNameCredit(txout, filter);
if (nCredit)
break;
}
return nCredit;
}

bool CWallet::IsChange(const CScript& script) const
{
// TODO: fix handling of 'change' outputs. The assumption is that any
Expand Down Expand Up @@ -140,6 +161,12 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
return credit;
}

std::optional<CNameScript> CWalletTx::GetNameCredit(const isminefilter& filter) const
{
// TODO: Caching like what GetCredit does.
return pwallet->GetNameCredit(*tx, filter);
}

CAmount CWalletTx::GetDebit(const isminefilter& filter) const
{
if (tx->vin.empty())
Expand All @@ -155,6 +182,12 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
return debit;
}

std::optional<CNameScript> CWalletTx::GetNameDebit(const isminefilter& filter) const
{
// TODO: Caching like what GetDebit does.
return pwallet->GetNameDebit(*tx, filter);
}

CAmount CWalletTx::GetChange() const
{
if (fChangeCached)
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <amount.h>
#include <primitives/transaction.h>
#include <script/names.h>
#include <serialize.h>
#include <wallet/ismine.h>
#include <threadsafety.h>
Expand Down Expand Up @@ -275,6 +276,8 @@ class CWalletTx
CAmount GetAvailableCredit(bool fUseCache = true, const isminefilter& filter = ISMINE_SPENDABLE) const NO_THREAD_SAFETY_ANALYSIS;
CAmount GetImmatureWatchOnlyCredit(const bool fUseCache = true) const;
CAmount GetChange() const;
std::optional<CNameScript> GetNameDebit(const isminefilter& filter) const;
std::optional<CNameScript> GetNameCredit(const isminefilter& filter) const;

/** Get the marginal bytes if spending the specified output from this transaction */
int GetSpendSize(unsigned int out, bool use_max_sig = false) const
Expand Down
37 changes: 37 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,31 @@ CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
return 0;
}

std::optional<CNameScript> CWallet::GetNameDebit(const CTxIn &txin, const isminefilter& filter) const
{
{
LOCK(cs_wallet);
std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
if (mi != mapWallet.end())
{
const CWalletTx& prev = (*mi).second;
if (txin.prevout.n < prev.tx->vout.size())
{
const CTxOut& prevout = prev.tx->vout[txin.prevout.n];

CNameScript op(prevout.scriptPubKey);

if (!op.isNameOp ())
return {};

if (IsMine(prevout) & filter)
return op;
}
}
}
return {};
}

isminetype CWallet::IsMine(const CTxOut& txout) const
{
AssertLockHeld(cs_wallet);
Expand Down Expand Up @@ -1373,6 +1398,18 @@ CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) co
return nDebit;
}

std::optional<CNameScript> CWallet::GetNameDebit(const CTransaction& tx, const isminefilter& filter) const
{
std::optional<CNameScript> nDebit;
for (const CTxIn& txin : tx.vin)
{
nDebit = GetNameDebit(txin, filter);
if (nDebit)
break;
}
return nDebit;
}

bool CWallet::IsHDEnabled() const
{
// All Active ScriptPubKeyMans must be HD for this to be true
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <outputtype.h>
#include <policy/feerate.h>
#include <psbt.h>
#include <script/names.h>
#include <tinyformat.h>
#include <util/message.h>
#include <util/strencodings.h>
Expand Down Expand Up @@ -689,18 +690,22 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
* filter, otherwise returns 0
*/
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
std::optional<CNameScript> GetNameDebit(const CTxIn& txin, const isminefilter& filter) const;
isminetype IsMine(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
CAmount GetCredit(const CTxOut& txout, const isminefilter& filter) const;
std::optional<CNameScript> GetNameCredit(const CTxOut& txout, const isminefilter& filter) const;
bool IsChange(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool IsChange(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
CAmount GetChange(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool IsMine(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/** should probably be renamed to IsRelevantToMe */
bool IsFromMe(const CTransaction& tx) const;
CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const;
std::optional<CNameScript> GetNameDebit(const CTransaction& tx, const isminefilter& filter) const;
/** Returns whether all of the inputs match the filter */
bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const;
CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const;
std::optional<CNameScript> GetNameCredit(const CTransaction& tx, const isminefilter& filter) const;
CAmount GetChange(const CTransaction& tx) const;
void chainStateFlushed(const CBlockLocator& loc) override;

Expand Down

0 comments on commit 6e0d254

Please sign in to comment.