Skip to content

Commit 7a26c6b

Browse files
committed
Use ENABLE_WALLET to wrap wallet fetch functions
And slightly refine the rest: - use const where possible - reorder functions
1 parent 5df3ed3 commit 7a26c6b

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

src/omnicore/fetchwallettx.cpp

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,92 @@
1-
// The fetch functions provide a sorted list of transaction hashes ordered by block, position in block and position in wallet including STO receipts
1+
/**
2+
* @file fetchwallettx.cpp
3+
*
4+
* The fetch functions provide a sorted list of transaction hashes ordered by block,
5+
* position in block and position in wallet including STO receipts.
6+
*/
7+
28
#include "omnicore/fetchwallettx.h"
39

10+
#include "omnicore/log.h"
411
#include "omnicore/omnicore.h"
512
#include "omnicore/pending.h"
613
#include "omnicore/utilsbitcoin.h"
714

815
#include "init.h"
9-
#include "wallet.h"
16+
#include "main.h"
1017
#include "sync.h"
18+
#include "tinyformat.h"
19+
#include "txdb.h"
20+
#ifdef ENABLE_WALLET
21+
#include "wallet.h"
22+
#endif
1123

12-
#include <stdint.h>
24+
#include <boost/algorithm/string.hpp>
1325

14-
#include <set>
26+
#include <stdint.h>
27+
#include <list>
1528
#include <map>
29+
#include <set>
1630
#include <string>
31+
#include <utility>
32+
#include <vector>
1733

