Skip to content

Commit 76a41eb

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#21602: rpc: add additional ban time fields to listbanned
d3b0b08 doc: release notes for new listbanned fields (Jarol Rodriguez) 60290d3 test: increase listbanned unit test coverage (Jon Atack) 3e978d1 rpc: add time_remaining field to listbanned (Jarol Rodriguez) 5456b34 rpc: add ban_duration field to listbanned (Jarol Rodriguez) c95c616 doc: improve listbanned help (Jarol Rodriguez) dd3c8ea rpc: swap position of banned_until and ban_created fields (Jarol Rodriguez) Pull request description: This PR adds a `ban_duration` and `time_remaining` field to the `listbanned` RPC command. Thanks to jonatack, this PR also expands the `listbanned` test coverage to include these new fields It's useful to keep track of `ban_duration` as this is another data point on which to sort banned peers. I found this helpful in adding additional context columns to the GUI `bantablemodel` as part of a follow-up PR. As [suggested](bitcoin#21602 (comment)) by jonatack, `time_remaining` is another useful user-centric data point. Since a ban always expires after its created, the `ban_created` field is now placed before the `banned_until` field. This new ordering is more logical. This PR also improves the `help listbanned` output by providing additional context to the descriptions of the `address`, `ban_created`, and `banned_until` fields. **Master: listbanned** ``` [ { "address": "1.2.3.4/32", "banned_until": 1617691101, "ban_created": 1617604701 }, { "address": "135.181.41.129/32", "banned_until": 1649140716, "ban_created": 1617604716 } ] ``` **PR: listbanned** ``` [ { "address": "1.2.3.4/32", "ban_created": 1617775773, "banned_until": 1617862173, "ban_duration": 86400, "time_remaining": 86392 }, { "address": "3.114.211.172/32", "ban_created": 1617753165, "banned_until": 1618357965, "ban_duration": 604800, "time_remaining": 582184 } ] ``` ACKs for top commit: jonatack: re-ACK d3b0b08 hebasto: ACK d3b0b08, tested on Linux Mint 20.1 (x86_64). MarcoFalke: review ACK d3b0b08 🕙 Tree-SHA512: 5b83ed2483344e546d57e43adc8a1ed7a1fff292124b14c86ca3a1aa2aec8b0f7198212fabff2c5145e7f726ca04ae567fe667b141254c7519df290cf63774e5
1 parent adea52a commit 76a41eb

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

doc/release-notes-21602.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Updated RPCs
2+
------------
3+
4+
5+
- The `listbanned` RPC now returns two new numeric fields: `ban_duration` and `time_remaining`.
6+
Respectively, these new fields indicate the duration of a ban and the time remaining until a ban expires,
7+
both in seconds. Additionally, the `ban_created` field is repositioned to come before `banned_until`. (#5976)

src/rpc/net.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,11 @@ static RPCHelpMan listbanned()
789789
{
790790
{RPCResult::Type::OBJ, "", "",
791791
{
792-
{RPCResult::Type::STR, "address", ""},
793-
{RPCResult::Type::NUM_TIME, "banned_until", ""},
794-
{RPCResult::Type::NUM_TIME, "ban_created", ""},
792+
{RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"},
793+
{RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"},
794+
{RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"},
795+
{RPCResult::Type::NUM_TIME, "ban_duration", "The ban duration, in seconds"},
796+
{RPCResult::Type::NUM_TIME, "time_remaining", "The time remaining until the ban expires, in seconds"},
795797
}},
796798
}},
797799
RPCExamples{
@@ -808,15 +810,18 @@ static RPCHelpMan listbanned()
808810

809811
banmap_t banMap;
810812
node.banman->GetBanned(banMap);
813+
const int64_t current_time{GetTime()};
811814

812815
UniValue bannedAddresses(UniValue::VARR);
813816
for (const auto& entry : banMap)
814817
{
815818
const CBanEntry& banEntry = entry.second;
816819
UniValue rec(UniValue::VOBJ);
817820
rec.pushKV("address", entry.first.ToString());
818-
rec.pushKV("banned_until", banEntry.nBanUntil);
819821
rec.pushKV("ban_created", banEntry.nCreateTime);
822+
rec.pushKV("banned_until", banEntry.nBanUntil);
823+
rec.pushKV("ban_duration", (banEntry.nBanUntil - banEntry.nCreateTime));
824+
rec.pushKV("time_remaining", (banEntry.nBanUntil - current_time));
820825

821826
bannedAddresses.push_back(rec);
822827
}

src/test/rpc_tests.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
317317
ar = r.get_array();
318318
o1 = ar[0].get_obj();
319319
adr = find_value(o1, "address");
320-
UniValue banned_until = find_value(o1, "banned_until");
320+
int64_t banned_until{find_value(o1, "banned_until").get_int64()};
321321
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
322-
BOOST_CHECK_EQUAL(banned_until.get_int64(), 9907731200); // absolute time check
322+
BOOST_CHECK_EQUAL(banned_until, 9907731200); // absolute time check
323323

324324
BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
325325

@@ -328,11 +328,16 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
328328
ar = r.get_array();
329329
o1 = ar[0].get_obj();
330330
adr = find_value(o1, "address");
331-
banned_until = find_value(o1, "banned_until");
331+
banned_until = find_value(o1, "banned_until").get_int64();
332+
const int64_t ban_created{find_value(o1, "ban_created").get_int64()};
333+
const int64_t ban_duration{find_value(o1, "ban_duration").get_int64()};
334+
const int64_t time_remaining{find_value(o1, "time_remaining").get_int64()};
335+
const int64_t now{GetTime()};
332336
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
333-
int64_t now = GetTime();
334-
BOOST_CHECK(banned_until.get_int64() > now);
335-
BOOST_CHECK(banned_until.get_int64()-now <= 200);
337+
BOOST_CHECK(banned_until > now);
338+
BOOST_CHECK(banned_until - now <= 200);
339+
BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created);
340+
BOOST_CHECK_EQUAL(time_remaining, banned_until - now);
336341

337342
// must throw an exception because 127.0.0.1 is in already banned subnet range
338343
BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.1 add")), std::runtime_error);

0 commit comments

Comments
 (0)