2016-01-06 22:27:39 +00:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2019-01-26 14:49:35 +00:00
|
|
|
// Copyright (c) 2015-2019 The Decred developers
|
2013-08-06 21:55:22 +00:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-30 18:51:28 +00:00
|
|
|
"context"
|
2014-03-18 19:40:49 +00:00
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/binary"
|
2013-10-29 17:18:53 +00:00
|
|
|
"errors"
|
2013-09-17 19:01:22 +00:00
|
|
|
"fmt"
|
2014-03-18 19:40:49 +00:00
|
|
|
"math"
|
2013-08-06 21:55:22 +00:00
|
|
|
"net"
|
2018-12-06 21:32:30 +00:00
|
|
|
"path"
|
2013-09-19 15:46:33 +00:00
|
|
|
"runtime"
|
2013-08-01 17:00:14 +00:00
|
|
|
"strconv"
|
2015-11-04 02:32:15 +00:00
|
|
|
"strings"
|
2013-08-06 21:55:22 +00:00
|
|
|
"sync"
|
2013-10-02 00:45:21 +00:00
|
|
|
"sync/atomic"
|
2013-08-06 21:55:22 +00:00
|
|
|
"time"
|
2014-07-02 13:50:08 +00:00
|
|
|
|
2016-01-20 21:46:42 +00:00
|
|
|
"github.com/decred/dcrd/addrmgr"
|
|
|
|
|
"github.com/decred/dcrd/blockchain"
|
2016-02-19 04:51:18 +00:00
|
|
|
"github.com/decred/dcrd/blockchain/indexers"
|
2018-07-06 00:56:28 +00:00
|
|
|
"github.com/decred/dcrd/blockchain/stake"
|
2016-01-20 21:46:42 +00:00
|
|
|
"github.com/decred/dcrd/chaincfg"
|
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
2016-05-12 09:06:07 +00:00
|
|
|
"github.com/decred/dcrd/connmgr"
|
2015-08-26 09:54:55 +00:00
|
|
|
"github.com/decred/dcrd/database"
|
2017-10-12 01:31:01 +00:00
|
|
|
"github.com/decred/dcrd/dcrutil"
|
2018-12-06 21:32:30 +00:00
|
|
|
"github.com/decred/dcrd/fees"
|
2017-11-08 21:48:44 +00:00
|
|
|
"github.com/decred/dcrd/gcs"
|
|
|
|
|
"github.com/decred/dcrd/gcs/blockcf"
|
2018-09-04 20:51:07 +00:00
|
|
|
"github.com/decred/dcrd/internal/version"
|
2019-03-18 15:08:51 +00:00
|
|
|
"github.com/decred/dcrd/lru"
|
2019-02-14 22:22:37 +00:00
|
|
|
"github.com/decred/dcrd/mempool/v2"
|
2016-05-26 21:52:36 +00:00
|
|
|
"github.com/decred/dcrd/mining"
|
2016-05-20 04:54:40 +00:00
|
|
|
"github.com/decred/dcrd/peer"
|
2016-05-18 18:26:48 +00:00
|
|
|
"github.com/decred/dcrd/txscript"
|
2016-01-20 21:46:42 +00:00
|
|
|
"github.com/decred/dcrd/wire"
|
2013-08-06 21:55:22 +00:00
|
|
|
)
|
|
|
|
|
|
2013-10-07 22:57:43 +00:00
|
|
|
const (
|
2015-08-24 15:48:59 +00:00
|
|
|
// defaultServices describes the default services that are supported by
|
|
|
|
|
// the server.
|
2018-03-30 19:42:37 +00:00
|
|
|
defaultServices = wire.SFNodeNetwork | wire.SFNodeCF
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2016-11-02 19:32:36 +00:00
|
|
|
// defaultRequiredServices describes the default services that are
|
|
|
|
|
// required to be supported by outbound peers.
|
|
|
|
|
defaultRequiredServices = wire.SFNodeNetwork
|
|
|
|
|
|
|
|
|
|
// defaultTargetOutbound is the default number of outbound peers to
|
|
|
|
|
// target.
|
|
|
|
|
defaultTargetOutbound = 8
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// connectionRetryInterval is the base amount of time to wait in between
|
|
|
|
|
// retries when connecting to persistent peers. It is adjusted by the
|
|
|
|
|
// number of retries such that there is a retry backoff.
|
2015-11-22 10:04:22 +00:00
|
|
|
connectionRetryInterval = time.Second * 5
|
2017-04-28 19:10:26 +00:00
|
|
|
|
|
|
|
|
// maxProtocolVersion is the max protocol version the server supports.
|
2017-11-08 21:48:44 +00:00
|
|
|
maxProtocolVersion = wire.NodeCFVersion
|
2019-03-18 15:08:51 +00:00
|
|
|
|
|
|
|
|
// maxKnownAddrsPerPeer is the maximum number of items to keep in the
|
|
|
|
|
// per-peer known address cache.
|
|
|
|
|
maxKnownAddrsPerPeer = 10000
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// userAgentName is the user agent name and is used to help identify
|
2018-03-01 23:23:35 +00:00
|
|
|
// ourselves to other Decred peers.
|
2016-05-20 04:54:40 +00:00
|
|
|
userAgentName = "dcrd"
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// userAgentVersion is the user agent version and is used to help
|
2016-05-20 04:54:40 +00:00
|
|
|
// identify ourselves to other peers.
|
2018-09-04 20:51:07 +00:00
|
|
|
userAgentVersion = fmt.Sprintf("%d.%d.%d", version.Major, version.Minor,
|
|
|
|
|
version.Patch)
|
2013-10-07 22:57:43 +00:00
|
|
|
)
|
2013-08-01 17:00:14 +00:00
|
|
|
|
2018-03-01 23:23:35 +00:00
|
|
|
// broadcastMsg provides the ability to house a Decred message to be broadcast
|
2013-08-06 21:55:22 +00:00
|
|
|
// to all connected peers except specified excluded peers.
|
|
|
|
|
type broadcastMsg struct {
|
2015-02-05 21:16:39 +00:00
|
|
|
message wire.Message
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
excludePeers []*serverPeer
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-28 00:03:45 +00:00
|
|
|
// broadcastInventoryAdd is a type used to declare that the InvVect it contains
|
2014-03-18 19:40:49 +00:00
|
|
|
// needs to be added to the rebroadcast map
|
2015-01-29 05:22:27 +00:00
|
|
|
type broadcastInventoryAdd relayMsg
|
2014-03-18 19:40:49 +00:00
|
|
|
|
2014-03-28 00:03:45 +00:00
|
|
|
// broadcastInventoryDel is a type used to declare that the InvVect it contains
|
2014-03-18 19:40:49 +00:00
|
|
|
// needs to be removed from the rebroadcast map
|
2015-02-05 21:16:39 +00:00
|
|
|
type broadcastInventoryDel *wire.InvVect
|
2014-03-18 19:40:49 +00:00
|
|
|
|
2018-07-06 00:56:28 +00:00
|
|
|
// broadcastPruneInventory is a type used to declare that rebroadcast
|
|
|
|
|
// inventory entries need to be filtered and removed where necessary
|
|
|
|
|
type broadcastPruneInventory struct{}
|
|
|
|
|
|
2015-01-29 05:22:27 +00:00
|
|
|
// relayMsg packages an inventory vector along with the newly discovered
|
2018-09-10 09:37:29 +00:00
|
|
|
// inventory and a flag that determines if the relay should happen immediately
|
|
|
|
|
// (it will be put into a trickle queue if false) so the relay has access to
|
|
|
|
|
// that information.
|
2015-01-29 05:22:27 +00:00
|
|
|
type relayMsg struct {
|
2018-09-10 09:37:29 +00:00
|
|
|
invVect *wire.InvVect
|
|
|
|
|
data interface{}
|
|
|
|
|
immediate bool
|
2015-01-29 05:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-11 20:39:11 +00:00
|
|
|
// updatePeerHeightsMsg is a message sent from the blockmanager to the server
|
|
|
|
|
// after a new block has been accepted. The purpose of the message is to update
|
|
|
|
|
// the heights of peers that were known to announce the block before we
|
|
|
|
|
// connected it to the main chain or recognized it as an orphan. With these
|
|
|
|
|
// updates, peer heights will be kept up to date, allowing for fresh data when
|
|
|
|
|
// selecting sync peer candidacy.
|
|
|
|
|
type updatePeerHeightsMsg struct {
|
2016-08-08 19:04:33 +00:00
|
|
|
newHash *chainhash.Hash
|
2015-08-26 04:03:18 +00:00
|
|
|
newHeight int64
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
originPeer *serverPeer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// peerState maintains state of inbound, persistent, outbound peers as well
|
|
|
|
|
// as banned peers and outbound groups.
|
|
|
|
|
type peerState struct {
|
2016-11-02 19:32:36 +00:00
|
|
|
inboundPeers map[int32]*serverPeer
|
|
|
|
|
outboundPeers map[int32]*serverPeer
|
|
|
|
|
persistentPeers map[int32]*serverPeer
|
|
|
|
|
banned map[string]time.Time
|
|
|
|
|
outboundGroups map[string]int
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-07 00:05:28 +00:00
|
|
|
// ConnectionsWithIP returns the number of connections with the given IP.
|
|
|
|
|
func (ps *peerState) ConnectionsWithIP(ip net.IP) int {
|
|
|
|
|
var total int
|
|
|
|
|
for _, p := range ps.inboundPeers {
|
|
|
|
|
if ip.Equal(p.NA().IP) {
|
|
|
|
|
total++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, p := range ps.outboundPeers {
|
|
|
|
|
if ip.Equal(p.NA().IP) {
|
|
|
|
|
total++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, p := range ps.persistentPeers {
|
|
|
|
|
if ip.Equal(p.NA().IP) {
|
|
|
|
|
total++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return total
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Count returns the count of all known peers.
|
|
|
|
|
func (ps *peerState) Count() int {
|
2016-02-27 15:52:40 +00:00
|
|
|
return len(ps.inboundPeers) + len(ps.outboundPeers) +
|
|
|
|
|
len(ps.persistentPeers)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// forAllOutboundPeers is a helper function that runs closure on all outbound
|
|
|
|
|
// peers known to peerState.
|
|
|
|
|
func (ps *peerState) forAllOutboundPeers(closure func(sp *serverPeer)) {
|
|
|
|
|
for _, e := range ps.outboundPeers {
|
|
|
|
|
closure(e)
|
|
|
|
|
}
|
|
|
|
|
for _, e := range ps.persistentPeers {
|
|
|
|
|
closure(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// forAllPeers is a helper function that runs closure on all peers known to
|
|
|
|
|
// peerState.
|
|
|
|
|
func (ps *peerState) forAllPeers(closure func(sp *serverPeer)) {
|
2016-02-27 15:52:40 +00:00
|
|
|
for _, e := range ps.inboundPeers {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
closure(e)
|
|
|
|
|
}
|
|
|
|
|
ps.forAllOutboundPeers(closure)
|
2015-02-11 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-01 23:23:35 +00:00
|
|
|
// server provides a Decred server for handling communications to and from
|
|
|
|
|
// Decred peers.
|
2013-08-06 21:55:22 +00:00
|
|
|
type server struct {
|
2016-01-12 21:54:10 +00:00
|
|
|
// The following variables must only be used atomically.
|
2016-06-08 13:07:22 +00:00
|
|
|
// Putting the uint64s first makes them 64-bit aligned for 32-bit systems.
|
|
|
|
|
bytesReceived uint64 // Total bytes received from all peers since start.
|
|
|
|
|
bytesSent uint64 // Total bytes sent by all peers since start.
|
2016-01-12 21:54:10 +00:00
|
|
|
started int32
|
|
|
|
|
shutdown int32
|
|
|
|
|
|
2015-02-06 05:18:27 +00:00
|
|
|
chainParams *chaincfg.Params
|
2014-07-06 06:04:24 +00:00
|
|
|
addrManager *addrmgr.AddrManager
|
2016-05-12 09:06:07 +00:00
|
|
|
connManager *connmgr.ConnManager
|
2015-09-24 23:22:00 +00:00
|
|
|
sigCache *txscript.SigCache
|
2014-03-18 19:40:49 +00:00
|
|
|
rpcServer *rpcServer
|
|
|
|
|
blockManager *blockManager
|
2019-03-30 18:51:28 +00:00
|
|
|
bg *BgBlkTmplGenerator
|
2016-08-19 16:08:37 +00:00
|
|
|
txMemPool *mempool.TxPool
|
2018-12-06 21:32:30 +00:00
|
|
|
feeEstimator *fees.Estimator
|
2014-06-12 01:09:38 +00:00
|
|
|
cpuMiner *CPUMiner
|
2014-03-18 19:40:49 +00:00
|
|
|
modifyRebroadcastInv chan interface{}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
newPeers chan *serverPeer
|
|
|
|
|
donePeers chan *serverPeer
|
|
|
|
|
banPeers chan *serverPeer
|
2014-03-18 19:40:49 +00:00
|
|
|
query chan interface{}
|
2015-01-29 05:22:27 +00:00
|
|
|
relayInv chan relayMsg
|
2014-03-18 19:40:49 +00:00
|
|
|
broadcast chan broadcastMsg
|
2015-02-11 20:39:11 +00:00
|
|
|
peerHeightsUpdate chan updatePeerHeightsMsg
|
2014-03-18 19:40:49 +00:00
|
|
|
wg sync.WaitGroup
|
2014-07-02 15:31:10 +00:00
|
|
|
quit chan struct{}
|
2014-03-18 19:40:49 +00:00
|
|
|
nat NAT
|
2015-08-26 04:03:18 +00:00
|
|
|
db database.DB
|
2015-01-30 22:25:42 +00:00
|
|
|
timeSource blockchain.MedianTimeSource
|
2015-08-24 15:48:59 +00:00
|
|
|
services wire.ServiceFlag
|
2019-03-30 18:51:28 +00:00
|
|
|
context context.Context
|
|
|
|
|
cancel context.CancelFunc
|
2016-02-19 04:51:18 +00:00
|
|
|
|
|
|
|
|
// The following fields are used for optional indexes. They will be nil
|
|
|
|
|
// if the associated index is not enabled. These fields are set during
|
|
|
|
|
// initial creation of the server and never changed afterwards, so they
|
|
|
|
|
// do not need to be protected for concurrent access.
|
|
|
|
|
txIndex *indexers.TxIndex
|
|
|
|
|
addrIndex *indexers.AddrIndex
|
|
|
|
|
existsAddrIndex *indexers.ExistsAddrIndex
|
2017-11-08 21:48:44 +00:00
|
|
|
cfIndex *indexers.CFIndex
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// serverPeer extends the peer to maintain state shared by the server and
|
|
|
|
|
// the blockmanager.
|
|
|
|
|
type serverPeer struct {
|
|
|
|
|
*peer.Peer
|
|
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
connReq *connmgr.ConnReq
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
server *server
|
|
|
|
|
persistent bool
|
2016-05-20 04:54:40 +00:00
|
|
|
continueHash *chainhash.Hash
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
relayMtx sync.Mutex
|
|
|
|
|
disableRelayTx bool
|
2017-07-24 19:39:23 +00:00
|
|
|
isWhitelisted bool
|
2016-05-20 04:54:40 +00:00
|
|
|
requestedTxns map[chainhash.Hash]struct{}
|
|
|
|
|
requestedBlocks map[chainhash.Hash]struct{}
|
2019-03-18 15:08:51 +00:00
|
|
|
knownAddresses lru.Cache
|
2016-05-12 09:06:07 +00:00
|
|
|
banScore connmgr.DynamicBanScore
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
quit chan struct{}
|
|
|
|
|
|
2019-03-29 00:42:33 +00:00
|
|
|
// addrsSent and getMiningStateSent both track whether or not the peer
|
|
|
|
|
// has already sent the respective request. It is used to prevent more
|
|
|
|
|
// than one response per connection.
|
|
|
|
|
addrsSent bool
|
|
|
|
|
getMiningStateSent bool
|
2018-06-04 02:00:07 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// The following chans are used to sync blockmanager and server.
|
|
|
|
|
txProcessed chan struct{}
|
|
|
|
|
blockProcessed chan struct{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// newServerPeer returns a new serverPeer instance. The peer needs to be set by
|
|
|
|
|
// the caller.
|
|
|
|
|
func newServerPeer(s *server, isPersistent bool) *serverPeer {
|
|
|
|
|
return &serverPeer{
|
|
|
|
|
server: s,
|
|
|
|
|
persistent: isPersistent,
|
2016-05-20 04:54:40 +00:00
|
|
|
requestedTxns: make(map[chainhash.Hash]struct{}),
|
|
|
|
|
requestedBlocks: make(map[chainhash.Hash]struct{}),
|
2019-03-18 15:08:51 +00:00
|
|
|
knownAddresses: lru.NewCache(maxKnownAddrsPerPeer),
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
|
txProcessed: make(chan struct{}, 1),
|
|
|
|
|
blockProcessed: make(chan struct{}, 1),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 04:03:18 +00:00
|
|
|
// newestBlock returns the current best block hash and height using the format
|
|
|
|
|
// required by the configuration for the peer package.
|
|
|
|
|
func (sp *serverPeer) newestBlock() (*chainhash.Hash, int64, error) {
|
|
|
|
|
best := sp.server.blockManager.chain.BestSnapshot()
|
2018-01-29 02:10:09 +00:00
|
|
|
return &best.Hash, best.Height, nil
|
2015-08-26 04:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// addKnownAddresses adds the given addresses to the set of known addreses to
|
|
|
|
|
// the peer to prevent sending duplicate addresses.
|
|
|
|
|
func (sp *serverPeer) addKnownAddresses(addresses []*wire.NetAddress) {
|
|
|
|
|
for _, na := range addresses {
|
2019-03-18 15:08:51 +00:00
|
|
|
sp.knownAddresses.Add(addrmgr.NetAddressKey(na))
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// addressKnown true if the given address is already known to the peer.
|
|
|
|
|
func (sp *serverPeer) addressKnown(na *wire.NetAddress) bool {
|
2019-03-18 15:08:51 +00:00
|
|
|
return sp.knownAddresses.Contains(addrmgr.NetAddressKey(na))
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setDisableRelayTx toggles relaying of transactions for the given peer.
|
|
|
|
|
// It is safe for concurrent access.
|
|
|
|
|
func (sp *serverPeer) setDisableRelayTx(disable bool) {
|
|
|
|
|
sp.relayMtx.Lock()
|
|
|
|
|
sp.disableRelayTx = disable
|
|
|
|
|
sp.relayMtx.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// relayTxDisabled returns whether or not relaying of transactions for the given
|
|
|
|
|
// peer is disabled.
|
|
|
|
|
// It is safe for concurrent access.
|
|
|
|
|
func (sp *serverPeer) relayTxDisabled() bool {
|
|
|
|
|
sp.relayMtx.Lock()
|
2016-04-13 01:19:13 +00:00
|
|
|
isDisabled := sp.disableRelayTx
|
|
|
|
|
sp.relayMtx.Unlock()
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
2016-04-13 01:19:13 +00:00
|
|
|
return isDisabled
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pushAddrMsg sends an addr message to the connected peer using the provided
|
|
|
|
|
// addresses.
|
|
|
|
|
func (sp *serverPeer) pushAddrMsg(addresses []*wire.NetAddress) {
|
|
|
|
|
// Filter addresses already known to the peer.
|
|
|
|
|
addrs := make([]*wire.NetAddress, 0, len(addresses))
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
|
if !sp.addressKnown(addr) {
|
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
known, err := sp.PushAddrMsg(addrs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("Can't push address message to %s: %v", sp.Peer, err)
|
|
|
|
|
sp.Disconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sp.addKnownAddresses(known)
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 03:11:22 +00:00
|
|
|
// addBanScore increases the persistent and decaying ban score fields by the
|
|
|
|
|
// values passed as parameters. If the resulting score exceeds half of the ban
|
|
|
|
|
// threshold, a warning is logged including the reason provided. Further, if
|
|
|
|
|
// the score is above the ban threshold, the peer will be banned and
|
|
|
|
|
// disconnected.
|
|
|
|
|
func (sp *serverPeer) addBanScore(persistent, transient uint32, reason string) {
|
|
|
|
|
// No warning is logged and no score is calculated if banning is disabled.
|
|
|
|
|
if cfg.DisableBanning {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-07-24 19:39:23 +00:00
|
|
|
if sp.isWhitelisted {
|
|
|
|
|
peerLog.Debugf("Misbehaving whitelisted peer %s: %s", sp, reason)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 03:11:22 +00:00
|
|
|
warnThreshold := cfg.BanThreshold >> 1
|
|
|
|
|
if transient == 0 && persistent == 0 {
|
|
|
|
|
// The score is not being increased, but a warning message is still
|
|
|
|
|
// logged if the score is above the warn threshold.
|
|
|
|
|
score := sp.banScore.Int()
|
|
|
|
|
if score > warnThreshold {
|
|
|
|
|
peerLog.Warnf("Misbehaving peer %s: %s -- ban score is %d, "+
|
|
|
|
|
"it was not increased this time", sp, reason, score)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
score := sp.banScore.Increase(persistent, transient)
|
|
|
|
|
if score > warnThreshold {
|
|
|
|
|
peerLog.Warnf("Misbehaving peer %s: %s -- ban score increased to %d",
|
|
|
|
|
sp, reason, score)
|
|
|
|
|
if score > cfg.BanThreshold {
|
|
|
|
|
peerLog.Warnf("Misbehaving peer %s -- banning and disconnecting",
|
|
|
|
|
sp)
|
|
|
|
|
sp.server.BanPeer(sp)
|
|
|
|
|
sp.Disconnect()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-03 06:23:57 +00:00
|
|
|
// hasServices returns whether or not the provided advertised service flags have
|
|
|
|
|
// all of the provided desired service flags set.
|
|
|
|
|
func hasServices(advertised, desired wire.ServiceFlag) bool {
|
|
|
|
|
return advertised&desired == desired
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnVersion is invoked when a peer receives a version wire message and is used
|
|
|
|
|
// to negotiate the protocol version details as well as kick start the
|
|
|
|
|
// communications.
|
2018-06-03 06:01:22 +00:00
|
|
|
func (sp *serverPeer) OnVersion(p *peer.Peer, msg *wire.MsgVersion) *wire.MsgReject {
|
2018-06-03 08:29:22 +00:00
|
|
|
// Update the address manager with the advertised services for outbound
|
|
|
|
|
// connections in case they have changed. This is not done for inbound
|
|
|
|
|
// connections to help prevent malicious behavior and is skipped when
|
|
|
|
|
// running on the simulation test network since it is only intended to
|
|
|
|
|
// connect to specified peers and actively avoids advertising and
|
|
|
|
|
// connecting to discovered peers.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: This is done before rejecting peers that are too old to ensure
|
|
|
|
|
// it is updated regardless in the case a new minimum protocol version is
|
|
|
|
|
// enforced and the remote node has not upgraded yet.
|
2018-06-03 17:29:50 +00:00
|
|
|
isInbound := sp.Inbound()
|
2018-06-04 03:03:23 +00:00
|
|
|
remoteAddr := sp.NA()
|
2018-06-03 08:29:22 +00:00
|
|
|
addrManager := sp.server.addrManager
|
2018-06-03 17:29:50 +00:00
|
|
|
if !cfg.SimNet && !isInbound {
|
2018-06-04 03:03:23 +00:00
|
|
|
addrManager.SetServices(remoteAddr, msg.Services)
|
2018-06-03 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-03 03:46:48 +00:00
|
|
|
// Ignore peers that have a protcol version that is too old. The peer
|
|
|
|
|
// negotiation logic will disconnect it after this callback returns.
|
|
|
|
|
if msg.ProtocolVersion < int32(wire.InitialProcotolVersion) {
|
2018-06-03 06:01:22 +00:00
|
|
|
return nil
|
2018-06-03 03:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-03 06:23:57 +00:00
|
|
|
// Reject outbound peers that are not full nodes.
|
|
|
|
|
wantServices := wire.SFNodeNetwork
|
2018-06-03 17:29:50 +00:00
|
|
|
if !isInbound && !hasServices(msg.Services, wantServices) {
|
2018-06-03 06:23:57 +00:00
|
|
|
missingServices := wantServices & ^msg.Services
|
|
|
|
|
srvrLog.Debugf("Rejecting peer %s with services %v due to not "+
|
|
|
|
|
"providing desired services %v", sp.Peer, msg.Services,
|
|
|
|
|
missingServices)
|
|
|
|
|
reason := fmt.Sprintf("required services %#x not offered",
|
|
|
|
|
uint64(missingServices))
|
|
|
|
|
return wire.NewMsgReject(msg.Command(), wire.RejectNonstandard, reason)
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Update the address manager and request known addresses from the
|
|
|
|
|
// remote peer for outbound connections. This is skipped when running
|
|
|
|
|
// on the simulation test network since it is only intended to connect
|
|
|
|
|
// to specified peers and actively avoids advertising and connecting to
|
|
|
|
|
// discovered peers.
|
2018-06-03 17:29:50 +00:00
|
|
|
if !cfg.SimNet && !isInbound {
|
2018-06-03 17:43:11 +00:00
|
|
|
// Advertise the local address when the server accepts incoming
|
|
|
|
|
// connections and it believes itself to be close to the best
|
|
|
|
|
// known tip.
|
|
|
|
|
if !cfg.DisableListen && sp.server.blockManager.IsCurrent() {
|
2018-06-03 17:29:50 +00:00
|
|
|
// Get address that best matches.
|
2018-06-04 03:03:23 +00:00
|
|
|
lna := addrManager.GetBestLocalAddress(remoteAddr)
|
2018-06-03 17:29:50 +00:00
|
|
|
if addrmgr.IsRoutable(lna) {
|
|
|
|
|
// Filter addresses the peer already knows about.
|
|
|
|
|
addresses := []*wire.NetAddress{lna}
|
|
|
|
|
sp.pushAddrMsg(addresses)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
2018-06-03 17:29:50 +00:00
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
2018-06-03 17:29:50 +00:00
|
|
|
// Request known addresses if the server address manager needs
|
|
|
|
|
// more.
|
|
|
|
|
if addrManager.NeedMoreAddresses() {
|
|
|
|
|
p.QueueMessage(wire.NewMsgGetAddr(), nil)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
2018-06-03 17:29:50 +00:00
|
|
|
|
|
|
|
|
// Mark the address as a known good address.
|
2018-06-04 03:03:23 +00:00
|
|
|
addrManager.Good(remoteAddr)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-03 17:29:50 +00:00
|
|
|
// Choose whether or not to relay transactions.
|
|
|
|
|
sp.setDisableRelayTx(msg.DisableRelayTx)
|
|
|
|
|
|
|
|
|
|
// Add the remote peer time as a sample for creating an offset against
|
|
|
|
|
// the local clock to keep the network time in sync.
|
|
|
|
|
sp.server.timeSource.AddTimeSample(p.Addr(), msg.Timestamp)
|
|
|
|
|
|
|
|
|
|
// Signal the block manager this peer is a new sync candidate.
|
|
|
|
|
sp.server.blockManager.NewPeer(sp)
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Add valid peer to the server.
|
|
|
|
|
sp.server.AddPeer(sp)
|
2018-06-03 06:01:22 +00:00
|
|
|
return nil
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnMemPool is invoked when a peer receives a mempool wire message. It creates
|
|
|
|
|
// and sends an inventory message with the contents of the memory pool up to the
|
2018-03-30 19:42:37 +00:00
|
|
|
// maximum inventory allowed per message.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnMemPool(p *peer.Peer, msg *wire.MsgMemPool) {
|
2016-01-23 03:11:22 +00:00
|
|
|
// A decaying ban score increase is applied to prevent flooding.
|
|
|
|
|
// The ban score accumulates and passes the ban threshold if a burst of
|
|
|
|
|
// mempool messages comes from a peer. The score decays each minute to
|
|
|
|
|
// half of its value.
|
|
|
|
|
sp.addBanScore(0, 33, "mempool")
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Generate inventory message with the available transactions in the
|
|
|
|
|
// transaction memory pool. Limit it to the max allowed inventory
|
2016-01-23 03:11:22 +00:00
|
|
|
// per message. The NewMsgInvSizeHint function automatically limits
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// the passed hint to the maximum allowed, so it's safe to pass it
|
|
|
|
|
// without double checking it here.
|
|
|
|
|
txMemPool := sp.server.txMemPool
|
|
|
|
|
txDescs := txMemPool.TxDescs()
|
|
|
|
|
invMsg := wire.NewMsgInvSizeHint(uint(len(txDescs)))
|
|
|
|
|
|
|
|
|
|
for i, txDesc := range txDescs {
|
2018-03-30 19:42:37 +00:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, txDesc.Tx.Hash())
|
|
|
|
|
invMsg.AddInvVect(iv)
|
|
|
|
|
if i+1 >= wire.MaxInvPerMsg {
|
|
|
|
|
break
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the inventory message if there is anything to send.
|
|
|
|
|
if len(invMsg.InvList) > 0 {
|
|
|
|
|
p.QueueMessage(invMsg, nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// pushMiningStateMsg pushes a mining state message to the queue for a
|
|
|
|
|
// requesting peer.
|
2016-11-22 05:14:24 +00:00
|
|
|
func (sp *serverPeer) pushMiningStateMsg(height uint32, blockHashes []chainhash.Hash, voteHashes []chainhash.Hash) error {
|
2016-05-20 04:54:40 +00:00
|
|
|
// Nothing to send, abort.
|
|
|
|
|
if len(blockHashes) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct the mining state request and queue it to be sent.
|
|
|
|
|
msg := wire.NewMsgMiningState()
|
|
|
|
|
msg.Height = height
|
2016-11-22 05:14:24 +00:00
|
|
|
for i := range blockHashes {
|
|
|
|
|
err := msg.AddBlockHash(&blockHashes[i])
|
2016-05-20 04:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-22 05:14:24 +00:00
|
|
|
for i := range voteHashes {
|
|
|
|
|
err := msg.AddVoteHash(&voteHashes[i])
|
2016-05-20 04:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-03-12 02:50:02 +00:00
|
|
|
if i+1 >= wire.MaxMSBlocksAtHeadPerMsg {
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-05-20 04:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sp.QueueMessage(msg, nil)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnGetMiningState is invoked when a peer receives a getminings wire message.
|
|
|
|
|
// It constructs a list of the current best blocks and votes that should be
|
|
|
|
|
// mined on and pushes a miningstate wire message back to the requesting peer.
|
|
|
|
|
func (sp *serverPeer) OnGetMiningState(p *peer.Peer, msg *wire.MsgGetMiningState) {
|
2019-03-29 00:42:33 +00:00
|
|
|
if sp.getMiningStateSent {
|
|
|
|
|
peerLog.Tracef("Ignoring getminingstate from %v - already sent", sp.Peer)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sp.getMiningStateSent = true
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// Access the block manager and get the list of best blocks to mine on.
|
|
|
|
|
bm := sp.server.blockManager
|
|
|
|
|
mp := sp.server.txMemPool
|
2018-09-12 10:09:43 +00:00
|
|
|
best := bm.chain.BestSnapshot()
|
2016-05-20 04:54:40 +00:00
|
|
|
|
|
|
|
|
// Send out blank mining states if it's early in the blockchain.
|
2018-09-12 10:09:43 +00:00
|
|
|
if best.Height < activeNetParams.StakeValidationHeight-1 {
|
2016-05-20 04:54:40 +00:00
|
|
|
err := sp.pushMiningStateMsg(0, nil, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Warnf("unexpected error while pushing data for "+
|
|
|
|
|
"mining state request: %v", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 19:05:55 +00:00
|
|
|
// Obtain the entire generation of blocks stemming from the parent of
|
|
|
|
|
// the current tip.
|
2018-02-24 23:33:13 +00:00
|
|
|
children, err := bm.TipGeneration()
|
2016-05-20 04:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Warnf("failed to access block manager to get the generation "+
|
2018-09-12 10:09:43 +00:00
|
|
|
"for a mining state request (block: %v): %v", best.Hash, err)
|
2016-05-20 04:54:40 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 17:16:08 +00:00
|
|
|
// Get the list of blocks of blocks that are eligible to built on and
|
|
|
|
|
// limit the list to the maximum number of allowed eligible block hashes
|
|
|
|
|
// per mining state message. There is nothing to send when there are no
|
|
|
|
|
// eligible blocks.
|
2018-09-12 10:09:43 +00:00
|
|
|
blockHashes := SortParentsByVotes(mp, best.Hash, children,
|
2016-11-21 17:16:08 +00:00
|
|
|
bm.server.chainParams)
|
|
|
|
|
numBlocks := len(blockHashes)
|
|
|
|
|
if numBlocks == 0 {
|
2016-05-20 04:54:40 +00:00
|
|
|
return
|
|
|
|
|
}
|
2016-11-21 17:16:08 +00:00
|
|
|
if numBlocks > wire.MaxMSBlocksAtHeadPerMsg {
|
2016-11-22 05:14:24 +00:00
|
|
|
blockHashes = blockHashes[:wire.MaxMSBlocksAtHeadPerMsg]
|
2016-05-20 04:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct the set of votes to send.
|
2016-11-22 05:14:24 +00:00
|
|
|
voteHashes := make([]chainhash.Hash, 0, wire.MaxMSVotesAtHeadPerMsg)
|
2018-03-04 06:02:06 +00:00
|
|
|
for i := range blockHashes {
|
2016-05-20 04:54:40 +00:00
|
|
|
// Fetch the vote hashes themselves and append them.
|
2018-03-04 06:02:06 +00:00
|
|
|
bh := &blockHashes[i]
|
2016-11-22 19:05:55 +00:00
|
|
|
vhsForBlock := mp.VoteHashesForBlock(bh)
|
|
|
|
|
if len(vhsForBlock) == 0 {
|
2016-05-20 04:54:40 +00:00
|
|
|
peerLog.Warnf("unexpected error while fetching vote hashes "+
|
2016-11-22 19:05:55 +00:00
|
|
|
"for block %v for a mining state request: no vote "+
|
|
|
|
|
"metadata for block", bh)
|
2016-05-20 04:54:40 +00:00
|
|
|
return
|
|
|
|
|
}
|
2017-03-08 20:44:15 +00:00
|
|
|
voteHashes = append(voteHashes, vhsForBlock...)
|
2016-05-20 04:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:09:43 +00:00
|
|
|
err = sp.pushMiningStateMsg(uint32(best.Height), blockHashes, voteHashes)
|
2016-05-20 04:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Warnf("unexpected error while pushing data for "+
|
|
|
|
|
"mining state request: %v", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnMiningState is invoked when a peer receives a miningstate wire message. It
|
|
|
|
|
// requests the data advertised in the message from the peer.
|
|
|
|
|
func (sp *serverPeer) OnMiningState(p *peer.Peer, msg *wire.MsgMiningState) {
|
|
|
|
|
err := sp.server.blockManager.RequestFromPeer(sp, msg.BlockHashes,
|
|
|
|
|
msg.VoteHashes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Warnf("couldn't handle mining state message: %v",
|
|
|
|
|
err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnTx is invoked when a peer receives a tx wire message. It blocks until the
|
|
|
|
|
// transaction has been fully processed. Unlock the block handler this does not
|
|
|
|
|
// serialize all transactions through a single thread transactions don't rely on
|
|
|
|
|
// the previous one in a linear fashion like blocks.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnTx(p *peer.Peer, msg *wire.MsgTx) {
|
2016-04-07 23:16:46 +00:00
|
|
|
if cfg.BlocksOnly {
|
|
|
|
|
peerLog.Tracef("Ignoring tx %v from %v - blocksonly enabled",
|
2016-08-08 19:04:33 +00:00
|
|
|
msg.TxHash(), p)
|
2016-04-07 23:16:46 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Add the transaction to the known inventory for the peer.
|
2016-05-20 04:54:40 +00:00
|
|
|
// Convert the raw MsgTx to a dcrutil.Tx which provides some convenience
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// methods and things such as hash caching.
|
2016-05-20 04:54:40 +00:00
|
|
|
tx := dcrutil.NewTx(msg)
|
2016-08-08 19:04:33 +00:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, tx.Hash())
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
p.AddKnownInventory(iv)
|
|
|
|
|
|
|
|
|
|
// Queue the transaction up to be handled by the block manager and
|
|
|
|
|
// intentionally block further receives until the transaction is fully
|
|
|
|
|
// processed and known good or bad. This helps prevent a malicious peer
|
2016-02-25 17:17:12 +00:00
|
|
|
// from queuing up a bunch of bad transactions before disconnecting (or
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// being disconnected) and wasting memory.
|
|
|
|
|
sp.server.blockManager.QueueTx(tx, sp)
|
|
|
|
|
<-sp.txProcessed
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnBlock is invoked when a peer receives a block wire message. It blocks
|
|
|
|
|
// until the network block has been fully processed.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnBlock(p *peer.Peer, msg *wire.MsgBlock, buf []byte) {
|
2016-05-20 04:54:40 +00:00
|
|
|
// Convert the raw MsgBlock to a dcrutil.Block which provides some
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// convenience methods and things such as hash caching.
|
2016-05-20 04:54:40 +00:00
|
|
|
block := dcrutil.NewBlockFromBlockAndBytes(msg, buf)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// Add the block to the known inventory for the peer.
|
2016-08-08 19:04:33 +00:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, block.Hash())
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
p.AddKnownInventory(iv)
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// Queue the block up to be handled by the block manager and
|
|
|
|
|
// intentionally block further receives until the network block is fully
|
|
|
|
|
// processed and known good or bad. This helps prevent a malicious peer
|
2016-05-30 17:24:00 +00:00
|
|
|
// from queuing up a bunch of bad blocks before disconnecting (or being
|
2016-05-20 04:54:40 +00:00
|
|
|
// disconnected) and wasting memory. Additionally, this behavior is
|
|
|
|
|
// depended on by at least the block acceptance test tool as the
|
|
|
|
|
// reference implementation processes blocks in the same thread and
|
|
|
|
|
// therefore blocks further messages until the network block has been
|
|
|
|
|
// fully processed.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
sp.server.blockManager.QueueBlock(block, sp)
|
|
|
|
|
<-sp.blockProcessed
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnInv is invoked when a peer receives an inv wire message and is used to
|
|
|
|
|
// examine the inventory being advertised by the remote peer and react
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// accordingly. We pass the message down to blockmanager which will call
|
|
|
|
|
// QueueMessage with any appropriate responses.
|
|
|
|
|
func (sp *serverPeer) OnInv(p *peer.Peer, msg *wire.MsgInv) {
|
2016-04-07 23:16:46 +00:00
|
|
|
if !cfg.BlocksOnly {
|
|
|
|
|
if len(msg.InvList) > 0 {
|
|
|
|
|
sp.server.blockManager.QueueInv(msg, sp)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newInv := wire.NewMsgInvSizeHint(uint(len(msg.InvList)))
|
|
|
|
|
for _, invVect := range msg.InvList {
|
|
|
|
|
if invVect.Type == wire.InvTypeTx {
|
2016-05-30 19:31:13 +00:00
|
|
|
peerLog.Infof("Peer %v is announcing transactions -- "+
|
|
|
|
|
"disconnecting", p)
|
|
|
|
|
p.Disconnect()
|
|
|
|
|
return
|
2016-04-07 23:16:46 +00:00
|
|
|
}
|
|
|
|
|
err := newInv.AddInvVect(invVect)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("Failed to add inventory vector: %v", err)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(newInv.InvList) > 0 {
|
|
|
|
|
sp.server.blockManager.QueueInv(newInv, sp)
|
|
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnHeaders is invoked when a peer receives a headers wire message. The
|
|
|
|
|
// message is passed down to the block manager.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnHeaders(p *peer.Peer, msg *wire.MsgHeaders) {
|
|
|
|
|
sp.server.blockManager.QueueHeaders(msg, sp)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// handleGetData is invoked when a peer receives a getdata wire message and is
|
|
|
|
|
// used to deliver block and transaction information.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnGetData(p *peer.Peer, msg *wire.MsgGetData) {
|
2016-05-24 19:42:52 +00:00
|
|
|
// Ignore empty getdata messages.
|
|
|
|
|
if len(msg.InvList) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
numAdded := 0
|
|
|
|
|
notFound := wire.NewMsgNotFound()
|
|
|
|
|
|
2016-01-23 03:11:22 +00:00
|
|
|
length := len(msg.InvList)
|
|
|
|
|
// A decaying ban score increase is applied to prevent exhausting resources
|
|
|
|
|
// with unusually large inventory queries.
|
|
|
|
|
// Requesting more than the maximum inventory vector length within a short
|
|
|
|
|
// period of time yields a score above the default ban threshold. Sustained
|
|
|
|
|
// bursts of small requests are not penalized as that would potentially ban
|
|
|
|
|
// peers performing IBD.
|
|
|
|
|
// This incremental score decays each minute to half of its value.
|
|
|
|
|
sp.addBanScore(0, uint32(length)*99/wire.MaxInvPerMsg, "getdata")
|
|
|
|
|
|
2016-02-25 17:17:12 +00:00
|
|
|
// We wait on this wait channel periodically to prevent queuing
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// far more data than we can send in a reasonable time, wasting memory.
|
|
|
|
|
// The waiting occurs after the database fetch for the next one to
|
|
|
|
|
// provide a little pipelining.
|
|
|
|
|
var waitChan chan struct{}
|
|
|
|
|
doneChan := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
|
|
for i, iv := range msg.InvList {
|
|
|
|
|
var c chan struct{}
|
|
|
|
|
// If this will be the last message we send.
|
2016-01-23 03:11:22 +00:00
|
|
|
if i == length-1 && len(notFound.InvList) == 0 {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
c = doneChan
|
|
|
|
|
} else if (i+1)%3 == 0 {
|
|
|
|
|
// Buffered so as to not make the send goroutine block.
|
|
|
|
|
c = make(chan struct{}, 1)
|
|
|
|
|
}
|
|
|
|
|
var err error
|
|
|
|
|
switch iv.Type {
|
|
|
|
|
case wire.InvTypeTx:
|
|
|
|
|
err = sp.server.pushTxMsg(sp, &iv.Hash, c, waitChan)
|
|
|
|
|
case wire.InvTypeBlock:
|
|
|
|
|
err = sp.server.pushBlockMsg(sp, &iv.Hash, c, waitChan)
|
|
|
|
|
default:
|
|
|
|
|
peerLog.Warnf("Unknown type in inventory request %d",
|
|
|
|
|
iv.Type)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
notFound.AddInvVect(iv)
|
|
|
|
|
|
|
|
|
|
// When there is a failure fetching the final entry
|
|
|
|
|
// and the done channel was sent in due to there
|
|
|
|
|
// being no outstanding not found inventory, consume
|
|
|
|
|
// it here because there is now not found inventory
|
|
|
|
|
// that will use the channel momentarily.
|
|
|
|
|
if i == len(msg.InvList)-1 && c != nil {
|
|
|
|
|
<-c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
numAdded++
|
|
|
|
|
waitChan = c
|
|
|
|
|
}
|
|
|
|
|
if len(notFound.InvList) != 0 {
|
|
|
|
|
p.QueueMessage(notFound, doneChan)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for messages to be sent. We can send quite a lot of data at this
|
|
|
|
|
// point and this will keep the peer busy for a decent amount of time.
|
|
|
|
|
// We don't process anything else by them in this time so that we
|
|
|
|
|
// have an idea of when we should hear back from them - else the idle
|
|
|
|
|
// timeout could fire when we were only half done sending the blocks.
|
|
|
|
|
if numAdded > 0 {
|
|
|
|
|
<-doneChan
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnGetBlocks is invoked when a peer receives a getblocks wire message.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnGetBlocks(p *peer.Peer, msg *wire.MsgGetBlocks) {
|
2018-05-29 22:55:53 +00:00
|
|
|
// Find the most recent known block in the best chain based on the block
|
|
|
|
|
// locator and fetch all of the block hashes after it until either
|
|
|
|
|
// wire.MaxBlocksPerMsg have been fetched or the provided stop hash is
|
|
|
|
|
// encountered.
|
|
|
|
|
//
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Use the block after the genesis block if no other blocks in the
|
|
|
|
|
// provided locator are known. This does mean the client will start
|
|
|
|
|
// over with the genesis block if unknown block locators are provided.
|
2018-05-29 22:55:53 +00:00
|
|
|
chain := sp.server.blockManager.chain
|
|
|
|
|
hashList := chain.LocateBlocks(msg.BlockLocatorHashes, &msg.HashStop,
|
|
|
|
|
wire.MaxBlocksPerMsg)
|
2015-08-26 04:03:18 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Generate inventory message.
|
|
|
|
|
invMsg := wire.NewMsgInv()
|
2015-08-26 04:03:18 +00:00
|
|
|
for i := range hashList {
|
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, &hashList[i])
|
|
|
|
|
invMsg.AddInvVect(iv)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the inventory message if there is anything to send.
|
|
|
|
|
if len(invMsg.InvList) > 0 {
|
|
|
|
|
invListLen := len(invMsg.InvList)
|
2018-05-29 22:55:53 +00:00
|
|
|
if invListLen == wire.MaxBlocksPerMsg {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Intentionally use a copy of the final hash so there
|
|
|
|
|
// is not a reference into the inventory slice which
|
|
|
|
|
// would prevent the entire slice from being eligible
|
|
|
|
|
// for GC as soon as it's sent.
|
|
|
|
|
continueHash := invMsg.InvList[invListLen-1].Hash
|
|
|
|
|
sp.continueHash = &continueHash
|
|
|
|
|
}
|
|
|
|
|
p.QueueMessage(invMsg, nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 20:57:38 +00:00
|
|
|
// OnGetHeaders is invoked when a peer receives a getheaders wire message.
|
|
|
|
|
func (sp *serverPeer) OnGetHeaders(p *peer.Peer, msg *wire.MsgGetHeaders) {
|
|
|
|
|
// Ignore getheaders requests if not in sync.
|
|
|
|
|
if !sp.server.blockManager.IsCurrent() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 22:55:53 +00:00
|
|
|
// Find the most recent known block in the best chain based on the block
|
|
|
|
|
// locator and fetch all of the headers after it until either
|
|
|
|
|
// wire.MaxBlockHeadersPerMsg have been fetched or the provided stop
|
|
|
|
|
// hash is encountered.
|
|
|
|
|
//
|
|
|
|
|
// Use the block after the genesis block if no other blocks in the
|
|
|
|
|
// provided locator are known. This does mean the client will start
|
|
|
|
|
// over with the genesis block if unknown block locators are provided.
|
|
|
|
|
chain := sp.server.blockManager.chain
|
|
|
|
|
headers := chain.LocateHeaders(msg.BlockLocatorHashes, &msg.HashStop)
|
|
|
|
|
|
|
|
|
|
// Send found headers to the requesting peer.
|
2018-01-28 11:41:48 +00:00
|
|
|
blockHeaders := make([]*wire.BlockHeader, len(headers))
|
|
|
|
|
for i := range headers {
|
|
|
|
|
blockHeaders[i] = &headers[i]
|
|
|
|
|
}
|
2016-10-18 20:57:38 +00:00
|
|
|
p.QueueMessage(&wire.MsgHeaders{Headers: blockHeaders}, nil)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-09 01:11:10 +00:00
|
|
|
// enforceNodeCFFlag disconnects the peer if the server is not configured to
|
|
|
|
|
// allow committed filters. Additionally, if the peer has negotiated to a
|
|
|
|
|
// protocol version that is high enough to observe the committed filter service
|
|
|
|
|
// support bit, it will be banned since it is intentionally violating the
|
|
|
|
|
// protocol.
|
|
|
|
|
func (sp *serverPeer) enforceNodeCFFlag(cmd string) bool {
|
2018-06-09 01:11:10 +00:00
|
|
|
if !hasServices(sp.server.services, wire.SFNodeCF) {
|
2018-06-09 01:11:10 +00:00
|
|
|
// Ban the peer if the protocol version is high enough that the peer is
|
|
|
|
|
// knowingly violating the protocol and banning is enabled.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: Even though the addBanScore function already examines whether
|
|
|
|
|
// or not banning is enabled, it is checked here as well to ensure the
|
|
|
|
|
// violation is logged and the peer is disconnected regardless.
|
|
|
|
|
if sp.ProtocolVersion() >= wire.NodeCFVersion && !cfg.DisableBanning {
|
2018-06-09 01:11:10 +00:00
|
|
|
// Disconnect the peer regardless of whether it was banned.
|
2018-06-09 01:11:10 +00:00
|
|
|
sp.addBanScore(100, 0, cmd)
|
|
|
|
|
sp.Disconnect()
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disconnect the peer regardless of protocol version or banning state.
|
|
|
|
|
peerLog.Debugf("%s sent an unsupported %s request -- disconnecting", sp,
|
|
|
|
|
cmd)
|
|
|
|
|
sp.Disconnect()
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 21:48:44 +00:00
|
|
|
// OnGetCFilter is invoked when a peer receives a getcfilter wire message.
|
|
|
|
|
func (sp *serverPeer) OnGetCFilter(p *peer.Peer, msg *wire.MsgGetCFilter) {
|
|
|
|
|
// Disconnect and/or ban depending on the node cf services flag and
|
|
|
|
|
// negotiated protocol version.
|
|
|
|
|
if !sp.enforceNodeCFFlag(msg.Command()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-09 01:11:10 +00:00
|
|
|
// Ignore request if CFs are disabled or the chain is not yet synced.
|
2017-11-08 21:48:44 +00:00
|
|
|
if cfg.NoCFilters || !sp.server.blockManager.IsCurrent() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for understood filter type.
|
|
|
|
|
switch msg.FilterType {
|
|
|
|
|
case wire.GCSFilterRegular, wire.GCSFilterExtended:
|
|
|
|
|
default:
|
|
|
|
|
peerLog.Warnf("OnGetCFilter: unsupported filter type %v",
|
|
|
|
|
msg.FilterType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filterBytes, err := sp.server.cfIndex.FilterByBlockHash(&msg.BlockHash,
|
|
|
|
|
msg.FilterType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("OnGetCFilter: failed to fetch cfilter: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the filter is not saved in the index (perhaps it was removed as a
|
|
|
|
|
// block was disconnected, or this has always been a sidechain block) build
|
|
|
|
|
// the filter on the spot.
|
|
|
|
|
if len(filterBytes) == 0 {
|
2018-07-02 07:22:18 +00:00
|
|
|
block, err := sp.server.blockManager.chain.BlockByHash(&msg.BlockHash)
|
2017-11-08 21:48:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("OnGetCFilter: failed to fetch non-mainchain "+
|
|
|
|
|
"block %v: %v", &msg.BlockHash, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var f *gcs.Filter
|
|
|
|
|
switch msg.FilterType {
|
|
|
|
|
case wire.GCSFilterRegular:
|
|
|
|
|
f, err = blockcf.Regular(block.MsgBlock())
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("OnGetCFilter: failed to build regular "+
|
|
|
|
|
"cfilter for block %v: %v", &msg.BlockHash, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
case wire.GCSFilterExtended:
|
|
|
|
|
f, err = blockcf.Extended(block.MsgBlock())
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("OnGetCFilter: failed to build extended "+
|
|
|
|
|
"cfilter for block %v: %v", &msg.BlockHash, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
peerLog.Errorf("OnGetCFilter: unhandled filter type %d",
|
|
|
|
|
msg.FilterType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filterBytes = f.NBytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peerLog.Tracef("Obtained CF for %v", &msg.BlockHash)
|
|
|
|
|
|
|
|
|
|
filterMsg := wire.NewMsgCFilter(&msg.BlockHash, msg.FilterType,
|
|
|
|
|
filterBytes)
|
|
|
|
|
sp.QueueMessage(filterMsg, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnGetCFHeaders is invoked when a peer receives a getcfheader wire message.
|
|
|
|
|
func (sp *serverPeer) OnGetCFHeaders(p *peer.Peer, msg *wire.MsgGetCFHeaders) {
|
|
|
|
|
// Disconnect and/or ban depending on the node cf services flag and
|
|
|
|
|
// negotiated protocol version.
|
|
|
|
|
if !sp.enforceNodeCFFlag(msg.Command()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-09 01:11:10 +00:00
|
|
|
// Ignore request if CFs are disabled or the chain is not yet synced.
|
2017-11-08 21:48:44 +00:00
|
|
|
if cfg.NoCFilters || !sp.server.blockManager.IsCurrent() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for understood filter type.
|
|
|
|
|
switch msg.FilterType {
|
|
|
|
|
case wire.GCSFilterRegular, wire.GCSFilterExtended:
|
|
|
|
|
default:
|
|
|
|
|
peerLog.Warnf("OnGetCFilter: unsupported filter type %v",
|
|
|
|
|
msg.FilterType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-09 01:11:10 +00:00
|
|
|
// Find the most recent known block in the best chain based on the block
|
|
|
|
|
// locator and fetch all of the block hashes after it until either
|
|
|
|
|
// wire.MaxCFHeadersPerMsg have been fetched or the provided stop hash is
|
|
|
|
|
// encountered.
|
|
|
|
|
//
|
|
|
|
|
// Use the block after the genesis block if no other blocks in the provided
|
|
|
|
|
// locator are known. This does mean the served filter headers will start
|
|
|
|
|
// over at the genesis block if unknown block locators are provided.
|
2017-11-08 21:48:44 +00:00
|
|
|
chain := sp.server.blockManager.chain
|
2018-06-09 01:11:10 +00:00
|
|
|
hashList := chain.LocateBlocks(msg.BlockLocatorHashes, &msg.HashStop,
|
|
|
|
|
wire.MaxCFHeadersPerMsg)
|
2017-11-08 21:48:44 +00:00
|
|
|
if len(hashList) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate cfheaders message and send it.
|
2018-06-09 01:11:10 +00:00
|
|
|
cfIndex := sp.server.cfIndex
|
2017-11-08 21:48:44 +00:00
|
|
|
headersMsg := wire.NewMsgCFHeaders()
|
|
|
|
|
for i := range hashList {
|
2018-06-09 01:11:10 +00:00
|
|
|
// Fetch the raw committed filter header bytes from the database.
|
|
|
|
|
hash := &hashList[i]
|
|
|
|
|
headerBytes, err := cfIndex.FilterHeaderByBlockHash(hash,
|
|
|
|
|
msg.FilterType)
|
|
|
|
|
if err != nil || len(headerBytes) == 0 {
|
|
|
|
|
peerLog.Warnf("Could not obtain CF header for %v: %v", hash, err)
|
2017-11-08 21:48:44 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deserialize the hash.
|
|
|
|
|
var header chainhash.Hash
|
|
|
|
|
err = header.SetBytes(headerBytes)
|
|
|
|
|
if err != nil {
|
2018-06-09 01:11:10 +00:00
|
|
|
peerLog.Warnf("Committed filter header deserialize failed: %v", err)
|
2017-11-08 21:48:44 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headersMsg.AddCFHeader(&header)
|
|
|
|
|
}
|
|
|
|
|
headersMsg.FilterType = msg.FilterType
|
|
|
|
|
headersMsg.StopHash = hashList[len(hashList)-1]
|
|
|
|
|
sp.QueueMessage(headersMsg, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnGetCFTypes is invoked when a peer receives a getcftypes wire message.
|
|
|
|
|
func (sp *serverPeer) OnGetCFTypes(p *peer.Peer, msg *wire.MsgGetCFTypes) {
|
|
|
|
|
// Disconnect and/or ban depending on the node cf services flag and
|
|
|
|
|
// negotiated protocol version.
|
|
|
|
|
if !sp.enforceNodeCFFlag(msg.Command()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-09 01:11:10 +00:00
|
|
|
// Ignore request if CFs are disabled.
|
2017-11-08 21:48:44 +00:00
|
|
|
if cfg.NoCFilters {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfTypesMsg := wire.NewMsgCFTypes([]wire.FilterType{
|
|
|
|
|
wire.GCSFilterRegular, wire.GCSFilterExtended})
|
|
|
|
|
sp.QueueMessage(cfTypesMsg, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnGetAddr is invoked when a peer receives a getaddr wire message and is used
|
|
|
|
|
// to provide the peer with known addresses from the address manager.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnGetAddr(p *peer.Peer, msg *wire.MsgGetAddr) {
|
|
|
|
|
// Don't return any addresses when running on the simulation test
|
|
|
|
|
// network. This helps prevent the network from becoming another
|
|
|
|
|
// public test network since it will not be able to learn about other
|
|
|
|
|
// peers that have not specifically been provided.
|
|
|
|
|
if cfg.SimNet {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do not accept getaddr requests from outbound peers. This reduces
|
|
|
|
|
// fingerprinting attacks.
|
|
|
|
|
if !p.Inbound() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 02:00:07 +00:00
|
|
|
// Only respond with addresses once per connection. This helps reduce
|
|
|
|
|
// traffic and further reduces fingerprinting attacks.
|
|
|
|
|
if sp.addrsSent {
|
|
|
|
|
peerLog.Tracef("Ignoring getaddr from %v - already sent", sp.Peer)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sp.addrsSent = true
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Get the current known addresses from the address manager.
|
|
|
|
|
addrCache := sp.server.addrManager.AddressCache()
|
|
|
|
|
|
|
|
|
|
// Push the addresses.
|
|
|
|
|
sp.pushAddrMsg(addrCache)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 04:54:40 +00:00
|
|
|
// OnAddr is invoked when a peer receives an addr wire message and is used to
|
|
|
|
|
// notify the server about advertised addresses.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (sp *serverPeer) OnAddr(p *peer.Peer, msg *wire.MsgAddr) {
|
|
|
|
|
// Ignore addresses when running on the simulation test network. This
|
|
|
|
|
// helps prevent the network from becoming another public test network
|
|
|
|
|
// since it will not be able to learn about other peers that have not
|
|
|
|
|
// specifically been provided.
|
|
|
|
|
if cfg.SimNet {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A message that has no addresses is invalid.
|
|
|
|
|
if len(msg.AddrList) == 0 {
|
|
|
|
|
peerLog.Errorf("Command [%s] from %s does not contain any addresses",
|
|
|
|
|
msg.Command(), p)
|
|
|
|
|
p.Disconnect()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 21:08:04 +00:00
|
|
|
now := time.Now()
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
for _, na := range msg.AddrList {
|
|
|
|
|
// Don't add more address if we're disconnecting.
|
|
|
|
|
if !p.Connected() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the timestamp to 5 days ago if it's more than 24 hours
|
|
|
|
|
// in the future so this address is one of the first to be
|
|
|
|
|
// removed when space is needed.
|
|
|
|
|
if na.Timestamp.After(now.Add(time.Minute * 10)) {
|
|
|
|
|
na.Timestamp = now.Add(-1 * time.Hour * 24 * 5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add address to known addresses for this peer.
|
|
|
|
|
sp.addKnownAddresses([]*wire.NetAddress{na})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add addresses to server address manager. The address manager handles
|
|
|
|
|
// the details of things such as preventing duplicate addresses, max
|
|
|
|
|
// addresses, and last seen updates.
|
|
|
|
|
// XXX bitcoind gives a 2 hour time penalty here, do we want to do the
|
|
|
|
|
// same?
|
|
|
|
|
sp.server.addrManager.AddAddresses(msg.AddrList, p.NA())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnRead is invoked when a peer receives a message and it is used to update
|
|
|
|
|
// the bytes received by the server.
|
|
|
|
|
func (sp *serverPeer) OnRead(p *peer.Peer, bytesRead int, msg wire.Message, err error) {
|
|
|
|
|
sp.server.AddBytesReceived(uint64(bytesRead))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnWrite is invoked when a peer sends a message and it is used to update
|
|
|
|
|
// the bytes sent by the server.
|
|
|
|
|
func (sp *serverPeer) OnWrite(p *peer.Peer, bytesWritten int, msg wire.Message, err error) {
|
|
|
|
|
sp.server.AddBytesSent(uint64(bytesWritten))
|
2013-10-30 17:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-18 19:40:49 +00:00
|
|
|
// randomUint16Number returns a random uint16 in a specified input range. Note
|
2014-03-27 19:05:27 +00:00
|
|
|
// that the range is in zeroth ordering; if you pass it 1800, you will get
|
|
|
|
|
// values from 0 to 1800.
|
2014-03-18 19:40:49 +00:00
|
|
|
func randomUint16Number(max uint16) uint16 {
|
2014-03-27 19:05:27 +00:00
|
|
|
// In order to avoid modulo bias and ensure every possible outcome in
|
|
|
|
|
// [0, max) has equal probability, the random number must be sampled
|
|
|
|
|
// from a random source that has a range limited to a multiple of the
|
|
|
|
|
// modulus.
|
2014-03-18 19:40:49 +00:00
|
|
|
var randomNumber uint16
|
|
|
|
|
var limitRange = (math.MaxUint16 / max) * max
|
|
|
|
|
for {
|
|
|
|
|
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
|
|
|
|
|
if randomNumber < limitRange {
|
|
|
|
|
return (randomNumber % max)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:05:27 +00:00
|
|
|
// AddRebroadcastInventory adds 'iv' to the list of inventories to be
|
|
|
|
|
// rebroadcasted at random intervals until they show up in a block.
|
2015-02-05 21:16:39 +00:00
|
|
|
func (s *server) AddRebroadcastInventory(iv *wire.InvVect, data interface{}) {
|
2014-06-02 21:10:33 +00:00
|
|
|
// Ignore if shutting down.
|
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-29 05:22:27 +00:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryAdd{invVect: iv, data: data}
|
2014-03-18 19:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:05:27 +00:00
|
|
|
// RemoveRebroadcastInventory removes 'iv' from the list of items to be
|
|
|
|
|
// rebroadcasted if present.
|
2015-02-05 21:16:39 +00:00
|
|
|
func (s *server) RemoveRebroadcastInventory(iv *wire.InvVect) {
|
2014-06-02 21:10:33 +00:00
|
|
|
// Ignore if shutting down.
|
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 19:40:49 +00:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryDel(iv)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 00:56:28 +00:00
|
|
|
// PruneRebroadcastInventory filters and removes rebroadcast inventory entries
|
|
|
|
|
// where necessary.
|
|
|
|
|
func (s *server) PruneRebroadcastInventory() {
|
|
|
|
|
// Ignore if shutting down.
|
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.modifyRebroadcastInv <- broadcastPruneInventory{}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 17:58:09 +00:00
|
|
|
// AnnounceNewTransactions generates and relays inventory vectors and notifies
|
|
|
|
|
// both websocket and getblocktemplate long poll clients of the passed
|
|
|
|
|
// transactions. This function should be called whenever new transactions
|
|
|
|
|
// are added to the mempool.
|
|
|
|
|
func (s *server) AnnounceNewTransactions(newTxs []*dcrutil.Tx) {
|
|
|
|
|
// Generate and relay inventory vectors for all newly accepted
|
|
|
|
|
// transactions into the memory pool due to the original being
|
|
|
|
|
// accepted.
|
|
|
|
|
for _, tx := range newTxs {
|
|
|
|
|
// Generate the inventory vector and relay it.
|
2016-08-08 19:04:33 +00:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, tx.Hash())
|
2018-09-10 09:37:29 +00:00
|
|
|
s.RelayInventory(iv, tx, false)
|
2016-04-14 17:58:09 +00:00
|
|
|
|
|
|
|
|
if s.rpcServer != nil {
|
|
|
|
|
// Notify websocket clients about mempool transactions.
|
|
|
|
|
s.rpcServer.ntfnMgr.NotifyMempoolTx(tx, true)
|
|
|
|
|
|
|
|
|
|
// Potentially notify any getblocktemplate long poll clients
|
|
|
|
|
// about stale block templates due to the new transaction.
|
|
|
|
|
s.rpcServer.gbtWorkState.NotifyMempoolTx(
|
|
|
|
|
s.txMemPool.LastUpdated())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// pushTxMsg sends a tx message for the provided transaction hash to the
|
|
|
|
|
// connected peer. An error is returned if the transaction hash is not known.
|
2016-08-08 19:04:33 +00:00
|
|
|
func (s *server) pushTxMsg(sp *serverPeer, hash *chainhash.Hash, doneChan chan<- struct{}, waitChan <-chan struct{}) error {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Attempt to fetch the requested transaction from the pool. A
|
|
|
|
|
// call could be made to check for existence first, but simply trying
|
|
|
|
|
// to fetch a missing transaction results in the same behavior.
|
2015-08-26 09:54:55 +00:00
|
|
|
// Do not allow peers to request transactions already in a block
|
|
|
|
|
// but are unconfirmed, as they may be expensive. Restrict that
|
|
|
|
|
// to the authenticated RPC only.
|
blockchain: Reverse utxo set semantics.
This modifies the way the unspent transaction output set is handled to
reverse its current semantics so that it is optimized for the typical
case, provides simpler handling, and resolves various issues with the
previous approach. In addition, it updates the transaction, address,
and existsaddress indexes to no longer remove entries from blocks that
have been disapproved as, in all cases, the data still exists in the
blockchain and thus should be queryable via the indexes even though
there is special handling applied which treats them as if they did not
exist in certain regards.
Prior to this change, transactions in the regular tree were not applied
to the utxo set until the next block was processed and did not vote
against them. However, that approach has several undesirable
consequences such as temporarily "invisible" utxos that are actually
spendable, disapproved transactions missing from indexes even though
they are still in the blockchain, and poor performance characteristics.
In a certain sense, the previous approach could be viewed as the
transactions not being valid until they were approved, however, that is
not really true because it was (and still is) perfectly acceptable to
spend utxos created by transactions in the regular tree of the same
block so long as they come before the transactions that spend them.
Further, utxos from a transaction in the regular tree of a block can be
spent in the next block so long as that block does not disapprove them,
which further illustrates that the utxos are actually valid unless they
are disapproved.
Consequently, this modifies that behavior to instead make the utxo set
always track the most recent block and remove the regular transactions
in the parent when a block votes against them. This approach is
significantly more efficient for the normal case where the previous
block is not disapproved by its successor.
Also, the terminology is changed in several places to refer to
disapproved blocks and transaction trees as opposed to invalid, because
invalid implies the tree/block is malformed or does not follow the
consensus rules. On the contrary, when a block votes against its
parent, it is only voting against regular transaction tree of the
parent. Both the block and transaction tree are still valid in that
case, only the regular transaction tree is treated as if it never
existed in terms of effects on the utxo set and duplicate transaction
semantics.
High level overview of changes:
- Modify the utxo viewpoint to reverse semantics as previously described
- Remove all code related to stake viewpoints
- Change all block connection code in the viewpoint to first undo all
transactions in the regular tree of the parent block if the current
one disapproves it then connect all of the stake txns followed by
the regular transactions in the block
- NOTE: The order here is important since stake transactions are not
allowed to spend outputs from the regular transactions in the same
block as the next block might disapprove them
- Change all block disconnection code in the viewpoint to first undo
all the transactions in the regular and stake trees of the block
being disconnected, and then resurrect the regular transactions in
the parent block if the block being disconnected disapproved of it
- Introduce a new type named viewFilteredSet for handling sets
filtered by transactions that already exist in a view
- Introduce a function on the viewpoint for specifically fetching the
inputs to the regular transactions
- Update mempool block connection and disconnection code to match the
new semantics
- Update all tests to handle the new semantics
- Modify the best state number of transactions to include all
transactions in all blocks regardless of disapproval because they
still had to be processed and still exist in the blockchain
- Remove include recent block parameter from mempool.FetchTransaction
since the utxoset now always includes the latest block
- This also has the side effect of correcting some unexpected results
such as coinbases in the most recent block being incorrectly
reported as having zero confirmations
- Modify mempool utxo fetch logic to use a cached disapproved view, when
needed, rather than recreating the view for every new transaction
added to it
- Update spend journal to include all transactions in the block instead
of only stake transactions from the current block and regular
transactions from the parent block
- Modify tx and address indexes to store the block index of each tx
along with its location within the files and update the query
functions to return the information as well
- Change the tx, address, and existsaddress indexes to index all
transactions regardless of their disapproval
- This also corrects several issues such as the inability to query and
retrieve transactions that exist in a disapproved block
- Update all RPC commands that return verbose transaction information
to set that newly available block index information properly
- Rename IsRegTxTreeKnownDisapproved in the mining.TxSource interface to
IsRegTxTreeKnownDisapproved
- NOTE: This will require a major bump to the mining module before
the next release
- Rename several utxoView instances to view for consistency
- Rename several variables that dealt with disapproved trees from
names that contained Invalid to ones that contain Disapproved
NOTE: This does not yet have database migration code and thus will
require a full chain download. It will exit with error in the case you
attempt to run it against an existing v4 database. The new database it
creates will be v5, so attempting to run an older version will reject
the new database to prevent corruption.
The database migration will be added in a separate commit.
2018-09-27 01:54:43 +00:00
|
|
|
tx, err := s.txMemPool.FetchTransaction(hash)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Tracef("Unable to fetch tx %v from transaction "+
|
2016-08-08 19:04:33 +00:00
|
|
|
"pool: %v", hash, err)
|
2013-10-31 17:51:40 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if doneChan != nil {
|
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Once we have fetched data wait for any previous operation to finish.
|
|
|
|
|
if waitChan != nil {
|
|
|
|
|
<-waitChan
|
|
|
|
|
}
|
2013-10-31 17:51:40 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
sp.QueueMessage(tx.MsgTx(), doneChan)
|
|
|
|
|
|
|
|
|
|
return nil
|
2013-10-31 17:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// pushBlockMsg sends a block message for the provided block hash to the
|
|
|
|
|
// connected peer. An error is returned if the block hash is not known.
|
2015-08-26 04:03:18 +00:00
|
|
|
func (s *server) pushBlockMsg(sp *serverPeer, hash *chainhash.Hash, doneChan chan<- struct{}, waitChan <-chan struct{}) error {
|
2018-07-02 07:22:18 +00:00
|
|
|
block, err := sp.server.blockManager.chain.BlockByHash(hash)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if err != nil {
|
2015-08-26 04:03:18 +00:00
|
|
|
peerLog.Tracef("Unable to fetch requested block hash %v: %v",
|
|
|
|
|
hash, err)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
if doneChan != nil {
|
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
|
}
|
|
|
|
|
return err
|
2014-01-24 20:13:19 +00:00
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// Once we have fetched data wait for any previous operation to finish.
|
|
|
|
|
if waitChan != nil {
|
|
|
|
|
<-waitChan
|
2014-01-24 20:13:19 +00:00
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// We only send the channel for this message if we aren't sending
|
|
|
|
|
// an inv straight after.
|
2016-04-06 18:50:27 +00:00
|
|
|
var dc chan<- struct{}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
continueHash := sp.continueHash
|
2015-08-26 04:03:18 +00:00
|
|
|
sendInv := continueHash != nil && continueHash.IsEqual(hash)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if !sendInv {
|
|
|
|
|
dc = doneChan
|
|
|
|
|
}
|
2015-08-26 04:03:18 +00:00
|
|
|
sp.QueueMessage(block.MsgBlock(), dc)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
|
|
|
|
// When the peer requests the final block that was advertised in
|
|
|
|
|
// response to a getblocks message which requested more blocks than
|
|
|
|
|
// would fit into a single message, send it a new inventory message
|
|
|
|
|
// to trigger it to issue another getblocks message for the next
|
|
|
|
|
// batch of inventory.
|
|
|
|
|
if sendInv {
|
2015-08-26 04:03:18 +00:00
|
|
|
best := sp.server.blockManager.chain.BestSnapshot()
|
|
|
|
|
invMsg := wire.NewMsgInvSizeHint(1)
|
2018-01-29 02:10:09 +00:00
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, &best.Hash)
|
2015-08-26 04:03:18 +00:00
|
|
|
invMsg.AddInvVect(iv)
|
|
|
|
|
sp.QueueMessage(invMsg, doneChan)
|
|
|
|
|
sp.continueHash = nil
|
2014-01-24 20:13:19 +00:00
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
return nil
|
2014-01-24 20:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-11 20:39:11 +00:00
|
|
|
// handleUpdatePeerHeight updates the heights of all peers who were known to
|
|
|
|
|
// announce a block we recently accepted.
|
|
|
|
|
func (s *server) handleUpdatePeerHeights(state *peerState, umsg updatePeerHeightsMsg) {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
2015-02-11 20:39:11 +00:00
|
|
|
// The origin peer should already have the updated height.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if sp == umsg.originPeer {
|
2015-02-11 20:39:11 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// This is a pointer to the underlying memory which doesn't
|
|
|
|
|
// change.
|
2016-08-08 19:04:33 +00:00
|
|
|
latestBlkHash := sp.LastAnnouncedBlock()
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
2015-02-11 20:39:11 +00:00
|
|
|
// Skip this peer if it hasn't recently announced any new blocks.
|
2016-08-08 19:04:33 +00:00
|
|
|
if latestBlkHash == nil {
|
2015-02-11 20:39:11 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the peer has recently announced a block, and this block
|
|
|
|
|
// matches our newly accepted block, then update their block
|
|
|
|
|
// height.
|
2016-08-08 19:04:33 +00:00
|
|
|
if *latestBlkHash == *umsg.newHash {
|
2017-03-08 20:44:15 +00:00
|
|
|
sp.UpdateLastBlockHeight(umsg.newHeight)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
sp.UpdateLastAnnouncedBlock(nil)
|
2015-02-11 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// handleAddPeerMsg deals with adding new peers. It is invoked from the
|
|
|
|
|
// peerHandler goroutine.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
|
|
|
|
|
if sp == nil {
|
2013-08-01 17:00:14 +00:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// Ignore new peers if we're shutting down.
|
2013-10-02 00:45:21 +00:00
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
2016-02-03 14:24:28 +00:00
|
|
|
srvrLog.Infof("New peer %s ignored - server is shutting down", sp)
|
|
|
|
|
sp.Disconnect()
|
2013-08-01 17:00:14 +00:00
|
|
|
return false
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disconnect banned peers.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
host, _, err := net.SplitHostPort(sp.Addr())
|
2013-08-06 21:55:22 +00:00
|
|
|
if err != nil {
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Debugf("can't split hostport %v", err)
|
2016-02-03 14:24:28 +00:00
|
|
|
sp.Disconnect()
|
2013-08-01 17:00:14 +00:00
|
|
|
return false
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2013-10-30 17:22:35 +00:00
|
|
|
if banEnd, ok := state.banned[host]; ok {
|
2013-08-06 21:55:22 +00:00
|
|
|
if time.Now().Before(banEnd) {
|
2016-02-03 14:24:28 +00:00
|
|
|
srvrLog.Debugf("Peer %s is banned for another %v - disconnecting",
|
2017-10-12 20:43:42 +00:00
|
|
|
host, time.Until(banEnd))
|
2016-02-03 14:24:28 +00:00
|
|
|
sp.Disconnect()
|
2013-08-01 17:00:14 +00:00
|
|
|
return false
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Infof("Peer %s is no longer banned", host)
|
2013-10-30 17:22:35 +00:00
|
|
|
delete(state.banned, host)
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-07 00:05:28 +00:00
|
|
|
// Limit max number of connections from a single IP. However, allow
|
|
|
|
|
// whitelisted inbound peers and localhost connections regardless.
|
|
|
|
|
isInboundWhitelisted := sp.isWhitelisted && sp.Inbound()
|
|
|
|
|
peerIP := sp.NA().IP
|
|
|
|
|
if cfg.MaxSameIP > 0 && !isInboundWhitelisted && !peerIP.IsLoopback() &&
|
|
|
|
|
state.ConnectionsWithIP(peerIP)+1 > cfg.MaxSameIP {
|
|
|
|
|
srvrLog.Infof("Max connections with %s reached [%d] - "+
|
|
|
|
|
"disconnecting peer", sp, cfg.MaxSameIP)
|
|
|
|
|
sp.Disconnect()
|
|
|
|
|
return false
|
|
|
|
|
}
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2018-11-06 21:15:05 +00:00
|
|
|
// Limit max number of total peers. However, allow whitelisted inbound
|
|
|
|
|
// peers regardless.
|
2018-11-07 00:05:28 +00:00
|
|
|
if state.Count()+1 > cfg.MaxPeers && !isInboundWhitelisted {
|
2016-02-03 14:24:28 +00:00
|
|
|
srvrLog.Infof("Max peers reached [%d] - disconnecting peer %s",
|
|
|
|
|
cfg.MaxPeers, sp)
|
|
|
|
|
sp.Disconnect()
|
2018-02-02 15:19:48 +00:00
|
|
|
// TODO: how to handle permanent peers here?
|
2013-08-01 17:00:14 +00:00
|
|
|
// they should be rescheduled.
|
|
|
|
|
return false
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the new peer and start it.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
srvrLog.Debugf("New peer %s", sp)
|
|
|
|
|
if sp.Inbound() {
|
2016-02-27 15:52:40 +00:00
|
|
|
state.inboundPeers[sp.ID()] = sp
|
2013-10-31 17:51:40 +00:00
|
|
|
} else {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]++
|
|
|
|
|
if sp.persistent {
|
|
|
|
|
state.persistentPeers[sp.ID()] = sp
|
2013-10-31 17:51:40 +00:00
|
|
|
} else {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.outboundPeers[sp.ID()] = sp
|
2013-10-31 17:51:40 +00:00
|
|
|
}
|
2013-08-01 17:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleDonePeerMsg deals with peers that have signalled they are done. It is
|
|
|
|
|
// invoked from the peerHandler goroutine.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
|
|
|
|
|
var list map[int32]*serverPeer
|
|
|
|
|
if sp.persistent {
|
2013-10-31 17:51:40 +00:00
|
|
|
list = state.persistentPeers
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
} else if sp.Inbound() {
|
2016-02-27 15:52:40 +00:00
|
|
|
list = state.inboundPeers
|
2013-10-31 17:51:40 +00:00
|
|
|
} else {
|
|
|
|
|
list = state.outboundPeers
|
|
|
|
|
}
|
2015-11-22 08:00:43 +00:00
|
|
|
if _, ok := list[sp.ID()]; ok {
|
|
|
|
|
if !sp.Inbound() && sp.VersionKnown() {
|
|
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
|
|
|
|
}
|
2016-10-28 02:28:30 +00:00
|
|
|
if !sp.Inbound() && sp.connReq != nil {
|
2016-05-12 09:06:07 +00:00
|
|
|
s.connManager.Disconnect(sp.connReq.ID())
|
|
|
|
|
}
|
2015-11-22 08:00:43 +00:00
|
|
|
delete(list, sp.ID())
|
|
|
|
|
srvrLog.Debugf("Removed peer %s", sp)
|
|
|
|
|
return
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2015-11-22 08:00:43 +00:00
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
if sp.connReq != nil {
|
2016-11-02 21:50:52 +00:00
|
|
|
s.connManager.Disconnect(sp.connReq.ID())
|
2016-05-12 09:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Update the address' last seen time if the peer has acknowledged
|
|
|
|
|
// our version and has sent us its version as well.
|
|
|
|
|
if sp.VerAckReceived() && sp.VersionKnown() && sp.NA() != nil {
|
|
|
|
|
s.addrManager.Connected(sp.NA())
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 17:51:40 +00:00
|
|
|
// If we get here it means that either we didn't know about the peer
|
|
|
|
|
// or we purposefully deleted it.
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleBanPeerMsg deals with banning peers. It is invoked from the
|
|
|
|
|
// peerHandler goroutine.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) handleBanPeerMsg(state *peerState, sp *serverPeer) {
|
|
|
|
|
host, _, err := net.SplitHostPort(sp.Addr())
|
2013-08-06 21:55:22 +00:00
|
|
|
if err != nil {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
srvrLog.Debugf("can't split ban peer %s %v", sp.Addr(), err)
|
2013-08-06 21:55:22 +00:00
|
|
|
return
|
|
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
direction := directionString(sp.Inbound())
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Infof("Banned peer %s (%s) for %v", host, direction,
|
2013-08-06 21:55:22 +00:00
|
|
|
cfg.BanDuration)
|
2013-10-30 17:22:35 +00:00
|
|
|
state.banned[host] = time.Now().Add(cfg.BanDuration)
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-12 17:11:22 +00:00
|
|
|
// handleRelayInvMsg deals with relaying inventory to peers that are not already
|
2013-09-09 15:58:56 +00:00
|
|
|
// known to have it. It is invoked from the peerHandler goroutine.
|
2015-01-29 05:22:27 +00:00
|
|
|
func (s *server) handleRelayInvMsg(state *peerState, msg relayMsg) {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
|
if !sp.Connected() {
|
2013-10-31 17:51:40 +00:00
|
|
|
return
|
2013-09-17 22:39:49 +00:00
|
|
|
}
|
2013-09-09 15:58:56 +00:00
|
|
|
|
2016-04-26 18:24:03 +00:00
|
|
|
// If the inventory is a block and the peer prefers headers,
|
|
|
|
|
// generate and send a headers message instead of an inventory
|
|
|
|
|
// message.
|
|
|
|
|
if msg.invVect.Type == wire.InvTypeBlock && sp.WantsHeaders() {
|
|
|
|
|
blockHeader, ok := msg.data.(wire.BlockHeader)
|
|
|
|
|
if !ok {
|
|
|
|
|
peerLog.Warnf("Underlying data for headers" +
|
|
|
|
|
" is not a block header")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
msgHeaders := wire.NewMsgHeaders()
|
|
|
|
|
if err := msgHeaders.AddBlockHeader(&blockHeader); err != nil {
|
|
|
|
|
peerLog.Errorf("Failed to add block"+
|
|
|
|
|
" header: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sp.QueueMessage(msgHeaders, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 21:16:39 +00:00
|
|
|
if msg.invVect.Type == wire.InvTypeTx {
|
2014-07-09 01:44:21 +00:00
|
|
|
// Don't relay the transaction to the peer when it has
|
|
|
|
|
// transaction relaying disabled.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if sp.relayTxDisabled() {
|
2014-07-09 01:44:21 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 09:37:29 +00:00
|
|
|
// Either queue the inventory to be relayed immediately or with
|
|
|
|
|
// the next batch depending on the immediate flag.
|
|
|
|
|
//
|
|
|
|
|
// It will be ignored in either case if the peer is already
|
|
|
|
|
// known to have the inventory.
|
|
|
|
|
if msg.immediate {
|
|
|
|
|
sp.QueueInventoryImmediate(msg.invVect)
|
|
|
|
|
} else {
|
|
|
|
|
sp.QueueInventory(msg.invVect)
|
|
|
|
|
}
|
2013-10-31 17:51:40 +00:00
|
|
|
})
|
2013-09-09 15:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
|
|
|
|
|
// from the peerHandler goroutine.
|
2013-10-30 17:22:35 +00:00
|
|
|
func (s *server) handleBroadcastMsg(state *peerState, bmsg *broadcastMsg) {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
2016-04-07 17:33:43 +00:00
|
|
|
if !sp.Connected() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 17:51:40 +00:00
|
|
|
for _, ep := range bmsg.excludePeers {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if sp == ep {
|
2016-04-07 17:33:43 +00:00
|
|
|
return
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-07 17:33:43 +00:00
|
|
|
|
|
|
|
|
sp.QueueMessage(bmsg.message, nil)
|
2013-10-31 17:51:40 +00:00
|
|
|
})
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-21 17:45:30 +00:00
|
|
|
type getConnCountMsg struct {
|
2014-06-29 21:36:41 +00:00
|
|
|
reply chan int32
|
2013-10-21 17:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
type getPeersMsg struct {
|
|
|
|
|
reply chan []*serverPeer
|
2013-10-21 17:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
type getOutboundGroup struct {
|
|
|
|
|
key string
|
|
|
|
|
reply chan int
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-24 20:10:02 +00:00
|
|
|
type getAddedNodesMsg struct {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
reply chan []*serverPeer
|
2014-01-24 20:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-05 21:47:54 +00:00
|
|
|
type disconnectNodeMsg struct {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp func(*serverPeer) bool
|
2015-03-05 21:47:54 +00:00
|
|
|
reply chan error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type connectNodeMsg struct {
|
|
|
|
|
addr string
|
|
|
|
|
permanent bool
|
|
|
|
|
reply chan error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type removeNodeMsg struct {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp func(*serverPeer) bool
|
2015-03-05 21:47:54 +00:00
|
|
|
reply chan error
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 20:29:23 +00:00
|
|
|
// handleQuery is the central handler for all queries and commands from other
|
|
|
|
|
// goroutines related to peer state.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) handleQuery(state *peerState, querymsg interface{}) {
|
2013-10-21 17:45:30 +00:00
|
|
|
switch msg := querymsg.(type) {
|
|
|
|
|
case getConnCountMsg:
|
2014-06-29 21:36:41 +00:00
|
|
|
nconnected := int32(0)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
|
if sp.Connected() {
|
2013-10-21 17:45:30 +00:00
|
|
|
nconnected++
|
|
|
|
|
}
|
2013-10-31 17:51:40 +00:00
|
|
|
})
|
2013-10-21 17:45:30 +00:00
|
|
|
msg.reply <- nconnected
|
2014-01-24 20:10:02 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
case getPeersMsg:
|
|
|
|
|
peers := make([]*serverPeer, 0, state.Count())
|
|
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
|
if !sp.Connected() {
|
2013-10-31 17:51:40 +00:00
|
|
|
return
|
2013-10-21 17:45:30 +00:00
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
peers = append(peers, sp)
|
2013-10-31 17:51:40 +00:00
|
|
|
})
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
msg.reply <- peers
|
2014-01-24 20:10:02 +00:00
|
|
|
|
2015-03-05 21:47:54 +00:00
|
|
|
case connectNodeMsg:
|
2018-02-02 15:19:48 +00:00
|
|
|
// XXX duplicate oneshots?
|
2016-11-02 20:04:45 +00:00
|
|
|
// Limit max number of total peers.
|
|
|
|
|
if state.Count() >= cfg.MaxPeers {
|
|
|
|
|
msg.reply <- errors.New("max peers reached")
|
|
|
|
|
return
|
|
|
|
|
}
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
for _, peer := range state.persistentPeers {
|
|
|
|
|
if peer.Addr() == msg.addr {
|
2015-03-05 21:47:54 +00:00
|
|
|
if msg.permanent {
|
2013-10-31 17:51:40 +00:00
|
|
|
msg.reply <- errors.New("peer already connected")
|
2015-03-05 21:47:54 +00:00
|
|
|
} else {
|
|
|
|
|
msg.reply <- errors.New("peer exists as a permanent peer")
|
2013-10-31 17:51:40 +00:00
|
|
|
}
|
2015-03-05 21:47:54 +00:00
|
|
|
return
|
2013-10-29 17:18:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-05 21:47:54 +00:00
|
|
|
|
2016-11-03 21:23:13 +00:00
|
|
|
netAddr, err := addrStringToNetAddr(msg.addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
msg.reply <- err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 15:19:48 +00:00
|
|
|
// TODO: if too many, nuke a non-perm peer.
|
2016-05-12 09:06:07 +00:00
|
|
|
go s.connManager.Connect(&connmgr.ConnReq{
|
2016-11-03 21:23:13 +00:00
|
|
|
Addr: netAddr,
|
2016-05-12 09:06:07 +00:00
|
|
|
Permanent: msg.permanent,
|
|
|
|
|
})
|
|
|
|
|
msg.reply <- nil
|
2015-03-05 21:47:54 +00:00
|
|
|
case removeNodeMsg:
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
found := disconnectPeer(state.persistentPeers, msg.cmp, func(sp *serverPeer) {
|
2015-03-05 21:47:54 +00:00
|
|
|
// Keep group counts ok since we remove from
|
|
|
|
|
// the list now.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2019-02-21 12:46:11 +00:00
|
|
|
|
|
|
|
|
peerLog.Debugf("Removing persistent peer %s:%d (reqid %d)",
|
|
|
|
|
sp.NA().IP, sp.NA().Port, sp.connReq.ID())
|
|
|
|
|
connReq := sp.connReq
|
|
|
|
|
|
|
|
|
|
// Mark the peer's connReq as nil to prevent it from scheduling a
|
|
|
|
|
// re-connect attempt.
|
|
|
|
|
sp.connReq = nil
|
|
|
|
|
s.connManager.Remove(connReq.ID())
|
2015-03-05 21:47:54 +00:00
|
|
|
})
|
2013-10-29 17:18:53 +00:00
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
msg.reply <- nil
|
|
|
|
|
} else {
|
|
|
|
|
msg.reply <- errors.New("peer not found")
|
|
|
|
|
}
|
2016-05-12 09:06:07 +00:00
|
|
|
case getOutboundGroup:
|
|
|
|
|
count, ok := state.outboundGroups[msg.key]
|
|
|
|
|
if ok {
|
|
|
|
|
msg.reply <- count
|
|
|
|
|
} else {
|
|
|
|
|
msg.reply <- 0
|
|
|
|
|
}
|
2014-01-24 20:10:02 +00:00
|
|
|
// Request a list of the persistent (added) peers.
|
|
|
|
|
case getAddedNodesMsg:
|
2018-02-18 18:07:06 +00:00
|
|
|
// Respond with a slice of the relevant peers.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
peers := make([]*serverPeer, 0, len(state.persistentPeers))
|
|
|
|
|
for _, sp := range state.persistentPeers {
|
|
|
|
|
peers = append(peers, sp)
|
2014-01-24 20:10:02 +00:00
|
|
|
}
|
|
|
|
|
msg.reply <- peers
|
2015-03-05 21:47:54 +00:00
|
|
|
case disconnectNodeMsg:
|
|
|
|
|
// Check inbound peers. We pass a nil callback since we don't
|
|
|
|
|
// require any additional actions on disconnect for inbound peers.
|
2016-02-27 15:52:40 +00:00
|
|
|
found := disconnectPeer(state.inboundPeers, msg.cmp, nil)
|
2015-03-05 21:47:54 +00:00
|
|
|
if found {
|
|
|
|
|
msg.reply <- nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check outbound peers.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
found = disconnectPeer(state.outboundPeers, msg.cmp, func(sp *serverPeer) {
|
2015-03-05 21:47:54 +00:00
|
|
|
// Keep group counts ok since we remove from
|
|
|
|
|
// the list now.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2015-03-05 21:47:54 +00:00
|
|
|
})
|
|
|
|
|
if found {
|
|
|
|
|
// If there are multiple outbound connections to the same
|
|
|
|
|
// ip:port, continue disconnecting them all until no such
|
|
|
|
|
// peers are found.
|
|
|
|
|
for found {
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
found = disconnectPeer(state.outboundPeers, msg.cmp, func(sp *serverPeer) {
|
|
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2015-03-05 21:47:54 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
msg.reply <- nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg.reply <- errors.New("peer not found")
|
2013-10-21 17:45:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 21:47:54 +00:00
|
|
|
// disconnectPeer attempts to drop the connection of a tageted peer in the
|
|
|
|
|
// passed peer list. Targets are identified via usage of the passed
|
|
|
|
|
// `compareFunc`, which should return `true` if the passed peer is the target
|
|
|
|
|
// peer. This function returns true on success and false if the peer is unable
|
|
|
|
|
// to be located. If the peer is found, and the passed callback: `whenFound'
|
|
|
|
|
// isn't nil, we call it with the peer as the argument before it is removed
|
|
|
|
|
// from the peerList, and is disconnected from the server.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func disconnectPeer(peerList map[int32]*serverPeer, compareFunc func(*serverPeer) bool, whenFound func(*serverPeer)) bool {
|
|
|
|
|
for addr, peer := range peerList {
|
2015-03-05 21:47:54 +00:00
|
|
|
if compareFunc(peer) {
|
|
|
|
|
if whenFound != nil {
|
|
|
|
|
whenFound(peer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is ok because we are not continuing
|
|
|
|
|
// to iterate so won't corrupt the loop.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
delete(peerList, addr)
|
2015-03-05 21:47:54 +00:00
|
|
|
peer.Disconnect()
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// newPeerConfig returns the configuration for the given serverPeer.
|
|
|
|
|
func newPeerConfig(sp *serverPeer) *peer.Config {
|
2018-08-21 18:22:53 +00:00
|
|
|
var userAgentComments []string
|
2018-09-04 20:51:07 +00:00
|
|
|
if version.PreRelease != "" {
|
|
|
|
|
userAgentComments = append(userAgentComments, version.PreRelease)
|
2018-08-21 18:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
return &peer.Config{
|
|
|
|
|
Listeners: peer.MessageListeners{
|
2016-05-20 04:54:40 +00:00
|
|
|
OnVersion: sp.OnVersion,
|
|
|
|
|
OnMemPool: sp.OnMemPool,
|
|
|
|
|
OnGetMiningState: sp.OnGetMiningState,
|
|
|
|
|
OnMiningState: sp.OnMiningState,
|
|
|
|
|
OnTx: sp.OnTx,
|
|
|
|
|
OnBlock: sp.OnBlock,
|
|
|
|
|
OnInv: sp.OnInv,
|
|
|
|
|
OnHeaders: sp.OnHeaders,
|
|
|
|
|
OnGetData: sp.OnGetData,
|
|
|
|
|
OnGetBlocks: sp.OnGetBlocks,
|
|
|
|
|
OnGetHeaders: sp.OnGetHeaders,
|
2017-11-08 21:48:44 +00:00
|
|
|
OnGetCFilter: sp.OnGetCFilter,
|
|
|
|
|
OnGetCFHeaders: sp.OnGetCFHeaders,
|
|
|
|
|
OnGetCFTypes: sp.OnGetCFTypes,
|
2016-05-20 04:54:40 +00:00
|
|
|
OnGetAddr: sp.OnGetAddr,
|
|
|
|
|
OnAddr: sp.OnAddr,
|
|
|
|
|
OnRead: sp.OnRead,
|
|
|
|
|
OnWrite: sp.OnWrite,
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
},
|
2018-08-21 18:22:53 +00:00
|
|
|
NewestBlock: sp.newestBlock,
|
|
|
|
|
HostToNetAddress: sp.server.addrManager.HostToNetAddress,
|
|
|
|
|
Proxy: cfg.Proxy,
|
|
|
|
|
UserAgentName: userAgentName,
|
|
|
|
|
UserAgentVersion: userAgentVersion,
|
|
|
|
|
UserAgentComments: userAgentComments,
|
2019-03-16 17:24:45 +00:00
|
|
|
Net: sp.server.chainParams.Net,
|
2018-08-21 18:22:53 +00:00
|
|
|
Services: sp.server.services,
|
|
|
|
|
DisableRelayTx: cfg.BlocksOnly,
|
|
|
|
|
ProtocolVersion: maxProtocolVersion,
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 03:51:07 +00:00
|
|
|
// inboundPeerConnected is invoked by the connection manager when a new inbound
|
|
|
|
|
// connection is established. It initializes a new inbound server peer
|
|
|
|
|
// instance, associates it with the connection, and starts a goroutine to wait
|
|
|
|
|
// for disconnection.
|
|
|
|
|
func (s *server) inboundPeerConnected(conn net.Conn) {
|
|
|
|
|
sp := newServerPeer(s, false)
|
2017-07-24 19:39:23 +00:00
|
|
|
sp.isWhitelisted = isWhitelisted(conn.RemoteAddr())
|
2016-11-04 03:51:07 +00:00
|
|
|
sp.Peer = peer.NewInboundPeer(newPeerConfig(sp))
|
|
|
|
|
sp.AssociateConnection(conn)
|
|
|
|
|
go s.peerDoneHandler(sp)
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-28 02:28:30 +00:00
|
|
|
// outboundPeerConnected is invoked by the connection manager when a new
|
|
|
|
|
// outbound connection is established. It initializes a new outbound server
|
|
|
|
|
// peer instance, associates it with the relevant state such as the connection
|
|
|
|
|
// request instance and the connection itself, and finally notifies the address
|
|
|
|
|
// manager of the attempt.
|
|
|
|
|
func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
|
|
|
|
|
sp := newServerPeer(s, c.Permanent)
|
2016-11-03 21:23:13 +00:00
|
|
|
p, err := peer.NewOutboundPeer(newPeerConfig(sp), c.Addr.String())
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
if err != nil {
|
2016-10-28 02:28:30 +00:00
|
|
|
srvrLog.Debugf("Cannot create outbound peer %s: %v", c.Addr, err)
|
|
|
|
|
s.connManager.Disconnect(c.ID())
|
2019-02-22 20:24:19 +00:00
|
|
|
return
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
sp.Peer = p
|
2016-10-28 02:28:30 +00:00
|
|
|
sp.connReq = c
|
2017-07-24 19:39:23 +00:00
|
|
|
sp.isWhitelisted = isWhitelisted(conn.RemoteAddr())
|
2016-10-28 02:28:30 +00:00
|
|
|
sp.AssociateConnection(conn)
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
go s.peerDoneHandler(sp)
|
2016-10-28 02:28:30 +00:00
|
|
|
s.addrManager.Attempt(sp.NA())
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// peerDoneHandler handles peer disconnects by notifiying the server that it's
|
|
|
|
|
// done.
|
|
|
|
|
func (s *server) peerDoneHandler(sp *serverPeer) {
|
2016-02-03 14:24:28 +00:00
|
|
|
sp.WaitForDisconnect()
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
s.donePeers <- sp
|
|
|
|
|
|
|
|
|
|
// Only tell block manager we are gone if we ever told it we existed.
|
|
|
|
|
if sp.VersionKnown() {
|
|
|
|
|
s.blockManager.DonePeer(sp)
|
|
|
|
|
}
|
|
|
|
|
close(sp.quit)
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// peerHandler is used to handle peer operations such as adding and removing
|
|
|
|
|
// peers to and from the server, banning peers, and broadcasting messages to
|
2014-09-08 19:19:47 +00:00
|
|
|
// peers. It must be run in a goroutine.
|
2013-08-06 21:55:22 +00:00
|
|
|
func (s *server) peerHandler() {
|
2013-08-09 19:32:22 +00:00
|
|
|
// Start the address manager and block manager, both of which are needed
|
|
|
|
|
// by peers. This is done here since their lifecycle is closely tied
|
|
|
|
|
// to this handler and rather than adding more channels to sychronize
|
|
|
|
|
// things, it's easier and slightly faster to simply start and stop them
|
|
|
|
|
// in this handler.
|
|
|
|
|
s.addrManager.Start()
|
|
|
|
|
s.blockManager.Start()
|
|
|
|
|
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Tracef("Starting peer handler")
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
|
2013-10-30 17:22:35 +00:00
|
|
|
state := &peerState{
|
2016-11-02 19:32:36 +00:00
|
|
|
inboundPeers: make(map[int32]*serverPeer),
|
|
|
|
|
persistentPeers: make(map[int32]*serverPeer),
|
|
|
|
|
outboundPeers: make(map[int32]*serverPeer),
|
|
|
|
|
banned: make(map[string]time.Time),
|
|
|
|
|
outboundGroups: make(map[string]int),
|
2013-08-01 17:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
if !cfg.DisableDNSSeed {
|
|
|
|
|
// Add peers discovered through DNS to the address manager.
|
2018-01-07 04:24:37 +00:00
|
|
|
connmgr.SeedFromDNS(activeNetParams.Params, defaultRequiredServices, dcrdLookup, func(addrs []*wire.NetAddress) {
|
2016-05-12 09:06:07 +00:00
|
|
|
// Bitcoind uses a lookup of the dns seeder here. This
|
|
|
|
|
// is rather strange since the values looked up by the
|
|
|
|
|
// DNS seed lookups will vary quite a lot.
|
|
|
|
|
// to replicate this behaviour we put all addresses as
|
|
|
|
|
// having come from the first one.
|
|
|
|
|
s.addrManager.AddAddresses(addrs, addrs[0])
|
|
|
|
|
})
|
2013-08-01 17:00:14 +00:00
|
|
|
}
|
2016-05-12 09:06:07 +00:00
|
|
|
go s.connManager.Start()
|
2013-08-01 17:00:14 +00:00
|
|
|
|
2013-10-02 00:45:21 +00:00
|
|
|
out:
|
|
|
|
|
for {
|
2013-08-06 21:55:22 +00:00
|
|
|
select {
|
|
|
|
|
// New peers connected to the server.
|
|
|
|
|
case p := <-s.newPeers:
|
2013-10-31 17:51:40 +00:00
|
|
|
s.handleAddPeerMsg(state, p)
|
2013-08-06 21:55:22 +00:00
|
|
|
|
|
|
|
|
// Disconnected peers.
|
|
|
|
|
case p := <-s.donePeers:
|
2013-10-31 17:51:40 +00:00
|
|
|
s.handleDonePeerMsg(state, p)
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2015-02-11 20:39:11 +00:00
|
|
|
// Block accepted in mainchain or orphan, update peer height.
|
|
|
|
|
case umsg := <-s.peerHeightsUpdate:
|
|
|
|
|
s.handleUpdatePeerHeights(state, umsg)
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// Peer to ban.
|
|
|
|
|
case p := <-s.banPeers:
|
2013-10-30 17:22:35 +00:00
|
|
|
s.handleBanPeerMsg(state, p)
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2013-09-09 15:58:56 +00:00
|
|
|
// New inventory to potentially be relayed to other peers.
|
|
|
|
|
case invMsg := <-s.relayInv:
|
2013-10-30 17:22:35 +00:00
|
|
|
s.handleRelayInvMsg(state, invMsg)
|
2013-09-09 15:58:56 +00:00
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// Message to broadcast to all connected peers except those
|
|
|
|
|
// which are excluded by the message.
|
|
|
|
|
case bmsg := <-s.broadcast:
|
2013-10-30 17:22:35 +00:00
|
|
|
s.handleBroadcastMsg(state, &bmsg)
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2013-10-21 17:45:30 +00:00
|
|
|
case qmsg := <-s.query:
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
s.handleQuery(state, qmsg)
|
2013-10-21 17:45:30 +00:00
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
case <-s.quit:
|
2016-02-03 14:24:28 +00:00
|
|
|
// Disconnect all peers on server shutdown.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
2016-02-11 03:34:43 +00:00
|
|
|
srvrLog.Tracef("Shutdown peer %s", sp)
|
2016-02-03 14:24:28 +00:00
|
|
|
sp.Disconnect()
|
2013-10-31 17:51:40 +00:00
|
|
|
})
|
2013-10-02 00:45:21 +00:00
|
|
|
break out
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2013-08-01 17:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
s.connManager.Stop()
|
2013-08-09 19:32:22 +00:00
|
|
|
s.blockManager.Stop()
|
|
|
|
|
s.addrManager.Stop()
|
2015-12-16 18:19:27 +00:00
|
|
|
|
|
|
|
|
// Drain channels before exiting so nothing is left waiting around
|
|
|
|
|
// to send.
|
|
|
|
|
cleanup:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-s.newPeers:
|
|
|
|
|
case <-s.donePeers:
|
|
|
|
|
case <-s.peerHeightsUpdate:
|
|
|
|
|
case <-s.relayInv:
|
|
|
|
|
case <-s.broadcast:
|
|
|
|
|
case <-s.query:
|
|
|
|
|
default:
|
|
|
|
|
break cleanup
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-06 21:55:22 +00:00
|
|
|
s.wg.Done()
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Tracef("Peer handler done")
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddPeer adds a new peer that has already been connected to the server.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) AddPeer(sp *serverPeer) {
|
|
|
|
|
s.newPeers <- sp
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BanPeer bans a peer that has already been connected to the server by ip.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) BanPeer(sp *serverPeer) {
|
|
|
|
|
s.banPeers <- sp
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 17:58:09 +00:00
|
|
|
// RelayInventory relays the passed inventory vector to all connected peers
|
|
|
|
|
// that are not already known to have it.
|
2018-09-10 09:37:29 +00:00
|
|
|
func (s *server) RelayInventory(invVect *wire.InvVect, data interface{}, immediate bool) {
|
|
|
|
|
s.relayInv <- relayMsg{invVect: invVect, data: data, immediate: immediate}
|
2013-09-09 15:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// BroadcastMessage sends msg to all peers currently connected to the server
|
|
|
|
|
// except those in the passed peers to exclude.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) BroadcastMessage(msg wire.Message, exclPeers ...*serverPeer) {
|
2013-08-06 21:55:22 +00:00
|
|
|
// XXX: Need to determine if this is an alert that has already been
|
|
|
|
|
// broadcast and refrain from broadcasting again.
|
|
|
|
|
bmsg := broadcastMsg{message: msg, excludePeers: exclPeers}
|
|
|
|
|
s.broadcast <- bmsg
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 20:29:23 +00:00
|
|
|
// ConnectedCount returns the number of currently connected peers.
|
2014-06-29 21:36:41 +00:00
|
|
|
func (s *server) ConnectedCount() int32 {
|
|
|
|
|
replyChan := make(chan int32)
|
2013-10-21 17:45:30 +00:00
|
|
|
|
|
|
|
|
s.query <- getConnCountMsg{reply: replyChan}
|
|
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
// OutboundGroupCount returns the number of peers connected to the given
|
|
|
|
|
// outbound group key.
|
|
|
|
|
func (s *server) OutboundGroupCount(key string) int {
|
|
|
|
|
replyChan := make(chan int)
|
|
|
|
|
s.query <- getOutboundGroup{key: key, reply: replyChan}
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 21:46:42 +00:00
|
|
|
// AddedNodeInfo returns an array of dcrjson.GetAddedNodeInfoResult structures
|
2014-01-24 20:10:02 +00:00
|
|
|
// describing the persistent (added) nodes.
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
func (s *server) AddedNodeInfo() []*serverPeer {
|
|
|
|
|
replyChan := make(chan []*serverPeer)
|
2014-01-24 20:10:02 +00:00
|
|
|
s.query <- getAddedNodesMsg{reply: replyChan}
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
// Peers returns an array of all connected peers.
|
|
|
|
|
func (s *server) Peers() []*serverPeer {
|
|
|
|
|
replyChan := make(chan []*serverPeer)
|
2013-10-21 17:45:30 +00:00
|
|
|
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
s.query <- getPeersMsg{reply: replyChan}
|
2013-10-21 17:45:30 +00:00
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 21:47:54 +00:00
|
|
|
// DisconnectNodeByAddr disconnects a peer by target address. Both outbound and
|
|
|
|
|
// inbound nodes will be searched for the target node. An error message will
|
|
|
|
|
// be returned if the peer was not found.
|
|
|
|
|
func (s *server) DisconnectNodeByAddr(addr string) error {
|
|
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
s.query <- disconnectNodeMsg{
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.Addr() == addr },
|
2015-03-05 21:47:54 +00:00
|
|
|
reply: replyChan,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DisconnectNodeByID disconnects a peer by target node id. Both outbound and
|
|
|
|
|
// inbound nodes will be searched for the target node. An error message will be
|
|
|
|
|
// returned if the peer was not found.
|
2015-10-20 14:51:41 +00:00
|
|
|
func (s *server) DisconnectNodeByID(id int32) error {
|
2015-03-05 21:47:54 +00:00
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
s.query <- disconnectNodeMsg{
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.ID() == id },
|
2015-03-05 21:47:54 +00:00
|
|
|
reply: replyChan,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveNodeByAddr removes a peer from the list of persistent peers if
|
|
|
|
|
// present. An error will be returned if the peer was not found.
|
|
|
|
|
func (s *server) RemoveNodeByAddr(addr string) error {
|
|
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
s.query <- removeNodeMsg{
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.Addr() == addr },
|
2015-03-05 21:47:54 +00:00
|
|
|
reply: replyChan,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 14:51:41 +00:00
|
|
|
// RemoveNodeByID removes a peer by node ID from the list of persistent peers
|
2015-03-05 21:47:54 +00:00
|
|
|
// if present. An error will be returned if the peer was not found.
|
2015-10-20 14:51:41 +00:00
|
|
|
func (s *server) RemoveNodeByID(id int32) error {
|
2015-03-05 21:47:54 +00:00
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
s.query <- removeNodeMsg{
|
peer: Refactor peer code into its own package.
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
2015-10-02 06:03:20 +00:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.ID() == id },
|
2015-03-05 21:47:54 +00:00
|
|
|
reply: replyChan,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConnectNode adds `addr' as a new outbound peer. If permanent is true then the
|
|
|
|
|
// peer will be persistent and reconnect if the connection is lost.
|
|
|
|
|
// It is an error to call this with an already existing peer.
|
|
|
|
|
func (s *server) ConnectNode(addr string, permanent bool) error {
|
|
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
|
|
s.query <- connectNodeMsg{addr: addr, permanent: permanent, reply: replyChan}
|
2013-10-29 17:18:53 +00:00
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 15:53:25 +00:00
|
|
|
// AddBytesSent adds the passed number of bytes to the total bytes sent counter
|
|
|
|
|
// for the server. It is safe for concurrent access.
|
|
|
|
|
func (s *server) AddBytesSent(bytesSent uint64) {
|
2016-01-12 21:54:10 +00:00
|
|
|
atomic.AddUint64(&s.bytesSent, bytesSent)
|
2014-02-13 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddBytesReceived adds the passed number of bytes to the total bytes received
|
|
|
|
|
// counter for the server. It is safe for concurrent access.
|
|
|
|
|
func (s *server) AddBytesReceived(bytesReceived uint64) {
|
2016-01-12 21:54:10 +00:00
|
|
|
atomic.AddUint64(&s.bytesReceived, bytesReceived)
|
2014-02-13 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-04 07:26:12 +00:00
|
|
|
// NetTotals returns the sum of all bytes received and sent across the network
|
2014-02-13 15:53:25 +00:00
|
|
|
// for all peers. It is safe for concurrent access.
|
2014-02-05 17:09:45 +00:00
|
|
|
func (s *server) NetTotals() (uint64, uint64) {
|
2016-01-12 21:54:10 +00:00
|
|
|
return atomic.LoadUint64(&s.bytesReceived),
|
|
|
|
|
atomic.LoadUint64(&s.bytesSent)
|
2014-02-04 07:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-11 20:39:11 +00:00
|
|
|
// UpdatePeerHeights updates the heights of all peers who have have announced
|
|
|
|
|
// the latest connected main chain block, or a recognized orphan. These height
|
|
|
|
|
// updates allow us to dynamically refresh peer heights, ensuring sync peer
|
|
|
|
|
// selection has access to the latest block heights for each peer.
|
2016-11-15 01:22:13 +00:00
|
|
|
func (s *server) UpdatePeerHeights(latestBlkHash *chainhash.Hash, latestHeight int64, updateSource *serverPeer) {
|
2015-02-11 20:39:11 +00:00
|
|
|
s.peerHeightsUpdate <- updatePeerHeightsMsg{
|
2016-08-08 19:04:33 +00:00
|
|
|
newHash: latestBlkHash,
|
2015-02-11 20:39:11 +00:00
|
|
|
newHeight: latestHeight,
|
|
|
|
|
originPeer: updateSource,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 00:09:35 +00:00
|
|
|
// rebroadcastHandler keeps track of user submitted inventories that we have
|
|
|
|
|
// sent out but have not yet made it into a block. We periodically rebroadcast
|
|
|
|
|
// them in case our peers restarted or otherwise lost track of them.
|
2014-03-18 19:40:49 +00:00
|
|
|
func (s *server) rebroadcastHandler() {
|
2014-03-28 19:49:38 +00:00
|
|
|
// Wait 5 min before first tx rebroadcast.
|
2014-03-28 00:09:35 +00:00
|
|
|
timer := time.NewTimer(5 * time.Minute)
|
2015-02-05 21:16:39 +00:00
|
|
|
pendingInvs := make(map[wire.InvVect]interface{})
|
2014-03-18 19:40:49 +00:00
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2018-07-06 00:56:28 +00:00
|
|
|
|
2014-03-18 19:40:49 +00:00
|
|
|
case riv := <-s.modifyRebroadcastInv:
|
|
|
|
|
switch msg := riv.(type) {
|
2018-07-06 00:56:28 +00:00
|
|
|
|
2014-03-18 19:40:49 +00:00
|
|
|
// Incoming InvVects are added to our map of RPC txs.
|
|
|
|
|
case broadcastInventoryAdd:
|
2015-01-29 05:22:27 +00:00
|
|
|
pendingInvs[*msg.invVect] = msg.data
|
2014-03-18 19:40:49 +00:00
|
|
|
|
2014-03-28 00:09:35 +00:00
|
|
|
// When an InvVect has been added to a block, we can
|
|
|
|
|
// now remove it, if it was present.
|
2014-03-18 19:40:49 +00:00
|
|
|
case broadcastInventoryDel:
|
2019-01-24 06:12:31 +00:00
|
|
|
delete(pendingInvs, *msg)
|
2018-07-06 00:56:28 +00:00
|
|
|
|
|
|
|
|
case broadcastPruneInventory:
|
|
|
|
|
best := s.blockManager.chain.BestSnapshot()
|
|
|
|
|
nextStakeDiff, err :=
|
|
|
|
|
s.blockManager.chain.CalcNextRequiredStakeDifficulty()
|
|
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Errorf("Failed to get next stake difficulty: %v",
|
|
|
|
|
err)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for iv, data := range pendingInvs {
|
|
|
|
|
tx, ok := data.(*dcrutil.Tx)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
txType := stake.DetermineTxType(tx.MsgTx())
|
|
|
|
|
|
|
|
|
|
// Remove the ticket rebroadcast if the amount not equal to
|
|
|
|
|
// the current stake difficulty.
|
|
|
|
|
if txType == stake.TxTypeSStx &&
|
|
|
|
|
tx.MsgTx().TxOut[0].Value != nextStakeDiff {
|
|
|
|
|
delete(pendingInvs, iv)
|
|
|
|
|
srvrLog.Debugf("Pending ticket purchase broadcast "+
|
|
|
|
|
"inventory for tx %v removed. Ticket value not "+
|
|
|
|
|
"equal to stake difficulty.", tx.Hash())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the ticket rebroadcast if it has already expired.
|
|
|
|
|
if txType == stake.TxTypeSStx &&
|
|
|
|
|
blockchain.IsExpired(tx, best.Height) {
|
|
|
|
|
delete(pendingInvs, iv)
|
|
|
|
|
srvrLog.Debugf("Pending ticket purchase broadcast "+
|
|
|
|
|
"inventory for tx %v removed. Transaction "+
|
|
|
|
|
"expired.", tx.Hash())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the revocation rebroadcast if the associated
|
|
|
|
|
// ticket has been revived.
|
|
|
|
|
if txType == stake.TxTypeSSRtx {
|
|
|
|
|
refSStxHash := tx.MsgTx().TxIn[0].PreviousOutPoint.Hash
|
|
|
|
|
if !s.blockManager.chain.CheckLiveTicket(refSStxHash) {
|
|
|
|
|
delete(pendingInvs, iv)
|
|
|
|
|
srvrLog.Debugf("Pending revocation broadcast "+
|
|
|
|
|
"inventory for tx %v removed. "+
|
|
|
|
|
"Associated ticket was revived.", tx.Hash())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-18 19:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case <-timer.C:
|
2014-03-28 00:09:35 +00:00
|
|
|
// Any inventory we have has not made it into a block
|
|
|
|
|
// yet. We periodically resubmit them until they have.
|
2015-01-29 05:22:27 +00:00
|
|
|
for iv, data := range pendingInvs {
|
2014-03-18 19:40:49 +00:00
|
|
|
ivCopy := iv
|
2018-09-10 09:37:29 +00:00
|
|
|
s.RelayInventory(&ivCopy, data, false)
|
2014-03-18 19:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-28 00:09:35 +00:00
|
|
|
// Process at a random time up to 30mins (in seconds)
|
|
|
|
|
// in the future.
|
|
|
|
|
timer.Reset(time.Second *
|
|
|
|
|
time.Duration(randomUint16Number(1800)))
|
2014-03-18 19:40:49 +00:00
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
|
break out
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
|
|
2014-06-02 21:10:33 +00:00
|
|
|
// Drain channels before exiting so nothing is left waiting around
|
|
|
|
|
// to send.
|
|
|
|
|
cleanup:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-s.modifyRebroadcastInv:
|
|
|
|
|
default:
|
|
|
|
|
break cleanup
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-18 19:40:49 +00:00
|
|
|
s.wg.Done()
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
// Start begins accepting connections from peers.
|
|
|
|
|
func (s *server) Start() {
|
|
|
|
|
// Already started?
|
2013-10-02 23:33:42 +00:00
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
2013-08-06 21:55:22 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Trace("Starting server")
|
2013-08-09 19:32:22 +00:00
|
|
|
|
|
|
|
|
// Start the peer handler which in turn starts the address and block
|
|
|
|
|
// managers.
|
2013-08-07 15:38:39 +00:00
|
|
|
s.wg.Add(1)
|
2013-09-12 22:24:37 +00:00
|
|
|
go s.peerHandler()
|
2014-03-18 19:40:49 +00:00
|
|
|
|
2013-12-10 01:31:22 +00:00
|
|
|
if s.nat != nil {
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
|
go s.upnpUpdateThread()
|
|
|
|
|
}
|
2013-08-09 19:32:22 +00:00
|
|
|
|
2013-09-18 05:36:40 +00:00
|
|
|
if !cfg.DisableRPC {
|
2014-03-18 19:40:49 +00:00
|
|
|
s.wg.Add(1)
|
|
|
|
|
|
|
|
|
|
// Start the rebroadcastHandler, which ensures user tx received by
|
|
|
|
|
// the RPC server are rebroadcast until being included in a block.
|
|
|
|
|
go s.rebroadcastHandler()
|
|
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
s.rpcServer.Start()
|
|
|
|
|
}
|
2014-06-12 01:09:38 +00:00
|
|
|
|
2019-03-30 18:51:28 +00:00
|
|
|
// Start the background block template generator if the config provides
|
|
|
|
|
// a mining address.
|
|
|
|
|
if len(cfg.MiningAddrs) > 0 {
|
|
|
|
|
s.bg.Start(s.context)
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 01:09:38 +00:00
|
|
|
// Start the CPU miner if generation is enabled.
|
|
|
|
|
if cfg.Generate {
|
|
|
|
|
s.cpuMiner.Start()
|
|
|
|
|
}
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop gracefully shuts down the server by stopping and disconnecting all
|
|
|
|
|
// peers and the main listener.
|
|
|
|
|
func (s *server) Stop() error {
|
2013-10-02 00:45:21 +00:00
|
|
|
// Make sure this only happens once.
|
|
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Infof("Server is already in the process of shutting down")
|
2013-08-06 21:55:22 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Warnf("Server shutting down")
|
2013-08-09 19:32:22 +00:00
|
|
|
|
2019-03-30 18:51:28 +00:00
|
|
|
// Stop the background block template generator.
|
|
|
|
|
if len(cfg.miningAddrs) > 0 {
|
|
|
|
|
minrLog.Info("Shutting down background block template generator.")
|
|
|
|
|
s.cancel()
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 16:35:46 +00:00
|
|
|
// Stop the CPU miner if needed.
|
|
|
|
|
if cfg.Generate && s.cpuMiner != nil {
|
|
|
|
|
s.cpuMiner.Stop()
|
|
|
|
|
}
|
2014-06-12 01:09:38 +00:00
|
|
|
|
2013-08-09 19:32:22 +00:00
|
|
|
// Shutdown the RPC server if it's not disabled.
|
2016-10-10 16:35:46 +00:00
|
|
|
if !cfg.DisableRPC && s.rpcServer != nil {
|
2013-08-09 19:32:22 +00:00
|
|
|
s.rpcServer.Stop()
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 21:32:30 +00:00
|
|
|
s.feeEstimator.Close()
|
|
|
|
|
|
2013-08-09 19:32:22 +00:00
|
|
|
// Signal the remaining goroutines to quit.
|
|
|
|
|
close(s.quit)
|
2013-08-06 21:55:22 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WaitForShutdown blocks until the main listener and peer handlers are stopped.
|
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
|
s.wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 01:43:36 +00:00
|
|
|
// parseListeners splits the list of listen addresses passed in addrs into
|
|
|
|
|
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
|
2013-11-14 03:12:41 +00:00
|
|
|
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
2013-11-14 01:43:36 +00:00
|
|
|
// detects addresses which apply to "all interfaces" and adds the address to
|
|
|
|
|
// both slices.
|
2013-11-26 00:40:16 +00:00
|
|
|
func parseListeners(addrs []string) ([]string, []string, bool, error) {
|
2013-11-14 02:57:34 +00:00
|
|
|
ipv4ListenAddrs := make([]string, 0, len(addrs)*2)
|
|
|
|
|
ipv6ListenAddrs := make([]string, 0, len(addrs)*2)
|
2013-11-26 00:40:16 +00:00
|
|
|
haveWildcard := false
|
|
|
|
|
|
2013-11-14 02:57:34 +00:00
|
|
|
for _, addr := range addrs {
|
2013-09-19 15:46:33 +00:00
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
|
if err != nil {
|
2013-11-14 01:43:36 +00:00
|
|
|
// Shouldn't happen due to already being normalized.
|
2013-11-26 00:40:16 +00:00
|
|
|
return nil, nil, false, err
|
2013-09-19 15:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-14 01:43:36 +00:00
|
|
|
// Empty host or host of * on plan9 is both IPv4 and IPv6.
|
2013-09-19 15:46:33 +00:00
|
|
|
if host == "" || (host == "*" && runtime.GOOS == "plan9") {
|
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
2013-11-26 00:40:16 +00:00
|
|
|
haveWildcard = true
|
2013-09-19 15:46:33 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-04 02:32:15 +00:00
|
|
|
// Strip IPv6 zone id if present since net.ParseIP does not
|
|
|
|
|
// handle it.
|
|
|
|
|
zoneIndex := strings.LastIndex(host, "%")
|
|
|
|
|
if zoneIndex > 0 {
|
|
|
|
|
host = host[:zoneIndex]
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-19 15:46:33 +00:00
|
|
|
// Parse the IP.
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
|
if ip == nil {
|
2013-11-26 00:40:16 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("'%s' is not a "+
|
|
|
|
|
"valid IP address", host)
|
2013-09-19 15:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-14 01:43:36 +00:00
|
|
|
// To4 returns nil when the IP is not an IPv4 address, so use
|
|
|
|
|
// this determine the address type.
|
2013-09-19 15:46:33 +00:00
|
|
|
if ip.To4() == nil {
|
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
|
|
|
|
} else {
|
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-26 00:40:16 +00:00
|
|
|
return ipv4ListenAddrs, ipv6ListenAddrs, haveWildcard, nil
|
2013-09-19 15:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-10 01:31:22 +00:00
|
|
|
func (s *server) upnpUpdateThread() {
|
|
|
|
|
// Go off immediately to prevent code duplication, thereafter we renew
|
|
|
|
|
// lease every 15 minutes.
|
|
|
|
|
timer := time.NewTimer(0 * time.Second)
|
2014-05-29 17:35:09 +00:00
|
|
|
lport, _ := strconv.ParseInt(activeNetParams.DefaultPort, 10, 16)
|
2013-12-10 01:31:22 +00:00
|
|
|
first := true
|
|
|
|
|
out:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-timer.C:
|
2018-02-02 15:19:48 +00:00
|
|
|
// TODO: pick external port more cleverly
|
|
|
|
|
// TODO: know which ports we are listening to on an external net.
|
|
|
|
|
// TODO: if specific listen port doesn't work then ask for wildcard
|
2013-12-10 01:31:22 +00:00
|
|
|
// listen port?
|
|
|
|
|
// XXX this assumes timeout is in seconds.
|
|
|
|
|
listenPort, err := s.nat.AddPortMapping("tcp", int(lport), int(lport),
|
2016-01-20 21:46:42 +00:00
|
|
|
"dcrd listen port", 20*60)
|
2013-12-10 01:31:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("can't add UPnP port mapping: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if first && err == nil {
|
2018-02-02 15:19:48 +00:00
|
|
|
// TODO: look this up periodically to see if upnp domain changed
|
2013-12-10 01:31:22 +00:00
|
|
|
// and so did ip.
|
|
|
|
|
externalip, err := s.nat.GetExternalAddress()
|
|
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("UPnP can't get external address: %v", err)
|
|
|
|
|
continue out
|
|
|
|
|
}
|
2015-02-05 21:16:39 +00:00
|
|
|
na := wire.NewNetAddressIPPort(externalip, uint16(listenPort),
|
2015-08-24 15:48:59 +00:00
|
|
|
s.services)
|
2014-07-06 13:47:42 +00:00
|
|
|
err = s.addrManager.AddLocalAddress(na, addrmgr.UpnpPrio)
|
|
|
|
|
if err != nil {
|
2018-02-02 15:19:48 +00:00
|
|
|
srvrLog.Warnf("Failed to add UPnP local address %s: %v",
|
|
|
|
|
na.IP.String(), err)
|
|
|
|
|
} else {
|
|
|
|
|
srvrLog.Warnf("Successfully bound via UPnP to %s",
|
|
|
|
|
addrmgr.NetAddressKey(na))
|
|
|
|
|
first = false
|
2014-07-06 13:47:42 +00:00
|
|
|
}
|
2013-12-10 01:31:22 +00:00
|
|
|
}
|
|
|
|
|
timer.Reset(time.Minute * 15)
|
|
|
|
|
case <-s.quit:
|
|
|
|
|
break out
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
|
|
|
|
|
|
if err := s.nat.DeletePortMapping("tcp", int(lport), int(lport)); err != nil {
|
|
|
|
|
srvrLog.Warnf("unable to remove UPnP port mapping: %v", err)
|
|
|
|
|
} else {
|
2018-02-18 18:07:06 +00:00
|
|
|
srvrLog.Debugf("successfully disestablished UPnP port mapping")
|
2013-12-10 01:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 22:05:43 +00:00
|
|
|
// standardScriptVerifyFlags returns the script flags that should be used when
|
|
|
|
|
// executing transaction scripts to enforce additional checks which are required
|
|
|
|
|
// for the script to be considered standard. Note these flags are different
|
|
|
|
|
// than what is required for the consensus rules in that they are more strict.
|
|
|
|
|
func standardScriptVerifyFlags(chain *blockchain.BlockChain) (txscript.ScriptFlags, error) {
|
2017-09-08 05:34:27 +00:00
|
|
|
scriptFlags := mempool.BaseStandardVerifyFlags
|
|
|
|
|
|
|
|
|
|
// Enable validation of OP_SHA256 if the stake vote for the agenda is
|
|
|
|
|
// active.
|
|
|
|
|
isActive, err := chain.IsLNFeaturesAgendaActive()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
if isActive {
|
|
|
|
|
scriptFlags |= txscript.ScriptVerifySHA256
|
|
|
|
|
}
|
|
|
|
|
return scriptFlags, nil
|
2017-09-14 22:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-20 21:46:42 +00:00
|
|
|
// newServer returns a new dcrd server configured to listen on addr for the
|
2018-03-01 23:23:35 +00:00
|
|
|
// Decred network type specified by chainParams. Use start to begin accepting
|
2013-08-06 21:55:22 +00:00
|
|
|
// connections from peers.
|
2018-12-06 21:32:30 +00:00
|
|
|
func newServer(listenAddrs []string, db database.DB, chainParams *chaincfg.Params, dataDir string, interrupt <-chan struct{}) (*server, error) {
|
2015-08-24 15:48:59 +00:00
|
|
|
services := defaultServices
|
2017-11-08 21:48:44 +00:00
|
|
|
if cfg.NoCFilters {
|
|
|
|
|
services &^= wire.SFNodeCF
|
|
|
|
|
}
|
2015-08-24 15:48:59 +00:00
|
|
|
|
2016-01-20 21:46:42 +00:00
|
|
|
amgr := addrmgr.New(cfg.DataDir, dcrdLookup)
|
2013-11-26 00:40:16 +00:00
|
|
|
|
2013-11-14 03:16:49 +00:00
|
|
|
var listeners []net.Listener
|
2013-12-10 01:31:22 +00:00
|
|
|
var nat NAT
|
2013-08-07 17:35:01 +00:00
|
|
|
if !cfg.DisableListen {
|
2013-11-26 00:40:16 +00:00
|
|
|
ipv4Addrs, ipv6Addrs, wildcard, err :=
|
|
|
|
|
parseListeners(listenAddrs)
|
2013-11-14 03:16:49 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
listeners = make([]net.Listener, 0, len(ipv4Addrs)+len(ipv6Addrs))
|
2013-11-26 19:30:24 +00:00
|
|
|
discover := true
|
|
|
|
|
if len(cfg.ExternalIPs) != 0 {
|
|
|
|
|
discover = false
|
2013-12-16 20:48:23 +00:00
|
|
|
// if this fails we have real issues.
|
|
|
|
|
port, _ := strconv.ParseUint(
|
2014-05-29 17:35:09 +00:00
|
|
|
activeNetParams.DefaultPort, 10, 16)
|
2013-11-26 19:30:24 +00:00
|
|
|
|
|
|
|
|
for _, sip := range cfg.ExternalIPs {
|
2013-12-16 20:48:23 +00:00
|
|
|
eport := uint16(port)
|
|
|
|
|
host, portstr, err := net.SplitHostPort(sip)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// no port, use default.
|
|
|
|
|
host = sip
|
|
|
|
|
} else {
|
|
|
|
|
port, err := strconv.ParseUint(
|
|
|
|
|
portstr, 10, 16)
|
|
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("Can not parse "+
|
|
|
|
|
"port from %s for "+
|
|
|
|
|
"externalip: %v", sip,
|
|
|
|
|
err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
eport = uint16(port)
|
|
|
|
|
}
|
2014-07-06 06:04:24 +00:00
|
|
|
na, err := amgr.HostToNetAddress(host, eport,
|
2015-08-24 15:48:59 +00:00
|
|
|
services)
|
2013-11-26 19:30:24 +00:00
|
|
|
if err != nil {
|
2013-12-16 20:48:23 +00:00
|
|
|
srvrLog.Warnf("Not adding %s as "+
|
|
|
|
|
"externalip: %v", sip, err)
|
2013-11-26 19:30:24 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-06 13:47:42 +00:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.ManualPrio)
|
|
|
|
|
if err != nil {
|
|
|
|
|
amgrLog.Warnf("Skipping specified external IP: %v", err)
|
|
|
|
|
}
|
2013-11-26 19:30:24 +00:00
|
|
|
}
|
2013-12-10 01:31:22 +00:00
|
|
|
} else if discover && cfg.Upnp {
|
|
|
|
|
nat, err = Discover()
|
|
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("Can't discover upnp: %v", err)
|
|
|
|
|
}
|
|
|
|
|
// nil nat here is fine, just means no upnp on network.
|
2013-11-26 19:30:24 +00:00
|
|
|
}
|
2013-11-14 03:16:49 +00:00
|
|
|
|
2018-02-02 15:19:48 +00:00
|
|
|
// TODO: nonstandard port...
|
2013-11-26 00:40:16 +00:00
|
|
|
if wildcard {
|
|
|
|
|
port, err :=
|
2014-05-29 17:35:09 +00:00
|
|
|
strconv.ParseUint(activeNetParams.DefaultPort,
|
2013-11-26 00:40:16 +00:00
|
|
|
10, 16)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// I can't think of a cleaner way to do this...
|
|
|
|
|
goto nowc
|
|
|
|
|
}
|
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
2017-10-12 19:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("Unable to get interface addresses: %v", err)
|
|
|
|
|
}
|
2013-11-26 00:40:16 +00:00
|
|
|
for _, a := range addrs {
|
|
|
|
|
ip, _, err := net.ParseCIDR(a.String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-02-05 21:16:39 +00:00
|
|
|
na := wire.NewNetAddressIPPort(ip,
|
2015-08-24 15:48:59 +00:00
|
|
|
uint16(port), services)
|
2013-11-26 19:30:24 +00:00
|
|
|
if discover {
|
2014-07-06 13:47:42 +00:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.InterfacePrio)
|
|
|
|
|
if err != nil {
|
|
|
|
|
amgrLog.Debugf("Skipping local address: %v", err)
|
|
|
|
|
}
|
2013-11-26 19:30:24 +00:00
|
|
|
}
|
2013-11-26 00:40:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nowc:
|
|
|
|
|
|
2013-11-14 03:12:41 +00:00
|
|
|
for _, addr := range ipv4Addrs {
|
2013-09-19 15:46:33 +00:00
|
|
|
listener, err := net.Listen("tcp4", addr)
|
|
|
|
|
if err != nil {
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 15:46:33 +00:00
|
|
|
err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 00:40:16 +00:00
|
|
|
|
2013-11-26 19:30:24 +00:00
|
|
|
if discover {
|
2014-07-06 13:47:42 +00:00
|
|
|
if na, err := amgr.DeserializeNetAddress(addr); err == nil {
|
|
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.BoundPrio)
|
|
|
|
|
if err != nil {
|
|
|
|
|
amgrLog.Warnf("Skipping bound address: %v", err)
|
|
|
|
|
}
|
2013-11-26 19:30:24 +00:00
|
|
|
}
|
2013-11-26 00:40:16 +00:00
|
|
|
}
|
2013-08-07 17:35:01 +00:00
|
|
|
}
|
2013-08-07 15:38:39 +00:00
|
|
|
|
2013-11-14 03:12:41 +00:00
|
|
|
for _, addr := range ipv6Addrs {
|
2013-09-19 15:46:33 +00:00
|
|
|
listener, err := net.Listen("tcp6", addr)
|
|
|
|
|
if err != nil {
|
2013-11-21 18:03:56 +00:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 15:46:33 +00:00
|
|
|
err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 19:30:24 +00:00
|
|
|
if discover {
|
2014-07-06 13:47:42 +00:00
|
|
|
if na, err := amgr.DeserializeNetAddress(addr); err == nil {
|
|
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.BoundPrio)
|
|
|
|
|
if err != nil {
|
|
|
|
|
amgrLog.Debugf("Skipping bound address: %v", err)
|
|
|
|
|
}
|
2013-11-26 19:30:24 +00:00
|
|
|
}
|
2013-11-26 00:40:16 +00:00
|
|
|
}
|
2013-09-19 15:46:33 +00:00
|
|
|
}
|
2013-11-14 03:16:49 +00:00
|
|
|
|
2013-09-19 15:46:33 +00:00
|
|
|
if len(listeners) == 0 {
|
2014-07-02 14:25:42 +00:00
|
|
|
return nil, errors.New("no valid listen address")
|
2013-08-07 17:35:01 +00:00
|
|
|
}
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2016-05-12 09:06:07 +00:00
|
|
|
|
2019-03-30 18:51:28 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-05-12 09:06:07 +00:00
|
|
|
s := server{
|
|
|
|
|
chainParams: chainParams,
|
|
|
|
|
addrManager: amgr,
|
|
|
|
|
newPeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
|
donePeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
|
banPeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
|
query: make(chan interface{}),
|
|
|
|
|
relayInv: make(chan relayMsg, cfg.MaxPeers),
|
|
|
|
|
broadcast: make(chan broadcastMsg, cfg.MaxPeers),
|
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
|
modifyRebroadcastInv: make(chan interface{}),
|
|
|
|
|
peerHeightsUpdate: make(chan updatePeerHeightsMsg),
|
|
|
|
|
nat: nat,
|
|
|
|
|
db: db,
|
|
|
|
|
timeSource: blockchain.NewMedianTime(),
|
|
|
|
|
services: services,
|
|
|
|
|
sigCache: txscript.NewSigCache(cfg.SigCacheMaxSize),
|
2019-03-30 18:51:28 +00:00
|
|
|
context: ctx,
|
|
|
|
|
cancel: cancel,
|
2016-05-12 09:06:07 +00:00
|
|
|
}
|
2016-02-19 04:51:18 +00:00
|
|
|
|
|
|
|
|
// Create the transaction and address indexes if needed.
|
|
|
|
|
//
|
|
|
|
|
// CAUTION: the txindex needs to be first in the indexes array because
|
|
|
|
|
// the addrindex uses data from the txindex during catchup. If the
|
|
|
|
|
// addrindex is run first, it may not have the transactions from the
|
|
|
|
|
// current block indexed.
|
|
|
|
|
var indexes []indexers.Indexer
|
|
|
|
|
if cfg.TxIndex || cfg.AddrIndex {
|
|
|
|
|
// Enable transaction index if address index is enabled since it
|
|
|
|
|
// requires it.
|
|
|
|
|
if !cfg.TxIndex {
|
|
|
|
|
indxLog.Infof("Transaction index enabled because it " +
|
|
|
|
|
"is required by the address index")
|
|
|
|
|
cfg.TxIndex = true
|
|
|
|
|
} else {
|
|
|
|
|
indxLog.Info("Transaction index is enabled")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.txIndex = indexers.NewTxIndex(db)
|
|
|
|
|
indexes = append(indexes, s.txIndex)
|
|
|
|
|
}
|
|
|
|
|
if cfg.AddrIndex {
|
|
|
|
|
indxLog.Info("Address index is enabled")
|
|
|
|
|
s.addrIndex = indexers.NewAddrIndex(db, chainParams)
|
|
|
|
|
indexes = append(indexes, s.addrIndex)
|
|
|
|
|
}
|
|
|
|
|
if !cfg.NoExistsAddrIndex {
|
|
|
|
|
indxLog.Info("Exists address index is enabled")
|
|
|
|
|
s.existsAddrIndex = indexers.NewExistsAddrIndex(db, chainParams)
|
|
|
|
|
indexes = append(indexes, s.existsAddrIndex)
|
|
|
|
|
}
|
2017-11-08 21:48:44 +00:00
|
|
|
if !cfg.NoCFilters {
|
|
|
|
|
indxLog.Info("CF index is enabled")
|
|
|
|
|
s.cfIndex = indexers.NewCfIndex(db, chainParams)
|
|
|
|
|
indexes = append(indexes, s.cfIndex)
|
|
|
|
|
}
|
2016-02-19 04:51:18 +00:00
|
|
|
|
2018-12-06 21:32:30 +00:00
|
|
|
feC := fees.EstimatorConfig{
|
|
|
|
|
MinBucketFee: cfg.minRelayTxFee,
|
|
|
|
|
MaxBucketFee: dcrutil.Amount(fees.DefaultMaxBucketFeeMultiplier) * cfg.minRelayTxFee,
|
|
|
|
|
MaxConfirms: fees.DefaultMaxConfirmations,
|
|
|
|
|
FeeRateStep: fees.DefaultFeeRateStep,
|
|
|
|
|
DatabaseFile: path.Join(dataDir, "feesdb"),
|
|
|
|
|
|
|
|
|
|
// 1e5 is the previous (up to 1.1.0) mempool.DefaultMinRelayTxFee that
|
|
|
|
|
// un-upgraded wallets will be using, so track this particular rate
|
|
|
|
|
// explicitly. Note that bumping this value will cause the existing fees
|
|
|
|
|
// database to become invalid and will force nodes to explicitly delete
|
|
|
|
|
// it.
|
|
|
|
|
ExtraBucketFee: 1e5,
|
|
|
|
|
}
|
|
|
|
|
fe, err := fees.NewEstimator(&feC)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.feeEstimator = fe
|
|
|
|
|
|
2016-02-19 04:51:18 +00:00
|
|
|
// Create an index manager if any of the optional indexes are enabled.
|
|
|
|
|
var indexManager blockchain.IndexManager
|
|
|
|
|
if len(indexes) > 0 {
|
|
|
|
|
indexManager = indexers.NewManager(db, indexes, chainParams)
|
|
|
|
|
}
|
2018-02-17 19:20:33 +00:00
|
|
|
bm, err := newBlockManager(&s, indexManager, interrupt)
|
2013-08-06 21:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-09-30 22:43:14 +00:00
|
|
|
s.blockManager = bm
|
2015-11-22 07:02:28 +00:00
|
|
|
|
2016-08-19 16:08:37 +00:00
|
|
|
txC := mempool.Config{
|
|
|
|
|
Policy: mempool.Policy{
|
2017-09-21 17:53:34 +00:00
|
|
|
MaxTxVersion: 2,
|
2016-04-11 21:37:52 +00:00
|
|
|
DisableRelayPriority: cfg.NoRelayPriority,
|
2018-02-05 22:01:55 +00:00
|
|
|
AcceptNonStd: cfg.AcceptNonStd,
|
2016-04-11 21:37:52 +00:00
|
|
|
FreeTxRelayLimit: cfg.FreeTxRelayLimit,
|
|
|
|
|
MaxOrphanTxs: cfg.MaxOrphanTxs,
|
|
|
|
|
MaxOrphanTxSize: defaultMaxOrphanTxSize,
|
|
|
|
|
MaxSigOpsPerTx: blockchain.MaxSigOpsPerBlock / 5,
|
|
|
|
|
MinRelayTxFee: cfg.minRelayTxFee,
|
2016-11-29 17:53:14 +00:00
|
|
|
AllowOldVotes: cfg.AllowOldVotes,
|
2017-09-14 22:05:43 +00:00
|
|
|
StandardVerifyFlags: func() (txscript.ScriptFlags, error) {
|
|
|
|
|
return standardScriptVerifyFlags(bm.chain)
|
|
|
|
|
},
|
2019-01-26 14:49:35 +00:00
|
|
|
AcceptSequenceLocks: bm.chain.IsFixSeqLocksAgendaActive,
|
2016-11-29 17:53:14 +00:00
|
|
|
},
|
2015-08-26 04:03:18 +00:00
|
|
|
ChainParams: chainParams,
|
2016-05-26 21:27:12 +00:00
|
|
|
NextStakeDifficulty: func() (int64, error) {
|
2018-09-12 10:09:43 +00:00
|
|
|
return bm.chain.BestSnapshot().NextStakeDiff, nil
|
2016-05-26 21:27:12 +00:00
|
|
|
},
|
2017-09-21 17:53:34 +00:00
|
|
|
FetchUtxoView: bm.chain.FetchUtxoView,
|
|
|
|
|
BlockByHash: bm.chain.BlockByHash,
|
2018-01-29 02:10:09 +00:00
|
|
|
BestHash: func() *chainhash.Hash { return &bm.chain.BestSnapshot().Hash },
|
2017-09-21 17:53:34 +00:00
|
|
|
BestHeight: func() int64 { return bm.chain.BestSnapshot().Height },
|
|
|
|
|
CalcSequenceLock: bm.chain.CalcSequenceLock,
|
|
|
|
|
SubsidyCache: bm.chain.FetchSubsidyCache(),
|
|
|
|
|
SigCache: s.sigCache,
|
2018-09-12 10:09:43 +00:00
|
|
|
PastMedianTime: func() time.Time {
|
|
|
|
|
return bm.chain.BestSnapshot().MedianTime
|
|
|
|
|
},
|
2018-12-06 21:32:30 +00:00
|
|
|
AddrIndex: s.addrIndex,
|
|
|
|
|
ExistsAddrIndex: s.existsAddrIndex,
|
|
|
|
|
AddTxToFeeEstimation: s.feeEstimator.AddMemPoolTransaction,
|
|
|
|
|
RemoveTxFromFeeEstimation: s.feeEstimator.RemoveMemPoolTransaction,
|
2019-03-30 18:51:28 +00:00
|
|
|
OnVoteReceived: func(voteTx *wire.MsgTx) {
|
|
|
|
|
if s.bg != nil {
|
|
|
|
|
s.bg.OnVoteReceived(voteTx)
|
|
|
|
|
}
|
|
|
|
|
},
|
2015-11-21 03:12:17 +00:00
|
|
|
}
|
2016-08-19 16:08:37 +00:00
|
|
|
s.txMemPool = mempool.New(&txC)
|
2015-11-22 07:02:28 +00:00
|
|
|
|
2018-09-12 17:18:44 +00:00
|
|
|
// Create the mining policy and block template generator based on the
|
|
|
|
|
// configuration options.
|
|
|
|
|
//
|
2015-12-08 07:54:49 +00:00
|
|
|
// NOTE: The CPU miner relies on the mempool, so the mempool has to be
|
|
|
|
|
// created before calling the function to create the CPU miner.
|
2016-05-26 21:52:36 +00:00
|
|
|
policy := mining.Policy{
|
2015-11-22 07:02:28 +00:00
|
|
|
BlockMinSize: cfg.BlockMinSize,
|
|
|
|
|
BlockMaxSize: cfg.BlockMaxSize,
|
|
|
|
|
BlockPrioritySize: cfg.BlockPrioritySize,
|
|
|
|
|
TxMinFreeFee: cfg.minRelayTxFee,
|
|
|
|
|
}
|
2019-03-30 18:51:28 +00:00
|
|
|
tg := newBlkTmplGenerator(&policy, s.txMemPool, s.timeSource, s.sigCache,
|
|
|
|
|
s.chainParams, bm.chain, bm)
|
|
|
|
|
|
|
|
|
|
// Create the background block template generator if the config has a
|
|
|
|
|
// mining address.
|
|
|
|
|
if len(cfg.miningAddrs) > 0 {
|
|
|
|
|
s.bg = newBgBlkTmplGenerator(tg, cfg.miningAddrs, cfg.SimNet)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 16:13:40 +00:00
|
|
|
s.cpuMiner = newCPUMiner(&cpuminerConfig{
|
|
|
|
|
ChainParams: s.chainParams,
|
|
|
|
|
PermitConnectionlessMining: cfg.SimNet,
|
2019-03-30 18:51:28 +00:00
|
|
|
BlockTemplateGenerator: tg,
|
2018-09-13 16:13:40 +00:00
|
|
|
MiningAddrs: cfg.miningAddrs,
|
|
|
|
|
ProcessBlock: bm.ProcessBlock,
|
|
|
|
|
ConnectedCount: s.ConnectedCount,
|
|
|
|
|
IsCurrent: bm.IsCurrent,
|
|
|
|
|
})
|
2013-08-06 21:55:22 +00:00
|
|
|
|
2016-05-12 09:06:07 +00:00
|
|
|
// Only setup a function to return new addresses to connect to when
|
|
|
|
|
// not running in connect-only mode. The simulation network is always
|
|
|
|
|
// in connect-only mode since it is only intended to connect to
|
|
|
|
|
// specified peers and actively avoid advertising and connecting to
|
|
|
|
|
// discovered peers in order to prevent it from becoming a public test
|
|
|
|
|
// network.
|
2016-11-03 21:23:13 +00:00
|
|
|
var newAddressFunc func() (net.Addr, error)
|
2016-05-12 09:06:07 +00:00
|
|
|
if !cfg.SimNet && len(cfg.ConnectPeers) == 0 {
|
2016-11-03 21:23:13 +00:00
|
|
|
newAddressFunc = func() (net.Addr, error) {
|
2016-05-12 09:06:07 +00:00
|
|
|
for tries := 0; tries < 100; tries++ {
|
2016-11-21 14:56:46 +00:00
|
|
|
addr := s.addrManager.GetAddress()
|
2016-05-12 09:06:07 +00:00
|
|
|
if addr == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Address will not be invalid, local or unroutable
|
|
|
|
|
// because addrmanager rejects those on addition.
|
|
|
|
|
// Just check that we don't already have an address
|
|
|
|
|
// in the same group so that we are not connecting
|
|
|
|
|
// to the same network segment at the expense of
|
|
|
|
|
// others.
|
|
|
|
|
key := addrmgr.GroupKey(addr.NetAddress())
|
|
|
|
|
if s.OutboundGroupCount(key) != 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only allow recent nodes (10mins) after we failed 30
|
|
|
|
|
// times
|
2017-10-12 20:43:42 +00:00
|
|
|
if tries < 30 && time.Since(addr.LastAttempt()) < 10*time.Minute {
|
2016-05-12 09:06:07 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allow nondefault ports after 50 failed tries.
|
|
|
|
|
if fmt.Sprintf("%d", addr.NetAddress().Port) !=
|
|
|
|
|
activeNetParams.DefaultPort && tries < 50 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-11-03 21:23:13 +00:00
|
|
|
|
|
|
|
|
addrString := addrmgr.NetAddressKey(addr.NetAddress())
|
|
|
|
|
return addrStringToNetAddr(addrString)
|
2016-05-12 09:06:07 +00:00
|
|
|
}
|
2016-11-03 21:23:13 +00:00
|
|
|
|
|
|
|
|
return nil, errors.New("no valid connect address")
|
2016-05-12 09:06:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a connection manager.
|
2016-11-02 19:32:36 +00:00
|
|
|
targetOutbound := defaultTargetOutbound
|
|
|
|
|
if cfg.MaxPeers < targetOutbound {
|
|
|
|
|
targetOutbound = cfg.MaxPeers
|
2016-10-31 20:09:29 +00:00
|
|
|
}
|
2016-05-12 09:06:07 +00:00
|
|
|
cmgr, err := connmgr.New(&connmgr.Config{
|
2016-11-04 03:51:07 +00:00
|
|
|
Listeners: listeners,
|
|
|
|
|
OnAccept: s.inboundPeerConnected,
|
2016-11-02 19:32:36 +00:00
|
|
|
RetryDuration: connectionRetryInterval,
|
|
|
|
|
TargetOutbound: uint32(targetOutbound),
|
|
|
|
|
Dial: dcrdDial,
|
|
|
|
|
OnConnection: s.outboundPeerConnected,
|
|
|
|
|
GetNewAddress: newAddressFunc,
|
2016-05-12 09:06:07 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.connManager = cmgr
|
|
|
|
|
|
|
|
|
|
// Start up persistent peers.
|
|
|
|
|
permanentPeers := cfg.ConnectPeers
|
|
|
|
|
if len(permanentPeers) == 0 {
|
|
|
|
|
permanentPeers = cfg.AddPeers
|
|
|
|
|
}
|
|
|
|
|
for _, addr := range permanentPeers {
|
2016-11-03 21:23:13 +00:00
|
|
|
tcpAddr, err := addrStringToNetAddr(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go s.connManager.Connect(&connmgr.ConnReq{
|
|
|
|
|
Addr: tcpAddr,
|
|
|
|
|
Permanent: true,
|
|
|
|
|
})
|
2016-05-12 09:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-18 05:36:40 +00:00
|
|
|
if !cfg.DisableRPC {
|
2019-03-30 18:51:28 +00:00
|
|
|
s.rpcServer, err = newRPCServer(cfg.RPCListeners, tg, &s)
|
2013-08-06 21:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-08-31 14:45:16 +00:00
|
|
|
|
|
|
|
|
// Signal process shutdown when the RPC server requests it.
|
|
|
|
|
go func() {
|
|
|
|
|
<-s.rpcServer.RequestedProcessShutdown()
|
|
|
|
|
shutdownRequestChannel <- struct{}{}
|
|
|
|
|
}()
|
2013-08-06 21:55:22 +00:00
|
|
|
}
|
2014-06-12 01:09:38 +00:00
|
|
|
|
2013-08-06 21:55:22 +00:00
|
|
|
return &s, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 21:23:13 +00:00
|
|
|
// addrStringToNetAddr takes an address in the form of 'host:port' and returns
|
|
|
|
|
// a net.Addr which maps to the original address with any host names resolved
|
|
|
|
|
// to IP addresses.
|
|
|
|
|
func addrStringToNetAddr(addr string) (net.Addr, error) {
|
|
|
|
|
host, strPort, err := net.SplitHostPort(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attempt to look up an IP address associated with the parsed host.
|
|
|
|
|
// The dcrdLookup function will transparently handle performing the
|
|
|
|
|
// lookup over Tor if necessary.
|
|
|
|
|
ips, err := dcrdLookup(host)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-11-12 02:49:22 +00:00
|
|
|
if len(ips) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("no addresses found for %s", host)
|
|
|
|
|
}
|
2016-11-03 21:23:13 +00:00
|
|
|
|
|
|
|
|
port, err := strconv.Atoi(strPort)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &net.TCPAddr{
|
|
|
|
|
IP: ips[0],
|
|
|
|
|
Port: port,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 19:39:23 +00:00
|
|
|
// isWhitelisted returns whether the IP address is included in the whitelisted
|
|
|
|
|
// networks and IPs.
|
|
|
|
|
func isWhitelisted(addr net.Addr) bool {
|
|
|
|
|
if len(cfg.whitelists) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(addr.String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
srvrLog.Warnf("Unable to SplitHostPort on '%s': %v", addr, err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
|
if ip == nil {
|
|
|
|
|
srvrLog.Warnf("Unable to parse IP '%s'", addr)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, ipnet := range cfg.whitelists {
|
|
|
|
|
if ipnet.Contains(ip) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|