18-
#include <boost/algorithm/string.hpp>
34+
namespace mastercore
35+
{
36+
/**
37+
* Gets the byte offset of a transaction from the transaction index.
38+
*/
39+
unsigned int GetTransactionByteOffset(const uint256& txid)
40+
{
41+
LOCK(cs_main);
42+
43+
CDiskTxPos position;
44+
if (pblocktree->ReadTxIndex(txid, position)) {
45+
return position.nTxOffset;
46+
}
1947

20-
using namespace mastercore;
48+
return 0;
49+
}
2150

2251
/**
23-
* Returns an ordered list of Omni transactions including STO receipts that are relevant to the wallet
24-
* Ignores order in the wallet (which can be skewed by watch addresses) and utilizes block height and position within block
25-
*/
26-
std::map<std::string,uint256> FetchWalletOmniTransactions(int count, int startBlock, int endBlock)
52+
* Returns an ordered list of Omni transactions including STO receipts that are relevant to the wallet.
53+
*
54+
* Ignores order in the wallet (which can be skewed by watch addresses) and utilizes block height and position within block.
55+
*/
56+
std::map<std::string, uint256> FetchWalletOmniTransactions(unsigned int count, int startBlock, int endBlock)
2757
{
28-
// Iterate backwards through wallet transactions until we have count items to return:
29-
std::map<std::string,uint256> mapResponse;
58+
std::map<std::string, uint256> mapResponse;
59+
#ifdef ENABLE_WALLET
60+
if (pwalletMain == NULL) {
61+
return mapResponse;
62+
}
3063
std::set<uint256> seenHashes;
3164
std::list<CAccountingEntry> acentries;
3265
CWallet::TxItems txOrdered;
3366
{
3467
LOCK(pwalletMain->cs_wallet);
3568
txOrdered = pwalletMain->OrderedTxItems(acentries, "*");
3669
}
70+
// Iterate backwards through wallet transactions until we have count items to return:
3771
for (CWallet::TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
38-
CWalletTx *const pwtx = (*it).second.first;
39-
if (pwtx == 0) continue;
40-
uint256 txHash = pwtx->GetHash();
72+
const CWalletTx* pwtx = it->second.first;
73+
if (pwtx == NULL) continue;
74+
const uint256& txHash = pwtx->GetHash();
4175
{
4276
LOCK(cs_tally);
4377
if (!p_txlistdb->exists(txHash)) continue;
4478
}
45-
uint256 blockHash = pwtx->hashBlock;
79+
const uint256& blockHash = pwtx->hashBlock;
4680
if ((0 == blockHash) || (NULL == GetBlockIndex(blockHash))) continue;
47-
CBlockIndex* pBlockIndex = GetBlockIndex(blockHash);
81+
const CBlockIndex* pBlockIndex = GetBlockIndex(blockHash);
4882
if (NULL == pBlockIndex) continue;
4983
int blockHeight = pBlockIndex->nHeight;
5084
if (blockHeight < startBlock || blockHeight > endBlock) continue;
5185
int blockPosition = GetTransactionByteOffset(txHash);
5286
std::string sortKey = strprintf("%06d%010d", blockHeight, blockPosition);
5387
mapResponse.insert(std::make_pair(sortKey, txHash));
5488
seenHashes.insert(txHash);
55-
if ((int)mapResponse.size() >= count) break;
89+
if (mapResponse.size() >= count) break;
5690
}
5791

5892
// Insert STO receipts - receiving an STO has no inbound transaction to the wallet, so we will insert these manually into the response
@@ -82,34 +116,27 @@ std::map<std::string,uint256> FetchWalletOmniTransactions(int count, int startBl
82116
}
83117

84118
// Insert pending transactions (sets block as 999999 and position as wallet position)
85-
for (PendingMap::iterator it = my_pending.begin(); it != my_pending.end(); ++it) {
86-
uint256 txHash = it->first;
119+
// TODO: resolve potential deadlock caused by cs_wallet, cs_pending
120+
// LOCK(cs_pending);
121+
for (PendingMap::const_iterator it = my_pending.begin(); it != my_pending.end(); ++it) {
122+
const uint256& txHash = it->first;
87123
int blockHeight = 999999;
88124
if (blockHeight < startBlock || blockHeight > endBlock) continue;
89125
int blockPosition = 0;
90126
{
91127
LOCK(pwalletMain->cs_wallet);
92128
std::map<uint256, CWalletTx>::const_iterator walletIt = pwalletMain->mapWallet.find(txHash);
93129
if (walletIt != pwalletMain->mapWallet.end()) {
94-
const CWalletTx* pendingWTx = &(*walletIt).second;
95-
blockPosition = pendingWTx->nOrderPos;
130+
const CWalletTx& wtx = walletIt->second;
131+
blockPosition = wtx.nOrderPos;
96132
}
97133
}
98134
std::string sortKey = strprintf("%06d%010d", blockHeight, blockPosition);
99135
mapResponse.insert(std::make_pair(sortKey, txHash));
100136
}
101-
137+
#endif
102138
return mapResponse;
103139
}
104140

105-
/** Gets the byte offset of a transaction from the tx index
106-
*/
107-
uint64_t GetTransactionByteOffset(const uint256& txid)
108-
{
109-
LOCK(cs_main);
110-
CDiskTxPos position;
111-
if (pblocktree->ReadTxIndex(txid, position)) {
112-
return position.nTxOffset;
113-
}
114-
return 0;
115-
}
141+
142+
} // namespace mastercore

src/omnicore/fetchwallettx.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33

44
class uint256;
55

6-
//#include "util.h"
7-
#include "txdb.h"
8-
9-
#include <stdint.h>
106
#include <map>
117
#include <string>
128

13-
/** Gets the byte offset of a transaction from the tx index
14-
*/
15-
uint64_t GetTransactionByteOffset(const uint256& txid);
9+
namespace mastercore
10+
{
11+
/** Gets the byte offset of a transaction from the transaction index. */
12+
unsigned int GetTransactionByteOffset(const uint256& txid);
1613

17-
/**
18-
* Returns an ordered list of Omni transactions that are relevant to the wallet
19-
* Ignores order in the wallet (which can be skewed by watch addresses) and utilizes block height and position within block
20-
*/
21-
std::map<std::string,uint256> FetchWalletOmniTransactions(int count, int startBlock = 0, int endBlock = 999999);
14+
/** Returns an ordered list of Omni transactions that are relevant to the wallet. */
15+
std::map<std::string, uint256> FetchWalletOmniTransactions(unsigned int count, int startBlock = 0, int endBlock = 999999);
16+
}
2217

2318
#endif // OMNICORE_FETCHWALLETTX_H

0 commit comments

Comments
 (0)