Skip to content

Commit 1e5750d

Browse files
committed
Merge branch 'groovy_gluon_7.3RC' into mainnet
2 parents 6808b47 + 69bb3f8 commit 1e5750d

File tree

2 files changed

+46
-126
lines changed

2 files changed

+46
-126
lines changed

explorer/adapter.cpp

Lines changed: 45 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ namespace beam { namespace explorer {
4242
namespace {
4343

4444
static const size_t PACKER_FRAGMENTS_SIZE = 4096;
45-
static const size_t CACHE_DEPTH = 100000;
4645

4746
const unsigned int FAKE_SEED = 10283UL;
4847
const char WALLET_DB_PATH[] = "explorer-wallet.db";
@@ -86,47 +85,6 @@ const char* difficulty_to_hex(char* buf, const Difficulty& d) {
8685
return uint256_to_hex(buf, raw);
8786
}
8887

89-
struct ResponseCache {
90-
io::SharedBuffer status;
91-
std::map<Height, io::SharedBuffer> blocks;
92-
Height currentHeight=0;
93-
94-
explicit ResponseCache(size_t depth) : _depth(depth)
95-
{}
96-
97-
void compact() {
98-
if (blocks.empty() || currentHeight <= _depth) return;
99-
Height horizon = currentHeight - _depth;
100-
if (blocks.rbegin()->first < horizon) {
101-
blocks.clear();
102-
return;
103-
}
104-
auto b = blocks.begin();
105-
auto it = b;
106-
while (it != blocks.end()) {
107-
if (it->first >= horizon) break;
108-
++it;
109-
}
110-
blocks.erase(b, it);
111-
}
112-
113-
bool get_block(io::SerializedMsg& out, Height h) {
114-
const auto& it = blocks.find(h);
115-
if (it == blocks.end()) return false;
116-
out.push_back(it->second);
117-
return true;
118-
}
119-
120-
void put_block(Height h, const io::SharedBuffer& body) {
121-
if (currentHeight - h > _depth) return;
122-
compact();
123-
blocks[h] = body;
124-
}
125-
126-
private:
127-
size_t _depth;
128-
};
129-
13088
using nlohmann::json;
13189

13290
} //namespace
@@ -237,9 +195,7 @@ class Adapter : public Node::IObserver, public IAdapter {
237195
_packer(PACKER_FRAGMENTS_SIZE),
238196
_node(node),
239197
_nodeBackend(node.get_Processor()),
240-
_statusDirty(true),
241-
_nodeIsSyncing(true),
242-
_cache(CACHE_DEPTH)
198+
_nodeIsSyncing(true)
243199
{
244200
init_helper_fragments();
245201
_hook = &node.m_Cfg.m_Observer;
@@ -305,77 +261,60 @@ class Adapter : public Node::IObserver, public IAdapter {
305261
void OnSyncProgress() override {
306262
const Node::SyncStatus& s = _node.m_SyncStatus;
307263
bool isSyncing = (s.m_Done != s.m_Total);
308-
if (isSyncing != _nodeIsSyncing) {
309-
_statusDirty = true;
310-
_nodeIsSyncing = isSyncing;
311-
}
264+
_nodeIsSyncing = isSyncing;
312265
if (_nextHook) _nextHook->OnSyncProgress();
313266
}
314267

315268
void OnStateChanged() override {
316-
const auto& cursor = _nodeBackend.m_Cursor;
317-
_cache.currentHeight = cursor.m_Sid.m_Height;
318-
_statusDirty = true;
319269
if (_nextHook) _nextHook->OnStateChanged();
320270
}
321271

322272
void OnRolledBack(const Block::SystemState::ID& id) override {
323-
324-
auto& blocks = _cache.blocks;
325-
326-
blocks.erase(blocks.lower_bound(id.m_Height), blocks.end());
327-
328273
if (_nextHook) _nextHook->OnRolledBack(id);
329274
}
330275

331276
bool get_status(io::SerializedMsg& out) override {
332-
if (_statusDirty) {
333-
const auto& cursor = _nodeBackend.m_Cursor;
334-
_cache.currentHeight = cursor.m_Sid.m_Height;
335277

336-
double possibleShieldedReadyHours = 0;
337-
uint64_t shieldedPer24h = 0;
278+
Height h = _nodeBackend.m_Cursor.m_Full.m_Height;
338279

339-
if (_cache.currentHeight)
340-
{
341-
NodeDB& db = _nodeBackend.get_DB();
342-
auto shieldedByLast24h =
343-
db.ShieldedOutpGet(_cache.currentHeight >= 1440 ? _cache.currentHeight - 1440 : 1);
344-
auto averageWindowBacklog = Rules::get().Shielded.MaxWindowBacklog / 2;
280+
double possibleShieldedReadyHours = 0;
281+
uint64_t shieldedPer24h = 0;
345282

346-
if (shieldedByLast24h && shieldedByLast24h != _nodeBackend.m_Extra.m_ShieldedOutputs)
347-
{
348-
shieldedPer24h = _nodeBackend.m_Extra.m_ShieldedOutputs - shieldedByLast24h;
349-
possibleShieldedReadyHours = ceil(averageWindowBacklog / (double)shieldedPer24h * 24);
350-
}
351-
}
352-
353-
char buf[80];
283+
if (h)
284+
{
285+
NodeDB& db = _nodeBackend.get_DB();
286+
auto shieldedByLast24h = db.ShieldedOutpGet(h >= 1440 ? h - 1440 : 1);
287+
auto averageWindowBacklog = Rules::get().Shielded.MaxWindowBacklog / 2;
354288

355-
_sm.clear();
356-
if (!serialize_json_msg(
357-
_sm,
358-
_packer,
359-
json{
360-
{ "timestamp", cursor.m_Full.m_TimeStamp },
361-
{ "height", _cache.currentHeight },
362-
{ "low_horizon", _nodeBackend.m_Extra.m_TxoHi },
363-
{ "hash", hash_to_hex(buf, cursor.m_ID.m_Hash) },
364-
{ "chainwork", uint256_to_hex(buf, cursor.m_Full.m_ChainWork) },
365-
{ "peers_count", _node.get_AcessiblePeerCount() },
366-
{ "shielded_outputs_total", _nodeBackend.m_Extra.m_ShieldedOutputs },
367-
{ "shielded_outputs_per_24h", shieldedPer24h },
368-
{ "shielded_possible_ready_in_hours", shieldedPer24h ? std::to_string(possibleShieldedReadyHours) : "-" }
369-
}
370-
)) {
371-
return false;
289+
if (shieldedByLast24h && shieldedByLast24h != _nodeBackend.m_Extra.m_ShieldedOutputs)
290+
{
291+
shieldedPer24h = _nodeBackend.m_Extra.m_ShieldedOutputs - shieldedByLast24h;
292+
possibleShieldedReadyHours = ceil(averageWindowBacklog / (double)shieldedPer24h * 24);
372293
}
294+
}
373295

374-
_cache.status = io::normalize(_sm, false);
375-
_statusDirty = false;
376-
_sm.clear();
296+
char buf[80];
297+
298+
io::SerializedMsg sm;
299+
if (!serialize_json_msg(
300+
sm,
301+
_packer,
302+
json{
303+
{ "timestamp", _nodeBackend.m_Cursor.m_Full.m_TimeStamp },
304+
{ "height", h },
305+
{ "low_horizon", _nodeBackend.m_Extra.m_TxoHi },
306+
{ "hash", hash_to_hex(buf, _nodeBackend.m_Cursor.m_ID.m_Hash) },
307+
{ "chainwork", uint256_to_hex(buf, _nodeBackend.m_Cursor.m_Full.m_ChainWork) },
308+
{ "peers_count", _node.get_AcessiblePeerCount() },
309+
{ "shielded_outputs_total", _nodeBackend.m_Extra.m_ShieldedOutputs },
310+
{ "shielded_outputs_per_24h", shieldedPer24h },
311+
{ "shielded_possible_ready_in_hours", shieldedPer24h ? std::to_string(possibleShieldedReadyHours) : "-" }
312+
}
313+
)) {
314+
return false;
377315
}
378-
out.push_back(_cache.status);
316+
317+
out.push_back(io::normalize(sm, false));
379318
return true;
380319
}
381320

@@ -1662,38 +1601,27 @@ class Adapter : public Node::IObserver, public IAdapter {
16621601
}
16631602

16641603
bool get_block_impl(io::SerializedMsg& out, uint64_t height, uint64_t& row, uint64_t* prevRow) {
1665-
if (_cache.get_block(out, height)) {
1666-
if (prevRow && row > 0) {
1667-
extract_row(height, row, prevRow);
1668-
}
1669-
return true;
1670-
}
16711604

1672-
if (_statusDirty) {
1673-
const auto &cursor = _nodeBackend.m_Cursor;
1674-
_cache.currentHeight = cursor.m_Sid.m_Height;
1675-
}
1605+
Height h = _nodeBackend.m_Cursor.m_Full.m_Height;
16761606

16771607
io::SharedBuffer body;
1678-
bool blockAvailable = (height <= _cache.currentHeight);
1608+
bool blockAvailable = (height <= h);
16791609
if (blockAvailable) {
16801610
json j;
16811611
if (!extract_block(j, height, row, prevRow)) {
16821612
blockAvailable = false;
16831613
} else {
1684-
_sm.clear();
1685-
if (serialize_json_msg(_sm, _packer, j)) {
1686-
body = io::normalize(_sm, false);
1687-
_cache.put_block(height, body);
1614+
io::SerializedMsg sm;
1615+
if (serialize_json_msg(sm, _packer, j)) {
1616+
body = io::normalize(sm, false);
16881617
} else {
16891618
return false;
16901619
}
1691-
_sm.clear();
16921620
}
16931621
}
16941622

16951623
if (blockAvailable) {
1696-
out.push_back(body);
1624+
out.push_back(std::move(body));
16971625
return true;
16981626
}
16991627

@@ -1703,14 +1631,13 @@ class Adapter : public Node::IObserver, public IAdapter {
17031631
bool json2Msg(const json& obj, io::SerializedMsg& out) {
17041632
LOG_DEBUG() << obj;
17051633

1706-
_sm.clear();
1634+
io::SerializedMsg sm;
17071635
io::SharedBuffer body;
1708-
if (serialize_json_msg(_sm, _packer, obj)) {
1709-
body = io::normalize(_sm, false);
1636+
if (serialize_json_msg(sm, _packer, obj)) {
1637+
body = io::normalize(sm, false);
17101638
} else {
17111639
return false;
17121640
}
1713-
_sm.clear();
17141641

17151642
out.push_back(body);
17161643

@@ -1892,20 +1819,13 @@ class Adapter : public Node::IObserver, public IAdapter {
18921819
// helper fragments
18931820
io::SharedBuffer _leftBrace, _comma, _rightBrace, _quote;
18941821

1895-
// If true then status boby needs to be refreshed
1896-
bool _statusDirty;
1897-
18981822
// True if node is syncing at the moment
18991823
bool _nodeIsSyncing;
19001824

19011825
// node observers chain
19021826
Node::IObserver** _hook;
19031827
Node::IObserver* _nextHook;
19041828

1905-
ResponseCache _cache;
1906-
1907-
io::SerializedMsg _sm;
1908-
19091829
wallet::IWalletDB::Ptr _walletDB;
19101830
wallet::Wallet::Ptr _wallet;
19111831
std::shared_ptr<beam::BroadcastRouter> _broadcastRouter;

explorer/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bool Server::on_request(uint64_t id, const HttpMsgReader::Message& msg)
128128

129129
if (_currentUrl.parse(path, m_Dirs))
130130
{
131-
switch (_currentUrl.dir)
131+
switch ((DirType) _currentUrl.dir)
132132
{
133133
#define THE_MACRO(dir) case DirType::dir: pFn = &Server::on_request_##dir; break;
134134
ExplorerNodeDirs(THE_MACRO)

0 commit comments

Comments
 (0)