Skip to content

Commit

Permalink
Add package peer
Browse files Browse the repository at this point in the history
This commit introduces package peer which contains peer related features
extracted from peer.go. It allows registration of custom message
listeners to implement full nodes, SPV nodes, etc. Basic handshake
including protocol negotiation is included by default.

Refactor to integrate package peer

This commit integrates the new package peer. It involves extending the
basic peer provided by the package to work with the blockmanager and
server to act as a full node.

Address review items:

* Rebase btcsuite#499
* Fix tests by removing hardcoded useragent, protocol version

Moved flag fields from stats to Peer

Address review items:

* Remove defer in simple case
* Start err text with lower case
* Fixed data race in TestOutboundPeer
* Declare isValidBIP0111 before use

Remove addrMgr in example_test.go

Address review items:

* Use services flag 0 in example
* Remove defer in Add/Remove listeners
* Remove todo for alerts

Refactor to replace add/remove listeners with funcs

Use bytes.Buffer writer in log tests.

This prevents undesired log output to stdout while running the tests.

Move a couple of funcs for consistent ordering.

Use 0 for services in tests since not full nodes.

peer: generate and use unique nonce at package level.

This changes the logic around the nonce to make it handled by the
package rather than the caller.  This makes sense because the real
access pattern is not the same as what the tests need to do and it
simplifies the logic for the caller.

In particular, all peers that are created by this package, whether they
are inbound or outbound need to have the same "factory" nonce under any
realistic scenario outside of tests.  This is because the entire point
of the nonce is to be used to detect self connections.

This change also means the example had to be changed because it wasn't
really representative of the proper way a caller will interact with this
package due to requiring different nonces.  As a result, the example was
simplified to only showcase an outbound peer.

Don't log or send reject message due to disconnect.

Remove trace logs that aren't particularly helpful.

Allow the peer.Config.NewestBlock function to be nil.

lookupFunc not used in example_test.go - move it

Finish TODO to switch queueWg to queueQuit chan.

Remove duplicate negotiate print.

peer: Unique nonce per peer.

Rather than only generating a single nonce at init time, generate a
unique nonce for each pushed version message and keep track of them in a
limited and concurrent safe most-recently-used nonce map so the self
connection detection still works.

Remove unused mruinvmap bits from btcd main dir.

Access to fields in trace stmt need mutex.

Move comment about peer addr inside function.

Callers don't need to know about this.

Don't convert fields in state snapshot in peer.

The caller might want to do things with this information and
pre-formatting it for the RPC server would prevent that.  Instead,
return the fields using their native types in the snapshot and then
convert them in the RPC server as needed.

Explicitly comment concurrent safe funcs.

Also use a style consistent with the rest of the code base.

Add some more details in a few comments.

Make peer.Config.Listeners concrete instead of pointer.

This removes the need for checking against nil everywhere and still
allows is not to be specified since the zero value will contain all nil
pointers for the callbacks.

Rename listeners and remove On{Read,Write}.

Bring back OnRead/OnWrite with better comments.

Use new listener names in example.

Update server for new listeners

Fix build issues with go vet

Take chain params in config instead of net + regtest flag.

Set to testnet by default if not specified.

Make copy of peer config so caller can't modify after.

Use  global request map to avoid duplicate getdata

Address review items:

* Re-order peer entry in docs
* Move PingTime in assignment
* Cleanup peer tests

Restore TestPeerConnection

Added peer listener tests

Reuse peer config

Restore done peers

Make the Config.ProtocolVersion comments more explicit.

Consistent case in trace statement.

Improve comments on peer.Config fields.

Also reorder them to more naturally group their function.

Add handler for MsgMerkleBlock.

Various cleanup.

- Improve and correct some comments
- Correct a log print
- Add log print for disconnect due to not receiving initial version msg
- Code consistency with spacing and comments
  • Loading branch information
tuxcanfly authored and davecgh committed Oct 2, 2015
1 parent c9ee3d9 commit fcba090
Show file tree
Hide file tree
Showing 19 changed files with 4,552 additions and 2,318 deletions.
293 changes: 209 additions & 84 deletions blockmanager.go

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ var (
// to parse and execute service commands specified via the -s flag.
var runServiceCommand func(string) error

// minUint32 is a helper function to return the minimum of two uint32s.
// This avoids a math import and the need to cast to floats.
func minUint32(a, b uint32) uint32 {
if a < b {
return a
}
return b
}

// config defines the configuration options for btcd.
//
// See loadConfig for details on the configuration load process.
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ information.
for the underlying JSON-RPC command and return values
* [wire](https://github.com/btcsuite/btcd/tree/master/wire) - Implements the
Bitcoin wire protocol
* [peer](https://github.com/btcsuite/btcd/tree/master/peer) -
Provides a common base for creating and managing Bitcoin network peers.
* [blockchain](https://github.com/btcsuite/btcd/tree/master/blockchain) -
Implements Bitcoin block handling and chain selection rules
* [txscript](https://github.com/btcsuite/btcd/tree/master/txscript) -
Expand Down
3 changes: 2 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"time"

"github.com/btcsuite/btcd/addrmgr"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/database"
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog"
Expand Down Expand Up @@ -118,6 +118,7 @@ func useLogger(subsystemID string, logger btclog.Logger) {

case "PEER":
peerLog = logger
peer.UseLogger(logger)

case "RPCS":
rpcsLog = logger
Expand Down
Loading

0 comments on commit fcba090

Please sign in to comment.