Skip to content

Commit c5ad937

Browse files
committed
Fix ZMQ DaemonInfo:
* top_block_hash was never set in handler * wide_difficulty was never sent in JSON * wide_cumulative_difficulty was never sent in JSON
1 parent 24ccaba commit c5ad937

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/rpc/daemon_handler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ namespace rpc
520520
res.info.target_height = res.info.height;
521521
}
522522

523+
m_core.get_blockchain_top(res.info.top_block_height, res.info.top_block_hash);
524+
523525
auto& chain = m_core.get_blockchain_storage();
524526

525527
res.info.wide_difficulty = chain.get_difficulty_for_next_block();

src/rpc/message_data_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ namespace rpc
176176
{
177177
uint64_t height;
178178
uint64_t target_height;
179+
uint64_t top_block_height;
179180
cryptonote::difficulty_type wide_difficulty;
180181
uint64_t difficulty;
181182
uint64_t target;

src/serialization/json_object.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,9 +1423,14 @@ void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::r
14231423
{
14241424
dest.StartObject();
14251425

1426+
const uint64_t difficulty_top64 = (info.wide_difficulty >> 64).convert_to<std::uint64_t>();
1427+
const uint64_t cumulative_difficulty_top64 = (info.wide_cumulative_difficulty >> 64).convert_to<std::uint64_t>();
1428+
14261429
INSERT_INTO_JSON_OBJECT(dest, height, info.height);
14271430
INSERT_INTO_JSON_OBJECT(dest, target_height, info.target_height);
1431+
INSERT_INTO_JSON_OBJECT(dest, top_block_height, info.top_block_height);
14281432
INSERT_INTO_JSON_OBJECT(dest, difficulty, info.difficulty);
1433+
INSERT_INTO_JSON_OBJECT(dest, difficulty_top64, difficulty_top64);
14291434
INSERT_INTO_JSON_OBJECT(dest, target, info.target);
14301435
INSERT_INTO_JSON_OBJECT(dest, tx_count, info.tx_count);
14311436
INSERT_INTO_JSON_OBJECT(dest, tx_pool_size, info.tx_pool_size);
@@ -1440,12 +1445,14 @@ void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::r
14401445
INSERT_INTO_JSON_OBJECT(dest, nettype, info.nettype);
14411446
INSERT_INTO_JSON_OBJECT(dest, top_block_hash, info.top_block_hash);
14421447
INSERT_INTO_JSON_OBJECT(dest, cumulative_difficulty, info.cumulative_difficulty);
1448+
INSERT_INTO_JSON_OBJECT(dest, cumulative_difficulty_top64, cumulative_difficulty_top64);
14431449
INSERT_INTO_JSON_OBJECT(dest, block_size_limit, info.block_size_limit);
14441450
INSERT_INTO_JSON_OBJECT(dest, block_weight_limit, info.block_weight_limit);
14451451
INSERT_INTO_JSON_OBJECT(dest, block_size_median, info.block_size_median);
14461452
INSERT_INTO_JSON_OBJECT(dest, block_weight_median, info.block_weight_median);
14471453
INSERT_INTO_JSON_OBJECT(dest, adjusted_time, info.adjusted_time);
14481454
INSERT_INTO_JSON_OBJECT(dest, start_time, info.start_time);
1455+
INSERT_INTO_JSON_OBJECT(dest, version, info.version);
14491456

14501457
dest.EndObject();
14511458
}
@@ -1457,9 +1464,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& inf
14571464
throw WRONG_TYPE("json object");
14581465
}
14591466

1467+
uint64_t difficulty_top64 = 0;
1468+
uint64_t cumulative_difficulty_top64 = 0;
1469+
14601470
GET_FROM_JSON_OBJECT(val, info.height, height);
14611471
GET_FROM_JSON_OBJECT(val, info.target_height, target_height);
1472+
GET_FROM_JSON_OBJECT(val, info.top_block_height, top_block_height);
14621473
GET_FROM_JSON_OBJECT(val, info.difficulty, difficulty);
1474+
GET_FROM_JSON_OBJECT(val, difficulty_top64, difficulty_top64);
14631475
GET_FROM_JSON_OBJECT(val, info.target, target);
14641476
GET_FROM_JSON_OBJECT(val, info.tx_count, tx_count);
14651477
GET_FROM_JSON_OBJECT(val, info.tx_pool_size, tx_pool_size);
@@ -1474,12 +1486,22 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& inf
14741486
GET_FROM_JSON_OBJECT(val, info.nettype, nettype);
14751487
GET_FROM_JSON_OBJECT(val, info.top_block_hash, top_block_hash);
14761488
GET_FROM_JSON_OBJECT(val, info.cumulative_difficulty, cumulative_difficulty);
1489+
GET_FROM_JSON_OBJECT(val, cumulative_difficulty_top64, cumulative_difficulty_top64);
14771490
GET_FROM_JSON_OBJECT(val, info.block_size_limit, block_size_limit);
14781491
GET_FROM_JSON_OBJECT(val, info.block_weight_limit, block_weight_limit);
14791492
GET_FROM_JSON_OBJECT(val, info.block_size_median, block_size_median);
14801493
GET_FROM_JSON_OBJECT(val, info.block_weight_median, block_weight_median);
14811494
GET_FROM_JSON_OBJECT(val, info.adjusted_time, adjusted_time);
14821495
GET_FROM_JSON_OBJECT(val, info.start_time, start_time);
1496+
GET_FROM_JSON_OBJECT(val, info.version, version);
1497+
1498+
info.wide_difficulty = difficulty_top64;
1499+
info.wide_difficulty <<= 64;
1500+
info.wide_difficulty += info.difficulty;
1501+
1502+
info.wide_cumulative_difficulty = cumulative_difficulty_top64;
1503+
info.wide_cumulative_difficulty <<= 64;
1504+
info.wide_cumulative_difficulty += info.cumulative_difficulty;
14831505
}
14841506

