fix serialize order of dict in peer stats and make the unit tests match this change

pull/1319/head
Jeff Becker 4 years ago
parent fa37c7c9b5
commit 576c6ec9d4
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -1,7 +1,7 @@
#include <peerstats/types.hpp>
#include <util/str.hpp>
#include <lokimq/bt_serialize.h>
#include <stdexcept>
namespace llarp
@ -104,36 +104,26 @@ namespace llarp
{
if (not buf)
throw std::runtime_error("PeerStats: Can't use null buf");
auto encodeUint64Entry = [&](std::string_view key, uint64_t value) {
if (not bencode_write_uint64_entry(buf, key.data(), key.size(), value))
throw std::runtime_error(stringify("PeerStats: Could not encode ", key));
const lokimq::bt_dict data = {
{NumConnectionAttemptsKey, numConnectionAttempts},
{NumConnectionSuccessesKey, numConnectionSuccesses},
{NumConnectionRejectionsKey, numConnectionRejections},
{NumConnectionTimeoutsKey, numConnectionTimeouts},
{NumPathBuildsKey, numPathBuilds},
{NumPacketsAttemptedKey, numPacketsAttempted},
{NumPacketsSentKey, numPacketsSent},
{NumPacketsDroppedKey, numPacketsDropped},
{NumPacketsResentKey, numPacketsResent},
{NumDistinctRCsReceivedKey, numDistinctRCsReceived},
{NumLateRCsKey, numLateRCs},
{PeakBandwidthBytesPerSecKey, (uint64_t)peakBandwidthBytesPerSec},
{LongestRCReceiveIntervalKey, longestRCReceiveInterval.count()},
{LeastRCRemainingLifetimeKey, leastRCRemainingLifetime.count()},
{LastRCUpdatedKey, lastRCUpdated.count()},
};
if (not bencode_start_dict(buf))
throw std::runtime_error("PeerStats: Could not create bencode dict");
// TODO: we don't have bencode support for dict entries other than uint64...?
// encodeUint64Entry(RouterIdKey, routerId);
encodeUint64Entry(NumConnectionAttemptsKey, numConnectionAttempts);
encodeUint64Entry(NumConnectionSuccessesKey, numConnectionSuccesses);
encodeUint64Entry(NumConnectionRejectionsKey, numConnectionRejections);
encodeUint64Entry(NumConnectionTimeoutsKey, numConnectionTimeouts);
encodeUint64Entry(NumPathBuildsKey, numPathBuilds);
encodeUint64Entry(NumPacketsAttemptedKey, numPacketsAttempted);
encodeUint64Entry(NumPacketsSentKey, numPacketsSent);
encodeUint64Entry(NumPacketsDroppedKey, numPacketsDropped);
encodeUint64Entry(NumPacketsResentKey, numPacketsResent);
encodeUint64Entry(NumDistinctRCsReceivedKey, numDistinctRCsReceived);
encodeUint64Entry(NumLateRCsKey, numLateRCs);
encodeUint64Entry(PeakBandwidthBytesPerSecKey, (uint64_t)peakBandwidthBytesPerSec);
encodeUint64Entry(LongestRCReceiveIntervalKey, longestRCReceiveInterval.count());
encodeUint64Entry(LeastRCRemainingLifetimeKey, leastRCRemainingLifetime.count());
encodeUint64Entry(LastRCUpdatedKey, lastRCUpdated.count());
if (not bencode_end(buf))
throw std::runtime_error("PeerStats: Could not end bencode dict");
const auto serialized = lokimq::bt_serialize(data);
if (not buf->write(serialized.begin(), serialized.end()))
throw std::runtime_error("PeerStats: buffer too small");
}
void

@ -54,36 +54,36 @@ TEST_CASE("Test PeerStats BEncode", "[PeerStats]")
std::string asString = (const char*)raw.data();
constexpr std::string_view expected =
"d"
"13:lastRCUpdated"
"i15e"
"24:leastRCRemainingLifetime"
"i14e"
"24:longestRCReceiveInterval"
"i13e"
"21:numConnectionAttempts"
"i1e"
"22:numConnectionSuccesses"
"i2e"
"23:numConnectionRejections"
"i3e"
"22:numConnectionSuccesses"
"i2e"
"21:numConnectionTimeouts"
"i4e"
"13:numPathBuilds"
"i5e"
"22:numDistinctRCsReceived"
"i10e"
"10:numLateRCs"
"i11e"
"19:numPacketsAttempted"
"i6e"
"14:numPacketsSent"
"i7e"
"17:numPacketsDropped"
"i8e"
"16:numPacketsResent"
"i9e"
"22:numDistinctRCsReceived"
"i10e"
"10:numLateRCs"
"i11e"
"14:numPacketsSent"
"i7e"
"13:numPathBuilds"
"i5e"
"24:peakBandwidthBytesPerSec"
"i12e"
"24:longestRCReceiveInterval"
"i13e"
"24:leastRCRemainingLifetime"
"i14e"
"13:lastRCUpdated"
"i15e"
"e";
CHECK(asString == expected);

Loading…
Cancel
Save