config.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package p2p
  2. import (
  3. "blockchain-go/common/mclock"
  4. "blockchain-go/log"
  5. "crypto/ecdsa"
  6. "errors"
  7. "net"
  8. "sync"
  9. "time"
  10. )
  11. const (
  12. defaultDialTimeout = 15 * time.Second
  13. // This is the fairness knob for the discovery mixer. When looking for peers, we'll
  14. // wait this long for a single source of candidates before moving on and trying other
  15. // sources.
  16. discmixTimeout = 5 * time.Second
  17. // Connectivity defaults.
  18. defaultMaxPendingPeers = 50
  19. defaultDialRatio = 3
  20. // This time limits inbound connection attempts per source IP.
  21. inboundThrottleTime = 30 * time.Second
  22. // Maximum time allowed for reading a complete message.
  23. // This is effectively the amount of time a connection can be idle.
  24. frameReadTimeout = 30 * time.Second
  25. // Maximum amount of time allowed for writing a complete message.
  26. frameWriteTimeout = 20 * time.Second
  27. // Maximum time to wait before stop the p2p server
  28. stopTimeout = 5 * time.Second
  29. )
  30. var errServerStopped = errors.New("server stopped")
  31. // Config holds Server options.
  32. type Config struct {
  33. // This field must be set to a valid secp256k1 private key.
  34. PrivateKey *ecdsa.PrivateKey `toml:"-"`
  35. // MaxPeers is the maximum number of peers that can be
  36. // connected. It must be greater than zero.
  37. MaxPeers int
  38. // MaxPendingPeers is the maximum number of peers that can be pending in the
  39. // handshake phase, counted separately for inbound and outbound connections.
  40. // Zero defaults to preset values.
  41. MaxPendingPeers int `toml:",omitempty"`
  42. // DialRatio controls the ratio of inbound to dialed connections.
  43. // Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
  44. // Setting DialRatio to zero defaults it to 3.
  45. DialRatio int `toml:",omitempty"`
  46. // NoDiscovery can be used to disable the peer discovery mechanism.
  47. // Disabling is useful for protocol debugging (manual topology).
  48. NoDiscovery bool
  49. // DiscoveryV5 specifies whether the new topic-discovery based V5 discovery
  50. // protocol should be started or not.
  51. DiscoveryV5 bool `toml:",omitempty"`
  52. // Name sets the node name of this server.
  53. // Use common.MakeName to create a name that follows existing conventions.
  54. Name string `toml:"-"`
  55. // BootstrapNodes are used to establish connectivity
  56. // with the rest of the network.
  57. //BootstrapNodes []*enode.Node
  58. // BootstrapNodesV5 are used to establish connectivity
  59. // with the rest of the network using the V5 discovery
  60. // protocol.
  61. //BootstrapNodesV5 []*enode.Node `toml:",omitempty"`
  62. // Static nodes are used as pre-configured connections which are always
  63. // maintained and re-connected on disconnects.
  64. //StaticNodes []*enode.Node
  65. // Trusted nodes are used as pre-configured connections which are always
  66. // allowed to connect, even above the peer limit.
  67. //TrustedNodes []*enode.Node
  68. // Connectivity can be restricted to certain IP networks.
  69. // If this option is set to a non-nil value, only hosts which match one of the
  70. // IP networks contained in the list are considered.
  71. //NetRestrict *netutil.Netlist `toml:",omitempty"`
  72. // NodeDatabase is the path to the database containing the previously seen
  73. // live nodes in the network.
  74. NodeDatabase string `toml:",omitempty"`
  75. // Protocols should contain the protocols supported
  76. // by the server. Matching protocols are launched for
  77. // each peer.
  78. //Protocols []Protocol `toml:"-"`
  79. // If ListenAddr is set to a non-nil address, the server
  80. // will listen for incoming connections.
  81. //
  82. // If the port is zero, the operating system will pick a port. The
  83. // ListenAddr field will be updated with the actual address when
  84. // the server is started.
  85. ListenAddr string
  86. // If set to a non-nil value, the given NAT port mapper
  87. // is used to make the listening port available to the
  88. // Internet.
  89. //NAT nat.Interface `toml:",omitempty"`
  90. // If Dialer is set to a non-nil value, the given Dialer
  91. // is used to dial outbound peer connections.
  92. //Dialer NodeDialer `toml:"-"`
  93. // If NoDial is true, the server will not dial any peers.
  94. NoDial bool `toml:",omitempty"`
  95. // If EnableMsgEvents is set then the server will emit PeerEvents
  96. // whenever a message is sent to or received from a peer
  97. EnableMsgEvents bool
  98. // Logger is a custom logger to use with the p2p.Server.
  99. Logger log.Logger `toml:",omitempty"`
  100. clock mclock.Clock
  101. }
  102. // Server manages all peer connections.
  103. type Server struct {
  104. // Config fields may not be modified while the server is running.
  105. Config
  106. // Hooks for testing. These are useful because we can inhibit
  107. // the whole protocol stack.
  108. //newTransport func(net.Conn, *ecdsa.PublicKey) transport
  109. //newPeerHook func(*Peer)
  110. listenFunc func(network, addr string) (net.Listener, error)
  111. lock sync.Mutex // protects running
  112. running bool
  113. listener net.Listener
  114. //ourHandshake *protoHandshake
  115. loopWG sync.WaitGroup // loop, listenLoop
  116. //peerFeed event.Feed
  117. log log.Logger
  118. //nodedb *enode.DB
  119. //localnode *enode.LocalNode
  120. //ntab *discover.UDPv4
  121. //DiscV5 *discover.UDPv5
  122. //discmix *enode.FairMix
  123. //dialsched *dialScheduler
  124. // Channels into the run loop.
  125. quit chan struct{}
  126. //addtrusted chan *enode.Node
  127. //removetrusted chan *enode.Node
  128. //peerOp chan peerOpFunc
  129. peerOpDone chan struct{}
  130. //delpeer chan peerDrop
  131. //checkpointPostHandshake chan *conn
  132. //checkpointAddPeer chan *conn
  133. // State of run loop and listenLoop.
  134. //inboundHistory expHeap
  135. }