utils_test.go 2.7 KB

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