2013-08-06 23:55:22 +02:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2013-09-17 21:01:22 +02:00
|
|
|
"fmt"
|
2013-08-06 23:55:22 +02:00
|
|
|
"github.com/conformal/btcdb"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"net"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// supportedServices describes which services are supported by the server.
|
|
|
|
const supportedServices = btcwire.SFNodeNetwork
|
|
|
|
|
|
|
|
// connectionRetryInterval is the amount of time to wait in between retries
|
|
|
|
// when connecting to persistent peers.
|
|
|
|
const connectionRetryInterval = time.Second * 10
|
|
|
|
|
2013-08-01 19:00:14 +02:00
|
|
|
// defaultMaxOutbound is the default number of max outbound peers.
|
|
|
|
const defaultMaxOutbound = 8
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// directionString is a helper function that returns a string that represents
|
|
|
|
// the direction of a connection (inbound or outbound).
|
|
|
|
func directionString(inbound bool) string {
|
|
|
|
if inbound {
|
|
|
|
return "inbound"
|
|
|
|
}
|
|
|
|
return "outbound"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// server provides a bitcoin server for handling communications to and from
|
|
|
|
// bitcoin peers.
|
|
|
|
type server struct {
|
|
|
|
nonce uint64
|
2013-08-07 17:38:39 +02:00
|
|
|
listeners []net.Listener
|
2013-08-06 23:55:22 +02:00
|
|
|
btcnet btcwire.BitcoinNet
|
|
|
|
started bool
|
2013-10-02 02:45:21 +02:00
|
|
|
shutdown int32 // atomic
|
|
|
|
shutdownSched int32 // atomic
|
2013-08-07 07:33:46 +02:00
|
|
|
addrManager *AddrManager
|
2013-08-06 23:55:22 +02:00
|
|
|
rpcServer *rpcServer
|
|
|
|
blockManager *blockManager
|
|
|
|
newPeers chan *peer
|
|
|
|
donePeers chan *peer
|
|
|
|
banPeers chan *peer
|
2013-08-01 19:00:14 +02:00
|
|
|
wakeup chan bool
|
2013-09-09 17:58:56 +02:00
|
|
|
relayInv chan *btcwire.InvVect
|
2013-08-06 23:55:22 +02:00
|
|
|
broadcast chan broadcastMsg
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan bool
|
|
|
|
db btcdb.Db
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleAddPeerMsg deals with adding new peers. It is invoked from the
|
|
|
|
// peerHandler goroutine.
|
2013-08-01 19:00:14 +02:00
|
|
|
func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time, p *peer) bool {
|
|
|
|
if p == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// Ignore new peers if we're shutting down.
|
|
|
|
direction := directionString(p.inbound)
|
2013-10-02 02:45:21 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-08-06 23:55:22 +02:00
|
|
|
log.Infof("[SRVR] New peer %s (%s) ignored - server is "+
|
2013-08-01 19:00:14 +02:00
|
|
|
"shutting down", p.addr, direction)
|
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 {
|
|
|
|
log.Errorf("[SRVR] %v", err)
|
|
|
|
p.Shutdown()
|
2013-08-01 19:00:14 +02:00
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
if banEnd, ok := banned[host]; ok {
|
|
|
|
if time.Now().Before(banEnd) {
|
|
|
|
log.Debugf("[SRVR] Peer %s is banned for another %v - "+
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("[SRVR] Peer %s is no longer banned", host)
|
|
|
|
delete(banned, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for max peers from a single IP.
|
|
|
|
|
|
|
|
// Limit max number of total peers.
|
|
|
|
if peers.Len() >= cfg.MaxPeers {
|
|
|
|
log.Infof("[SRVR] Max peers reached [%d] - disconnecting "+
|
2013-08-01 19:00:14 +02:00
|
|
|
"peer %s (%s)", cfg.MaxPeers, p.addr, direction)
|
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-08-01 19:00:14 +02:00
|
|
|
log.Infof("[SRVR] New peer %s (%s)", p.addr, direction)
|
2013-08-06 23:55:22 +02:00
|
|
|
peers.PushBack(p)
|
2013-08-01 19:00:14 +02:00
|
|
|
if p.inbound {
|
|
|
|
p.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-01 19:00:14 +02:00
|
|
|
func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool {
|
2013-08-06 23:55:22 +02:00
|
|
|
direction := directionString(p.inbound)
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
if e.Value == p {
|
|
|
|
|
|
|
|
// Issue an asynchronous reconnect if the peer was a
|
|
|
|
// persistent outbound connection.
|
2013-10-02 02:45:21 +02:00
|
|
|
if !p.inbound && p.persistent &&
|
|
|
|
atomic.LoadInt32(&s.shutdown) == 0 {
|
2013-08-01 19:00:14 +02:00
|
|
|
// attempt reconnect.
|
|
|
|
addr := p.addr
|
|
|
|
e.Value = newOutboundPeer(s, addr, true)
|
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
peers.Remove(e)
|
|
|
|
log.Infof("[SRVR] Removed peer %s (%s)", p.addr,
|
|
|
|
direction)
|
|
|
|
return true
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
log.Warnf("[SRVR] Lost peer %v that we never had!", p)
|
|
|
|
return false
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleBanPeerMsg deals with banning peers. It is invoked from the
|
|
|
|
// peerHandler goroutine.
|
|
|
|
func (s *server) handleBanPeerMsg(banned map[string]time.Time, 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 {
|
|
|
|
log.Errorf("[SRVR] %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
direction := directionString(p.inbound)
|
|
|
|
log.Infof("[SRVR] Banned peer %s (%s) for %v", host, direction,
|
|
|
|
cfg.BanDuration)
|
|
|
|
banned[host] = time.Now().Add(cfg.BanDuration)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (s *server) handleRelayInvMsg(peers *list.List, iv *btcwire.InvVect) {
|
|
|
|
// TODO(davec): Don't relay inventory during the initial block chain
|
|
|
|
// download.
|
|
|
|
|
|
|
|
// Loop through all connected peers and relay the inventory to those
|
|
|
|
// which are not already known to have it.
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
p := e.Value.(*peer)
|
2013-09-18 00:39:49 +02:00
|
|
|
if p.conn == nil {
|
|
|
|
continue
|
|
|
|
}
|
2013-09-09 17:58:56 +02:00
|
|
|
|
|
|
|
// Queue the inventory to be relayed with the next batch. It
|
|
|
|
// will be ignored if the peer is already known to have the
|
|
|
|
// inventory.
|
|
|
|
p.QueueInventory(iv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
|
|
|
|
// from the peerHandler goroutine.
|
|
|
|
func (s *server) handleBroadcastMsg(peers *list.List, bmsg *broadcastMsg) {
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
excluded := false
|
|
|
|
for _, p := range bmsg.excludePeers {
|
|
|
|
if e.Value == p {
|
|
|
|
excluded = true
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
p := e.Value.(*peer)
|
|
|
|
// Don't broadcast to still connecting outbound peers .
|
|
|
|
if p.conn == nil {
|
|
|
|
excluded = true
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
if !excluded {
|
|
|
|
p.QueueMessage(bmsg.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
log.Infof("[SRVR] 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-08-06 23:55:22 +02:00
|
|
|
log.Errorf("[SRVR] %v", err)
|
|
|
|
}
|
|
|
|
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-08-07 17:38:39 +02:00
|
|
|
log.Tracef("[SRVR] Listener handler done for %s", listener.Addr())
|
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-08-07 17:38:39 +02:00
|
|
|
log.Tracef("[SRVR] Starting peer handler")
|
2013-08-06 23:55:22 +02:00
|
|
|
peers := list.New()
|
|
|
|
bannedPeers := make(map[string]time.Time)
|
2013-08-01 19:00:14 +02:00
|
|
|
outboundPeers := 0
|
|
|
|
maxOutbound := defaultMaxOutbound
|
|
|
|
if cfg.MaxPeers < maxOutbound {
|
|
|
|
maxOutbound = cfg.MaxPeers
|
|
|
|
}
|
2013-09-18 07:36:40 +02:00
|
|
|
var wakeupTimer *time.Timer
|
2013-08-01 19:00:14 +02:00
|
|
|
|
|
|
|
// Do initial DNS seeding to populate address manager.
|
|
|
|
if !cfg.DisableDNSSeed {
|
|
|
|
proxy := ""
|
|
|
|
if cfg.Proxy != "" && cfg.UseTor {
|
|
|
|
proxy = cfg.Proxy
|
|
|
|
}
|
|
|
|
for _, seeder := range activeNetParams.dnsSeeds {
|
|
|
|
seedpeers := dnsDiscover(seeder, proxy)
|
|
|
|
if len(seedpeers) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addresses := make([]*btcwire.NetAddress, len(seedpeers))
|
|
|
|
// if this errors then we have *real* problems
|
|
|
|
intPort, _ := strconv.Atoi(activeNetParams.peerPort)
|
|
|
|
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+
|
|
|
|
s.addrManager.rand.Int31n(secondsIn4Days)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
}
|
|
|
|
// XXX if this is empty do we want to use hardcoded
|
|
|
|
// XXX peers like bitcoind does?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start up persistent peers.
|
|
|
|
permanentPeers := cfg.ConnectPeers
|
|
|
|
if len(permanentPeers) == 0 {
|
|
|
|
permanentPeers = cfg.AddPeers
|
|
|
|
}
|
|
|
|
for _, addr := range permanentPeers {
|
|
|
|
if s.handleAddPeerMsg(peers, bannedPeers,
|
|
|
|
newOutboundPeer(s, addr, true)) {
|
|
|
|
outboundPeers++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing else happens, wake us up soon.
|
|
|
|
time.AfterFunc(10*time.Second, func() { s.wakeup <- true })
|
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-08-01 19:00:14 +02:00
|
|
|
if s.handleAddPeerMsg(peers, bannedPeers, p) &&
|
|
|
|
!p.inbound {
|
|
|
|
outboundPeers++
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// Disconnected peers.
|
|
|
|
case p := <-s.donePeers:
|
2013-08-01 19:00:14 +02:00
|
|
|
// handleDonePeerMsg return true if it removed a peer
|
|
|
|
if s.handleDonePeerMsg(peers, p) {
|
|
|
|
outboundPeers--
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
// Peer to ban.
|
|
|
|
case p := <-s.banPeers:
|
|
|
|
s.handleBanPeerMsg(bannedPeers, p)
|
|
|
|
|
2013-09-09 17:58:56 +02:00
|
|
|
// New inventory to potentially be relayed to other peers.
|
|
|
|
case invMsg := <-s.relayInv:
|
|
|
|
s.handleRelayInvMsg(peers, invMsg)
|
|
|
|
|
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:
|
|
|
|
s.handleBroadcastMsg(peers, &bmsg)
|
|
|
|
|
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-08-06 23:55:22 +02:00
|
|
|
// Shutdown the peer handler.
|
|
|
|
case <-s.quit:
|
|
|
|
// Shutdown peers.
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
p := e.Value.(*peer)
|
|
|
|
p.Shutdown()
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Timer was just to make sure we woke up again soon. so cancel
|
|
|
|
// and remove it as soon as we next come around.
|
|
|
|
if wakeupTimer != nil {
|
|
|
|
wakeupTimer.Stop()
|
|
|
|
wakeupTimer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only try connect to more peers if we actually need more
|
2013-10-02 02:45:21 +02:00
|
|
|
if outboundPeers >= maxOutbound ||
|
|
|
|
atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-08-01 19:00:14 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
groups := make(map[string]int)
|
|
|
|
for e := peers.Front(); e != nil; e = e.Next() {
|
|
|
|
peer := e.Value.(*peer)
|
|
|
|
if !peer.inbound {
|
|
|
|
groups[GroupKey(peer.na)]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:01:22 +02:00
|
|
|
tries := 0
|
|
|
|
for outboundPeers < maxOutbound &&
|
2013-10-02 02:45:21 +02:00
|
|
|
peers.Len() < cfg.MaxPeers &&
|
|
|
|
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.
|
|
|
|
|
|
|
|
nPeers := outboundPeers
|
|
|
|
if nPeers > 8 {
|
|
|
|
nPeers = 8
|
|
|
|
}
|
|
|
|
addr := s.addrManager.GetAddress("any", 10+nPeers*10)
|
|
|
|
if addr == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
key := GroupKey(addr.na)
|
|
|
|
// 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
|
|
|
|
// others. bitcoind breaks out of the loop here, but
|
|
|
|
// we continue to try other addresses.
|
|
|
|
if groups[key] != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if time.Now().After(addr.lastattempt.Add(10*time.Minute)) &&
|
|
|
|
tries < 30 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow nondefault ports after 50 failed tries.
|
|
|
|
if fmt.Sprintf("%d", addr.na.Port) !=
|
|
|
|
activeNetParams.peerPort && tries < 50 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addrStr := NetAddressKey(addr.na)
|
|
|
|
|
|
|
|
tries = 0
|
|
|
|
// any failure will be due to banned peers etc. we have
|
|
|
|
// already checked that we have room for more peers.
|
|
|
|
if s.handleAddPeerMsg(peers, bannedPeers,
|
|
|
|
newOutboundPeer(s, addrStr, false)) {
|
|
|
|
outboundPeers++
|
|
|
|
groups[key]++
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
|
|
|
|
// We we need more peers, wake up in ten seconds and try again.
|
2013-09-17 21:01:22 +02:00
|
|
|
if outboundPeers < maxOutbound && peers.Len() < cfg.MaxPeers {
|
|
|
|
time.AfterFunc(10*time.Second, func() {
|
|
|
|
s.wakeup <- true
|
|
|
|
})
|
|
|
|
}
|
2013-08-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if wakeupTimer != nil {
|
|
|
|
wakeupTimer.Stop()
|
|
|
|
wakeupTimer = nil
|
2013-08-06 23:55:22 +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-08-07 17:38:39 +02:00
|
|
|
log.Tracef("[SRVR] 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start begins accepting connections from peers.
|
|
|
|
func (s *server) Start() {
|
|
|
|
// Already started?
|
|
|
|
if s.started {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("[SRVR] 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()
|
2013-08-09 21:32:22 +02:00
|
|
|
|
|
|
|
// Start the RPC server if it's not disabled.
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2013-08-06 23:55:22 +02:00
|
|
|
s.rpcServer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.started = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-06 23:55:22 +02:00
|
|
|
log.Infof("[SRVR] Server is already in the process of shutting down")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("[SRVR] 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
|
|
|
|
|
|
|
// 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()
|
|
|
|
log.Infof("[SRVR] Server shutdown complete")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
log.Warnf("[SRVR] Server shutdown in %v", duration)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
log.Warnf("[SRVR] Server shutdown in %v", remaining)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newServer returns a new btcd server configured to listen on addr for the
|
|
|
|
// bitcoin network type specified in btcnet. Use start to begin accepting
|
|
|
|
// connections from peers.
|
|
|
|
func newServer(addr string, db btcdb.Db, btcnet btcwire.BitcoinNet) (*server, error) {
|
|
|
|
nonce, err := btcwire.RandomUint64()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:38:39 +02:00
|
|
|
var listeners []net.Listener
|
2013-08-07 19:35:01 +02:00
|
|
|
if !cfg.DisableListen {
|
|
|
|
// IPv4 listener.
|
|
|
|
listener4, err := net.Listen("tcp4", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener4)
|
2013-08-07 17:38:39 +02:00
|
|
|
|
2013-08-07 19:35:01 +02:00
|
|
|
// IPv6 listener.
|
|
|
|
listener6, err := net.Listen("tcp6", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener6)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s := server{
|
|
|
|
nonce: nonce,
|
2013-08-07 17:38:39 +02:00
|
|
|
listeners: listeners,
|
2013-08-06 23:55:22 +02:00
|
|
|
btcnet: btcnet,
|
2013-08-07 07:33:46 +02:00
|
|
|
addrManager: NewAddrManager(),
|
2013-08-06 23:55:22 +02:00
|
|
|
newPeers: make(chan *peer, cfg.MaxPeers),
|
|
|
|
donePeers: make(chan *peer, cfg.MaxPeers),
|
|
|
|
banPeers: make(chan *peer, cfg.MaxPeers),
|
2013-08-01 19:00:14 +02:00
|
|
|
wakeup: make(chan bool),
|
2013-09-09 17:58:56 +02:00
|
|
|
relayInv: make(chan *btcwire.InvVect, cfg.MaxPeers),
|
2013-08-06 23:55:22 +02:00
|
|
|
broadcast: make(chan broadcastMsg, cfg.MaxPeers),
|
|
|
|
quit: make(chan bool),
|
|
|
|
db: db,
|
|
|
|
}
|
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-08-06 23:55:22 +02:00
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
if !cfg.DisableRPC {
|
2013-09-18 18:25:18 +02:00
|
|
|
s.rpcServer, err = newRPCServer(&s)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|