adapter_test.go raw

   1  package verify
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"io"
   7  	"net/http"
   8  	"strings"
   9  	"testing"
  10  
  11  	"git.smesh.lol/tinywallet/pkg/tx"
  12  )
  13  
  14  type doer func(*http.Request) (*http.Response, error)
  15  
  16  func (d doer) Do(r *http.Request) (*http.Response, error) { return d(r) }
  17  
  18  func reply(status int, body string) *http.Response {
  19  	return &http.Response{StatusCode: status, Body: io.NopCloser(strings.NewReader(body))}
  20  }
  21  
  22  func TestAdapterNeedsExplicitConfiguration(t *testing.T) {
  23  	if _, err := NewHTTPAdapter("x", "", doer(nil)); err == nil {
  24  		t.Fatal("an empty base URL was accepted")
  25  	}
  26  
  27  	if _, err := NewHTTPAdapter("", "https://a.example", doer(nil)); err == nil {
  28  		t.Fatal("an unnamed indexer was accepted")
  29  	}
  30  }
  31  
  32  func TestAdapterTxHashesNothingAndEngineDoes(t *testing.T) {
  33  	built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 7, Script: []byte{0x51}})
  34  	raw := rawOf(t, built)
  35  
  36  	txid, err := TxidOf(raw)
  37  
  38  	if err != nil {
  39  		t.Fatal(err)
  40  	}
  41  
  42  	adapter, err := NewHTTPAdapter("idx", "https://idx.example", doer(func(r *http.Request) (*http.Response, error) {
  43  		switch {
  44  		case strings.HasSuffix(r.URL.Path, "/hex"):
  45  			return reply(200, hexString(raw)), nil
  46  		case strings.Contains(r.URL.Path, "/tx/"):
  47  			return reply(200, `{"status":{"confirmed":true,"block_height":962000}}`), nil
  48  		default:
  49  			return reply(404, ""), nil
  50  		}
  51  	}))
  52  
  53  	if err != nil {
  54  		t.Fatal(err)
  55  	}
  56  
  57  	fact, err := adapter.Tx(context.Background(), txid)
  58  
  59  	if err != nil {
  60  		t.Fatal(err)
  61  	}
  62  
  63  	if !fact.Found || fact.Height != 962000 {
  64  		t.Fatalf("fact %+v", fact)
  65  	}
  66  
  67  	if got, err := TxidOf(fact.Raw); err != nil || got != txid {
  68  		t.Fatal("the adapter's bytes do not hash to the requested txid")
  69  	}
  70  }
  71  
  72  func TestAdapterTipAndFee(t *testing.T) {
  73  	tipHash := strings.Repeat("11", 32)
  74  
  75  	adapter, err := NewHTTPAdapter("idx", "https://idx.example", doer(func(r *http.Request) (*http.Response, error) {
  76  		switch r.URL.Path {
  77  		case "/blocks/tip/hash":
  78  			return reply(200, tipHash), nil
  79  		case "/blocks/tip/height":
  80  			return reply(200, "962000"), nil
  81  		case "/fee-estimates":
  82  			return reply(200, `{"1":30.0,"6":12.4}`), nil
  83  		default:
  84  			return reply(404, ""), nil
  85  		}
  86  	}))
  87  
  88  	if err != nil {
  89  		t.Fatal(err)
  90  	}
  91  
  92  	tip, err := adapter.Tip(context.Background())
  93  
  94  	if err != nil {
  95  		t.Fatal(err)
  96  	}
  97  
  98  	if tip.Height != 962000 {
  99  		t.Fatalf("tip %+v", tip)
 100  	}
 101  
 102  	rate, err := adapter.FeeEstimate(context.Background())
 103  
 104  	if err != nil {
 105  		t.Fatal(err)
 106  	}
 107  
 108  	if rate != 12 {
 109  		t.Fatalf("fee rate %d, want 12", rate)
 110  	}
 111  }
 112  
 113  func hexString(b []byte) string {
 114  	const digits = "0123456789abcdef"
 115  
 116  	out := make([]byte, 0, 2*len(b))
 117  
 118  	for _, v := range b {
 119  		out = append(out, digits[v>>4], digits[v&0xf])
 120  	}
 121  
 122  	return string(out)
 123  }
 124  
 125  var _ = errors.New
 126