Skip to content

test: add functional test for anchors.dat - #21338

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
brunoerg:2021-03-anchors-test
Mar 24, 2021
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
brunoerg:2021-03-anchors-test

Conversation

@brunoerg

@brunoerg brunoerg commented Mar 2, 2021

Copy link
Copy Markdown
Contributor

This PR adds a functional test for anchors.dat.

It creates a node and adds 2 outbound block-relay-only connections and 5 inbound connections.
When the node is down, anchors.dat should contain the 2 addresses from the outbound block-relay-only connections.

@michaelfolkson

Copy link
Copy Markdown
Contributor

Concept ACK

I was thinking this should be a p2p_ test rather than a feature_ test but maybe you're right...

@brunoerg

brunoerg commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

I added feature_anchors.py in BASE_SCRIPTS

@jaonoctus

Copy link
Copy Markdown

CI Tests are failing for two reasons so far:

  1. The following scripts are not being run: ['feature_anchors.py']. Check the test lists in test_runner.py
  2. Some lint errors
feature_anchors.py:31:1: W293 blank line contains whitespace
feature_anchors.py:77:25: W292 no newline at end of file

@brunoerg

brunoerg commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

Thanks, @jaonoctus. Fixed!

@jaonoctus jaonoctus left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Concept ACK

@jaonoctus jaonoctus left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python's open(...) seems to be used to open text files without explicitly specifying encoding='utf8'

Comment thread test/functional/feature_anchors.py Resolved
@hebasto

hebasto commented Mar 2, 2021

Copy link
Copy Markdown
Member

Concept ACK.

@DrahtBot DrahtBot added the Tests label Mar 2, 2021
@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 13762cd to b1a5e04 Compare March 2, 2021 18:46
Comment thread test/functional/feature_anchors.py Outdated

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.

could make the path a named variable to avoid repeating the join three times?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, no problem! thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 83b5836 to 5304652 Compare March 2, 2021 19:34

@leonardojobim leonardojobim left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approach ACK.

Tested 5304652 on Ubuntu 20.04.
The test has failed intermittently when run several times.
The reason is the file anchors.dat sometimes has 2 lines, as shown below.
A simple solution for this would be concatenate all line before the validations.

2021-03-02T19:52:13.969000Z TestFramework (INFO): Check the addresses in anchors.dat
item (first line): fabfb5da02fc5a030000e1f505000000000000000000000000000000000000ffff7f0000012b6efc5a030000e1f505000000000000000000000000000000000000ffff7f0000012b6d1c665ef1c1d383c4869170e021306a945bdbfb9f39c12bb2e0
ip_port: 7f0000012b6e
ip_port in item: True
ip_port: 7f0000012b6d
ip_port in item: True
item (second line): 279f548ecaf3
ip_port: 7f0000012b6e
ip_port in item: False
2021-03-02T19:52:13.969000Z TestFramework (ERROR): Assertion failed

@brunoerg

brunoerg commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

@leonardojobim Amazing review! I'm working on it at this moment! Thank you!

@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 05a8912 to a83908f Compare March 2, 2021 21:03
@brunoerg

brunoerg commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

@leonardojobim fixed! could you please test it again?

@DrahtBot

DrahtBot commented Mar 2, 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.

@leonardojobim leonardojobim left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested ACK a83908f

The tests were run multiple times successfully.

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

Concept ACK and some very light review, will review a bit more soon.

The file permission of the new test file should be 755 instead of 644.

-rwxr-xr-x 1 1685 Jan 21 20:54 test/functional/feature_abortnode.py*
-rw-r--r-- 1 2902 Mar  6 13:37 test/functional/feature_anchors.py
-rwxr-xr-x 1 4242 Mar  6 13:10 test/functional/feature_asmap.py*

In order to run the test, I had to first run:

sudo chmod 755 test/functional/feature_anchors.py

Nit: in terms of logging and function naming, I prefer "Test" to "Check that/if"

Don't hesitate to try running Python linters on your functional test changes like pycodestyle and black to see their suggestions.

Comment thread test/functional/feature_anchors.py Resolved
Comment thread test/functional/feature_anchors.py Resolved
Comment thread test/functional/feature_anchors.py Resolved
Comment thread test/functional/feature_anchors.py Resolved
@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 41b10a0 to 9ef15a0 Compare March 6, 2021 13:39
@brunoerg

brunoerg commented Mar 6, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for the review @jonatack. Just pushed some fixes!

