Skip to content

Commit

Permalink
Logs for electrum connection (#3715)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukasz-zimnoch authored Sep 14, 2023
2 parents 5c2604c + 7fbb5ca commit 0bef44a
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions pkg/bitcoin/electrum/electrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import (
"github.com/keep-network/keep-core/pkg/internal/byteutils"
)

// TODO: Some problems with Electrum re-connect were detected while developing
// integration tests: https://github.com/keep-network/keep-core/issues/3586.
// Make sure the problem is resolved soon.

var (
supportedProtocolVersions = []string{"1.4"}
logger = log.Logger("keep-electrum")
Expand Down Expand Up @@ -93,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 @@ -129,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 @@ -178,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 @@ -243,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 @@ -265,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 @@ -291,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 @@ -325,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 @@ -434,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 @@ -565,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 @@ -614,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 @@ -673,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 @@ -708,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 @@ -757,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 @@ -783,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 0bef44a

Please sign in to comment.