config.go 4.2 KB

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