blockstore_repair_test.go raw

   1  package store_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/store"
  11  )
  12  
  13  // dataFile returns the path of the store's only data file.
  14  func dataFile(t *testing.T, dir string) string {
  15  	t.Helper()
  16  	matches, err := filepath.Glob(filepath.Join(dir, "*.blk"))
  17  	if err != nil || len(matches) != 1 {
  18  		t.Fatalf("data files: %v %v", matches, err)
  19  	}
  20  	return matches[0]
  21  }
  22  
  23  // TestTornWriteIsDropped appends bytes past the last good block, the shape a
  24  // crash mid-append leaves, and requires the store to open with every good
  25  // block and the next append at the last good block's end.
  26  func TestTornWriteIsDropped(t *testing.T) {
  27  	dir := t.TempDir()
  28  	vs := blocks(t)
  29  	hashOf := func(raw []byte) chainhash.Hash { return chainhash.DoubleHashH(raw[:80]) }
  30  
  31  	s, err := store.NewBlockStore(dir)
  32  	if err != nil {
  33  		t.Fatal(err)
  34  	}
  35  	for _, raw := range vs[:3] {
  36  		if err := s.PutBlock(hashOf(raw), raw); err != nil {
  37  			t.Fatal(err)
  38  		}
  39  	}
  40  	if err := s.Close(); err != nil {
  41  		t.Fatal(err)
  42  	}
  43  	goodSize, err := os.Stat(dataFile(t, dir))
  44  	if err != nil {
  45  		t.Fatal(err)
  46  	}
  47  	f, err := os.OpenFile(dataFile(t, dir), os.O_WRONLY|os.O_APPEND, 0o644)
  48  	if err != nil {
  49  		t.Fatal(err)
  50  	}
  51  	// Half a record header and a few bytes of a block.
  52  	if _, err := f.Write([]byte{1, 2, 3, 4, 5}); err != nil {
  53  		t.Fatal(err)
  54  	}
  55  	f.Close()
  56  
  57  	s, err = store.NewBlockStore(dir)
  58  	if err != nil {
  59  		t.Fatalf("open after torn write: %v", err)
  60  	}
  61  	if s.Len() != 3 {
  62  		t.Fatalf("blocks after torn write: %d, want 3", s.Len())
  63  	}
  64  	if fi, err := os.Stat(dataFile(t, dir)); err != nil || fi.Size() != goodSize.Size() {
  65  		t.Fatalf("data file size %v (err %v), want %d", fi.Size(), err, goodSize.Size())
  66  	}
  67  	// The append that follows goes to the right place and survives reopen.
  68  	if err := s.PutBlock(hashOf(vs[3]), vs[3]); err != nil {
  69  		t.Fatalf("append after repair: %v", err)
  70  	}
  71  	if err := s.Close(); err != nil {
  72  		t.Fatal(err)
  73  	}
  74  	s, err = store.NewBlockStore(dir)
  75  	if err != nil {
  76  		t.Fatal(err)
  77  	}
  78  	if s.Len() != 4 {
  79  		t.Fatalf("blocks after post-repair reopen: %d, want 4", s.Len())
  80  	}
  81  	if got, err := s.GetBlock(hashOf(vs[3])); err != nil || string(got) != string(vs[3]) {
  82  		t.Fatalf("post-repair block read: %v", err)
  83  	}
  84  }
  85  
  86  // TestCorruptBlockRefused requires a record whose bytes do not commit to its
  87  // hash to stop the store rather than be served.
  88  func TestCorruptBlockRefused(t *testing.T) {
  89  	dir := t.TempDir()
  90  	vs := blocks(t)
  91  	s, err := store.NewBlockStore(dir)
  92  	if err != nil {
  93  		t.Fatal(err)
  94  	}
  95  	if err := s.PutBlock(chainhash.DoubleHashH(vs[0][:80]), vs[0]); err != nil {
  96  		t.Fatal(err)
  97  	}
  98  	s.Close()
  99  	// Flip one byte of the stored block body.
 100  	f, err := os.OpenFile(dataFile(t, dir), os.O_WRONLY, 0o644)
 101  	if err != nil {
 102  		t.Fatal(err)
 103  	}
 104  	if _, err := f.WriteAt([]byte{0xff}, 8+10); err != nil {
 105  		t.Fatal(err)
 106  	}
 107  	f.Close()
 108  	if _, err := store.NewBlockStore(dir); err == nil {
 109  		t.Fatal("a store with a block that does not commit to its hash opened")
 110  	}
 111  }
 112