github.com/0xsequence/ethkit@v1.25.0/go-ethereum/rpc/client.go (about) 1 // Copyright 2016 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 rpc 18 19 import ( 20 "context" 21 "encoding/json" 22 "errors" 23 "fmt" 24 "net/url" 25 "reflect" 26 "strconv" 27 "sync/atomic" 28 "time" 29 30 "github.com/0xsequence/ethkit/go-ethereum/log" 31 ) 32 33 var ( 34 ErrBadResult = errors.New("bad result in JSON-RPC response") 35 ErrClientQuit = errors.New("client is closed") 36 ErrNoResult = errors.New("no result in JSON-RPC response") 37 ErrSubscriptionQueueOverflow = errors.New("subscription queue overflow") 38 errClientReconnected = errors.New("client reconnected") 39 errDead = errors.New("connection lost") 40 ) 41 42 const ( 43 // Timeouts 44 defaultDialTimeout = 10 * time.Second // used if context has no deadline 45 subscribeTimeout = 5 * time.Second // overall timeout eth_subscribe, rpc_modules calls 46 ) 47 48 const ( 49 // Subscriptions are removed when the subscriber cannot keep up. 50 // 51 // This can be worked around by supplying a channel with sufficiently sized buffer, 52 // but this can be inconvenient and hard to explain in the docs. Another issue with 53 // buffered channels is that the buffer is static even though it might not be needed 54 // most of the time. 55 // 56 // The approach taken here is to maintain a per-subscription linked list buffer 57 // shrinks on demand. If the buffer reaches the size below, the subscription is 58 // dropped. 59 maxClientSubscriptionBuffer = 20000 60 ) 61 62 // BatchElem is an element in a batch request. 63 type BatchElem struct { 64 Method string 65 Args []interface{} 66 // The result is unmarshaled into this field. Result must be set to a 67 // non-nil pointer value of the desired type, otherwise the response will be 68 // discarded. 69 Result interface{} 70 // Error is set if the server returns an error for this request, or if 71 // unmarshaling into Result fails. It is not set for I/O errors. 72 Error error 73 } 74 75 // Client represents a connection to an RPC server. 76 type Client struct { 77 idgen func() ID // for subscriptions 78 isHTTP bool // connection type: http, ws or ipc 79 services *serviceRegistry 80 81 idCounter uint32 82 83 // This function, if non-nil, is called when the connection is lost. 84 reconnectFunc reconnectFunc 85 86 // writeConn is used for writing to the connection on the caller's goroutine. It should 87 // only be accessed outside of dispatch, with the write lock held. The write lock is 88 // taken by sending on reqInit and released by sending on reqSent. 89 writeConn jsonWriter 90 91 // for dispatch 92 close chan struct{} 93 closing chan struct{} // closed when client is quitting 94 didClose chan struct{} // closed when client quits 95 reconnected chan ServerCodec // where write/reconnect sends the new connection 96 readOp chan readOp // read messages 97 readErr chan error // errors from read 98 reqInit chan *requestOp // register response IDs, takes write lock 99 reqSent chan error // signals write completion, releases write lock 100 reqTimeout chan *requestOp // removes response IDs when call timeout expires 101 } 102 103 type reconnectFunc func(ctx context.Context) (ServerCodec, error) 104 105 type clientContextKey struct{} 106 107 type clientConn struct { 108 codec ServerCodec 109 handler *handler 110 } 111 112 func (c *Client) newClientConn(conn ServerCodec) *clientConn { 113 ctx := context.Background() 114 ctx = context.WithValue(ctx, clientContextKey{}, c) 115 ctx = context.WithValue(ctx, peerInfoContextKey{}, conn.peerInfo()) 116 handler := newHandler(ctx, conn, c.idgen, c.services) 117 return &clientConn{conn, handler} 118 } 119 120 func (cc *clientConn) close(err error, inflightReq *requestOp) { 121 cc.handler.close(err, inflightReq) 122 cc.codec.close() 123 } 124 125 type readOp struct { 126 msgs []*jsonrpcMessage 127 batch bool 128 } 129 130 type requestOp struct { 131 ids []json.RawMessage 132 err error 133 resp chan *jsonrpcMessage // receives up to len(ids) responses 134 sub *ClientSubscription // only set for EthSubscribe requests 135 } 136 137 func (op *requestOp) wait(ctx context.Context, c *Client) (*jsonrpcMessage, error) { 138 select { 139 case <-ctx.Done(): 140 // Send the timeout to dispatch so it can remove the request IDs. 141 if !c.isHTTP { 142 select { 143 case c.reqTimeout <- op: 144 case <-c.closing: 145 } 146 } 147 return nil, ctx.Err() 148 case resp := <-op.resp: 149 return resp, op.err 150 } 151 } 152 153 // Dial creates a new client for the given URL. 154 // 155 // The currently supported URL schemes are "http", "https", "ws" and "wss". If rawurl is a 156 // file name with no URL scheme, a local socket connection is established using UNIX 157 // domain sockets on supported platforms and named pipes on Windows. If you want to 158 // configure transport options, use DialHTTP, DialWebsocket or DialIPC instead. 159 // 160 // For websocket connections, the origin is set to the local host name. 161 // 162 // The client reconnects automatically if the connection is lost. 163 func Dial(rawurl string) (*Client, error) { 164 return DialContext(context.Background(), rawurl) 165 } 166 167 // DialContext creates a new RPC client, just like Dial. 168 // 169 // The context is used to cancel or time out the initial connection establishment. It does 170 // not affect subsequent interactions with the client. 171 func DialContext(ctx context.Context, rawurl string) (*Client, error) { 172 u, err := url.Parse(rawurl) 173 if err != nil { 174 return nil, err 175 } 176 switch u.Scheme { 177 case "http", "https": 178 return DialHTTP(rawurl) 179 case "ws", "wss": 180 return DialWebsocket(ctx, rawurl, "") 181 case "stdio": 182 return DialStdIO(ctx) 183 case "": 184 return DialIPC(ctx, rawurl) 185 default: 186 return nil, fmt.Errorf("no known transport for URL scheme %q", u.Scheme) 187 } 188 } 189 190 // ClientFromContext retrieves the client from the context, if any. This can be used to perform 191 // 'reverse calls' in a handler method. 192 func ClientFromContext(ctx context.Context) (*Client, bool) { 193 client, ok := ctx.Value(clientContextKey{}).(*Client) 194 return client, ok 195 } 196 197 func newClient(initctx context.Context, connect reconnectFunc) (*Client, error) { 198 conn, err := connect(initctx) 199 if err != nil { 200 return nil, err 201 } 202 c := initClient(conn, randomIDGenerator(), new(serviceRegistry)) 203 c.reconnectFunc = connect 204 return c, nil 205 } 206 207 func initClient(conn ServerCodec, idgen func() ID, services *serviceRegistry) *Client { 208 _, isHTTP := conn.(*httpConn) 209 c := &Client{ 210 isHTTP: isHTTP, 211 idgen: idgen, 212 services: services, 213 writeConn: conn, 214 close: make(chan struct{}), 215 closing: make(chan struct{}), 216 didClose: make(chan struct{}), 217 reconnected: make(chan ServerCodec), 218 readOp: make(chan readOp), 219 readErr: make(chan error), 220 reqInit: make(chan *requestOp), 221 reqSent: make(chan error, 1), 222 reqTimeout: make(chan *requestOp), 223 } 224 if !isHTTP { 225 go c.dispatch(conn) 226 } 227 return c 228 } 229 230 // RegisterName creates a service for the given receiver type under the given name. When no 231 // methods on the given receiver match the criteria to be either a RPC method or a 232 // subscription an error is returned. Otherwise a new service is created and added to the 233 // service collection this client provides to the server. 234 func (c *Client) RegisterName(name string, receiver interface{}) error { 235 return c.services.registerName(name, receiver) 236 } 237 238 func (c *Client) nextID() json.RawMessage { 239 id := atomic.AddUint32(&c.idCounter, 1) 240 return strconv.AppendUint(nil, uint64(id), 10) 241 } 242 243 // SupportedModules calls the rpc_modules method, retrieving the list of 244 // APIs that are available on the server. 245 func (c *Client) SupportedModules() (map[string]string, error) { 246 var result map[string]string 247 ctx, cancel := context.WithTimeout(context.Background(), subscribeTimeout) 248 defer cancel() 249 err := c.CallContext(ctx, &result, "rpc_modules") 250 return result, err 251 } 252 253 // Close closes the client, aborting any in-flight requests. 254 func (c *Client) Close() { 255 if c.isHTTP { 256 return 257 } 258 select { 259 case c.close <- struct{}{}: 260 <-c.didClose 261 case <-c.didClose: 262 } 263 } 264 265 // SetHeader adds a custom HTTP header to the client's requests. 266 // This method only works for clients using HTTP, it doesn't have 267 // any effect for clients using another transport. 268 func (c *Client) SetHeader(key, value string) { 269 if !c.isHTTP { 270 return 271 } 272 conn := c.writeConn.(*httpConn) 273 conn.mu.Lock() 274 conn.headers.Set(key, value) 275 conn.mu.Unlock() 276 } 277 278 // Call performs a JSON-RPC call with the given arguments and unmarshals into 279 // result if no error occurred. 280 // 281 // The result must be a pointer so that package json can unmarshal into it. You 282 // can also pass nil, in which case the result is ignored. 283 func (c *Client) Call(result interface{}, method string, args ...interface{}) error { 284 ctx := context.Background() 285 return c.CallContext(ctx, result, method, args...) 286 } 287 288 // CallContext performs a JSON-RPC call with the given arguments. If the context is 289 // canceled before the call has successfully returned, CallContext returns immediately. 290 // 291 // The result must be a pointer so that package json can unmarshal into it. You 292 // can also pass nil, in which case the result is ignored. 293 func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error { 294 if result != nil && reflect.TypeOf(result).Kind() != reflect.Ptr { 295 return fmt.Errorf("call result parameter must be pointer or nil interface: %v", result) 296 } 297 msg, err := c.newMessage(method, args...) 298 if err != nil { 299 return err 300 } 301 op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)} 302 303 if c.isHTTP { 304 err = c.sendHTTP(ctx, op, msg) 305 } else { 306 err = c.send(ctx, op, msg) 307 } 308 if err != nil { 309 return err 310 } 311 312 // dispatch has accepted the request and will close the channel when it quits. 313 switch resp, err := op.wait(ctx, c); { 314 case err != nil: 315 return err 316 case resp.Error != nil: 317 return resp.Error 318 case len(resp.Result) == 0: 319 return ErrNoResult 320 default: 321 return json.Unmarshal(resp.Result, &result) 322 } 323 } 324 325 // BatchCall sends all given requests as a single batch and waits for the server 326 // to return a response for all of them. 327 // 328 // In contrast to Call, BatchCall only returns I/O errors. Any error specific to 329 // a request is reported through the Error field of the corresponding BatchElem. 330 // 331 // Note that batch calls may not be executed atomically on the server side. 332 func (c *Client) BatchCall(b []BatchElem) error { 333 ctx := context.Background() 334 return c.BatchCallContext(ctx, b) 335 } 336 337 // BatchCallContext sends all given requests as a single batch and waits for the server 338 // to return a response for all of them. The wait duration is bounded by the 339 // context's deadline. 340 // 341 // In contrast to CallContext, BatchCallContext only returns errors that have occurred 342 // while sending the request. Any error specific to a request is reported through the 343 // Error field of the corresponding BatchElem. 344 // 345 // Note that batch calls may not be executed atomically on the server side. 346 func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error { 347 var ( 348 msgs = make([]*jsonrpcMessage, len(b)) 349 byID = make(map[string]int, len(b)) 350 ) 351 op := &requestOp{ 352 ids: make([]json.RawMessage, len(b)), 353 resp: make(chan *jsonrpcMessage, len(b)), 354 } 355 for i, elem := range b { 356 msg, err := c.newMessage(elem.Method, elem.Args...) 357 if err != nil { 358 return err 359 } 360 msgs[i] = msg 361 op.ids[i] = msg.ID 362 byID[string(msg.ID)] = i 363 } 364 365 var err error 366 if c.isHTTP { 367 err = c.sendBatchHTTP(ctx, op, msgs) 368 } else { 369 err = c.send(ctx, op, msgs) 370 } 371 372 // Wait for all responses to come back. 373 for n := 0; n < len(b) && err == nil; n++ { 374 var resp *jsonrpcMessage 375 resp, err = op.wait(ctx, c) 376 if err != nil { 377 break 378 } 379 // Find the element corresponding to this response. 380 // The element is guaranteed to be present because dispatch 381 // only sends valid IDs to our channel. 382 elem := &b[byID[string(resp.ID)]] 383 if resp.Error != nil { 384 elem.Error = resp.Error 385 continue 386 } 387 if len(resp.Result) == 0 { 388 elem.Error = ErrNoResult 389 continue 390 } 391 elem.Error = json.Unmarshal(resp.Result, elem.Result) 392 } 393 return err 394 } 395 396 // Notify sends a notification, i.e. a method call that doesn't expect a response. 397 func (c *Client) Notify(ctx context.Context, method string, args ...interface{}) error { 398 op := new(requestOp) 399 msg, err := c.newMessage(method, args...) 400 if err != nil { 401 return err 402 } 403 msg.ID = nil 404 405 if c.isHTTP { 406 return c.sendHTTP(ctx, op, msg) 407 } 408 return c.send(ctx, op, msg) 409 } 410 411 // EthSubscribe registers a subscription under the "eth" namespace. 412 func (c *Client) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) { 413 return c.Subscribe(ctx, "eth", channel, args...) 414 } 415 416 // ShhSubscribe registers a subscription under the "shh" namespace. 417 // Deprecated: use Subscribe(ctx, "shh", ...). 418 func (c *Client) ShhSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) { 419 return c.Subscribe(ctx, "shh", channel, args...) 420 } 421 422 // Subscribe calls the "<namespace>_subscribe" method with the given arguments, 423 // registering a subscription. Server notifications for the subscription are 424 // sent to the given channel. The element type of the channel must match the 425 // expected type of content returned by the subscription. 426 // 427 // The context argument cancels the RPC request that sets up the subscription but has no 428 // effect on the subscription after Subscribe has returned. 429 // 430 // Slow subscribers will be dropped eventually. Client buffers up to 20000 notifications 431 // before considering the subscriber dead. The subscription Err channel will receive 432 // ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure 433 // that the channel usually has at least one reader to prevent this issue. 434 func (c *Client) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*ClientSubscription, error) { 435 // Check type of channel first. 436 chanVal := reflect.ValueOf(channel) 437 if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 { 438 panic(fmt.Sprintf("channel argument of Subscribe has type %T, need writable channel", channel)) 439 } 440 if chanVal.IsNil() { 441 panic("channel given to Subscribe must not be nil") 442 } 443 if c.isHTTP { 444 return nil, ErrNotificationsUnsupported 445 } 446 447 msg, err := c.newMessage(namespace+subscribeMethodSuffix, args...) 448 if err != nil { 449 return nil, err 450 } 451 op := &requestOp{ 452 ids: []json.RawMessage{msg.ID}, 453 resp: make(chan *jsonrpcMessage), 454 sub: newClientSubscription(c, namespace, chanVal), 455 } 456 457 // Send the subscription request. 458 // The arrival and validity of the response is signaled on sub.quit. 459 if err := c.send(ctx, op, msg); err != nil { 460 return nil, err 461 } 462 if _, err := op.wait(ctx, c); err != nil { 463 return nil, err 464 } 465 return op.sub, nil 466 } 467 468 func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) { 469 msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method} 470 if paramsIn != nil { // prevent sending "params":null 471 var err error 472 if msg.Params, err = json.Marshal(paramsIn); err != nil { 473 return nil, err 474 } 475 } 476 return msg, nil 477 } 478 479 // send registers op with the dispatch loop, then sends msg on the connection. 480 // if sending fails, op is deregistered. 481 func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error { 482 select { 483 case c.reqInit <- op: 484 err := c.write(ctx, msg, false) 485 c.reqSent <- err 486 return err 487 case <-ctx.Done(): 488 // This can happen if the client is overloaded or unable to keep up with 489 // subscription notifications. 490 return ctx.Err() 491 case <-c.closing: 492 return ErrClientQuit 493 } 494 } 495 496 func (c *Client) write(ctx context.Context, msg interface{}, retry bool) error { 497 if c.writeConn == nil { 498 // The previous write failed. Try to establish a new connection. 499 if err := c.reconnect(ctx); err != nil { 500 return err 501 } 502 } 503 err := c.writeConn.writeJSON(ctx, msg) 504 if err != nil { 505 c.writeConn = nil 506 if !retry { 507 return c.write(ctx, msg, true) 508 } 509 } 510 return err 511 } 512 513 func (c *Client) reconnect(ctx context.Context) error { 514 if c.reconnectFunc == nil { 515 return errDead 516 } 517 518 if _, ok := ctx.Deadline(); !ok { 519 var cancel func() 520 ctx, cancel = context.WithTimeout(ctx, defaultDialTimeout) 521 defer cancel() 522 } 523 newconn, err := c.reconnectFunc(ctx) 524 if err != nil { 525 log.Trace("RPC client reconnect failed", "err", err) 526 return err 527 } 528 select { 529 case c.reconnected <- newconn: 530 c.writeConn = newconn 531 return nil 532 case <-c.didClose: 533 newconn.close() 534 return ErrClientQuit 535 } 536 } 537 538 // dispatch is the main loop of the client. 539 // It sends read messages to waiting calls to Call and BatchCall 540 // and subscription notifications to registered subscriptions. 541 func (c *Client) dispatch(codec ServerCodec) { 542 var ( 543 lastOp *requestOp // tracks last send operation 544 reqInitLock = c.reqInit // nil while the send lock is held 545 conn = c.newClientConn(codec) 546 reading = true 547 ) 548 defer func() { 549 close(c.closing) 550 if reading { 551 conn.close(ErrClientQuit, nil) 552 c.drainRead() 553 } 554 close(c.didClose) 555 }() 556 557 // Spawn the initial read loop. 558 go c.read(codec) 559 560 for { 561 select { 562 case <-c.close: 563 return 564 565 // Read path: 566 case op := <-c.readOp: 567 if op.batch { 568 conn.handler.handleBatch(op.msgs) 569 } else { 570 conn.handler.handleMsg(op.msgs[0]) 571 } 572 573 case err := <-c.readErr: 574 conn.handler.log.Debug("RPC connection read error", "err", err) 575 conn.close(err, lastOp) 576 reading = false 577 578 // Reconnect: 579 case newcodec := <-c.reconnected: 580 log.Debug("RPC client reconnected", "reading", reading, "conn", newcodec.remoteAddr()) 581 if reading { 582 // Wait for the previous read loop to exit. This is a rare case which 583 // happens if this loop isn't notified in time after the connection breaks. 584 // In those cases the caller will notice first and reconnect. Closing the 585 // handler terminates all waiting requests (closing op.resp) except for 586 // lastOp, which will be transferred to the new handler. 587 conn.close(errClientReconnected, lastOp) 588 c.drainRead() 589 } 590 go c.read(newcodec) 591 reading = true 592 conn = c.newClientConn(newcodec) 593 // Re-register the in-flight request on the new handler 594 // because that's where it will be sent. 595 conn.handler.addRequestOp(lastOp) 596 597 // Send path: 598 case op := <-reqInitLock: 599 // Stop listening for further requests until the current one has been sent. 600 reqInitLock = nil 601 lastOp = op 602 conn.handler.addRequestOp(op) 603 604 case err := <-c.reqSent: 605 if err != nil { 606 // Remove response handlers for the last send. When the read loop 607 // goes down, it will signal all other current operations. 608 conn.handler.removeRequestOp(lastOp) 609 } 610 // Let the next request in. 611 reqInitLock = c.reqInit 612 lastOp = nil 613 614 case op := <-c.reqTimeout: 615 conn.handler.removeRequestOp(op) 616 } 617 } 618 } 619 620 // drainRead drops read messages until an error occurs. 621 func (c *Client) drainRead() { 622 for { 623 select { 624 case <-c.readOp: 625 case <-c.readErr: 626 return 627 } 628 } 629 } 630 631 // read decodes RPC messages from a codec, feeding them into dispatch. 632 func (c *Client) read(codec ServerCodec) { 633 for { 634 msgs, batch, err := codec.readBatch() 635 if _, ok := err.(*json.SyntaxError); ok { 636 codec.writeJSON(context.Background(), errorMessage(&parseError{err.Error()})) 637 } 638 if err != nil { 639 c.readErr <- err 640 return 641 } 642 c.readOp <- readOp{msgs, batch} 643 } 644 }