merge_helpers_test.go raw

   1  package utxostore
   2  
   3  import (
   4  	"encoding/binary"
   5  	"os"
   6  	"path/filepath"
   7  	"testing"
   8  
   9  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  10  
  11  	"git.smesh.lol/btcd-lite/internal/utxo"
  12  	"git.smesh.lol/btcd-lite/internal/wire"
  13  )
  14  
  15  func mkOp(i uint32) wire.OutPoint {
  16  	var h [32]byte
  17  	binary.LittleEndian.PutUint32(h[:4], i)
  18  	binary.BigEndian.PutUint32(h[28:], i)
  19  	return wire.OutPoint{Hash: h}
  20  }
  21  
  22  // TestWriteKeysThenReadBack is the merge's plumbing on its own: the key list
  23  // and the value file must hand back, for each key, the entry that key created.
  24  // It is a unit test of the two cursors because the merge's failure was exactly
  25  // this pairing.
  26  func TestWriteKeysThenReadBack(t *testing.T) {
  27  	dir := t.TempDir()
  28  	var ts []utxo.Touch
  29  	for h := 0; h < 10; h++ {
  30  		for k := 0; k < 5; k++ {
  31  			id := uint32(h*5 + k)
  32  			ts = append(ts, utxo.Touch{OutPoint: mkOp(id), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)}})
  33  		}
  34  	}
  35  	d := utxo.Diff{From: 0, To: 10, Touches: ts}
  36  	createdPath, spentPath := dir+"/kc", dir+"/ks"
  37  	if err := writeKeys(createdPath, spentPath, d); err != nil {
  38  		t.Fatal(err)
  39  	}
  40  	k, err := openKeys(createdPath)
  41  	if err != nil {
  42  		t.Fatal(err)
  43  	}
  44  	defer k.close()
  45  	if err := k.openValues(createdPath + "v"); err != nil {
  46  		t.Fatal(err)
  47  	}
  48  	var keys []wire.OutPoint
  49  	var entries []utxo.Entry
  50  	for k.have {
  51  		keys = append(keys, k.cur)
  52  		e, ok := k.entry(k.i)
  53  		if !ok {
  54  			t.Fatalf("no entry for key %d", k.i)
  55  		}
  56  		entries = append(entries, e)
  57  		k.advance()
  58  	}
  59  	if len(keys) != 50 {
  60  		t.Fatalf("%d keys", len(keys))
  61  	}
  62  	for i := range keys {
  63  		if entries[i].OutPoint != keys[i] {
  64  			t.Fatalf("key %d is %x, its entry names %x", i, keys[i].Hash[:2], entries[i].OutPoint.Hash[:2])
  65  		}
  66  		// The key's id is where op put it: little endian in the first four
  67  		// bytes of the hash.
  68  		id := int64(binary.LittleEndian.Uint32(keys[i].Hash[:4]))
  69  		if entries[i].Value != id {
  70  			t.Fatalf("key %d has value %d", i, entries[i].Value)
  71  		}
  72  	}
  73  }
  74  
  75  // TestMergeCreationsOnly runs the merge over a base with no entries and a diff
  76  // of creations only, which is the case that exposed the value file being read
  77  // at the wrong index.
  78  func TestMergeCreationsOnly(t *testing.T) {
  79  	dir := t.TempDir()
  80  	if _, err := WriteSet(dir, 0, nil); err != nil {
  81  		t.Fatal(err)
  82  	}
  83  	if _, err := Promote(dir, 0); err != nil {
  84  		t.Fatal(err)
  85  	}
  86  	var ts []utxo.Touch
  87  	var want []utxo.Entry
  88  	for h := 0; h < 10; h++ {
  89  		var block []utxo.Touch
  90  		for k := 0; k < 5; k++ {
  91  			id := uint32(h*5 + k)
  92  			e := utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)}
  93  			block = append(block, utxo.Touch{OutPoint: e.OutPoint, Height: uint32(h), New: e})
  94  			want = append(want, e)
  95  		}
  96  		ts = append(ts, block...)
  97  	}
  98  	if _, err := CutRange(dir, 0, 10, ts); err != nil {
  99  		t.Fatal(err)
 100  	}
 101  	d, err := LoadDiff(dir, 0, 10)
 102  	if err != nil {
 103  		t.Fatal(err)
 104  	}
 105  	got, err := MergeRange(dir, 0, 10, d, NameOf(want))
 106  	if err != nil {
 107  		t.Fatal(err)
 108  	}
 109  	if got != NameOf(want) {
 110  		t.Fatalf("merge gives %s, the set is %s", got, NameOf(want))
 111  	}
 112  }
 113  
 114  // TestMergeStreamCreationsCount checks the merge's output length for the
 115  // creations-only case, so a name match cannot hide a wrong count.
 116  func TestMergeStreamCreationsCount(t *testing.T) {
 117  	dir := t.TempDir()
 118  	if _, err := WriteSet(dir, 0, nil); err != nil {
 119  		t.Fatal(err)
 120  	}
 121  	if _, err := Promote(dir, 0); err != nil {
 122  		t.Fatal(err)
 123  	}
 124  	var ts []utxo.Touch
 125  	for h := 0; h < 10; h++ {
 126  		for k := 0; k < 5; k++ {
 127  			id := uint32(h*5 + k)
 128  			ts = append(ts, utxo.Touch{OutPoint: mkOp(id), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)}})
 129  		}
 130  	}
 131  	if _, err := CutRange(dir, 0, 10, ts); err != nil {
 132  		t.Fatal(err)
 133  	}
 134  	d, _ := LoadDiff(dir, 0, 10)
 135  	if _, err := MergeRange(dir, 0, 10, d, chainhash.Hash{}); err != nil {
 136  		t.Fatal(err)
 137  	}
 138  	got, err := LoadReplay(dir, 0)
 139  	_ = got
 140  	if err != nil {
 141  		t.Fatal(err)
 142  	}
 143  	f, err := os.OpenFile(filepath.Join(dir, SetFile(10)), os.O_RDONLY, 0)
 144  	if err != nil {
 145  		t.Fatal(err)
 146  	}
 147  	fi, _ := f.Stat()
 148  	f.Close()
 149  	if want := int64(50 * EntrySize); fi.Size() != want {
 150  		t.Fatalf("merged file is %d bytes, want %d", fi.Size(), want)
 151  	}
 152  }
 153