Skip to content

Commit 0ff7d85

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 afa2ab6 commit 0ff7d85

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

divi/src/TransactionDiskAccessor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
2424
{
2525
LOCK(cs_main);
2626
{
27-
if (mempool.lookup(hash, txOut)) {
27+
if (mempool.lookup(hash, txOut) || mempool.lookupBareTxid(hash, txOut)) {
2828
return true;
2929
}
3030
}
@@ -49,8 +49,9 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
4949
return true;
5050
}
5151

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

5657
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
@@ -70,7 +71,7 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
7071
CBlock block;
7172
if (ReadBlockFromDisk(block, pindexSlow)) {
7273
BOOST_FOREACH (const CTransaction& tx, block.vtx) {
73-
if (tx.GetHash() == hash) {
74+
if (tx.GetHash() == hash || tx.GetBareTxid() == hash) {
7475
txOut = tx;
7576
hashBlock = pindexSlow->GetBlockHash();
7677
return true;
@@ -101,4 +102,4 @@ bool CollateralIsExpectedAmount(const COutPoint &outpoint, int64_t expectedAmoun
101102
return true;
102103
}
103104
assert(false);
104-
}
105+
}

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Value allocatefunds(const Array& params, bool fHelp)
7878
" <future> (numeric, required) amount of divi funded will also be accepted for partially funding master nodes and other purposes.\n"
7979

8080
"\nResult:\n"
81-
"\"vin\" (string) funding transaction id necessary for next step.\n");
81+
"\"vin\" (string) funding transaction id or bare txid necessary for next step.\n");
8282

8383
if (params[0].get_str() != "masternode")
8484
{
@@ -114,7 +114,7 @@ Value fundmasternode(const Array& params, bool fHelp)
114114
"1. alias (string, required) helpful identifier to recognize this allocation later.\n"
115115
"2. amount (diamond, platinum, gold, silver, copper) tier of masternode. \n"
116116
" <future> (numeric, required) amount of divi funded will also be accepted for partially funding master nodes and other purposes.\n"
117-
"3. TxID (string, required) funding transaction id .\n"
117+
"3. TxID (string, required) funding transaction id or bare txid.\n"
118118
"4. masternode (string, required) ip address of masternode.\n"
119119
"(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"
120120

@@ -229,7 +229,7 @@ Value setupmasternode(const Array& params, bool fHelp)
229229

230230
"\nArguments:\n"
231231
"1. alias (string, required) Helpful identifier to recognize this masternode later. \n"
232-
"2. txHash (string, required) Funding transaction. \n"
232+
"2. txHash (string, required) Funding transaction hash or bare txid. \n"
233233
"3. outputIndex (string, required) Output index transaction. \n"
234234
"4. collateralPubkey (string, required) collateral pubkey. \n"
235235
"5. ip_address (string, required) Local ip address of this node\n"

0 commit comments

Comments
 (0)