Skip to content

Commit 7fbb5ca

Browse files
committed
Adding logger around electrum client calls
In case of electrum client timeouts/errors we wrap these calls to measure the time it takes to complete it. In case of a sucessfull call the elapsed time might be also benefitial for debugging purposes.
1 parent d768be0 commit 7fbb5ca

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

pkg/bitcoin/electrum/electrum.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func (c *Connection) GetTransaction(
8989
// as Esplora/Electrs doesn't support verbose transactions.
9090
// See: https://github.com/Blockstream/electrs/pull/36
9191
return client.GetRawTransaction(ctx, txID)
92-
})
92+
},
93+
"GetRawTransaction",
94+
)
9395
if err != nil {
9496
return nil, fmt.Errorf(
9597
"failed to get raw transaction with ID [%s]: [%w]",
@@ -125,7 +127,9 @@ func (c *Connection) GetTransactionConfirmations(
125127
// as Esplora/Electrs doesn't support verbose transactions.
126128
// See: https://github.com/Blockstream/electrs/pull/36
127129
return client.GetRawTransaction(ctx, txID)
128-
})
130+
},
131+
"GetRawTransaction",
132+
)
129133
if err != nil {
130134
return 0,
131135
fmt.Errorf(
@@ -174,7 +178,9 @@ txOutLoop:
174178
client *electrum.Client,
175179
) ([]*electrum.GetMempoolResult, error) {
176180
return client.GetHistory(ctx, reversedScriptHashString)
177-
})
181+
},
182+
"GetHistory",
183+
)
178184
if err != nil {
179185
// Don't return an error, but continue to the next TxOut entry.
180186
txLogger.Errorf("failed to get history for script hash: [%v]", err)
@@ -239,7 +245,9 @@ func (c *Connection) BroadcastTransaction(
239245
c,
240246
func(ctx context.Context, client *electrum.Client) (string, error) {
241247
return client.BroadcastTransaction(ctx, rawTx)
242-
})
248+
},
249+
"BroadcastTransaction",
250+
)
243251
if err != nil {
244252
return fmt.Errorf("failed to broadcast the transaction: [%w]", err)
245253
}
@@ -261,7 +269,9 @@ func (c *Connection) GetLatestBlockHeight() (uint, error) {
261269
}
262270
tip := <-headersChan
263271
return tip.Height, nil
264-
})
272+
},
273+
"SubscribeHeaders",
274+
)
265275
if err != nil {
266276
return 0, fmt.Errorf("failed to subscribe for headers: [%w]", err)
267277
}
@@ -287,6 +297,7 @@ func (c *Connection) GetBlockHeader(
287297
) (*electrum.GetBlockHeaderResult, error) {
288298
return client.GetBlockHeader(ctx, uint32(blockHeight), 0)
289299
},
300+
"GetBlockHeader",
290301
)
291302
if err != nil {
292303
return nil, fmt.Errorf("failed to get block header: [%w]", err)
@@ -321,6 +332,7 @@ func (c *Connection) GetTransactionMerkleProof(
321332
uint32(blockHeight),
322333
)
323334
},
335+
"GetMerkleProof",
324336
)
325337
if err != nil {
326338
return nil, fmt.Errorf("failed to get merkle proof: [%w]", err)
@@ -430,7 +442,9 @@ func (c *Connection) getConfirmedScriptHistory(
430442
client *electrum.Client,
431443
) ([]*electrum.GetMempoolResult, error) {
432444
return client.GetHistory(ctx, reversedScriptHashString)
433-
})
445+
},
446+
"GetHistory",
447+
)
434448
if err != nil {
435449
return nil, fmt.Errorf(
436450
"failed to get history for script [0x%x]: [%v]",
@@ -561,7 +575,9 @@ func (c *Connection) getScriptMempool(
561575
client *electrum.Client,
562576
) ([]*electrum.GetMempoolResult, error) {
563577
return client.GetMempool(ctx, reversedScriptHashString)
564-
})
578+
},
579+
"GetMempool",
580+
)
565581
if err != nil {
566582
return nil, fmt.Errorf(
567583
"failed to get mempool for script [0x%x]: [%v]",
@@ -610,7 +626,9 @@ func (c *Connection) EstimateSatPerVByteFee(blocks uint32) (int64, error) {
610626
// since version 1.4.2 of the protocol. We need to replace it
611627
// somehow once it disappears from Electrum implementations.
612628
return client.GetFee(ctx, blocks)
613-
})
629+
},
630+
"GetFee",
631+
)
614632
if err != nil {
615633
return 0, fmt.Errorf("failed to get fee: [%v]", err)
616634
}
@@ -669,6 +687,7 @@ func (c *Connection) verifyServer() error {
669687
}
670688
return &Server{serverVersion, protocolVersion}, nil
671689
},
690+
"ServerVersion",
672691
)
673692
if err != nil {
674693
return fmt.Errorf("failed to get server version: [%w]", err)
@@ -704,6 +723,7 @@ func (c *Connection) keepAlive() {
704723
func(ctx context.Context, client *electrum.Client) (interface{}, error) {
705724
return nil, client.Ping(ctx)
706725
},
726+
"Ping",
707727
)
708728
if err != nil {
709729
logger.Errorf(
@@ -753,7 +773,11 @@ func connectWithRetry(
753773
func requestWithRetry[K interface{}](
754774
c *Connection,
755775
requestFn func(ctx context.Context, client *electrum.Client) (K, error),
776+
requestName string,
756777
) (K, error) {
778+
startTime := time.Now()
779+
logger.Infof("starting [%s] request to Electrum server", requestName)
780+
757781
var result K
758782

759783
err := wrappers.DoWithDefaultRetry(
@@ -779,6 +803,19 @@ func requestWithRetry[K interface{}](
779803
return nil
780804
})
781805

806+
solveRequestOutcome := func(err error) string {
807+
if err != nil {
808+
return fmt.Sprintf("error: [%v]", err)
809+
}
810+
return "success"
811+
}
812+
813+
logger.Infof("[%s] request to Electrum server completed with [%s] after [%s]",
814+
requestName,
815+
solveRequestOutcome(err),
816+
time.Since(startTime),
817+
)
818+
782819
return result, err
783820
}
784821

0 commit comments

Comments
 (0)