gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/node/node_example_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package node_test 18 19 import ( 20 "fmt" 21 "log" 22 23 "gitlab.com/aquachain/aquachain/node" 24 "gitlab.com/aquachain/aquachain/p2p" 25 "gitlab.com/aquachain/aquachain/rpc" 26 ) 27 28 // SampleService is a trivial network service that can be attached to a node for 29 // life cycle management. 30 // 31 // The following methods are needed to implement a node.Service: 32 // - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on 33 // - APIs() []rpc.API - api methods the service wants to expose on rpc channels 34 // - Start() error - method invoked when the node is ready to start the service 35 // - Stop() error - method invoked when the node terminates the service 36 type SampleService struct{} 37 38 func (s *SampleService) Protocols() []p2p.Protocol { return nil } 39 func (s *SampleService) APIs() []rpc.API { return nil } 40 func (s *SampleService) Start(*p2p.Server) error { fmt.Println("Service starting..."); return nil } 41 func (s *SampleService) Stop() error { fmt.Println("Service stopping..."); return nil } 42 43 func ExampleService() { 44 // Create a network node to run protocols with the default values. A unique chain id 45 // is required for side chains 46 minimalConfig := &node.Config{P2P: p2p.Config{ChainId: 12345}} 47 stack, err := node.New(minimalConfig) 48 49 if err != nil { 50 log.Fatalf("Failed to create network node: %v", err) 51 } 52 // Create and register a simple network service. This is done through the definition 53 // of a node.ServiceConstructor that will instantiate a node.Service. The reason for 54 // the factory method approach is to support service restarts without relying on the 55 // individual implementations' support for such operations. 56 constructor := func(context *node.ServiceContext) (node.Service, error) { 57 return new(SampleService), nil 58 } 59 if err := stack.Register(constructor); err != nil { 60 log.Fatalf("Failed to register service: %v", err) 61 } 62 // Boot up the entire protocol stack, do a restart and terminate 63 if err := stack.Start(); err != nil { 64 log.Fatalf("Failed to start the protocol stack: %v", err) 65 } 66 if err := stack.Restart(); err != nil { 67 log.Fatalf("Failed to restart the protocol stack: %v", err) 68 } 69 if err := stack.Stop(); err != nil { 70 log.Fatalf("Failed to stop the protocol stack: %v", err) 71 } 72 // Output: 73 // Service starting... 74 // Service stopping... 75 // Service starting... 76 // Service stopping... 77 }