wallet: Make CWalletTx "from" and "message" member variables

Instead of storing "from" and "message" inside of mapValue, store these
explicitly as members of CWalletTx.
This commit is contained in:
Ava Chow
2025-06-16 15:03:00 -07:00
parent c6ba98dcc8
commit 2155e913d3
6 changed files with 30 additions and 14 deletions

View File

@@ -391,6 +391,8 @@ struct WalletTx
CAmount debit;
CAmount change;
int64_t time;
std::optional<std::string> from; // Deprecated
std::optional<std::string> message; // Deprecated
std::map<std::string, std::string> value_map;
bool is_coinbase;

View File

@@ -123,11 +123,9 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
if (wtx.is_coinbase)
{
strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
}
else if (wtx.value_map.contains("from") && !wtx.value_map["from"].empty())
{
} else if (wtx.from) {
// Online transaction
strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(wtx.value_map["from"]) + "<br>";
strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(*wtx.from) + "<br>";
}
else
{
@@ -273,9 +271,10 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
//
// Message
//
if (wtx.value_map.contains("message") && !wtx.value_map["message"].empty())
strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["message"], true) + "<br>";
if (wtx.value_map.contains("comment") && !wtx.value_map["comment"].empty())
if (wtx.message) {
strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(*wtx.message, true) + "<br>";
}
if (wtx.value_map.count("comment") && !wtx.value_map["comment"].empty())
strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["comment"], true) + "<br>";
strHTML += "<b>" + tr("Transaction ID") + ":</b> " + rec->getTxHash() + "<br>";

View File

@@ -112,7 +112,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
{
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
sub.type = TransactionRecord::RecvFromOther;
sub.address = mapValue["from"];
sub.address = wtx.from.value_or("");
}
if (wtx.is_coinbase)
{

View File

@@ -79,6 +79,8 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
result.debit = CachedTxGetDebit(wallet, wtx, /*avoid_reuse=*/true);
result.change = CachedTxGetChange(wallet, wtx);
result.time = wtx.GetTxTime();
result.from = wtx.m_from;
result.message = wtx.m_message;
result.value_map = wtx.mapValue;
result.is_coinbase = wtx.IsCoinBase();
return result;

View File

@@ -194,6 +194,11 @@ public:
class CWalletTx
{
public:
// "from" and "message" are obsolete fields that could be set in
// the UI prior to 2011 (removed in commit 4d9b223)
// These fields are kept to avoid losing metadata.
std::optional<std::string> m_from;
std::optional<std::string> m_message;
/**
* Key/value map with information about the transaction.
*
@@ -206,8 +211,6 @@ public:
* bumpfee on transaction created by bumpfee
* "replaced_by_txid" - txid (as HexStr) of transaction created by
* bumpfee on transaction replaced by bumpfee
* "from", "message" - obsolete fields that could be set in UI prior to
* 2011 (removed in commit 4d9b223)
*
* The following keys are serialized in the wallet database, but shouldn't
* be read or written through the map (they will be temporarily added and
@@ -218,6 +221,8 @@ public:
* "timesmart" - serialized nTimeSmart value
* "spent" - serialized vfSpent value that existed prior to
* 2014 (removed in commit 93a18a3)
* "from", "message" - obsolete fields that could be set in UI prior to
* 2011 (removed in commit 4d9b223)
*/
mapValue_t mapValue;
std::vector<std::pair<std::string, std::string> > vOrderForm;
@@ -285,6 +290,8 @@ public:
void Serialize(Stream& s) const
{
mapValue_t mapValueCopy = mapValue;
if (m_from) mapValueCopy["from"] = *m_from;
if (m_message) mapValueCopy["message"] = *m_message;
mapValueCopy["fromaccount"] = "";
if (nOrderPos != -1) {
@@ -318,15 +325,19 @@ public:
m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});
const auto it_op = mapValue.find("n");
nOrderPos = (it_op != mapValue.end()) ? LocaleIndependentAtoi<int64_t>(it_op->second) : -1;
const auto it_ts = mapValue.find("timesmart");
nTimeSmart = (it_ts != mapValue.end()) ? static_cast<unsigned int>(LocaleIndependentAtoi<int64_t>(it_ts->second)) : 0;
for (const auto& [key, value] : mapValue) {
if (key == "n") nOrderPos = LocaleIndependentAtoi<int64_t>(value);
else if (key == "timesmart") nTimeSmart = LocaleIndependentAtoi<int64_t>(value);
else if (key == "from") m_from = value;
else if (key == "message") m_message = value;
}
mapValue.erase("fromaccount");
mapValue.erase("spent");
mapValue.erase("n");
mapValue.erase("timesmart");
mapValue.erase("from");
mapValue.erase("message");
}
void SetTx(CTransactionRef arg)

View File

@@ -742,6 +742,8 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
if (copyFrom == copyTo) continue;
assert(copyFrom && "Oldest wallet transaction in range assumed to have been found.");
if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
copyTo->m_from = copyFrom->m_from;
copyTo->m_message = copyFrom->m_message;
copyTo->mapValue = copyFrom->mapValue;
copyTo->vOrderForm = copyFrom->vOrderForm;
// nTimeReceived not copied on purpose