staging_test.go raw

   1  package staging_test
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"testing"
   7  
   8  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   9  
  10  	"git.smesh.lol/btcd-lite/internal/staging"
  11  )
  12  
  13  // appendN writes n bodies of 100+i bytes at heights 0..n-1.
  14  func appendN(t *testing.T, log *staging.Log, n int) []chainhash.Hash {
  15  	t.Helper()
  16  	hashes := make([]chainhash.Hash, n)
  17  	for i := 0; i < n; i++ {
  18  		h := chainhash.Hash{byte(i), byte(i >> 8)}
  19  		hashes[i] = h
  20  		if err := log.Append(uint32(i), h, make([]byte, 100+i)); err != nil {
  21  			t.Fatal(err)
  22  		}
  23  	}
  24  	return hashes
  25  }
  26  
  27  // TestAppendAndWalk: what is appended is what comes back, with its claim.
  28  func TestAppendAndWalk(t *testing.T) {
  29  	dir := t.TempDir()
  30  	log, err := staging.Open(dir)
  31  	if err != nil {
  32  		t.Fatal(err)
  33  	}
  34  	hashes := appendN(t, log, 3)
  35  	if log.Len() != 3 {
  36  		t.Fatalf("%d claims", log.Len())
  37  	}
  38  	if got, want := log.Bytes(), int64(100+101+102); got != want {
  39  		t.Fatalf("%d bytes, want %d", got, want)
  40  	}
  41  	if !log.Has(1) || log.Has(3) {
  42  		t.Fatal("Has answers the wrong heights")
  43  	}
  44  	if err := log.Close(); err != nil {
  45  		t.Fatal(err)
  46  	}
  47  
  48  	// A reopen finds the same claims and the same bodies.
  49  	log2, err := staging.Open(dir)
  50  	if err != nil {
  51  		t.Fatal(err)
  52  	}
  53  	defer log2.Close()
  54  	if log2.Len() != 3 {
  55  		t.Fatalf("after a reopen: %d claims", log2.Len())
  56  	}
  57  	seen := 0
  58  	if err := log2.Walk(func(c staging.Claim, body []byte) error {
  59  		if c.Hash != hashes[seen] {
  60  			t.Fatalf("claim %d names %s, not %s", seen, c.Hash, hashes[seen])
  61  		}
  62  		if len(body) != 100+seen {
  63  			t.Fatalf("body %d is %d bytes", seen, len(body))
  64  		}
  65  		seen++
  66  		return nil
  67  	}); err != nil {
  68  		t.Fatal(err)
  69  	}
  70  	if seen != 3 {
  71  		t.Fatalf("walked %d bodies", seen)
  72  	}
  73  }
  74  
  75  // TestTornClaimIsCut: a claim recorded without its body, which is what a crash
  76  // between the two writes leaves, is dropped and the log reopens one shorter.
  77  func TestTornClaimIsCut(t *testing.T) {
  78  	dir := t.TempDir()
  79  	log, err := staging.Open(dir)
  80  	if err != nil {
  81  		t.Fatal(err)
  82  	}
  83  	appendN(t, log, 2)
  84  	if err := log.Close(); err != nil {
  85  		t.Fatal(err)
  86  	}
  87  	// Half a claim, as a crash mid-write leaves it.
  88  	claims := filepath.Join(dir, "claims.dat")
  89  	raw, err := os.ReadFile(claims)
  90  	if err != nil {
  91  		t.Fatal(err)
  92  	}
  93  	if err := os.WriteFile(claims, append(raw, make([]byte, 20)...), 0o644); err != nil {
  94  		t.Fatal(err)
  95  	}
  96  	log2, err := staging.Open(dir)
  97  	if err != nil {
  98  		t.Fatalf("a torn claim stopped the open: %v", err)
  99  	}
 100  	defer log2.Close()
 101  	if log2.Len() != 2 {
 102  		t.Fatalf("%d claims after a torn append", log2.Len())
 103  	}
 104  	fi, err := os.Stat(claims)
 105  	if err != nil {
 106  		t.Fatal(err)
 107  	}
 108  	if fi.Size() != 2*44 {
 109  		t.Fatalf("the claim file is %d bytes, not two claims", fi.Size())
 110  	}
 111  	// And the next append lands on the end of the good one.
 112  	if err := log2.Append(2, chainhash.Hash{2}, make([]byte, 10)); err != nil {
 113  		t.Fatal(err)
 114  	}
 115  	if log2.Len() != 3 {
 116  		t.Fatalf("%d claims after appending over the cut", log2.Len())
 117  	}
 118  }
 119  
 120  // TestTornBodyIsCut: a claim whose body never finished is dropped, the segment
 121  // is cut back, and the body before it survives.
 122  func TestTornBodyIsCut(t *testing.T) {
 123  	dir := t.TempDir()
 124  	log, err := staging.Open(dir)
 125  	if err != nil {
 126  		t.Fatal(err)
 127  	}
 128  	appendN(t, log, 2)
 129  	if err := log.Close(); err != nil {
 130  		t.Fatal(err)
 131  	}
 132  	// Cut the second body in half: the claim is there, the bytes are not.
 133  	seg := filepath.Join(dir, "00000000.dat")
 134  	fi, err := os.Stat(seg)
 135  	if err != nil {
 136  		t.Fatal(err)
 137  	}
 138  	if err := os.Truncate(seg, fi.Size()-50); err != nil {
 139  		t.Fatal(err)
 140  	}
 141  	log2, err := staging.Open(dir)
 142  	if err != nil {
 143  		t.Fatal(err)
 144  	}
 145  	defer log2.Close()
 146  	if log2.Len() != 1 {
 147  		t.Fatalf("%d claims after a torn body", log2.Len())
 148  	}
 149  	if log2.Has(1) {
 150  		t.Fatal("the height whose body was torn is still claimed")
 151  	}
 152  	if fi, err := os.Stat(seg); err != nil || fi.Size() != 4+100 {
 153  		t.Fatalf("the segment is %v bytes, not one body", fi.Size())
 154  	}
 155  }
 156  
 157  // TestDrainEmpties: a drained log reopens empty, with no segment files left.
 158  func TestDrainEmpties(t *testing.T) {
 159  	dir := t.TempDir()
 160  	log, err := staging.Open(dir)
 161  	if err != nil {
 162  		t.Fatal(err)
 163  	}
 164  	appendN(t, log, 2)
 165  	if err := log.Drain(); err != nil {
 166  		t.Fatal(err)
 167  	}
 168  	if log.Len() != 0 || log.Bytes() != 0 {
 169  		t.Fatalf("after a drain: %d claims, %d bytes", log.Len(), log.Bytes())
 170  	}
 171  	entries, err := os.ReadDir(dir)
 172  	if err != nil {
 173  		t.Fatal(err)
 174  	}
 175  	if len(entries) != 0 {
 176  		t.Fatalf("the drained directory holds %d files", len(entries))
 177  	}
 178  	if err := log.Close(); err != nil {
 179  		t.Fatal(err)
 180  	}
 181  	log2, err := staging.Open(dir)
 182  	if err != nil {
 183  		t.Fatal(err)
 184  	}
 185  	defer log2.Close()
 186  	if log2.Len() != 0 {
 187  		t.Fatalf("a drained log reopened with %d claims", log2.Len())
 188  	}
 189  }
 190