-
Notifications
You must be signed in to change notification settings - Fork 95
/
multirpc.go
1451 lines (1294 loc) · 39.8 KB
/
multirpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
//go:build lgpl
// https://ethereumnodes.com/ for RPC providers
package eth
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/networks/erc20"
dexeth "decred.org/dcrdex/dex/networks/eth"
"github.com/decred/slog"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)
const (
// failQuarantine is how long we will wait after a failed request before
// trying a provider again.
failQuarantine = time.Minute
// receiptCacheExpiration is how long we will track a receipt after the
// last request. There is no persistent storage, so all receipts are cached
// in-memory.
receiptCacheExpiration = time.Hour
unconfirmedReceiptExpiration = time.Minute
tipCapSuggestionExpiration = time.Hour
brickedFailCount = 100
providerDelimiter = " "
)
// nonceProviderStickiness is the minimum amount of time that must pass between
// requests to DIFFERENT nonce providers. If we use a provider for a
// nonce-sensitive (NS) operation, and later have another NS operation, we will
// use the same provider if < nonceProviderStickiness has passed.
var nonceProviderStickiness = time.Minute
// TODO: Handle rate limiting? From the docs:
// When you are rate limited, your JSON-RPC responses have HTTP Status code 429.
// I don't think we have access to these codes through ethclient.Client, but
// I haven't verified that.
// The suggested tip cap is expected to be very-slowly changing. We'll only
// update once per tipCapSuggestionExpiration.
type cachedTipCap struct {
cap *big.Int
stamp time.Time
}
type combinedRPCClient struct {
*ethclient.Client
rpc *rpc.Client
}
type provider struct {
// host is the domain and tld of the provider, and is used as a identifier
// in logs and as a unique, path- and subdomaion-independent ID for e.g. map
// keys.
host string
ec *combinedRPCClient
ws bool
tipCapV atomic.Value // *cachedTipCap
// tip tracks the best known header as well as any error encount
tip struct {
sync.RWMutex
header *types.Header
headerStamp time.Time
failStamp time.Time
failCount int
}
}
func (p *provider) setTip(header *types.Header) {
p.tip.Lock()
p.tip.header = header
p.tip.headerStamp = time.Now()
p.tip.failStamp = time.Time{}
p.tip.failCount = 0
p.tip.Unlock()
}
// cachedTip retrieves the last known best header.
func (p *provider) cachedTip() *types.Header {
stale := time.Second * 10
if p.ws {
stale = time.Minute * 2
}
p.tip.RLock()
defer p.tip.RUnlock()
if time.Since(p.tip.failStamp) < failQuarantine || time.Since(p.tip.headerStamp) > stale {
return nil
}
return p.tip.header
}
// setFailed should be called after a failed request, the provider is considered
// failed for failQuarantine.
func (p *provider) setFailed() {
p.tip.Lock()
p.tip.failStamp = time.Now()
p.tip.failCount++
p.tip.Unlock()
}
// failed will be true if setFailed has been called in the last failQuarantine.
func (p *provider) failed() bool {
p.tip.Lock()
defer p.tip.Unlock()
return p.tip.failCount > brickedFailCount || time.Since(p.tip.failStamp) < failQuarantine
}
// bestHeader get the best known header from the provider, cached if available,
// otherwise a new RPC call is made.
func (p *provider) bestHeader(ctx context.Context, log dex.Logger) (*types.Header, error) {
// Check if we have a cached header.
if tip := p.cachedTip(); tip != nil {
log.Tracef("using cached header from %q", p.host)
return tip, nil
}
log.Tracef("fetching fresh header from %q", p.host)
hdr, err := p.ec.HeaderByNumber(ctx, nil /* latest */)
if err != nil {
p.setFailed()
return nil, fmt.Errorf("HeaderByNumber error: %w", err)
}
p.setTip(hdr)
return hdr, nil
}
func (p *provider) headerByHash(ctx context.Context, h common.Hash) (*types.Header, error) {
hdr, err := p.ec.HeaderByHash(ctx, h)
if err != nil {
p.setFailed()
return nil, fmt.Errorf("HeaderByHash error: %w", err)
}
return hdr, nil
}
// suggestTipCap returns a tip cap suggestion, cached if available, otherwise a
// new RPC call is made.
func (p *provider) suggestTipCap(ctx context.Context, log dex.Logger) *big.Int {
if cachedV := p.tipCapV.Load(); cachedV != nil {
rec := cachedV.(*cachedTipCap)
if time.Since(rec.stamp) < tipCapSuggestionExpiration {
return rec.cap
}
}
tipCap, err := p.ec.SuggestGasTipCap(ctx)
if err != nil {
p.setFailed()
log.Errorf("error getting tip cap suggestion from %q: %v", p.host, err)
return dexeth.GweiToWei(dexeth.MinGasTipCap)
}
minGasTipCapWei := dexeth.GweiToWei(dexeth.MinGasTipCap)
if tipCap.Cmp(minGasTipCapWei) < 0 {
return tipCap.Set(minGasTipCapWei)
}
p.tipCapV.Store(&cachedTipCap{
cap: tipCap,
stamp: time.Now(),
})
return tipCap
}
// subscribeHeaders starts a listening loop for header updates for a provider.
// The Subscription and header chan are passed in, because error-free
// instantiation of these variable is necessary to accepting that a websocket
// connection is valid, so they are generated early in connectProviders.
func (p *provider) subscribeHeaders(ctx context.Context, sub ethereum.Subscription, h chan *types.Header, log dex.Logger) {
defer sub.Unsubscribe()
var lastWarning time.Time
newSub := func() (ethereum.Subscription, error) {
for {
var err error
sub, err = p.ec.SubscribeNewHead(ctx, h)
if err == nil {
return sub, nil
}
if time.Since(lastWarning) > 5*time.Minute {
log.Warnf("can't resubscribe to %q headers: %v", p.host, err)
}
select {
case <-time.After(time.Second * 30):
case <-ctx.Done():
return nil, context.Canceled
}
}
}
// I thought the filter logs might catch some transactions we could cache
// to avoid rpc calls, but in testing, I get nothing in the channel. May
// revisit later.
// logs := make(chan types.Log, 128)
// newAcctSub := func(retryTimeout time.Duration) ethereum.Subscription {
// config := ethereum.FilterQuery{
// Addresses: []common.Address{addr},
// }
// acctSub, err := p.ec.SubscribeFilterLogs(ctx, config, logs)
// if err != nil {
// log.Errorf("failed to subscribe to filter logs: %v", err)
// return newRetrySubscription(ctx, retryTimeout)
// }
// return acctSub
// }
// // If we fail the first time, don't try again.
// acctSub := newAcctSub(time.Hour * 24 * 365)
// defer acctSub.Unsubscribe()
// Start the background filtering
log.Tracef("handling websocket subscriptions for %q", p.host)
for {
select {
case hdr := <-h:
log.Tracef("%q reported new tip at height %s (%s)", p.host, hdr.Number, hdr.Hash())
p.setTip(hdr)
case err, ok := <-sub.Err():
if !ok {
// Subscription cancelled
return
}
if ctx.Err() != nil || err == nil { // Both conditions indicate normal close
return
}
log.Errorf("%q header subscription error: %v", p.host, err)
log.Infof("Attempting to resubscribe to %q block headers", p.host)
sub, err = newSub()
if err != nil { // context cancelled
return
}
// case l := <-logs:
// log.Tracef("%q log reported: %+v", p.host, l)
// case err, ok := <-acctSub.Err():
// if err != nil && !errors.Is(err, retryError) {
// log.Errorf("%q log subscription error: %v", p.host, err)
// }
// if ok {
// acctSub = newAcctSub(time.Minute * 5)
// }
case <-ctx.Done():
return
}
}
}
// receiptRecord is a cached receipt and its last-access time. Receipts are
// stored in-memory for up to receiptCacheExpiration.
type receiptRecord struct {
r *types.Receipt
lastAccess time.Time
confirmed bool
}
type endpoint struct {
addr string
jwt string
}
// multiRPCClient is an ethFetcher backed by one or more public RPC providers.
type multiRPCClient struct {
cfg *params.ChainConfig
creds *accountCredentials
log dex.Logger
chainID *big.Int
providerMtx sync.Mutex
endpoints []endpoint
providers []*provider
lastNonce struct {
sync.Mutex
nonce uint64
stamp time.Time
}
// When we send transactions close together, we'll want to use the same
// provider.
lastProvider struct {
sync.Mutex
*provider
stamp time.Time
}
receipts struct {
sync.RWMutex
cache map[common.Hash]*receiptRecord
lastClean time.Time
}
}
var _ ethFetcher = (*multiRPCClient)(nil)
func newMultiRPCClient(dir string, endpoints []endpoint, log dex.Logger, cfg *params.ChainConfig, chainID *big.Int, net dex.Network) (*multiRPCClient, error) {
walletDir := getWalletDir(dir, net)
creds, err := pathCredentials(filepath.Join(walletDir, "keystore"))
if err != nil {
return nil, fmt.Errorf("error parsing credentials from %q: %w", dir, err)
}
m := &multiRPCClient{
cfg: cfg,
log: log,
creds: creds,
chainID: chainID,
endpoints: endpoints,
}
m.receipts.cache = make(map[common.Hash]*receiptRecord)
m.receipts.lastClean = time.Now()
return m, nil
}
// connectProviders attempts to connect to the list of endpoints, returning a
// list of providers that were successfully connected. It is not an error for a
// connection to fail. The caller can infer failed connections from the length
// and contents of the returned provider list.
func connectProviders(ctx context.Context, endpoints []endpoint, log dex.Logger, chainID *big.Int) ([]*provider, error) {
providers := make([]*provider, 0, len(endpoints))
var success bool
defer func() {
if !success {
for _, p := range providers {
p.ec.Close()
}
}
}()
for _, ep := range endpoints {
// First try to get a websocket connection. Websockets have a header
// feed, so are much preferred to http connections. So much so, that
// we'll do some path inspection here and make an attempt to find a
// websocket server, even if the user requested http.
var ec *ethclient.Client
var rpcClient *rpc.Client
var sub ethereum.Subscription
var h chan *types.Header
host := providerIPC
addr := ep.addr
if strings.HasSuffix(addr, ".ipc") {
// Clean file path.
addr = dex.CleanAndExpandPath(addr)
} else {
wsURL, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("Failed to parse url %q", addr)
}
host = wsURL.Host
ogScheme := wsURL.Scheme
switch ogScheme {
case "https":
wsURL.Scheme = "wss"
case "http":
wsURL.Scheme = "ws"
case "ws", "wss":
default:
return nil, fmt.Errorf("unknown scheme for endpoint %q: %q", addr, wsURL.Scheme)
}
replaced := ogScheme != wsURL.Scheme
// Handle known paths.
switch {
case strings.Contains(wsURL.String(), "infura.io/v3"):
if replaced {
wsURL.Path = "/ws" + wsURL.Path
}
case strings.Contains(wsURL.Host, "rpc.rivet.cloud"):
// subdomain contains API key, so can't simply replace.
wsURL.Host = strings.Replace(wsURL.Host, ".rpc.", ".ws.", 1)
host = providerRivetCloud
}
if ep.jwt == "" {
rpcClient, err = rpc.DialWebsocket(ctx, wsURL.String(), "")
} else {
// Geth clients should always be able to get a
// websocket connection, making http unnecessary.
var authFn func(h http.Header) error
authFn, err = dexeth.JWTHTTPAuthFn(ep.jwt)
if err != nil {
return nil, fmt.Errorf("unable to create auth function: %v", err)
}
rpcClient, err = rpc.DialOptions(ctx, wsURL.String(), rpc.WithHTTPAuth(authFn))
}
if err == nil {
ec = ethclient.NewClient(rpcClient)
h = make(chan *types.Header, 8)
sub, err = ec.SubscribeNewHead(ctx, h)
if err != nil {
rpcClient.Close()
ec = nil
if replaced {
log.Debugf("Connected to websocket, but headers subscription not supported. Trying HTTP")
} else {
log.Errorf("Connected to websocket, but headers subscription not supported. Attempting HTTP fallback")
}
}
} else {
if replaced {
log.Debugf("couldn't get a websocket connection for %q (original scheme: %q) (OK)", wsURL, ogScheme)
} else {
log.Errorf("failed to get websocket connection to %q. attempting http(s) fallback: error = %v", addr, err)
}
}
}
// Weren't able to get a websocket connection. Try HTTP now. Dial does
// path discrimination, so I won't even try to validate the protocol.
if ec == nil {
var err error
rpcClient, err = rpc.DialContext(ctx, addr)
if err != nil {
log.Errorf("error creating http client for %q: %v", addr, err)
continue
}
ec = ethclient.NewClient(rpcClient)
}
// Get chain ID.
reportedChainID, err := ec.ChainID(ctx)
if err != nil {
// If we can't get a header, don't use this provider.
ec.Close()
log.Errorf("Failed to get chain ID from %q: %v", addr, err)
continue
}
if chainID.Cmp(reportedChainID) != 0 {
ec.Close()
log.Errorf("%q reported wrong chain ID. expected %d, got %d", addr, chainID, reportedChainID)
continue
}
hdr, err := ec.HeaderByNumber(ctx, nil /* latest */)
if err != nil {
// If we can't get a header, don't use this provider.
ec.Close()
log.Errorf("Failed to get header from %q: %v", addr, err)
continue
}
p := &provider{
host: host,
ws: sub != nil,
ec: &combinedRPCClient{
Client: ec,
rpc: rpcClient,
},
}
p.setTip(hdr)
providers = append(providers, p)
// Start websocket listen loop.
if sub != nil {
go p.subscribeHeaders(ctx, sub, h, log)
}
}
if len(providers) == 0 {
return nil, fmt.Errorf("failed to connect")
}
log.Debugf("Connected with %d of %d RPC servers", len(providers), len(endpoints))
success = true
return providers, nil
}
func (m *multiRPCClient) connect(ctx context.Context) (err error) {
providers, err := connectProviders(ctx, m.endpoints, m.log, m.chainID)
if err != nil {
return err
}
m.providerMtx.Lock()
m.providers = providers
m.providerMtx.Unlock()
var connections int
for _, p := range m.providerList() {
if _, err := p.bestHeader(ctx, m.log); err != nil {
m.log.Errorf("Failed to synchrnoize header from %s: %v", p.host, err)
} else {
connections++
}
}
// TODO: Require at least two if all connections are non-local.
if connections == 0 {
return fmt.Errorf("no connections established")
}
go func() {
<-ctx.Done()
for _, p := range m.providerList() {
p.ec.Close()
}
}()
return nil
}
// registerNonce returns true and saves the nonce for the next call when a nonce
// has not been received recently.
func (m *multiRPCClient) registerNonce(nonce uint64) bool {
const expiration = time.Minute
ln := &m.lastNonce
set := func() bool {
ln.nonce = nonce
ln.stamp = time.Now()
return true
}
ln.Lock()
defer ln.Unlock()
// Ok if the nonce is larger than previous.
if ln.nonce < nonce {
return set()
}
// Ok if initiation.
if ln.stamp.IsZero() {
return set()
}
// Ok if expiration has passed.
if time.Now().After(ln.stamp.Add(expiration)) {
return set()
}
// Nonce is the same or less than previous and expiration has not
// passed.
return false
}
// voidUnusedNonce sets time to zero time so that the next call to registerNonce
// will return true. This is needed when we know that a tx has failed at the
// time of sending so that the same nonce can be used again.
func (m *multiRPCClient) voidUnusedNonce() {
m.lastNonce.Lock()
defer m.lastNonce.Unlock()
m.lastNonce.stamp = time.Time{}
}
func (m *multiRPCClient) reconfigure(ctx context.Context, settings map[string]string) error {
endpoints, err := endpointsFromSettings(settings)
if err != nil {
return fmt.Errorf("unable to read endpoints: %v", err)
}
providers, err := connectProviders(ctx, endpoints, m.log, m.chainID)
if err != nil {
return err
}
m.providerMtx.Lock()
oldProviders := m.providers
m.providers = providers
m.endpoints = endpoints
m.providerMtx.Unlock()
for _, p := range oldProviders {
p.ec.Close()
}
return nil
}
func (m *multiRPCClient) cachedReceipt(txHash common.Hash) *types.Receipt {
m.receipts.Lock()
defer m.receipts.Unlock()
cached := m.receipts.cache[txHash]
// Periodically clean up the receipts.
if time.Since(m.receipts.lastClean) > time.Minute*20 {
m.receipts.lastClean = time.Now()
defer func() {
for txHash, rec := range m.receipts.cache {
if time.Since(rec.lastAccess) > receiptCacheExpiration {
delete(m.receipts.cache, txHash)
}
}
}()
}
// If confirmed or if it was just fetched, return it as is.
if cached != nil {
// If the cached receipt has the requisite confirmations, it's always
// considered good and we'll just update the lastAccess stamp so we don't
// delete it from the map.
// If it's not confirmed, we never update the lastAccess stamp, which just
// serves to age out the receipt so a new one can be requested and
// confirmations checked again.
if cached.confirmed {
cached.lastAccess = time.Now()
}
if time.Since(cached.lastAccess) < unconfirmedReceiptExpiration {
return cached.r
}
}
return nil
}
func (m *multiRPCClient) transactionReceipt(ctx context.Context, txHash common.Hash) (r *types.Receipt, tx *types.Transaction, err error) {
// TODO
// TODO: Plug in to the monitoredTx system from #1638.
// TODO
if tx, _, err = m.getTransaction(ctx, txHash); err != nil {
return nil, nil, err
}
if r = m.cachedReceipt(txHash); r != nil {
return r, tx, nil
}
// Fetch a fresh one.
if err = m.withPreferred(func(p *provider) error {
r, err = p.ec.TransactionReceipt(ctx, txHash)
return err
}); err != nil {
if isNotFoundError(err) {
return nil, nil, asset.CoinNotFoundError
}
return nil, nil, err
}
var confs int64
if r.BlockNumber != nil {
tip, err := m.bestHeader(ctx)
if err != nil {
return nil, nil, fmt.Errorf("bestHeader error: %v", err)
}
confs = new(big.Int).Sub(tip.Number, r.BlockNumber).Int64() + 1
}
m.receipts.Lock()
m.receipts.cache[txHash] = &receiptRecord{
r: r,
lastAccess: time.Now(),
confirmed: confs > txConfsNeededToConfirm,
}
m.receipts.Unlock()
return r, tx, nil
}
type rpcTransaction struct {
tx *types.Transaction
txExtraDetail
}
type txExtraDetail struct {
BlockNumber *string `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"`
From *common.Address `json:"from,omitempty"`
}
func (tx *rpcTransaction) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &tx.tx); err != nil {
return err
}
return json.Unmarshal(b, &tx.txExtraDetail)
}
func getRPCTransaction(ctx context.Context, p *provider, txHash common.Hash) (*rpcTransaction, error) {
var resp *rpcTransaction
err := p.ec.rpc.CallContext(ctx, &resp, "eth_getTransactionByHash", txHash)
if err != nil {
return nil, err
}
if resp == nil {
return nil, asset.CoinNotFoundError
}
// Just copying geth with this one.
if _, r, _ := resp.tx.RawSignatureValues(); r == nil {
return nil, fmt.Errorf("server returned transaction without signature")
}
return resp, nil
}
func (m *multiRPCClient) getTransaction(ctx context.Context, txHash common.Hash) (tx *types.Transaction, h int64, err error) {
return tx, h, m.withPreferred(func(p *provider) error {
resp, err := getRPCTransaction(ctx, p, txHash)
if err != nil {
if isNotFoundError(err) {
return asset.CoinNotFoundError
}
return err
}
tx = resp.tx
if resp.BlockNumber == nil {
h = -1
} else {
bigH, ok := new(big.Int).SetString(*resp.BlockNumber, 0 /* must start with 0x */)
if !ok {
return fmt.Errorf("couldn't parse hex number %q", *resp.BlockNumber)
}
h = bigH.Int64()
}
return nil
})
}
func (m *multiRPCClient) getConfirmedNonce(ctx context.Context, blockNumber int64) (n uint64, err error) {
return n, m.withPreferred(func(p *provider) error {
n, err = p.ec.PendingNonceAt(ctx, m.address())
return err
})
}
func (m *multiRPCClient) providerList() []*provider {
m.providerMtx.Lock()
defer m.providerMtx.Unlock()
providers := make([]*provider, len(m.providers))
copy(providers, m.providers)
return providers
}
// acceptabilityFilter: When running a pick-a-provider function (withOne,
// withAny, withPreferred), sometimes errors will need special handling
// depending on what they are. Zero or more acceptabilityFilters can be added
// to provide extra control.
//
// discard: If a filter indicates discard = true, the error will be discarded,
// provider iteration will end immediately and a nil error will be returned.
// propagate: If a filter indicates propagate = true, provider iteration will
// be ended and the error will be returned immediately.
// fail: If a filter indicates fail = true, the provider will be quarantined
// and provider iteration will continue
//
// If false is returned for all three for all filters, the error is logged and
// provider iteration will continue.
type acceptabilityFilter func(error) (discard, propagate, fail bool)
func allRPCErrorsAreFails(err error) (discard, propagate, fail bool) {
return false, false, true
}
func errorFilter(err error, matches ...interface{}) bool {
errStr := err.Error()
for _, mi := range matches {
var s string
switch m := mi.(type) {
case string:
s = m
case error:
if errors.Is(err, m) {
return true
}
s = m.Error()
}
if strings.Contains(errStr, s) {
return true
}
}
return false
}
// withOne runs the provider function against the providers in order until one
// succeeds or all have failed.
func (m *multiRPCClient) withOne(providers []*provider, f func(*provider) error, acceptabilityFilters ...acceptabilityFilter) (superError error) {
readyProviders := make([]*provider, 0, len(providers))
for _, p := range providers {
if !p.failed() {
readyProviders = append(readyProviders, p)
}
}
if len(readyProviders) == 0 {
// Just try them all.
m.log.Tracef("all providers in a failed state, so acting like none are")
readyProviders = providers
}
for _, p := range readyProviders {
err := f(p)
if err == nil {
break
}
if superError == nil {
superError = err
} else if err.Error() != superError.Error() {
superError = fmt.Errorf("%v: %w", superError, err)
}
for _, f := range acceptabilityFilters {
discard, propagate, fail := f(err)
if discard {
return nil
}
if propagate {
return err
}
if fail {
p.setFailed()
}
}
}
return
}
// withAny runs the provider function against known providers in random order
// until one succeeds or all have failed.
func (m *multiRPCClient) withAny(f func(*provider) error, acceptabilityFilters ...acceptabilityFilter) error {
providers := m.providerList()
shuffleProviders(providers)
return m.withOne(providers, f, acceptabilityFilters...)
}
// withPreferred is like withAny, but will prioritize recently used nonce
// providers.
func (m *multiRPCClient) withPreferred(f func(*provider) error, acceptabilityFilters ...acceptabilityFilter) error {
return m.withOne(m.nonceProviderList(), f, acceptabilityFilters...)
}
// nonceProviderList returns the randomized provider list, but with any recent
// nonce provider inserted in the first position.
func (m *multiRPCClient) nonceProviderList() []*provider {
m.providerMtx.Lock()
defer m.providerMtx.Unlock()
providers := make([]*provider, 0, len(m.providers))
var lastProvider *provider
if time.Since(m.lastProvider.stamp) < nonceProviderStickiness {
lastProvider = m.lastProvider.provider
}
for _, p := range m.providers {
if lastProvider != nil && lastProvider.host == p.host {
continue // already added it
}
providers = append(providers, p)
}
shuffleProviders(providers)
if lastProvider != nil {
providers = append([]*provider{lastProvider}, providers...)
}
return providers
}
// nextNonce returns the next nonce number for the account.
func (m *multiRPCClient) nextNonce(ctx context.Context) (nonce uint64, err error) {
checks := 5
checkDelay := time.Second * 5
for i := 0; i < checks; i++ {
var host string
err = m.withPreferred(func(p *provider) error {
host = p.host
nonce, err = p.ec.PendingNonceAt(ctx, m.creds.addr)
return err
})
if err != nil {
return 0, err
}
if m.registerNonce(nonce) {
return nonce, nil
}
m.log.Warnf("host %s returned recently used account nonce number %d. try %d of %d.",
host, nonce, i+1, checks)
// Delay all but the last check.
if i+1 < checks {
select {
case <-time.After(checkDelay):
case <-ctx.Done():
return 0, ctx.Err()
}
}
}
return 0, errors.New("preferred provider returned a recently used account nonce")
}
func (m *multiRPCClient) address() common.Address {
return m.creds.addr
}
func (m *multiRPCClient) addressBalance(ctx context.Context, addr common.Address) (bal *big.Int, err error) {
return bal, m.withAny(func(p *provider) error {
bal, err = p.ec.BalanceAt(ctx, addr, nil /* latest */)
return err
})
}
func (m *multiRPCClient) bestHeader(ctx context.Context) (hdr *types.Header, err error) {
// Check for an unexpired cached header first.
var bestHeader *types.Header
for _, p := range m.providerList() {
h := p.cachedTip()
if h == nil {
continue
}
if bestHeader == nil ||
// In fact, we should be comparing the total terminal difficulty of
// the blocks. We don't have the TTD, even though it is sent by RPC,
// because ethclient strips it from header data and the header
// subscriptions may or may not send the ttd (Infura docs do not
// show it in message), but it doesn't come through the geth client
// subscription machinery regardless.
h.Number.Cmp(bestHeader.Number) > 0 {
bestHeader = h
}
}
if bestHeader != nil {
return bestHeader, nil
}
return hdr, m.withAny(func(p *provider) error {
hdr, err = p.bestHeader(ctx, m.log)
return err
}, allRPCErrorsAreFails)
}
func (m *multiRPCClient) headerByHash(ctx context.Context, h common.Hash) (hdr *types.Header, err error) {
return hdr, m.withAny(func(p *provider) error {
hdr, err = p.headerByHash(ctx, h)
return err
})
}
func (m *multiRPCClient) chainConfig() *params.ChainConfig {
return m.cfg
}
func (m *multiRPCClient) peerCount() (c uint32) {
m.providerMtx.Lock()
defer m.providerMtx.Unlock()
for _, p := range m.providers {
if !p.failed() {
c++
}
}
return
}
func (m *multiRPCClient) contractBackend() bind.ContractBackend {
return m
}
func (m *multiRPCClient) lock() error {
return m.creds.ks.Lock(m.creds.addr)
}
func (m *multiRPCClient) locked() bool {
status, _ := m.creds.wallet.Status()
return status != "Unlocked"
}
func (m *multiRPCClient) pendingTransactions() ([]*types.Transaction, error) {
return []*types.Transaction{}, nil
}
func (m *multiRPCClient) shutdown() {
for _, p := range m.providerList() {
p.ec.Close()
}
}
func (m *multiRPCClient) sendSignedTransaction(ctx context.Context, tx *types.Transaction) error {
var lastProvider *provider
if err := m.withPreferred(func(p *provider) error {
lastProvider = p
m.log.Tracef("Sending signed tx via %q", p.host)
return p.ec.SendTransaction(ctx, tx)
}, func(err error) (discard, propagate, fail bool) {
return errorFilter(err, core.ErrAlreadyKnown, "known transaction"), false, false
}); err != nil {
return err
}
m.lastProvider.Lock()
m.lastProvider.provider = lastProvider
m.lastProvider.stamp = time.Now()
m.lastProvider.Unlock()
return nil
}