Comment thread test/functional/feature_anchors.py Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hello Bruno. I think we should concatenate regtest sub directory using the os.path.join() function instead of using + "/regtest" (just to avoid assuming the test is running in a Unix based OS).
node0_anchors_path = os.path.join( self.nodes[0].datadir, "regtest", "anchors.dat" )

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.

Good point.

In general, don't hesitate to refer to test/functional/feature_asmap.py for an example of a similar kind of test file.

    self.datadir = os.path.join(self.node.datadir, self.chain)
    self.default_asmap = os.path.join(self.datadir, DEFAULT_ASMAP_FILENAME)
    self.asmap_raw = os.path.join(os.path.dirname(os.path.realpath(__file__)), ASMAP)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hello, Abel.. Thanks a lot for the review! Just changed it.

@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 87f8dcc to 0605a3c Compare March 7, 2021 23:58

@aarmoa aarmoa left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approach ACK.

Tested 0605a3c on Linux Mint 20.

I tried breaking the test without successing with the following:

  • line 29: change range(2) for range(1)
    Assertion in check_node_connections(node=self.nodes[0], num_in=5, num_out=2) fails (AssertionError: not(1 == 2))
  • line 36: change range(5) for range(6)
    Assertion in check_node_connections(node=self.nodes[0], num_in=5, num_out=2) fails as expected (AssertionError: not(6 == 5))
  • Added a line to change expected ip address (ip = "7f000002") before line 73
    Assertion that block_relay_nodes_port are included in anchors fail as expected
  • Added a line to change expected ip address (ip = "7f000002") before line 76
    Assertion that inbound_nodes_port are not included in anchors still passes as expected
  • Commented out line 81 (self.start_node(0))
    Assertion that anchors file does not exist fails as expected.

Executing the new test alone from command line works correctly (test/functional/feature_anchors.py).

Executing all functional tests with test/functional/test_runner.py includes new test:

187/202 - feature_anchors.py passed, Duration: 4 s

TEST | STATUS | DURATION

example_test.py | ✓ Passed | 11 s
feature_abortnode.py | ✓ Passed | 34 s
feature_anchors.py | ✓ Passed | 4 s
.
.
.

@simsbluebox simsbluebox left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ACK 0605a3c, I ran tests OK on Debian 10.8 (buster) and reviewed the code and it looks fine for me.

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

Seems fine. Left two comments

Comment thread test/functional/feature_anchors.py Outdated

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.

wouldn't it make more sense for this to be an array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. I am concatenating the lines to verify if the addresses are into this. I don't think an array would be useful here.

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.

Let's assume ip_port is aa and anchors is ['fa', 'af']

With array:

>>> 'aa' in ['fa', 'af']
False

With string

>>> 'aa' in 'faaf'
True

Am I missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm using string for the reason you're showing with your example. I think the question about using array or string is: how is anchors.dat structured?

Let's suppose:

ip_port is 7f0000013e01 and the file content is:
fabfb5da02fc5a030000e1f505000000000000000000000000000000000000ffff7f0000013e01fc5a030000e1f505000000000000000000000000000000000000ffff7f0000013e0075f7d7cf33e71e42f8add50c305f091fb881b6fa5633932559d86cd9f6141260

but we have:
fabfb5da02fc5a030000e1f505000000000000000000000000000000000000ffff7f00 (breaks line)
00013e01fc5a030000e1f505000000000000000000000000000000000000ffff7f000 (breaks line)
0013e0075f7d7cf33e71e42f8add50c305f091fb881b6fa5633932559d86cd9f6141260 (breaks line)

in this case, my logic using string would be useful.
But how is the file structure for this case?

Anyway, almost 100% of my tests showed me that anchors.dat usually has just one line.

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.

anchors.dat is binary data, so why would newlines matter and what is the point of rstrip?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can remove rstrip I guess, it doesn't affect anything, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So, can I read only the first line?
I was using array but the test `was failing, assuming:

item (first line): fabfb5da02fc5a030000e1f505000000000000000000000000000000000000ffff7f0000012b6efc5a030000e1f505000000000000000000000000000000000000ffff7f0000012b6d1c665ef1c1d383c4869170e021306a945bdbfb9f39c12bb2e0
ip_port: 7f0000012b6e
ip_port in item: True
ip_port: 7f0000012b6d
ip_port in item: True
item (second line): 279f548ecaf3
ip_port: 7f0000012b6e
ip_port in item: False`

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.

