github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/p2p/simulations/adapters/inproc.go (about) 1 // Copyright 2017 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 package adapters 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "maps" 24 "math" 25 "net" 26 "sync" 27 28 "github.com/ethereum/go-ethereum/event" 29 "github.com/ethereum/go-ethereum/log" 30 "github.com/ethereum/go-ethereum/node" 31 "github.com/ethereum/go-ethereum/p2p" 32 "github.com/ethereum/go-ethereum/p2p/enode" 33 "github.com/ethereum/go-ethereum/p2p/simulations/pipes" 34 "github.com/ethereum/go-ethereum/rpc" 35 "github.com/gorilla/websocket" 36 ) 37 38 // SimAdapter is a NodeAdapter which creates in-memory simulation nodes and 39 // connects them using net.Pipe 40 type SimAdapter struct { 41 pipe func() (net.Conn, net.Conn, error) 42 mtx sync.RWMutex 43 nodes map[enode.ID]*SimNode 44 lifecycles LifecycleConstructors 45 } 46 47 // NewSimAdapter creates a SimAdapter which is capable of running in-memory 48 // simulation nodes running any of the given services (the services to run on a 49 // particular node are passed to the NewNode function in the NodeConfig) 50 // the adapter uses a net.Pipe for in-memory simulated network connections 51 func NewSimAdapter(services LifecycleConstructors) *SimAdapter { 52 return &SimAdapter{ 53 pipe: pipes.NetPipe, 54 nodes: make(map[enode.ID]*SimNode), 55 lifecycles: services, 56 } 57 } 58 59 // Name returns the name of the adapter for logging purposes 60 func (s *SimAdapter) Name() string { 61 return "sim-adapter" 62 } 63 64 // NewNode returns a new SimNode using the given config 65 func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) { 66 s.mtx.Lock() 67 defer s.mtx.Unlock() 68 69 id := config.ID 70 // verify that the node has a private key in the config 71 if config.PrivateKey == nil { 72 return nil, fmt.Errorf("node is missing private key: %s", id) 73 } 74 75 // check a node with the ID doesn't already exist 76 if _, exists := s.nodes[id]; exists { 77 return nil, fmt.Errorf("node already exists: %s", id) 78 } 79 80 // check the services are valid 81 if len(config.Lifecycles) == 0 { 82 return nil, errors.New("node must have at least one service") 83 } 84 for _, service := range config.Lifecycles { 85 if _, exists := s.lifecycles[service]; !exists { 86 return nil, fmt.Errorf("unknown node service %q", service) 87 } 88 } 89 90 err := config.initDummyEnode() 91 if err != nil { 92 return nil, err 93 } 94 95 n, err := node.New(&node.Config{ 96 P2P: p2p.Config{ 97 PrivateKey: config.PrivateKey, 98 MaxPeers: math.MaxInt32, 99 NoDiscovery: true, 100 Dialer: s, 101 EnableMsgEvents: config.EnableMsgEvents, 102 }, 103 ExternalSigner: config.ExternalSigner, 104 Logger: log.New("node.id", id.String()), 105 }) 106 if err != nil { 107 return nil, err 108 } 109 110 simNode := &SimNode{ 111 ID: id, 112 config: config, 113 node: n, 114 adapter: s, 115 running: make(map[string]node.Lifecycle), 116 } 117 s.nodes[id] = simNode 118 return simNode, nil 119 } 120 121 // Dial implements the p2p.NodeDialer interface by connecting to the node using 122 // an in-memory net.Pipe 123 func (s *SimAdapter) Dial(ctx context.Context, dest *enode.Node) (conn net.Conn, err error) { 124 node, ok := s.GetNode(dest.ID()) 125 if !ok { 126 return nil, fmt.Errorf("unknown node: %s", dest.ID()) 127 } 128 srv := node.Server() 129 if srv == nil { 130 return nil, fmt.Errorf("node not running: %s", dest.ID()) 131 } 132 // SimAdapter.pipe is net.Pipe (NewSimAdapter) 133 pipe1, pipe2, err := s.pipe() 134 if err != nil { 135 return nil, err 136 } 137 // this is simulated 'listening' 138 // asynchronously call the dialed destination node's p2p server 139 // to set up connection on the 'listening' side 140 go srv.SetupConn(pipe1, 0, nil) 141 return pipe2, nil 142 } 143 144 // DialRPC implements the RPCDialer interface by creating an in-memory RPC 145 // client of the given node 146 func (s *SimAdapter) DialRPC(id enode.ID) (*rpc.Client, error) { 147 node, ok := s.GetNode(id) 148 if !ok { 149 return nil, fmt.Errorf("unknown node: %s", id) 150 } 151 return node.node.Attach(), nil 152 } 153 154 // GetNode returns the node with the given ID if it exists 155 func (s *SimAdapter) GetNode(id enode.ID) (*SimNode, bool) { 156 s.mtx.RLock() 157 defer s.mtx.RUnlock() 158 node, ok := s.nodes[id] 159 return node, ok 160 } 161 162 // SimNode is an in-memory simulation node which connects to other nodes using 163 // net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that 164 // pipe 165 type SimNode struct { 166 lock sync.RWMutex 167 ID enode.ID 168 config *NodeConfig 169 adapter *SimAdapter 170 node *node.Node 171 running map[string]node.Lifecycle 172 client *rpc.Client 173 registerOnce sync.Once 174 } 175 176 // Close closes the underlying node.Node to release 177 // acquired resources. 178 func (sn *SimNode) Close() error { 179 return sn.node.Close() 180 } 181 182 // Addr returns the node's discovery address 183 func (sn *SimNode) Addr() []byte { 184 return []byte(sn.Node().String()) 185 } 186 187 // Node returns a node descriptor representing the SimNode 188 func (sn *SimNode) Node() *enode.Node { 189 return sn.config.Node() 190 } 191 192 // Client returns an rpc.Client which can be used to communicate with the 193 // underlying services (it is set once the node has started) 194 func (sn *SimNode) Client() (*rpc.Client, error) { 195 sn.lock.RLock() 196 defer sn.lock.RUnlock() 197 if sn.client == nil { 198 return nil, errors.New("node not started") 199 } 200 return sn.client, nil 201 } 202 203 // ServeRPC serves RPC requests over the given connection by creating an 204 // in-memory client to the node's RPC server. 205 func (sn *SimNode) ServeRPC(conn *websocket.Conn) error { 206 handler, err := sn.node.RPCHandler() 207 if err != nil { 208 return err 209 } 210 codec := rpc.NewFuncCodec(conn, func(v any, _ bool) error { return conn.WriteJSON(v) }, conn.ReadJSON) 211 handler.ServeCodec(codec, 0) 212 return nil 213 } 214 215 // Snapshots creates snapshots of the services by calling the 216 // simulation_snapshot RPC method 217 func (sn *SimNode) Snapshots() (map[string][]byte, error) { 218 sn.lock.RLock() 219 services := maps.Clone(sn.running) 220 sn.lock.RUnlock() 221 if len(services) == 0 { 222 return nil, errors.New("no running services") 223 } 224 snapshots := make(map[string][]byte) 225 for name, service := range services { 226 if s, ok := service.(interface { 227 Snapshot() ([]byte, error) 228 }); ok { 229 snap, err := s.Snapshot() 230 if err != nil { 231 return nil, err 232 } 233 snapshots[name] = snap 234 } 235 } 236 return snapshots, nil 237 } 238 239 // Start registers the services and starts the underlying devp2p node 240 func (sn *SimNode) Start(snapshots map[string][]byte) error { 241 // ensure we only register the services once in the case of the node 242 // being stopped and then started again 243 var regErr error 244 sn.registerOnce.Do(func() { 245 for _, name := range sn.config.Lifecycles { 246 ctx := &ServiceContext{ 247 RPCDialer: sn.adapter, 248 Config: sn.config, 249 } 250 if snapshots != nil { 251 ctx.Snapshot = snapshots[name] 252 } 253 serviceFunc := sn.adapter.lifecycles[name] 254 service, err := serviceFunc(ctx, sn.node) 255 if err != nil { 256 regErr = err 257 break 258 } 259 // if the service has already been registered, don't register it again. 260 if _, ok := sn.running[name]; ok { 261 continue 262 } 263 sn.running[name] = service 264 } 265 }) 266 if regErr != nil { 267 return regErr 268 } 269 270 if err := sn.node.Start(); err != nil { 271 return err 272 } 273 274 // create an in-process RPC client 275 client := sn.node.Attach() 276 sn.lock.Lock() 277 sn.client = client 278 sn.lock.Unlock() 279 280 return nil 281 } 282 283 // Stop closes the RPC client and stops the underlying devp2p node 284 func (sn *SimNode) Stop() error { 285 sn.lock.Lock() 286 if sn.client != nil { 287 sn.client.Close() 288 sn.client = nil 289 } 290 sn.lock.Unlock() 291 return sn.node.Close() 292 } 293 294 // Service returns a running service by name 295 func (sn *SimNode) Service(name string) node.Lifecycle { 296 sn.lock.RLock() 297 defer sn.lock.RUnlock() 298 return sn.running[name] 299 } 300 301 // Services returns a copy of the underlying services 302 func (sn *SimNode) Services() []node.Lifecycle { 303 sn.lock.RLock() 304 defer sn.lock.RUnlock() 305 services := make([]node.Lifecycle, 0, len(sn.running)) 306 for _, service := range sn.running { 307 services = append(services, service) 308 } 309 return services 310 } 311 312 // ServiceMap returns a map by names of the underlying services 313 func (sn *SimNode) ServiceMap() map[string]node.Lifecycle { 314 sn.lock.RLock() 315 defer sn.lock.RUnlock() 316 return maps.Clone(sn.running) 317 } 318 319 // Server returns the underlying p2p.Server 320 func (sn *SimNode) Server() *p2p.Server { 321 return sn.node.Server() 322 } 323 324 // SubscribeEvents subscribes the given channel to peer events from the 325 // underlying p2p.Server 326 func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription { 327 srv := sn.Server() 328 if srv == nil { 329 panic("node not running") 330 } 331 return srv.SubscribeEvents(ch) 332 } 333 334 // NodeInfo returns information about the node 335 func (sn *SimNode) NodeInfo() *p2p.NodeInfo { 336 server := sn.Server() 337 if server == nil { 338 return &p2p.NodeInfo{ 339 ID: sn.ID.String(), 340 Enode: sn.Node().String(), 341 } 342 } 343 return server.NodeInfo() 344 }