rabid.go raw
1 package verify
2
3 import (
4 "context"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "io"
9 "net/http"
10 "strings"
11 )
12
13 // RabidAdapter speaks the forked chain's explorer, which answers a pruned node: it has the tip, the
14 // block list and recent transactions, and it refuses anything older than its retention window.
15 //
16 // The shape differs from Esplora, so it is a separate type rather than a path override: pretending
17 // the two were the same would produce a tip that is a height and a fee that is a minimum relay
18 // rate, silently.
19 type RabidAdapter struct {
20 // Operator is the name shown to the user and used in agreement reports.
21 Operator string
22 // BaseURL is the API root, without a trailing slash.
23 BaseURL string
24 // Client performs the requests.
25 Client HTTPDoer
26 }
27
28 // NewRabidAdapter builds an adapter for one pruned explorer.
29 func NewRabidAdapter(name, baseURL string, client HTTPDoer) (*RabidAdapter, error) {
30 if strings.TrimSpace(name) == "" {
31 return nil, fmt.Errorf("verify: an indexer needs a name")
32 }
33
34 if client == nil {
35 return nil, fmt.Errorf("verify: an indexer needs an HTTP client")
36 }
37
38 if !strings.HasPrefix(baseURL, "https://") && !strings.HasPrefix(baseURL, "http://") {
39 return nil, fmt.Errorf("verify: %q is not an indexer URL", baseURL)
40 }
41
42 return &RabidAdapter{Operator: name, BaseURL: strings.TrimRight(baseURL, "/"), Client: client}, nil
43 }
44
45 // Name identifies the operator.
46 func (a *RabidAdapter) Name() string { return a.Operator }
47
48 // Tx asks one transaction. The explorer keeps only recent blocks, so an answer that it does not
49 // have the transaction is unverifiable, never "absent": the chain may well have it.
50 func (a *RabidAdapter) Tx(ctx context.Context, txid [32]byte) (TxFact, error) {
51 body, err := a.get(ctx, "/tx/"+Hash(txid))
52
53 if err != nil {
54 return TxFact{}, err
55 }
56
57 var answer struct {
58 TxID string `json:"txid"`
59 Hex string `json:"hex"`
60 Raw string `json:"raw"`
61 Error string `json:"error"`
62 Height int64 `json:"block_height"`
63 }
64
65 if err := json.Unmarshal(body, &answer); err != nil {
66 return TxFact{}, fmt.Errorf("verify: %s: tx: %w", a.Name(), err)
67 }
68
69 if answer.Error != "" || (answer.TxID == "" && answer.Hex == "") {
70 return TxFact{}, fmt.Errorf("%w: %s has no record of %s (it keeps only recent blocks)",
71 ErrUnverifiable, a.Name(), Hash(txid))
72 }
73
74 rawText := answer.Hex
75
76 if rawText == "" {
77 rawText = answer.Raw
78 }
79
80 raw, err := hex.DecodeString(rawText)
81
82 if err != nil {
83 return TxFact{}, fmt.Errorf("verify: %s: tx bytes: %w", a.Name(), err)
84 }
85
86 return TxFact{Found: true, Raw: raw, Height: answer.Height}, nil
87 }
88
89 // Tip asks for the chain head.
90 func (a *RabidAdapter) Tip(ctx context.Context) (Tip, error) {
91 body, err := a.get(ctx, "/tip")
92
93 if err != nil {
94 return Tip{}, err
95 }
96
97 var answer struct {
98 Tip struct {
99 Height int64 `json:"height"`
100 Hash string `json:"hash"`
101 } `json:"tip"`
102 }
103
104 if err := json.Unmarshal(body, &answer); err != nil {
105 return Tip{}, fmt.Errorf("verify: %s: tip: %w", a.Name(), err)
106 }
107
108 hash, err := parseDisplayHash(answer.Tip.Hash)
109
110 if err != nil {
111 return Tip{}, fmt.Errorf("verify: %s: tip hash: %w", a.Name(), err)
112 }
113
114 return Tip{Height: answer.Tip.Height, Hash: hash}, nil
115 }
116
117 // FeeEstimate reports the minimum relay rate the explorer sees, which is a floor and not an
118 // estimate: it is the only fee number this explorer publishes.
119 func (a *RabidAdapter) FeeEstimate(ctx context.Context) (int64, error) {
120 body, err := a.get(ctx, "/mempool")
121
122 if err != nil {
123 return 0, err
124 }
125
126 var answer struct {
127 MinFeeRate float64 `json:"min_fee_rate"`
128 }
129
130 if err := json.Unmarshal(body, &answer); err != nil {
131 return 0, fmt.Errorf("verify: %s: mempool: %w", a.Name(), err)
132 }
133
134 if answer.MinFeeRate <= 0 {
135 return 0, fmt.Errorf("%w: %s published no fee floor", ErrUnverifiable, a.Name())
136 }
137
138 // The explorer reports BTC/kvB; the engine speaks satoshis per virtual byte.
139 return int64(answer.MinFeeRate*1e8/1000 + 0.5), nil
140 }
141
142 func (a *RabidAdapter) get(ctx context.Context, path string) ([]byte, error) {
143 request, err := http.NewRequestWithContext(ctx, http.MethodGet, a.BaseURL+path, nil)
144
145 if err != nil {
146 return nil, err
147 }
148
149 response, err := a.Client.Do(request)
150
151 if err != nil {
152 return nil, fmt.Errorf("verify: %s: %w", a.Name(), err)
153 }
154
155 defer response.Body.Close()
156
157 if response.StatusCode != http.StatusOK {
158 return nil, fmt.Errorf("verify: %s: %s: HTTP %d", a.Name(), path, response.StatusCode)
159 }
160
161 return io.ReadAll(io.LimitReader(response.Body, 4<<20))
162 }
163
164 // AdapterFor builds the right adapter for a preset.
165 func AdapterFor(preset Preset, client HTTPDoer) (Source, error) {
166 switch preset.Shape {
167 case "esplora":
168 return NewHTTPAdapter(preset.Name, preset.BaseURL, client)
169 case "rabid":
170 return NewRabidAdapter(preset.Name, preset.BaseURL, client)
171 default:
172 return nil, fmt.Errorf("verify: preset %s has unknown shape %q", preset.Name, preset.Shape)
173 }
174 }
175