Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Jul 19, 2021

Just writing to a log will be easily missed.

The same is done in CTxMemPool::check() and other debug checks for
developers, like lock order debugging.

Can be reviewed with https://en.wikipedia.org/wiki/De_Morgan%27s_laws and
git difftool --tool=meld cccc47b0ef~ cccc47b0ef

The same is done in CTxMemPool::check() and other debug checks for
developers, like lock order debugging.
@jonatack
Copy link
Member

Approach ACK

@DrahtBot DrahtBot added the P2P label Jul 19, 2021
Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK cccc47b

Tested with the following changes

--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -463,6 +463,7 @@ void CAddrMan::Check()
 {
 #ifdef DEBUG_ADDRMAN
     AssertLockHeld(cs);
+    LogPrint(BCLog::ADDRMAN, "Check addrman\n");

--- a/src/addrman.h
+++ b/src/addrman.h
@@ -122,6 +122,7 @@ public:
  *    * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
  *      consistency checks for the entire data structure.
  */

+#define DEBUG_ADDRMAN
 
 //! total number of buckets for tried addresses
 #define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8

assert(setTried.count(vvTried[n][i]));
assert(mapInfo[vvTried[n][i]].GetTriedBucket(nKey, m_asmap) == n);
assert(mapInfo[vvTried[n][i]].GetBucketPosition(nKey, false, n) == i);
setTried.erase(vvTried[n][i]);
Copy link
Member

@jonatack jonatack Jul 19, 2021

Choose a reason for hiding this comment

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

nit, spacing here is off by one

//! Perform consistency check. Returns an error code or zero.
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
#endif
EXCLUSIVE_LOCKS_REQUIRED(cs);
Copy link
Member

Choose a reason for hiding this comment

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

     //! Consistency check
-    void Check()
-        EXCLUSIVE_LOCKS_REQUIRED(cs);
+    void Check() EXCLUSIVE_LOCKS_REQUIRED(cs);

Copy link
Contributor

@lsilva01 lsilva01 left a comment

Choose a reason for hiding this comment

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

Code Review and tested ACK fa4bfef on Ubuntu 20.04. The change simplifies the CAddrMan::Check() code.

@DrahtBot
Copy link
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.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept and code review ACK fa4bfef

Also built locally with #define DEBUG_ADDRMAN set in src/addrman.cpp (the CI result for this PR obviously doesn't have any value, as the modified method CAddrMan::Check() is empty after the pre-processor run)

@jonatack
Copy link
Member

jonatack commented Jul 20, 2021

re-ACK fa4bfef have been testing this rebased to current master with DEBUG_ADDRMAN defined

could squash

@jnewbery
Copy link
Contributor

jnewbery commented Jul 21, 2021

I've added these two commits to #20233, since the two PRs are doing overlapping things.

EDIT: removed them again after discussing with Marco.

@lsilva01
Copy link
Contributor

If DEBUG_ADDRMAN is set, the following error will occur when addrman_tests is run:

$ src/test/test_bitcoin --run_test=addrman_tests --log_level=unit_scope
Running 18 test cases...
Entering test module "Bitcoin Core Test Suite"
test/addrman_tests.cpp(124): Entering test suite "addrman_tests"
test/addrman_tests.cpp(126): Entering test case "addrman_simple"
test_bitcoin: addrman.cpp:518: void CAddrMan::Check(): Assertion `!nKey.IsNull()' failed.
unknown location(0): fatal error: in "addrman_tests/addrman_simple": signal: SIGABRT (application abort requested)
test/addrman_tests.cpp(133): last checkpoint
test/addrman_tests.cpp(126): Leaving test case "addrman_simple"; testing time: 144057us

test/addrman_tests.cpp(175): Entering test case "addrman_ports"
test_bitcoin: util/system.cpp:654: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
unknown location(0): fatal error: in "addrman_tests/addrman_ports": signal: SIGABRT (application abort requested)
test/addrman_tests.cpp(175): last checkpoint: "addrman_ports" fixture ctor
test/addrman_tests.cpp(175): Leaving test case "addrman_ports"; testing time: 287us
....

If the line 518 (assert(!nKey.IsNull())) of addrman.cpp is commented out, the test will run successfully.

void CAddrMan::Check()
{
#ifdef DEBUG_ADDRMAN
    AssertLockHeld(cs);
// ....
    assert(!setTried.size());
    assert(!mapNew.size());
    // assert(!nKey.IsNull());
#endif
}

@jnewbery
Copy link
Contributor

If DEBUG_ADDRMAN is set, the following error will occur when addrman_tests is run ...

@lsilva01 this is fixed in the first commit of #20233.

@practicalswift
Copy link
Contributor

Concept ACK

@vasild
Copy link
Contributor

vasild commented Jul 26, 2021

This will make it inconvenient to do something other than abort() on Check() failure.

For example, it might make sense to do the check right after Unserialize() and if it fails - throw std::ios_base::failure as if disk corruption has occurred.

In general assert() should be used when a possible programming error has been detected, not on bad input.

@maflcko maflcko closed this Sep 10, 2021
@maflcko maflcko deleted the 2107-addrmanCheckAssert branch September 10, 2021 15:21
@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants