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