config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package p2p
  2. import (
  3. "blockchain-go/common/mclock"
  4. "blockchain-go/log"
  5. "blockchain-go/p2p/enode"
  6. "blockchain-go/p2p/nat"
  7. "blockchain-go/p2p/netutil"
  8. "crypto/ecdsa"
  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. // Config holds Server options.
  31. type Config struct {
  32. // This field must be set to a valid secp256k1 private key.
  33. PrivateKey *ecdsa.PrivateKey `toml:"-"`
  34. // MaxPeers is the maximum number of peers that can be
  35. // connected. It must be greater than zero.
  36. MaxPeers int
  37. // MaxPendingPeers is the maximum number of peers that can be pending in the
  38. // handshake phase, counted separately for inbound and outbound connections.
  39. // Zero defaults to preset values.
  40. MaxPendingPeers int `toml:",omitempty"`
  41. // DialRatio controls the ratio of inbound to dialed connections.
  42. // Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
  43. // Setting DialRatio to zero defaults it to 3.
  44. DialRatio int `toml:",omitempty"`
  45. // NoDiscovery can be used to disable the peer discovery mechanism.
  46. // Disabling is useful for protocol debugging (manual topology).
  47. NoDiscovery bool
  48. // DiscoveryV5 specifies whether the new topic-discovery based V5 discovery
  49. // protocol should be started or not.
  50. DiscoveryV5 bool `toml:",omitempty"`
  51. // Name sets the node name of this server.
  52. // Use common.MakeName to create a name that follows existing conventions.
  53. Name string `toml:"-"`
  54. // BootstrapNodes are used to establish connectivity
  55. // with the rest of the network.
  56. BootstrapNodes []*enode.Node
  57. // BootstrapNodesV5 are used to establish connectivity
  58. // with the rest of the network using the V5 discovery
  59. // protocol.
  60. //BootstrapNodesV5 []*enode.Node `toml:",omitempty"`
  61. // Static nodes are used as pre-configured connections which are always
  62. // maintained and re-connected on disconnects.
  63. //StaticNodes []*enode.Node
  64. // Trusted nodes are used as pre-configured connections which are always
  65. // allowed to connect, even above the peer limit.
  66. //TrustedNodes []*enode.Node
  67. // Connectivity can be restricted to certain IP networks.
  68. // If this option is set to a non-nil value, only hosts which match one of the
  69. // IP networks contained in the list are considered.
  70. NetRestrict *netutil.Netlist `toml:",omitempty"`
  71. // NodeDatabase is the path to the database containing the previously seen
  72. // live nodes in the network.
  73. NodeDatabase string `toml:",omitempty"`
  74. // Protocols should contain the protocols supported
  75. // by the server. Matching protocols are launched for
  76. // each peer.
  77. Protocols []Protocol `toml:"-"`
  78. // If ListenAddr is set to a non-nil address, the server
  79. // will listen for incoming connections.
  80. //
  81. // If the port is zero, the operating system will pick a port. The
  82. // ListenAddr field will be updated with the actual address when
  83. // the server is started.
  84. ListenAddr string
  85. // If set to a non-nil value, the given NAT port mapper
  86. // is used to make the listening port available to the
  87. // Internet.
  88. NAT nat.Interface `toml:",omitempty"`
  89. // If Dialer is set to a non-nil value, the given Dialer
  90. // is used to dial outbound peer connections.
  91. Dialer NodeDialer `toml:"-"`
  92. // If NoDial is true, the server will not dial any peers.
  93. NoDial bool `toml:",omitempty"`
  94. // If EnableMsgEvents is set then the server will emit PeerEvents
  95. // whenever a message is sent to or received from a peer
  96. EnableMsgEvents bool
  97. // Logger is a custom logger to use with the p2p.Server.
  98. Logger log.Logger `toml:",omitempty"`
  99. clock mclock.Clock
  100. }