2015-11-26 16:35:44 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Contains a batch of utility type declarations used by the tests. As the node
|
|
|
|
// operates on unique types, a lot of them are needed to check various features.
|
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-12-16 09:58:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-11-26 16:35:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NoopService is a trivial implementation of the Service interface.
|
|
|
|
type NoopService struct{}
|
|
|
|
|
|
|
|
func (s *NoopService) Protocols() []p2p.Protocol { return nil }
|
2015-10-15 14:07:19 +00:00
|
|
|
func (s *NoopService) APIs() []rpc.API { return nil }
|
2015-11-26 16:35:44 +00:00
|
|
|
func (s *NoopService) Start(*p2p.Server) error { return nil }
|
|
|
|
func (s *NoopService) Stop() error { return nil }
|
|
|
|
|
|
|
|
func NewNoopService(*ServiceContext) (Service, error) { return new(NoopService), nil }
|
|
|
|
|
|
|
|
// Set of services all wrapping the base NoopService resulting in the same method
|
|
|
|
// signatures but different outer types.
|
|
|
|
type NoopServiceA struct{ NoopService }
|
|
|
|
type NoopServiceB struct{ NoopService }
|
|
|
|
type NoopServiceC struct{ NoopService }
|
|
|
|
|
|
|
|
func NewNoopServiceA(*ServiceContext) (Service, error) { return new(NoopServiceA), nil }
|
|
|
|
func NewNoopServiceB(*ServiceContext) (Service, error) { return new(NoopServiceB), nil }
|
|
|
|
func NewNoopServiceC(*ServiceContext) (Service, error) { return new(NoopServiceC), nil }
|
|
|
|
|
|
|
|
// InstrumentedService is an implementation of Service for which all interface
|
|
|
|
// methods can be instrumented both return value as well as event hook wise.
|
|
|
|
type InstrumentedService struct {
|
|
|
|
protocols []p2p.Protocol
|
2016-02-02 17:06:43 +00:00
|
|
|
apis []rpc.API
|
2015-11-26 16:35:44 +00:00
|
|
|
start error
|
|
|
|
stop error
|
|
|
|
|
|
|
|
protocolsHook func()
|
|
|
|
startHook func(*p2p.Server)
|
|
|
|
stopHook func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstrumentedService(*ServiceContext) (Service, error) { return new(InstrumentedService), nil }
|
|
|
|
|
|
|
|
func (s *InstrumentedService) Protocols() []p2p.Protocol {
|
|
|
|
if s.protocolsHook != nil {
|
|
|
|
s.protocolsHook()
|
|
|
|
}
|
|
|
|
return s.protocols
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:07:19 +00:00
|
|
|
func (s *InstrumentedService) APIs() []rpc.API {
|
2016-02-02 17:06:43 +00:00
|
|
|
return s.apis
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 16:35:44 +00:00
|
|
|
func (s *InstrumentedService) Start(server *p2p.Server) error {
|
|
|
|
if s.startHook != nil {
|
|
|
|
s.startHook(server)
|
|
|
|
}
|
|
|
|
return s.start
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InstrumentedService) Stop() error {
|
|
|
|
if s.stopHook != nil {
|
|
|
|
s.stopHook()
|
|
|
|
}
|
|
|
|
return s.stop
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentingWrapper is a method to specialize a service constructor returning
|
|
|
|
// a generic InstrumentedService into one returning a wrapping specific one.
|
|
|
|
type InstrumentingWrapper func(base ServiceConstructor) ServiceConstructor
|
|
|
|
|
|
|
|
func InstrumentingWrapperMaker(base ServiceConstructor, kind reflect.Type) ServiceConstructor {
|
|
|
|
return func(ctx *ServiceContext) (Service, error) {
|
|
|
|
obj, err := base(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrapper := reflect.New(kind)
|
|
|
|
wrapper.Elem().Field(0).Set(reflect.ValueOf(obj).Elem())
|
|
|
|
|
|
|
|
return wrapper.Interface().(Service), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set of services all wrapping the base InstrumentedService resulting in the
|
|
|
|
// same method signatures but different outer types.
|
|
|
|
type InstrumentedServiceA struct{ InstrumentedService }
|
|
|
|
type InstrumentedServiceB struct{ InstrumentedService }
|
|
|
|
type InstrumentedServiceC struct{ InstrumentedService }
|
|
|
|
|
|
|
|
func InstrumentedServiceMakerA(base ServiceConstructor) ServiceConstructor {
|
|
|
|
return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceA{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor {
|
|
|
|
return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceB{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor {
|
|
|
|
return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{}))
|
|
|
|
}
|
2016-02-02 17:06:43 +00:00
|
|
|
|
2018-05-22 07:29:41 +00:00
|
|
|
// OneMethodAPI is a single-method API handler to be returned by test services.
|
|
|
|
type OneMethodAPI struct {
|
2016-02-02 17:06:43 +00:00
|
|
|
fun func()
|
|
|
|
}
|
|
|
|
|
2018-05-22 07:29:41 +00:00
|
|
|
func (api *OneMethodAPI) TheOneMethod() {
|
2016-02-02 17:06:43 +00:00
|
|
|
if api.fun != nil {
|
|
|
|
api.fun()
|
|
|
|
}
|
|
|
|
}
|