2017-07-22 04:53:54 +02:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-07-22 04:53:54 +02:00
|
|
|
package rpcclient
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-03-11 05:21:59 +01:00
|
|
|
"io"
|
2015-01-01 23:34:07 +01:00
|
|
|
"io/ioutil"
|
2016-08-08 05:47:07 +02:00
|
|
|
"math"
|
2014-05-07 18:14:39 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-10-09 13:23:05 +02:00
|
|
|
"os"
|
2019-10-30 02:50:11 +01:00
|
|
|
"strings"
|
2014-05-07 18:14:39 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2015-01-16 08:25:00 +01:00
|
|
|
"github.com/btcsuite/go-socks/socks"
|
2015-01-16 00:25:14 +01:00
|
|
|
"github.com/btcsuite/websocket"
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/btcjson"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidAuth is an error to describe the condition where the client
|
|
|
|
// is either unable to authenticate or the specified endpoint is
|
|
|
|
// incorrect.
|
|
|
|
ErrInvalidAuth = errors.New("authentication failure")
|
|
|
|
|
2014-05-10 02:26:56 +02:00
|
|
|
// ErrInvalidEndpoint is an error to describe the condition where the
|
|
|
|
// websocket handshake failed with the specified endpoint.
|
|
|
|
ErrInvalidEndpoint = errors.New("the endpoint either does not support " +
|
|
|
|
"websockets or does not exist")
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// ErrClientNotConnected is an error to describe the condition where a
|
|
|
|
// websocket client has been created, but the connection was never
|
|
|
|
// established. This condition differs from ErrClientDisconnect, which
|
|
|
|
// represents an established connection that was lost.
|
|
|
|
ErrClientNotConnected = errors.New("the client was never connected")
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// ErrClientDisconnect is an error to describe the condition where the
|
|
|
|
// client has been disconnected from the RPC server. When the
|
|
|
|
// DisableAutoReconnect option is not set, any outstanding futures
|
|
|
|
// when a client disconnect occurs will return this error as will
|
|
|
|
// any new requests.
|
|
|
|
ErrClientDisconnect = errors.New("the client has been disconnected")
|
|
|
|
|
|
|
|
// ErrClientShutdown is an error to describe the condition where the
|
|
|
|
// client is either already shutdown, or in the process of shutting
|
|
|
|
// down. Any outstanding futures when a client shutdown occurs will
|
|
|
|
// return this error as will any new requests.
|
|
|
|
ErrClientShutdown = errors.New("the client has been shutdown")
|
2014-07-10 20:11:57 +02:00
|
|
|
|
|
|
|
// ErrNotWebsocketClient is an error to describe the condition of
|
|
|
|
// calling a Client method intended for a websocket client when the
|
|
|
|
// client has been configured to run in HTTP POST mode instead.
|
|
|
|
ErrNotWebsocketClient = errors.New("client is not configured for " +
|
|
|
|
"websockets")
|
|
|
|
|
|
|
|
// ErrClientAlreadyConnected is an error to describe the condition where
|
|
|
|
// a new client connection cannot be established due to a websocket
|
|
|
|
// client having already connected to the RPC server.
|
|
|
|
ErrClientAlreadyConnected = errors.New("websocket client has already " +
|
|
|
|
"connected")
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sendBufferSize is the number of elements the websocket send channel
|
|
|
|
// can queue before blocking.
|
|
|
|
sendBufferSize = 50
|
|
|
|
|
|
|
|
// sendPostBufferSize is the number of elements the HTTP POST send
|
|
|
|
// channel can queue before blocking.
|
|
|
|
sendPostBufferSize = 100
|
|
|
|
|
|
|
|
// connectionRetryInterval is the amount of time to wait in between
|
|
|
|
// retries when automatically reconnecting to an RPC server.
|
|
|
|
connectionRetryInterval = time.Second * 5
|
|
|
|
|
2021-08-21 15:04:31 +02:00
|
|
|
// requestRetryInterval is the initial amount of time to wait in between
|
|
|
|
// retries when sending HTTP POST requests.
|
|
|
|
requestRetryInterval = time.Millisecond * 500
|
|
|
|
)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// jsonRequest holds information about a json request that is used to properly
|
|
|
|
// detect, interpret, and deliver a reply to it.
|
|
|
|
type jsonRequest struct {
|
2015-01-01 23:34:07 +01:00
|
|
|
id uint64
|
|
|
|
method string
|
|
|
|
cmd interface{}
|
|
|
|
marshalledJSON []byte
|
2021-09-02 08:39:55 +02:00
|
|
|
responseChan chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 02:50:11 +01:00
|
|
|
// BackendVersion represents the version of the backend the client is currently
|
|
|
|
// connected to.
|
|
|
|
type BackendVersion uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BitcoindPre19 represents a bitcoind version before 0.19.0.
|
|
|
|
BitcoindPre19 BackendVersion = iota
|
|
|
|
|
|
|
|
// BitcoindPost19 represents a bitcoind version equal to or greater than
|
|
|
|
// 0.19.0.
|
|
|
|
BitcoindPost19
|
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
// Lbcd represents a catch-all lbcd version.
|
|
|
|
Lbcd
|
2019-10-30 02:50:11 +01:00
|
|
|
)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// Client represents a Bitcoin RPC client which allows easy access to the
|
|
|
|
// various RPC methods available on a Bitcoin RPC server. Each of the wrapper
|
|
|
|
// functions handle the details of converting the passed and return types to and
|
|
|
|
// from the underlying JSON types which are required for the JSON-RPC
|
|
|
|
// invocations
|
|
|
|
//
|
|
|
|
// The client provides each RPC in both synchronous (blocking) and asynchronous
|
|
|
|
// (non-blocking) forms. The asynchronous forms are based on the concept of
|
|
|
|
// futures where they return an instance of a type that promises to deliver the
|
|
|
|
// result of the invocation at some future time. Invoking the Receive method on
|
|
|
|
// the returned future will block until the result is available if it's not
|
|
|
|
// already.
|
|
|
|
type Client struct {
|
2014-05-08 20:04:05 +02:00
|
|
|
id uint64 // atomic, so must stay 64-bit aligned
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// config holds the connection configuration assoiated with this client.
|
|
|
|
config *ConnConfig
|
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// chainParams holds the params for the chain that this client is using,
|
|
|
|
// and is used for many wallet methods.
|
|
|
|
chainParams *chaincfg.Params
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// wsConn is the underlying websocket connection when not in HTTP POST
|
|
|
|
// mode.
|
|
|
|
wsConn *websocket.Conn
|
|
|
|
|
|
|
|
// httpClient is the underlying HTTP client to use when running in HTTP
|
|
|
|
// POST mode.
|
|
|
|
httpClient *http.Client
|
|
|
|
|
2019-10-30 02:50:11 +01:00
|
|
|
// backendVersion is the version of the backend the client is currently
|
|
|
|
// connected to. This should be retrieved through GetVersion.
|
|
|
|
backendVersionMu sync.Mutex
|
|
|
|
backendVersion *BackendVersion
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// mtx is a mutex to protect access to connection related fields.
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
|
|
|
// disconnected indicated whether or not the server is disconnected.
|
|
|
|
disconnected bool
|
|
|
|
|
2020-12-06 04:39:40 +01:00
|
|
|
// whether or not to batch requests, false unless changed by Batch()
|
|
|
|
batch bool
|
|
|
|
batchList *list.List
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// retryCount holds the number of times the client has tried to
|
|
|
|
// reconnect to the RPC server.
|
|
|
|
retryCount int64
|
|
|
|
|
|
|
|
// Track command and their response channels by ID.
|
|
|
|
requestLock sync.Mutex
|
2014-05-08 20:04:05 +02:00
|
|
|
requestMap map[uint64]*list.Element
|
2014-05-07 18:14:39 +02:00
|
|
|
requestList *list.List
|
|
|
|
|
2014-05-10 06:44:43 +02:00
|
|
|
// Notifications.
|
2016-08-17 20:12:27 +02:00
|
|
|
ntfnHandlers *NotificationHandlers
|
|
|
|
ntfnStateLock sync.Mutex
|
|
|
|
ntfnState *notificationState
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Networking infrastructure.
|
2014-07-10 20:11:57 +02:00
|
|
|
sendChan chan []byte
|
2021-08-21 15:04:31 +02:00
|
|
|
sendPostChan chan *jsonRequest
|
2014-07-10 20:11:57 +02:00
|
|
|
connEstablished chan struct{}
|
|
|
|
disconnect chan struct{}
|
|
|
|
shutdown chan struct{}
|
|
|
|
wg sync.WaitGroup
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NextID returns the next id to be used when sending a JSON-RPC message. This
|
|
|
|
// ID allows responses to be associated with particular requests per the
|
|
|
|
// JSON-RPC specification. Typically the consumer of the client does not need
|
|
|
|
// to call this function, however, if a custom request is being created and used
|
|
|
|
// this function should be used to ensure the ID is unique amongst all requests
|
|
|
|
// being made.
|
2014-05-08 20:04:05 +02:00
|
|
|
func (c *Client) NextID() uint64 {
|
|
|
|
return atomic.AddUint64(&c.id, 1)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// addRequest associates the passed jsonRequest with its id. This allows the
|
|
|
|
// response from the remote server to be unmarshalled to the appropriate type
|
|
|
|
// and sent to the specified channel when it is received.
|
2014-07-08 00:48:38 +02:00
|
|
|
//
|
|
|
|
// If the client has already begun shutting down, ErrClientShutdown is returned
|
|
|
|
// and the request is not added.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) addRequest(jReq *jsonRequest) error {
|
2014-05-07 18:14:39 +02:00
|
|
|
c.requestLock.Lock()
|
|
|
|
defer c.requestLock.Unlock()
|
|
|
|
|
2014-07-08 00:48:38 +02:00
|
|
|
// A non-blocking read of the shutdown channel with the request lock
|
|
|
|
// held avoids adding the request to the client's internal data
|
|
|
|
// structures if the client is in the process of shutting down (and
|
|
|
|
// has not yet grabbed the request lock), or has finished shutdown
|
|
|
|
// already (responding to each outstanding request with
|
|
|
|
// ErrClientShutdown).
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
return ErrClientShutdown
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-12-06 04:39:40 +01:00
|
|
|
if !c.batch {
|
|
|
|
element := c.requestList.PushBack(jReq)
|
|
|
|
c.requestMap[jReq.id] = element
|
|
|
|
} else {
|
|
|
|
element := c.batchList.PushBack(jReq)
|
|
|
|
c.requestMap[jReq.id] = element
|
|
|
|
}
|
2014-07-08 00:48:38 +02:00
|
|
|
return nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeRequest returns and removes the jsonRequest which contains the response
|
|
|
|
// channel and original method associated with the passed id or nil if there is
|
|
|
|
// no association.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2014-05-08 20:04:05 +02:00
|
|
|
func (c *Client) removeRequest(id uint64) *jsonRequest {
|
2014-05-07 18:14:39 +02:00
|
|
|
c.requestLock.Lock()
|
|
|
|
defer c.requestLock.Unlock()
|
|
|
|
|
|
|
|
element := c.requestMap[id]
|
|
|
|
if element != nil {
|
|
|
|
delete(c.requestMap, id)
|
|
|
|
request := c.requestList.Remove(element).(*jsonRequest)
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeAllRequests removes all the jsonRequests which contain the response
|
|
|
|
// channels for outstanding requests.
|
|
|
|
//
|
2014-07-08 03:11:01 +02:00
|
|
|
// This function MUST be called with the request lock held.
|
2014-05-07 18:14:39 +02:00
|
|
|
func (c *Client) removeAllRequests() {
|
2014-05-08 20:04:05 +02:00
|
|
|
c.requestMap = make(map[uint64]*list.Element)
|
2014-05-07 18:14:39 +02:00
|
|
|
c.requestList.Init()
|
|
|
|
}
|
|
|
|
|
2014-05-10 06:44:43 +02:00
|
|
|
// trackRegisteredNtfns examines the passed command to see if it is one of
|
|
|
|
// the notification commands and updates the notification state that is used
|
|
|
|
// to automatically re-establish registered notifications on reconnects.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) trackRegisteredNtfns(cmd interface{}) {
|
2014-05-10 06:44:43 +02:00
|
|
|
// Nothing to do if the caller is not interested in notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-17 20:12:27 +02:00
|
|
|
c.ntfnStateLock.Lock()
|
|
|
|
defer c.ntfnStateLock.Unlock()
|
2014-05-10 06:44:43 +02:00
|
|
|
|
|
|
|
switch bcmd := cmd.(type) {
|
2015-01-01 23:34:07 +01:00
|
|
|
case *btcjson.NotifyBlocksCmd:
|
2014-05-10 06:44:43 +02:00
|
|
|
c.ntfnState.notifyBlocks = true
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
case *btcjson.NotifyNewTransactionsCmd:
|
|
|
|
if bcmd.Verbose != nil && *bcmd.Verbose {
|
2014-05-10 06:44:43 +02:00
|
|
|
c.ntfnState.notifyNewTxVerbose = true
|
|
|
|
} else {
|
|
|
|
c.ntfnState.notifyNewTx = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
case *btcjson.NotifySpentCmd:
|
2014-05-10 06:44:43 +02:00
|
|
|
for _, op := range bcmd.OutPoints {
|
|
|
|
c.ntfnState.notifySpent[op] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
case *btcjson.NotifyReceivedCmd:
|
2014-05-10 06:44:43 +02:00
|
|
|
for _, addr := range bcmd.Addresses {
|
|
|
|
c.ntfnState.notifyReceived[addr] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 04:39:40 +01:00
|
|
|
// FutureGetBulkResult waits for the responses promised by the future
|
|
|
|
// and returns them in a channel
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBulkResult chan *Response
|
2020-12-06 04:39:40 +01:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an map
|
|
|
|
// of results by request id
|
|
|
|
func (r FutureGetBulkResult) Receive() (BulkResult, error) {
|
|
|
|
m := make(BulkResult)
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-12-06 04:39:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var arr []IndividualBulkResult
|
|
|
|
err = json.Unmarshal(res, &arr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, results := range arr {
|
|
|
|
m[results.Id] = results
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndividualBulkResult represents one result
|
|
|
|
// from a bulk json rpc api
|
|
|
|
type IndividualBulkResult struct {
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
Error *btcjson.RPCError `json:"error"`
|
|
|
|
Id uint64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BulkResult = map[uint64]IndividualBulkResult
|
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// inMessage is the first type that an incoming message is unmarshaled
|
|
|
|
// into. It supports both requests (for notification support) and
|
|
|
|
// responses. The partially-unmarshaled message is a notification if
|
|
|
|
// the embedded ID (from the response) is nil. Otherwise, it is a
|
|
|
|
// response.
|
|
|
|
type inMessage struct {
|
|
|
|
ID *float64 `json:"id"`
|
|
|
|
*rawNotification
|
|
|
|
*rawResponse
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// rawNotification is a partially-unmarshaled JSON-RPC notification.
|
|
|
|
type rawNotification struct {
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []json.RawMessage `json:"params"`
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// rawResponse is a partially-unmarshaled JSON-RPC response. For this
|
|
|
|
// to be valid (according to JSON-RPC 1.0 spec), ID may not be nil.
|
|
|
|
type rawResponse struct {
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
Error *btcjson.RPCError `json:"error"`
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Response is the raw bytes of a JSON-RPC result, or the error if the response
|
2014-06-10 02:49:41 +02:00
|
|
|
// error object was non-null.
|
2021-09-02 08:39:55 +02:00
|
|
|
type Response struct {
|
2014-06-10 02:49:41 +02:00
|
|
|
result []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// result checks whether the unmarshaled response contains a non-nil error,
|
2015-01-01 23:34:07 +01:00
|
|
|
// returning an unmarshaled btcjson.RPCError (or an unmarshaling error) if so.
|
2014-06-10 02:49:41 +02:00
|
|
|
// If the response is not an error, the raw bytes of the request are
|
|
|
|
// returned for further unmashaling into specific result types.
|
|
|
|
func (r rawResponse) result() (result []byte, err error) {
|
|
|
|
if r.Error != nil {
|
|
|
|
return nil, r.Error
|
|
|
|
}
|
|
|
|
return r.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleMessage is the main handler for incoming notifications and responses.
|
|
|
|
func (c *Client) handleMessage(msg []byte) {
|
2015-01-01 23:34:07 +01:00
|
|
|
// Attempt to unmarshal the message as either a notification or
|
|
|
|
// response.
|
2014-06-18 17:16:09 +02:00
|
|
|
var in inMessage
|
2018-02-20 01:36:29 +01:00
|
|
|
in.rawResponse = new(rawResponse)
|
|
|
|
in.rawNotification = new(rawNotification)
|
2014-06-10 02:49:41 +02:00
|
|
|
err := json.Unmarshal(msg, &in)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Remote server sent invalid message: %v", err)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// JSON-RPC 1.0 notifications are requests with a null id.
|
|
|
|
if in.ID == nil {
|
|
|
|
ntfn := in.rawNotification
|
|
|
|
if ntfn == nil {
|
|
|
|
log.Warn("Malformed notification: missing " +
|
|
|
|
"method and parameters")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ntfn.Method == "" {
|
|
|
|
log.Warn("Malformed notification: missing method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// params are not optional: nil isn't valid (but len == 0 is)
|
|
|
|
if ntfn.Params == nil {
|
|
|
|
log.Warn("Malformed notification: missing params")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Deliver the notification.
|
|
|
|
log.Tracef("Received notification [%s]", in.Method)
|
|
|
|
c.handleNotification(in.rawNotification)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:47:07 +02:00
|
|
|
// ensure that in.ID can be converted to an integer without loss of precision
|
|
|
|
if *in.ID < 0 || *in.ID != math.Trunc(*in.ID) {
|
|
|
|
log.Warn("Malformed response: invalid identifier")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
if in.rawResponse == nil {
|
|
|
|
log.Warn("Malformed response: missing result and error")
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
|
2016-08-08 05:47:07 +02:00
|
|
|
id := uint64(*in.ID)
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Tracef("Received response for id %d (result %s)", id, in.Result)
|
2014-05-07 18:14:39 +02:00
|
|
|
request := c.removeRequest(id)
|
|
|
|
|
|
|
|
// Nothing more to do if there is no request associated with this reply.
|
|
|
|
if request == nil || request.responseChan == nil {
|
2014-06-10 02:49:41 +02:00
|
|
|
log.Warnf("Received unexpected reply: %s (id %d)", in.Result,
|
|
|
|
id)
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
2014-05-10 06:44:43 +02:00
|
|
|
|
|
|
|
// Since the command was successful, examine it to see if it's a
|
|
|
|
// notification, and if is, add it to the notification state so it
|
|
|
|
// can automatically be re-established on reconnect.
|
|
|
|
c.trackRegisteredNtfns(request.cmd)
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Deliver the response.
|
|
|
|
result, err := in.rawResponse.result()
|
2021-09-02 08:39:55 +02:00
|
|
|
request.responseChan <- &Response{result: result, err: err}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:21:59 +01:00
|
|
|
// shouldLogReadError returns whether or not the passed error, which is expected
|
|
|
|
// to have come from reading from the websocket connection in wsInHandler,
|
|
|
|
// should be logged.
|
|
|
|
func (c *Client) shouldLogReadError(err error) bool {
|
|
|
|
// No logging when the connetion is being forcibly disconnected.
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// No logging when the connection has been disconnected.
|
|
|
|
if err == io.EOF {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if opErr, ok := err.(*net.OpError); ok && !opErr.Temporary() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// wsInHandler handles all incoming messages for the websocket connection
|
|
|
|
// associated with the client. It must be run as a goroutine.
|
|
|
|
func (c *Client) wsInHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
// Break out of the loop once the shutdown channel has been
|
|
|
|
// closed. Use a non-blocking select here so we fall through
|
|
|
|
// otherwise.
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
break out
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2014-05-10 02:26:56 +02:00
|
|
|
_, msg, err := c.wsConn.ReadMessage()
|
|
|
|
if err != nil {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Log the error if it's not due to disconnecting.
|
2016-03-11 05:21:59 +01:00
|
|
|
if c.shouldLogReadError(err) {
|
2014-05-07 18:14:39 +02:00
|
|
|
log.Errorf("Websocket receive error from "+
|
|
|
|
"%s: %v", c.config.Host, err)
|
|
|
|
}
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
c.handleMessage(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the connection is closed.
|
|
|
|
c.Disconnect()
|
|
|
|
c.wg.Done()
|
|
|
|
log.Tracef("RPC client input handler done for %s", c.config.Host)
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:38:25 +02:00
|
|
|
// disconnectChan returns a copy of the current disconnect channel. The channel
|
|
|
|
// is read protected by the client mutex, and is safe to call while the channel
|
|
|
|
// is being reassigned during a reconnect.
|
|
|
|
func (c *Client) disconnectChan() <-chan struct{} {
|
|
|
|
c.mtx.Lock()
|
|
|
|
ch := c.disconnect
|
|
|
|
c.mtx.Unlock()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// wsOutHandler handles all outgoing messages for the websocket connection. It
|
|
|
|
// uses a buffered channel to serialize output messages while allowing the
|
|
|
|
// sender to continue running asynchronously. It must be run as a goroutine.
|
|
|
|
func (c *Client) wsOutHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
// Send any messages ready for send until the client is
|
|
|
|
// disconnected closed.
|
|
|
|
select {
|
|
|
|
case msg := <-c.sendChan:
|
2014-05-10 02:26:56 +02:00
|
|
|
err := c.wsConn.WriteMessage(websocket.TextMessage, msg)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Disconnect()
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:38:25 +02:00
|
|
|
case <-c.disconnectChan():
|
2014-05-07 18:14:39 +02:00
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain any channels before exiting so nothing is left waiting around
|
|
|
|
// to send.
|
|
|
|
cleanup:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.sendChan:
|
|
|
|
default:
|
|
|
|
break cleanup
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.wg.Done()
|
|
|
|
log.Tracef("RPC client output handler done for %s", c.config.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendMessage sends the passed JSON to the connected server using the
|
|
|
|
// websocket connection. It is backed by a buffered channel, so it will not
|
|
|
|
// block until the send channel is full.
|
|
|
|
func (c *Client) sendMessage(marshalledJSON []byte) {
|
|
|
|
// Don't send the message if disconnected.
|
2014-07-08 03:11:01 +02:00
|
|
|
select {
|
|
|
|
case c.sendChan <- marshalledJSON:
|
2015-06-15 17:38:25 +02:00
|
|
|
case <-c.disconnectChan():
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 06:44:43 +02:00
|
|
|
// reregisterNtfns creates and sends commands needed to re-establish the current
|
|
|
|
// notification state associated with the client. It should only be called on
|
2015-01-01 23:34:07 +01:00
|
|
|
// on reconnect by the resendRequests function.
|
2014-05-10 06:44:43 +02:00
|
|
|
func (c *Client) reregisterNtfns() error {
|
|
|
|
// Nothing to do if the caller is not interested in notifications.
|
|
|
|
if c.ntfnHandlers == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to avoid holding the lock on the notification state for the
|
|
|
|
// entire time of the potentially long running RPCs issued below, make a
|
|
|
|
// copy of it and work from that.
|
|
|
|
//
|
|
|
|
// Also, other commands will be running concurrently which could modify
|
|
|
|
// the notification state (while not under the lock of course) which
|
|
|
|
// also register it with the remote RPC server, so this prevents double
|
|
|
|
// registrations.
|
2016-08-17 20:12:27 +02:00
|
|
|
c.ntfnStateLock.Lock()
|
2014-05-10 06:44:43 +02:00
|
|
|
stateCopy := c.ntfnState.Copy()
|
2016-08-17 20:12:27 +02:00
|
|
|
c.ntfnStateLock.Unlock()
|
2014-05-10 06:44:43 +02:00
|
|
|
|
|
|
|
// Reregister notifyblocks if needed.
|
|
|
|
if stateCopy.notifyBlocks {
|
|
|
|
log.Debugf("Reregistering [notifyblocks]")
|
|
|
|
if err := c.NotifyBlocks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reregister notifynewtransactions if needed.
|
|
|
|
if stateCopy.notifyNewTx || stateCopy.notifyNewTxVerbose {
|
|
|
|
log.Debugf("Reregistering [notifynewtransactions] (verbose=%v)",
|
|
|
|
stateCopy.notifyNewTxVerbose)
|
|
|
|
err := c.NotifyNewTransactions(stateCopy.notifyNewTxVerbose)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reregister the combination of all previously registered notifyspent
|
|
|
|
// outpoints in one command if needed.
|
|
|
|
nslen := len(stateCopy.notifySpent)
|
|
|
|
if nslen > 0 {
|
2015-01-01 23:34:07 +01:00
|
|
|
outpoints := make([]btcjson.OutPoint, 0, nslen)
|
2014-05-10 06:44:43 +02:00
|
|
|
for op := range stateCopy.notifySpent {
|
|
|
|
outpoints = append(outpoints, op)
|
|
|
|
}
|
|
|
|
log.Debugf("Reregistering [notifyspent] outpoints: %v", outpoints)
|
|
|
|
if err := c.notifySpentInternal(outpoints).Receive(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reregister the combination of all previously registered
|
|
|
|
// notifyreceived addresses in one command if needed.
|
|
|
|
nrlen := len(stateCopy.notifyReceived)
|
|
|
|
if nrlen > 0 {
|
|
|
|
addresses := make([]string, 0, nrlen)
|
|
|
|
for addr := range stateCopy.notifyReceived {
|
|
|
|
addresses = append(addresses, addr)
|
|
|
|
}
|
|
|
|
log.Debugf("Reregistering [notifyreceived] addresses: %v", addresses)
|
|
|
|
if err := c.notifyReceivedInternal(addresses).Receive(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:23:21 +02:00
|
|
|
// ignoreResends is a set of all methods for requests that are "long running"
|
|
|
|
// are not be reissued by the client on reconnect.
|
|
|
|
var ignoreResends = map[string]struct{}{
|
2016-11-03 14:20:38 +01:00
|
|
|
"rescan": {},
|
2014-06-11 20:23:21 +02:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// resendRequests resends any requests that had not completed when the client
|
2014-05-10 06:44:43 +02:00
|
|
|
// disconnected. It is intended to be called once the client has reconnected as
|
|
|
|
// a separate goroutine.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) resendRequests() {
|
2014-05-10 06:44:43 +02:00
|
|
|
// Set the notification state back up. If anything goes wrong,
|
|
|
|
// disconnect the client.
|
|
|
|
if err := c.reregisterNtfns(); err != nil {
|
|
|
|
log.Warnf("Unable to re-establish notification state: %v", err)
|
|
|
|
c.Disconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// Since it's possible to block on send and more requests might be
|
2014-05-07 18:14:39 +02:00
|
|
|
// added by the caller while resending, make a copy of all of the
|
2015-01-01 23:34:07 +01:00
|
|
|
// requests that need to be resent now and work from the copy. This
|
2014-05-07 18:14:39 +02:00
|
|
|
// also allows the lock to be released quickly.
|
|
|
|
c.requestLock.Lock()
|
2015-01-01 23:34:07 +01:00
|
|
|
resendReqs := make([]*jsonRequest, 0, c.requestList.Len())
|
2014-06-11 20:23:21 +02:00
|
|
|
var nextElem *list.Element
|
|
|
|
for e := c.requestList.Front(); e != nil; e = nextElem {
|
|
|
|
nextElem = e.Next()
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
jReq := e.Value.(*jsonRequest)
|
|
|
|
if _, ok := ignoreResends[jReq.method]; ok {
|
2014-06-11 20:23:21 +02:00
|
|
|
// If a request is not sent on reconnect, remove it
|
|
|
|
// from the request structures, since no reply is
|
|
|
|
// expected.
|
2015-01-01 23:34:07 +01:00
|
|
|
delete(c.requestMap, jReq.id)
|
2014-06-11 20:23:21 +02:00
|
|
|
c.requestList.Remove(e)
|
|
|
|
} else {
|
2015-01-01 23:34:07 +01:00
|
|
|
resendReqs = append(resendReqs, jReq)
|
2014-06-11 20:23:21 +02:00
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
c.requestLock.Unlock()
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
for _, jReq := range resendReqs {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Stop resending commands if the client disconnected again
|
|
|
|
// since the next reconnect will handle them.
|
|
|
|
if c.Disconnected() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
log.Tracef("Sending command [%s] with id %d", jReq.method,
|
|
|
|
jReq.id)
|
|
|
|
c.sendMessage(jReq.marshalledJSON)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wsReconnectHandler listens for client disconnects and automatically tries
|
|
|
|
// to reconnect with retry interval that scales based on the number of retries.
|
|
|
|
// It also resends any commands that had not completed when the client
|
|
|
|
// disconnected so the disconnect/reconnect process is largely transparent to
|
|
|
|
// the caller. This function is not run when the DisableAutoReconnect config
|
|
|
|
// options is set.
|
|
|
|
//
|
|
|
|
// This function must be run as a goroutine.
|
|
|
|
func (c *Client) wsReconnectHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.disconnect:
|
|
|
|
// On disconnect, fallthrough to reestablish the
|
|
|
|
// connection.
|
|
|
|
|
|
|
|
case <-c.shutdown:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnect:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
break out
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
wsConn, err := dial(c.config)
|
|
|
|
if err != nil {
|
|
|
|
c.retryCount++
|
|
|
|
log.Infof("Failed to connect to %s: %v",
|
|
|
|
c.config.Host, err)
|
|
|
|
|
|
|
|
// Scale the retry interval by the number of
|
|
|
|
// retries so there is a backoff up to a max
|
|
|
|
// of 1 minute.
|
|
|
|
scaledInterval := connectionRetryInterval.Nanoseconds() * c.retryCount
|
|
|
|
scaledDuration := time.Duration(scaledInterval)
|
|
|
|
if scaledDuration > time.Minute {
|
|
|
|
scaledDuration = time.Minute
|
|
|
|
}
|
|
|
|
log.Infof("Retrying connection to %s in "+
|
|
|
|
"%s", c.config.Host, scaledDuration)
|
|
|
|
time.Sleep(scaledDuration)
|
|
|
|
continue reconnect
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Reestablished connection to RPC server %s",
|
|
|
|
c.config.Host)
|
|
|
|
|
2019-10-30 02:50:11 +01:00
|
|
|
// Reset the version in case the backend was
|
|
|
|
// disconnected due to an upgrade.
|
|
|
|
c.backendVersionMu.Lock()
|
|
|
|
c.backendVersion = nil
|
|
|
|
c.backendVersionMu.Unlock()
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// Reset the connection state and signal the reconnect
|
|
|
|
// has happened.
|
|
|
|
c.wsConn = wsConn
|
|
|
|
c.retryCount = 0
|
|
|
|
|
|
|
|
c.mtx.Lock()
|
2015-06-15 17:38:25 +02:00
|
|
|
c.disconnect = make(chan struct{})
|
2014-05-07 18:14:39 +02:00
|
|
|
c.disconnected = false
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
// Start processing input and output for the
|
|
|
|
// new connection.
|
|
|
|
c.start()
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// Reissue pending requests in another goroutine since
|
2014-05-07 18:14:39 +02:00
|
|
|
// the send can block.
|
2015-01-01 23:34:07 +01:00
|
|
|
go c.resendRequests()
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Break out of the reconnect loop back to wait for
|
|
|
|
// disconnect again.
|
|
|
|
break reconnect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.wg.Done()
|
|
|
|
log.Tracef("RPC client reconnect handler done for %s", c.config.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleSendPostMessage handles performing the passed HTTP request, reading the
|
2015-01-01 23:34:07 +01:00
|
|
|
// result, unmarshalling it, and delivering the unmarshalled result to the
|
2014-05-07 18:14:39 +02:00
|
|
|
// provided response channel.
|
2021-08-21 15:04:31 +02:00
|
|
|
func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
|
|
|
|
protocol := "http"
|
|
|
|
if !c.config.DisableTLS {
|
|
|
|
protocol = "https"
|
|
|
|
}
|
|
|
|
url := protocol + "://" + c.config.Host
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var backoff time.Duration
|
|
|
|
var httpResponse *http.Response
|
|
|
|
tries := 10
|
|
|
|
for i := 0; tries == 0 || i < tries; i++ {
|
|
|
|
bodyReader := bytes.NewReader(jReq.marshalledJSON)
|
|
|
|
httpReq, err := http.NewRequest("POST", url, bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
jReq.responseChan <- &Response{result: nil, err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httpReq.Close = true
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
for key, value := range c.config.ExtraHeaders {
|
|
|
|
httpReq.Header.Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure basic access authorization.
|
|
|
|
user, pass, err := c.config.getAuth()
|
|
|
|
if err != nil {
|
|
|
|
jReq.responseChan <- &Response{result: nil, err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httpReq.SetBasicAuth(user, pass)
|
|
|
|
|
|
|
|
httpResponse, err = c.httpClient.Do(httpReq)
|
|
|
|
if err != nil {
|
|
|
|
backoff = requestRetryInterval * time.Duration(i+1)
|
|
|
|
if backoff > time.Minute {
|
|
|
|
backoff = time.Minute
|
|
|
|
}
|
|
|
|
log.Debugf("Failed command [%s] with id %d attempt %d. Retrying in %v... \n", jReq.method, jReq.id, i, backoff)
|
|
|
|
time.Sleep(backoff)
|
|
|
|
continue
|
|
|
|
}
|
2021-10-16 00:16:58 +02:00
|
|
|
defer httpResponse.Body.Close()
|
2021-08-21 15:04:31 +02:00
|
|
|
break
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{err: err}
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-16 00:16:58 +02:00
|
|
|
// Read the raw bytes from the response.
|
2015-01-01 23:34:07 +01:00
|
|
|
respBytes, err := ioutil.ReadAll(httpResponse.Body)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2015-01-01 23:34:07 +01:00
|
|
|
err = fmt.Errorf("error reading json reply: %v", err)
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{err: err}
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
2014-07-03 14:04:58 +02:00
|
|
|
|
2016-09-16 21:40:27 +02:00
|
|
|
// Try to unmarshal the response as a regular JSON-RPC response.
|
2014-06-18 17:16:09 +02:00
|
|
|
var resp rawResponse
|
2020-12-06 04:39:40 +01:00
|
|
|
var batchResponse json.RawMessage
|
|
|
|
if c.batch {
|
|
|
|
err = json.Unmarshal(respBytes, &batchResponse)
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
|
|
}
|
2014-06-18 17:16:09 +02:00
|
|
|
if err != nil {
|
2016-09-16 21:40:27 +02:00
|
|
|
// When the response itself isn't a valid JSON-RPC response
|
|
|
|
// return an error which includes the HTTP status code and raw
|
|
|
|
// response bytes.
|
|
|
|
err = fmt.Errorf("status code: %d, response: %q",
|
|
|
|
httpResponse.StatusCode, string(respBytes))
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{err: err}
|
2014-06-18 17:16:09 +02:00
|
|
|
return
|
|
|
|
}
|
2020-12-06 04:39:40 +01:00
|
|
|
var res []byte
|
|
|
|
if c.batch {
|
|
|
|
// errors must be dealt with downstream since a whole request cannot
|
|
|
|
// "error out" other than through the status code error handled above
|
|
|
|
res, err = batchResponse, nil
|
|
|
|
} else {
|
|
|
|
res, err = resp.result()
|
|
|
|
}
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{result: res, err: err}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendPostHandler handles all outgoing messages when the client is running
|
|
|
|
// in HTTP POST mode. It uses a buffered channel to serialize output messages
|
|
|
|
// while allowing the sender to continue running asynchronously. It must be run
|
|
|
|
// as a goroutine.
|
|
|
|
func (c *Client) sendPostHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
// Send any messages ready for send until the shutdown channel
|
|
|
|
// is closed.
|
|
|
|
select {
|
2021-08-21 15:04:31 +02:00
|
|
|
case jReq := <-c.sendPostChan:
|
|
|
|
c.handleSendPostMessage(jReq)
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
case <-c.shutdown:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain any wait channels before exiting so nothing is left waiting
|
|
|
|
// around to send.
|
|
|
|
cleanup:
|
|
|
|
for {
|
|
|
|
select {
|
2021-08-21 15:04:31 +02:00
|
|
|
case jReq := <-c.sendPostChan:
|
|
|
|
jReq.responseChan <- &Response{
|
2014-06-10 02:49:41 +02:00
|
|
|
result: nil,
|
|
|
|
err: ErrClientShutdown,
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break cleanup
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.wg.Done()
|
|
|
|
log.Tracef("RPC client send handler done for %s", c.config.Host)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendPostRequest sends the passed HTTP request to the RPC server using the
|
|
|
|
// HTTP client associated with the client. It is backed by a buffered channel,
|
|
|
|
// so it will not block until the send channel is full.
|
2021-08-21 15:04:31 +02:00
|
|
|
func (c *Client) sendPostRequest(jReq *jsonRequest) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Don't send the message if shutting down.
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{result: nil, err: ErrClientShutdown}
|
2014-05-07 18:14:39 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-08-21 15:04:31 +02:00
|
|
|
log.Tracef("Sending command [%s] with id %d", jReq.method, jReq.id)
|
|
|
|
|
|
|
|
c.sendPostChan <- jReq
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFutureError returns a new future result channel that already has the
|
|
|
|
// passed error waitin on the channel with the reply set to nil. This is useful
|
|
|
|
// to easily return errors from the various Async functions.
|
2021-09-02 08:39:55 +02:00
|
|
|
func newFutureError(err error) chan *Response {
|
|
|
|
responseChan := make(chan *Response, 1)
|
|
|
|
responseChan <- &Response{err: err}
|
2014-05-07 18:14:39 +02:00
|
|
|
return responseChan
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// ReceiveFuture receives from the passed futureResult channel to extract a
|
2014-05-07 18:14:39 +02:00
|
|
|
// reply or any errors. The examined errors include an error in the
|
|
|
|
// futureResult and the error in the reply from the server. This will block
|
|
|
|
// until the result is available on the passed channel.
|
2021-09-02 08:39:55 +02:00
|
|
|
func ReceiveFuture(f chan *Response) ([]byte, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Wait for a response on the returned channel.
|
2014-06-10 02:49:41 +02:00
|
|
|
r := <-f
|
|
|
|
return r.result, r.err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
// sendRequest sends the passed json request to the associated server using the
|
|
|
|
// provided response channel for the reply. It handles both websocket and HTTP
|
|
|
|
// POST mode depending on the configuration of the client.
|
|
|
|
func (c *Client) sendRequest(jReq *jsonRequest) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Choose which marshal and send function to use depending on whether
|
|
|
|
// the client running in HTTP POST mode or not. When running in HTTP
|
|
|
|
// POST mode, the command is issued via an HTTP client. Otherwise,
|
|
|
|
// the command is issued via the asynchronous websocket channels.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2020-12-06 04:39:40 +01:00
|
|
|
if c.batch {
|
|
|
|
if err := c.addRequest(jReq); err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-21 15:04:31 +02:00
|
|
|
c.sendPostRequest(jReq)
|
2020-12-06 04:39:40 +01:00
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
return
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// Check whether the websocket connection has never been established,
|
|
|
|
// in which case the handler goroutines are not running.
|
|
|
|
select {
|
|
|
|
case <-c.connEstablished:
|
|
|
|
default:
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{err: ErrClientNotConnected}
|
2015-01-01 23:34:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the request to the internal tracking map so the response from the
|
|
|
|
// remote server can be properly detected and routed to the response
|
|
|
|
// channel. Then send the marshalled request via the websocket
|
|
|
|
// connection.
|
|
|
|
if err := c.addRequest(jReq); err != nil {
|
2021-09-02 08:39:55 +02:00
|
|
|
jReq.responseChan <- &Response{err: err}
|
2015-01-01 23:34:07 +01:00
|
|
|
return
|
2014-07-10 20:11:57 +02:00
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
log.Tracef("Sending command [%s] with id %d", jReq.method, jReq.id)
|
|
|
|
c.sendMessage(jReq.marshalledJSON)
|
|
|
|
}
|
2014-07-10 20:11:57 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// SendCmd sends the passed command to the associated server and returns a
|
2015-01-01 23:34:07 +01:00
|
|
|
// response channel on which the reply will be delivered at some point in the
|
|
|
|
// future. It handles both websocket and HTTP POST mode depending on the
|
|
|
|
// configuration of the client.
|
2021-09-02 08:39:55 +02:00
|
|
|
func (c *Client) SendCmd(cmd interface{}) chan *Response {
|
2020-12-06 04:39:40 +01:00
|
|
|
rpcVersion := btcjson.RpcVersion1
|
|
|
|
if c.batch {
|
|
|
|
rpcVersion = btcjson.RpcVersion2
|
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
// Get the method associated with the command.
|
|
|
|
method, err := btcjson.CmdMethod(cmd)
|
2014-07-08 00:48:38 +02:00
|
|
|
if err != nil {
|
2015-01-01 23:34:07 +01:00
|
|
|
return newFutureError(err)
|
2014-07-08 00:48:38 +02:00
|
|
|
}
|
2015-01-01 23:34:07 +01:00
|
|
|
|
|
|
|
// Marshal the command.
|
|
|
|
id := c.NextID()
|
2020-12-06 04:39:40 +01:00
|
|
|
marshalledJSON, err := btcjson.MarshalCmd(rpcVersion, id, cmd)
|
2015-01-01 23:34:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return newFutureError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the request and send it along with a channel to respond on.
|
2021-09-02 08:39:55 +02:00
|
|
|
responseChan := make(chan *Response, 1)
|
2015-01-01 23:34:07 +01:00
|
|
|
jReq := &jsonRequest{
|
|
|
|
id: id,
|
|
|
|
method: method,
|
|
|
|
cmd: cmd,
|
|
|
|
marshalledJSON: marshalledJSON,
|
|
|
|
responseChan: responseChan,
|
|
|
|
}
|
2020-12-06 04:39:40 +01:00
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
c.sendRequest(jReq)
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
return responseChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCmdAndWait sends the passed command to the associated server, waits
|
|
|
|
// for the reply, and returns the result from it. It will return the error
|
|
|
|
// field in the reply if there is one.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) sendCmdAndWait(cmd interface{}) (interface{}, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Marshal the command to JSON-RPC, send it to the connected server, and
|
|
|
|
// wait for a response on the returned channel.
|
2021-09-02 08:39:55 +02:00
|
|
|
return ReceiveFuture(c.SendCmd(cmd))
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// Disconnected returns whether or not the server is disconnected. If a
|
|
|
|
// websocket client was created but never connected, this also returns false.
|
2014-05-06 02:51:33 +02:00
|
|
|
func (c *Client) Disconnected() bool {
|
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
select {
|
|
|
|
case <-c.connEstablished:
|
|
|
|
return c.disconnected
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2014-05-06 02:51:33 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 03:11:01 +02:00
|
|
|
// doDisconnect disconnects the websocket associated with the client if it
|
|
|
|
// hasn't already been disconnected. It will return false if the disconnect is
|
|
|
|
// not needed or the client is running in HTTP POST mode.
|
2014-05-07 18:14:39 +02:00
|
|
|
//
|
2014-07-08 03:11:01 +02:00
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (c *Client) doDisconnect() bool {
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2014-07-08 03:11:01 +02:00
|
|
|
return false
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
|
|
|
|
|
|
|
// Nothing to do if already disconnected.
|
|
|
|
if c.disconnected {
|
2014-07-08 03:11:01 +02:00
|
|
|
return false
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("Disconnecting RPC client %s", c.config.Host)
|
|
|
|
close(c.disconnect)
|
2014-07-10 20:11:57 +02:00
|
|
|
if c.wsConn != nil {
|
|
|
|
c.wsConn.Close()
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
c.disconnected = true
|
2014-07-08 03:11:01 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// doShutdown closes the shutdown channel and logs the shutdown unless shutdown
|
|
|
|
// is already in progress. It will return false if the shutdown is not needed.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (c *Client) doShutdown() bool {
|
|
|
|
// Ignore the shutdown request if the client is already in the process
|
|
|
|
// of shutting down or already shutdown.
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("Shutting down RPC client %s", c.config.Host)
|
|
|
|
close(c.shutdown)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect disconnects the current websocket associated with the client. The
|
|
|
|
// connection will automatically be re-established unless the client was
|
|
|
|
// created with the DisableAutoReconnect flag.
|
|
|
|
//
|
|
|
|
// This function has no effect when the client is running in HTTP POST mode.
|
|
|
|
func (c *Client) Disconnect() {
|
|
|
|
// Nothing to do if already disconnected or running in HTTP POST mode.
|
|
|
|
if !c.doDisconnect() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.requestLock.Lock()
|
|
|
|
defer c.requestLock.Unlock()
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// When operating without auto reconnect, send errors to any pending
|
|
|
|
// requests and shutdown the client.
|
|
|
|
if c.config.DisableAutoReconnect {
|
|
|
|
for e := c.requestList.Front(); e != nil; e = e.Next() {
|
|
|
|
req := e.Value.(*jsonRequest)
|
2021-09-02 08:39:55 +02:00
|
|
|
req.responseChan <- &Response{
|
2014-06-10 02:49:41 +02:00
|
|
|
result: nil,
|
|
|
|
err: ErrClientDisconnect,
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.removeAllRequests()
|
2014-07-08 03:11:01 +02:00
|
|
|
c.doShutdown()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the client by disconnecting any connections associated
|
|
|
|
// with the client and, when automatic reconnect is enabled, preventing future
|
|
|
|
// attempts to reconnect. It also stops all goroutines.
|
|
|
|
func (c *Client) Shutdown() {
|
2014-07-08 03:11:01 +02:00
|
|
|
// Do the shutdown under the request lock to prevent clients from
|
|
|
|
// adding new requests while the client shutdown process is initiated.
|
|
|
|
c.requestLock.Lock()
|
|
|
|
defer c.requestLock.Unlock()
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// Ignore the shutdown request if the client is already in the process
|
|
|
|
// of shutting down or already shutdown.
|
2014-07-08 03:11:01 +02:00
|
|
|
if !c.doShutdown() {
|
2014-05-07 18:14:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the ErrClientShutdown error to any pending requests.
|
|
|
|
for e := c.requestList.Front(); e != nil; e = e.Next() {
|
|
|
|
req := e.Value.(*jsonRequest)
|
2021-09-02 08:39:55 +02:00
|
|
|
req.responseChan <- &Response{
|
2014-06-10 02:49:41 +02:00
|
|
|
result: nil,
|
|
|
|
err: ErrClientShutdown,
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.removeAllRequests()
|
2014-05-02 06:12:02 +02:00
|
|
|
|
2014-07-08 03:11:01 +02:00
|
|
|
// Disconnect the client if needed.
|
|
|
|
c.doDisconnect()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// start begins processing input and output messages.
|
2014-05-07 18:14:39 +02:00
|
|
|
func (c *Client) start() {
|
|
|
|
log.Tracef("Starting RPC client %s", c.config.Host)
|
|
|
|
|
|
|
|
// Start the I/O processing handlers depending on whether the client is
|
|
|
|
// in HTTP POST mode or the default websocket mode.
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2014-05-07 18:14:39 +02:00
|
|
|
c.wg.Add(1)
|
|
|
|
go c.sendPostHandler()
|
|
|
|
} else {
|
2014-06-04 21:58:31 +02:00
|
|
|
c.wg.Add(3)
|
|
|
|
go func() {
|
|
|
|
if c.ntfnHandlers != nil {
|
|
|
|
if c.ntfnHandlers.OnClientConnected != nil {
|
|
|
|
c.ntfnHandlers.OnClientConnected()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.wg.Done()
|
|
|
|
}()
|
2014-05-07 18:14:39 +02:00
|
|
|
go c.wsInHandler()
|
|
|
|
go c.wsOutHandler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForShutdown blocks until the client goroutines are stopped and the
|
|
|
|
// connection is closed.
|
|
|
|
func (c *Client) WaitForShutdown() {
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnConfig describes the connection configuration parameters for the client.
|
|
|
|
// This
|
|
|
|
type ConnConfig struct {
|
|
|
|
// Host is the IP address and port of the RPC server you want to connect
|
|
|
|
// to.
|
|
|
|
Host string
|
|
|
|
|
|
|
|
// Endpoint is the websocket endpoint on the RPC server. This is
|
2014-06-12 20:00:42 +02:00
|
|
|
// typically "ws".
|
2014-05-07 18:14:39 +02:00
|
|
|
Endpoint string
|
|
|
|
|
|
|
|
// User is the username to use to authenticate to the RPC server.
|
|
|
|
User string
|
|
|
|
|
|
|
|
// Pass is the passphrase to use to authenticate to the RPC server.
|
|
|
|
Pass string
|
|
|
|
|
2020-05-25 22:15:11 +02:00
|
|
|
// CookiePath is the path to a cookie file containing the username and
|
|
|
|
// passphrase to use to authenticate to the RPC server. It is used
|
|
|
|
// instead of User and Pass if non-empty.
|
|
|
|
CookiePath string
|
|
|
|
|
2019-10-09 13:23:05 +02:00
|
|
|
cookieLastCheckTime time.Time
|
|
|
|
cookieLastModTime time.Time
|
|
|
|
cookieLastUser string
|
|
|
|
cookieLastPass string
|
|
|
|
cookieLastErr error
|
2020-05-25 22:15:11 +02:00
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// Params is the string representing the network that the server
|
|
|
|
// is running. If there is no parameter set in the config, then
|
|
|
|
// mainnet will be used by default.
|
|
|
|
Params string
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// DisableTLS specifies whether transport layer security should be
|
|
|
|
// disabled. It is recommended to always use TLS if the RPC server
|
|
|
|
// supports it as otherwise your username and password is sent across
|
|
|
|
// the wire in cleartext.
|
|
|
|
DisableTLS bool
|
|
|
|
|
|
|
|
// Certificates are the bytes for a PEM-encoded certificate chain used
|
|
|
|
// for the TLS connection. It has no effect if the DisableTLS parameter
|
|
|
|
// is true.
|
|
|
|
Certificates []byte
|
|
|
|
|
|
|
|
// Proxy specifies to connect through a SOCKS 5 proxy server. It may
|
|
|
|
// be an empty string if a proxy is not required.
|
|
|
|
Proxy string
|
|
|
|
|
|
|
|
// ProxyUser is an optional username to use for the proxy server if it
|
|
|
|
// requires authentication. It has no effect if the Proxy parameter
|
|
|
|
// is not set.
|
|
|
|
ProxyUser string
|
|
|
|
|
|
|
|
// ProxyPass is an optional password to use for the proxy server if it
|
|
|
|
// requires authentication. It has no effect if the Proxy parameter
|
|
|
|
// is not set.
|
|
|
|
ProxyPass string
|
|
|
|
|
|
|
|
// DisableAutoReconnect specifies the client should not automatically
|
|
|
|
// try to reconnect to the server when it has been disconnected.
|
|
|
|
DisableAutoReconnect bool
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// DisableConnectOnNew specifies that a websocket client connection
|
|
|
|
// should not be tried when creating the client with New. Instead, the
|
|
|
|
// client is created and returned unconnected, and Connect must be
|
|
|
|
// called manually.
|
|
|
|
DisableConnectOnNew bool
|
|
|
|
|
2015-01-16 01:14:45 +01:00
|
|
|
// HTTPPostMode instructs the client to run using multiple independent
|
2014-05-07 18:14:39 +02:00
|
|
|
// connections issuing HTTP POST requests instead of using the default
|
|
|
|
// of websockets. Websockets are generally preferred as some of the
|
|
|
|
// features of the client such notifications only work with websockets,
|
|
|
|
// however, not all servers support the websocket extensions, so this
|
|
|
|
// flag can be set to true to use basic HTTP POST requests instead.
|
2015-01-16 01:14:45 +01:00
|
|
|
HTTPPostMode bool
|
2014-09-10 17:40:47 +02:00
|
|
|
|
2020-11-20 10:58:10 +01:00
|
|
|
// ExtraHeaders specifies the extra headers when perform request. It's
|
|
|
|
// useful when RPC provider need customized headers.
|
|
|
|
ExtraHeaders map[string]string
|
|
|
|
|
2017-10-29 20:56:03 +01:00
|
|
|
// EnableBCInfoHacks is an option provided to enable compatibility hacks
|
2014-09-10 17:40:47 +02:00
|
|
|
// when connecting to blockchain.info RPC server
|
|
|
|
EnableBCInfoHacks bool
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:15:11 +02:00
|
|
|
// getAuth returns the username and passphrase that will actually be used for
|
|
|
|
// this connection. This will be the result of checking the cookie if a cookie
|
|
|
|
// path is configured; if not, it will be the user-configured username and
|
|
|
|
// passphrase.
|
|
|
|
func (config *ConnConfig) getAuth() (username, passphrase string, err error) {
|
2019-10-09 13:08:42 +02:00
|
|
|
// Try username+passphrase auth first.
|
|
|
|
if config.Pass != "" {
|
2020-05-25 22:15:11 +02:00
|
|
|
return config.User, config.Pass, nil
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:08:42 +02:00
|
|
|
// If no username or passphrase is set, try cookie auth.
|
2019-10-09 13:23:05 +02:00
|
|
|
return config.retrieveCookie()
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieveCookie returns the cookie username and passphrase.
|
|
|
|
func (config *ConnConfig) retrieveCookie() (username, passphrase string, err error) {
|
|
|
|
if !config.cookieLastCheckTime.IsZero() && time.Now().Before(config.cookieLastCheckTime.Add(30*time.Second)) {
|
|
|
|
return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr
|
2020-05-25 22:15:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 13:23:05 +02:00
|
|
|
config.cookieLastCheckTime = time.Now()
|
|
|
|
|
|
|
|
st, err := os.Stat(config.CookiePath)
|
|
|
|
if err != nil {
|
|
|
|
config.cookieLastErr = err
|
|
|
|
return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
modTime := st.ModTime()
|
|
|
|
if !modTime.Equal(config.cookieLastModTime) {
|
|
|
|
config.cookieLastModTime = modTime
|
|
|
|
config.cookieLastUser, config.cookieLastPass, config.cookieLastErr = readCookieFile(config.CookiePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr
|
2020-05-25 22:15:11 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 06:00:03 +02:00
|
|
|
// newHTTPClient returns a new http client that is configured according to the
|
2014-05-07 18:14:39 +02:00
|
|
|
// proxy and TLS settings in the associated connection configuration.
|
2014-06-13 06:00:03 +02:00
|
|
|
func newHTTPClient(config *ConnConfig) (*http.Client, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
// Set proxy function if there is a proxy configured.
|
|
|
|
var proxyFunc func(*http.Request) (*url.URL, error)
|
|
|
|
if config.Proxy != "" {
|
|
|
|
proxyURL, err := url.Parse(config.Proxy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proxyFunc = http.ProxyURL(proxyURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure TLS if needed.
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if !config.DisableTLS {
|
2014-06-26 04:48:40 +02:00
|
|
|
if len(config.Certificates) > 0 {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AppendCertsFromPEM(config.Certificates)
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
RootCAs: pool,
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: proxyFunc,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial opens a websocket connection using the passed connection configuration
|
|
|
|
// details.
|
|
|
|
func dial(config *ConnConfig) (*websocket.Conn, error) {
|
2014-05-10 02:26:56 +02:00
|
|
|
// Setup TLS if not disabled.
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
var scheme = "ws"
|
|
|
|
if !config.DisableTLS {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2014-06-26 04:48:40 +02:00
|
|
|
if len(config.Certificates) > 0 {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AppendCertsFromPEM(config.Certificates)
|
|
|
|
tlsConfig.RootCAs = pool
|
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
scheme = "wss"
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 02:26:56 +02:00
|
|
|
// Create a websocket dialer that will be used to make the connection.
|
|
|
|
// It is modified by the proxy setting below as needed.
|
|
|
|
dialer := websocket.Dialer{TLSClientConfig: tlsConfig}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2014-05-10 02:26:56 +02:00
|
|
|
// Setup the proxy if one is configured.
|
2014-05-07 18:14:39 +02:00
|
|
|
if config.Proxy != "" {
|
|
|
|
proxy := &socks.Proxy{
|
|
|
|
Addr: config.Proxy,
|
|
|
|
Username: config.ProxyUser,
|
|
|
|
Password: config.ProxyPass,
|
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
dialer.NetDial = proxy.Dial
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 02:26:56 +02:00
|
|
|
// The RPC server requires basic authorization, so create a custom
|
|
|
|
// request header with the Authorization header set.
|
2020-05-25 22:15:11 +02:00
|
|
|
user, pass, err := config.getAuth()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
login := user + ":" + pass
|
2014-05-10 02:26:56 +02:00
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
|
|
|
|
requestHeader := make(http.Header)
|
|
|
|
requestHeader.Add("Authorization", auth)
|
2020-11-20 10:58:10 +01:00
|
|
|
for key, value := range config.ExtraHeaders {
|
|
|
|
requestHeader.Add(key, value)
|
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
|
|
|
|
// Dial the connection.
|
|
|
|
url := fmt.Sprintf("%s://%s/%s", scheme, config.Host, config.Endpoint)
|
|
|
|
wsConn, resp, err := dialer.Dial(url, requestHeader)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
2014-07-03 19:26:45 +02:00
|
|
|
if err != websocket.ErrBadHandshake || resp == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
|
2014-07-03 19:26:45 +02:00
|
|
|
// Detect HTTP authentication error status codes.
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized ||
|
|
|
|
resp.StatusCode == http.StatusForbidden {
|
|
|
|
return nil, ErrInvalidAuth
|
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
|
2014-07-03 19:26:45 +02:00
|
|
|
// The connection was authenticated and the status response was
|
|
|
|
// ok, but the websocket handshake still failed, so the endpoint
|
|
|
|
// is invalid in some way.
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
2014-05-10 02:26:56 +02:00
|
|
|
return nil, ErrInvalidEndpoint
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-05-10 02:26:56 +02:00
|
|
|
|
2014-07-03 19:26:45 +02:00
|
|
|
// Return the status text from the server if none of the special
|
|
|
|
// cases above apply.
|
|
|
|
return nil, errors.New(resp.Status)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2021-10-16 00:16:58 +02:00
|
|
|
resp.Body.Close()
|
2014-05-10 02:26:56 +02:00
|
|
|
return wsConn, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 16:31:02 +02:00
|
|
|
// New creates a new RPC client based on the provided connection configuration
|
2014-05-07 18:14:39 +02:00
|
|
|
// details. The notification handlers parameter may be nil if you are not
|
2016-01-02 22:06:55 +01:00
|
|
|
// interested in receiving notifications and will be ignored if the
|
2014-05-07 18:14:39 +02:00
|
|
|
// configuration is set to run in HTTP POST mode.
|
|
|
|
func New(config *ConnConfig, ntfnHandlers *NotificationHandlers) (*Client, error) {
|
|
|
|
// Either open a websocket connection or create an HTTP client depending
|
|
|
|
// on the HTTP POST mode. Also, set the notification handlers to nil
|
|
|
|
// when running in HTTP POST mode.
|
|
|
|
var wsConn *websocket.Conn
|
|
|
|
var httpClient *http.Client
|
2014-07-10 20:11:57 +02:00
|
|
|
connEstablished := make(chan struct{})
|
|
|
|
var start bool
|
2015-01-16 01:14:45 +01:00
|
|
|
if config.HTTPPostMode {
|
2014-05-07 18:14:39 +02:00
|
|
|
ntfnHandlers = nil
|
2014-07-10 20:11:57 +02:00
|
|
|
start = true
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
var err error
|
2014-06-13 06:00:03 +02:00
|
|
|
httpClient, err = newHTTPClient(config)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-10 20:11:57 +02:00
|
|
|
if !config.DisableConnectOnNew {
|
|
|
|
var err error
|
|
|
|
wsConn, err = dial(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
start = true
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &Client{
|
2014-07-10 20:11:57 +02:00
|
|
|
config: config,
|
|
|
|
wsConn: wsConn,
|
|
|
|
httpClient: httpClient,
|
|
|
|
requestMap: make(map[uint64]*list.Element),
|
|
|
|
requestList: list.New(),
|
2020-12-06 04:39:40 +01:00
|
|
|
batch: false,
|
|
|
|
batchList: list.New(),
|
2014-07-10 20:11:57 +02:00
|
|
|
ntfnHandlers: ntfnHandlers,
|
|
|
|
ntfnState: newNotificationState(),
|
|
|
|
sendChan: make(chan []byte, sendBufferSize),
|
2021-08-21 15:04:31 +02:00
|
|
|
sendPostChan: make(chan *jsonRequest, sendPostBufferSize),
|
2014-07-10 20:11:57 +02:00
|
|
|
connEstablished: connEstablished,
|
|
|
|
disconnect: make(chan struct{}),
|
|
|
|
shutdown: make(chan struct{}),
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:46:29 +01:00
|
|
|
// Default network is mainnet, no parameters are necessary but if mainnet
|
|
|
|
// is specified it will be the param
|
|
|
|
switch config.Params {
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case chaincfg.MainNetParams.Name:
|
|
|
|
client.chainParams = &chaincfg.MainNetParams
|
|
|
|
case chaincfg.TestNet3Params.Name:
|
|
|
|
client.chainParams = &chaincfg.TestNet3Params
|
|
|
|
case chaincfg.RegressionNetParams.Name:
|
|
|
|
client.chainParams = &chaincfg.RegressionNetParams
|
|
|
|
case chaincfg.SimNetParams.Name:
|
|
|
|
client.chainParams = &chaincfg.SimNetParams
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("rpcclient.New: Unknown chain %s", config.Params)
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
if start {
|
2015-11-21 21:28:18 +01:00
|
|
|
log.Infof("Established connection to RPC server %s",
|
|
|
|
config.Host)
|
2014-07-10 20:11:57 +02:00
|
|
|
close(connEstablished)
|
|
|
|
client.start()
|
2015-01-16 01:14:45 +01:00
|
|
|
if !client.config.HTTPPostMode && !client.config.DisableAutoReconnect {
|
2014-07-10 20:11:57 +02:00
|
|
|
client.wg.Add(1)
|
|
|
|
go client.wsReconnectHandler()
|
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
2014-07-10 20:11:57 +02:00
|
|
|
|
2020-12-06 04:39:40 +01:00
|
|
|
// Batch is a factory that creates a client able to interact with the server using
|
|
|
|
// JSON-RPC 2.0. The client is capable of accepting an arbitrary number of requests
|
|
|
|
// and having the server process the all at the same time. It's compatible with both
|
|
|
|
// btcd and bitcoind
|
|
|
|
func NewBatch(config *ConnConfig) (*Client, error) {
|
|
|
|
if !config.HTTPPostMode {
|
|
|
|
return nil, errors.New("http post mode is required to use batch client")
|
|
|
|
}
|
|
|
|
// notification parameter is nil since notifications are not supported in POST mode.
|
|
|
|
client, err := New(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.batch = true //copy the client with changed batch setting
|
|
|
|
client.start()
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:11:57 +02:00
|
|
|
// Connect establishes the initial websocket connection. This is necessary when
|
|
|
|
// a client was created after setting the DisableConnectOnNew field of the
|
|
|
|
// Config struct.
|
|
|
|
//
|
|
|
|
// Up to tries number of connections (each after an increasing backoff) will
|
|
|
|
// be tried if the connection can not be established. The special value of 0
|
|
|
|
// indicates an unlimited number of connection attempts.
|
|
|
|
//
|
|
|
|
// This method will error if the client is not configured for websockets, if the
|
|
|
|
// connection has already been established, or if none of the connection
|
|
|
|
// attempts were successful.
|
|
|
|
func (c *Client) Connect(tries int) error {
|
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
|
|
|
|
2015-01-16 01:14:45 +01:00
|
|
|
if c.config.HTTPPostMode {
|
2014-07-10 20:11:57 +02:00
|
|
|
return ErrNotWebsocketClient
|
|
|
|
}
|
|
|
|
if c.wsConn != nil {
|
|
|
|
return ErrClientAlreadyConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin connection attempts. Increase the backoff after each failed
|
|
|
|
// attempt, up to a maximum of one minute.
|
|
|
|
var err error
|
|
|
|
var backoff time.Duration
|
|
|
|
for i := 0; tries == 0 || i < tries; i++ {
|
|
|
|
var wsConn *websocket.Conn
|
|
|
|
wsConn, err = dial(c.config)
|
|
|
|
if err != nil {
|
|
|
|
backoff = connectionRetryInterval * time.Duration(i+1)
|
|
|
|
if backoff > time.Minute {
|
|
|
|
backoff = time.Minute
|
|
|
|
}
|
|
|
|
time.Sleep(backoff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection was established. Set the websocket connection
|
|
|
|
// member of the client and start the goroutines necessary
|
|
|
|
// to run the client.
|
2015-11-21 21:28:18 +01:00
|
|
|
log.Infof("Established connection to RPC server %s",
|
|
|
|
c.config.Host)
|
2014-07-10 20:11:57 +02:00
|
|
|
c.wsConn = wsConn
|
|
|
|
close(c.connEstablished)
|
|
|
|
c.start()
|
|
|
|
if !c.config.DisableAutoReconnect {
|
|
|
|
c.wg.Add(1)
|
|
|
|
go c.wsReconnectHandler()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// All connection attempts failed, so return the last error.
|
|
|
|
return err
|
|
|
|
}
|
2019-10-30 02:50:11 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
// bitcoind19Str is the string representation of bitcoind v0.19.0.
|
|
|
|
bitcoind19Str = "0.19.0"
|
|
|
|
|
|
|
|
// bitcoindVersionPrefix specifies the prefix included in every bitcoind
|
|
|
|
// version exposed through GetNetworkInfo.
|
|
|
|
bitcoindVersionPrefix = "/Satoshi:"
|
|
|
|
|
|
|
|
// bitcoindVersionSuffix specifies the suffix included in every bitcoind
|
|
|
|
// version exposed through GetNetworkInfo.
|
|
|
|
bitcoindVersionSuffix = "/"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseBitcoindVersion parses the bitcoind version from its string
|
|
|
|
// representation.
|
|
|
|
func parseBitcoindVersion(version string) BackendVersion {
|
|
|
|
// Trim the version of its prefix and suffix to determine the
|
|
|
|
// appropriate version number.
|
|
|
|
version = strings.TrimPrefix(
|
|
|
|
strings.TrimSuffix(version, bitcoindVersionSuffix),
|
|
|
|
bitcoindVersionPrefix,
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case version < bitcoind19Str:
|
|
|
|
return BitcoindPre19
|
|
|
|
default:
|
|
|
|
return BitcoindPost19
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackendVersion retrieves the version of the backend the client is currently
|
|
|
|
// connected to.
|
|
|
|
func (c *Client) BackendVersion() (BackendVersion, error) {
|
|
|
|
c.backendVersionMu.Lock()
|
|
|
|
defer c.backendVersionMu.Unlock()
|
|
|
|
|
|
|
|
if c.backendVersion != nil {
|
|
|
|
return *c.backendVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start by calling GetInfo. This method doesn't exist for
|
|
|
|
// bitcoind nodes as of v0.16.0, so we'll assume the client is connected
|
|
|
|
// to a btcd backend if it does exist.
|
|
|
|
info, err := c.GetInfo()
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
// Parse the btcd version and cache it.
|
|
|
|
case nil:
|
2021-10-15 07:45:32 +02:00
|
|
|
log.Debugf("Detected lbcd version: %v", info.Version)
|
|
|
|
version := Lbcd
|
2019-10-30 02:50:11 +01:00
|
|
|
c.backendVersion = &version
|
|
|
|
return *c.backendVersion, nil
|
|
|
|
|
|
|
|
// Inspect the RPC error to ensure the method was not found, otherwise
|
|
|
|
// we actually ran into an error.
|
|
|
|
case *btcjson.RPCError:
|
|
|
|
if err.Code != btcjson.ErrRPCMethodNotFound.Code {
|
2021-10-15 07:45:32 +02:00
|
|
|
return 0, fmt.Errorf("unable to detect lbcd version: "+
|
2019-10-30 02:50:11 +01:00
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-10-15 07:45:32 +02:00
|
|
|
return 0, fmt.Errorf("unable to detect lbcd version: %v", err)
|
2019-10-30 02:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since the GetInfo method was not found, we assume the client is
|
|
|
|
// connected to a bitcoind backend, which exposes its version through
|
|
|
|
// GetNetworkInfo.
|
|
|
|
networkInfo, err := c.GetNetworkInfo()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to detect bitcoind version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the bitcoind version and cache it.
|
|
|
|
log.Debugf("Detected bitcoind version: %v", networkInfo.SubVersion)
|
|
|
|
version := parseBitcoindVersion(networkInfo.SubVersion)
|
|
|
|
c.backendVersion = &version
|
|
|
|
|
|
|
|
return *c.backendVersion, nil
|
|
|
|
}
|
2020-12-06 04:39:40 +01:00
|
|
|
|
|
|
|
func (c *Client) sendAsync() FutureGetBulkResult {
|
|
|
|
// convert the array of marshalled json requests to a single request we can send
|
2021-09-02 08:39:55 +02:00
|
|
|
responseChan := make(chan *Response, 1)
|
2020-12-06 04:39:40 +01:00
|
|
|
marshalledRequest := []byte("[")
|
|
|
|
for iter := c.batchList.Front(); iter != nil; iter = iter.Next() {
|
|
|
|
request := iter.Value.(*jsonRequest)
|
|
|
|
marshalledRequest = append(marshalledRequest, request.marshalledJSON...)
|
|
|
|
marshalledRequest = append(marshalledRequest, []byte(",")...)
|
|
|
|
}
|
|
|
|
if len(marshalledRequest) > 0 {
|
|
|
|
// removes the trailing comma to process the request individually
|
|
|
|
marshalledRequest = marshalledRequest[:len(marshalledRequest)-1]
|
|
|
|
}
|
|
|
|
marshalledRequest = append(marshalledRequest, []byte("]")...)
|
|
|
|
request := jsonRequest{
|
|
|
|
id: c.NextID(),
|
|
|
|
method: "",
|
|
|
|
cmd: nil,
|
|
|
|
marshalledJSON: marshalledRequest,
|
|
|
|
responseChan: responseChan,
|
|
|
|
}
|
2021-08-21 15:04:31 +02:00
|
|
|
c.sendPostRequest(&request)
|
2020-12-06 04:39:40 +01:00
|
|
|
return responseChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshall's bulk requests and sends to the server
|
|
|
|
// creates a response channel to receive the response
|
|
|
|
func (c *Client) Send() error {
|
|
|
|
// if batchlist is empty, there's nothing to send
|
|
|
|
if c.batchList.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear batchlist in case of an error
|
|
|
|
defer func() {
|
|
|
|
c.batchList = list.New()
|
|
|
|
}()
|
|
|
|
|
|
|
|
result, err := c.sendAsync().Receive()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for iter := c.batchList.Front(); iter != nil; iter = iter.Next() {
|
|
|
|
var requestError error
|
|
|
|
request := iter.Value.(*jsonRequest)
|
|
|
|
individualResult := result[request.id]
|
|
|
|
fullResult, err := json.Marshal(individualResult.Result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if individualResult.Error != nil {
|
|
|
|
requestError = individualResult.Error
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
result := Response{
|
2020-12-06 04:39:40 +01:00
|
|
|
result: fullResult,
|
|
|
|
err: requestError,
|
|
|
|
}
|
|
|
|
request.responseChan <- &result
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|