Skip to content

Commit 8213137

Browse files
committed
Add a wrapper around BitcoinAPI in mempool recorder
1 parent bfd32e1 commit 8213137

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

tools/mempool_recorder/main.cpp

+33-4
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,47 @@ struct MempoolFiles {
6464
{}
6565
};
6666

67+
class SaferBitcoinApi {
68+
std::string username;
69+
std::string password;
70+
std::string address;
71+
int port;
72+
BitcoinAPI bitcoinAPI;
73+
74+
public:
75+
76+
SaferBitcoinApi(std::string username_, std::string password_, std::string address_, int port_) :
77+
username(std::move(username_)), password(std::move(password_)), address(std::move(address_)), port(port_), bitcoinAPI(username, password, address, port) {}
78+
79+
std::vector<chaintip_t> getchaintips() {
80+
return bitcoinAPI.getchaintips();
81+
}
82+
83+
int getblockcount() {
84+
return bitcoinAPI.getblockcount();
85+
}
86+
87+
std::string getpreviousblockhash(const std::string& blockhash) {
88+
return bitcoinAPI.getpreviousblockhash(blockhash);
89+
}
90+
91+
std::vector<std::string> getrawmempool() {
92+
return bitcoinAPI.getrawmempool();
93+
}
94+
};
95+
6796
class MempoolRecorder {
6897
blocksci::Blockchain chain;
6998
blocksci::BlockHeight lastHeight;
7099
std::unordered_map<blocksci::uint256, MempoolRecord, std::hash<blocksci::uint256>> mempool;
71-
BitcoinAPI &bitcoinAPI;
100+
SaferBitcoinApi &bitcoinAPI;
72101

73102
MempoolFiles files;
74103
std::unordered_map<std::string, std::pair<BlockRecord, int>> blocksSeen;
75104

76105
static constexpr int heightCutoff = 1000;
77106
public:
78-
MempoolRecorder(const std::string &dataLocation, BitcoinAPI &bitcoinAPI_) :
107+
MempoolRecorder(const std::string &dataLocation, SaferBitcoinApi &bitcoinAPI_) :
79108
chain(dataLocation),
80109
lastHeight(static_cast<int>(chain.size())),
81110
bitcoinAPI(bitcoinAPI_),
@@ -114,7 +143,7 @@ class MempoolRecorder {
114143
updateTxTimes(system_clock::to_time_t(system_clock::now()));
115144
updateBlockTimes(system_clock::to_time_t(system_clock::now()));
116145
} catch (BitcoinException& e){
117-
std::cout << "Failed to update mempool with error: " << e.what() << std::endl;
146+
std::cerr << "Failed to update mempool with error: " << e.what() << std::endl;
118147
}
119148
}
120149

@@ -203,7 +232,7 @@ int main(int argc, char * argv[]) {
203232
return 0;
204233
}
205234

206-
BitcoinAPI bitcoinAPI{username, password, address, port};
235+
SaferBitcoinApi bitcoinAPI{username, password, address, port};
207236

208237
auto connected = false;
209238
while (!connected) {

0 commit comments

Comments
 (0)