-
-
Notifications
You must be signed in to change notification settings - Fork 746
Expand file tree
/
Copy pathblockchain.go
More file actions
546 lines (481 loc) · 23 KB
/
Copy pathblockchain.go
File metadata and controls
546 lines (481 loc) · 23 KB
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
package coins
import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"reflect"
"time"
"github.com/juju/errors"
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/arbitrum"
"github.com/trezor/blockbook/bchain/coins/avalanche"
"github.com/trezor/blockbook/bchain/coins/base"
"github.com/trezor/blockbook/bchain/coins/bch"
"github.com/trezor/blockbook/bchain/coins/bellcoin"
"github.com/trezor/blockbook/bchain/coins/bitcore"
"github.com/trezor/blockbook/bchain/coins/bitzeny"
"github.com/trezor/blockbook/bchain/coins/bsc"
"github.com/trezor/blockbook/bchain/coins/btc"
"github.com/trezor/blockbook/bchain/coins/btg"
"github.com/trezor/blockbook/bchain/coins/cpuchain"
"github.com/trezor/blockbook/bchain/coins/dash"
"github.com/trezor/blockbook/bchain/coins/dcr"
"github.com/trezor/blockbook/bchain/coins/deeponion"
"github.com/trezor/blockbook/bchain/coins/digibyte"
"github.com/trezor/blockbook/bchain/coins/divi"
"github.com/trezor/blockbook/bchain/coins/dogecoin"
"github.com/trezor/blockbook/bchain/coins/ecash"
"github.com/trezor/blockbook/bchain/coins/eth"
"github.com/trezor/blockbook/bchain/coins/firo"
"github.com/trezor/blockbook/bchain/coins/flo"
"github.com/trezor/blockbook/bchain/coins/fujicoin"
"github.com/trezor/blockbook/bchain/coins/gamecredits"
"github.com/trezor/blockbook/bchain/coins/grs"
"github.com/trezor/blockbook/bchain/coins/koto"
"github.com/trezor/blockbook/bchain/coins/liquid"
"github.com/trezor/blockbook/bchain/coins/litecoin"
"github.com/trezor/blockbook/bchain/coins/monacoin"
"github.com/trezor/blockbook/bchain/coins/monetaryunit"
"github.com/trezor/blockbook/bchain/coins/myriad"
"github.com/trezor/blockbook/bchain/coins/namecoin"
"github.com/trezor/blockbook/bchain/coins/nuls"
"github.com/trezor/blockbook/bchain/coins/omotenashicoin"
"github.com/trezor/blockbook/bchain/coins/optimism"
"github.com/trezor/blockbook/bchain/coins/pivx"
"github.com/trezor/blockbook/bchain/coins/polis"
"github.com/trezor/blockbook/bchain/coins/polygon"
"github.com/trezor/blockbook/bchain/coins/qtum"
"github.com/trezor/blockbook/bchain/coins/ravencoin"
"github.com/trezor/blockbook/bchain/coins/ritocoin"
"github.com/trezor/blockbook/bchain/coins/snowgem"
"github.com/trezor/blockbook/bchain/coins/trezarcoin"
"github.com/trezor/blockbook/bchain/coins/tron"
"github.com/trezor/blockbook/bchain/coins/unobtanium"
"github.com/trezor/blockbook/bchain/coins/vertcoin"
"github.com/trezor/blockbook/bchain/coins/viacoin"
"github.com/trezor/blockbook/bchain/coins/vipstarcoin"
"github.com/trezor/blockbook/bchain/coins/zec"
"github.com/trezor/blockbook/common"
)
type blockChainFactory func(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error)
// BlockChainFactories is a map of constructors of coin RPC interfaces
var BlockChainFactories = make(map[string]blockChainFactory)
func init() {
BlockChainFactories["Bitcoin"] = btc.NewBitcoinRPC
BlockChainFactories["Testnet"] = btc.NewBitcoinRPC
BlockChainFactories["Testnet4"] = btc.NewBitcoinRPC
BlockChainFactories["Signet"] = btc.NewBitcoinRPC
BlockChainFactories["Regtest"] = btc.NewBitcoinRPC
BlockChainFactories["Zcash"] = zec.NewZCashRPC
BlockChainFactories["Zcash Testnet"] = zec.NewZCashRPC
BlockChainFactories["Ethereum"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Archive"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Classic"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Testnet Sepolia"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Testnet Sepolia Archive"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Testnet Hoodi"] = eth.NewEthereumRPC
BlockChainFactories["Ethereum Testnet Hoodi Archive"] = eth.NewEthereumRPC
BlockChainFactories["Bcash"] = bch.NewBCashRPC
BlockChainFactories["Bcash Testnet"] = bch.NewBCashRPC
BlockChainFactories["Bgold"] = btg.NewBGoldRPC
BlockChainFactories["Bgold Testnet"] = btg.NewBGoldRPC
BlockChainFactories["Dash"] = dash.NewDashRPC
BlockChainFactories["Dash Testnet"] = dash.NewDashRPC
BlockChainFactories["Decred"] = dcr.NewDecredRPC
BlockChainFactories["Decred Testnet"] = dcr.NewDecredRPC
BlockChainFactories["GameCredits"] = gamecredits.NewGameCreditsRPC
BlockChainFactories["Koto"] = koto.NewKotoRPC
BlockChainFactories["Koto Testnet"] = koto.NewKotoRPC
BlockChainFactories["Litecoin"] = litecoin.NewLitecoinRPC
BlockChainFactories["Litecoin Testnet"] = litecoin.NewLitecoinRPC
BlockChainFactories["Dogecoin"] = dogecoin.NewDogecoinRPC
BlockChainFactories["Dogecoin Testnet"] = dogecoin.NewDogecoinRPC
BlockChainFactories["Vertcoin"] = vertcoin.NewVertcoinRPC
BlockChainFactories["Vertcoin Testnet"] = vertcoin.NewVertcoinRPC
BlockChainFactories["Namecoin"] = namecoin.NewNamecoinRPC
BlockChainFactories["Monacoin"] = monacoin.NewMonacoinRPC
BlockChainFactories["Monacoin Testnet"] = monacoin.NewMonacoinRPC
BlockChainFactories["MonetaryUnit"] = monetaryunit.NewMonetaryUnitRPC
BlockChainFactories["DigiByte"] = digibyte.NewDigiByteRPC
BlockChainFactories["DigiByte Testnet"] = digibyte.NewDigiByteRPC
BlockChainFactories["Myriad"] = myriad.NewMyriadRPC
BlockChainFactories["Liquid"] = liquid.NewLiquidRPC
BlockChainFactories["Groestlcoin"] = grs.NewGroestlcoinRPC
BlockChainFactories["Groestlcoin Testnet"] = grs.NewGroestlcoinRPC
BlockChainFactories["Groestlcoin Signet"] = grs.NewGroestlcoinRPC
BlockChainFactories["Groestlcoin Regtest"] = grs.NewGroestlcoinRPC
BlockChainFactories["PIVX"] = pivx.NewPivXRPC
BlockChainFactories["PIVX Testnet"] = pivx.NewPivXRPC
BlockChainFactories["Polis"] = polis.NewPolisRPC
BlockChainFactories["Firo"] = firo.NewFiroRPC
BlockChainFactories["Fujicoin"] = fujicoin.NewFujicoinRPC
BlockChainFactories["Flo"] = flo.NewFloRPC
BlockChainFactories["Bellcoin"] = bellcoin.NewBellcoinRPC
BlockChainFactories["Qtum"] = qtum.NewQtumRPC
BlockChainFactories["Viacoin"] = viacoin.NewViacoinRPC
BlockChainFactories["Qtum Testnet"] = qtum.NewQtumRPC
BlockChainFactories["NULS"] = nuls.NewNulsRPC
BlockChainFactories["VIPSTARCOIN"] = vipstarcoin.NewVIPSTARCOINRPC
BlockChainFactories["Flux"] = zec.NewZCashRPC
BlockChainFactories["Ravencoin"] = ravencoin.NewRavencoinRPC
BlockChainFactories["Ritocoin"] = ritocoin.NewRitocoinRPC
BlockChainFactories["Divi"] = divi.NewDiviRPC
BlockChainFactories["CPUchain"] = cpuchain.NewCPUchainRPC
BlockChainFactories["Unobtanium"] = unobtanium.NewUnobtaniumRPC
BlockChainFactories["DeepOnion"] = deeponion.NewDeepOnionRPC
BlockChainFactories["SnowGem"] = snowgem.NewSnowGemRPC
BlockChainFactories["Bitcore"] = bitcore.NewBitcoreRPC
BlockChainFactories["Omotenashicoin"] = omotenashicoin.NewOmotenashiCoinRPC
BlockChainFactories["Omotenashicoin Testnet"] = omotenashicoin.NewOmotenashiCoinRPC
BlockChainFactories["BitZeny"] = bitzeny.NewBitZenyRPC
BlockChainFactories["Trezarcoin"] = trezarcoin.NewTrezarcoinRPC
BlockChainFactories["ECash"] = ecash.NewECashRPC
BlockChainFactories["Avalanche"] = avalanche.NewAvalancheRPC
BlockChainFactories["Avalanche Archive"] = avalanche.NewAvalancheRPC
BlockChainFactories["BNB Smart Chain"] = bsc.NewBNBSmartChainRPC
BlockChainFactories["BNB Smart Chain Archive"] = bsc.NewBNBSmartChainRPC
BlockChainFactories["Polygon"] = polygon.NewPolygonRPC
BlockChainFactories["Polygon Archive"] = polygon.NewPolygonRPC
BlockChainFactories["Optimism"] = optimism.NewOptimismRPC
BlockChainFactories["Optimism Archive"] = optimism.NewOptimismRPC
BlockChainFactories["Arbitrum"] = arbitrum.NewArbitrumRPC
BlockChainFactories["Arbitrum Archive"] = arbitrum.NewArbitrumRPC
BlockChainFactories["Arbitrum Nova"] = arbitrum.NewArbitrumRPC
BlockChainFactories["Arbitrum Nova Archive"] = arbitrum.NewArbitrumRPC
BlockChainFactories["Base"] = base.NewBaseRPC
BlockChainFactories["Base Archive"] = base.NewBaseRPC
BlockChainFactories["Tron"] = tron.NewTronRPC
BlockChainFactories["Tron Testnet Nile"] = tron.NewTronRPC
}
type metricsSetter interface {
SetMetrics(*common.Metrics)
}
// NewBlockChain creates bchain.BlockChain and bchain.Mempool for the coin passed by the parameter coin
func NewBlockChain(coin string, configfile string, pushHandler func(bchain.NotificationType), metrics *common.Metrics) (bchain.BlockChain, bchain.Mempool, error) {
data, err := os.ReadFile(configfile)
if err != nil {
return nil, nil, errors.Annotatef(err, "Error reading file %v", configfile)
}
var config json.RawMessage
err = json.Unmarshal(data, &config)
if err != nil {
return nil, nil, errors.Annotatef(err, "Error parsing file %v", configfile)
}
bcf, ok := BlockChainFactories[coin]
if !ok {
return nil, nil, errors.New(fmt.Sprint("Unsupported coin '", coin, "'. Must be one of ", reflect.ValueOf(BlockChainFactories).MapKeys()))
}
bc, err := bcf(config, pushHandler)
if err != nil {
return nil, nil, err
}
if withMetrics, ok := bc.(metricsSetter); ok {
withMetrics.SetMetrics(metrics)
}
err = bc.Initialize()
if err != nil {
return nil, nil, err
}
mempool, err := bc.CreateMempool(bc)
if err != nil {
return nil, nil, err
}
return &blockChainWithMetrics{b: bc, m: metrics}, &mempoolWithMetrics{mempool: mempool, m: metrics}, nil
}
type blockChainWithMetrics struct {
b bchain.BlockChain
m *common.Metrics
}
func (c *blockChainWithMetrics) observeRPCLatency(method string, start time.Time, err error) {
var e string
if err != nil {
e = "failure"
}
c.m.RPCLatency.With(common.Labels{"method": method, "error": e}).Observe(float64(time.Since(start)) / 1e6) // in milliseconds
}
func (c *blockChainWithMetrics) Initialize() error {
return c.b.Initialize()
}
func (c *blockChainWithMetrics) CreateMempool(chain bchain.BlockChain) (bchain.Mempool, error) {
return c.b.CreateMempool(chain)
}
func (c *blockChainWithMetrics) InitializeMempool(addrDescForOutpoint bchain.AddrDescForOutpointFunc, onNewTx bchain.OnNewTxFunc) error {
return c.b.InitializeMempool(addrDescForOutpoint, onNewTx)
}
func (c *blockChainWithMetrics) Shutdown(ctx context.Context) error {
return c.b.Shutdown(ctx)
}
func (c *blockChainWithMetrics) IsTestnet() bool {
return c.b.IsTestnet()
}
func (c *blockChainWithMetrics) GetNetworkName() string {
return c.b.GetNetworkName()
}
func (c *blockChainWithMetrics) GetCoinName() string {
return c.b.GetCoinName()
}
func (c *blockChainWithMetrics) GetSubversion() string {
return c.b.GetSubversion()
}
func (c *blockChainWithMetrics) GetChainInfo() (v *bchain.ChainInfo, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetChainInfo", s, err) }(time.Now())
return c.b.GetChainInfo()
}
func (c *blockChainWithMetrics) GetBestBlockHash() (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBestBlockHash", s, err) }(time.Now())
return c.b.GetBestBlockHash()
}
func (c *blockChainWithMetrics) GetBestBlockHeight() (v uint32, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBestBlockHeight", s, err) }(time.Now())
return c.b.GetBestBlockHeight()
}
func (c *blockChainWithMetrics) GetBlockHash(height uint32) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockHash", s, err) }(time.Now())
return c.b.GetBlockHash(height)
}
func (c *blockChainWithMetrics) GetBlockHeader(hash string) (v *bchain.BlockHeader, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockHeader", s, err) }(time.Now())
return c.b.GetBlockHeader(hash)
}
func (c *blockChainWithMetrics) GetBlock(hash string, height uint32) (v *bchain.Block, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlock", s, err) }(time.Now())
return c.b.GetBlock(hash, height)
}
func (c *blockChainWithMetrics) GetBlockInfo(hash string) (v *bchain.BlockInfo, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockInfo", s, err) }(time.Now())
return c.b.GetBlockInfo(hash)
}
func (c *blockChainWithMetrics) GetBlockRaw(hash string) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockRaw", s, err) }(time.Now())
return c.b.GetBlockRaw(hash)
}
func (c *blockChainWithMetrics) GetMempoolTransactions() (v []string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolTransactions", s, err) }(time.Now())
return c.b.GetMempoolTransactions()
}
func (c *blockChainWithMetrics) GetTransaction(txid string) (v *bchain.Tx, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetTransaction", s, err) }(time.Now())
return c.b.GetTransaction(txid)
}
func (c *blockChainWithMetrics) GetTransactionSpecific(tx *bchain.Tx) (v json.RawMessage, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetTransactionSpecific", s, err) }(time.Now())
return c.b.GetTransactionSpecific(tx)
}
func (c *blockChainWithMetrics) GetAddressChainExtraData(addrDesc bchain.AddressDescriptor) (v json.RawMessage, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetAddressChainExtraData", s, err) }(time.Now())
return c.b.GetAddressChainExtraData(addrDesc)
}
func (c *blockChainWithMetrics) GetTransactionForMempool(txid string) (v *bchain.Tx, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetTransactionForMempool", s, err) }(time.Now())
return c.b.GetTransactionForMempool(txid)
}
func (c *blockChainWithMetrics) EstimateSmartFee(blocks int, conservative bool) (v big.Int, err error) {
defer func(s time.Time) { c.observeRPCLatency("EstimateSmartFee", s, err) }(time.Now())
return c.b.EstimateSmartFee(blocks, conservative)
}
func (c *blockChainWithMetrics) EstimateFee(blocks int) (v big.Int, err error) {
defer func(s time.Time) { c.observeRPCLatency("EstimateFee", s, err) }(time.Now())
return c.b.EstimateFee(blocks)
}
func (c *blockChainWithMetrics) LongTermFeeRate() (v *bchain.LongTermFeeRate, err error) {
defer func(s time.Time) { c.observeRPCLatency("LongTermFeeRate", s, err) }(time.Now())
return c.b.LongTermFeeRate()
}
func (c *blockChainWithMetrics) SendRawTransaction(tx string, disableAlternativeRPC bool) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("SendRawTransaction", s, err) }(time.Now())
return c.b.SendRawTransaction(tx, disableAlternativeRPC)
}
func (c *blockChainWithMetrics) GetMempoolEntry(txid string) (v *bchain.MempoolEntry, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolEntry", s, err) }(time.Now())
return c.b.GetMempoolEntry(txid)
}
func (c *blockChainWithMetrics) GetChainParser() bchain.BlockChainParser {
return c.b.GetChainParser()
}
func (c *blockChainWithMetrics) EthereumTypeGetBalance(addrDesc bchain.AddressDescriptor) (v *big.Int, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetBalance", s, err) }(time.Now())
return c.b.EthereumTypeGetBalance(addrDesc)
}
func (c *blockChainWithMetrics) EthereumTypeGetNonces(addrDesc bchain.AddressDescriptor, withConfirmed bool, privatePendingNonces ...uint64) (pending uint64, confirmed uint64, confirmedOK bool, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetNonces", s, err) }(time.Now())
return c.b.EthereumTypeGetNonces(addrDesc, withConfirmed, privatePendingNonces...)
}
func (c *blockChainWithMetrics) EthereumTypeEstimateGas(params map[string]interface{}) (v uint64, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeEstimateGas", s, err) }(time.Now())
return c.b.EthereumTypeEstimateGas(params)
}
func (c *blockChainWithMetrics) EthereumTypeGetEip1559Fees() (v *bchain.Eip1559Fees, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetEip1559Fees", s, err) }(time.Now())
return c.b.EthereumTypeGetEip1559Fees()
}
func (c *blockChainWithMetrics) GetContractInfo(contractDesc bchain.AddressDescriptor) (v *bchain.ContractInfo, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetContractInfo", s, err) }(time.Now())
return c.b.GetContractInfo(contractDesc)
}
func (c *blockChainWithMetrics) EthereumTypeGetErc20ContractBalance(addrDesc, contractDesc bchain.AddressDescriptor) (v *big.Int, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetErc20ContractBalance", s, err) }(time.Now())
return c.b.EthereumTypeGetErc20ContractBalance(addrDesc, contractDesc)
}
func (c *blockChainWithMetrics) EthereumTypeGetErc20ContractBalances(addrDesc bchain.AddressDescriptor, contractDescs []bchain.AddressDescriptor) (v []*big.Int, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetErc20ContractBalances", s, err) }(time.Now())
return c.b.EthereumTypeGetErc20ContractBalances(addrDesc, contractDescs)
}
// GetTokenURI returns URI of non fungible or multi token defined by token id
func (c *blockChainWithMetrics) GetTokenURI(contractDesc bchain.AddressDescriptor, tokenID *big.Int) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetTokenURI", s, err) }(time.Now())
return c.b.GetTokenURI(contractDesc, tokenID)
}
func (c *blockChainWithMetrics) EthereumTypeGetSupportedStakingPools() []string {
return c.b.EthereumTypeGetSupportedStakingPools()
}
func (c *blockChainWithMetrics) EthereumTypeGetStakingPoolsData(addrDesc bchain.AddressDescriptor) (v []bchain.StakingPoolData, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeStakingPoolsData", s, err) }(time.Now())
return c.b.EthereumTypeGetStakingPoolsData(addrDesc)
}
// EthereumTypeRpcCall calls eth_call with given data and to address
func (c *blockChainWithMetrics) EthereumTypeRpcCall(data, to, from string) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeRpcCall", s, err) }(time.Now())
return c.b.EthereumTypeRpcCall(data, to, from)
}
func (c *blockChainWithMetrics) EthereumTypeRpcCallBatch(calls []bchain.EthereumTypeRPCCall) (v []bchain.EthereumTypeRPCCallResult, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeRpcCallBatch", s, err) }(time.Now())
batcher, ok := c.b.(interface {
EthereumTypeRpcCallBatch(calls []bchain.EthereumTypeRPCCall) ([]bchain.EthereumTypeRPCCallResult, error)
})
if !ok {
return nil, errors.New("EthereumTypeRpcCallBatch: not supported")
}
return batcher.EthereumTypeRpcCallBatch(calls)
}
func (c *blockChainWithMetrics) EthereumTypeMulticallAggregate3(calls []bchain.EthereumMulticallCall, blockNumber *big.Int) (v []bchain.EthereumMulticallResult, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeMulticallAggregate3", s, err) }(time.Now())
caller, ok := c.b.(interface {
EthereumTypeMulticallAggregate3(calls []bchain.EthereumMulticallCall, blockNumber *big.Int) ([]bchain.EthereumMulticallResult, error)
})
if !ok {
return nil, errors.New("EthereumTypeMulticallAggregate3: not supported")
}
return caller.EthereumTypeMulticallAggregate3(calls, blockNumber)
}
func (c *blockChainWithMetrics) EthereumTypeGetRawTransaction(txid string) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetRawTransaction", s, err) }(time.Now())
return c.b.EthereumTypeGetRawTransaction(txid)
}
func (c *blockChainWithMetrics) EthereumTypeGetTransactionReceipt(txid string) (v *bchain.RpcReceipt, err error) {
defer func(s time.Time) { c.observeRPCLatency("EthereumTypeGetTransactionReceipt", s, err) }(time.Now())
return c.b.EthereumTypeGetTransactionReceipt(txid)
}
type mempoolWithMetrics struct {
mempool bchain.Mempool
m *common.Metrics
}
func (c *mempoolWithMetrics) chainTypeLabel() string {
switch c.mempool.(type) {
case *bchain.MempoolBitcoinType:
return "utxo"
case *bchain.MempoolEthereumType:
return "evm"
default:
return "other"
}
}
func (c *mempoolWithMetrics) observeRPCLatency(method string, start time.Time, err error) {
var e string
if err != nil {
e = "failure"
}
c.m.RPCLatency.With(common.Labels{"method": method, "error": e}).Observe(float64(time.Since(start)) / 1e6) // in milliseconds
}
func (c *mempoolWithMetrics) Resync() (count int, err error) {
start := time.Now()
defer func(s time.Time) { c.observeRPCLatency("ResyncMempool", s, err) }(start)
count, err = c.mempool.Resync()
duration := time.Since(start)
c.m.MempoolResyncDuration.Observe(float64(duration) / 1e6) // in milliseconds
status := "success"
if err != nil {
status = "failure"
}
throughput := 0.0
if err == nil {
seconds := duration.Seconds()
if seconds > 0 {
throughput = float64(count) / seconds
}
}
c.m.MempoolResyncThroughput.With(common.Labels{
"chain": c.chainTypeLabel(),
"status": status,
}).Observe(throughput)
if err == nil {
c.m.MempoolSize.Set(float64(count))
}
return count, err
}
func (c *mempoolWithMetrics) GetTransactions(address string) (v []bchain.Outpoint, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolTransactions", s, err) }(time.Now())
return c.mempool.GetTransactions(address)
}
func (c *mempoolWithMetrics) GetAddrDescTransactions(addrDesc bchain.AddressDescriptor) (v []bchain.Outpoint, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolTransactionsForAddrDesc", s, err) }(time.Now())
return c.mempool.GetAddrDescTransactions(addrDesc)
}
func (c *mempoolWithMetrics) GetAllEntries() (v bchain.MempoolTxidEntries) {
defer func(s time.Time) { c.observeRPCLatency("GetAllEntries", s, nil) }(time.Now())
return c.mempool.GetAllEntries()
}
func (c *mempoolWithMetrics) GetTransactionTime(txid string) uint32 {
return c.mempool.GetTransactionTime(txid)
}
func (c *mempoolWithMetrics) GetTxidFilterEntries(filterScripts string, fromTimestamp uint32) (bchain.MempoolTxidFilterEntries, error) {
return c.mempool.GetTxidFilterEntries(filterScripts, fromTimestamp)
}
func (c *blockChainWithMetrics) ResolveENS(name string) (*bchain.ENSResolution, error) {
if ensResolver, ok := c.b.(interface {
ResolveENS(string) (*bchain.ENSResolution, error)
}); ok {
return ensResolver.ResolveENS(name)
}
return nil, errors.New("ENS resolution not supported by underlying chain")
}
func (c *blockChainWithMetrics) CheckENSExpiration(name string) (bool, error) {
if ensResolver, ok := c.b.(interface {
CheckENSExpiration(string) (bool, error)
}); ok {
return ensResolver.CheckENSExpiration(name)
}
return false, errors.New("ENS expiration check not supported by underlying chain")
}
// AverageBlockTimeDuration forwards the wrapped chain's configured block cadence
// so blockbook.go's duck-typed lookup reaches it through the metrics wrapper.
// Returns an error when the underlying chain doesn't expose one.
func (c *blockChainWithMetrics) AverageBlockTimeDuration() (time.Duration, error) {
if p, ok := c.b.(interface {
AverageBlockTimeDuration() (time.Duration, error)
}); ok {
return p.AverageBlockTimeDuration()
}
return 0, errors.New("average block time not supported by underlying chain")
}
// MissingBlockRetryOverride forwards the wrapped chain's per-chain sync-worker
// retry override through the metrics wrapper; nil when none is provided.
func (c *blockChainWithMetrics) MissingBlockRetryOverride() *bchain.MissingBlockRetry {
if p, ok := c.b.(interface {
MissingBlockRetryOverride() *bchain.MissingBlockRetry
}); ok {
return p.MissingBlockRetryOverride()
}
return nil
}
// XpubConfigOverride forwards the wrapped chain's per-chain xpub config
// override through the metrics wrapper; nil when none is provided.
func (c *blockChainWithMetrics) XpubConfigOverride() *bchain.XpubConfig {
if p, ok := c.b.(interface {
XpubConfigOverride() *bchain.XpubConfig
}); ok {
return p.XpubConfigOverride()
}
return nil
}