github.com/jimmyx0x/go-ethereum@v1.10.28/node/utils_test.go (about)

     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  
    17  // Contains a batch of utility type declarations used by the tests. As the node
    18  // operates on unique types, a lot of them are needed to check various features.
    19  
    20  package node
    21  
    22  import (
    23  	"github.com/ethereum/go-ethereum/p2p"
    24  	"github.com/ethereum/go-ethereum/rpc"
    25  )
    26  
    27  // NoopLifecycle is a trivial implementation of the Service interface.
    28  type NoopLifecycle struct{}
    29  
    30  func (s *NoopLifecycle) Start() error { return nil }
    31  func (s *NoopLifecycle) Stop() error  { return nil }
    32  
    33  func NewNoop() *Noop {
    34  	noop := new(Noop)
    35  	return noop
    36  }
    37  
    38  // Set of services all wrapping the base NoopLifecycle resulting in the same method
    39  // signatures but different outer types.
    40  type Noop struct{ NoopLifecycle }
    41  
    42  // InstrumentedService is an implementation of Lifecycle for which all interface
    43  // methods can be instrumented both return value as well as event hook wise.
    44  type InstrumentedService struct {
    45  	start error
    46  	stop  error
    47  
    48  	startHook func()
    49  	stopHook  func()
    50  }
    51  
    52  func (s *InstrumentedService) Start() error {
    53  	if s.startHook != nil {
    54  		s.startHook()
    55  	}
    56  	return s.start
    57  }
    58  
    59  func (s *InstrumentedService) Stop() error {
    60  	if s.stopHook != nil {
    61  		s.stopHook()
    62  	}
    63  	return s.stop
    64  }
    65  
    66  type FullService struct{}
    67  
    68  func NewFullService(stack *Node) (*FullService, error) {
    69  	fs := new(FullService)
    70  
    71  	stack.RegisterProtocols(fs.Protocols())
    72  	stack.RegisterAPIs(fs.APIs())
    73  	stack.RegisterLifecycle(fs)
    74  	return fs, nil
    75  }
    76  
    77  func (f *FullService) Start() error { return nil }
    78  
    79  func (f *FullService) Stop() error { return nil }
    80  
    81  func (f *FullService) Protocols() []p2p.Protocol {
    82  	return []p2p.Protocol{
    83  		{
    84  			Name:    "test1",
    85  			Version: uint(1),
    86  		},
    87  		{
    88  			Name:    "test2",
    89  			Version: uint(2),
    90  		},
    91  	}
    92  }
    93  
    94  func (f *FullService) APIs() []rpc.API {
    95  	return []rpc.API{
    96  		{
    97  			Namespace: "admin",
    98  		},
    99  		{
   100  			Namespace: "debug",
   101  		},
   102  		{
   103  			Namespace: "net",
   104  		},
   105  	}
   106  }