mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
netsync: Initialize netsync package.
Create doc.go, interface.go, and README for new package.
This commit is contained in:
parent
3135a40371
commit
1cf7e233e5
5 changed files with 138 additions and 26 deletions
25
netsync/README.md
Normal file
25
netsync/README.md
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
netsync
|
||||||
|
=======
|
||||||
|
|
||||||
|
[](https://travis-ci.org/btcsuite/btcd)
|
||||||
|
[](http://copyfree.org)
|
||||||
|
[](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.
|
|
@ -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 (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
13
netsync/doc.go
Normal file
13
netsync/doc.go
Normal 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
94
netsync/interface.go
Normal 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{}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by an ISC
|
// Use of this source code is governed by an ISC
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package main
|
package netsync
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
|
@ -134,30 +134,6 @@ type headerNode struct {
|
||||||
hash *chainhash.Hash
|
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
|
// peerSyncState stores additional information that the blockManager tracks
|
||||||
// about a peer.
|
// about a peer.
|
||||||
type peerSyncState struct {
|
type peerSyncState struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue