netsync: Initialize netsync package.

Create doc.go, interface.go, and README for new package.
This commit is contained in:
Jim Posen 2017-08-24 13:33:51 -07:00 committed by Dave Collins
parent 3135a40371
commit 1cf7e233e5
5 changed files with 138 additions and 26 deletions

25
netsync/README.md Normal file
View file

@ -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.

View file

@ -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"

13
netsync/doc.go Normal file
View file

@ -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

94
netsync/interface.go Normal file
View file

@ -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{}
}

View file

@ -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 {