process_test.go raw

   1  package process_test
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"strings"
   7  	"testing"
   8  
   9  	"git.smesh.lol/nostrino/pkg/btcwire"
  10  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  11  	"git.smesh.lol/nostrino/pkg/neutrino"
  12  
  13  	"git.smesh.lol/btcd-lite/internal/process"
  14  	"git.smesh.lol/btcd-lite/internal/staging"
  15  	"git.smesh.lol/btcd-lite/internal/store"
  16  )
  17  
  18  // headerAt is a distinct valid header per height.
  19  func headerAt(i uint32) btcwire.BlockHeader {
  20  	return btcwire.BlockHeader{
  21  		Version:    1,
  22  		MerkleRoot: chainhash.Hash{byte(i), byte(i >> 8)},
  23  		Timestamp:  1231006505 + i,
  24  		Bits:       0x1d00ffff,
  25  		Nonce:      i,
  26  	}
  27  }
  28  
  29  // fixture is a chain of n heights in a header store, an empty body store and a
  30  // staging log, which is what a fill leaves behind.
  31  func fixture(t *testing.T, n int) (process.Config, *staging.Log, *store.BlockStore) {
  32  	t.Helper()
  33  	dir := t.TempDir()
  34  	headers, err := store.NewHeaderStore(filepath.Join(dir, "headers"))
  35  	if err != nil {
  36  		t.Fatal(err)
  37  	}
  38  	t.Cleanup(func() { headers.Close() })
  39  	hdrs := make([]btcwire.BlockHeader, 0, n)
  40  	for i := 0; i < n; i++ {
  41  		hdrs = append(hdrs, headerAt(uint32(i)))
  42  	}
  43  	if err := headers.SetAnchor(neutrino.Anchor{Height: 0, BlockHash: hdrs[0].BlockHash(), FilterHeight: -1}); err != nil {
  44  		t.Fatal(err)
  45  	}
  46  	if err := headers.PutHeaders(0, hdrs); err != nil {
  47  		t.Fatal(err)
  48  	}
  49  	bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks"))
  50  	if err != nil {
  51  		t.Fatal(err)
  52  	}
  53  	t.Cleanup(func() { bodies.Close() })
  54  	log, err := staging.Open(filepath.Join(dir, "staging"))
  55  	if err != nil {
  56  		t.Fatal(err)
  57  	}
  58  	t.Cleanup(func() { log.Close() })
  59  	return process.Config{
  60  		Chain:   "btc",
  61  		Staging: log,
  62  		Bodies:  bodies,
  63  		Headers: headers,
  64  		Missing: filepath.Join(dir, "missing.log"),
  65  		Logf:    t.Logf,
  66  	}, log, bodies
  67  }
  68  
  69  // body is the bytes a fill would have had for a header: the header and filler.
  70  func body(h btcwire.BlockHeader) []byte {
  71  	return append(h.SerializeAll(), make([]byte, 24)...)
  72  }
  73  
  74  // TestStagedBodiesBecomeBlocks: a body whose claim and header agree is stored,
  75  // and the log is empty afterwards.
  76  func TestStagedBodiesBecomeBlocks(t *testing.T) {
  77  	cfg, log, bodies := fixture(t, 4)
  78  	for i := uint32(0); i < 4; i++ {
  79  		h := headerAt(i)
  80  		if err := log.Append(i, h.BlockHash(), body(h)); err != nil {
  81  			t.Fatal(err)
  82  		}
  83  	}
  84  	res, err := process.Run(cfg)
  85  	if err != nil {
  86  		t.Fatal(err)
  87  	}
  88  	if res.Kept != 4 || res.Dropped != 0 {
  89  		t.Fatalf("%d kept, %d dropped", res.Kept, res.Dropped)
  90  	}
  91  	if bodies.Len() != 4 {
  92  		t.Fatalf("the store holds %d blocks", bodies.Len())
  93  	}
  94  	if log.Len() != 0 {
  95  		t.Fatalf("the log holds %d bodies after the pass", log.Len())
  96  	}
  97  	// The stored block is the staged bytes, under the header's hash.
  98  	h2 := headerAt(2)
  99  	got, err := bodies.GetBlock(h2.BlockHash())
 100  	if err != nil {
 101  		t.Fatalf("height 2 is not stored: %v", err)
 102  	}
 103  	if string(got) != string(body(h2)) {
 104  		t.Fatal("the stored bytes are not the staged ones")
 105  	}
 106  }
 107  
 108  // TestWrongClaimIsDropped: a body filed under a hash it does not commit to is
 109  // dropped, the rest of the fill survives, and the height is written down.
 110  func TestWrongClaimIsDropped(t *testing.T) {
 111  	cfg, log, bodies := fixture(t, 3)
 112  	good := headerAt(0)
 113  	if err := log.Append(0, good.BlockHash(), body(good)); err != nil {
 114  		t.Fatal(err)
 115  	}
 116  	h1, h2 := headerAt(1), headerAt(2)
 117  	// Height 1's body arrives under height 1's hash but the body is height 2's.
 118  	if err := log.Append(1, h1.BlockHash(), body(h2)); err != nil {
 119  		t.Fatal(err)
 120  	}
 121  	if err := log.Append(2, h2.BlockHash(), body(h2)); err != nil {
 122  		t.Fatal(err)
 123  	}
 124  	res, err := process.Run(cfg)
 125  	if err != nil {
 126  		t.Fatal(err)
 127  	}
 128  	if res.Kept != 2 || res.Dropped != 1 {
 129  		t.Fatalf("%d kept, %d dropped", res.Kept, res.Dropped)
 130  	}
 131  	if len(res.Failed) != 1 || res.Failed[0] != 1 {
 132  		t.Fatalf("failed heights %v", res.Failed)
 133  	}
 134  	if bodies.HasBlock(h1.BlockHash()) {
 135  		t.Fatal("the refused body was stored")
 136  	}
 137  	if !bodies.HasBlock(h2.BlockHash()) {
 138  		t.Fatal("the body after the refused one was not stored")
 139  	}
 140  	raw, err := os.ReadFile(cfg.Missing)
 141  	if err != nil {
 142  		t.Fatalf("no record of the drop: %v", err)
 143  	}
 144  	if !strings.Contains(string(raw), "btc 1 ") {
 145  		t.Fatalf("the record does not name the height: %q", raw)
 146  	}
 147  }
 148  
 149  // TestClaimAgainstTheWrongChainIsDropped: a body that is a real block but not
 150  // this chain's at that height is refused. That is the check the split exists
 151  // for: the downloader asked a peer, and a peer can answer with anything.
 152  func TestClaimAgainstTheWrongChainIsDropped(t *testing.T) {
 153  	cfg, log, bodies := fixture(t, 3)
 154  	// A body from another chain: a header the store does not hold.
 155  	other := btcwire.BlockHeader{Version: 1, Nonce: 999, MerkleRoot: chainhash.Hash{9}}
 156  	if err := log.Append(1, other.BlockHash(), body(other)); err != nil {
 157  		t.Fatal(err)
 158  	}
 159  	res, err := process.Run(cfg)
 160  	if err != nil {
 161  		t.Fatal(err)
 162  	}
 163  	if res.Dropped != 1 || bodies.Len() != 0 {
 164  		t.Fatalf("%d dropped, %d stored", res.Dropped, bodies.Len())
 165  	}
 166  }
 167  
 168  // TestMissingHeaderIsDropped: a claim at a height the chain does not have is
 169  // dropped rather than stored.
 170  func TestMissingHeaderIsDropped(t *testing.T) {
 171  	cfg, log, _ := fixture(t, 2)
 172  	h := headerAt(2)
 173  	if err := log.Append(2, h.BlockHash(), body(h)); err != nil {
 174  		t.Fatal(err)
 175  	}
 176  	res, err := process.Run(cfg)
 177  	if err != nil {
 178  		t.Fatal(err)
 179  	}
 180  	if res.Dropped != 1 {
 181  		t.Fatalf("%d dropped", res.Dropped)
 182  	}
 183  	if !strings.Contains(string(mustRead(t, cfg.Missing)), "height") {
 184  		t.Logf("recorded: %s", mustRead(t, cfg.Missing))
 185  	}
 186  }
 187  
 188  func mustRead(t *testing.T, path string) string {
 189  	t.Helper()
 190  	raw, err := os.ReadFile(path)
 191  	if err != nil {
 192  		t.Fatalf("read %s: %v", path, err)
 193  	}
 194  	return string(raw)
 195  }
 196