Skip to content

rpc: add additional ban time fields to listbanned - #21602

Merged
maflcko merged 6 commits into
bitcoin:masterfrom
jarolrod:ban-time-info
Apr 11, 2021
Merged

maflcko merged 6 commits into
bitcoin:masterfrom
jarolrod:ban-time-info

Conversation

@jarolrod

@jarolrod jarolrod commented Apr 5, 2021 •

Copy link
Copy Markdown
Contributor

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
  }
]

@jonatack

jonatack commented Apr 5, 2021 •

Copy link
Copy Markdown
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.

Comment thread src/rpc/net.cpp Resolved
@jarolrod

jarolrod commented Apr 5, 2021

Copy link
Copy Markdown
Contributor Author

@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!

@jarolrod

jarolrod commented Apr 5, 2021

Copy link
Copy Markdown
Contributor Author

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

@jarolrod jarolrod changed the title net, rpc: add ban_time field to listbanned net, rpc: add additional ban time fields to listbanned Apr 5, 2021
Comment thread src/rpc/net.cpp Resolved
@jarolrod

jarolrod commented Apr 5, 2021

Copy link
Copy Markdown
Contributor Author

updated from 8e34b5e to 833bebd, 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.

@sipa

sipa commented Apr 6, 2021

Copy link
Copy Markdown
Member

utACK 833bebd

@DrahtBot

DrahtBot commented Apr 6, 2021

Copy link
Copy Markdown
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@jonatack jonatack left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/rpc/net.cpp Resolved
@jonatack

jonatack commented Apr 6, 2021

Copy link
Copy Markdown
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

jarolrod and others added 5 commits April 6, 2021 18:22
A ban expires after its creation. Therefore, for the listbanned RPC,
position banned_until after ban_created in help and output.
Add descriptions for the address, ban_created, and banned_until fields.
Add test coverage for the new ban_duration and time_remaining fields.
While here, some code improvements.
@jarolrod jarolrod changed the title net, rpc: add additional ban time fields to listbanned rpc: add additional ban time fields to listbanned Apr 7, 2021
@jarolrod

jarolrod commented Apr 7, 2021

Copy link
Copy Markdown
Contributor Author

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

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK c5c920f394891f1d6958913330da564bf24a71a9

Comment thread doc/release-notes.md Resolved
@jarolrod

jarolrod commented Apr 8, 2021

Copy link
Copy Markdown
Contributor Author

Updated from c5c920f to d3b0b08

Changes:

  • Include PR# into release notes

@jonatack

jonatack commented Apr 8, 2021

Copy link
Copy Markdown
Member

re-ACK d3b0b08

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);

@promag promag left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review d3b0b08

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

I'd also squash 5456b34 with 3e978d1.

@hebasto

hebasto commented Apr 11, 2021

Copy link
Copy Markdown
Member

Concept ACK on:

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

@hebasto hebasto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK d3b0b08.

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 :)

Comment thread src/rpc/net.cpp

banmap_t banMap;
node.banman->GetBanned(banMap);
const int64_t current_time{GetTime()};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bitcoin/src/util/time.h

Lines 40 to 44 in dd01dc6

/**
* DEPRECATED
* Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
*/
int64_t GetTime();

Suggested change
const int64_t current_time{GetTime()};
const int64_t current_time{GetSystemTimeInSeconds()};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@hebasto hebasto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review ACK d3b0b08 🕙

Show signature and timestamp

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 -

@maflcko

maflcko commented Apr 11, 2021

Copy link
Copy Markdown
Member

(edited OP to remove @)

@maflcko
maflcko merged commit f6c44e9 into bitcoin:master Apr 11, 2021
Comment thread src/test/rpc_tests.cpp
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);

@maflcko maflcko Apr 11, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #21676

maflcko pushed a commit that referenced this pull request Apr 15, 2021
…_tests

fa40d6a test: Reset mocktime in the common setup (MarcoFalke)
fa78590 test: Use mocktime to avoid intermittent failure (MarcoFalke)

Pull request description:

  See #21602 (comment)

ACKs for top commit:
  jonatack:
    Code review ACK fa40d6a
  jarolrod:
    ACK fa40d6a

Tree-SHA512: 4967e006f3d2c4eb92f03c9086a6abe3190ad54755d251c30d20422c574bb1a154c06f3d5bcb0d4deaa3c4abfd3864d743b71d84897edd358e829bb42233ad12
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
A ban expires after its creation. Therefore, for the listbanned RPC,
position banned_until after ban_created in help and output.

Github-Pull: bitcoin#21602
Rebased-From: dd3c8ea
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
Add descriptions for the address, ban_created, and banned_until fields.

Github-Pull: bitcoin#21602
Rebased-From: c95c616
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
Add test coverage for the new ban_duration and time_remaining fields.
While here, some code improvements.

Github-Pull: bitcoin#21602
Rebased-From: 60290d3
shaavan added a commit to shaavan/gui that referenced this pull request Jul 22, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
shaavan added a commit to shaavan/gui that referenced this pull request Jul 22, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
shaavan added a commit to shaavan/gui that referenced this pull request Jul 22, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
shaavan added a commit to shaavan/gui that referenced this pull request Jul 22, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
shaavan added a commit to shaavan/gui that referenced this pull request Jul 24, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
shaavan added a commit to shaavan/gui that referenced this pull request Jul 26, 2021
New ban table columns that incorporate: bitcoin/bitcoin#21602
New columns: IP/Netmask, Ban creation time, Ban Until time, Duration of
Ban, time remaining for node to unban
maflcko pushed a commit that referenced this pull request Dec 30, 2021
…ng ban

da349f1 test: check ban_duration and time_remaining after setting ban (brunoerg)

Pull request description:

  This PR adds functional test coverage for `ban_duration` and `time_remaining` introduced in #21602

ACKs for top commit:
  shaavan:
    ACK da349f1
  theStack:
    Tested ACK da349f1

Tree-SHA512: 51e63f3a36adb1c81e4d49426486af2cd9c8c4319f94e06a47fa7da8100a8b53c029d28d4a4771bdbf4e0a2bfb4ddd3740b9974bd08d8ff06f2a0fc2b6d8a6b5
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants