chunk_test.go raw

   1  package chunk_test
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/binary"
   6  	"testing"
   7  
   8  	"git.smesh.lol/btcd-lite/internal/chunk"
   9  )
  10  
  11  func collect(t *testing.T) (chunk.Recorder, *[]chunk.Chunk) {
  12  	t.Helper()
  13  	var got []chunk.Chunk
  14  	return func(c chunk.Chunk) error { got = append(got, c); return nil }, &got
  15  }
  16  
  17  // TestCutHeadersDeterministic: the same header stream cut twice gives the same
  18  // boundaries and the same names, which is what makes two instances agree.
  19  func TestCutHeadersDeterministic(t *testing.T) {
  20  	raw := make([]byte, 25*80)
  21  	for i := range raw {
  22  		raw[i] = byte(i * 7)
  23  	}
  24  	rec1, a := collect(t)
  25  	n1, err := chunk.CutHeaders("btc", raw, 80, 0, 10, rec1)
  26  	if err != nil || n1 != 3 {
  27  		t.Fatalf("cut: %d %v", n1, err)
  28  	}
  29  	rec2, b := collect(t)
  30  	if _, err := chunk.CutHeaders("btc", raw, 80, 0, 10, rec2); err != nil {
  31  		t.Fatal(err)
  32  	}
  33  	if len(*a) != len(*b) {
  34  		t.Fatalf("%d and %d chunks", len(*a), len(*b))
  35  	}
  36  	for i := range *a {
  37  		if (*a)[i].Name() != (*b)[i].Name() {
  38  			t.Fatalf("chunk %d has two names", i)
  39  		}
  40  	}
  41  	// The chunks tile the range with no gap and no repeat.
  42  	next := uint32(0)
  43  	for _, c := range *a {
  44  		if c.From != next {
  45  			t.Fatalf("run jumps from %d to %d", next, c.From)
  46  		}
  47  		next = c.To
  48  	}
  49  	if next != 25 {
  50  		t.Fatalf("run ends at %d", next)
  51  	}
  52  }
  53  
  54  // TestCutFiltersBudget: the byte budget, not the count, decides a cut, so a
  55  // busy block does not overflow a chunk and a quiet one does not waste one.
  56  func TestCutFiltersBudget(t *testing.T) {
  57  	big := chunk.PackLengthPrefixed([][]byte{make([]byte, 400), make([]byte, 400), make([]byte, 400), make([]byte, 10)})
  58  	rec, got := collect(t)
  59  	if _, err := chunk.CutLengthPrefixed("btc", chunk.Filters, big, 100, 1000, 1000, rec); err != nil {
  60  		t.Fatal(err)
  61  	}
  62  	// Each 400-byte record is 404 on the wire and the budget is 1000, so two
  63  	// fit a chunk and the third starts the next one.
  64  	if len(*got) != 2 {
  65  		t.Fatalf("%d chunks, want 2", len(*got))
  66  	}
  67  	if (*got)[0].From != 100 || (*got)[0].To != 102 || (*got)[1].From != 102 || (*got)[1].To != 104 {
  68  		t.Fatalf("boundaries %d-%d, %d-%d", (*got)[0].From, (*got)[0].To, (*got)[1].From, (*got)[1].To)
  69  	}
  70  	// A count cap cuts too, and a record bigger than the budget is a whole
  71  	// chunk rather than an error.
  72  	rec2, got2 := collect(t)
  73  	one := chunk.PackLengthPrefixed([][]byte{make([]byte, 400)})
  74  	if _, err := chunk.CutLengthPrefixed("btc", chunk.Filters, one, 0, 10, 1000, rec2); err != nil || len(*got2) != 1 {
  75  		t.Fatalf("oversized record: %d %v", len(*got2), err)
  76  	}
  77  }
  78  
  79  // TestPackRoundTrip: pack then read back gives the same records.
  80  func TestPackRoundTrip(t *testing.T) {
  81  	recs := [][]byte{{1, 2, 3}, {}, make([]byte, 300)}
  82  	raw := chunk.PackLengthPrefixed(recs)
  83  	at := 0
  84  	for _, want := range recs {
  85  		n := int(binary.LittleEndian.Uint32(raw[at : at+4]))
  86  		got := raw[at+4 : at+4+n]
  87  		if !bytes.Equal(got, want) {
  88  			t.Fatalf("record differs")
  89  		}
  90  		at += 4 + n
  91  	}
  92  }
  93