2014-01-01 17:16:15 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-08-06 23:55:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2014-03-18 20:40:49 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2013-10-29 18:18:53 +01:00
|
|
|
"errors"
|
2013-09-17 21:01:22 +02:00
|
|
|
"fmt"
|
2014-03-18 20:40:49 +01:00
|
|
|
"math"
|
2014-07-06 08:04:24 +02:00
|
|
|
mrand "math/rand"
|
2013-08-06 23:55:22 +02:00
|
|
|
"net"
|
2013-09-19 17:46:33 +02:00
|
|
|
"runtime"
|
2013-08-01 19:00:14 +02:00
|
|
|
"strconv"
|
2013-08-06 23:55:22 +02:00
|
|
|
"sync"
|
2013-10-02 02:45:21 +02:00
|
|
|
"sync/atomic"
|
2013-08-06 23:55:22 +02:00
|
|
|
"time"
|
2014-07-02 15:50:08 +02:00
|
|
|
|
2014-07-06 08:04:24 +02:00
|
|
|
"github.com/conformal/btcd/addrmgr"
|
2014-07-02 15:50:08 +02:00
|
|
|
"github.com/conformal/btcdb"
|
|
|
|
"github.com/conformal/btcjson"
|
|
|
|
"github.com/conformal/btcnet"
|
|
|
|
"github.com/conformal/btcwire"
|
2013-08-06 23:55:22 +02:00
|
|
|
)
|
|
|
|
|
2013-10-08 00:57:43 +02:00
|
|
|
const (
|
2014-03-18 20:40:49 +01:00
|
|
|
// These constants are used by the DNS seed code to pick a random last seen
|
|
|
|
// time.
|
2013-10-08 00:57:43 +02:00
|
|
|
secondsIn3Days int32 = 24 * 60 * 60 * 3
|
|
|
|
secondsIn4Days int32 = 24 * 60 * 60 * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// supportedServices describes which services are supported by the
|
|
|
|
// server.
|
|
|
|
supportedServices = btcwire.SFNodeNetwork
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-08 00:57:43 +02:00
|
|
|
// connectionRetryInterval is the amount of time to wait in between
|
|
|
|
// retries when connecting to persistent peers.
|
|
|
|
connectionRetryInterval = time.Second * 10
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-08 00:57:43 +02:00
|
|
|
// defaultMaxOutbound is the default number of max outbound peers.
|
|
|
|
defaultMaxOutbound = 8
|
|
|
|
)
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// broadcastMsg provides the ability to house a bitcoin message to be broadcast
|
|
|
|
// to all connected peers except specified excluded peers.
|
|
|
|
type broadcastMsg struct {
|
|
|
|
message btcwire.Message
|
|
|
|
excludePeers []*peer
|
|
|
|
}
|
|
|
|
|
2014-03-28 01:03:45 +01:00
|
|
|
// broadcastInventoryAdd is a type used to declare that the InvVect it contains
|
2014-03-18 20:40:49 +01:00
|
|
|
// needs to be added to the rebroadcast map
|
|
|
|
type broadcastInventoryAdd *btcwire.InvVect
|
|
|
|
|
2014-03-28 01:03:45 +01:00
|
|
|
// broadcastInventoryDel is a type used to declare that the InvVect it contains
|
2014-03-18 20:40:49 +01:00
|
|
|
// needs to be removed from the rebroadcast map
|
|
|
|
type broadcastInventoryDel *btcwire.InvVect
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// server provides a bitcoin server for handling communications to and from
|
|
|
|
// bitcoin peers.
|
|
|
|
type server struct {
|
2014-03-18 20:40:49 +01:00
|
|
|
nonce uint64
|
|
|
|
listeners []net.Listener
|
2014-05-27 06:20:20 +02:00
|
|
|
netParams *btcnet.Params
|
2014-03-18 20:40:49 +01:00
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
shutdownSched int32 // atomic
|
|
|
|
bytesMutex sync.Mutex // For the following two fields.
|
|
|
|
bytesReceived uint64 // Total bytes received from all peers since start.
|
|
|
|
bytesSent uint64 // Total bytes sent by all peers since start.
|
2014-07-06 08:04:24 +02:00
|
|
|
addrManager *addrmgr.AddrManager
|
2014-03-18 20:40:49 +01:00
|
|
|
rpcServer *rpcServer
|
|
|
|
blockManager *blockManager
|
|
|
|
txMemPool *txMemPool
|
2014-06-12 03:09:38 +02:00
|
|
|
cpuMiner *CPUMiner
|
2014-03-18 20:40:49 +01:00
|
|
|
modifyRebroadcastInv chan interface{}
|
|
|
|
newPeers chan *peer
|
|
|
|
donePeers chan *peer
|
|
|
|
banPeers chan *peer
|
2014-07-02 17:31:10 +02:00
|
|
|
wakeup chan struct{}
|
2014-03-18 20:40:49 +01:00
|
|
|
query chan interface{}
|
|
|
|
relayInv chan *btcwire.InvVect
|
|
|
|
broadcast chan broadcastMsg
|
|
|
|
wg sync.WaitGroup
|
2014-07-02 17:31:10 +02:00
|
|
|
quit chan struct{}
|
2014-03-18 20:40:49 +01:00
|
|
|
nat NAT
|
|
|
|
db btcdb.Db
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 18:22:35 +01:00
|
|
|
type peerState struct {
|
2013-10-31 18:51:40 +01:00
|
|
|
peers *list.List
|
|
|
|
outboundPeers *list.List
|
|
|
|
persistentPeers *list.List
|
|
|
|
banned map[string]time.Time
|
2013-11-07 16:34:55 +01:00
|
|
|
outboundGroups map[string]int
|
2013-10-30 18:22:35 +01:00
|
|
|
maxOutboundPeers int
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
// randomUint16Number returns a random uint16 in a specified input range. Note
|
2014-03-27 20:05:27 +01:00
|
|
|
// that the range is in zeroth ordering; if you pass it 1800, you will get
|
|
|
|
// values from 0 to 1800.
|
2014-03-18 20:40:49 +01:00
|
|
|
func randomUint16Number(max uint16) uint16 {
|
2014-03-27 20:05:27 +01:00
|
|
|
// In order to avoid modulo bias and ensure every possible outcome in
|
|
|
|
// [0, max) has equal probability, the random number must be sampled
|
|
|
|
// from a random source that has a range limited to a multiple of the
|
|
|
|
// modulus.
|
2014-03-18 20:40:49 +01:00
|
|
|
var randomNumber uint16
|
|
|
|
var limitRange = (math.MaxUint16 / max) * max
|
|
|
|
for {
|
|
|
|
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
|
|
|
|
if randomNumber < limitRange {
|
|
|
|
return (randomNumber % max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:05:27 +01:00
|
|
|
// AddRebroadcastInventory adds 'iv' to the list of inventories to be
|
|
|
|
// rebroadcasted at random intervals until they show up in a block.
|
2014-03-18 20:40:49 +01:00
|
|
|
func (s *server) AddRebroadcastInventory(iv *btcwire.InvVect) {
|
2014-06-02 23:10:33 +02:00
|
|
|
// Ignore if shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryAdd(iv)
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:05:27 +01:00
|
|
|
// RemoveRebroadcastInventory removes 'iv' from the list of items to be
|
|
|
|
// rebroadcasted if present.
|
2014-03-18 20:40:49 +01:00
|
|
|
func (s *server) RemoveRebroadcastInventory(iv *btcwire.InvVect) {
|
2014-06-02 23:10:33 +02:00
|
|
|
// Ignore if shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:40:49 +01:00
|
|
|
s.modifyRebroadcastInv <- broadcastInventoryDel(iv)
|
|
|
|
}
|
|
|
|
|
2013-10-31 18:51:40 +01:00
|
|
|
func (p *peerState) Count() int {
|
|
|
|
return p.peers.Len() + p.outboundPeers.Len() + p.persistentPeers.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerState) OutboundCount() int {
|
|
|
|
return p.outboundPeers.Len() + p.persistentPeers.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerState) NeedMoreOutbound() bool {
|
|
|
|
return p.OutboundCount() < p.maxOutboundPeers &&
|
|
|
|
p.Count() < cfg.MaxPeers
|
|
|
|
}
|
|
|
|
|
2014-01-24 21:13:19 +01:00
|
|
|
// forAllOutboundPeers is a helper function that runs closure on all outbound
|
|
|
|
// peers known to peerState.
|
|
|
|
func (p *peerState) forAllOutboundPeers(closure func(p *peer)) {
|
|
|
|
for e := p.outboundPeers.Front(); e != nil; e = e.Next() {
|
|
|
|
closure(e.Value.(*peer))
|
|
|
|
}
|
|
|
|
for e := p.persistentPeers.Front(); e != nil; e = e.Next() {
|
|
|
|
closure(e.Value.(*peer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forAllPeers is a helper function that runs closure on all peers known to
|
|
|
|
// peerState.
|
|
|
|
func (p *peerState) forAllPeers(closure func(p *peer)) {
|
|
|
|
for e := p.peers.Front(); e != nil; e = e.Next() {
|
|
|
|
closure(e.Value.(*peer))
|
|
|
|
}
|
|
|
|
p.forAllOutboundPeers(closure)
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// handleAddPeerMsg deals with adding new peers. It is invoked from the
|
|
|
|
// peerHandler goroutine.
|
2013-10-30 18:22:35 +01:00
|
|
|
func (s *server) handleAddPeerMsg(state *peerState, p *peer) bool {
|
2013-08-01 19:00:14 +02:00
|
|
|
if p == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Ignore new peers if we're shutting down.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("New peer %s ignored - server is shutting "+
|
2013-10-03 03:05:10 +02:00
|
|
|
"down", p)
|
2013-08-06 23:55:22 +02:00
|
|
|
p.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect banned peers.
|
2013-08-01 19:00:14 +02:00
|
|
|
host, _, err := net.SplitHostPort(p.addr)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("can't split hostport %v", err)
|
2013-08-06 23:55:22 +02:00
|
|
|
p.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-30 18:22:35 +01:00
|
|
|
if banEnd, ok := state.banned[host]; ok {
|
2013-08-06 23:55:22 +02:00
|
|
|
if time.Now().Before(banEnd) {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("Peer %s is banned for another %v - "+
|
2013-08-06 23:55:22 +02:00
|
|
|
"disconnecting", host, banEnd.Sub(time.Now()))
|
|
|
|
p.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Peer %s is no longer banned", host)
|
2013-10-30 18:22:35 +01:00
|
|
|
delete(state.banned, host)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for max peers from a single IP.
|
|
|
|
|
|
|
|
// Limit max number of total peers.
|
2013-10-31 18:51:40 +01:00
|
|
|
if state.Count() >= cfg.MaxPeers {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Max peers reached [%d] - disconnecting "+
|
2013-10-03 03:05:10 +02:00
|
|
|
"peer %s", cfg.MaxPeers, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
p.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
// TODO(oga) how to handle permanent peers here?
|
|
|
|
// they should be rescheduled.
|
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new peer and start it.
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("New peer %s", p)
|
2013-10-31 20:53:29 +01:00
|
|
|
if p.inbound {
|
|
|
|
state.peers.PushBack(p)
|
|
|
|
p.Start()
|
2013-10-31 18:51:40 +01:00
|
|
|
} else {
|
2014-07-06 08:04:24 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(p.na)]++
|
2013-10-31 20:53:29 +01:00
|
|
|
if p.persistent {
|
|
|
|
state.persistentPeers.PushBack(p)
|
2013-10-31 18:51:40 +01:00
|
|
|
} else {
|
|
|
|
state.outboundPeers.PushBack(p)
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleDonePeerMsg deals with peers that have signalled they are done. It is
|
|
|
|
// invoked from the peerHandler goroutine.
|
2013-10-31 18:51:40 +01:00
|
|
|
func (s *server) handleDonePeerMsg(state *peerState, p *peer) {
|
|
|
|
var list *list.List
|
|
|
|
if p.persistent {
|
|
|
|
list = state.persistentPeers
|
|
|
|
} else if p.inbound {
|
|
|
|
list = state.peers
|
|
|
|
} else {
|
|
|
|
list = state.outboundPeers
|
|
|
|
}
|
|
|
|
for e := list.Front(); e != nil; e = e.Next() {
|
2013-08-06 23:55:22 +02:00
|
|
|
if e.Value == p {
|
|
|
|
// Issue an asynchronous reconnect if the peer was a
|
|
|
|
// persistent outbound connection.
|
2014-07-22 22:58:24 +02:00
|
|
|
if !p.inbound && p.persistent && atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
e.Value = newOutboundPeer(s, p.addr, true, p.retryCount+1)
|
2013-10-31 18:51:40 +01:00
|
|
|
return
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-31 20:53:29 +01:00
|
|
|
if !p.inbound {
|
2014-07-06 08:04:24 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(p.na)]--
|
2013-10-31 20:53:29 +01:00
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
list.Remove(e)
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("Removed peer %s", p)
|
2013-10-31 18:51:40 +01:00
|
|
|
return
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
// If we get here it means that either we didn't know about the peer
|
|
|
|
// or we purposefully deleted it.
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleBanPeerMsg deals with banning peers. It is invoked from the
|
|
|
|
// peerHandler goroutine.
|
2013-10-30 18:22:35 +01:00
|
|
|
func (s *server) handleBanPeerMsg(state *peerState, p *peer) {
|
2013-08-01 19:00:14 +02:00
|
|
|
host, _, err := net.SplitHostPort(p.addr)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Debugf("can't split ban peer %s %v", p.addr, err)
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
direction := directionString(p.inbound)
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Banned peer %s (%s) for %v", host, direction,
|
2013-08-06 23:55:22 +02:00
|
|
|
cfg.BanDuration)
|
2013-10-30 18:22:35 +01:00
|
|
|
state.banned[host] = time.Now().Add(cfg.BanDuration)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:11:22 +02:00
|
|
|
// handleRelayInvMsg deals with relaying inventory to peers that are not already
|
2013-09-09 17:58:56 +02:00
|
|
|
// known to have it. It is invoked from the peerHandler goroutine.
|
2013-10-30 18:22:35 +01:00
|
|
|
func (s *server) handleRelayInvMsg(state *peerState, iv *btcwire.InvVect) {
|
2014-01-24 21:13:19 +01:00
|
|
|
state.forAllPeers(func(p *peer) {
|
2013-10-03 00:06:29 +02:00
|
|
|
if !p.Connected() {
|
2013-10-31 18:51:40 +01:00
|
|
|
return
|
2013-09-18 00:39:49 +02:00
|
|
|
}
|
2013-09-09 17:58:56 +02:00
|
|
|
|
2014-07-09 03:44:21 +02:00
|
|
|
if iv.Type == btcwire.InvTypeTx {
|
|
|
|
// Don't relay the transaction to the peer when it has
|
|
|
|
// transaction relaying disabled.
|
|
|
|
if p.RelayTxDisabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't relay the transaction if there is a bloom
|
|
|
|
// filter loaded and the transaction doesn't match it.
|
|
|
|
if p.filter.IsLoaded() {
|
|
|
|
tx, err := s.txMemPool.FetchTransaction(&iv.Hash)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Warn("Attempt to relay tx %s "+
|
|
|
|
"that is not in the memory pool",
|
|
|
|
iv.Hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.filter.MatchTxAndUpdate(tx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue the inventory to be relayed with the next batch.
|
|
|
|
// It will be ignored if the peer is already known to
|
|
|
|
// have the inventory.
|
2013-09-09 17:58:56 +02:00
|
|
|
p.QueueInventory(iv)
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-09-09 17:58:56 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
|
|
|
|
// from the peerHandler goroutine.
|
2013-10-30 18:22:35 +01:00
|
|
|
func (s *server) handleBroadcastMsg(state *peerState, bmsg *broadcastMsg) {
|
2014-01-24 21:13:19 +01:00
|
|
|
state.forAllPeers(func(p *peer) {
|
2013-08-06 23:55:22 +02:00
|
|
|
excluded := false
|
2013-10-31 18:51:40 +01:00
|
|
|
for _, ep := range bmsg.excludePeers {
|
|
|
|
if p == ep {
|
2013-08-06 23:55:22 +02:00
|
|
|
excluded = true
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
// Don't broadcast to still connecting outbound peers .
|
2013-10-03 00:06:29 +02:00
|
|
|
if !p.Connected() {
|
2013-08-01 19:00:14 +02:00
|
|
|
excluded = true
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
if !excluded {
|
2013-10-16 16:49:09 +02:00
|
|
|
p.QueueMessage(bmsg.message, nil)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
type getConnCountMsg struct {
|
2014-06-29 23:36:41 +02:00
|
|
|
reply chan int32
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type getPeerInfoMsg struct {
|
2014-02-16 20:45:48 +01:00
|
|
|
reply chan []*btcjson.GetPeerInfoResult
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 18:18:53 +01:00
|
|
|
type addNodeMsg struct {
|
|
|
|
addr string
|
|
|
|
permanent bool
|
|
|
|
reply chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
type delNodeMsg struct {
|
|
|
|
addr string
|
|
|
|
reply chan error
|
|
|
|
}
|
2013-10-21 19:45:30 +02:00
|
|
|
|
2014-01-24 21:10:02 +01:00
|
|
|
type getAddedNodesMsg struct {
|
|
|
|
reply chan []*peer
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01:00
|
|
|
// handleQuery is the central handler for all queries and commands from other
|
|
|
|
// goroutines related to peer state.
|
2013-10-30 18:22:35 +01:00
|
|
|
func (s *server) handleQuery(querymsg interface{}, state *peerState) {
|
2013-10-21 19:45:30 +02:00
|
|
|
switch msg := querymsg.(type) {
|
|
|
|
case getConnCountMsg:
|
2014-06-29 23:36:41 +02:00
|
|
|
nconnected := int32(0)
|
2014-01-24 21:13:19 +01:00
|
|
|
state.forAllPeers(func(p *peer) {
|
2013-10-31 18:51:40 +01:00
|
|
|
if p.Connected() {
|
2013-10-21 19:45:30 +02:00
|
|
|
nconnected++
|
|
|
|
}
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-10-21 19:45:30 +02:00
|
|
|
msg.reply <- nconnected
|
2014-01-24 21:10:02 +01:00
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
case getPeerInfoMsg:
|
2014-02-04 07:24:40 +01:00
|
|
|
syncPeer := s.blockManager.SyncPeer()
|
2014-02-16 20:45:48 +01:00
|
|
|
infos := make([]*btcjson.GetPeerInfoResult, 0, state.peers.Len())
|
2014-01-24 21:13:19 +01:00
|
|
|
state.forAllPeers(func(p *peer) {
|
2013-10-31 18:51:40 +01:00
|
|
|
if !p.Connected() {
|
|
|
|
return
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
2014-02-04 07:24:40 +01:00
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
// A lot of this will make the race detector go mad,
|
|
|
|
// however it is statistics for purely informational purposes
|
|
|
|
// and we don't really care if they are raced to get the new
|
|
|
|
// version.
|
2014-04-23 18:36:33 +02:00
|
|
|
p.StatsMtx.Lock()
|
2014-02-16 20:45:48 +01:00
|
|
|
info := &btcjson.GetPeerInfoResult{
|
2013-10-31 18:51:40 +01:00
|
|
|
Addr: p.addr,
|
2013-12-28 00:34:39 +01:00
|
|
|
Services: fmt.Sprintf("%08d", p.services),
|
2013-10-31 18:51:40 +01:00
|
|
|
LastSend: p.lastSend.Unix(),
|
|
|
|
LastRecv: p.lastRecv.Unix(),
|
2014-02-05 05:30:34 +01:00
|
|
|
BytesSent: p.bytesSent,
|
|
|
|
BytesRecv: p.bytesReceived,
|
2013-10-31 18:51:40 +01:00
|
|
|
ConnTime: p.timeConnected.Unix(),
|
|
|
|
Version: p.protocolVersion,
|
|
|
|
SubVer: p.userAgent,
|
|
|
|
Inbound: p.inbound,
|
|
|
|
StartingHeight: p.lastBlock,
|
2013-10-21 19:45:30 +02:00
|
|
|
BanScore: 0,
|
2014-02-04 07:24:40 +01:00
|
|
|
SyncNode: p == syncPeer,
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
2014-06-12 21:22:31 +02:00
|
|
|
info.PingTime = float64(p.lastPingMicros)
|
2014-01-21 16:03:15 +01:00
|
|
|
if p.lastPingNonce != 0 {
|
2014-06-12 21:22:31 +02:00
|
|
|
wait := float64(time.Now().Sub(p.lastPingTime).Nanoseconds())
|
2014-01-21 16:03:15 +01:00
|
|
|
// We actually want microseconds.
|
|
|
|
info.PingWait = wait / 1000
|
|
|
|
}
|
2014-04-23 18:36:33 +02:00
|
|
|
p.StatsMtx.Unlock()
|
2013-10-21 19:45:30 +02:00
|
|
|
infos = append(infos, info)
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-10-21 19:45:30 +02:00
|
|
|
msg.reply <- infos
|
2014-01-24 21:10:02 +01:00
|
|
|
|
2013-10-29 18:18:53 +01:00
|
|
|
case addNodeMsg:
|
2013-10-31 18:51:40 +01:00
|
|
|
// XXX(oga) duplicate oneshots?
|
|
|
|
if msg.permanent {
|
|
|
|
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
|
|
|
|
peer := e.Value.(*peer)
|
|
|
|
if peer.addr == msg.addr {
|
|
|
|
msg.reply <- errors.New("peer already connected")
|
|
|
|
return
|
|
|
|
}
|
2013-10-29 18:18:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO(oga) if too many, nuke a non-perm peer.
|
2013-10-30 18:22:35 +01:00
|
|
|
if s.handleAddPeerMsg(state,
|
2014-07-22 22:58:24 +02:00
|
|
|
newOutboundPeer(s, msg.addr, msg.permanent, 0)) {
|
2013-10-29 18:18:53 +01:00
|
|
|
msg.reply <- nil
|
|
|
|
} else {
|
|
|
|
msg.reply <- errors.New("failed to add peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
case delNodeMsg:
|
|
|
|
found := false
|
2013-10-31 18:51:40 +01:00
|
|
|
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
|
2013-10-29 18:18:53 +01:00
|
|
|
peer := e.Value.(*peer)
|
|
|
|
if peer.addr == msg.addr {
|
2013-10-31 20:53:29 +01:00
|
|
|
// Keep group counts ok since we remove from
|
|
|
|
// the list now.
|
2014-07-06 08:04:24 +02:00
|
|
|
state.outboundGroups[addrmgr.GroupKey(peer.na)]--
|
2013-10-31 18:51:40 +01:00
|
|
|
// This is ok because we are not continuing
|
|
|
|
// to iterate so won't corrupt the loop.
|
|
|
|
state.persistentPeers.Remove(e)
|
2013-10-29 18:18:53 +01:00
|
|
|
peer.Disconnect()
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
msg.reply <- nil
|
|
|
|
} else {
|
|
|
|
msg.reply <- errors.New("peer not found")
|
|
|
|
}
|
2014-01-24 21:10:02 +01:00
|
|
|
|
|
|
|
// Request a list of the persistent (added) peers.
|
|
|
|
case getAddedNodesMsg:
|
|
|
|
// Respond with a slice of the relavent peers.
|
|
|
|
peers := make([]*peer, 0, state.persistentPeers.Len())
|
|
|
|
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
|
|
|
|
peer := e.Value.(*peer)
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
msg.reply <- peers
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// listenHandler is the main listener which accepts incoming connections for the
|
|
|
|
// server. It must be run as a goroutine.
|
2013-08-07 17:38:39 +02:00
|
|
|
func (s *server) listenHandler(listener net.Listener) {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Server listening on %s", listener.Addr())
|
2013-10-02 02:45:21 +02:00
|
|
|
for atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-08-07 17:38:39 +02:00
|
|
|
conn, err := listener.Accept()
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
// Only log the error if we're not forcibly shutting down.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Errorf("can't accept connection: %v",
|
2013-10-02 23:49:31 +02:00
|
|
|
err)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2013-09-12 21:19:10 +02:00
|
|
|
s.AddPeer(newInboundPeer(s, conn))
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
s.wg.Done()
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Tracef("Listener handler done for %s", listener.Addr())
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:38:45 +02:00
|
|
|
// seedFromDNS uses DNS seeding to populate the address manager with peers.
|
|
|
|
func (s *server) seedFromDNS() {
|
|
|
|
// Nothing to do if DNS seeding is disabled.
|
|
|
|
if cfg.DisableDNSSeed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, seeder := range activeNetParams.dnsSeeds {
|
2014-07-04 20:04:01 +02:00
|
|
|
go func(seeder string) {
|
2014-07-10 20:52:02 +02:00
|
|
|
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2014-07-04 20:04:01 +02:00
|
|
|
seedpeers, err := dnsDiscover(seeder)
|
|
|
|
if err != nil {
|
|
|
|
discLog.Infof("DNS discovery failed on seed %s: %v", seeder, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
numPeers := len(seedpeers)
|
|
|
|
|
|
|
|
discLog.Infof("%d addresses found from DNS seed %s", numPeers, seeder)
|
2013-10-08 00:38:45 +02:00
|
|
|
|
2014-07-04 20:04:01 +02:00
|
|
|
if numPeers == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addresses := make([]*btcwire.NetAddress, len(seedpeers))
|
|
|
|
// if this errors then we have *real* problems
|
|
|
|
intPort, _ := strconv.Atoi(activeNetParams.DefaultPort)
|
|
|
|
for i, peer := range seedpeers {
|
|
|
|
addresses[i] = new(btcwire.NetAddress)
|
|
|
|
addresses[i].SetAddress(peer, uint16(intPort))
|
|
|
|
// bitcoind seeds with addresses from
|
|
|
|
// a time randomly selected between 3
|
|
|
|
// and 7 days ago.
|
|
|
|
addresses[i].Timestamp = time.Now().Add(-1 *
|
|
|
|
time.Second * time.Duration(secondsIn3Days+
|
2014-07-06 08:04:24 +02:00
|
|
|
randSource.Int31n(secondsIn4Days)))
|
2014-07-04 20:04:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bitcoind uses a lookup of the dns seeder here. This
|
|
|
|
// is rather strange since the values looked up by the
|
|
|
|
// DNS seed lookups will vary quite a lot.
|
|
|
|
// to replicate this behaviour we put all addresses as
|
|
|
|
// having come from the first one.
|
|
|
|
s.addrManager.AddAddresses(addresses, addresses[0])
|
|
|
|
}(seeder)
|
2013-10-08 00:38:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// peerHandler is used to handle peer operations such as adding and removing
|
|
|
|
// peers to and from the server, banning peers, and broadcasting messages to
|
|
|
|
// peers. It must be run a a goroutine.
|
|
|
|
func (s *server) peerHandler() {
|
2013-08-09 21:32:22 +02:00
|
|
|
// Start the address manager and block manager, both of which are needed
|
|
|
|
// by peers. This is done here since their lifecycle is closely tied
|
|
|
|
// to this handler and rather than adding more channels to sychronize
|
|
|
|
// things, it's easier and slightly faster to simply start and stop them
|
|
|
|
// in this handler.
|
|
|
|
s.addrManager.Start()
|
|
|
|
s.blockManager.Start()
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Tracef("Starting peer handler")
|
2013-10-30 18:22:35 +01:00
|
|
|
state := &peerState{
|
2013-10-31 18:51:40 +01:00
|
|
|
peers: list.New(),
|
2013-10-30 18:22:35 +01:00
|
|
|
persistentPeers: list.New(),
|
2013-10-31 18:51:40 +01:00
|
|
|
outboundPeers: list.New(),
|
|
|
|
banned: make(map[string]time.Time),
|
2013-10-30 18:22:35 +01:00
|
|
|
maxOutboundPeers: defaultMaxOutbound,
|
2013-10-31 20:53:29 +01:00
|
|
|
outboundGroups: make(map[string]int),
|
2013-10-30 18:22:35 +01:00
|
|
|
}
|
|
|
|
if cfg.MaxPeers < state.maxOutboundPeers {
|
|
|
|
state.maxOutboundPeers = cfg.MaxPeers
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:38:45 +02:00
|
|
|
// Add peers discovered through DNS to the address manager.
|
|
|
|
s.seedFromDNS()
|
2013-08-01 19:00:14 +02:00
|
|
|
|
|
|
|
// Start up persistent peers.
|
|
|
|
permanentPeers := cfg.ConnectPeers
|
|
|
|
if len(permanentPeers) == 0 {
|
|
|
|
permanentPeers = cfg.AddPeers
|
|
|
|
}
|
|
|
|
for _, addr := range permanentPeers {
|
2014-07-22 22:58:24 +02:00
|
|
|
s.handleAddPeerMsg(state, newOutboundPeer(s, addr, true, 0))
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing else happens, wake us up soon.
|
2014-07-02 17:31:10 +02:00
|
|
|
time.AfterFunc(10*time.Second, func() { s.wakeup <- struct{}{} })
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-02 02:45:21 +02:00
|
|
|
out:
|
|
|
|
for {
|
2013-08-06 23:55:22 +02:00
|
|
|
select {
|
|
|
|
// New peers connected to the server.
|
|
|
|
case p := <-s.newPeers:
|
2013-10-31 18:51:40 +01:00
|
|
|
s.handleAddPeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// Disconnected peers.
|
|
|
|
case p := <-s.donePeers:
|
2013-10-31 18:51:40 +01:00
|
|
|
s.handleDonePeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// Peer to ban.
|
|
|
|
case p := <-s.banPeers:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleBanPeerMsg(state, p)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-09-09 17:58:56 +02:00
|
|
|
// New inventory to potentially be relayed to other peers.
|
|
|
|
case invMsg := <-s.relayInv:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleRelayInvMsg(state, invMsg)
|
2013-09-09 17:58:56 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Message to broadcast to all connected peers except those
|
|
|
|
// which are excluded by the message.
|
|
|
|
case bmsg := <-s.broadcast:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleBroadcastMsg(state, &bmsg)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-08-01 19:00:14 +02:00
|
|
|
// Used by timers below to wake us back up.
|
|
|
|
case <-s.wakeup:
|
|
|
|
// this page left intentionally blank
|
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
case qmsg := <-s.query:
|
2013-10-30 18:22:35 +01:00
|
|
|
s.handleQuery(qmsg, state)
|
2013-10-21 19:45:30 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Shutdown the peer handler.
|
|
|
|
case <-s.quit:
|
|
|
|
// Shutdown peers.
|
2014-01-24 21:13:19 +01:00
|
|
|
state.forAllPeers(func(p *peer) {
|
2013-08-06 23:55:22 +02:00
|
|
|
p.Shutdown()
|
2013-10-31 18:51:40 +01:00
|
|
|
})
|
2013-10-02 02:45:21 +02:00
|
|
|
break out
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2014-05-28 22:19:38 +02:00
|
|
|
// Don't try to connect to more peers when running on the
|
|
|
|
// simulation test network. The simulation network is only
|
|
|
|
// intended to connect to specified peers and actively avoid
|
|
|
|
// advertising and connecting to discovered peers.
|
|
|
|
if cfg.SimNet {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only try connect to more peers if we actually need more.
|
2013-10-31 18:51:40 +01:00
|
|
|
if !state.NeedMoreOutbound() || len(cfg.ConnectPeers) > 0 ||
|
2013-10-02 02:45:21 +02:00
|
|
|
atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-08-01 19:00:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2013-09-17 21:01:22 +02:00
|
|
|
tries := 0
|
2013-10-31 18:51:40 +01:00
|
|
|
for state.NeedMoreOutbound() &&
|
2013-10-02 02:45:21 +02:00
|
|
|
atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-09-17 21:01:22 +02:00
|
|
|
// We bias like bitcoind does, 10 for no outgoing
|
|
|
|
// up to 90 (8) for the selection of new vs tried
|
|
|
|
//addresses.
|
|
|
|
|
2013-10-31 18:51:40 +01:00
|
|
|
nPeers := state.OutboundCount()
|
2013-09-17 21:01:22 +02:00
|
|
|
if nPeers > 8 {
|
|
|
|
nPeers = 8
|
|
|
|
}
|
|
|
|
addr := s.addrManager.GetAddress("any", 10+nPeers*10)
|
|
|
|
if addr == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-07-06 08:04:24 +02:00
|
|
|
key := addrmgr.GroupKey(addr.NetAddress())
|
2013-09-17 21:01:22 +02:00
|
|
|
// Address will not be invalid, local or unroutable
|
|
|
|
// because addrmanager rejects those on addition.
|
|
|
|
// Just check that we don't already have an address
|
|
|
|
// in the same group so that we are not connecting
|
|
|
|
// to the same network segment at the expense of
|
2014-07-04 20:04:01 +02:00
|
|
|
// others.
|
2013-10-31 20:53:29 +01:00
|
|
|
if state.outboundGroups[key] != 0 {
|
2014-07-04 20:04:01 +02:00
|
|
|
break
|
2013-09-17 21:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tries++
|
|
|
|
// After 100 bad tries exit the loop and we'll try again
|
|
|
|
// later.
|
|
|
|
if tries > 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX if we have limited that address skip
|
|
|
|
|
|
|
|
// only allow recent nodes (10mins) after we failed 30
|
|
|
|
// times
|
2014-07-06 08:04:24 +02:00
|
|
|
if time.Now().After(addr.LastAttempt().Add(10*time.Minute)) &&
|
2013-09-17 21:01:22 +02:00
|
|
|
tries < 30 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow nondefault ports after 50 failed tries.
|
2014-07-06 08:04:24 +02:00
|
|
|
if fmt.Sprintf("%d", addr.NetAddress().Port) !=
|
2014-05-29 19:35:09 +02:00
|
|
|
activeNetParams.DefaultPort && tries < 50 {
|
2013-09-17 21:01:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-06 08:04:24 +02:00
|
|
|
addrStr := addrmgr.NetAddressKey(addr.NetAddress())
|
2013-09-17 21:01:22 +02:00
|
|
|
|
|
|
|
tries = 0
|
|
|
|
// any failure will be due to banned peers etc. we have
|
|
|
|
// already checked that we have room for more peers.
|
2013-10-30 18:22:35 +01:00
|
|
|
if s.handleAddPeerMsg(state,
|
2014-07-22 22:58:24 +02:00
|
|
|
newOutboundPeer(s, addrStr, false, 0)) {
|
2013-09-17 21:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
|
2014-07-22 22:58:24 +02:00
|
|
|
// We need more peers, wake up in ten seconds and try again.
|
2013-10-31 18:51:40 +01:00
|
|
|
if state.NeedMoreOutbound() {
|
2013-09-17 21:01:22 +02:00
|
|
|
time.AfterFunc(10*time.Second, func() {
|
2014-07-02 17:31:10 +02:00
|
|
|
s.wakeup <- struct{}{}
|
2013-09-17 21:01:22 +02:00
|
|
|
})
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 21:32:22 +02:00
|
|
|
s.blockManager.Stop()
|
|
|
|
s.addrManager.Stop()
|
2013-08-06 23:55:22 +02:00
|
|
|
s.wg.Done()
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Tracef("Peer handler done")
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeer adds a new peer that has already been connected to the server.
|
|
|
|
func (s *server) AddPeer(p *peer) {
|
|
|
|
s.newPeers <- p
|
|
|
|
}
|
|
|
|
|
|
|
|
// BanPeer bans a peer that has already been connected to the server by ip.
|
|
|
|
func (s *server) BanPeer(p *peer) {
|
|
|
|
s.banPeers <- p
|
|
|
|
}
|
|
|
|
|
2013-09-09 17:58:56 +02:00
|
|
|
// RelayInventory relays the passed inventory to all connected peers that are
|
|
|
|
// not already known to have it.
|
|
|
|
func (s *server) RelayInventory(invVect *btcwire.InvVect) {
|
|
|
|
s.relayInv <- invVect
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// BroadcastMessage sends msg to all peers currently connected to the server
|
|
|
|
// except those in the passed peers to exclude.
|
|
|
|
func (s *server) BroadcastMessage(msg btcwire.Message, exclPeers ...*peer) {
|
|
|
|
// XXX: Need to determine if this is an alert that has already been
|
|
|
|
// broadcast and refrain from broadcasting again.
|
|
|
|
bmsg := broadcastMsg{message: msg, excludePeers: exclPeers}
|
|
|
|
s.broadcast <- bmsg
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01:00
|
|
|
// ConnectedCount returns the number of currently connected peers.
|
2014-06-29 23:36:41 +02:00
|
|
|
func (s *server) ConnectedCount() int32 {
|
|
|
|
replyChan := make(chan int32)
|
2013-10-21 19:45:30 +02:00
|
|
|
|
|
|
|
s.query <- getConnCountMsg{reply: replyChan}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2014-01-24 21:10:02 +01:00
|
|
|
// AddedNodeInfo returns an array of btcjson.GetAddedNodeInfoResult structures
|
|
|
|
// describing the persistent (added) nodes.
|
|
|
|
func (s *server) AddedNodeInfo() []*peer {
|
|
|
|
replyChan := make(chan []*peer)
|
|
|
|
s.query <- getAddedNodesMsg{reply: replyChan}
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01:00
|
|
|
// PeerInfo returns an array of PeerInfo structures describing all connected
|
|
|
|
// peers.
|
2014-02-16 20:45:48 +01:00
|
|
|
func (s *server) PeerInfo() []*btcjson.GetPeerInfoResult {
|
|
|
|
replyChan := make(chan []*btcjson.GetPeerInfoResult)
|
2013-10-21 19:45:30 +02:00
|
|
|
|
|
|
|
s.query <- getPeerInfoMsg{reply: replyChan}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01:00
|
|
|
// AddAddr adds `addr' as a new outbound peer. If permanent is true then the
|
|
|
|
// peer will be persistent and reconnect if the connection is lost.
|
|
|
|
// It is an error to call this with an already existing peer.
|
2013-10-29 18:18:53 +01:00
|
|
|
func (s *server) AddAddr(addr string, permanent bool) error {
|
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
s.query <- addNodeMsg{addr: addr, permanent: permanent, reply: replyChan}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:29:23 +01:00
|
|
|
// RemoveAddr removes `addr' from the list of persistent peers if present.
|
|
|
|
// An error will be returned if the peer was not found.
|
2013-10-29 18:18:53 +01:00
|
|
|
func (s *server) RemoveAddr(addr string) error {
|
|
|
|
replyChan := make(chan error)
|
|
|
|
|
|
|
|
s.query <- delNodeMsg{addr: addr, reply: replyChan}
|
|
|
|
|
|
|
|
return <-replyChan
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:53:25 +01:00
|
|
|
// AddBytesSent adds the passed number of bytes to the total bytes sent counter
|
|
|
|
// for the server. It is safe for concurrent access.
|
|
|
|
func (s *server) AddBytesSent(bytesSent uint64) {
|
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
s.bytesSent += bytesSent
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBytesReceived adds the passed number of bytes to the total bytes received
|
|
|
|
// counter for the server. It is safe for concurrent access.
|
|
|
|
func (s *server) AddBytesReceived(bytesReceived uint64) {
|
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
s.bytesReceived += bytesReceived
|
|
|
|
}
|
|
|
|
|
2014-02-04 08:26:12 +01:00
|
|
|
// NetTotals returns the sum of all bytes received and sent across the network
|
2014-02-13 16:53:25 +01:00
|
|
|
// for all peers. It is safe for concurrent access.
|
2014-02-05 18:09:45 +01:00
|
|
|
func (s *server) NetTotals() (uint64, uint64) {
|
2014-02-13 16:53:25 +01:00
|
|
|
s.bytesMutex.Lock()
|
|
|
|
defer s.bytesMutex.Unlock()
|
|
|
|
|
|
|
|
return s.bytesReceived, s.bytesSent
|
2014-02-04 08:26:12 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 01:09:35 +01:00
|
|
|
// rebroadcastHandler keeps track of user submitted inventories that we have
|
|
|
|
// sent out but have not yet made it into a block. We periodically rebroadcast
|
|
|
|
// them in case our peers restarted or otherwise lost track of them.
|
2014-03-18 20:40:49 +01:00
|
|
|
func (s *server) rebroadcastHandler() {
|
2014-03-28 20:49:38 +01:00
|
|
|
// Wait 5 min before first tx rebroadcast.
|
2014-03-28 01:09:35 +01:00
|
|
|
timer := time.NewTimer(5 * time.Minute)
|
2014-03-18 20:40:49 +01:00
|
|
|
pendingInvs := make(map[btcwire.InvVect]struct{})
|
|
|
|
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case riv := <-s.modifyRebroadcastInv:
|
|
|
|
switch msg := riv.(type) {
|
|
|
|
// Incoming InvVects are added to our map of RPC txs.
|
|
|
|
case broadcastInventoryAdd:
|
|
|
|
pendingInvs[*msg] = struct{}{}
|
|
|
|
|
2014-03-28 01:09:35 +01:00
|
|
|
// When an InvVect has been added to a block, we can
|
|
|
|
// now remove it, if it was present.
|
2014-03-18 20:40:49 +01:00
|
|
|
case broadcastInventoryDel:
|
|
|
|
if _, ok := pendingInvs[*msg]; ok {
|
|
|
|
delete(pendingInvs, *msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-timer.C:
|
2014-03-28 01:09:35 +01:00
|
|
|
// Any inventory we have has not made it into a block
|
|
|
|
// yet. We periodically resubmit them until they have.
|
2014-03-18 20:40:49 +01:00
|
|
|
for iv := range pendingInvs {
|
|
|
|
ivCopy := iv
|
|
|
|
s.RelayInventory(&ivCopy)
|
|
|
|
}
|
|
|
|
|
2014-03-28 01:09:35 +01:00
|
|
|
// Process at a random time up to 30mins (in seconds)
|
|
|
|
// in the future.
|
|
|
|
timer.Reset(time.Second *
|
|
|
|
time.Duration(randomUint16Number(1800)))
|
2014-03-18 20:40:49 +01:00
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
|
2014-06-02 23:10:33 +02:00
|
|
|
// Drain channels before exiting so nothing is left waiting around
|
|
|
|
// to send.
|
|
|
|
cleanup:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.modifyRebroadcastInv:
|
|
|
|
default:
|
|
|
|
break cleanup
|
|
|
|
}
|
|
|
|
}
|
2014-03-18 20:40:49 +01:00
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Start begins accepting connections from peers.
|
|
|
|
func (s *server) Start() {
|
|
|
|
// Already started?
|
2013-10-03 01:33:42 +02:00
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Trace("Starting server")
|
2013-08-09 21:32:22 +02:00
|
|
|
|
|
|
|
// Start all the listeners. There will not be any if listening is
|
|
|
|
// disabled.
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
|
|
|
s.wg.Add(1)
|
2013-09-13 00:24:37 +02:00
|
|
|
go s.listenHandler(listener)
|
2013-08-07 17:38:39 +02:00
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
|
|
|
// Start the peer handler which in turn starts the address and block
|
|
|
|
// managers.
|
2013-08-07 17:38:39 +02:00
|
|
|
s.wg.Add(1)
|
2013-09-13 00:24:37 +02:00
|
|
|
go s.peerHandler()
|
2014-03-18 20:40:49 +01:00
|
|
|
|
2013-12-10 02:31:22 +01:00
|
|
|
if s.nat != nil {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.upnpUpdateThread()
|
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2014-03-18 20:40:49 +01:00
|
|
|
s.wg.Add(1)
|
|
|
|
|
|
|
|
// Start the rebroadcastHandler, which ensures user tx received by
|
|
|
|
// the RPC server are rebroadcast until being included in a block.
|
|
|
|
go s.rebroadcastHandler()
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
s.rpcServer.Start()
|
|
|
|
}
|
2014-06-12 03:09:38 +02:00
|
|
|
|
|
|
|
// Start the CPU miner if generation is enabled.
|
|
|
|
if cfg.Generate {
|
|
|
|
s.cpuMiner.Start()
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully shuts down the server by stopping and disconnecting all
|
|
|
|
// peers and the main listener.
|
|
|
|
func (s *server) Stop() error {
|
2013-10-02 02:45:21 +02:00
|
|
|
// Make sure this only happens once.
|
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Infof("Server is already in the process of shutting down")
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutting down")
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2013-10-02 02:45:21 +02:00
|
|
|
// Stop all the listeners. There will not be any listeners if
|
|
|
|
// listening is disabled.
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-08-09 21:32:22 +02:00
|
|
|
|
2014-06-12 03:09:38 +02:00
|
|
|
// Stop the CPU miner if needed
|
|
|
|
s.cpuMiner.Stop()
|
|
|
|
|
2013-08-09 21:32:22 +02:00
|
|
|
// Shutdown the RPC server if it's not disabled.
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2013-08-09 21:32:22 +02:00
|
|
|
s.rpcServer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal the remaining goroutines to quit.
|
|
|
|
close(s.quit)
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForShutdown blocks until the main listener and peer handlers are stopped.
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleShutdown schedules a server shutdown after the specified duration.
|
|
|
|
// It also dynamically adjusts how often to warn the server is going down based
|
|
|
|
// on remaining duration.
|
|
|
|
func (s *server) ScheduleShutdown(duration time.Duration) {
|
|
|
|
// Don't schedule shutdown more than once.
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.AddInt32(&s.shutdownSched, 1) != 1 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutdown in %v", duration)
|
2013-08-06 23:55:22 +02:00
|
|
|
go func() {
|
|
|
|
remaining := duration
|
|
|
|
tickDuration := dynamicTickDuration(remaining)
|
|
|
|
done := time.After(remaining)
|
|
|
|
ticker := time.NewTicker(tickDuration)
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
ticker.Stop()
|
|
|
|
s.Stop()
|
|
|
|
break out
|
|
|
|
case <-ticker.C:
|
|
|
|
remaining = remaining - tickDuration
|
|
|
|
if remaining < time.Second {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change tick duration dynamically based on remaining time.
|
|
|
|
newDuration := dynamicTickDuration(remaining)
|
|
|
|
if tickDuration != newDuration {
|
|
|
|
tickDuration = newDuration
|
|
|
|
ticker.Stop()
|
|
|
|
ticker = time.NewTicker(tickDuration)
|
|
|
|
}
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Server shutdown in %v", remaining)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01:00
|
|
|
// parseListeners splits the list of listen addresses passed in addrs into
|
|
|
|
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
|
2013-11-14 04:12:41 +01:00
|
|
|
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
2013-11-14 02:43:36 +01:00
|
|
|
// detects addresses which apply to "all interfaces" and adds the address to
|
|
|
|
// both slices.
|
2013-11-26 01:40:16 +01:00
|
|
|
func parseListeners(addrs []string) ([]string, []string, bool, error) {
|
2013-11-14 03:57:34 +01:00
|
|
|
ipv4ListenAddrs := make([]string, 0, len(addrs)*2)
|
|
|
|
ipv6ListenAddrs := make([]string, 0, len(addrs)*2)
|
2013-11-26 01:40:16 +01:00
|
|
|
haveWildcard := false
|
|
|
|
|
2013-11-14 03:57:34 +01:00
|
|
|
for _, addr := range addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
2013-11-14 02:43:36 +01:00
|
|
|
// Shouldn't happen due to already being normalized.
|
2013-11-26 01:40:16 +01:00
|
|
|
return nil, nil, false, err
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01:00
|
|
|
// Empty host or host of * on plan9 is both IPv4 and IPv6.
|
2013-09-19 17:46:33 +02:00
|
|
|
if host == "" || (host == "*" && runtime.GOOS == "plan9") {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
2013-11-26 01:40:16 +01:00
|
|
|
haveWildcard = true
|
2013-09-19 17:46:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the IP.
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
2013-11-26 01:40:16 +01:00
|
|
|
return nil, nil, false, fmt.Errorf("'%s' is not a "+
|
|
|
|
"valid IP address", host)
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-11-14 02:43:36 +01:00
|
|
|
// To4 returns nil when the IP is not an IPv4 address, so use
|
|
|
|
// this determine the address type.
|
2013-09-19 17:46:33 +02:00
|
|
|
if ip.To4() == nil {
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
|
|
|
} else {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
return ipv4ListenAddrs, ipv6ListenAddrs, haveWildcard, nil
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-12-10 02:31:22 +01:00
|
|
|
func (s *server) upnpUpdateThread() {
|
|
|
|
// Go off immediately to prevent code duplication, thereafter we renew
|
|
|
|
// lease every 15 minutes.
|
|
|
|
timer := time.NewTimer(0 * time.Second)
|
2014-05-29 19:35:09 +02:00
|
|
|
lport, _ := strconv.ParseInt(activeNetParams.DefaultPort, 10, 16)
|
2013-12-10 02:31:22 +01:00
|
|
|
first := true
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
// TODO(oga) pick external port more cleverly
|
|
|
|
// TODO(oga) know which ports we are listening to on an external net.
|
|
|
|
// TODO(oga) if specific listen port doesn't work then ask for wildcard
|
|
|
|
// listen port?
|
|
|
|
// XXX this assumes timeout is in seconds.
|
|
|
|
listenPort, err := s.nat.AddPortMapping("tcp", int(lport), int(lport),
|
|
|
|
"btcd listen port", 20*60)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Warnf("can't add UPnP port mapping: %v", err)
|
|
|
|
}
|
|
|
|
if first && err == nil {
|
|
|
|
// TODO(oga): look this up periodically to see if upnp domain changed
|
|
|
|
// and so did ip.
|
|
|
|
externalip, err := s.nat.GetExternalAddress()
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Warnf("UPnP can't get external address: %v", err)
|
|
|
|
continue out
|
|
|
|
}
|
|
|
|
na := btcwire.NewNetAddressIPPort(externalip, uint16(listenPort),
|
|
|
|
btcwire.SFNodeNetwork)
|
2014-07-06 15:47:42 +02:00
|
|
|
err = s.addrManager.AddLocalAddress(na, addrmgr.UpnpPrio)
|
|
|
|
if err != nil {
|
|
|
|
// XXX DeletePortMapping?
|
|
|
|
}
|
2014-07-06 08:04:24 +02:00
|
|
|
srvrLog.Warnf("Successfully bound via UPnP to %s", addrmgr.NetAddressKey(na))
|
2013-12-10 02:31:22 +01:00
|
|
|
first = false
|
|
|
|
}
|
|
|
|
timer.Reset(time.Minute * 15)
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
|
|
|
|
if err := s.nat.DeletePortMapping("tcp", int(lport), int(lport)); err != nil {
|
|
|
|
srvrLog.Warnf("unable to remove UPnP port mapping: %v", err)
|
|
|
|
} else {
|
|
|
|
srvrLog.Debugf("succesfully disestablished UPnP port mapping")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// newServer returns a new btcd server configured to listen on addr for the
|
2014-05-23 06:14:36 +02:00
|
|
|
// bitcoin network type specified by netParams. Use start to begin accepting
|
2013-08-06 23:55:22 +02:00
|
|
|
// connections from peers.
|
2014-05-23 06:14:36 +02:00
|
|
|
func newServer(listenAddrs []string, db btcdb.Db, netParams *btcnet.Params) (*server, error) {
|
2013-08-06 23:55:22 +02:00
|
|
|
nonce, err := btcwire.RandomUint64()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-06 08:04:24 +02:00
|
|
|
amgr := addrmgr.New(cfg.DataDir, btcdLookup)
|
2013-11-26 01:40:16 +01:00
|
|
|
|
2013-11-14 04:16:49 +01:00
|
|
|
var listeners []net.Listener
|
2013-12-10 02:31:22 +01:00
|
|
|
var nat NAT
|
2013-08-07 19:35:01 +02:00
|
|
|
if !cfg.DisableListen {
|
2013-11-26 01:40:16 +01:00
|
|
|
ipv4Addrs, ipv6Addrs, wildcard, err :=
|
|
|
|
parseListeners(listenAddrs)
|
2013-11-14 04:16:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = make([]net.Listener, 0, len(ipv4Addrs)+len(ipv6Addrs))
|
2013-11-26 20:30:24 +01:00
|
|
|
discover := true
|
|
|
|
if len(cfg.ExternalIPs) != 0 {
|
|
|
|
discover = false
|
2013-12-16 21:48:23 +01:00
|
|
|
// if this fails we have real issues.
|
|
|
|
port, _ := strconv.ParseUint(
|
2014-05-29 19:35:09 +02:00
|
|
|
activeNetParams.DefaultPort, 10, 16)
|
2013-11-26 20:30:24 +01:00
|
|
|
|
|
|
|
for _, sip := range cfg.ExternalIPs {
|
2013-12-16 21:48:23 +01:00
|
|
|
eport := uint16(port)
|
|
|
|
host, portstr, err := net.SplitHostPort(sip)
|
|
|
|
if err != nil {
|
|
|
|
// no port, use default.
|
|
|
|
host = sip
|
|
|
|
} else {
|
|
|
|
port, err := strconv.ParseUint(
|
|
|
|
portstr, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Warnf("Can not parse "+
|
|
|
|
"port from %s for "+
|
|
|
|
"externalip: %v", sip,
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
eport = uint16(port)
|
|
|
|
}
|
2014-07-06 08:04:24 +02:00
|
|
|
na, err := amgr.HostToNetAddress(host, eport,
|
2013-11-26 20:30:24 +01:00
|
|
|
btcwire.SFNodeNetwork)
|
|
|
|
if err != nil {
|
2013-12-16 21:48:23 +01:00
|
|
|
srvrLog.Warnf("Not adding %s as "+
|
|
|
|
"externalip: %v", sip, err)
|
2013-11-26 20:30:24 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.ManualPrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Warnf("Skipping specified external IP: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-12-10 02:31:22 +01:00
|
|
|
} else if discover && cfg.Upnp {
|
|
|
|
nat, err = Discover()
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Warnf("Can't discover upnp: %v", err)
|
|
|
|
}
|
|
|
|
// nil nat here is fine, just means no upnp on network.
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-11-14 04:16:49 +01:00
|
|
|
|
2013-11-26 01:40:16 +01:00
|
|
|
// TODO(oga) nonstandard port...
|
|
|
|
if wildcard {
|
|
|
|
port, err :=
|
2014-05-29 19:35:09 +02:00
|
|
|
strconv.ParseUint(activeNetParams.DefaultPort,
|
2013-11-26 01:40:16 +01:00
|
|
|
10, 16)
|
|
|
|
if err != nil {
|
|
|
|
// I can't think of a cleaner way to do this...
|
|
|
|
goto nowc
|
|
|
|
}
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
for _, a := range addrs {
|
|
|
|
ip, _, err := net.ParseCIDR(a.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
na := btcwire.NewNetAddressIPPort(ip,
|
|
|
|
uint16(port), btcwire.SFNodeNetwork)
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02:00
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.InterfacePrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Debugf("Skipping local address: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nowc:
|
|
|
|
|
2013-11-14 04:12:41 +01:00
|
|
|
for _, addr := range ipv4Addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
listener, err := net.Listen("tcp4", addr)
|
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 17:46:33 +02:00
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 01:40:16 +01:00
|
|
|
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02:00
|
|
|
if na, err := amgr.DeserializeNetAddress(addr); err == nil {
|
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.BoundPrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Warnf("Skipping bound address: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
2013-08-07 19:35:01 +02:00
|
|
|
}
|
2013-08-07 17:38:39 +02:00
|
|
|
|
2013-11-14 04:12:41 +01:00
|
|
|
for _, addr := range ipv6Addrs {
|
2013-09-19 17:46:33 +02:00
|
|
|
listener, err := net.Listen("tcp6", addr)
|
|
|
|
if err != nil {
|
2013-11-21 19:03:56 +01:00
|
|
|
srvrLog.Warnf("Can't listen on %s: %v", addr,
|
2013-09-19 17:46:33 +02:00
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
2013-11-26 20:30:24 +01:00
|
|
|
if discover {
|
2014-07-06 15:47:42 +02:00
|
|
|
if na, err := amgr.DeserializeNetAddress(addr); err == nil {
|
|
|
|
err = amgr.AddLocalAddress(na, addrmgr.BoundPrio)
|
|
|
|
if err != nil {
|
|
|
|
amgrLog.Debugf("Skipping bound address: %v", err)
|
|
|
|
}
|
2013-11-26 20:30:24 +01:00
|
|
|
}
|
2013-11-26 01:40:16 +01:00
|
|
|
}
|
2013-09-19 17:46:33 +02:00
|
|
|
}
|
2013-11-14 04:16:49 +01:00
|
|
|
|
2013-09-19 17:46:33 +02:00
|
|
|
if len(listeners) == 0 {
|
2014-07-02 16:25:42 +02:00
|
|
|
return nil, errors.New("no valid listen address")
|
2013-08-07 19:35:01 +02:00
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := server{
|
2014-03-18 20:40:49 +01:00
|
|
|
nonce: nonce,
|
|
|
|
listeners: listeners,
|
2014-05-27 06:20:20 +02:00
|
|
|
netParams: netParams,
|
2014-03-18 20:40:49 +01:00
|
|
|
addrManager: amgr,
|
|
|
|
newPeers: make(chan *peer, cfg.MaxPeers),
|
|
|
|
donePeers: make(chan *peer, cfg.MaxPeers),
|
|
|
|
banPeers: make(chan *peer, cfg.MaxPeers),
|
2014-07-02 17:31:10 +02:00
|
|
|
wakeup: make(chan struct{}),
|
2014-03-18 20:40:49 +01:00
|
|
|
query: make(chan interface{}),
|
|
|
|
relayInv: make(chan *btcwire.InvVect, cfg.MaxPeers),
|
|
|
|
broadcast: make(chan broadcastMsg, cfg.MaxPeers),
|
2014-07-02 17:31:10 +02:00
|
|
|
quit: make(chan struct{}),
|
2014-03-18 20:40:49 +01:00
|
|
|
modifyRebroadcastInv: make(chan interface{}),
|
|
|
|
nat: nat,
|
|
|
|
db: db,
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-01 00:43:14 +02:00
|
|
|
bm, err := newBlockManager(&s)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-01 00:43:14 +02:00
|
|
|
s.blockManager = bm
|
2013-09-20 19:55:27 +02:00
|
|
|
s.txMemPool = newTxMemPool(&s)
|
2014-06-12 03:09:38 +02:00
|
|
|
s.cpuMiner = newCPUMiner(&s)
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2013-11-14 02:51:37 +01:00
|
|
|
s.rpcServer, err = newRPCServer(cfg.RPCListeners, &s)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 03:09:38 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dynamicTickDuration is a convenience function used to dynamically choose a
|
|
|
|
// tick duration based on remaining time. It is primarily used during
|
|
|
|
// server shutdown to make shutdown warnings more frequent as the shutdown time
|
|
|
|
// approaches.
|
|
|
|
func dynamicTickDuration(remaining time.Duration) time.Duration {
|
|
|
|
switch {
|
|
|
|
case remaining <= time.Second*5:
|
|
|
|
return time.Second
|
|
|
|
case remaining <= time.Second*15:
|
|
|
|
return time.Second * 5
|
|
|
|
case remaining <= time.Minute:
|
|
|
|
return time.Second * 15
|
|
|
|
case remaining <= time.Minute*5:
|
|
|
|
return time.Minute
|
|
|
|
case remaining <= time.Minute*15:
|
|
|
|
return time.Minute * 5
|
|
|
|
case remaining <= time.Hour:
|
|
|
|
return time.Minute * 15
|
|
|
|
}
|
|
|
|
return time.Hour
|
|
|
|
}
|