node_example_test.go 4.1 KB

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