config.go 4.1 KB

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