Skip to content

Commit 7aa2375

Browse files
committed
Bare txid for GetTransaction / GetWalletTx.
This extends the two functions GetWalletTx and GetTransaction to (also) find transactions by bare txid, not just by the normal txid. These methods are mainly used in places where we need to look up e.g. the previous transaction to a spend, so that we can know the address that is being spent or the value of the input. The change is fine to do (won't cause any extra consensus changes) because all it does is make those methods return the correct previous transaction (for after the fork) in cases where they would have failed otherwise (since both are SHA-256d hashes and thus cannot have collisions). Also actual checks that some spent coin actually exists are the explicitly on the consensus-level anyway.
1 parent e069c23 commit 7aa2375

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

divi/src/BlockDiskAccessor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
7979
{
8080
LOCK(cs_main);
8181
{
82-
if (mempool.lookup(hash, txOut)) {
82+
if (mempool.lookup(hash, txOut) || mempool.lookupBareTxid(hash, txOut)) {
8383
return true;
8484
}
8585
}
@@ -104,8 +104,9 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
104104
return true;
105105
}
106106

107-
// transaction not found in the index, nothing more can be done
108-
return false;
107+
// The index only keys by txid. So even if we did not find the
108+
// transaction here, it could be that the lookup is by bare txid
109+
// and can be found through UTXO lookup.
109110
}
110111

111112
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
@@ -125,7 +126,7 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
125126
CBlock block;
126127
if (ReadBlockFromDisk(block, pindexSlow)) {
127128
BOOST_FOREACH (const CTransaction& tx, block.vtx) {
128-
if (tx.GetHash() == hash) {
129+
if (tx.GetHash() == hash || tx.GetBareTxid() == hash) {
129130
txOut = tx;
130131
hashBlock = pindexSlow->GetBlockHash();
131132
return true;

divi/src/WalletTransactionRecord.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,22 @@ WalletTransactionRecord::WalletTransactionRecord(
3030
const CWalletTx* WalletTransactionRecord::GetWalletTx(const uint256& hash) const
3131
{
3232
AssertLockHeld(cs_walletTxRecord);
33-
std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
34-
if (it == mapWallet.end())
35-
return NULL;
36-
return &(it->second);
33+
34+
{
35+
const auto mit = mapWallet.find(hash);
36+
if (mit != mapWallet.end())
37+
return &mit->second;
38+
}
39+
40+
{
41+
const auto mit = mapBareTxid.find(hash);
42+
if (mit != mapBareTxid.end())
43+
return mit->second;
44+
}
45+
46+
return nullptr;
3747
}
48+
3849
std::vector<const CWalletTx*> WalletTransactionRecord::GetWalletTransactionReferences() const
3950
{
4051
AssertLockHeld(cs_walletTxRecord);
@@ -50,7 +61,12 @@ std::vector<const CWalletTx*> WalletTransactionRecord::GetWalletTransactionRefer
5061
std::pair<std::map<uint256, CWalletTx>::iterator, bool> WalletTransactionRecord::AddTransaction(const CWalletTx& newlyAddedTransaction)
5162
{
5263
AssertLockHeld(cs_walletTxRecord);
53-
return mapWallet.insert(std::make_pair(newlyAddedTransaction.GetHash(), newlyAddedTransaction));
64+
65+
auto res = mapWallet.emplace(newlyAddedTransaction.GetHash(), newlyAddedTransaction);
66+
if (res.second)
67+
mapBareTxid.emplace(newlyAddedTransaction.GetBareTxid(), &res.first->second);
68+
69+
return res;
5470
};
5571

5672
void WalletTransactionRecord::UpdateMetadata(

divi/src/WalletTransactionRecord.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ struct WalletTransactionRecord
1010
CCriticalSection& cs_walletTxRecord;
1111
const std::string walletFilename_;
1212
const bool databaseWritesAreDisallowed_;
13+
14+
/** Map from the bare txid of transactions in the wallet to the matching
15+
* transactions themselves. */
16+
std::map<uint256, const CWalletTx*> mapBareTxid;
17+
1318
public:
1419
std::map<uint256, CWalletTx> mapWallet;
1520

1621
WalletTransactionRecord(CCriticalSection& requiredWalletLock,const std::string& walletFilename);
1722
WalletTransactionRecord(CCriticalSection& requiredWalletLock);
1823
const CWalletTx* GetWalletTx(const uint256& hash) const;
24+
25+
/** Tries to look up a transaction in the wallet, either by hash (txid) or
26+
* the bare txid that is used after segwit-light to identify outputs. */
1927
std::vector<const CWalletTx*> GetWalletTransactionReferences() const;
2028
std::pair<std::map<uint256, CWalletTx>::iterator, bool> AddTransaction(const CWalletTx& newlyAddedTransaction);
2129
void UpdateMetadata(const uint256& hashOfTransactionToUpdate, const CWalletTx& updatedTransaction, bool updateDiskAndTimestamp,bool writeToWalletDb=false);
2230
};
2331

24-
#endif// WALLET_TRANSACTION_RECORD_H
32+
#endif// WALLET_TRANSACTION_RECORD_H

divi/src/rpcmasternode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Value allocatefunds(const Array& params, bool fHelp)
8686
" <future> (numeric, required) amount of divi funded will also be accepted for partially funding master nodes and other purposes.\n"
8787

8888
"\nResult:\n"
89-
"\"vin\" (string) funding transaction id necessary for next step.\n");
89+
"\"vin\" (string) funding transaction id or bare txid necessary for next step.\n");
9090

9191
if (params[0].get_str() != "masternode")
9292
{
@@ -107,7 +107,7 @@ Value allocatefunds(const Array& params, bool fHelp)
107107
SendMoney(acctAddr.Get(), CMasternode::GetTierCollateralAmount(nMasternodeTier), wtx);
108108

109109
Object obj;
110-
obj.push_back(Pair("txhash", wtx.GetHash().GetHex()));
110+
obj.push_back(Pair("txhash", wtx.GetHashForSpendingOutput().GetHex()));
111111
return obj;
112112
}
113113

@@ -122,7 +122,7 @@ Value fundmasternode(const Array& params, bool fHelp)
122122
"1. alias (string, required) helpful identifier to recognize this allocation later.\n"
123123
"2. amount (diamond, platinum, gold, silver, copper) tier of masternode. \n"
124124
" <future> (numeric, required) amount of divi funded will also be accepted for partially funding master nodes and other purposes.\n"
125-
"3. TxID (string, required) funding transaction id .\n"
125+
"3. TxID (string, required) funding transaction id or bare txid.\n"
126126
"4. masternode (string, required) ip address of masternode.\n"
127127
"(use an empty string for the pay wallet if the same as the funding wallet and you wish to assign a different voting wallet).\n"
128128

@@ -237,7 +237,7 @@ Value setupmasternode(const Array& params, bool fHelp)
237237

238238
"\nArguments:\n"
239239
"1. alias (string, required) Helpful identifier to recognize this masternode later. \n"
240-
"2. txHash (string, required) Funding transaction. \n"
240+
"2. txHash (string, required) Funding transaction hash or bare txid. \n"
241241
"3. outputIndex (string, required) Output index transaction. \n"
242242
"4. collateralPubkey (string, required) collateral pubkey. \n"
243243
"5. ip_address (string, required) Local ip address of this node\n"

0 commit comments

Comments
 (0)