Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit b60d548

Browse files
authored
fix(explorer): Move block cache at higher level (#930)
* fix(explorer): Move block cache at higher level To reduce number of calls made to explorer
1 parent 20f765f commit b60d548

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

core/src/wallet/bitcoin/BitcoinLikeWallet.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ namespace ledger {
5858
_synchronizerFactory(synchronizer),
5959
_keychainFactory(keychainFactory),
6060
_explorer(explorer),
61-
_blockCache(std::chrono::seconds(configuration->getInt(api::Configuration::TTL_BLOCK_CACHE)
62-
.value_or(api::ConfigurationDefaults::DEFAULT_TTL_CACHE))),
6361
_mempoolGracePeriod(std::chrono::seconds(configuration->getInt(api::Configuration::MEMPOOL_GRACE_PERIOD_SECS).value_or(api::ConfigurationDefaults::DEFAULT_BTC_LIKE_MEMPOOL_GRACE))) {
6462
}
6563

@@ -237,19 +235,11 @@ namespace ledger {
237235
}
238236

239237
void BitcoinLikeWallet::getLastBlock(const std::shared_ptr<api::BlockCallback> &callback) {
240-
auto currencyName = getCurrency().name;
241-
auto optBlock = _blockCache.get(currencyName);
242-
if (optBlock.hasValue()) {
243-
Future<api::Block>::successful(optBlock.getValue()).callback(getMainExecutionContext(), callback);
244-
} else {
245-
_explorer->getCurrentBlock()
246-
.map<api::Block>(getContext(), [self = getSelf(), currencyName](const std::shared_ptr<Block> &b) -> api::Block {
247-
auto block = b->toApiBlock();
248-
self->_blockCache.put(currencyName, block);
249-
return block;
250-
})
251-
.callback(getMainExecutionContext(), callback);
252-
}
238+
_explorer->getCurrentBlock()
239+
.map<api::Block>(getContext(), [](const std::shared_ptr<Block> &b) -> api::Block {
240+
return b->toApiBlock();
241+
})
242+
.callback(getMainExecutionContext(), callback);
253243
}
254244
} // namespace core
255245
} // namespace ledger

core/src/wallet/bitcoin/BitcoinLikeWallet.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ namespace ledger {
9090
std::shared_ptr<BitcoinLikeKeychainFactory> _keychainFactory;
9191
BitcoinLikeAccountSynchronizerFactory _synchronizerFactory;
9292
std::chrono::seconds _mempoolGracePeriod;
93-
TTLCache<std::string, api::Block> _blockCache;
9493
};
9594
} // namespace core
9695
} // namespace ledger

core/src/wallet/bitcoin/explorers/LedgerApiBitcoinLikeBlockchainExplorer.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include "LedgerApiBitcoinLikeBlockchainExplorer.hpp"
3333

34+
#include "api/ConfigurationDefaults.hpp"
35+
3436
#include <api/ErrorCode.hpp>
3537
#include <api_impl/BigIntImpl.hpp>
3638
namespace ledger {
@@ -40,7 +42,9 @@ namespace ledger {
4042
const std::shared_ptr<HttpClient> &http,
4143
const api::BitcoinLikeNetworkParameters &parameters,
4244
const std::shared_ptr<api::DynamicObject> &configuration) : DedicatedContext(context),
43-
BitcoinLikeBlockchainExplorer(configuration, {api::Configuration::BLOCKCHAIN_EXPLORER_API_ENDPOINT}) {
45+
BitcoinLikeBlockchainExplorer(configuration, {api::Configuration::BLOCKCHAIN_EXPLORER_API_ENDPOINT}),
46+
_blockCache(std::chrono::seconds(configuration->getInt(api::Configuration::TTL_BLOCK_CACHE)
47+
.value_or(api::ConfigurationDefaults::DEFAULT_TTL_CACHE))) {
4448
_http = http;
4549
_parameters = parameters;
4650
_explorerVersion = configuration->getString(api::Configuration::BLOCKCHAIN_EXPLORER_VERSION).value_or("v3");
@@ -90,7 +94,15 @@ namespace ledger {
9094
}
9195

9296
FuturePtr<BitcoinLikeBlockchainExplorer::Block> LedgerApiBitcoinLikeBlockchainExplorer::getCurrentBlock() const {
93-
return getLedgerApiCurrentBlock();
97+
auto optBlock = _blockCache.get("lastBlock");
98+
if (optBlock.hasValue()) {
99+
return Future<std::shared_ptr<BitcoinLikeBlockchainExplorer::Block>>::successful(optBlock.getValue());
100+
}
101+
return getLedgerApiCurrentBlock()
102+
.mapPtr<BitcoinLikeBlockchainExplorer::Block>(getContext(), [self = this](const std::shared_ptr<BitcoinLikeBlockchainExplorer::Block> &b) {
103+
self->_blockCache.put("lastBlock", b);
104+
return b;
105+
});
94106
}
95107

96108
FuturePtr<BitcoinLikeBlockchainExplorerTransaction>

core/src/wallet/bitcoin/explorers/LedgerApiBitcoinLikeBlockchainExplorer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <async/Future.hpp>
4141
#include <collections/collections.hpp>
4242
#include <net/HttpClient.hpp>
43+
#include <utils/TTLCache.h>
4344
#include <wallet/bitcoin/explorers/BitcoinLikeBlockchainExplorer.hpp>
4445
#include <wallet/common/explorers/AbstractLedgerApiBlockchainExplorer.h>
4546

@@ -83,6 +84,7 @@ namespace ledger {
8384
private:
8485
api::BitcoinLikeNetworkParameters _parameters;
8586
std::string _explorerVersion;
87+
mutable TTLCache<std::string, std::shared_ptr<BitcoinLikeBlockchainExplorer::Block>> _blockCache;
8688
};
8789
} // namespace core
8890
} // namespace ledger

0 commit comments

Comments
 (0)