|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "net/http" |
| 11 | +) |
| 12 | + |
| 13 | +type Check func(ctx context.Context, client *http.Client, cfg Config) error |
| 14 | + |
| 15 | +var registry = map[string]Check{ |
| 16 | + "blockdownload": checkBlockDownload, |
| 17 | + "txindex": checkTxIndex, |
| 18 | +} |
| 19 | + |
| 20 | +type rpcRequest struct { |
| 21 | + JSONRPC string `json:"jsonrpc"` |
| 22 | + ID string `json:"id"` |
| 23 | + Method string `json:"method"` |
| 24 | + Params []any `json:"params"` |
| 25 | +} |
| 26 | + |
| 27 | +func doRPC(ctx context.Context, client *http.Client, cfg Config, method string) (json.RawMessage, error) { |
| 28 | + logger := slog.Default() |
| 29 | + body, err := json.Marshal(rpcRequest{JSONRPC: "1.0", ID: "hc", Method: method, Params: []any{}}) |
| 30 | + if err != nil { |
| 31 | + return nil, fmt.Errorf("marshal request: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.NodeURL, bytes.NewReader(body)) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("build request: %w", err) |
| 37 | + } |
| 38 | + req.Header.Set("Content-Type", "application/json") |
| 39 | + if cfg.NodeUser != "" { |
| 40 | + req.SetBasicAuth(cfg.NodeUser, cfg.NodePass) |
| 41 | + } |
| 42 | + |
| 43 | + resp, err := client.Do(req) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("node unreachable: %w", err) |
| 46 | + } |
| 47 | + defer resp.Body.Close() |
| 48 | + |
| 49 | + if resp.StatusCode != http.StatusOK { |
| 50 | + return nil, fmt.Errorf("node returned HTTP %d", resp.StatusCode) |
| 51 | + } |
| 52 | + |
| 53 | + data, err := io.ReadAll(resp.Body) |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("read response: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + logger.Debug("rpc response", "method", method, "body", string(data)) |
| 60 | + |
| 61 | + var envelope struct { |
| 62 | + Result json.RawMessage `json:"result"` |
| 63 | + Error any `json:"error"` |
| 64 | + } |
| 65 | + |
| 66 | + if err := json.Unmarshal(data, &envelope); err != nil { |
| 67 | + return nil, fmt.Errorf("invalid JSON from node: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + if envelope.Error != nil { |
| 71 | + return nil, fmt.Errorf("node RPC error: %v", envelope.Error) |
| 72 | + } |
| 73 | + |
| 74 | + return envelope.Result, nil |
| 75 | +} |
| 76 | + |
| 77 | +func checkBlockDownload(ctx context.Context, client *http.Client, cfg Config) error { |
| 78 | + result, err := doRPC(ctx, client, cfg, "getblockchaininfo") |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + var info struct { |
| 84 | + InitialBlockDownload bool `json:"initialblockdownload"` |
| 85 | + } |
| 86 | + |
| 87 | + if err := json.Unmarshal(result, &info); err != nil { |
| 88 | + return fmt.Errorf("parse getblockchaininfo: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + if info.InitialBlockDownload { |
| 92 | + return fmt.Errorf("node is still syncing (initialblockdownload=true)") |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func checkTxIndex(ctx context.Context, client *http.Client, cfg Config) error { |
| 99 | + result, err := doRPC(ctx, client, cfg, "getindexinfo") |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + var info struct { |
| 105 | + TxIndex struct { |
| 106 | + Synced bool `json:"synced"` |
| 107 | + } `json:"txindex"` |
| 108 | + } |
| 109 | + |
| 110 | + if err := json.Unmarshal(result, &info); err != nil { |
| 111 | + return fmt.Errorf("parse getindexinfo: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + if !info.TxIndex.Synced { |
| 115 | + return fmt.Errorf("txindex is not synced") |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
0 commit comments