package verify import ( "context" "encoding/hex" "encoding/json" "fmt" "io" "net/http" "strings" ) // RabidAdapter speaks the forked chain's explorer, which answers a pruned node: it has the tip, the // block list and recent transactions, and it refuses anything older than its retention window. // // The shape differs from Esplora, so it is a separate type rather than a path override: pretending // the two were the same would produce a tip that is a height and a fee that is a minimum relay // rate, silently. type RabidAdapter struct { // Operator is the name shown to the user and used in agreement reports. Operator string // BaseURL is the API root, without a trailing slash. BaseURL string // Client performs the requests. Client HTTPDoer } // NewRabidAdapter builds an adapter for one pruned explorer. func NewRabidAdapter(name, baseURL string, client HTTPDoer) (*RabidAdapter, error) { if strings.TrimSpace(name) == "" { return nil, fmt.Errorf("verify: an indexer needs a name") } if client == nil { return nil, fmt.Errorf("verify: an indexer needs an HTTP client") } if !strings.HasPrefix(baseURL, "https://") && !strings.HasPrefix(baseURL, "http://") { return nil, fmt.Errorf("verify: %q is not an indexer URL", baseURL) } return &RabidAdapter{Operator: name, BaseURL: strings.TrimRight(baseURL, "/"), Client: client}, nil } // Name identifies the operator. func (a *RabidAdapter) Name() string { return a.Operator } // Tx asks one transaction. The explorer keeps only recent blocks, so an answer that it does not // have the transaction is unverifiable, never "absent": the chain may well have it. func (a *RabidAdapter) Tx(ctx context.Context, txid [32]byte) (TxFact, error) { body, err := a.get(ctx, "/tx/"+Hash(txid)) if err != nil { return TxFact{}, err } var answer struct { TxID string `json:"txid"` Hex string `json:"hex"` Raw string `json:"raw"` Error string `json:"error"` Height int64 `json:"block_height"` } if err := json.Unmarshal(body, &answer); err != nil { return TxFact{}, fmt.Errorf("verify: %s: tx: %w", a.Name(), err) } if answer.Error != "" || (answer.TxID == "" && answer.Hex == "") { return TxFact{}, fmt.Errorf("%w: %s has no record of %s (it keeps only recent blocks)", ErrUnverifiable, a.Name(), Hash(txid)) } rawText := answer.Hex if rawText == "" { rawText = answer.Raw } raw, err := hex.DecodeString(rawText) if err != nil { return TxFact{}, fmt.Errorf("verify: %s: tx bytes: %w", a.Name(), err) } return TxFact{Found: true, Raw: raw, Height: answer.Height}, nil } // Tip asks for the chain head. func (a *RabidAdapter) Tip(ctx context.Context) (Tip, error) { body, err := a.get(ctx, "/tip") if err != nil { return Tip{}, err } var answer struct { Tip struct { Height int64 `json:"height"` Hash string `json:"hash"` } `json:"tip"` } if err := json.Unmarshal(body, &answer); err != nil { return Tip{}, fmt.Errorf("verify: %s: tip: %w", a.Name(), err) } hash, err := parseDisplayHash(answer.Tip.Hash) if err != nil { return Tip{}, fmt.Errorf("verify: %s: tip hash: %w", a.Name(), err) } return Tip{Height: answer.Tip.Height, Hash: hash}, nil } // FeeEstimate reports the minimum relay rate the explorer sees, which is a floor and not an // estimate: it is the only fee number this explorer publishes. func (a *RabidAdapter) FeeEstimate(ctx context.Context) (int64, error) { body, err := a.get(ctx, "/mempool") if err != nil { return 0, err } var answer struct { MinFeeRate float64 `json:"min_fee_rate"` } if err := json.Unmarshal(body, &answer); err != nil { return 0, fmt.Errorf("verify: %s: mempool: %w", a.Name(), err) } if answer.MinFeeRate <= 0 { return 0, fmt.Errorf("%w: %s published no fee floor", ErrUnverifiable, a.Name()) } // The explorer reports BTC/kvB; the engine speaks satoshis per virtual byte. return int64(answer.MinFeeRate*1e8/1000 + 0.5), nil } func (a *RabidAdapter) get(ctx context.Context, path string) ([]byte, error) { request, err := http.NewRequestWithContext(ctx, http.MethodGet, a.BaseURL+path, nil) if err != nil { return nil, err } response, err := a.Client.Do(request) if err != nil { return nil, fmt.Errorf("verify: %s: %w", a.Name(), err) } defer response.Body.Close() if response.StatusCode != http.StatusOK { return nil, fmt.Errorf("verify: %s: %s: HTTP %d", a.Name(), path, response.StatusCode) } return io.ReadAll(io.LimitReader(response.Body, 4<<20)) } // AdapterFor builds the right adapter for a preset. func AdapterFor(preset Preset, client HTTPDoer) (Source, error) { switch preset.Shape { case "esplora": return NewHTTPAdapter(preset.Name, preset.BaseURL, client) case "rabid": return NewRabidAdapter(preset.Name, preset.BaseURL, client) default: return nil, fmt.Errorf("verify: preset %s has unknown shape %q", preset.Name, preset.Shape) } }