2015-08-29 22:35:28 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2014-01-01 17:16:15 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
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
|
|
|
package peer
|
2013-10-26 08:57:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2015-08-29 22:35:28 +02:00
|
|
|
"fmt"
|
2013-10-26 08:57:00 +02:00
|
|
|
"testing"
|
2014-07-02 15:50:08 +02:00
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-10-26 08:57:00 +02:00
|
|
|
)
|
|
|
|
|
2015-08-29 22:35:28 +02:00
|
|
|
// TestMruInventoryMap ensures the MruInventoryMap behaves as expected including
|
|
|
|
// limiting, eviction of least-recently used entries, specific entry removal,
|
|
|
|
// and existence tests.
|
|
|
|
func TestMruInventoryMap(t *testing.T) {
|
|
|
|
// Create a bunch of fake inventory vectors to use in testing the mru
|
|
|
|
// inventory code.
|
|
|
|
numInvVects := 10
|
|
|
|
invVects := make([]*wire.InvVect, 0, numInvVects)
|
|
|
|
for i := 0; i < numInvVects; i++ {
|
|
|
|
hash := &wire.ShaHash{byte(i)}
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
|
|
|
|
invVects = append(invVects, iv)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
limit int
|
|
|
|
}{
|
|
|
|
{name: "limit 0", limit: 0},
|
|
|
|
{name: "limit 1", limit: 1},
|
|
|
|
{name: "limit 5", limit: 5},
|
|
|
|
{name: "limit 7", limit: 7},
|
|
|
|
{name: "limit one less than available", limit: numInvVects - 1},
|
|
|
|
{name: "limit all available", limit: numInvVects},
|
|
|
|
}
|
|
|
|
|
|
|
|
testLoop:
|
|
|
|
for i, test := range tests {
|
|
|
|
// Create a new mru inventory map limited by the specified test
|
|
|
|
// limit and add all of the test inventory vectors. This will
|
|
|
|
// cause evicition since there are more test inventory vectors
|
|
|
|
// than the limits.
|
2015-11-21 04:17:41 +01:00
|
|
|
mruInvMap := newMruInventoryMap(uint(test.limit))
|
2015-08-29 22:35:28 +02:00
|
|
|
for j := 0; j < numInvVects; j++ {
|
|
|
|
mruInvMap.Add(invVects[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the limited number of most recent entries in the
|
|
|
|
// inventory vector list exist.
|
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 j := numInvVects - test.limit; j < numInvVects; j++ {
|
2015-08-29 22:35:28 +02:00
|
|
|
if !mruInvMap.Exists(invVects[j]) {
|
|
|
|
t.Errorf("Exists #%d (%s) entry %s does not "+
|
|
|
|
"exist", i, test.name, *invVects[j])
|
|
|
|
continue testLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the entries before the limited number of most recent
|
|
|
|
// entries in the inventory vector list do not exist.
|
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 j := 0; j < numInvVects-test.limit; j++ {
|
2015-08-29 22:35:28 +02:00
|
|
|
if mruInvMap.Exists(invVects[j]) {
|
|
|
|
t.Errorf("Exists #%d (%s) entry %s exists", i,
|
|
|
|
test.name, *invVects[j])
|
|
|
|
continue testLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readd the entry that should currently be the least-recently
|
|
|
|
// used entry so it becomes the most-recently used entry, then
|
|
|
|
// force an eviction by adding an entry that doesn't exist and
|
|
|
|
// ensure the evicted entry is the new least-recently used
|
|
|
|
// entry.
|
|
|
|
//
|
|
|
|
// This check needs at least 2 entries.
|
|
|
|
if test.limit > 1 {
|
|
|
|
origLruIndex := numInvVects - test.limit
|
|
|
|
mruInvMap.Add(invVects[origLruIndex])
|
|
|
|
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock,
|
|
|
|
&wire.ShaHash{0x00, 0x01})
|
|
|
|
mruInvMap.Add(iv)
|
|
|
|
|
|
|
|
// Ensure the original lru entry still exists since it
|
|
|
|
// was updated and should've have become the mru entry.
|
|
|
|
if !mruInvMap.Exists(invVects[origLruIndex]) {
|
|
|
|
t.Errorf("MRU #%d (%s) entry %s does not exist",
|
|
|
|
i, test.name, *invVects[origLruIndex])
|
|
|
|
continue testLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the entry that should've become the new lru
|
|
|
|
// entry was evicted.
|
|
|
|
newLruIndex := origLruIndex + 1
|
|
|
|
if mruInvMap.Exists(invVects[newLruIndex]) {
|
|
|
|
t.Errorf("MRU #%d (%s) entry %s exists", i,
|
|
|
|
test.name, *invVects[newLruIndex])
|
|
|
|
continue testLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all of the entries in the inventory vector list,
|
|
|
|
// including those that don't exist in the map, and ensure they
|
|
|
|
// no longer exist.
|
|
|
|
for j := 0; j < numInvVects; j++ {
|
|
|
|
mruInvMap.Delete(invVects[j])
|
|
|
|
if mruInvMap.Exists(invVects[j]) {
|
|
|
|
t.Errorf("Delete #%d (%s) entry %s exists", i,
|
|
|
|
test.name, *invVects[j])
|
|
|
|
continue testLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMruInventoryMapStringer tests the stringized output for the
|
|
|
|
// MruInventoryMap type.
|
|
|
|
func TestMruInventoryMapStringer(t *testing.T) {
|
|
|
|
// Create a couple of fake inventory vectors to use in testing the mru
|
|
|
|
// inventory stringer code.
|
|
|
|
hash1 := &wire.ShaHash{0x01}
|
|
|
|
hash2 := &wire.ShaHash{0x02}
|
|
|
|
iv1 := wire.NewInvVect(wire.InvTypeBlock, hash1)
|
|
|
|
iv2 := wire.NewInvVect(wire.InvTypeBlock, hash2)
|
|
|
|
|
|
|
|
// Create new mru inventory map and add the inventory vectors.
|
2015-11-21 04:17:41 +01:00
|
|
|
mruInvMap := newMruInventoryMap(uint(2))
|
2015-08-29 22:35:28 +02:00
|
|
|
mruInvMap.Add(iv1)
|
|
|
|
mruInvMap.Add(iv2)
|
|
|
|
|
|
|
|
// Ensure the stringer gives the expected result. Since map iteration
|
|
|
|
// is not ordered, either entry could be first, so account for both
|
|
|
|
// cases.
|
|
|
|
wantStr1 := fmt.Sprintf("<%d>[%s, %s]", 2, *iv1, *iv2)
|
|
|
|
wantStr2 := fmt.Sprintf("<%d>[%s, %s]", 2, *iv2, *iv1)
|
|
|
|
gotStr := mruInvMap.String()
|
|
|
|
if gotStr != wantStr1 && gotStr != wantStr2 {
|
|
|
|
t.Fatalf("unexpected string representation - got %q, want %q "+
|
|
|
|
"or %q", gotStr, wantStr1, wantStr2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 08:57:00 +02:00
|
|
|
// BenchmarkMruInventoryList performs basic benchmarks on the most recently
|
|
|
|
// used inventory handling.
|
|
|
|
func BenchmarkMruInventoryList(b *testing.B) {
|
|
|
|
// Create a bunch of fake inventory vectors to use in benchmarking
|
|
|
|
// the mru inventory code.
|
|
|
|
b.StopTimer()
|
|
|
|
numInvVects := 100000
|
2015-02-05 22:16:39 +01:00
|
|
|
invVects := make([]*wire.InvVect, 0, numInvVects)
|
2013-10-26 08:57:00 +02:00
|
|
|
for i := 0; i < numInvVects; i++ {
|
2015-02-05 22:16:39 +01:00
|
|
|
hashBytes := make([]byte, wire.HashSize)
|
2013-10-26 08:57:00 +02:00
|
|
|
rand.Read(hashBytes)
|
2015-02-05 22:16:39 +01:00
|
|
|
hash, _ := wire.NewShaHash(hashBytes)
|
|
|
|
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
|
2013-10-26 08:57:00 +02:00
|
|
|
invVects = append(invVects, iv)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
// Benchmark the add plus evicition code.
|
|
|
|
limit := 20000
|
2015-11-21 04:17:41 +01:00
|
|
|
mruInvMap := newMruInventoryMap(uint(limit))
|
2013-10-26 08:57:00 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mruInvMap.Add(invVects[i%numInvVects])
|
|
|
|
}
|
|
|
|
}
|