utxo_test.go raw

   1  package service_test
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/binary"
   6  	"encoding/json"
   7  	"net/http"
   8  	"net/http/httptest"
   9  	"testing"
  10  
  11  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  12  
  13  	"git.smesh.lol/btcd-lite/internal/service"
  14  	"git.smesh.lol/btcd-lite/internal/utxo"
  15  	utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
  16  	"git.smesh.lol/btcd-lite/internal/wire"
  17  )
  18  
  19  func qop(i uint32) wire.OutPoint {
  20  	var h [32]byte
  21  	binary.LittleEndian.PutUint32(h[:4], i)
  22  	binary.BigEndian.PutUint32(h[28:], i)
  23  	return wire.OutPoint{Hash: h}
  24  }
  25  
  26  // chain lays down two checkpoints: height 0 with nothing, height 10 with three
  27  // entries.
  28  func chain(t *testing.T) (string, []utxo.Entry) {
  29  	t.Helper()
  30  	dir := t.TempDir()
  31  	if _, err := utxostore.WriteSet(dir, 0, nil); err != nil {
  32  		t.Fatal(err)
  33  	}
  34  	if _, err := utxostore.Promote(dir, 0); err != nil {
  35  		t.Fatal(err)
  36  	}
  37  	set := []utxo.Entry{
  38  		{OutPoint: qop(1), Value: 100, Height: 3},
  39  		{OutPoint: qop(2), Value: 200, Height: 5},
  40  		{OutPoint: qop(3), Value: 300, Height: 9, Coinbase: true},
  41  	}
  42  	if _, err := utxostore.WriteSet(dir, 10, set); err != nil {
  43  		t.Fatal(err)
  44  	}
  45  	if _, err := utxostore.Promote(dir, 10); err != nil {
  46  		t.Fatal(err)
  47  	}
  48  	return dir, set
  49  }
  50  
  51  func post(t *testing.T, url, body string) (int, service.UTXOResponse) {
  52  	t.Helper()
  53  	resp, err := http.Post(url, "application/json", bytes.NewReader([]byte(body)))
  54  	if err != nil {
  55  		t.Fatal(err)
  56  	}
  57  	defer resp.Body.Close()
  58  	var out service.UTXOResponse
  59  	_ = json.NewDecoder(resp.Body).Decode(&out)
  60  	return resp.StatusCode, out
  61  }
  62  
  63  // TestUTXOQuery: an unspent outpoint comes back with its value and height, a
  64  // spent one comes back unspent false, and the answer names the checkpoint it
  65  // came from.
  66  func TestUTXOQuery(t *testing.T) {
  67  	dir, set := chain(t)
  68  	srv := httptest.NewServer(service.Handler(nil, service.WithUTXO(map[string]service.UTXOSource{"btc": &service.DiskUTXO{Dirs: []string{dir}}})))
  69  	defer srv.Close()
  70  	unspent, spent := set[1], qop(99)
  71  	body := `{"outpoints":["` + unspent.OutPoint.Hash.String() + `:0","` + spent.Hash.String() + `:0"]}`
  72  	code, out := post(t, srv.URL+"/v1/chains/btc/utxo/query", body)
  73  	if code != 200 {
  74  		t.Fatalf("status %d", code)
  75  	}
  76  	if out.At != 10 || len(out.Results) != 2 {
  77  		t.Fatalf("answer %+v", out)
  78  	}
  79  	if !out.Results[0].Unspent || out.Results[0].Value != 200 || out.Results[0].Height != 5 {
  80  		t.Fatalf("unspent answer %+v", out.Results[0])
  81  	}
  82  	if out.Results[1].Unspent {
  83  		t.Fatalf("a spent outpoint came back unspent: %+v", out.Results[1])
  84  	}
  85  }
  86  
  87  // TestUTXOQueryHeight: a query at a height below the only entries is answered
  88  // from the checkpoint at or below it, which is empty.
  89  func TestUTXOQueryHeight(t *testing.T) {
  90  	dir, set := chain(t)
  91  	srv := httptest.NewServer(service.Handler(nil, service.WithUTXO(map[string]service.UTXOSource{"btc": &service.DiskUTXO{Dirs: []string{dir}}})))
  92  	defer srv.Close()
  93  	body := `{"outpoints":["` + set[0].OutPoint.Hash.String() + `:0"],"height":5}`
  94  	code, out := post(t, srv.URL+"/v1/chains/btc/utxo/query", body)
  95  	if code != 200 {
  96  		t.Fatalf("status %d", code)
  97  	}
  98  	if out.At != 0 || out.Results[0].Unspent {
  99  		t.Fatalf("answer at 5: %+v", out)
 100  	}
 101  	// And at 10 it is unspent.
 102  	body = `{"outpoints":["` + set[0].OutPoint.Hash.String() + `:0"],"height":10}`
 103  	_, out = post(t, srv.URL+"/v1/chains/btc/utxo/query", body)
 104  	if out.At != 10 || !out.Results[0].Unspent {
 105  		t.Fatalf("answer at 10: %+v", out)
 106  	}
 107  }
 108  
 109  // TestUTXOQueryRefusals: another chain, a bad outpoint and no outpoints are all
 110  // refused.
 111  func TestUTXOQueryRefusals(t *testing.T) {
 112  	dir, _ := chain(t)
 113  	srv := httptest.NewServer(service.Handler(nil, service.WithUTXO(map[string]service.UTXOSource{"btc": &service.DiskUTXO{Dirs: []string{dir}}})))
 114  	defer srv.Close()
 115  	for _, c := range []struct {
 116  		url  string
 117  		body string
 118  		want int
 119  	}{
 120  		{"/v1/chains/xbt/utxo/query", `{"outpoints":["` + chainhash.Hash{}.String() + `:0"]}`, 404},
 121  		{"/v1/chains/btc/utxo/query", `{"outpoints":["nonsense"]}`, 400},
 122  		{"/v1/chains/btc/utxo/query", `{"outpoints":[]}`, 400},
 123  	} {
 124  		code, _ := post(t, srv.URL+c.url, c.body)
 125  		if code != c.want {
 126  			t.Fatalf("%s answered %d, want %d", c.url, code, c.want)
 127  		}
 128  	}
 129  }
 130  
 131  // TestUTXOCheckpoints lists the chain's boundaries.
 132  func TestUTXOCheckpoints(t *testing.T) {
 133  	dir, _ := chain(t)
 134  	srv := httptest.NewServer(service.Handler(nil, service.WithUTXO(map[string]service.UTXOSource{"btc": &service.DiskUTXO{Dirs: []string{dir}}})))
 135  	defer srv.Close()
 136  	resp, err := http.Get(srv.URL + "/v1/chains/btc/utxo/checkpoints")
 137  	if err != nil {
 138  		t.Fatal(err)
 139  	}
 140  	defer resp.Body.Close()
 141  	var out struct {
 142  		Checkpoints []uint32 `json:"checkpoints"`
 143  	}
 144  	_ = json.NewDecoder(resp.Body).Decode(&out)
 145  	if len(out.Checkpoints) != 2 || out.Checkpoints[1] != 10 {
 146  		t.Fatalf("%+v", out)
 147  	}
 148  }
 149