From 1cf7e233e5b5ff9fe185e4ea9549051908a8e3db Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Thu, 24 Aug 2017 13:33:51 -0700 Subject: [PATCH] netsync: Initialize netsync package. Create doc.go, interface.go, and README for new package. --- netsync/README.md | 25 +++++++++++ netsync/blocklogger.go | 6 ++- netsync/doc.go | 13 ++++++ netsync/interface.go | 94 ++++++++++++++++++++++++++++++++++++++++++ netsync/manager.go | 26 +----------- 5 files changed, 138 insertions(+), 26 deletions(-) create mode 100644 netsync/README.md create mode 100644 netsync/doc.go create mode 100644 netsync/interface.go diff --git a/netsync/README.md b/netsync/README.md new file mode 100644 index 00000000..4734d331 --- /dev/null +++ b/netsync/README.md @@ -0,0 +1,25 @@ +netsync +======= + +[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd) +[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) +[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/netsync) + +## Overview + +This package implements a concurrency safe block syncing protocol. The provided +implementation of SyncManager communicates with connected peers to perform an +initial block download, keep the chain in sync, and announce new blocks +connected to the chain. Currently the sync manager selects a single sync peer +that it downloads all blocks from until it is up to date with the longest chain +the sync peer is aware of. + +## Installation and Updating + +```bash +$ go get -u github.com/btcsuite/btcd/netsync +``` + +## License + +Package netsync is licensed under the [copyfree](http://copyfree.org) ISC License. diff --git a/netsync/blocklogger.go b/netsync/blocklogger.go index 1901a708..18480e78 100644 --- a/netsync/blocklogger.go +++ b/netsync/blocklogger.go @@ -1,4 +1,8 @@ -package main +// Copyright (c) 2015-2017 The btcsuite developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package netsync import ( "sync" diff --git a/netsync/doc.go b/netsync/doc.go new file mode 100644 index 00000000..8fd29aad --- /dev/null +++ b/netsync/doc.go @@ -0,0 +1,13 @@ +// Copyright (c) 2017 The btcsuite developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +/* +The netsync package implements a concurrency safe block syncing protocol. The +provided implementation of SyncManager communicates with connected peers to +perform an initial block download, keep the chain and mempool in sync, and +announce new blocks connected to the chain. Currently the block manager selects +a single sync peer that it downloads all blocks from until it is up to date with +the longest chain the sync peer is aware of. +*/ +package netsync diff --git a/netsync/interface.go b/netsync/interface.go new file mode 100644 index 00000000..69cd2421 --- /dev/null +++ b/netsync/interface.go @@ -0,0 +1,94 @@ +// Copyright (c) 2017 The btcsuite developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package netsync + +import ( + "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/mempool" + "github.com/btcsuite/btcd/peer" + "github.com/btcsuite/btcd/wire" + "github.com/btcsuite/btcutil" +) + +// PeerNotifier exposes methods to notify peers of status changes to +// transactions, blocks, etc. Currently server (in the main package) implements +// this interface. +type PeerNotifier interface { + AnnounceNewTransactions(newTxs []*mempool.TxDesc) + + UpdatePeerHeights(latestBlkHash *chainhash.Hash, latestHeight int32, updateSource *peer.Peer) + + RelayInventory(invVect *wire.InvVect, data interface{}) + + TransactionConfirmed(tx *btcutil.Tx) +} + +// Config is a configuration struct used to initialize a new SyncManager. +type Config struct { + PeerNotifier PeerNotifier + Chain *blockchain.BlockChain + TxMemPool *mempool.TxPool + ChainParams *chaincfg.Params + + DisableCheckpoints bool + MaxPeers int +} + +// SyncManager is the interface used to communicate block related messages with +// peers. The SyncManager is started as by executing Start() in a goroutine. +// Once started, it selects peers to sync from and starts the initial block +// download. Once the chain is in sync, the SyncManager handles incoming block +// and header notifications and relays announcements of new blocks to peers. +type SyncManager interface { + // NewPeer informs the SyncManager of a newly active peer. + NewPeer(p *peer.Peer) + + // QueueTx adds the passed transaction message and peer to the block + // handling queue. + QueueTx(tx *btcutil.Tx, p *peer.Peer, done chan struct{}) + + // QueueBlock adds the passed block message and peer to the block handling + // queue. + QueueBlock(block *btcutil.Block, p *peer.Peer, done chan struct{}) + + // QueueInv adds the passed inv message and peer to the block handling + // queue. + QueueInv(inv *wire.MsgInv, p *peer.Peer) + + // QueueHeaders adds the passed headers message and peer to the block + // handling queue. + QueueHeaders(headers *wire.MsgHeaders, p *peer.Peer) + + // DonePeer informs the SyncManager that a peer has disconnected. + DonePeer(p *peer.Peer) + + // Start begins the core block handler which processes block and inv + // messages. + Start() + + // Stop gracefully shuts down the SyncManager by stopping all asynchronous + // handlers and waiting for them to finish. + Stop() error + + // SyncPeerID returns the ID of the current sync peer, or 0 if there is + // none. + SyncPeerID() int32 + + // ProcessBlock makes use of ProcessBlock on an internal instance of a block + // chain. + ProcessBlock(block *btcutil.Block, flags blockchain.BehaviorFlags) (bool, error) + + // IsCurrent returns whether or not the SyncManager believes it is synced + // with the connected peers. + IsCurrent() bool + + // Pause pauses the SyncManager until the returned channel is closed. + // + // Note that while paused, all peer and block processing is halted. The + // message sender should avoid pausing the SyncManager for long durations. + Pause() chan<- struct{} +} diff --git a/netsync/manager.go b/netsync/manager.go index 93344ad8..09a37c3c 100644 --- a/netsync/manager.go +++ b/netsync/manager.go @@ -2,7 +2,7 @@ // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. -package main +package netsync import ( "container/list" @@ -134,30 +134,6 @@ type headerNode struct { hash *chainhash.Hash } -// PeerNotifier exposes methods to notify peers of status changes to -// transactions, blocks, etc. Currently server implements this interface. -type PeerNotifier interface { - AnnounceNewTransactions(newTxs []*mempool.TxDesc) - - UpdatePeerHeights(latestBlkHash *chainhash.Hash, latestHeight int32, updateSource *peerpkg.Peer) - - RelayInventory(invVect *wire.InvVect, data interface{}) - - TransactionConfirmed(tx *btcutil.Tx) -} - -// blockManangerConfig is a configuration struct used to initialize a new -// blockManager. -type blockManagerConfig struct { - PeerNotifier PeerNotifier - Chain *blockchain.BlockChain - TxMemPool *mempool.TxPool - ChainParams *chaincfg.Params - - DisableCheckpoints bool - MaxPeers int -} - // peerSyncState stores additional information that the blockManager tracks // about a peer. type peerSyncState struct {