If newlines don't have a meaning, they shouldn't be interpreted. You can just use read()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, just pushed the changes. It makes sense. Could you please check?

Comment thread test/functional/feature_anchors.py Outdated

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 think this check can be skipped (will be checked by open already)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Make sense.
When the file doesn't exit in the node directory the open function will return a FileNotFoundError.
Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch 2 times, most recently from 99af51c to c94f5da Compare March 16, 2021 21:56
@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 832f6ad to e05c42a Compare March 19, 2021 23:26
@brunoerg
brunoerg force-pushed the 2021-03-anchors-test branch from 061cc28 to 581791c Compare March 19, 2021 23:31
@ottosch

ottosch commented Mar 20, 2021

Copy link
Copy Markdown

Tested ACK. I ran it 100x and they all passed.
OS: Archlinux.

tack-21338

@maflcko

maflcko commented Mar 20, 2021

Copy link
Copy Markdown
Member

Concept ACK 581791c

@maflcko
maflcko requested a review from hebasto March 20, 2021 07:36

@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 581791c

from test_framework.util import assert_equal


def check_node_connections(*, node, num_in, num_out):

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 code is used once only. What are benefits of the separated function here?

# Copyright (c) 2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test Anchors functionality"""

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.

nit:

Suggested change
"""Test Anchors functionality"""
"""Test block-relay-only anchors functionality"""

self.setup_nodes()

def run_test(self):
self.log.info("Add 2 block-relay-only connections to node 0")

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.

nit: here and after s/"node 0"/"node"/ as it is the only node in this test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perfect, thanks!


def run_test(self):
self.log.info("Add 2 block-relay-only connections to node 0")
for i in range(2):

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.

nit: Numbers of connections, i.e., 2 and 5, could be named variables.

self.start_node(0)

self.log.info("When node starts, check if anchors.dat doesn't exist anymore")
assert not os.path.exists(node0_anchors_path)

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.

The same assertion seems reasonable to place in the test beginning too, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, make sense.

@maflcko
maflcko merged commit 7ce7ce5 into bitcoin:master Mar 24, 2021
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Jun 11, 2021
… for the first time

ef99d03 test: check anchors.dat when node starts for the first time (bruno)

Pull request description:

  See bitcoin/bitcoin#21338 (comment), bitcoin/bitcoin#21338 (comment), bitcoin/bitcoin#21338 (comment).

ACKs for top commit:
  laanwj:
    Code review ACK ef99d03

Tree-SHA512: 505f1f34fbc0c72a92968883be0f1c5f169a4ba3aa8a56e1ce8bc5e514f49e3a17ce51fd40be0073dc4bc06eaeda36dfe90ace843c181170e34643226afd78ef
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
581791c test: add functional test for anchors.dat (bruno)

Pull request description:

  This PR adds a functional test for anchors.dat.

  It creates a node and adds 2 outbound block-relay-only connections and 5 inbound connections.
  When the node is down, anchors.dat should contain the 2 addresses from the outbound block-relay-only connections.

ACKs for top commit:
  MarcoFalke:
    Concept ACK 581791c
  hebasto:
    ACK 581791c

Tree-SHA512: 77038b09e36ee5ae473a26d6f566c0ed283af258c34df8486706a24f72b05abab621a293ac886d03849bc45bc28be7336137252225b25aff393baa6b5238688c
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
… first time

ef99d03 test: check anchors.dat when node starts for the first time (bruno)

Pull request description:

  See bitcoin#21338 (comment), bitcoin#21338 (comment), bitcoin#21338 (comment).

ACKs for top commit:
  laanwj:
    Code review ACK ef99d03

Tree-SHA512: 505f1f34fbc0c72a92968883be0f1c5f169a4ba3aa8a56e1ce8bc5e514f49e3a17ce51fd40be0073dc4bc06eaeda36dfe90ace843c181170e34643226afd78ef
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 2, 2022
Summary:
```
It creates a node and adds 2 outbound block-relay-only connections and 5 inbound connections.
When the node is down, anchors.dat should contain the 2 addresses from the outbound block-relay-only connections.
```

Backport of [[bitcoin/bitcoin#21338 | core#21338]].

Ref T1696.

Test Plan:
  ./test/functional/test_runner.py feature_anchors

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1696

Differential Revision: https://reviews.bitcoinabc.org/D10959
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 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.