The only way the chain params are used by the peer is to ascertain the
network magic to use on the wire (the Net field), so rather than
requiring an entire chain params instance, it is desirable to reduce the
scope such that the caller only needs to specify the specific value the
module peer needs.
To that end, this introduces a new field named Net on the config struct
which the caller can optionally specify in the same vain as the chain
params could previously optionally be specified.
The ultimate goal is to remove the chaincfg dependency altogether,
however, since removing the public ChainParams field is a breaking API
change, the field is deprecated and, in order to maintain API
compatibility, some additional semantics around handling of the
optionalality of the new Net field are introduced.
In particular, the Net field is chosen as follows:
- Use the value specified by the caller directly when set
- Fallback to the network associated with the chain params when set
- Fallback to the test network if neither are set
Finally, it updates the example and tests to specify the network
directly and adds a new test which explicitly tests the new fallback
semantics.
This modifies the OnVersion callback to allow a reject message to be
returned in which case the message will be sent to the peer and the peer
will be disconnected.
Contains the following upstream commits:
- 5cbd1f85bf
- 777ccdade3
- f3d759d783
- 1ffc3dc18d
Also the merge commit updates the code to use the correct Decred
protocol versions and contains a few style-related changes.
Contains the following upstream commits:
- 00bddf7540
- 250228c32f
- f1bd2f8d6e
- cbbe3a8bbe
- aa03d68e1e
In addition to the normal required changes for syncing, the following
changes have been made in order to facilitate integration of the new
package into Decred:
- Removed check in PushRejectMsg against protocol version since all
peers since the initial version support it
- Fixed leaked timer in `syncMiningStateAfterSync` function
- Add the Decred-specific OnGetMiningState and OnMiningState handlers to
the new peer package
- Add handler for the new mining state messages to the 'serverPeer' type
and register them
- Use the new constant for wire.InitialProtocolVersion in the reject
message instead of hard coded number
- Remove logic specific to the regression network since Decred does not
have it
This commit does not change functionality. It makes the creation of inbound and outbound peers more homogeneous. As a result the Start method of peer was removed as it was found not to be necessary. This is the first of several pull requests/commits designed to make the peer public API and internals less complex.
This commit introduces package peer which contains peer related features
refactored from peer.go.
The following is an overview of the features the package provides:
- Provides a basic concurrent safe bitcoin peer for handling bitcoin
communications via the peer-to-peer protocol
- Full duplex reading and writing of bitcoin protocol messages
- Automatic handling of the initial handshake process including protocol
version negotiation
- Automatic periodic keep-alive pinging and pong responses
- Asynchronous message queueing of outbound messages with optional
channel for notification when the message is actually sent
- Inventory message batching and send trickling with known inventory
detection and avoidance
- Ability to wait for shutdown/disconnect
- Flexible peer configuration
- Caller is responsible for creating outgoing connections and listening
for incoming connections so they have flexibility to establish
connections as they see fit (proxies, etc.)
- User agent name and version
- Bitcoin network
- Service support signalling (full nodes, bloom filters, etc.)
- Maximum supported protocol version
- Ability to register callbacks for handling bitcoin protocol messages
- Proper handling of bloom filter related commands when the caller does
not specify the related flag to signal support
- Disconnects the peer when the protocol version is high enough
- Does not invoke the related callbacks for older protocol versions
- Snapshottable peer statistics such as the total number of bytes read
and written, the remote address, user agent, and negotiated protocol
version
- Helper functions for pushing addresses, getblocks, getheaders, and
reject messages
- These could all be sent manually via the standard message output
function, but the helpers provide additional nice functionality such
as duplicate filtering and address randomization
- Full documentation with example usage
- Test coverage
In addition to the addition of the new package, btcd has been refactored
to make use of the new package by extending the basic peer it provides to
work with the blockmanager and server to act as a full node. The
following is a broad overview of the changes to integrate the package:
- The server is responsible for all connection management including
persistent peers and banning
- Callbacks for all messages that are required to implement a full node
are registered
- Logic necessary to serve data and behave as a full node is now in the
callback registered with the peer
Finally, the following peer-related things have been improved as a part
of this refactor:
- Don't log or send reject message due to peer disconnects
- Remove trace logs that aren't particularly helpful
- Finish an old TODO to switch the queue WaitGroup over to a channel
- Improve various comments and fix some code consistency cases
- Improve a few logging bits
- Implement a most-recently-used nonce tracking for detecting self
connections and generate a unique nonce for each peer