node_example_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package node_test
  17. import (
  18. "fmt"
  19. "log"
  20. "github.com/ethereum/go-ethereum/node"
  21. "github.com/ethereum/go-ethereum/p2p"
  22. "github.com/ethereum/go-ethereum/p2p/discover"
  23. )
  24. // SampleService is a trivial network service that can be attached to a node for
  25. // life cycle management.
  26. //
  27. // The following methods are needed to implement a node.Service:
  28. // - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on
  29. // - Start() error - method invoked when the node is ready to start the service
  30. // - Stop() error - method invoked when the node terminates the service
  31. type SampleService struct{}
  32. func (s *SampleService) Protocols() []p2p.Protocol { return nil }
  33. func (s *SampleService) Start(*p2p.Server) error { fmt.Println("Service starting..."); return nil }
  34. func (s *SampleService) Stop() error { fmt.Println("Service stopping..."); return nil }
  35. func ExampleUsage() {
  36. // Create a network node to run protocols with the default values. The below list
  37. // is only used to display each of the configuration options. All of these could
  38. // have been ommited if the default behavior is desired.
  39. nodeConfig := &node.Config{
  40. DataDir: "", // Empty uses ephemeral storage
  41. PrivateKey: nil, // Nil generates a node key on the fly
  42. Name: "", // Any textual node name is allowed
  43. NoDiscovery: false, // Can disable discovering remote nodes
  44. BootstrapNodes: []*discover.Node{}, // List of bootstrap nodes to use
  45. ListenAddr: ":0", // Network interface to listen on
  46. NAT: nil, // UPnP port mapper to use for crossing firewalls
  47. Dialer: nil, // Custom dialer to use for establishing peer connections
  48. NoDial: false, // Can prevent this node from dialing out
  49. MaxPeers: 0, // Number of peers to allow
  50. MaxPendingPeers: 0, // Number of peers allowed to handshake concurrently
  51. }
  52. stack, err := node.New(nodeConfig)
  53. if err != nil {
  54. log.Fatalf("Failed to create network node: %v", err)
  55. }
  56. // Create and register a simple network service. This is done through the definition
  57. // of a node.ServiceConstructor that will instantiate a node.Service. The reason for
  58. // the factory method approach is to support service restarts without relying on the
  59. // individual implementations' support for such operations.
  60. constructor := func(context *node.ServiceContext) (node.Service, error) {
  61. return new(SampleService), nil
  62. }
  63. if err := stack.Register(constructor); err != nil {
  64. log.Fatalf("Failed to register service: %v", err)
  65. }
  66. // Boot up the entire protocol stack, do a restart and terminate
  67. if err := stack.Start(); err != nil {
  68. log.Fatalf("Failed to start the protocol stack: %v", err)
  69. }
  70. if err := stack.Restart(); err != nil {
  71. log.Fatalf("Failed to restart the protocol stack: %v", err)
  72. }
  73. if err := stack.Stop(); err != nil {
  74. log.Fatalf("Failed to stop the protocol stack: %v", err)
  75. }
  76. // Output:
  77. // Service starting...
  78. // Service stopping...
  79. // Service starting...
  80. // Service stopping...
  81. }