2016-01-06 23:27:39 +01:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-08-06 23:55:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-03-18 20:40:49 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2013-10-29 18:18:53 +01:00
|
|
|
"errors"
|
2013-09-17 21:01:22 +02:00
|
|
|
"fmt"
|
2014-03-18 20:40:49 +01:00
|
|
|
"math"
|
2014-07-06 08:04:24 +02:00
|
|
|
mrand "math/rand"
|
2013-08-06 23:55:22 +02:00
|
|
|
"net"
|
2013-09-19 17:46:33 +02:00
|
|
|
"runtime"
|
2013-08-01 19:00:14 +02:00
|
|
|
"strconv"
|
2015-11-04 03:32:15 +01:00
|
|
|
"strings"
|
2013-08-06 23:55:22 +02:00
|
|
|
"sync"
|
2013-10-02 02:45:21 +02:00
|
|
|
"sync/atomic"
|
2013-08-06 23:55:22 +02:00
|
|
|
"time"
|
2014-07-02 15:50:08 +02:00
|
|
|
|
2015-01-17 07:48:13 +01:00
|
|
|
"github.com/btcsuite/btcd/addrmgr"
|
2015-01-30 23:25:42 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-02-06 06:18:27 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2015-01-27 22:38:23 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-11-25 23:27:14 +01:00
|
|
|
"github.com/btcsuite/btcd/mining"
|
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 08:03:20 +02:00
|
|
|
"github.com/btcsuite/btcd/peer"
|
2015-09-25 01:22:00 +02:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-29 06:22:27 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
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 08:03:20 +02:00
|
|
|
"github.com/btcsuite/btcutil/bloom"
|
2013-08-06 23:55:22 +02:00
|
|
|
)
|
|
|
|
|
2013-10-08 00:57:43 +02:00
|
|
|
const (
|
2015-08-24 17:48:59 +02:00
|
|
|
// These constants are used by the DNS seed code to pick a random last
|
|
|
|
// seen time.
|
2013-10-08 00:57:43 +02:00
|
|
|
secondsIn3Days int32 = 24 * 60 * 60 * 3
|
|
|
|
secondsIn4Days int32 = 24 * 60 * 60 * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-08-24 17:48:59 +02:00
|
|
|
// defaultServices describes the default services that are supported by
|
|
|
|
// the server.
|
|
|
|
defaultServices = wire.SFNodeNetwork | wire.SFNodeBloom
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-08 00:57:43 +02:00
|
|
|
// defaultMaxOutbound is the default number of max outbound peers.
|
|
|
|
defaultMaxOutbound = 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 08:03:20 +02: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 11:04:22 +01:00
|
|
|
connectionRetryInterval = time.Second * 5
|
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 08:03:20 +02:00
|
|
|
|
|
|
|
// maxConnectionRetryInterval is the max amount of time retrying of a
|
|
|
|
// persistent peer is allowed to grow to. This is necessary since the
|
|
|
|
// retry logic uses a backoff mechanism which increases the interval
|
|
|
|
// base done the number of retries that have been done.
|
|
|
|
maxConnectionRetryInterval = time.Minute * 5
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// userAgentName is the user agent name and is used to help identify
|
|
|
|
// ourselves to other bitcoin peers.
|
|
|
|
userAgentName = "btcd"
|
|
|
|
|
|
|
|
// userAgentVersion is the user agent version and is used to help
|
|
|
|
// identify ourselves to other bitcoin peers.
|
|
|
|
userAgentVersion = fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)
|
2013-10-08 00:57:43 +02:00
|
|
|
)
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// broadcastMsg provides the ability to house a bitcoin message to be broadcast
|
|
|
|
// to all connected peers except specified excluded peers.
|
|
|
|
type broadcastMsg struct {
|
2015-02-05 22:16:39 +01: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 08:03:20 +02:00
|
|
|
excludePeers []*serverPeer
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 01:03:45 +01:00
|
|
|
// broadcastInventoryAdd is a type used to declare that the InvVect it contains
|
2014-03-18 20:40:49 +01:00
|
|
|
// needs to be added to the rebroadcast map
|
2015-01-29 06:22:27 +01:00
|
|
|
type broadcastInventoryAdd relayMsg
|
2014-03-18 20:40:49 +01:00
|
|
|
|
2014-03-28 01:03:45 +01:00
|
|
|
// broadcastInventoryDel is a type used to declare that the InvVect it contains
|
2014-03-18 20:40:49 +01:00
|
|
|
// needs to be removed from the rebroadcast map
|
2015-02-05 22:16:39 +01:00
|
|
|
type broadcastInventoryDel *wire.InvVect
|
2014-03-18 20:40:49 +01:00
|
|
|
|
2015-01-29 06:22:27 +01:00
|
|
|
// relayMsg packages an inventory vector along with the newly discovered
|
|
|
|
// inventory so the relay has access to that information.
|
|
|
|
type relayMsg struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
invVect *wire.InvVect
|
2015-01-29 06:22:27 +01:00
|
|
|
data interface{}
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:39:11 +01: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 {
|
|
|
|
newSha *wire.ShaHash
|
|
|
|
newHeight int32
|
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 08:03:20 +02:00
|
|
|
originPeer *serverPeer
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerState maintains state of inbound, persistent, outbound peers as well
|
|
|
|
// as banned peers and outbound groups.
|
|
|
|
type peerState struct {
|
|
|
|
pendingPeers map[string]*serverPeer
|
|
|
|
peers map[int32]*serverPeer
|
|
|
|
outboundPeers map[int32]*serverPeer
|
|
|
|
persistentPeers map[int32]*serverPeer
|
|
|
|
banned map[string]time.Time
|
|
|
|
outboundGroups map[string]int
|
|
|
|
maxOutboundPeers int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the count of all known peers.
|
|
|
|
func (ps *peerState) Count() int {
|
|
|
|
return len(ps.peers) + len(ps.outboundPeers) + len(ps.persistentPeers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutboundCount returns the count of known outbound peers.
|
|
|
|
func (ps *peerState) OutboundCount() int {
|
|
|
|
return len(ps.outboundPeers) + len(ps.persistentPeers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedMoreOutbound returns true if more outbound peers are required.
|
|
|
|
func (ps *peerState) NeedMoreOutbound() bool {
|
|
|
|
return ps.OutboundCount() < ps.maxOutboundPeers &&
|
|
|
|
ps.Count() < cfg.MaxPeers
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedMoreTries returns true if more outbound peer attempts can be tried.
|
|
|
|
func (ps *peerState) NeedMoreTries() bool {
|
|
|
|
return len(ps.pendingPeers) < 2*(ps.maxOutboundPeers-ps.OutboundCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forPendingPeers is a helper function that runs closure on all pending peers
|
|
|
|
// known to peerState.
|
|
|
|
func (ps *peerState) forPendingPeers(closure func(sp *serverPeer)) {
|
|
|
|
for _, e := range ps.pendingPeers {
|
|
|
|
closure(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forAllPeers is a helper function that runs closure on all peers known to
|
|
|
|
// peerState.
|
|
|
|
func (ps *peerState) forAllPeers(closure func(sp *serverPeer)) {
|
|
|
|
for _, e := range ps.peers {
|
|
|
|
closure(e)
|
|
|
|
}
|
|
|
|
ps.forAllOutboundPeers(closure)
|
2015-02-11 21:39:11 +01:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// server provides a bitcoin server for handling communications to and from
|
|
|
|
// bitcoin peers.
|
|
|
|
type server struct {
|
2014-03-18 20:40:49 +01:00
|
|
|
listeners []net.Listener
|
2015-02-06 06:18:27 +01:00
|
|
|
chainParams *chaincfg.Params
|
2014-03-18 20:40:49 +01:00
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
shutdownSched int32 // atomic
|
|
|
|
bytesMutex sync.Mutex // For the following two fields.
|
|
|
|
bytesReceived uint64 // Total bytes received from all peers since start.
|
|
|
|
bytesSent uint64 // Total bytes sent by all peers since start.
|
2014-07-06 08:04:24 +02:00
|
|
|
addrManager *addrmgr.AddrManager
|
2015-09-25 01:22:00 +02:00
|
|
|
sigCache *txscript.SigCache
|
2014-03-18 20:40:49 +01:00
|
|
|
rpcServer *rpcServer
|
|
|
|
blockManager *blockManager
|
2015-01-04 02:42:01 +01:00
|
|
|
addrIndexer *addrIndexer
|
2014-03-18 20:40:49 +01:00
|
|
|
txMemPool *txMemPool
|
2014-06-12 03:09:38 +02:00
|
|
|
cpuMiner *CPUMiner
|
2015-11-21 04:12:17 +01:00
|
|
|
relayNtfnChan chan *btcutil.Tx
|
2014-03-18 20:40:49 +01: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 08:03:20 +02:00
|
|
|
pendingPeers chan *serverPeer
|
|
|
|
newPeers chan *serverPeer
|
|
|
|
donePeers chan *serverPeer
|
|
|
|
banPeers chan *serverPeer
|
|
|
|
retryPeers chan *serverPeer
|
2014-07-02 17:31:10 +02:00
|
|
|
wakeup chan struct{}
|
2014-03-18 20:40:49 +01:00
|
|
|
query chan interface{}
|
2015-01-29 06:22:27 +01:00
|
|
|
relayInv chan relayMsg
|
2014-03-18 20:40:49 +01:00
|
|
|
broadcast chan broadcastMsg
|
2015-02-11 21:39:11 +01:00
|
|
|
peerHeightsUpdate chan updatePeerHeightsMsg
|
2014-03-18 20:40:49 +01:00
|
|
|
wg sync.WaitGroup
|
2014-07-02 17:31:10 +02:00
|
|
|
quit chan struct{}
|
2014-03-18 20:40:49 +01:00
|
|
|
nat NAT
|
2015-01-27 22:38:23 +01:00
|
|
|
db database.Db
|
2015-01-30 23:25:42 +01:00
|
|
|
timeSource blockchain.MedianTimeSource
|
2015-08-24 17:48:59 +02:00
|
|
|
services wire.ServiceFlag
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
// serverPeer extends the peer to maintain state shared by the server and
|
|
|
|
// the blockmanager.
|
|
|
|
type serverPeer struct {
|
|
|
|
*peer.Peer
|
|
|
|
|
|
|
|
server *server
|
|
|
|
persistent bool
|
|
|
|
continueHash *wire.ShaHash
|
|
|
|
relayMtx sync.Mutex
|
|
|
|
disableRelayTx bool
|
|
|
|
requestQueue []*wire.InvVect
|
|
|
|
requestedTxns map[wire.ShaHash]struct{}
|
|
|
|
requestedBlocks map[wire.ShaHash]struct{}
|
|
|
|
filter *bloom.Filter
|
|
|
|
knownAddresses map[string]struct{}
|
|
|
|
quit chan struct{}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
requestedTxns: make(map[wire.ShaHash]struct{}),
|
|
|
|
requestedBlocks: make(map[wire.ShaHash]struct{}),
|
|
|
|
filter: bloom.LoadFilter(nil),
|
|
|
|
knownAddresses: make(map[string]struct{}),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
txProcessed: make(chan struct{}, 1),
|
|
|
|
blockProcessed: make(chan struct{}, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
sp.knownAddresses[addrmgr.NetAddressKey(na)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addressKnown true if the given address is already known to the peer.
|
|
|
|
func (sp *serverPeer) addressKnown(na *wire.NetAddress) bool {
|
|
|
|
_, exists := sp.knownAddresses[addrmgr.NetAddressKey(na)]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
defer sp.relayMtx.Unlock()
|
|
|
|
|
|
|
|
return sp.disableRelayTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnVersion is invoked when a peer receives a version bitcoin message
|
|
|
|
// and is used to negotiate the protocol version details as well as kick start
|
|
|
|
// the communications.
|
|
|
|
func (sp *serverPeer) OnVersion(p *peer.Peer, msg *wire.MsgVersion) {
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Choose whether or not to relay transactions before a filter command
|
|
|
|
// is received.
|
|
|
|
sp.setDisableRelayTx(msg.DisableRelayTx)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if !cfg.SimNet {
|
|
|
|
addrManager := sp.server.addrManager
|
|
|
|
// Outbound connections.
|
|
|
|
if !p.Inbound() {
|
|
|
|
// TODO(davec): Only do this if not doing the initial block
|
|
|
|
// download and the local address is routable.
|
|
|
|
if !cfg.DisableListen /* && isCurrent? */ {
|
|
|
|
// Get address that best matches.
|
|
|
|
lna := addrManager.GetBestLocalAddress(p.NA())
|
|
|
|
if addrmgr.IsRoutable(lna) {
|
|
|
|
// Filter addresses the peer already knows about.
|
|
|
|
addresses := []*wire.NetAddress{lna}
|
|
|
|
sp.pushAddrMsg(addresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request known addresses if the server address manager needs
|
|
|
|
// more and the peer has a protocol version new enough to
|
|
|
|
// include a timestamp with addresses.
|
|
|
|
hasTimestamp := p.ProtocolVersion() >=
|
|
|
|
wire.NetAddressTimeVersion
|
|
|
|
if addrManager.NeedMoreAddresses() && hasTimestamp {
|
|
|
|
p.QueueMessage(wire.NewMsgGetAddr(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the address as a known good address.
|
|
|
|
addrManager.Good(p.NA())
|
|
|
|
} else {
|
|
|
|
// A peer might not be advertising the same address that it
|
|
|
|
// actually connected from. One example of why this can happen
|
|
|
|
// is with NAT. Only add the address to the address manager if
|
|
|
|
// the addresses agree.
|
|
|
|
if addrmgr.NetAddressKey(&msg.AddrMe) == addrmgr.NetAddressKey(p.NA()) {
|
|
|
|
addrManager.AddAddress(p.NA(), p.NA())
|
|
|
|
addrManager.Good(p.NA())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add valid peer to the server.
|
|
|
|
sp.server.AddPeer(sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnMemPool is invoked when a peer receives a mempool bitcoin message.
|
|
|
|
// It creates and sends an inventory message with the contents of the memory
|
|
|
|
// pool up to the maximum inventory allowed per message. When the peer has a
|
|
|
|
// bloom filter loaded, the contents are filtered accordingly.
|
|
|
|
func (sp *serverPeer) OnMemPool(p *peer.Peer, msg *wire.MsgMemPool) {
|
|
|
|
// Generate inventory message with the available transactions in the
|
|
|
|
// transaction memory pool. Limit it to the max allowed inventory
|
|
|
|
// per message. The the NewMsgInvSizeHint function automatically limits
|
|
|
|
// 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 {
|
|
|
|
// Another thread might have removed the transaction from the
|
|
|
|
// pool since the initial query.
|
|
|
|
hash := txDesc.Tx.Sha()
|
|
|
|
if !txMemPool.IsTransactionInPool(hash) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either add all transactions when there is no bloom filter,
|
|
|
|
// or only the transactions that match the filter when there is
|
|
|
|
// one.
|
|
|
|
if !sp.filter.IsLoaded() || sp.filter.MatchTxAndUpdate(txDesc.Tx) {
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, hash)
|
|
|
|
invMsg.AddInvVect(iv)
|
|
|
|
if i+1 >= wire.MaxInvPerMsg {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the inventory message if there is anything to send.
|
|
|
|
if len(invMsg.InvList) > 0 {
|
|
|
|
p.QueueMessage(invMsg, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnTx is invoked when a peer receives a tx bitcoin message. It blocks
|
|
|
|
// until the bitcoin 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.
|
|
|
|
func (sp *serverPeer) OnTx(p *peer.Peer, msg *wire.MsgTx) {
|
|
|
|
// Add the transaction to the known inventory for the peer.
|
|
|
|
// Convert the raw MsgTx to a btcutil.Tx which provides some convenience
|
|
|
|
// methods and things such as hash caching.
|
|
|
|
tx := btcutil.NewTx(msg)
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeTx, tx.Sha())
|
|
|
|
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
|
|
|
|
// from queueing up a bunch of bad transactions before disconnecting (or
|
|
|
|
// being disconnected) and wasting memory.
|
|
|
|
sp.server.blockManager.QueueTx(tx, sp)
|
|
|
|
<-sp.txProcessed
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnBlock is invoked when a peer receives a block bitcoin message. It
|
|
|
|
// blocks until the bitcoin block has been fully processed.
|
|
|
|
func (sp *serverPeer) OnBlock(p *peer.Peer, msg *wire.MsgBlock, buf []byte) {
|
|
|
|
// Convert the raw MsgBlock to a btcutil.Block which provides some
|
|
|
|
// convenience methods and things such as hash caching.
|
|
|
|
block := btcutil.NewBlockFromBlockAndBytes(msg, buf)
|
|
|
|
|
|
|
|
// Add the block to the known inventory for the peer.
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, block.Sha())
|
|
|
|
p.AddKnownInventory(iv)
|
|
|
|
|
|
|
|
// Queue the block up to be handled by the block
|
|
|
|
// manager and intentionally block further receives
|
|
|
|
// until the bitcoin block is fully processed and known
|
|
|
|
// good or bad. This helps prevent a malicious peer
|
|
|
|
// from queueing up a bunch of bad blocks before
|
|
|
|
// disconnecting (or being 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 bitcoin block has been fully processed.
|
|
|
|
sp.server.blockManager.QueueBlock(block, sp)
|
|
|
|
<-sp.blockProcessed
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnInv is invoked when a peer receives an inv bitcoin message and is
|
|
|
|
// used to examine the inventory being advertised by the remote peer and react
|
|
|
|
// 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) {
|
|
|
|
sp.server.blockManager.QueueInv(msg, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnHeaders is invoked when a peer receives a headers bitcoin
|
|
|
|
// message. The message is passed down to the block manager.
|
|
|
|
func (sp *serverPeer) OnHeaders(p *peer.Peer, msg *wire.MsgHeaders) {
|
|
|
|
sp.server.blockManager.QueueHeaders(msg, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetData is invoked when a peer receives a getdata bitcoin message and
|
|
|
|
// is used to deliver block and transaction information.
|
|
|
|
func (sp *serverPeer) OnGetData(p *peer.Peer, msg *wire.MsgGetData) {
|
|
|
|
numAdded := 0
|
|
|
|
notFound := wire.NewMsgNotFound()
|
|
|
|
|
|
|
|
// We wait on this wait channel periodically to prevent queueing
|
|
|
|
// 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.
|
|
|
|
if i == len(msg.InvList)-1 && len(notFound.InvList) == 0 {
|
|
|
|
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)
|
|
|
|
case wire.InvTypeFilteredBlock:
|
|
|
|
err = sp.server.pushMerkleBlockMsg(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnGetBlocks is invoked when a peer receives a getblocks bitcoin
|
|
|
|
// message.
|
|
|
|
func (sp *serverPeer) OnGetBlocks(p *peer.Peer, msg *wire.MsgGetBlocks) {
|
|
|
|
db := sp.server.db
|
|
|
|
|
|
|
|
// Return all block hashes to the latest one (up to max per message) if
|
|
|
|
// no stop hash was specified.
|
|
|
|
// Attempt to find the ending index of the stop hash if specified.
|
|
|
|
endIdx := database.AllShas
|
|
|
|
if !msg.HashStop.IsEqual(&zeroHash) {
|
|
|
|
height, err := db.FetchBlockHeightBySha(&msg.HashStop)
|
|
|
|
if err == nil {
|
|
|
|
endIdx = height + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the most recent known block based on the block locator.
|
|
|
|
// 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.
|
|
|
|
// This mirrors the behavior in the reference implementation.
|
|
|
|
startIdx := int32(1)
|
|
|
|
for _, hash := range msg.BlockLocatorHashes {
|
|
|
|
height, err := db.FetchBlockHeightBySha(hash)
|
|
|
|
if err == nil {
|
|
|
|
// Start with the next hash since we know this one.
|
|
|
|
startIdx = height + 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't attempt to fetch more than we can put into a single message.
|
|
|
|
autoContinue := false
|
|
|
|
if endIdx-startIdx > wire.MaxBlocksPerMsg {
|
|
|
|
endIdx = startIdx + wire.MaxBlocksPerMsg
|
|
|
|
autoContinue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate inventory message.
|
|
|
|
//
|
|
|
|
// The FetchBlockBySha call is limited to a maximum number of hashes
|
|
|
|
// per invocation. Since the maximum number of inventory per message
|
|
|
|
// might be larger, call it multiple times with the appropriate indices
|
|
|
|
// as needed.
|
|
|
|
invMsg := wire.NewMsgInv()
|
|
|
|
for start := startIdx; start < endIdx; {
|
|
|
|
// Fetch the inventory from the block database.
|
|
|
|
hashList, err := db.FetchHeightRange(start, endIdx)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Warnf("Block lookup failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The database did not return any further hashes. Break out of
|
|
|
|
// the loop now.
|
|
|
|
if len(hashList) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add block inventory to the message.
|
|
|
|
for _, hash := range hashList {
|
|
|
|
hashCopy := hash
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, &hashCopy)
|
|
|
|
invMsg.AddInvVect(iv)
|
|
|
|
}
|
|
|
|
start += int32(len(hashList))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the inventory message if there is anything to send.
|
|
|
|
if len(invMsg.InvList) > 0 {
|
|
|
|
invListLen := len(invMsg.InvList)
|
|
|
|
if autoContinue && invListLen == wire.MaxBlocksPerMsg {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnGetHeaders is invoked when a peer receives a getheaders bitcoin
|
|
|
|
// message.
|
|
|
|
func (sp *serverPeer) OnGetHeaders(p *peer.Peer, msg *wire.MsgGetHeaders) {
|
|
|
|
// Ignore getheaders requests if not in sync.
|
|
|
|
if !sp.server.blockManager.IsCurrent() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db := sp.server.db
|
|
|
|
|
|
|
|
// Attempt to look up the height of the provided stop hash.
|
|
|
|
endIdx := database.AllShas
|
|
|
|
height, err := db.FetchBlockHeightBySha(&msg.HashStop)
|
|
|
|
if err == nil {
|
|
|
|
endIdx = height + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no block locators so a specific header is being requested
|
|
|
|
// as identified by the stop hash.
|
|
|
|
if len(msg.BlockLocatorHashes) == 0 {
|
|
|
|
// No blocks with the stop hash were found so there is nothing
|
|
|
|
// to do. Just return. This behavior mirrors the reference
|
|
|
|
// implementation.
|
|
|
|
if endIdx == database.AllShas {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch and send the requested block header.
|
|
|
|
header, err := db.FetchBlockHeaderBySha(&msg.HashStop)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Warnf("Lookup of known block hash failed: %v",
|
|
|
|
err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
headersMsg := wire.NewMsgHeaders()
|
|
|
|
headersMsg.AddBlockHeader(header)
|
|
|
|
p.QueueMessage(headersMsg, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the most recent known block based on the block locator.
|
|
|
|
// 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.
|
|
|
|
// This mirrors the behavior in the reference implementation.
|
|
|
|
startIdx := int32(1)
|
|
|
|
for _, hash := range msg.BlockLocatorHashes {
|
|
|
|
height, err := db.FetchBlockHeightBySha(hash)
|
|
|
|
if err == nil {
|
|
|
|
// Start with the next hash since we know this one.
|
|
|
|
startIdx = height + 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't attempt to fetch more than we can put into a single message.
|
|
|
|
if endIdx-startIdx > wire.MaxBlockHeadersPerMsg {
|
|
|
|
endIdx = startIdx + wire.MaxBlockHeadersPerMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate headers message and send it.
|
|
|
|
//
|
|
|
|
// The FetchHeightRange call is limited to a maximum number of hashes
|
|
|
|
// per invocation. Since the maximum number of headers per message
|
|
|
|
// might be larger, call it multiple times with the appropriate indices
|
|
|
|
// as needed.
|
|
|
|
headersMsg := wire.NewMsgHeaders()
|
|
|
|
for start := startIdx; start < endIdx; {
|
|
|
|
// Fetch the inventory from the block database.
|
|
|
|
hashList, err := db.FetchHeightRange(start, endIdx)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Warnf("Header lookup failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The database did not return any further hashes. Break out of
|
|
|
|
// the loop now.
|
|
|
|
if len(hashList) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add headers to the message.
|
|
|
|
for _, hash := range hashList {
|
|
|
|
header, err := db.FetchBlockHeaderBySha(&hash)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Warnf("Lookup of known block hash "+
|
|
|
|
"failed: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
headersMsg.AddBlockHeader(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start at the next block header after the latest one on the
|
|
|
|
// next loop iteration.
|
|
|
|
start += int32(len(hashList))
|
|
|
|
}
|
|
|
|
p.QueueMessage(headersMsg, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnFilterAdd is invoked when a peer receives a filteradd bitcoin
|
|
|
|
// message and is used by remote peers to add data to an already loaded bloom
|
|
|
|
// filter. The peer will be disconnected if a filter is not loaded when this
|
|
|
|
// message is received.
|
|
|
|
func (sp *serverPeer) OnFilterAdd(p *peer.Peer, msg *wire.MsgFilterAdd) {
|
|
|
|
if sp.filter.IsLoaded() {
|
|
|
|
peerLog.Debugf("%s sent a filteradd request with no filter "+
|
|
|
|
"loaded -- disconnecting", p)
|
|
|
|
p.Disconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sp.filter.Add(msg.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnFilterClear is invoked when a peer receives a filterclear bitcoin
|
|
|
|
// message and is used by remote peers to clear an already loaded bloom filter.
|
|
|
|
// The peer will be disconnected if a filter is not loaded when this message is
|
|
|
|
// received.
|
|
|
|
func (sp *serverPeer) OnFilterClear(p *peer.Peer, msg *wire.MsgFilterClear) {
|
|
|
|
if !sp.filter.IsLoaded() {
|
|
|
|
peerLog.Debugf("%s sent a filterclear request with no "+
|
|
|
|
"filter loaded -- disconnecting", p)
|
|
|
|
p.Disconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sp.filter.Unload()
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnFilterLoad is invoked when a peer receives a filterload bitcoin
|
|
|
|
// message and it used to load a bloom filter that should be used for
|
|
|
|
// delivering merkle blocks and associated transactions that match the filter.
|
|
|
|
func (sp *serverPeer) OnFilterLoad(p *peer.Peer, msg *wire.MsgFilterLoad) {
|
|
|
|
sp.setDisableRelayTx(false)
|
|
|
|
|
|
|
|
sp.filter.Reload(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnGetAddr is invoked when a peer receives a getaddr bitcoin message
|
|
|
|
// and is used to provide the peer with known addresses from the address
|
|
|
|
// manager.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current known addresses from the address manager.
|
|
|
|
addrCache := sp.server.addrManager.AddressCache()
|
|
|
|
|
|
|
|
// Push the addresses.
|
|
|
|
sp.pushAddrMsg(addrCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnAddr is invoked when a peer receives an addr bitcoin message and is
|
|
|
|
// used to notify the server about advertised addresses.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore old style addresses which don't include a timestamp.
|
|
|
|
if p.ProtocolVersion() < wire.NetAddressTimeVersion {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
now := time.Now()
|
|
|
|
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 18:22:35 +01:00
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
// randomUint16Number returns a random uint16 in a specified input range. Note
|
2014-03-27 20:05:27 +01:00
|
|
|
// that the range is in zeroth ordering; if you pass it 1800, you will get
|
|
|
|
// values from 0 to 1800.
|
2014-03-18 20:40:49 +01:00
|
|
|
func randomUint16Number(max uint16) uint16 {
|
2014-03-27 20:05:27 +01: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 20:40:49 +01: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 20:05:27 +01: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 22:16:39 +01:00
|
|
|
func (s *server) AddRebroadcastInventory(iv *wire.InvVect, data interface{}) {
|
2014-06-02 23:10:33 +02:00
|
|
|
// Ignore if shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-29 06:22:27 +01:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryAdd{invVect: iv, data: data}
|
2014-03-18 20:40:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 20:05:27 +01:00
|
|
|
// RemoveRebroadcastInventory removes 'iv' from the list of items to be
|
|
|
|
// rebroadcasted if present.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (s *server) RemoveRebroadcastInventory(iv *wire.InvVect) {
|
2014-06-02 23:10:33 +02:00
|
|
|
// Ignore if shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryDel(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 08:03:20 +02: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.
|
|
|
|
func (s *server) pushTxMsg(sp *serverPeer, sha *wire.ShaHash, doneChan, waitChan chan struct{}) error {
|
|
|
|
// 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.
|
|
|
|
tx, err := s.txMemPool.FetchTransaction(sha)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Tracef("Unable to fetch tx %v from transaction "+
|
|
|
|
"pool: %v", sha, err)
|
2013-10-31 18:51:40 +01: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 08:03:20 +02: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 18:51:40 +01: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 08:03:20 +02:00
|
|
|
sp.QueueMessage(tx.MsgTx(), doneChan)
|
|
|
|
|
|
|
|
return nil
|
2013-10-31 18:51:40 +01: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 08:03:20 +02: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.
|
|
|
|
func (s *server) pushBlockMsg(sp *serverPeer, sha *wire.ShaHash, doneChan, waitChan chan struct{}) error {
|
|
|
|
blk, err := s.db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Tracef("Unable to fetch requested block sha %v: %v",
|
|
|
|
sha, err)
|
|
|
|
|
|
|
|
if doneChan != nil {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
return err
|
2014-01-24 21:13:19 +01: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 08:03:20 +02:00
|
|
|
|
|
|
|
// Once we have fetched data wait for any previous operation to finish.
|
|
|
|
if waitChan != nil {
|
|
|
|
<-waitChan
|
2014-01-24 21:13:19 +01: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 08:03:20 +02:00
|
|
|
|
|
|
|
// We only send the channel for this message if we aren't sending
|
|
|
|
// an inv straight after.
|
|
|
|
var dc chan struct{}
|
|
|
|
continueHash := sp.continueHash
|
|
|
|
sendInv := continueHash != nil && continueHash.IsEqual(sha)
|
|
|
|
if !sendInv {
|
|
|
|
dc = doneChan
|
|
|
|
}
|
|
|
|
sp.QueueMessage(blk.MsgBlock(), dc)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
hash, _, err := s.db.NewestSha()
|
|
|
|
if err == nil {
|
|
|
|
invMsg := wire.NewMsgInvSizeHint(1)
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
|
|
|
|
invMsg.AddInvVect(iv)
|
|
|
|
sp.QueueMessage(invMsg, doneChan)
|
|
|
|
sp.continueHash = nil
|
|
|
|
} else if doneChan != nil {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-01-24 21:13:19 +01: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 08:03:20 +02:00
|
|
|
// pushMerkleBlockMsg sends a merkleblock message for the provided block hash to
|
|
|
|
// the connected peer. Since a merkle block requires the peer to have a filter
|
|
|
|
// loaded, this call will simply be ignored if there is no filter loaded. An
|
|
|
|
// error is returned if the block hash is not known.
|
|
|
|
func (s *server) pushMerkleBlockMsg(sp *serverPeer, sha *wire.ShaHash, doneChan, waitChan chan struct{}) error {
|
|
|
|
// Do not send a response if the peer doesn't have a filter loaded.
|
|
|
|
if !sp.filter.IsLoaded() {
|
|
|
|
if doneChan != nil {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := s.db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Tracef("Unable to fetch requested block sha %v: %v",
|
|
|
|
sha, err)
|
|
|
|
|
|
|
|
if doneChan != nil {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a merkle block by filtering the requested block according
|
|
|
|
// to the filter for the peer.
|
|
|
|
merkle, matchedTxIndices := bloom.NewMerkleBlock(blk, sp.filter)
|
|
|
|
|
|
|
|
// Once we have fetched data wait for any previous operation to finish.
|
|
|
|
if waitChan != nil {
|
|
|
|
<-waitChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the merkleblock. Only send the done channel with this message
|
|
|
|
// if no transactions will be sent afterwards.
|
|
|
|
var dc chan struct{}
|
|
|
|
if len(matchedTxIndices) == 0 {
|
|
|
|
dc = doneChan
|
2014-01-24 21:13:19 +01: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 08:03:20 +02:00
|
|
|
sp.QueueMessage(merkle, dc)
|
|
|
|
|
|
|
|
// Finally, send any matched transactions.
|
|
|
|
blkTransactions := blk.MsgBlock().Transactions
|
|
|
|
for i, txIndex := range matchedTxIndices {
|
|
|
|
// Only send the done channel on the final transaction.
|
|
|
|
var dc chan struct{}
|
|
|
|
if i == len(matchedTxIndices)-1 {
|
|
|
|
dc = doneChan
|
|
|
|
}
|
|
|
|
if txIndex < uint32(len(blkTransactions)) {
|
|
|
|
sp.QueueMessage(blkTransactions[txIndex], dc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-01-24 21:13:19 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 21:39:11 +01: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 08:03:20 +02:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
2015-02-11 21:39:11 +01: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 08:03:20 +02:00
|
|
|
if sp == umsg.originPeer {
|
2015-02-11 21:39:11 +01: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 08:03:20 +02:00
|
|
|
// This is a pointer to the underlying memory which doesn't
|
|
|
|
// change.
|
|
|
|
latestBlkSha := sp.LastAnnouncedBlock()
|
|
|
|
|
2015-02-11 21:39:11 +01:00
|
|
|
// Skip this peer if it hasn't recently announced any new 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 08:03:20 +02:00
|
|
|
if latestBlkSha == nil {
|
2015-02-11 21:39:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the peer has recently announced a block, and this block
|
|
|
|
// matches our newly accepted block, then update their block
|
|
|
|
// height.
|
2015-04-04 20:25:49 +02:00
|
|
|
if *latestBlkSha == *umsg.newSha {
|
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 08:03:20 +02:00
|
|
|
sp.UpdateLastBlockHeight(umsg.newHeight)
|
|
|
|
sp.UpdateLastAnnouncedBlock(nil)
|
2015-02-11 21:39:11 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
|
|
|
|
if sp == nil {
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Ignore new peers if we're shutting down.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("New peer %s ignored - server is shutting "+
|
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 08:03:20 +02:00
|
|
|
"down", sp)
|
|
|
|
sp.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
host, _, err := net.SplitHostPort(sp.Addr())
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("can't split hostport %v", 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 08:03:20 +02:00
|
|
|
sp.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-30 18:22:35 +01:00
|
|
|
if banEnd, ok := state.banned[host]; ok {
|
2013-08-06 23:55:22 +02:00
|
|
|
if time.Now().Before(banEnd) {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("Peer %s is banned for another %v - "+
|
2013-08-06 23:55:22 +02:00
|
|
|
"disconnecting", host, banEnd.Sub(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 08:03:20 +02:00
|
|
|
sp.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Peer %s is no longer banned", host)
|
2013-10-30 18:22:35 +01:00
|
|
|
delete(state.banned, host)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for max peers from a single 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 08:03:20 +02:00
|
|
|
// Limit max outbound peers.
|
|
|
|
if _, ok := state.pendingPeers[sp.Addr()]; ok {
|
|
|
|
if state.OutboundCount() >= state.maxOutboundPeers {
|
|
|
|
srvrLog.Infof("Max outbound peers reached [%d] - disconnecting "+
|
|
|
|
"peer %s", state.maxOutboundPeers, sp)
|
|
|
|
sp.Shutdown()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Limit max number of total peers.
|
2013-10-31 18:51:40 +01:00
|
|
|
if state.Count() >= cfg.MaxPeers {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Max peers reached [%d] - disconnecting "+
|
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 08:03:20 +02:00
|
|
|
"peer %s", cfg.MaxPeers, sp)
|
|
|
|
sp.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
// TODO(oga) how to handle permanent peers here?
|
|
|
|
// they should be rescheduled.
|
|
|
|
return false
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
srvrLog.Debugf("New peer %s", sp)
|
|
|
|
if sp.Inbound() {
|
|
|
|
state.peers[sp.ID()] = sp
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]++
|
|
|
|
if sp.persistent {
|
|
|
|
state.persistentPeers[sp.ID()] = sp
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
state.outboundPeers[sp.ID()] = sp
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
// Remove from pending peers.
|
|
|
|
delete(state.pendingPeers, sp.Addr())
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
|
|
|
|
if _, ok := state.pendingPeers[sp.Addr()]; ok {
|
|
|
|
delete(state.pendingPeers, sp.Addr())
|
|
|
|
srvrLog.Debugf("Removed pending peer %s", sp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var list map[int32]*serverPeer
|
|
|
|
if sp.persistent {
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
} else if sp.Inbound() {
|
2013-10-31 18:51:40 +01:00
|
|
|
list = state.peers
|
|
|
|
} else {
|
|
|
|
list = state.outboundPeers
|
|
|
|
}
|
2015-11-22 09:00:43 +01:00
|
|
|
if _, ok := list[sp.ID()]; ok {
|
|
|
|
// Issue an asynchronous reconnect if the peer was a
|
|
|
|
// persistent outbound connection.
|
|
|
|
if !sp.Inbound() && sp.persistent && atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
// Retry peer
|
2015-12-27 20:56:40 +01:00
|
|
|
sp2 := s.newOutboundPeer(sp.Addr(), sp.persistent)
|
|
|
|
if sp2 != nil {
|
|
|
|
go s.retryConn(sp2, false)
|
2013-10-31 20:53:29 +01:00
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2015-11-22 09:00:43 +01:00
|
|
|
if !sp.Inbound() && sp.VersionKnown() {
|
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
|
|
|
}
|
|
|
|
delete(list, sp.ID())
|
|
|
|
srvrLog.Debugf("Removed peer %s", sp)
|
|
|
|
return
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2015-11-22 09:00:43 +01: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 08:03:20 +02: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 18:51:40 +01:00
|
|
|
// If we get here it means that either we didn't know about the peer
|
|
|
|
// or we purposefully deleted it.
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) handleBanPeerMsg(state *peerState, sp *serverPeer) {
|
|
|
|
host, _, err := net.SplitHostPort(sp.Addr())
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
srvrLog.Debugf("can't split ban peer %s %v", sp.Addr(), err)
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
direction := directionString(sp.Inbound())
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Banned peer %s (%s) for %v", host, direction,
|
2013-08-06 23:55:22 +02:00
|
|
|
cfg.BanDuration)
|
2013-10-30 18:22:35 +01:00
|
|
|
state.banned[host] = time.Now().Add(cfg.BanDuration)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-09-12 19:11:22 +02:00
|
|
|
// handleRelayInvMsg deals with relaying inventory to peers that are not already
|
2013-09-09 17:58:56 +02:00
|
|
|
// known to have it. It is invoked from the peerHandler goroutine.
|
2015-01-29 06:22:27 +01: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 08:03:20 +02:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
if !sp.Connected() {
|
2013-10-31 18:51:40 +01:00
|
|
|
return
|
2013-09-18 00:39:49 +02:00
|
|
|
}
|
2013-09-09 17:58:56 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
if msg.invVect.Type == wire.InvTypeTx {
|
2014-07-09 03:44:21 +02: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 08:03:20 +02:00
|
|
|
if sp.relayTxDisabled() {
|
2014-07-09 03:44:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Don't relay the transaction if there is a bloom
|
|
|
|
// filter loaded and the transaction doesn't match 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 08:03:20 +02:00
|
|
|
if sp.filter.IsLoaded() {
|
2015-01-29 06:22:27 +01:00
|
|
|
tx, ok := msg.data.(*btcutil.Tx)
|
|
|
|
if !ok {
|
|
|
|
peerLog.Warnf("Underlying data for tx" +
|
|
|
|
" inv relay is not a transaction")
|
2014-07-09 03:44:21 +02: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 08:03:20 +02:00
|
|
|
if !sp.filter.MatchTxAndUpdate(tx) {
|
2014-07-09 03:44:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue the inventory to be relayed with the next batch.
|
|
|
|
// It will be ignored if the peer is already known to
|
|
|
|
// have the inventory.
|
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 08:03:20 +02:00
|
|
|
sp.QueueInventory(msg.invVect)
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-09-09 17:58:56 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
|
|
|
|
// from the peerHandler goroutine.
|
2013-10-30 18:22:35 +01: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 08:03:20 +02:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
2013-08-06 23:55:22 +02:00
|
|
|
excluded := false
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
if sp == ep {
|
2013-08-06 23:55:22 +02:00
|
|
|
excluded = true
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
// Don't broadcast to still connecting 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 08:03:20 +02:00
|
|
|
if !sp.Connected() {
|
2013-08-01 19:00:14 +02:00
|
|
|
excluded = true
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
if !excluded {
|
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 08:03:20 +02:00
|
|
|
sp.QueueMessage(bmsg.message, nil)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
type getConnCountMsg struct {
|
2014-06-29 23:36:41 +02:00
|
|
|
reply chan int32
|
2013-10-21 19:45:30 +02: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 08:03:20 +02:00
|
|
|
type getPeersMsg struct {
|
|
|
|
reply chan []*serverPeer
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-24 21:10:02 +01: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 08:03:20 +02:00
|
|
|
reply chan []*serverPeer
|
2014-01-24 21:10:02 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp func(*serverPeer) bool
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp func(*serverPeer) bool
|
2015-03-05 22:47:54 +01:00
|
|
|
reply chan error
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01: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 08:03:20 +02:00
|
|
|
func (s *server) handleQuery(state *peerState, querymsg interface{}) {
|
2013-10-21 19:45:30 +02:00
|
|
|
switch msg := querymsg.(type) {
|
|
|
|
case getConnCountMsg:
|
2014-06-29 23:36:41 +02: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 08:03:20 +02:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
if sp.Connected() {
|
2013-10-21 19:45:30 +02:00
|
|
|
nconnected++
|
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-10-21 19:45:30 +02:00
|
|
|
msg.reply <- nconnected
|
2014-01-24 21:10:02 +01: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 08:03:20 +02:00
|
|
|
case getPeersMsg:
|
|
|
|
peers := make([]*serverPeer, 0, state.Count())
|
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
if !sp.Connected() {
|
2013-10-31 18:51:40 +01:00
|
|
|
return
|
2013-10-21 19:45:30 +02: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 08:03:20 +02:00
|
|
|
peers = append(peers, sp)
|
2013-10-31 18:51:40 +01: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 08:03:20 +02:00
|
|
|
msg.reply <- peers
|
2014-01-24 21:10:02 +01:00
|
|
|
|
2015-03-05 22:47:54 +01:00
|
|
|
case connectNodeMsg:
|
2013-10-31 18:51:40 +01:00
|
|
|
// XXX(oga) duplicate oneshots?
|
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 08:03:20 +02:00
|
|
|
for _, peer := range state.persistentPeers {
|
|
|
|
if peer.Addr() == msg.addr {
|
2015-03-05 22:47:54 +01:00
|
|
|
if msg.permanent {
|
2013-10-31 18:51:40 +01:00
|
|
|
msg.reply <- errors.New("peer already connected")
|
2015-03-05 22:47:54 +01:00
|
|
|
} else {
|
|
|
|
msg.reply <- errors.New("peer exists as a permanent peer")
|
2013-10-31 18:51:40 +01:00
|
|
|
}
|
2015-03-05 22:47:54 +01:00
|
|
|
return
|
2013-10-29 18:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 22:47:54 +01:00
|
|
|
|
2013-10-29 18:18:53 +01:00
|
|
|
// TODO(oga) if too many, nuke a non-perm peer.
|
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 08:03:20 +02:00
|
|
|
sp := s.newOutboundPeer(msg.addr, msg.permanent)
|
|
|
|
if sp != nil {
|
|
|
|
go s.peerConnHandler(sp)
|
2013-10-29 18:18:53 +01:00
|
|
|
msg.reply <- nil
|
|
|
|
} else {
|
|
|
|
msg.reply <- errors.New("failed to add peer")
|
|
|
|
}
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
found := disconnectPeer(state.persistentPeers, msg.cmp, func(sp *serverPeer) {
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2015-03-05 22:47:54 +01:00
|
|
|
})
|
2013-10-29 18:18:53 +01:00
|
|
|
|
|
|
|
if found {
|
|
|
|
msg.reply <- nil
|
|
|
|
} else {
|
|
|
|
msg.reply <- errors.New("peer not found")
|
|
|
|
}
|
2014-01-24 21:10:02 +01:00
|
|
|
// Request a list of the persistent (added) peers.
|
|
|
|
case getAddedNodesMsg:
|
|
|
|
// Respond with a slice of the relavent 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 08:03:20 +02:00
|
|
|
peers := make([]*serverPeer, 0, len(state.persistentPeers))
|
|
|
|
for _, sp := range state.persistentPeers {
|
|
|
|
peers = append(peers, sp)
|
2014-01-24 21:10:02 +01:00
|
|
|
}
|
|
|
|
msg.reply <- peers
|
2015-03-05 22:47:54 +01:00
|
|
|
case disconnectNodeMsg:
|
|
|
|
// Check inbound peers. We pass a nil callback since we don't
|
|
|
|
// require any additional actions on disconnect for inbound peers.
|
|
|
|
found := disconnectPeer(state.peers, msg.cmp, nil)
|
|
|
|
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 08:03:20 +02:00
|
|
|
found = disconnectPeer(state.outboundPeers, msg.cmp, func(sp *serverPeer) {
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
found = disconnectPeer(state.outboundPeers, msg.cmp, func(sp *serverPeer) {
|
|
|
|
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
|
2015-03-05 22:47:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
msg.reply <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.reply <- errors.New("peer not found")
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
func disconnectPeer(peerList map[int32]*serverPeer, compareFunc func(*serverPeer) bool, whenFound func(*serverPeer)) bool {
|
|
|
|
for addr, peer := range peerList {
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
delete(peerList, addr)
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
// newPeerConfig returns the configuration for the given serverPeer.
|
|
|
|
func newPeerConfig(sp *serverPeer) *peer.Config {
|
|
|
|
return &peer.Config{
|
|
|
|
Listeners: peer.MessageListeners{
|
|
|
|
OnVersion: sp.OnVersion,
|
|
|
|
OnMemPool: sp.OnMemPool,
|
|
|
|
OnTx: sp.OnTx,
|
|
|
|
OnBlock: sp.OnBlock,
|
|
|
|
OnInv: sp.OnInv,
|
|
|
|
OnHeaders: sp.OnHeaders,
|
|
|
|
OnGetData: sp.OnGetData,
|
|
|
|
OnGetBlocks: sp.OnGetBlocks,
|
|
|
|
OnGetHeaders: sp.OnGetHeaders,
|
|
|
|
OnFilterAdd: sp.OnFilterAdd,
|
|
|
|
OnFilterClear: sp.OnFilterClear,
|
|
|
|
OnFilterLoad: sp.OnFilterLoad,
|
|
|
|
OnGetAddr: sp.OnGetAddr,
|
|
|
|
OnAddr: sp.OnAddr,
|
|
|
|
OnRead: sp.OnRead,
|
|
|
|
OnWrite: sp.OnWrite,
|
2015-11-10 19:20:29 +01:00
|
|
|
|
|
|
|
// Note: The reference client currently bans peers that send alerts
|
|
|
|
// not signed with its key. We could verify against their key, but
|
|
|
|
// since the reference client is currently unwilling to support
|
|
|
|
// other implementions' alert messages, we will not relay theirs.
|
|
|
|
OnAlert: 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 08:03:20 +02:00
|
|
|
},
|
|
|
|
NewestBlock: sp.server.db.NewestSha,
|
|
|
|
BestLocalAddress: sp.server.addrManager.GetBestLocalAddress,
|
|
|
|
HostToNetAddress: sp.server.addrManager.HostToNetAddress,
|
|
|
|
Proxy: cfg.Proxy,
|
|
|
|
UserAgentName: userAgentName,
|
|
|
|
UserAgentVersion: userAgentVersion,
|
|
|
|
ChainParams: sp.server.chainParams,
|
|
|
|
Services: sp.server.services,
|
2015-12-03 16:29:06 +01:00
|
|
|
DisableRelayTx: 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 08:03:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// listenHandler is the main listener which accepts incoming connections for the
|
|
|
|
// server. It must be run as a goroutine.
|
2013-08-07 17:38:39 +02:00
|
|
|
func (s *server) listenHandler(listener net.Listener) {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Server listening on %s", listener.Addr())
|
2013-10-02 02:45:21 +02:00
|
|
|
for atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-08-07 17:38:39 +02:00
|
|
|
conn, err := listener.Accept()
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
// Only log the error if we're not forcibly shutting down.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Errorf("can't accept connection: %v",
|
2013-10-02 23:49:31 +02:00
|
|
|
err)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
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 08:03:20 +02:00
|
|
|
sp := newServerPeer(s, false)
|
|
|
|
sp.Peer = peer.NewInboundPeer(newPeerConfig(sp), conn)
|
|
|
|
sp.Start()
|
|
|
|
go s.peerDoneHandler(sp)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
s.wg.Done()
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Tracef("Listener handler done for %s", listener.Addr())
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:38:45 +02:00
|
|
|
// seedFromDNS uses DNS seeding to populate the address manager with peers.
|
|
|
|
func (s *server) seedFromDNS() {
|
|
|
|
// Nothing to do if DNS seeding is disabled.
|
|
|
|
if cfg.DisableDNSSeed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-09 23:21:16 +01:00
|
|
|
for _, seeder := range activeNetParams.DNSSeeds {
|
2014-07-04 20:04:01 +02:00
|
|
|
go func(seeder string) {
|
2014-07-10 20:52:02 +02:00
|
|
|
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2014-07-04 20:04:01 +02:00
|
|
|
seedpeers, err := dnsDiscover(seeder)
|
|
|
|
if err != nil {
|
|
|
|
discLog.Infof("DNS discovery failed on seed %s: %v", seeder, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
numPeers := len(seedpeers)
|
|
|
|
|
|
|
|
discLog.Infof("%d addresses found from DNS seed %s", numPeers, seeder)
|
2013-10-08 00:38:45 +02:00
|
|
|
|
2014-07-04 20:04:01 +02:00
|
|
|
if numPeers == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-02-05 22:16:39 +01:00
|
|
|
addresses := make([]*wire.NetAddress, len(seedpeers))
|
2014-07-04 20:04:01 +02:00
|
|
|
// if this errors then we have *real* problems
|
|
|
|
intPort, _ := strconv.Atoi(activeNetParams.DefaultPort)
|
|
|
|
for i, peer := range seedpeers {
|
2015-02-05 22:16:39 +01:00
|
|
|
addresses[i] = new(wire.NetAddress)
|
2014-07-04 20:04:01 +02:00
|
|
|
addresses[i].SetAddress(peer, uint16(intPort))
|
|
|
|
// bitcoind seeds with addresses from
|
|
|
|
// a time randomly selected between 3
|
|
|
|
// and 7 days ago.
|
|
|
|
addresses[i].Timestamp = time.Now().Add(-1 *
|
|
|
|
time.Second * time.Duration(secondsIn3Days+
|
2014-07-06 08:04:24 +02:00
|
|
|
randSource.Int31n(secondsIn4Days)))
|
2014-07-04 20:04:01 +02: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(addresses, addresses[0])
|
|
|
|
}(seeder)
|
2013-10-08 00:38:45 +02: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 08:03:20 +02:00
|
|
|
// newOutboundPeer initializes a new outbound peer and setups the message
|
|
|
|
// listeners.
|
|
|
|
func (s *server) newOutboundPeer(addr string, persistent bool) *serverPeer {
|
|
|
|
sp := newServerPeer(s, persistent)
|
|
|
|
p, err := peer.NewOutboundPeer(newPeerConfig(sp), addr)
|
|
|
|
if err != nil {
|
2015-11-22 11:04:22 +01:00
|
|
|
srvrLog.Errorf("Cannot create outbound peer %s: %v", addr, 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 08:03:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sp.Peer = p
|
|
|
|
go s.peerDoneHandler(sp)
|
|
|
|
return sp
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerConnHandler handles peer connections. It must be run in a goroutine.
|
|
|
|
func (s *server) peerConnHandler(sp *serverPeer) {
|
|
|
|
err := s.establishConn(sp)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Debugf("Failed to connect to %s: %v", sp.Addr(), err)
|
2015-12-16 19:19:27 +01:00
|
|
|
sp.Disconnect()
|
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 08:03:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerDoneHandler handles peer disconnects by notifiying the server that it's
|
|
|
|
// done.
|
|
|
|
func (s *server) peerDoneHandler(sp *serverPeer) {
|
|
|
|
sp.WaitForShutdown()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// establishConn establishes a connection to the peer.
|
|
|
|
func (s *server) establishConn(sp *serverPeer) error {
|
|
|
|
srvrLog.Debugf("Attempting to connect to %s", sp.Addr())
|
|
|
|
conn, err := btcdDial("tcp", sp.Addr())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sp.Connect(conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srvrLog.Debugf("Connected to %s", sp.Addr())
|
|
|
|
s.addrManager.Attempt(sp.NA())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-22 11:04:22 +01:00
|
|
|
// retryConn retries connection to the peer after the given duration. It must
|
|
|
|
// be run as a goroutine.
|
|
|
|
func (s *server) retryConn(sp *serverPeer, initialAttempt bool) {
|
|
|
|
retryDuration := connectionRetryInterval
|
|
|
|
for {
|
|
|
|
if initialAttempt {
|
|
|
|
retryDuration = 0
|
|
|
|
initialAttempt = false
|
|
|
|
} else {
|
|
|
|
srvrLog.Debugf("Retrying connection to %s in %s", sp.Addr(),
|
|
|
|
retryDuration)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(retryDuration):
|
|
|
|
err := s.establishConn(sp)
|
|
|
|
if err != nil {
|
|
|
|
retryDuration += connectionRetryInterval
|
|
|
|
if retryDuration > maxConnectionRetryInterval {
|
|
|
|
retryDuration = maxConnectionRetryInterval
|
|
|
|
}
|
|
|
|
continue
|
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 08:03:20 +02:00
|
|
|
}
|
2015-11-22 11:04:22 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-sp.quit:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-s.quit:
|
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 08:03:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02: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 21:19:47 +02:00
|
|
|
// peers. It must be run in a goroutine.
|
2013-08-06 23:55:22 +02:00
|
|
|
func (s *server) peerHandler() {
|
2013-08-09 21:32:22 +02: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 19:03:56 +01: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 08:03:20 +02:00
|
|
|
|
2013-10-30 18:22:35 +01:00
|
|
|
state := &peerState{
|
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 08:03:20 +02:00
|
|
|
pendingPeers: make(map[string]*serverPeer),
|
|
|
|
peers: make(map[int32]*serverPeer),
|
|
|
|
persistentPeers: make(map[int32]*serverPeer),
|
|
|
|
outboundPeers: make(map[int32]*serverPeer),
|
2013-10-31 18:51:40 +01:00
|
|
|
banned: make(map[string]time.Time),
|
2013-10-30 18:22:35 +01:00
|
|
|
maxOutboundPeers: defaultMaxOutbound,
|
2013-10-31 20:53:29 +01:00
|
|
|
outboundGroups: make(map[string]int),
|
2013-10-30 18:22:35 +01:00
|
|
|
}
|
|
|
|
if cfg.MaxPeers < state.maxOutboundPeers {
|
|
|
|
state.maxOutboundPeers = cfg.MaxPeers
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
2013-10-08 00:38:45 +02:00
|
|
|
// Add peers discovered through DNS to the address manager.
|
|
|
|
s.seedFromDNS()
|
2013-08-01 19:00:14 +02:00
|
|
|
|
|
|
|
// Start up persistent peers.
|
|
|
|
permanentPeers := cfg.ConnectPeers
|
|
|
|
if len(permanentPeers) == 0 {
|
|
|
|
permanentPeers = cfg.AddPeers
|
|
|
|
}
|
|
|
|
for _, addr := range permanentPeers {
|
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 08:03:20 +02:00
|
|
|
sp := s.newOutboundPeer(addr, true)
|
|
|
|
if sp != nil {
|
2015-11-22 11:04:22 +01:00
|
|
|
go s.retryConn(sp, 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 08:03:20 +02:00
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing else happens, wake us up soon.
|
2014-07-02 17:31:10 +02:00
|
|
|
time.AfterFunc(10*time.Second, func() { s.wakeup <- struct{}{} })
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-02 02:45:21 +02:00
|
|
|
out:
|
|
|
|
for {
|
2013-08-06 23:55:22 +02:00
|
|
|
select {
|
|
|
|
// New peers connected to the server.
|
|
|
|
case p := <-s.newPeers:
|
2013-10-31 18:51:40 +01:00
|
|
|
s.handleAddPeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// Disconnected peers.
|
|
|
|
case p := <-s.donePeers:
|
2013-10-31 18:51:40 +01:00
|
|
|
s.handleDonePeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2015-02-11 21:39:11 +01:00
|
|
|
// Block accepted in mainchain or orphan, update peer height.
|
|
|
|
case umsg := <-s.peerHeightsUpdate:
|
|
|
|
s.handleUpdatePeerHeights(state, umsg)
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Peer to ban.
|
|
|
|
case p := <-s.banPeers:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleBanPeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-09-09 17:58:56 +02:00
|
|
|
// New inventory to potentially be relayed to other peers.
|
|
|
|
case invMsg := <-s.relayInv:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleRelayInvMsg(state, invMsg)
|
2013-09-09 17:58:56 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Message to broadcast to all connected peers except those
|
|
|
|
// which are excluded by the message.
|
|
|
|
case bmsg := <-s.broadcast:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleBroadcastMsg(state, &bmsg)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-08-01 19:00:14 +02:00
|
|
|
// Used by timers below to wake us back up.
|
|
|
|
case <-s.wakeup:
|
|
|
|
// this page left intentionally blank
|
|
|
|
|
2013-10-21 19:45:30 +02: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 08:03:20 +02:00
|
|
|
s.handleQuery(state, qmsg)
|
2013-10-21 19:45:30 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Shutdown the peer handler.
|
|
|
|
case <-s.quit:
|
|
|
|
// Shutdown 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 08:03:20 +02:00
|
|
|
state.forAllPeers(func(sp *serverPeer) {
|
|
|
|
sp.Shutdown()
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-10-02 02:45:21 +02:00
|
|
|
break out
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2014-05-28 22:19:38 +02:00
|
|
|
// Don't try to connect to more peers when running on the
|
|
|
|
// simulation test network. The simulation network is only
|
|
|
|
// intended to connect to specified peers and actively avoid
|
|
|
|
// advertising and connecting to discovered peers.
|
|
|
|
if cfg.SimNet {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only try connect to more peers if we actually need more.
|
2013-10-31 18:51:40 +01:00
|
|
|
if !state.NeedMoreOutbound() || len(cfg.ConnectPeers) > 0 ||
|
2013-10-02 02:45:21 +02:00
|
|
|
atomic.LoadInt32(&s.shutdown) != 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 08:03:20 +02:00
|
|
|
state.forPendingPeers(func(sp *serverPeer) {
|
|
|
|
sp.Shutdown()
|
|
|
|
})
|
2013-08-01 19:00:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2013-09-17 21:01:22 +02:00
|
|
|
tries := 0
|
2013-10-31 18:51:40 +01:00
|
|
|
for state.NeedMoreOutbound() &&
|
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 08:03:20 +02:00
|
|
|
state.NeedMoreTries() &&
|
2013-10-02 02:45:21 +02:00
|
|
|
atomic.LoadInt32(&s.shutdown) == 0 {
|
2015-04-06 18:13:00 +02:00
|
|
|
addr := s.addrManager.GetAddress("any")
|
2013-09-17 21:01:22 +02:00
|
|
|
if addr == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-07-06 08:04:24 +02:00
|
|
|
key := addrmgr.GroupKey(addr.NetAddress())
|
2013-09-17 21:01:22 +02:00
|
|
|
// 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
|
2014-07-04 20:04:01 +02:00
|
|
|
// others.
|
2013-10-31 20:53:29 +01:00
|
|
|
if state.outboundGroups[key] != 0 {
|
2014-07-04 20:04:01 +02:00
|
|
|
break
|
2013-09-17 21:01:22 +02: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 08:03:20 +02:00
|
|
|
// Check that we don't have a pending connection to this addr.
|
|
|
|
addrStr := addrmgr.NetAddressKey(addr.NetAddress())
|
|
|
|
if _, ok := state.pendingPeers[addrStr]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:01:22 +02:00
|
|
|
tries++
|
|
|
|
// After 100 bad tries exit the loop and we'll try again
|
|
|
|
// later.
|
|
|
|
if tries > 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX if we have limited that address skip
|
|
|
|
|
|
|
|
// only allow recent nodes (10mins) after we failed 30
|
|
|
|
// times
|
2015-05-31 23:47:58 +02:00
|
|
|
if tries < 30 && time.Now().Sub(addr.LastAttempt()) < 10*time.Minute {
|
2013-09-17 21:01:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow nondefault ports after 50 failed tries.
|
2014-07-06 08:04:24 +02:00
|
|
|
if fmt.Sprintf("%d", addr.NetAddress().Port) !=
|
2014-05-29 19:35:09 +02:00
|
|
|
activeNetParams.DefaultPort && tries < 50 {
|
2013-09-17 21:01:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tries = 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 08:03:20 +02:00
|
|
|
sp := s.newOutboundPeer(addrStr, false)
|
|
|
|
if sp != nil {
|
|
|
|
go s.peerConnHandler(sp)
|
|
|
|
state.pendingPeers[sp.Addr()] = sp
|
2013-09-17 21:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2014-07-22 22:58:24 +02:00
|
|
|
// We need more peers, wake up in ten seconds and try again.
|
2013-10-31 18:51:40 +01:00
|
|
|
if state.NeedMoreOutbound() {
|
2013-09-17 21:01:22 +02:00
|
|
|
time.AfterFunc(10*time.Second, func() {
|
2014-07-02 17:31:10 +02:00
|
|
|
s.wakeup <- struct{}{}
|
2013-09-17 21:01:22 +02:00
|
|
|
})
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 02:42:01 +01:00
|
|
|
if cfg.AddrIndex {
|
|
|
|
s.addrIndexer.Stop()
|
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
s.blockManager.Stop()
|
|
|
|
s.addrManager.Stop()
|
2015-12-16 19:19:27 +01: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.wakeup:
|
|
|
|
case <-s.query:
|
|
|
|
default:
|
|
|
|
break cleanup
|
|
|
|
}
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
s.wg.Done()
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Tracef("Peer handler done")
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) AddPeer(sp *serverPeer) {
|
|
|
|
s.newPeers <- sp
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) BanPeer(sp *serverPeer) {
|
|
|
|
s.banPeers <- sp
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 17:58:56 +02:00
|
|
|
// RelayInventory relays the passed inventory to all connected peers that are
|
|
|
|
// not already known to have it.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (s *server) RelayInventory(invVect *wire.InvVect, data interface{}) {
|
2015-01-29 06:22:27 +01:00
|
|
|
s.relayInv <- relayMsg{invVect: invVect, data: data}
|
2013-09-09 17:58:56 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02: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 08:03:20 +02:00
|
|
|
func (s *server) BroadcastMessage(msg wire.Message, exclPeers ...*serverPeer) {
|
2013-08-06 23:55:22 +02: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 21:29:23 +01:00
|
|
|
// ConnectedCount returns the number of currently connected peers.
|
2014-06-29 23:36:41 +02:00
|
|
|
func (s *server) ConnectedCount() int32 {
|
|
|
|
replyChan := make(chan int32)
|
2013-10-21 19:45:30 +02:00
|
|
|
|
|
|
|
s.query <- getConnCountMsg{reply: replyChan}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2014-01-24 21:10:02 +01:00
|
|
|
// AddedNodeInfo returns an array of btcjson.GetAddedNodeInfoResult structures
|
|
|
|
// 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 08:03:20 +02:00
|
|
|
func (s *server) AddedNodeInfo() []*serverPeer {
|
|
|
|
replyChan := make(chan []*serverPeer)
|
2014-01-24 21:10:02 +01: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 08:03:20 +02:00
|
|
|
// Peers returns an array of all connected peers.
|
|
|
|
func (s *server) Peers() []*serverPeer {
|
|
|
|
replyChan := make(chan []*serverPeer)
|
2013-10-21 19:45:30 +02: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 08:03:20 +02:00
|
|
|
s.query <- getPeersMsg{reply: replyChan}
|
2013-10-21 19:45:30 +02:00
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.Addr() == addr },
|
2015-03-05 22:47:54 +01: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 16:51:41 +02:00
|
|
|
func (s *server) DisconnectNodeByID(id int32) error {
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.ID() == id },
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.Addr() == addr },
|
2015-03-05 22:47:54 +01:00
|
|
|
reply: replyChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2015-10-20 16:51:41 +02:00
|
|
|
// RemoveNodeByID removes a peer by node ID from the list of persistent peers
|
2015-03-05 22:47:54 +01:00
|
|
|
// if present. An error will be returned if the peer was not found.
|
2015-10-20 16:51:41 +02:00
|
|
|
func (s *server) RemoveNodeByID(id int32) error {
|
2015-03-05 22:47:54 +01: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 08:03:20 +02:00
|
|
|
cmp: func(sp *serverPeer) bool { return sp.ID() == id },
|
2015-03-05 22:47:54 +01: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 18:18:53 +01:00
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:53:25 +01: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) {
|
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
s.bytesSent += bytesSent
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
s.bytesReceived += bytesReceived
|
|
|
|
}
|
|
|
|
|
2014-02-04 08:26:12 +01:00
|
|
|
// NetTotals returns the sum of all bytes received and sent across the network
|
2014-02-13 16:53:25 +01:00
|
|
|
// for all peers. It is safe for concurrent access.
|
2014-02-05 18:09:45 +01:00
|
|
|
func (s *server) NetTotals() (uint64, uint64) {
|
2014-02-13 16:53:25 +01:00
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
return s.bytesReceived, s.bytesSent
|
2014-02-04 08:26:12 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 21:39:11 +01: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.
|
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 08:03:20 +02:00
|
|
|
func (s *server) UpdatePeerHeights(latestBlkSha *wire.ShaHash, latestHeight int32, updateSource *serverPeer) {
|
2015-02-11 21:39:11 +01:00
|
|
|
s.peerHeightsUpdate <- updatePeerHeightsMsg{
|
|
|
|
newSha: latestBlkSha,
|
|
|
|
newHeight: latestHeight,
|
|
|
|
originPeer: updateSource,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 01:09:35 +01: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 20:40:49 +01:00
|
|
|
func (s *server) rebroadcastHandler() {
|
2014-03-28 20:49:38 +01:00
|
|
|
// Wait 5 min before first tx rebroadcast.
|
2014-03-28 01:09:35 +01:00
|
|
|
timer := time.NewTimer(5 * time.Minute)
|
2015-02-05 22:16:39 +01:00
|
|
|
pendingInvs := make(map[wire.InvVect]interface{})
|
2014-03-18 20:40:49 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2015-11-21 04:12:17 +01:00
|
|
|
case tx := <-s.relayNtfnChan:
|
|
|
|
// Generate an inv and relay it.
|
|
|
|
inv := wire.NewInvVect(wire.InvTypeTx, tx.Sha())
|
|
|
|
s.RelayInventory(inv, tx)
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
case riv := <-s.modifyRebroadcastInv:
|
|
|
|
switch msg := riv.(type) {
|
|
|
|
// Incoming InvVects are added to our map of RPC txs.
|
|
|
|
case broadcastInventoryAdd:
|
2015-01-29 06:22:27 +01:00
|
|
|
pendingInvs[*msg.invVect] = msg.data
|
2014-03-18 20:40:49 +01:00
|
|
|
|
2014-03-28 01:09:35 +01:00
|
|
|
// When an InvVect has been added to a block, we can
|
|
|
|
// now remove it, if it was present.
|
2014-03-18 20:40:49 +01:00
|
|
|
case broadcastInventoryDel:
|
|
|
|
if _, ok := pendingInvs[*msg]; ok {
|
|
|
|
delete(pendingInvs, *msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-timer.C:
|
2014-03-28 01:09:35 +01:00
|
|
|
// Any inventory we have has not made it into a block
|
|
|
|
// yet. We periodically resubmit them until they have.
|
2015-01-29 06:22:27 +01:00
|
|
|
for iv, data := range pendingInvs {
|
2014-03-18 20:40:49 +01:00
|
|
|
ivCopy := iv
|
2015-01-29 06:22:27 +01:00
|
|
|
s.RelayInventory(&ivCopy, data)
|
2014-03-18 20:40:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 01:09:35 +01: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 20:40:49 +01:00
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
|
2014-06-02 23:10:33 +02: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 20:40:49 +01:00
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Start begins accepting connections from peers.
|
|
|
|
func (s *server) Start() {
|
|
|
|
// Already started?
|
2013-10-03 01:33:42 +02:00
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Trace("Starting server")
|
2013-08-09 21:32:22 +02:00
|
|
|
|
|
|
|
// Start all the listeners. There will not be any if listening is
|
|
|
|
// disabled.
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
|
|
|
s.wg.Add(1)
|
2013-09-13 00:24:37 +02:00
|
|
|
go s.listenHandler(listener)
|
2013-08-07 17:38:39 +02:00
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
|
|
|
// Start the peer handler which in turn starts the address and block
|
|
|
|
// managers.
|
2013-08-07 17:38:39 +02:00
|
|
|
s.wg.Add(1)
|
2013-09-13 00:24:37 +02:00
|
|
|
go s.peerHandler()
|
2014-03-18 20:40:49 +01:00
|
|
|
|
2013-12-10 02:31:22 +01:00
|
|
|
if s.nat != nil {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.upnpUpdateThread()
|
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2014-03-18 20:40:49 +01: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 23:55:22 +02:00
|
|
|
s.rpcServer.Start()
|
|
|
|
}
|
2014-06-12 03:09:38 +02:00
|
|
|
|
|
|
|
// Start the CPU miner if generation is enabled.
|
|
|
|
if cfg.Generate {
|
|
|
|
s.cpuMiner.Start()
|
|
|
|
}
|
2015-01-04 02:42:01 +01:00
|
|
|
|
|
|
|
if cfg.AddrIndex {
|
|
|
|
s.addrIndexer.Start()
|
|
|
|
}
|
2013-08-06 23:55:22 +02: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 02:45:21 +02:00
|
|
|
// Make sure this only happens once.
|
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Server is already in the process of shutting down")
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutting down")
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2013-10-02 02:45:21 +02:00
|
|
|
// Stop all the listeners. There will not be any listeners if
|
|
|
|
// listening is disabled.
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2014-06-12 03:09:38 +02:00
|
|
|
// Stop the CPU miner if needed
|
|
|
|
s.cpuMiner.Stop()
|
|
|
|
|
2013-08-09 21:32:22 +02:00
|
|
|
// Shutdown the RPC server if it's not disabled.
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2013-08-09 21:32:22 +02:00
|
|
|
s.rpcServer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal the remaining goroutines to quit.
|
|
|
|
close(s.quit)
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForShutdown blocks until the main listener and peer handlers are stopped.
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleShutdown schedules a server shutdown after the specified duration.
|
|
|
|
// It also dynamically adjusts how often to warn the server is going down based
|
|
|
|
// on remaining duration.
|
|
|
|
func (s *server) ScheduleShutdown(duration time.Duration) {
|
|
|
|
// Don't schedule shutdown more than once.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.AddInt32(&s.shutdownSched, 1) != 1 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutdown in %v", duration)
|
2013-08-06 23:55:22 +02:00
|
|
|
go func() {
|
|
|
|
remaining := duration
|
|
|
|
tickDuration := dynamicTickDuration(remaining)
|
|
|
|
done := time.After(remaining)
|
|
|
|
ticker := time.NewTicker(tickDuration)
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
ticker.Stop()
|
|
|
|
s.Stop()
|
|
|
|
break out
|
|
|
|
case <-ticker.C:
|
|
|
|
remaining = remaining - tickDuration
|
|
|
|
if remaining < time.Second {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change tick duration dynamically based on remaining time.
|
|
|
|
newDuration := dynamicTickDuration(remaining)
|
|
|
|
if tickDuration != newDuration {
|
|
|
|
tickDuration = newDuration
|
|
|
|
ticker.Stop()
|
|
|
|
ticker = time.NewTicker(tickDuration)
|
|
|
|
}
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutdown in %v", remaining)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01: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 04:12:41 +01:00
|
|
|
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
2013-11-14 02:43:36 +01:00
|
|
|
// detects addresses which apply to "all interfaces" and adds the address to
|
|
|
|
// both slices.
|
2013-11-26 01:40:16 +01:00
|
|
|
func parseListeners(addrs []string) ([]string, []string, bool, error) {
|
2013-11-14 03:57:34 +01:00
|
|
|
ipv4ListenAddrs := make([]string, 0, len(addrs)*2)
|
|
|
|
ipv6ListenAddrs := make([]string, 0, len(addrs)*2)
|
2013-11-26 01:40:16 +01:00
|
|
|
haveWildcard := false
|
|
|
|
|
2013-11-14 03:57:34 +01:00
|
|
|
for _, addr := range addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
2013-11-14 02:43:36 +01:00
|
|
|
// Shouldn't happen due to already being normalized.
|
2013-11-26 01:40:16 +01:00
|
|
|
return nil, nil, false, err
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01:00
|
|
|
// Empty host or host of * on plan9 is both IPv4 and IPv6.
|
2013-09-19 17:46:33 +02:00
|
|
|
if host == "" || (host == "*" && runtime.GOOS == "plan9") {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
2013-11-26 01:40:16 +01:00
|
|
|
haveWildcard = true
|
2013-09-19 17:46:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:32:15 +01: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 17:46:33 +02:00
|
|
|
// Parse the IP.
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
2013-11-26 01:40:16 +01:00
|
|
|
return nil, nil, false, fmt.Errorf("'%s' is not a "+
|
|
|
|
"valid IP address", host)
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01:00
|
|
|
// To4 returns nil when the IP is not an IPv4 address, so use
|
|
|
|
// this determine the address type.
|
2013-09-19 17:46:33 +02:00
|
|
|
if ip.To4() == nil {
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
|
|
|
} else {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
return ipv4ListenAddrs, ipv6ListenAddrs, haveWildcard, nil
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-12-10 02:31:22 +01: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 19:35:09 +02:00
|
|
|
lport, _ := strconv.ParseInt(activeNetParams.DefaultPort, 10, 16)
|
2013-12-10 02:31:22 +01:00
|
|
|
first := true
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
// TODO(oga) pick external port more cleverly
|
|
|
|
// TODO(oga) know which ports we are listening to on an external net.
|
|
|
|
// TODO(oga) if specific listen port doesn't work then ask for wildcard
|
|
|
|
// listen port?
|
|
|
|
// XXX this assumes timeout is in seconds.
|
|
|
|
listenPort, err := s.nat.AddPortMapping("tcp", int(lport), int(lport),
|
|
|
|
"btcd listen port", 20*60)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Warnf("can't add UPnP port mapping: %v", err)
|
|
|
|
}
|
|
|
|
if first && err == nil {
|
|
|
|
// TODO(oga): look this up periodically to see if upnp domain changed
|
|
|
|
// 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 22:16:39 +01:00
|
|
|
na := wire.NewNetAddressIPPort(externalip, uint16(listenPort),
|
2015-08-24 17:48:59 +02:00
|
|
|
s.services)
|
2014-07-06 15:47:42 +02:00
|
|
|
err = s.addrManager.AddLocalAddress(na, addrmgr.UpnpPrio)
|
|
|
|
if err != nil {
|
|
|
|
// XXX DeletePortMapping?
|
|
|
|
}
|
2014-07-06 08:04:24 +02:00
|
|
|
srvrLog.Warnf("Successfully bound via UPnP to %s", addrmgr.NetAddressKey(na))
|
2013-12-10 02:31:22 +01:00
|
|
|
first = false
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
srvrLog.Debugf("succesfully disestablished UPnP port mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// newServer returns a new btcd server configured to listen on addr for the
|
2015-02-06 06:18:27 +01:00
|
|
|
// bitcoin network type specified by chainParams. Use start to begin accepting
|
2013-08-06 23:55:22 +02:00
|
|
|
// connections from peers.
|
2015-02-06 06:18:27 +01:00
|
|
|
func newServer(listenAddrs []string, db database.Db, chainParams *chaincfg.Params) (*server, error) {
|
2015-08-24 17:48:59 +02:00
|
|
|
services := defaultServices
|
|
|
|
if cfg.NoPeerBloomFilters {
|
|
|
|
services &^= wire.SFNodeBloom
|
|
|
|
}
|
|
|
|
|
2014-07-06 08:04:24 +02:00
|
|
|
amgr := addrmgr.New(cfg.DataDir, btcdLookup)
|
2013-11-26 01:40:16 +01:00
|
|
|
|
2013-11-14 04:16:49 +01:00
|
|
|
var listeners []net.Listener
|
2013-12-10 02:31:22 +01:00
|
|
|
var nat NAT
|
2013-08-07 19:35:01 +02:00
|
|
|
if !cfg.DisableListen {
|
2013-11-26 01:40:16 +01:00
|
|
|
ipv4Addrs, ipv6Addrs, wildcard, err :=
|
|
|
|
parseListeners(listenAddrs)
|
2013-11-14 04:16:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = make([]net.Listener, 0, len(ipv4Addrs)+len(ipv6Addrs))
|
2013-11-26 20:30:24 +01:00
|
|
|
discover := true
|
|
|
|
if len(cfg.ExternalIPs) != 0 {
|
|
|
|
discover = false
|
2013-12-16 21:48:23 +01:00
|
|
|
// if this fails we have real issues.
|
|
|
|
port, _ := strconv.ParseUint(
|
2014-05-29 19:35:09 +02:00
|
|
|
activeNetParams.DefaultPort, 10, 16)
|
2013-11-26 20:30:24 +01:00
|
|
|
|
|
|
|
for _, sip := range cfg.ExternalIPs {
|
2013-12-16 21:48:23 +01: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 08:04:24 +02:00
|
|
|
na, err := amgr.HostToNetAddress(host, eport,
|
2015-08-24 17:48:59 +02:00
|
|
|
services)
|
2013-11-26 20:30:24 +01:00
|
|
|
if err != nil {
|
2013-12-16 21:48:23 +01:00
|
|
|
srvrLog.Warnf("Not adding %s as "+
|
|
|
|
"externalip: %v", sip, err)
|
2013-11-26 20:30:24 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.ManualPrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Warnf("Skipping specified external IP: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-12-10 02:31:22 +01: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 20:30:24 +01:00
|
|
|
}
|
2013-11-14 04:16:49 +01:00
|
|
|
|
2013-11-26 01:40:16 +01:00
|
|
|
// TODO(oga) nonstandard port...
|
|
|
|
if wildcard {
|
|
|
|
port, err :=
|
2014-05-29 19:35:09 +02:00
|
|
|
strconv.ParseUint(activeNetParams.DefaultPort,
|
2013-11-26 01:40:16 +01:00
|
|
|
10, 16)
|
|
|
|
if err != nil {
|
|
|
|
// I can't think of a cleaner way to do this...
|
|
|
|
goto nowc
|
|
|
|
}
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
for _, a := range addrs {
|
|
|
|
ip, _, err := net.ParseCIDR(a.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-02-05 22:16:39 +01:00
|
|
|
na := wire.NewNetAddressIPPort(ip,
|
2015-08-24 17:48:59 +02:00
|
|
|
uint16(port), services)
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.InterfacePrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Debugf("Skipping local address: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nowc:
|
|
|
|
|
2013-11-14 04:12:41 +01:00
|
|
|
for _, addr := range ipv4Addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
listener, err := net.Listen("tcp4", addr)
|
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 17:46:33 +02:00
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 01:40:16 +01:00
|
|
|
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02: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 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
2013-08-07 19:35:01 +02:00
|
|
|
}
|
2013-08-07 17:38:39 +02:00
|
|
|
|
2013-11-14 04:12:41 +01:00
|
|
|
for _, addr := range ipv6Addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
listener, err := net.Listen("tcp6", addr)
|
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 17:46:33 +02:00
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02: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 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
2013-11-14 04:16:49 +01:00
|
|
|
|
2013-09-19 17:46:33 +02:00
|
|
|
if len(listeners) == 0 {
|
2014-07-02 16:25:42 +02:00
|
|
|
return nil, errors.New("no valid listen address")
|
2013-08-07 19:35:01 +02:00
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := server{
|
2014-03-18 20:40:49 +01:00
|
|
|
listeners: listeners,
|
2015-02-06 06:18:27 +01:00
|
|
|
chainParams: chainParams,
|
2014-03-18 20:40:49 +01:00
|
|
|
addrManager: amgr,
|
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 08:03:20 +02:00
|
|
|
newPeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
donePeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
banPeers: make(chan *serverPeer, cfg.MaxPeers),
|
|
|
|
retryPeers: make(chan *serverPeer, cfg.MaxPeers),
|
2014-07-02 17:31:10 +02:00
|
|
|
wakeup: make(chan struct{}),
|
2014-03-18 20:40:49 +01:00
|
|
|
query: make(chan interface{}),
|
2015-01-29 06:22:27 +01:00
|
|
|
relayInv: make(chan relayMsg, cfg.MaxPeers),
|
2014-03-18 20:40:49 +01:00
|
|
|
broadcast: make(chan broadcastMsg, cfg.MaxPeers),
|
2014-07-02 17:31:10 +02:00
|
|
|
quit: make(chan struct{}),
|
2015-11-21 04:12:17 +01:00
|
|
|
relayNtfnChan: make(chan *btcutil.Tx, cfg.MaxPeers),
|
2014-03-18 20:40:49 +01:00
|
|
|
modifyRebroadcastInv: make(chan interface{}),
|
2015-02-11 21:39:11 +01:00
|
|
|
peerHeightsUpdate: make(chan updatePeerHeightsMsg),
|
2014-03-18 20:40:49 +01:00
|
|
|
nat: nat,
|
|
|
|
db: db,
|
2015-01-30 23:25:42 +01:00
|
|
|
timeSource: blockchain.NewMedianTime(),
|
2015-08-24 17:48:59 +02:00
|
|
|
services: services,
|
2015-09-25 01:22:00 +02:00
|
|
|
sigCache: txscript.NewSigCache(cfg.SigCacheMaxSize),
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-01 00:43:14 +02:00
|
|
|
bm, err := newBlockManager(&s)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-01 00:43:14 +02:00
|
|
|
s.blockManager = bm
|
2015-11-22 08:02:28 +01:00
|
|
|
|
2015-11-21 04:12:17 +01:00
|
|
|
txC := mempoolConfig{
|
|
|
|
DisableRelayPriority: cfg.NoRelayPriority,
|
|
|
|
EnableAddrIndex: cfg.AddrIndex,
|
|
|
|
FetchTransactionStore: s.blockManager.blockChain.FetchTransactionStore,
|
|
|
|
FreeTxRelayLimit: cfg.FreeTxRelayLimit,
|
|
|
|
MaxOrphanTxs: cfg.MaxOrphanTxs,
|
|
|
|
MinRelayTxFee: cfg.minRelayTxFee,
|
|
|
|
NewestSha: s.db.NewestSha,
|
|
|
|
RelayNtfnChan: s.relayNtfnChan,
|
|
|
|
SigCache: s.sigCache,
|
|
|
|
TimeSource: s.timeSource,
|
|
|
|
}
|
|
|
|
s.txMemPool = newTxMemPool(&txC)
|
|
|
|
|
2015-12-08 08:54:49 +01:00
|
|
|
// Create the mining policy based on the configuration options.
|
|
|
|
// NOTE: The CPU miner relies on the mempool, so the mempool has to be
|
|
|
|
// created before calling the function to create the CPU miner.
|
|
|
|
policy := mining.Policy{
|
|
|
|
BlockMinSize: cfg.BlockMinSize,
|
|
|
|
BlockMaxSize: cfg.BlockMaxSize,
|
|
|
|
BlockPrioritySize: cfg.BlockPrioritySize,
|
|
|
|
TxMinFreeFee: cfg.minRelayTxFee,
|
|
|
|
}
|
|
|
|
s.cpuMiner = newCPUMiner(&policy, &s)
|
|
|
|
|
2015-01-04 02:42:01 +01:00
|
|
|
if cfg.AddrIndex {
|
|
|
|
ai, err := newAddrIndexer(&s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.addrIndexer = ai
|
|
|
|
}
|
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2015-11-22 08:02:28 +01:00
|
|
|
s.rpcServer, err = newRPCServer(cfg.RPCListeners, &policy, &s)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 03:09:38 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dynamicTickDuration is a convenience function used to dynamically choose a
|
|
|
|
// tick duration based on remaining time. It is primarily used during
|
|
|
|
// server shutdown to make shutdown warnings more frequent as the shutdown time
|
|
|
|
// approaches.
|
|
|
|
func dynamicTickDuration(remaining time.Duration) time.Duration {
|
|
|
|
switch {
|
|
|
|
case remaining <= time.Second*5:
|
|
|
|
return time.Second
|
|
|
|
case remaining <= time.Second*15:
|
|
|
|
return time.Second * 5
|
|
|
|
case remaining <= time.Minute:
|
|
|
|
return time.Second * 15
|
|
|
|
case remaining <= time.Minute*5:
|
|
|
|
return time.Minute
|
|
|
|
case remaining <= time.Minute*15:
|
|
|
|
return time.Minute * 5
|
|
|
|
case remaining <= time.Hour:
|
|
|
|
return time.Minute * 15
|
|
|
|
}
|
|
|
|
return time.Hour
|
|
|
|
}
|