Skip to content

Commit

Permalink
Adding logger around electrum client calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dimpar committed Sep 14, 2023
1 parent d768be0 commit 43846c1
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions pkg/bitcoin/electrum/electrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func (c *Connection) GetTransaction(
// as Esplora/Electrs doesn't support verbose transactions.
// See: https://github.com/Blockstream/electrs/pull/36
return client.GetRawTransaction(ctx, txID)
})
},
"GetRawTransaction",
)
if err != nil {
return nil, fmt.Errorf(
"failed to get raw transaction with ID [%s]: [%w]",
Expand Down Expand Up @@ -125,7 +127,9 @@ func (c *Connection) GetTransactionConfirmations(
// as Esplora/Electrs doesn't support verbose transactions.
// See: https://github.com/Blockstream/electrs/pull/36
return client.GetRawTransaction(ctx, txID)
})
},
"GetRawTransaction",
)
if err != nil {
return 0,
fmt.Errorf(
Expand Down Expand Up @@ -174,7 +178,9 @@ txOutLoop:
client *electrum.Client,
) ([]*electrum.GetMempoolResult, error) {
return client.GetHistory(ctx, reversedScriptHashString)
})
},
"GetHistory",
)
if err != nil {
// Don't return an error, but continue to the next TxOut entry.
txLogger.Errorf("failed to get history for script hash: [%v]", err)
Expand Down Expand Up @@ -239,7 +245,9 @@ func (c *Connection) BroadcastTransaction(
c,
func(ctx context.Context, client *electrum.Client) (string, error) {
return client.BroadcastTransaction(ctx, rawTx)
})
},
"BroadcastTransaction",
)
if err != nil {
return fmt.Errorf("failed to broadcast the transaction: [%w]", err)
}
Expand All @@ -261,7 +269,9 @@ func (c *Connection) GetLatestBlockHeight() (uint, error) {
}
tip := <-headersChan
return tip.Height, nil
})
},
"SubscribeHeaders",
)
if err != nil {
return 0, fmt.Errorf("failed to subscribe for headers: [%w]", err)
}
Expand All @@ -287,6 +297,7 @@ func (c *Connection) GetBlockHeader(
) (*electrum.GetBlockHeaderResult, error) {
return client.GetBlockHeader(ctx, uint32(blockHeight), 0)
},
"GetBlockHeader",
)
if err != nil {
return nil, fmt.Errorf("failed to get block header: [%w]", err)
Expand Down Expand Up @@ -321,6 +332,7 @@ func (c *Connection) GetTransactionMerkleProof(
uint32(blockHeight),
)
},
"GetMerkleProof",
)
if err != nil {
return nil, fmt.Errorf("failed to get merkle proof: [%w]", err)
Expand Down Expand Up @@ -430,7 +442,9 @@ func (c *Connection) getConfirmedScriptHistory(
client *electrum.Client,
) ([]*electrum.GetMempoolResult, error) {
return client.GetHistory(ctx, reversedScriptHashString)
})
},
"GetHistory",
)
if err != nil {
return nil, fmt.Errorf(
"failed to get history for script [0x%x]: [%v]",
Expand Down Expand Up @@ -561,7 +575,9 @@ func (c *Connection) getScriptMempool(
client *electrum.Client,
) ([]*electrum.GetMempoolResult, error) {
return client.GetMempool(ctx, reversedScriptHashString)
})
},
"GetMempool",
)
if err != nil {
return nil, fmt.Errorf(
"failed to get mempool for script [0x%x]: [%v]",
Expand Down Expand Up @@ -610,7 +626,9 @@ func (c *Connection) EstimateSatPerVByteFee(blocks uint32) (int64, error) {
// since version 1.4.2 of the protocol. We need to replace it
// somehow once it disappears from Electrum implementations.
return client.GetFee(ctx, blocks)
})
},
"GetFee",
)
if err != nil {
return 0, fmt.Errorf("failed to get fee: [%v]", err)
}
Expand Down Expand Up @@ -669,6 +687,7 @@ func (c *Connection) verifyServer() error {
}
return &Server{serverVersion, protocolVersion}, nil
},
"ServerVersion",
)
if err != nil {
return fmt.Errorf("failed to get server version: [%w]", err)
Expand Down Expand Up @@ -704,6 +723,7 @@ func (c *Connection) keepAlive() {
func(ctx context.Context, client *electrum.Client) (interface{}, error) {
return nil, client.Ping(ctx)
},
"Ping",
)
if err != nil {
logger.Errorf(
Expand Down Expand Up @@ -753,7 +773,11 @@ func connectWithRetry(
func requestWithRetry[K interface{}](
c *Connection,
requestFn func(ctx context.Context, client *electrum.Client) (K, error),
requestName string,
) (K, error) {
startTime := time.Now()
logger.Infof("starting [%s] request to Electrum server", requestName)

var result K

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

solveRequestOutcome := func(err error) string {
if err != nil {
return fmt.Sprintf("error: [%v]", err)
}
return "success"
}

logger.Infof("[%s] request to Electrum server completed with [%s] after [%s]",
requestName,
solveRequestOutcome(err),
time.Since(startTime),
)

return result, err
}

Expand Down

0 comments on commit 43846c1

Please sign in to comment.