config.go 4.1 KB

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