node_example_test.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. rpc "github.com/ethereum/go-ethereum/rpc/v2"
  24. )
  25. // SampleService is a trivial network service that can be attached to a node for
  26. // life cycle management.
  27. //
  28. // The following methods are needed to implement a node.Service:
  29. // - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on
  30. // - Start() error - method invoked when the node is ready to start the service
  31. // - Stop() error - method invoked when the node terminates the service
  32. type SampleService struct{}
  33. func (s *SampleService) Protocols() []p2p.Protocol { return nil }
  34. func (s *SampleService) APIs() []rpc.API { return nil }
  35. func (s *SampleService) Start(*p2p.Server) error { fmt.Println("Service starting..."); return nil }
  36. func (s *SampleService) Stop() error { fmt.Println("Service stopping..."); return nil }
  37. func ExampleUsage() {
  38. // Create a network node to run protocols with the default values. The below list
  39. // is only used to display each of the configuration options. All of these could
  40. // have been ommited if the default behavior is desired.
  41. nodeConfig := &node.Config{
  42. DataDir: "", // Empty uses ephemeral storage
  43. PrivateKey: nil, // Nil generates a node key on the fly
  44. Name: "", // Any textual node name is allowed
  45. NoDiscovery: false, // Can disable discovering remote nodes
  46. BootstrapNodes: []*discover.Node{}, // List of bootstrap nodes to use
  47. ListenAddr: ":0", // Network interface to listen on
  48. NAT: nil, // UPnP port mapper to use for crossing firewalls
  49. Dialer: nil, // Custom dialer to use for establishing peer connections
  50. NoDial: false, // Can prevent this node from dialing out
  51. MaxPeers: 0, // Number of peers to allow
  52. MaxPendingPeers: 0, // Number of peers allowed to handshake concurrently
  53. }
  54. stack, err := node.New(nodeConfig)
  55. if err != nil {
  56. log.Fatalf("Failed to create network node: %v", err)
  57. }
  58. // Create and register a simple network service. This is done through the definition
  59. // of a node.ServiceConstructor that will instantiate a node.Service. The reason for
  60. // the factory method approach is to support service restarts without relying on the
  61. // individual implementations' support for such operations.
  62. constructor := func(context *node.ServiceContext) (node.Service, error) {
  63. return new(SampleService), nil
  64. }
  65. if err := stack.Register(constructor); err != nil {
  66. log.Fatalf("Failed to register service: %v", err)
  67. }
  68. // Boot up the entire protocol stack, do a restart and terminate
  69. if err := stack.Start(); err != nil {
  70. log.Fatalf("Failed to start the protocol stack: %v", err)
  71. }
  72. if err := stack.Restart(); err != nil {
  73. log.Fatalf("Failed to restart the protocol stack: %v", err)
  74. }
  75. if err := stack.Stop(); err != nil {
  76. log.Fatalf("Failed to stop the protocol stack: %v", err)
  77. }
  78. // Output:
  79. // Service starting...
  80. // Service stopping...
  81. // Service starting...
  82. // Service stopping...
  83. }