From dd3c8eaa3399b28dc78a883ff78cbe7cc5c31b5b Mon Sep 17 00:00:00 2001 From: Jarol Rodriguez Date: Tue, 6 Apr 2021 17:54:29 -0400 Subject: [PATCH 1/6] rpc: swap position of banned_until and ban_created fields A ban expires after its creation. Therefore, for the listbanned RPC, position banned_until after ban_created in help and output. --- src/rpc/net.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 96533a50c84..41e5ea1c6c1 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -750,8 +750,8 @@ static RPCHelpMan listbanned() {RPCResult::Type::OBJ, "", "", { {RPCResult::Type::STR, "address", ""}, - {RPCResult::Type::NUM_TIME, "banned_until", ""}, {RPCResult::Type::NUM_TIME, "ban_created", ""}, + {RPCResult::Type::NUM_TIME, "banned_until", ""}, }}, }}, RPCExamples{ @@ -774,8 +774,8 @@ static RPCHelpMan listbanned() const CBanEntry& banEntry = entry.second; UniValue rec(UniValue::VOBJ); rec.pushKV("address", entry.first.ToString()); - rec.pushKV("banned_until", banEntry.nBanUntil); rec.pushKV("ban_created", banEntry.nCreateTime); + rec.pushKV("banned_until", banEntry.nBanUntil); bannedAddresses.push_back(rec); } From c95c61657afd058b46549fb3d65633d7c736f5fc Mon Sep 17 00:00:00 2001 From: Jarol Rodriguez Date: Tue, 6 Apr 2021 18:18:52 -0400 Subject: [PATCH 2/6] doc: improve listbanned help Add descriptions for the address, ban_created, and banned_until fields. --- src/rpc/net.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 41e5ea1c6c1..27ff6bcdafc 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -749,9 +749,9 @@ static RPCHelpMan listbanned() { {RPCResult::Type::OBJ, "", "", { - {RPCResult::Type::STR, "address", ""}, - {RPCResult::Type::NUM_TIME, "ban_created", ""}, - {RPCResult::Type::NUM_TIME, "banned_until", ""}, + {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"}, + {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"}, + {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"}, }}, }}, RPCExamples{ From 5456b345312857981cb426712f0665800c682e09 Mon Sep 17 00:00:00 2001 From: Jarol Rodriguez Date: Tue, 6 Apr 2021 23:01:10 -0400 Subject: [PATCH 3/6] rpc: add ban_duration field to listbanned --- src/rpc/net.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 27ff6bcdafc..b11138cacc9 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -752,6 +752,7 @@ static RPCHelpMan listbanned() {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"}, {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"}, {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"}, + {RPCResult::Type::NUM_TIME, "ban_duration", "The ban duration, in seconds"}, }}, }}, RPCExamples{ @@ -776,6 +777,7 @@ static RPCHelpMan listbanned() rec.pushKV("address", entry.first.ToString()); rec.pushKV("ban_created", banEntry.nCreateTime); rec.pushKV("banned_until", banEntry.nBanUntil); + rec.pushKV("ban_duration", (banEntry.nBanUntil - banEntry.nCreateTime)); bannedAddresses.push_back(rec); } From 3e978d1a5dbd43f85bd03e759984ab1f209d6e34 Mon Sep 17 00:00:00 2001 From: Jarol Rodriguez Date: Tue, 6 Apr 2021 23:45:31 -0400 Subject: [PATCH 4/6] rpc: add time_remaining field to listbanned --- src/rpc/net.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index b11138cacc9..4826f091a6b 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -753,6 +753,7 @@ static RPCHelpMan listbanned() {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"}, {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the ban expires"}, {RPCResult::Type::NUM_TIME, "ban_duration", "The ban duration, in seconds"}, + {RPCResult::Type::NUM_TIME, "time_remaining", "The time remaining until the ban expires, in seconds"}, }}, }}, RPCExamples{ @@ -768,6 +769,7 @@ static RPCHelpMan listbanned() banmap_t banMap; node.banman->GetBanned(banMap); + const int64_t current_time{GetTime()}; UniValue bannedAddresses(UniValue::VARR); for (const auto& entry : banMap) @@ -778,6 +780,7 @@ static RPCHelpMan listbanned() rec.pushKV("ban_created", banEntry.nCreateTime); rec.pushKV("banned_until", banEntry.nBanUntil); rec.pushKV("ban_duration", (banEntry.nBanUntil - banEntry.nCreateTime)); + rec.pushKV("time_remaining", (banEntry.nBanUntil - current_time)); bannedAddresses.push_back(rec); } From 60290d3f5ec8e7e3b8cb1ebae02d5d72f6005184 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 6 Apr 2021 19:36:10 +0200 Subject: [PATCH 5/6] test: increase listbanned unit test coverage Add test coverage for the new ban_duration and time_remaining fields. While here, some code improvements. --- src/test/rpc_tests.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 67e70b3bc39..1e1b0518a79 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -269,9 +269,9 @@ BOOST_AUTO_TEST_CASE(rpc_ban) ar = r.get_array(); o1 = ar[0].get_obj(); adr = find_value(o1, "address"); - UniValue banned_until = find_value(o1, "banned_until"); + int64_t banned_until{find_value(o1, "banned_until").get_int64()}; BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24"); - BOOST_CHECK_EQUAL(banned_until.get_int64(), 9907731200); // absolute time check + BOOST_CHECK_EQUAL(banned_until, 9907731200); // absolute time check BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned"))); @@ -280,11 +280,16 @@ BOOST_AUTO_TEST_CASE(rpc_ban) ar = r.get_array(); o1 = ar[0].get_obj(); adr = find_value(o1, "address"); - banned_until = find_value(o1, "banned_until"); + banned_until = find_value(o1, "banned_until").get_int64(); + const int64_t ban_created{find_value(o1, "ban_created").get_int64()}; + const int64_t ban_duration{find_value(o1, "ban_duration").get_int64()}; + const int64_t time_remaining{find_value(o1, "time_remaining").get_int64()}; + const int64_t now{GetTime()}; BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24"); - int64_t now = GetTime(); - BOOST_CHECK(banned_until.get_int64() > now); - BOOST_CHECK(banned_until.get_int64()-now <= 200); + BOOST_CHECK(banned_until > now); + BOOST_CHECK(banned_until - now <= 200); + BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created); + BOOST_CHECK_EQUAL(time_remaining, banned_until - now); // must throw an exception because 127.0.0.1 is in already banned subnet range BOOST_CHECK_THROW(r = CallRPC(std::string("setban 127.0.0.1 add")), std::runtime_error); From d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec Mon Sep 17 00:00:00 2001 From: Jarol Rodriguez Date: Wed, 7 Apr 2021 01:54:27 -0400 Subject: [PATCH 6/6] doc: release notes for new listbanned fields --- doc/release-notes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/release-notes.md b/doc/release-notes.md index 334dfa80a41..9ed29315975 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -104,6 +104,10 @@ Updated RPCs with the `-json` option set, the following fields: `addresses`, `reqSigs` are no longer returned in the tx output of the response. (#20286) +- The `listbanned` RPC now returns two new numeric fields: `ban_duration` and `time_remaining`. + Respectively, these new fields indicate the duration of a ban and the time remaining until a ban expires, + both in seconds. Additionally, the `ban_created` field is repositioned to come before `banned_until`. (#21602) + Changes to Wallet or GUI related RPCs can be found in the GUI or Wallet section below. New RPCs