2016-02-03 15:24:28 +01:00
|
|
|
// Copyright (c) 2015-2016 The btcsuite developers
|
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
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package peer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/peer"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mockRemotePeer creates a basic inbound peer listening on the simnet port for
|
|
|
|
// use with Example_peerConnection. It does not return until the listner is
|
|
|
|
// active.
|
|
|
|
func mockRemotePeer() error {
|
|
|
|
// Configure peer to act as a simnet node that offers no services.
|
|
|
|
peerCfg := &peer.Config{
|
|
|
|
UserAgentName: "peer", // User agent name to advertise.
|
|
|
|
UserAgentVersion: "1.0.0", // User agent version to advertise.
|
|
|
|
ChainParams: &chaincfg.SimNetParams,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept connections on the simnet port.
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:18555")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Accept: error %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and start the inbound peer.
|
2016-02-03 15:24:28 +01:00
|
|
|
p := peer.NewInboundPeer(peerCfg)
|
|
|
|
if err := p.Connect(conn); err != nil {
|
|
|
|
fmt.Printf("Connect: error %v\n", 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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This example demonstrates the basic process for initializing and creating an
|
|
|
|
// outbound peer. Peers negotiate by exchanging version and verack messages.
|
|
|
|
// For demonstration, a simple handler for version message is attached to the
|
|
|
|
// peer.
|
|
|
|
func Example_newOutboundPeer() {
|
|
|
|
// Ordinarily this will not be needed since the outbound peer will be
|
|
|
|
// connecting to a remote peer, however, since this example is executed
|
|
|
|
// and tested, a mock remote peer is needed to listen for the outbound
|
|
|
|
// peer.
|
|
|
|
if err := mockRemotePeer(); err != nil {
|
|
|
|
fmt.Printf("mockRemotePeer: unexpected error %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an outbound peer that is configured to act as a simnet node
|
|
|
|
// that offers no services and has listeners for the version and verack
|
|
|
|
// messages. The verack listener is used here to signal the code below
|
|
|
|
// when the handshake has been finished by signalling a channel.
|
|
|
|
verack := make(chan struct{})
|
|
|
|
peerCfg := &peer.Config{
|
|
|
|
UserAgentName: "peer", // User agent name to advertise.
|
|
|
|
UserAgentVersion: "1.0.0", // User agent version to advertise.
|
|
|
|
ChainParams: &chaincfg.SimNetParams,
|
|
|
|
Services: 0,
|
|
|
|
Listeners: peer.MessageListeners{
|
|
|
|
OnVersion: func(p *peer.Peer, msg *wire.MsgVersion) {
|
|
|
|
fmt.Println("outbound: received version")
|
|
|
|
},
|
|
|
|
OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
|
|
|
|
verack <- struct{}{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p, err := peer.NewOutboundPeer(peerCfg, "127.0.0.1:18555")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("NewOutboundPeer: error %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Establish the connection to the peer address and mark it connected.
|
|
|
|
conn, err := net.Dial("tcp", p.Addr())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("net.Dial: error %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.Connect(conn); err != nil {
|
|
|
|
fmt.Printf("Connect: error %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the verack message or timeout in case of failure.
|
|
|
|
select {
|
|
|
|
case <-verack:
|
|
|
|
case <-time.After(time.Second * 1):
|
|
|
|
fmt.Printf("Example_peerConnection: verack timeout")
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:24:28 +01:00
|
|
|
// Disconnect the peer.
|
|
|
|
p.Disconnect()
|
|
|
|
p.WaitForDisconnect()
|
peer: Refactor peer code into its own package.
This commit introduces package peer which contains peer related features
refactored from peer.go.
The following is an overview of the features the package provides:
- Provides a basic concurrent safe bitcoin peer for handling bitcoin
communications via the peer-to-peer protocol
- Full duplex reading and writing of bitcoin protocol messages
- Automatic handling of the initial handshake process including protocol
version negotiation
- Automatic periodic keep-alive pinging and pong responses
- Asynchronous message queueing of outbound messages with optional
channel for notification when the message is actually sent
- Inventory message batching and send trickling with known inventory
detection and avoidance
- Ability to wait for shutdown/disconnect
- Flexible peer configuration
- Caller is responsible for creating outgoing connections and listening
for incoming connections so they have flexibility to establish
connections as they see fit (proxies, etc.)
- User agent name and version
- Bitcoin network
- Service support signalling (full nodes, bloom filters, etc.)
- Maximum supported protocol version
- Ability to register callbacks for handling bitcoin protocol messages
- Proper handling of bloom filter related commands when the caller does
not specify the related flag to signal support
- Disconnects the peer when the protocol version is high enough
- Does not invoke the related callbacks for older protocol versions
- Snapshottable peer statistics such as the total number of bytes read
and written, the remote address, user agent, and negotiated protocol
version
- Helper functions for pushing addresses, getblocks, getheaders, and
reject messages
- These could all be sent manually via the standard message output
function, but the helpers provide additional nice functionality such
as duplicate filtering and address randomization
- Full documentation with example usage
- Test coverage
In addition to the addition of the new package, btcd has been refactored
to make use of the new package by extending the basic peer it provides to
work with the blockmanager and server to act as a full node. The
following is a broad overview of the changes to integrate the package:
- The server is responsible for all connection management including
persistent peers and banning
- Callbacks for all messages that are required to implement a full node
are registered
- Logic necessary to serve data and behave as a full node is now in the
callback registered with the peer
Finally, the following peer-related things have been improved as a part
of this refactor:
- Don't log or send reject message due to peer disconnects
- Remove trace logs that aren't particularly helpful
- Finish an old TODO to switch the queue WaitGroup over to a channel
- Improve various comments and fix some code consistency cases
- Improve a few logging bits
- Implement a most-recently-used nonce tracking for detecting self
connections and generate a unique nonce for each peer
2015-10-02 08:03:20 +02:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// outbound: received version
|
|
|
|
}
|