|
1 | 1 | package electrum |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/checksum0/go-electrum/electrum" |
| 11 | + "github.com/ipfs/go-log" |
| 12 | + "golang.org/x/exp/slices" |
| 13 | + |
| 14 | + "github.com/keep-network/keep-common/pkg/wrappers" |
4 | 15 | "github.com/keep-network/keep-core/pkg/bitcoin" |
5 | 16 | ) |
6 | 17 |
|
7 | | -type Client struct{} |
| 18 | +var ( |
| 19 | + supportedProtocolVersions = []string{"1.4"} |
| 20 | + logger = log.Logger("keep-electrum") |
| 21 | +) |
| 22 | + |
| 23 | +// Connection is a handle for interactions with Electrum server. |
| 24 | +type Connection struct { |
| 25 | + ctx context.Context |
| 26 | + client *electrum.Client |
| 27 | + requestRetryTimeout time.Duration |
| 28 | +} |
| 29 | + |
| 30 | +// Connect initializes handle with provided Config. |
| 31 | +func Connect(ctx context.Context, config Config) (bitcoin.Chain, error) { |
| 32 | + if config.KeepAliveInterval == 0 { |
| 33 | + config.KeepAliveInterval = DefaultKeepAliveInterval |
| 34 | + } |
| 35 | + if config.RequestRetryTimeout == 0 { |
| 36 | + config.RequestRetryTimeout = DefaultRequestRetryTimeout |
| 37 | + } |
| 38 | + |
| 39 | + var client *electrum.Client |
| 40 | + var err error |
| 41 | + switch config.Protocol { |
| 42 | + case TCP: |
| 43 | + client, err = electrum.NewClientTCP(ctx, config.URL) |
| 44 | + case SSL: |
| 45 | + // TODO: Implement certificate verification to be able to disable the `InsecureSkipVerify: true` workaround. |
| 46 | + tlsConfig := &tls.Config{InsecureSkipVerify: true} |
| 47 | + client, err = electrum.NewClientSSL(ctx, config.URL, tlsConfig) |
| 48 | + default: |
| 49 | + return nil, fmt.Errorf("unsupported protocol: [%s]", config.Protocol) |
| 50 | + } |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to initialize electrum client: [%w]", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Get the server's details. |
| 56 | + err = wrappers.DoWithDefaultRetry( |
| 57 | + config.RequestRetryTimeout, |
| 58 | + func(ctx context.Context, |
| 59 | + ) error { |
| 60 | + serverVersion, protocolVersion, err := client.ServerVersion(ctx) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("ServerVersion failed: [%w]", err) |
| 63 | + } |
| 64 | + logger.Infof( |
| 65 | + "connected to electrum server [version: [%s], protocol: [%s]]", |
| 66 | + serverVersion, |
| 67 | + protocolVersion, |
| 68 | + ) |
| 69 | + |
| 70 | + // Log a warning if connected to a server running an unsupported protocol version. |
| 71 | + if !slices.Contains(supportedProtocolVersions, protocolVersion) { |
| 72 | + logger.Warnf( |
| 73 | + "electrum server [%s] runs an unsupported protocol version: [%s]; expected one of: [%s]", |
| 74 | + config.URL, |
| 75 | + protocolVersion, |
| 76 | + strings.Join(supportedProtocolVersions, ","), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("failed to get server version: [%w]", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Keep the connection alive and check the connection health. |
| 87 | + go func() { |
| 88 | + ticker := time.NewTicker(config.KeepAliveInterval) |
| 89 | + |
| 90 | + for { |
| 91 | + select { |
| 92 | + case <-ticker.C: |
| 93 | + err := wrappers.DoWithDefaultRetry(config.RequestRetryTimeout, client.Ping) |
| 94 | + if err != nil { |
| 95 | + logger.Errorf( |
| 96 | + "failed to ping the electrum server; "+ |
| 97 | + "please verify health of the electrum server: [%v]", |
| 98 | + err, |
| 99 | + ) |
| 100 | + } |
| 101 | + case <-ctx.Done(): |
| 102 | + ticker.Stop() |
| 103 | + client.Shutdown() |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + }() |
| 108 | + |
| 109 | + // TODO: Add reconnects on lost connection. |
| 110 | + |
| 111 | + return &Connection{ |
| 112 | + ctx: ctx, |
| 113 | + client: client, |
| 114 | + requestRetryTimeout: config.RequestRetryTimeout, |
| 115 | + }, nil |
| 116 | +} |
8 | 117 |
|
9 | | -func (c *Client) GetTransaction( |
| 118 | +func (c *Connection) GetTransaction( |
10 | 119 | transactionHash bitcoin.Hash, |
11 | 120 | ) (*bitcoin.Transaction, error) { |
12 | 121 | // TODO: Implementation. |
13 | 122 | panic("not implemented") |
14 | 123 | } |
15 | 124 |
|
16 | | -func (c *Client) GetTransactionConfirmations( |
| 125 | +func (c *Connection) GetTransactionConfirmations( |
17 | 126 | transactionHash bitcoin.Hash, |
18 | 127 | ) (uint, error) { |
19 | 128 | // TODO: Implementation. |
20 | 129 | panic("not implemented") |
21 | 130 | } |
22 | 131 |
|
23 | | -func (c *Client) BroadcastTransaction( |
| 132 | +func (c *Connection) BroadcastTransaction( |
24 | 133 | transaction *bitcoin.Transaction, |
25 | 134 | ) error { |
26 | 135 | // TODO: Implementation. |
27 | 136 | panic("not implemented") |
28 | 137 | } |
29 | 138 |
|
30 | | -func (c *Client) GetLatestBlockHeight() (uint, error) { |
| 139 | +func (c *Connection) GetLatestBlockHeight() (uint, error) { |
31 | 140 | // TODO: Implementation. |
32 | 141 | panic("not implemented") |
33 | 142 | } |
34 | 143 |
|
35 | | -func (c *Client) GetBlockHeader( |
| 144 | +func (c *Connection) GetBlockHeader( |
36 | 145 | blockHeight uint, |
37 | 146 | ) (*bitcoin.BlockHeader, error) { |
38 | 147 | // TODO: Implementation. |
|
0 commit comments