rpc: add additional ban time fields to listbanned #21602

pull jarolrod wants to merge 6 commits into bitcoin:master from jarolrod:ban-time-info changing 3 files +24 −10
  1. jarolrod commented at 6:43 AM on April 5, 2021: member

    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 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
      }
    ]
    
  2. fanquake added the label P2P on Apr 5, 2021
  3. fanquake added the label RPC/REST/ZMQ on Apr 5, 2021
  4. jonatack commented at 4:27 PM on April 5, 2021: member

    I'm not sure about this.

    Would this change cause versioning/compatibility issues with previous/existing ban entries? (test coverage needed). It may be better to avoid changing CBanEntry, and have the RPC handle the calculation.

    As a human user, I've found myself wishing for a "time_remaining" field in listbanned.

  5. in src/rpc/net.cpp:755 in 9c896134b1 outdated
     753 | -                        {RPCResult::Type::NUM_TIME, "banned_until", ""},
     754 | -                        {RPCResult::Type::NUM_TIME, "ban_created", ""},
     755 | +                        {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"},
     756 | +                        {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the node is banned until"},
     757 | +                        {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"},
     758 | +                        {RPCResult::Type::NUM_TIME, "ban_time", "The amount of time the ban will last for in seconds"},
    


    jonatack commented at 4:28 PM on April 5, 2021:
                            {RPCResult::Type::NUM_TIME, "ban_time", "The ban duration, in seconds"},
    

    jarolrod commented at 4:55 PM on April 5, 2021:

    Thanks for making my original text look like a rube goldberg machine 🤗


    jarolrod commented at 9:54 PM on April 5, 2021:

    addressed in 8e34b5ed06f3ca0865b9b7dca6e31f2e95c6f5c8

  6. jarolrod commented at 4:50 PM on April 5, 2021: member

    @jonatack

    It may be better to avoid changing CBanEntry, and have the RPC handle the calculation.

    You're right, taking into consideration compatibility guarantees, performing the calculation is the simplest path. I will revise this PR to perform the calculation and avoid changing CBanEntry and Banman.

    As a human user, I've found myself wishing for a "time_remaining" field in listbanned.

    I will look into adding this field, thanks for the review!

  7. jarolrod force-pushed on Apr 5, 2021
  8. jarolrod commented at 9:56 PM on April 5, 2021: member

    updated from 9c896134b12f012d0736742693df4b50c133e506 to 8e34b5ed06f3ca0865b9b7dca6e31f2e95c6f5c8, addressed @jonatack suggestions:

    • perform the calculation inside the RPC command instead of changing CBanEntry and Banman. This avoids any compatibility issues.
    • Add additional ban_time_remaining field to listbanned
  9. jarolrod renamed this:
    net, rpc: add ban_time field to listbanned
    net, rpc: add additional ban time fields to listbanned
    on Apr 5, 2021
  10. in src/rpc/net.cpp:781 in 523471a385 outdated
     777 | @@ -777,6 +778,7 @@ static RPCHelpMan listbanned()
     778 |          rec.pushKV("banned_until", banEntry.nBanUntil);
     779 |          rec.pushKV("ban_created", banEntry.nCreateTime);
     780 |          rec.pushKV("ban_time", (banEntry.nBanUntil - banEntry.nCreateTime));
     781 | +        rec.pushKV("ban_time_remaining", (banEntry.nBanUntil - GetTime()));
    


    sipa commented at 10:12 PM on April 5, 2021:

    Maybe query the time once before the loop, and use it for every iteration? This is needlessly slow and inconsistent.


    jarolrod commented at 10:15 PM on April 5, 2021:

    whoops, brain fart, forgot we were in a loop


    jarolrod commented at 10:27 PM on April 5, 2021:

    addressed in 833bebdece0f6fac5fecf602e88ef7c114f2416c

  11. jarolrod force-pushed on Apr 5, 2021
  12. jarolrod commented at 10:30 PM on April 5, 2021: member

    updated from 8e34b5e to 833bebdece0f6fac5fecf602e88ef7c114f2416c, addressed @sipa suggestion:

    • GetTime() was being called inside of a loop due to an oversight. This is unnecessary and can produce inconsistent results. This moves GetTime() out of the loop so it is only called once.
  13. sipa commented at 12:54 AM on April 6, 2021: member

    utACK 833bebdece0f6fac5fecf602e88ef7c114f2416c

  14. DrahtBot commented at 2:28 AM on April 6, 2021: member

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #19825 (rpc: simpler setban and new ban manipulation commands by dhruv)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  15. in src/rpc/net.cpp:784 in 833bebdece outdated
     779 | @@ -776,6 +780,8 @@ static RPCHelpMan listbanned()
     780 |          rec.pushKV("address", entry.first.ToString());
     781 |          rec.pushKV("banned_until", banEntry.nBanUntil);
     782 |          rec.pushKV("ban_created", banEntry.nCreateTime);
     783 | +        rec.pushKV("ban_time", (banEntry.nBanUntil - banEntry.nCreateTime));
     784 | +        rec.pushKV("ban_time_remaining", (banEntry.nBanUntil - current_time));
    


    jonatack commented at 4:46 PM on April 6, 2021:

    suggestions

    @@ -751,10 +751,10 @@ static RPCHelpMan listbanned()
                     {RPCResult::Type::OBJ, "", "",
                         {
                             {RPCResult::Type::STR, "address", "The IP/Subnet of the banned node"},
    -                        {RPCResult::Type::NUM_TIME, "banned_until", "The " + UNIX_EPOCH_TIME + " the node is banned until"},
                             {RPCResult::Type::NUM_TIME, "ban_created", "The " + UNIX_EPOCH_TIME + " the ban was created"},
    -                        {RPCResult::Type::NUM_TIME, "ban_time", "The ban duration, in seconds"},
    -                        {RPCResult::Type::NUM_TIME, "ban_time_remaining", "The amount of seconds until the ban expires"},
    +                        {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{
    @@ -770,7 +770,7 @@ static RPCHelpMan listbanned()
     
         banmap_t banMap;
         node.banman->GetBanned(banMap);
    -    int64_t current_time = GetTime();
    +    const int64_t current_time{GetTime()};
     
         UniValue bannedAddresses(UniValue::VARR);
         for (const auto& entry : banMap)
    @@ -778,10 +778,10 @@ 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("ban_time", (banEntry.nBanUntil - banEntry.nCreateTime));
    -        rec.pushKV("ban_time_remaining", (banEntry.nBanUntil - current_time));
    +        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);
         }
    

    result

    $ bitcoin-cli -signet help listbanned
    listbanned
    
    List all manually banned IPs/Subnets.
    
    Result:
    [                              (json array)
      {                            (json object)
        "address" : "str",         (string) The IP/Subnet of the banned node
        "ban_created" : xxx,       (numeric) The UNIX epoch time the ban was created
        "banned_until" : xxx,      (numeric) The UNIX epoch time the ban expires
        "ban_duration" : xxx,      (numeric) The ban duration, in seconds
        "time_remaining" : xxx     (numeric) The time remaining until the ban expires, in seconds
      },
    
    $ bitcoin-cli -signet setban 51.15.12.61 add 36
    
    $ bitcoin-cli -signet listbanned
    [
      {
        "address": "51.15.12.61/32",
        "ban_created": 1617727507,
        "banned_until": 1617727543,
        "ban_duration": 36,
        "time_remaining": 29
      }
    ]
    

    jarolrod commented at 6:14 AM on April 7, 2021:

    Thanks for the in-depth review! Special thanks for contributing a test. Addressed in c5c920f

  16. jonatack commented at 4:53 PM on April 6, 2021: member

    Tested approach ACK

    Various ideas and suggestions below grouped into one comment. If you retouch, perhaps update the relevant help in each commit that adds a field, so each commit is self-contained. Also, needs a release note.

  17. jonatack commented at 5:40 PM on April 6, 2021: member

    @jarolrod feel free to pull in the last commit in this branch https://github.com/jonatack/bitcoin/commits/pr21602-suggestions to add unit test coverage for these changes

  18. 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.
    dd3c8eaa33
  19. doc: improve listbanned help
    Add descriptions for the address, ban_created, and banned_until fields.
    c95c61657a
  20. rpc: add ban_duration field to listbanned 5456b34531
  21. rpc: add time_remaining field to listbanned 3e978d1a5d
  22. test: increase listbanned unit test coverage
    Add test coverage for the new ban_duration and time_remaining fields.
    While here, some code improvements.
    60290d3f5e
  23. jarolrod force-pushed on Apr 7, 2021
  24. jarolrod renamed this:
    net, rpc: add additional ban time fields to listbanned
    rpc: add additional ban time fields to listbanned
    on Apr 7, 2021
  25. jarolrod commented at 6:21 AM on April 7, 2021: member

    updated from 833bebd to c5c920f, addressed @jonatack suggestions

    • swap positions of ban_created and banned_until
    • rename ban_time to ban_duration
    • rename ban_time_remaining to time_remaining
    • drop #include util/time.h, this is already included by something else.
      • Other commands already use time.h functions in this part of the code.
    • Add test coverage for new listbanned fields, thanks to jon
    • Add release notes for new fields + banned_until reposition
  26. in doc/release-notes.md:109 in c5c920f394 outdated
     103 | @@ -104,6 +104,10 @@ Updated RPCs
     104 |    with the `-json` option set, the following fields: `addresses`, `reqSigs` are no longer
     105 |    returned in the tx output of the response. (#20286)
     106 |  
     107 | +- The `listbanned` RPC now returns two new numeric fields: `ban_duration` and `time_remaining`.
     108 | +  Respectively, these new fields indicate the duration of a ban and the time remaining until a ban expires,
     109 | +  both in seconds. Additionally, the `ban_created` field is repositioned to come before `banned_until`.
    


    jonatack commented at 5:03 PM on April 8, 2021:
      both in seconds. Additionally, the `ban_created` field is repositioned to come before `banned_until`.  (#21602)
    

    jarolrod commented at 5:23 PM on April 8, 2021:

    done in d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec

  27. jonatack commented at 5:08 PM on April 8, 2021: member

    Tested ACK c5c920f394891f1d6958913330da564bf24a71a9

  28. doc: release notes for new listbanned fields d3b0b08b0f
  29. jarolrod force-pushed on Apr 8, 2021
  30. jarolrod commented at 5:25 PM on April 8, 2021: member

    Updated from c5c920f to d3b0b08

    Changes:

    • Include PR# into release notes
  31. jonatack commented at 6:01 PM on April 8, 2021: member

    re-ACK d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec

    There's a small potential race in the added unit tests. I didn't hit while running the test, even with valgrind to slow it down, but maybe something like this would provide a margin:

    @@ -277,6 +277,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
     
         BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 200")));
         BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
    +    const int64_t now{GetTime()};
         ar = r.get_array();
         o1 = ar[0].get_obj();
         adr = find_value(o1, "address");
    @@ -284,12 +285,13 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
         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");
         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);
    +    BOOST_CHECK(ban_duration >= banned_until - ban_created - 1);
    +    BOOST_CHECK(ban_duration <= banned_until - ban_created + 1);
    +    BOOST_CHECK(time_remaining >= banned_until - now - 1);
    +    BOOST_CHECK(time_remaining <= banned_until - now + 1);
    
  32. promag commented at 3:43 PM on April 9, 2021: member

    Code review d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec

    I'd drop dd3c8eaa3399b28dc78a883ff78cbe7cc5c31b5b, but if kept then mark it move-only or squash with c95c61657afd058b46549fb3d65633d7c736f5fc.

    I'd also squash 5456b345312857981cb426712f0665800c682e09 with 3e978d1a5dbd43f85bd03e759984ab1f209d6e34.

  33. hebasto commented at 7:09 AM on April 11, 2021: member

    Concept ACK on:

    I found this helpful in adding additional context columns to the GUI bantablemodel as part of a follow-up PR.

  34. in src/rpc/net.cpp:772 in d3b0b08b0f
     768 | @@ -767,15 +769,18 @@ static RPCHelpMan listbanned()
     769 |  
     770 |      banmap_t banMap;
     771 |      node.banman->GetBanned(banMap);
     772 | +    const int64_t current_time{GetTime()};
    


    hebasto commented at 7:36 AM on April 11, 2021:

    MarcoFalke commented at 11:24 AM on April 11, 2021:

    banman uses GetTime() (mockable), so mixing non-mockable and mockable should be avoided


    hebasto commented at 11:26 AM on April 11, 2021:

    Ah, thanks!


    hebasto commented at 11:28 AM on April 11, 2021:

    So it should be GetTime<std::chrono::seconds>?


    MarcoFalke commented at 11:41 AM on April 11, 2021:

    I guess so, but that refactor should also be done for nCreateTime, banman, ...

  35. hebasto commented at 7:41 AM on April 11, 2021: member

    Approach ACK d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec.

    I found this helpful in adding additional context columns to the GUI bantablemodel as part of a follow-up PR.

    It seems, that adding additional time-related fields to the "Banned peers" table in the GUI does not depend on this PR :)

  36. hebasto approved
  37. hebasto commented at 11:27 AM on April 11, 2021: member

    ACK d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec, tested on Linux Mint 20.1 (x86_64).

  38. MarcoFalke commented at 11:35 AM on April 11, 2021: member

    review ACK d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec 🕙

    <details><summary>Show signature and timestamp</summary>

    Signature:

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512
    
    review ACK d3b0b08b0f04d2f1dbebbafd7ab0384dfe045dec 🕙
    -----BEGIN PGP SIGNATURE-----
    
    iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
    pUgCnQv+Pt8SJP4wssUe0R41DmjzJ0d0nEqEiWA38vaipjeD6zNgy5yBhuOmtsm/
    vdlSvsOogPjoXM7nrCEaODFRdM8Sj9cfFtPi94U6dmU8lIv92urpREeUm92Zf1PI
    2z0mMJwktzszLVK1t3p6Qh90iV3RDy/OE/kkKYOjs41wLMtt1akHchkGVVMuA6P9
    w3L8rVoG/6dlWsWd9H9bWvJ/0QEm12zK9hlHZONGBCHC4NmM/QEYLFqULmsAVmuw
    7h9XysqcQVCVTFcUD4Qk7y2WOM3vtW3OI2N+DzLK9tOcYvJ0LlGByNe5RSGnLzgl
    wewt7k2piUr8qSQNKIvCSIFoy3K3BuVAGWpvFmMgR6tEidhrMhMxFc+rCcvPQbDI
    gHypLP4J3DwU55PQ/vAO4IpsSudiVmgUbO9gkrZCJ5r9qMTgJhWtN7epkTgcVLUn
    aXbS1eLlMrEcnRt1VdLc42VOo4P755/lNg/QmZcQTub3Z46KxhOhf+gSTOut3KL6
    3lJzmjRC
    =qw8I
    -----END PGP SIGNATURE-----
    

    Timestamp of file with hash 4d1380dd6579b539625e1fcb8f85f0b5ec4b795d7be43d7e91a24d790b488c20 -

    </details>

  39. MarcoFalke commented at 11:36 AM on April 11, 2021: member

    (edited OP to remove @)

  40. MarcoFalke merged this on Apr 11, 2021
  41. MarcoFalke closed this on Apr 11, 2021

  42. in src/test/rpc_tests.cpp:292 in d3b0b08b0f
     291 | -    BOOST_CHECK(banned_until.get_int64() > now);
     292 | -    BOOST_CHECK(banned_until.get_int64()-now <= 200);
     293 | +    BOOST_CHECK(banned_until > now);
     294 | +    BOOST_CHECK(banned_until - now <= 200);
     295 | +    BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created);
     296 | +    BOOST_CHECK_EQUAL(time_remaining, banned_until - now);
    


    MarcoFalke commented at 11:47 AM on April 11, 2021:

    this may fail:

    Running 1 test case...
    test/rpc_tests.cpp(293): error: in "rpc_tests/rpc_ban": check time_remaining == banned_until - now has failed [200 != 199]
    
    *** 1 failure is detected in the test module "Bitcoin Core Test Suite"
    

    diff:

    diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
    index 3b6faf7bbb..be0d79aa86 100644
    --- a/src/test/rpc_tests.cpp
    +++ b/src/test/rpc_tests.cpp
    @@ -277,6 +277,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
     
         BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 200")));
         BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
    +    std::this_thread::sleep_for(std::chrono::milliseconds(1100)); // CPU scheduler puts this thread to sleep
         ar = r.get_array();
         o1 = ar[0].get_obj();
         adr = find_value(o1, "address");
    

    Can be fixed by using mocktime (SetTime()), or by relaxing the test.

    Also, the whole test can be moved to the functional test suite. For example, ./test/functional/rpc_setban.py


    MarcoFalke commented at 8:08 AM on April 14, 2021:

    See #21676

  43. sidhujag referenced this in commit 8537b7d752 on Apr 11, 2021
  44. MarcoFalke referenced this in commit a5e756b74e on Apr 15, 2021
  45. luke-jr referenced this in commit a9247f554e on Jun 27, 2021
  46. luke-jr referenced this in commit ce5bc62213 on Jun 27, 2021
  47. luke-jr referenced this in commit dcf9087bc3 on Jun 27, 2021
  48. luke-jr referenced this in commit 2017d7c807 on Jun 27, 2021
  49. luke-jr referenced this in commit 4438b91c8b on Jun 27, 2021
  50. shaavan referenced this in commit 00974f26d1 on Jul 22, 2021
  51. shaavan referenced this in commit f28b01525f on Jul 22, 2021
  52. shaavan referenced this in commit 22d737f7f4 on Jul 22, 2021
  53. shaavan referenced this in commit eb537dafb4 on Jul 22, 2021
  54. shaavan referenced this in commit 4368342913 on Jul 24, 2021
  55. shaavan referenced this in commit 959473992d on Jul 26, 2021
  56. MarcoFalke referenced this in commit f3d6a5ce77 on Dec 30, 2021
  57. sidhujag referenced this in commit 0acb5b4d02 on Dec 30, 2021
  58. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me