HomePhabricator

net: fix GetListenPort() to derive the proper port

Description

net: fix GetListenPort() to derive the proper port

Summary:

timedata: make it possible to reset the state

Add a new function TestOnlyResetTimeData() which would reset the
internal state used by GetTimeOffset(), GetAdjustedTime() and
AddTimeData().

This is needed so that unit tests that call AddTimeData() can restore
the state in order not to confuse other tests that rely on it.

Currently timedata_tests/addtimedata is the only test that modifies
the state (via AddTimeData()) and also the only test that relies on
that state.

timedata: rename variables to match the coding style

Rename the local variables in src/timedata.cpp:
setKnown -> g_sources
vTimeOffsets -> g_time_offsets
fDone -> g_warning_emitted

net: make CaptureMessage() mockable

Rename CaptureMessage() to CaptureMessageToFile() and introduce a
std::function variable called CaptureMessage whose value can be
changed by unit tests, should they need to inspect message contents.

net: pass Span by value to CaptureMessage()

Span is lightweight and need not be passed by const reference.

net: fix GetListenPort() to derive the proper port

GetListenPort() uses a simple logic: "if -port=P is given, then we
must be listening on P, otherwise we must be listening on 8333".
This is however not true if -bind= has been provided with :port part
or if -whitebind= has been provided. Thus, extend GetListenPort() to
return the port from -bind= or -whitebind=, if any.

Fixes https://github.com/bitcoin/bitcoin/issues/20184 (cases 1. 2. 3. 5.)

TestChainState is from core#20332

net: only assume all local addresses if listening on any

If -bind= is provided then we would bind only to a particular address
and should not add all the other addresses of the machine to the list of
local addresses.

Fixes https://github.com/bitcoin/bitcoin/issues/20184 (case 4.)

This is a backport of core#20196

Depends on D14689

Test Plan:
ninja all check-all

run new functional tests as per instructions

$ sudo su
$ ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up
$ exit
$ ifconfig | grep LOOPBACK
$ test/functional/test_runner.py feature_bind_port_discover.py --ihave1111and2222
$ test/functional/test_runner.py feature_bind_port_externalip.py --ihave1111
$ sudo su
$ ifconfig lo:0 down && ifconfig lo:1 down

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D14690

Details

Provenance
Vasil Dimov <vd@FreeBSD.org>Authored on Jan 18 2021, 13:27
PiRKCommitted on Oct 28 2023, 03:13
PiRKPushed on Oct 28 2023, 03:13
Reviewer
Restricted Project
Differential Revision
D14690: net: fix GetListenPort() to derive the proper port
Parents
rABC7ea1e609a913: [e.cash] Remove mention of bits for the denomination
Branches
Unknown
Tags
Unknown