Skip to content

Commit 0bef44a

Browse files
Logs for electrum connection (#3715)
Refs the discussion #3701 (comment) Electrum client calls sometimes are prone to timeouts or other issues. For debugging purposes we wrap electrum client calls with a logger and measure the time it takes to complete a call.
2 parents 5c2604c + 7fbb5ca commit 0bef44a

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

pkg/bitcoin/electrum/electrum.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import (
2121
"github.com/keep-network/keep-core/pkg/internal/byteutils"
2222
)
2323

24-
// TODO: Some problems with Electrum re-connect were detected while developing
25-
// integration tests: https://github.com/keep-network/keep-core/issues/3586.
26-
// Make sure the problem is resolved soon.
27-
2824
var (
2925
supportedProtocolVersions = []string{"1.4"}
3026
logger = log.Logger("keep-electrum")
@@ -93,7 +89,9 @@ func (c *Connection) GetTransaction(
9389
// as Esplora/Electrs doesn't support verbose transactions.
9490
// See: https://github.com/Blockstream/electrs/pull/36
9591
return client.GetRawTransaction(ctx, txID)
96-
})
92+
},
93+
"GetRawTransaction",
94+
)
9795
if err != nil {
9896
return nil, fmt.Errorf(
9997
"failed to get raw transaction with ID [%s]: [%w]",
@@ -129,7 +127,9 @@ func (c *Connection) GetTransactionConfirmations(
129127
// as Esplora/Electrs doesn't support verbose transactions.
130128
// See: https://github.com/Blockstream/electrs/pull/36
131129
return client.GetRawTransaction(ctx, txID)
132-
})
130+
},
131+
"GetRawTransaction",
132+
)
133133
if err != nil {
134134
return 0,
135135
fmt.Errorf(
@@ -178,7 +178,9 @@ txOutLoop:
178178
client *electrum.Client,
179179
) ([]*electrum.GetMempoolResult, error) {
180180
return client.GetHistory(ctx, reversedScriptHashString)
181-
})
181+
},
182+
"GetHistory",
183+
)
182184
if err != nil {
183185
// Don't return an error, but continue to the next TxOut entry.
184186
txLogger.Errorf("failed to get history for script hash: [%v]", err)
@@ -243,7 +245,9 @@ func (c *Connection) BroadcastTransaction(
243245
c,
244246
func(ctx context.Context, client *electrum.Client) (string, error) {
245247
return client.BroadcastTransaction(ctx, rawTx)
246-
})
248+
},
249+
"BroadcastTransaction",
250+
)
247251
if err != nil {
248252
return fmt.Errorf("failed to broadcast the transaction: [%w]", err)
249253
}
@@ -265,7 +269,9 @@ func (c *Connection) GetLatestBlockHeight() (uint, error) {
265269
}
266270
tip := <-headersChan
267271
return tip.Height, nil
268-
})
272+
},
273+
"SubscribeHeaders",
274+
)
269275
if err != nil {
270276
return 0, fmt.Errorf("failed to subscribe for headers: [%w]", err)
271277
}
@@ -291,6 +297,7 @@ func (c *Connection) GetBlockHeader(
291297
) (*electrum.GetBlockHeaderResult, error) {
292298
return client.GetBlockHeader(ctx, uint32(blockHeight), 0)
293299
},
300+
"GetBlockHeader",
294301
)
295302
if err != nil {
296303
return nil, fmt.Errorf("failed to get block header: [%w]", err)
@@ -325,6 +332,7 @@ func (c *Connection) GetTransactionMerkleProof(
325332
uint32(blockHeight),
326333
)
327334
},
335+
"GetMerkleProof",
328336
)
329337
if err != nil {
330338
return nil, fmt.Errorf("failed to get merkle proof: [%w]", err)
@@ -434,7 +442,9 @@ func (c *Connection) getConfirmedScriptHistory(
434442
client *electrum.Client,
435443
) ([]*electrum.GetMempoolResult, error) {
436444
return client.GetHistory(ctx, reversedScriptHashString)
437-
})
445+
},
446+
"GetHistory",
447+
)
438448
if err != nil {
439449
return nil, fmt.Errorf(
440450
"failed to get history for script [0x%x]: [%v]",
@@ -565,7 +575,9 @@ func (c *Connection) getScriptMempool(
565575
client *electrum.Client,
566576
) ([]*electrum.GetMempoolResult, error) {
567577
return client.GetMempool(ctx, reversedScriptHashString)
568-
})
578+
},
579+
"GetMempool",
580+
)
569581
if err != nil {
570582
return nil, fmt.Errorf(
571583
"failed to get mempool for script [0x%x]: [%v]",
@@ -614,7 +626,9 @@ func (c *Connection) EstimateSatPerVByteFee(blocks uint32) (int64, error) {
614626
// since version 1.4.2 of the protocol. We need to replace it
615627
// somehow once it disappears from Electrum implementations.
616628
return client.GetFee(ctx, blocks)
617-
})
629+
},
630+
"GetFee",
631+
)
618632
if err != nil {
619633
return 0, fmt.Errorf("failed to get fee: [%v]", err)
620634
}
@@ -673,6 +687,7 @@ func (c *Connection) verifyServer() error {
673687
}
674688
return &Server{serverVersion, protocolVersion}, nil
675689
},
690+
"ServerVersion",
676691
)
677692
if err != nil {
678693
return fmt.Errorf("failed to get server version: [%w]", err)
@@ -708,6 +723,7 @@ func (c *Connection) keepAlive() {
708723
func(ctx context.Context, client *electrum.Client) (interface{}, error) {
709724
return nil, client.Ping(ctx)
710725
},
726+
"Ping",
711727
)
712728
if err != nil {
713729
logger.Errorf(
@@ -757,7 +773,11 @@ func connectWithRetry(
757773
func requestWithRetry[K interface{}](
758774
c *Connection,
759775
requestFn func(ctx context.Context, client *electrum.Client) (K, error),
776+
requestName string,
760777
) (K, error) {
778+
startTime := time.Now()
779+
logger.Infof("starting [%s] request to Electrum server", requestName)
780+
761781
var result K
762782

763783
err := wrappers.DoWithDefaultRetry(
@@ -783,6 +803,19 @@ func requestWithRetry[K interface{}](
783803
return nil
784804
})
785805

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+
786819
return result, err
787820
}
788821

0 commit comments

Comments
 (0)