14851507
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_distribution& dist)

tests/unit_tests/json_serialization.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,68 @@ TEST(JsonSerialization, InvalidVectorBytes)
124124
EXPECT_THROW(cryptonote::json::fromJsonValue(doc, out), cryptonote::json::BAD_INPUT);
125125
}
126126

127+
TEST(JsonSerialization, DaemonInfo)
128+
{
129+
cryptonote::rpc::DaemonInfo info{};
130+
info.height = 154544;
131+
info.target_height = 15345435;
132+
info.top_block_height = 2344;
133+
info.wide_difficulty = cryptonote::difficulty_type{"100000000000000000005443"};
134+
info.difficulty = 200376420520695107;
135+
info.target = 7657567;
136+
info.tx_count = 355;
137+
info.tx_pool_size = 45435;
138+
info.alt_blocks_count = 43535;
139+
info.outgoing_connections_count = 1444;
140+
info.incoming_connections_count = 1444;
141+
info.white_peerlist_size = 14550;
142+
info.grey_peerlist_size = 34324;
143+
info.mainnet = true;
144+
info.testnet = true;
145+
info.stagenet = true;
146+
info.nettype = "main";
147+
info.top_block_hash = crypto::hash{1};
148+
info.wide_cumulative_difficulty = cryptonote::difficulty_type{"200000000000000000005543"};
149+
info.cumulative_difficulty = 400752841041384871;
150+
info.block_size_limit = 4324234;
151+
info.block_weight_limit = 3434;
152+
info.block_size_median = 3434;
153+
info.adjusted_time = 4535;
154+
info.block_weight_median = 43535;
155+
info.start_time = 34535;
156+
info.version = "1.0";
157+
158+
const auto info_copy = test_json(info);
159+
160+
EXPECT_EQ(info.height, info_copy.height);
161+
EXPECT_EQ(info.target_height, info_copy.target_height);
162+
EXPECT_EQ(info.top_block_height, info_copy.top_block_height);
163+
EXPECT_EQ(info.wide_difficulty, info_copy.wide_difficulty);
164+
EXPECT_EQ(info.difficulty, info_copy.difficulty);
165+
EXPECT_EQ(info.target, info_copy.target);
166+
EXPECT_EQ(info.tx_count, info_copy.tx_count);
167+
EXPECT_EQ(info.tx_pool_size, info_copy.tx_pool_size);
168+
EXPECT_EQ(info.alt_blocks_count, info_copy.alt_blocks_count);
169+
EXPECT_EQ(info.outgoing_connections_count, info_copy.outgoing_connections_count);
170+
EXPECT_EQ(info.incoming_connections_count, info_copy.incoming_connections_count);
171+
EXPECT_EQ(info.white_peerlist_size, info_copy.white_peerlist_size);
172+
EXPECT_EQ(info.grey_peerlist_size, info_copy.grey_peerlist_size);
173+
EXPECT_EQ(info.mainnet, info_copy.mainnet);
174+
EXPECT_EQ(info.testnet, info_copy.testnet);
175+
EXPECT_EQ(info.stagenet, info_copy.stagenet);
176+
EXPECT_EQ(info.nettype, info_copy.nettype);
177+
EXPECT_EQ(info.top_block_hash, info_copy.top_block_hash);
178+
EXPECT_EQ(info.wide_cumulative_difficulty, info_copy.wide_cumulative_difficulty);
179+
EXPECT_EQ(info.cumulative_difficulty, info_copy.cumulative_difficulty);
180+
EXPECT_EQ(info.block_size_limit, info_copy.block_size_limit);
181+
EXPECT_EQ(info.block_weight_limit, info_copy.block_weight_limit);
182+
EXPECT_EQ(info.block_size_median, info_copy.block_size_median);
183+
EXPECT_EQ(info.adjusted_time, info_copy.adjusted_time);
184+
EXPECT_EQ(info.block_weight_median, info_copy.block_weight_median);
185+
EXPECT_EQ(info.start_time, info_copy.start_time);
186+
EXPECT_EQ(info.version, info_copy.version);
187+
}
188+
127189
TEST(JsonSerialization, MinerTransaction)
128190
{
129191
cryptonote::account_base acct;

0 commit comments

Comments
 (0)