github.com/klaytn/klaytn@v1.12.1/node/utils_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of go-ethereum. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from node/utils_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 // Contains a batch of utility type declarations used by the tests. As the node 22 // operates on unique types, a lot of them are needed to check various features. 23 24 package node 25 26 import ( 27 "reflect" 28 29 "github.com/klaytn/klaytn/networks/p2p" 30 "github.com/klaytn/klaytn/networks/rpc" 31 ) 32 33 // NoopService is a trivial implementation of the Service interface. 34 type NoopService struct{} 35 36 func (s *NoopService) Protocols() []p2p.Protocol { return nil } 37 func (s *NoopService) APIs() []rpc.API { return nil } 38 func (s *NoopService) Start(p2p.Server) error { return nil } 39 func (s *NoopService) Stop() error { return nil } 40 func (s *NoopService) Components() []interface{} { return nil } 41 func (s *NoopService) SetComponents(components []interface{}) {} 42 43 func NewNoopService(*ServiceContext) (Service, error) { return new(NoopService), nil } 44 45 // Set of services all wrapping the base NoopService resulting in the same method 46 // signatures but different outer types. 47 type NoopServiceA struct{ NoopService } 48 49 type ( 50 NoopServiceB struct{ NoopService } 51 NoopServiceC struct{ NoopService } 52 ) 53 54 func NewNoopServiceA(*ServiceContext) (Service, error) { return new(NoopServiceA), nil } 55 func NewNoopServiceB(*ServiceContext) (Service, error) { return new(NoopServiceB), nil } 56 func NewNoopServiceC(*ServiceContext) (Service, error) { return new(NoopServiceC), nil } 57 58 // InstrumentedService is an implementation of Service for which all interface 59 // methods can be instrumented both return value as well as event hook wise. 60 type InstrumentedService struct { 61 protocols []p2p.Protocol 62 apis []rpc.API 63 start error 64 stop error 65 66 protocolsHook func() 67 startHook func(p2p.Server) 68 stopHook func() 69 } 70 71 func NewInstrumentedService(*ServiceContext) (Service, error) { return new(InstrumentedService), nil } 72 73 func (s *InstrumentedService) Protocols() []p2p.Protocol { 74 if s.protocolsHook != nil { 75 s.protocolsHook() 76 } 77 return s.protocols 78 } 79 80 func (s *InstrumentedService) APIs() []rpc.API { 81 return s.apis 82 } 83 84 func (s *InstrumentedService) Start(server p2p.Server) error { 85 if s.startHook != nil { 86 s.startHook(server) 87 } 88 return s.start 89 } 90 91 func (s *InstrumentedService) Stop() error { 92 if s.stopHook != nil { 93 s.stopHook() 94 } 95 return s.stop 96 } 97 98 func (s *InstrumentedService) Components() []interface{} { 99 return nil 100 } 101 102 func (s *InstrumentedService) SetComponents(components []interface{}) { 103 } 104 105 // InstrumentingWrapper is a method to specialize a service constructor returning 106 // a generic InstrumentedService into one returning a wrapping specific one. 107 type InstrumentingWrapper func(base ServiceConstructor) ServiceConstructor 108 109 func InstrumentingWrapperMaker(base ServiceConstructor, kind reflect.Type) ServiceConstructor { 110 return func(ctx *ServiceContext) (Service, error) { 111 obj, err := base(ctx) 112 if err != nil { 113 return nil, err 114 } 115 wrapper := reflect.New(kind) 116 wrapper.Elem().Field(0).Set(reflect.ValueOf(obj).Elem()) 117 118 return wrapper.Interface().(Service), nil 119 } 120 } 121 122 // Set of services all wrapping the base InstrumentedService resulting in the 123 // same method signatures but different outer types. 124 type InstrumentedServiceA struct{ InstrumentedService } 125 126 type ( 127 InstrumentedServiceB struct{ InstrumentedService } 128 InstrumentedServiceC struct{ InstrumentedService } 129 ) 130 131 func InstrumentedServiceMakerA(base ServiceConstructor) ServiceConstructor { 132 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceA{})) 133 } 134 135 func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor { 136 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceB{})) 137 } 138 139 func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor { 140 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{})) 141 } 142 143 // OneMethodApi is a single-method API handler to be returned by test services. 144 type OneMethodApi struct { 145 fun func() 146 } 147 148 func (api *OneMethodApi) TheOneMethod() { 149 if api.fun != nil { 150 api.fun() 151 } 152 }