trace_test.go raw

   1  package utxobuild_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/utxo"
  11  	utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
  12  	"git.smesh.lol/btcd-lite/internal/utxobuild"
  13  	"git.smesh.lol/btcd-lite/internal/wire"
  14  )
  15  
  16  // TestRangeTwoTrace builds to checkpoint 10, then merges 10..20 by hand with the
  17  // walk in the same process, printing the first differing entry and the key
  18  // lists the merge read.
  19  func TestRangeTwoTrace(t *testing.T) {
  20  	raw, spends := chainOfBodies(22)
  21  	src := bodies{}
  22  	for h := range raw {
  23  		src[uint32(h)] = raw[h]
  24  	}
  25  	dir := t.TempDir()
  26  	if _, err := utxobuild.Build(utxobuild.Config{Dir: dir, Interval: 10, From: 0, To: 20, Blocks: src, Logf: t.Logf}); err != nil {
  27  		t.Fatalf("build: %v", err)
  28  	}
  29  	// The walk from the checkpoint's own file.
  30  	base, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(10)))
  31  	if err != nil {
  32  		t.Fatal(err)
  33  	}
  34  	set := make([]utxo.Entry, 0, len(base)/utxostore.EntrySize)
  35  	buf := make([]byte, utxostore.EntrySize)
  36  	for i := 0; i+utxostore.EntrySize <= len(base); i += utxostore.EntrySize {
  37  		copy(buf, base[i:i+utxostore.EntrySize])
  38  		set = append(set, utxo.Decode(buf))
  39  	}
  40  	t.Logf("base at 10: %d entries", len(set))
  41  	var touches []utxo.Touch
  42  	// The file at 10 is the set after block 10, so the walk continues at 11.
  43  	for h := uint32(11); h <= 20; h++ {
  44  		b, err := wire.ParseBlock(raw[h])
  45  		if err != nil {
  46  			t.Fatal(err)
  47  		}
  48  		lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
  49  		ts, err := utxo.BlockTouches(b, h, lookup)
  50  		if err != nil {
  51  			t.Fatalf("body %d: %v", h, err)
  52  		}
  53  		for _, tc := range ts {
  54  			if tc.Spent {
  55  				t.Logf("  body %d spends %s (fixture says %s)", h, tc.OutPoint, spends[h])
  56  			}
  57  		}
  58  		touches = append(touches, ts...)
  59  		set, err = utxo.ApplyRange(set, ts)
  60  		if err != nil {
  61  			t.Fatalf("body %d apply: %v", h, err)
  62  		}
  63  	}
  64  	t.Logf("walk 10..20: %d entries %s", len(set), utxostore.NameOf(set))
  65  	if _, err := utxostore.CutRange(dir, 10, 20, touches); err != nil {
  66  		t.Fatal(err)
  67  	}
  68  	d, err := utxostore.LoadDiff(dir, 10, 20)
  69  	if err != nil {
  70  		t.Fatal(err)
  71  	}
  72  	got, err := utxostore.MergeRange(dir, 10, 20, d, chainhash.Hash{})
  73  	t.Logf("merge 10..20: %s err %v", got, err)
  74  	if got != utxostore.NameOf(set) {
  75  		file, err := os.ReadFile(filepath.Join(dir, utxostore.SetFile(20)))
  76  		if err != nil {
  77  			t.Fatal(err)
  78  		}
  79  		written := make([]utxo.Entry, 0, len(file)/utxostore.EntrySize)
  80  		for i := 0; i+utxostore.EntrySize <= len(file); i += utxostore.EntrySize {
  81  			copy(buf, file[i:i+utxostore.EntrySize])
  82  			written = append(written, utxo.Decode(buf))
  83  		}
  84  		t.Logf("walk %d entries, merge wrote %d", len(set), len(written))
  85  		have := map[wire.OutPoint]bool{}
  86  		for _, e := range written {
  87  			have[e.OutPoint] = true
  88  		}
  89  		for _, e := range set {
  90  			if !have[e.OutPoint] {
  91  				t.Logf("  not written: %s v=%d h=%d", e.OutPoint, e.Value, e.Height)
  92  			}
  93  		}
  94  		for i := 0; i < len(set) && i < len(written); i++ {
  95  			if set[i] != written[i] {
  96  				t.Logf("  first order difference at %d: walk %s, merge %s", i, set[i].OutPoint, written[i].OutPoint)
  97  				break
  98  			}
  99  		}
 100  	}
 101  }
 102