streambuild_test.go raw

   1  package gate
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"testing"
   7  
   8  	"git.smesh.lol/btcd-lite/internal/chains"
   9  	"git.smesh.lol/btcd-lite/internal/utxo"
  10  	"git.smesh.lol/btcd-lite/internal/utxo/store"
  11  	"git.smesh.lol/btcd-lite/internal/utxobuild"
  12  	"git.smesh.lol/btcd-lite/internal/wire"
  13  )
  14  
  15  // TestStreamedWalkMatchesTheReplay: the walk that never holds the set must
  16  // produce the same dataset as the walk that does, down to the name each
  17  // snapshot gets. Two builds of 301 blocks, one in memory and one epoch at a
  18  // time, and every snapshot file and every per-block diff has to be identical.
  19  func TestStreamedWalkMatchesTheReplay(t *testing.T) {
  20  	r := nodeOn(t)
  21  	r.createWallet(t)
  22  	r.mine(t, 150)
  23  	to := r.must(t, "getnewaddress")
  24  	if _, err := r.cli("sendtoaddress", to, "1"); err != nil {
  25  		t.Fatalf("sendtoaddress: %v", err)
  26  	}
  27  	r.mine(t, 150)
  28  	tip := r.height(t)
  29  	src := &mapSource{bodies: rpcBodies(t, r, tip)}
  30  	interval := chains.Regtest().Exported.Retarget
  31  
  32  	replay := t.TempDir()
  33  	res, err := utxobuild.Build(utxobuild.Config{
  34  		Dir: filepath.Join(replay, "utxo"), DiffDir: filepath.Join(replay, "diff"),
  35  		Chain: "btc", Interval: interval, From: 0, To: uint32(tip) + 1,
  36  		Blocks: src, Logf: t.Logf,
  37  	})
  38  	if err != nil {
  39  		t.Fatalf("replay: %v", err)
  40  	}
  41  	streamed := t.TempDir()
  42  	sres, err := utxobuild.BuildStream(utxobuild.Config{
  43  		Dir: filepath.Join(streamed, "utxo"), DiffDir: filepath.Join(streamed, "diff"),
  44  		Chain: "btc", Interval: interval, From: 0, To: uint32(tip) + 1,
  45  		Blocks: src, Logf: t.Logf,
  46  	})
  47  	if err != nil {
  48  		t.Fatalf("streamed: %v", err)
  49  	}
  50  	// Diagnose the first snapshot's entries, entry by entry.
  51  	a144, err := utxostore.LoadSet(filepath.Join(replay, "utxo"), 144)
  52  	if err != nil {
  53  		t.Fatalf("replay set 144: %v", err)
  54  	}
  55  	b144, err := utxostore.LoadSet(filepath.Join(streamed, "utxo"), 144)
  56  	if err != nil {
  57  		t.Fatalf("streamed set 144: %v", err)
  58  	}
  59  	if len(a144) != len(b144) {
  60  		inA := map[wire.OutPoint]bool{}
  61  		for _, e := range a144 {
  62  			inA[e.OutPoint] = true
  63  		}
  64  		inB := map[wire.OutPoint]bool{}
  65  		for _, e := range b144 {
  66  			inB[e.OutPoint] = true
  67  		}
  68  		n := 0
  69  		for op := range inA {
  70  			if !inB[op] {
  71  				t.Logf("only in replay: %s height %d", op, opHeight(a144, op))
  72  				if n++; n == 4 {
  73  					break
  74  				}
  75  			}
  76  		}
  77  		for op := range inB {
  78  			if !inA[op] {
  79  				t.Logf("only in streamed: %s", op)
  80  			}
  81  		}
  82  		t.Fatalf("set 144: replay %d entries, streamed %d", len(a144), len(b144))
  83  	}
  84  	for i := range a144 {
  85  		if a144[i] != b144[i] {
  86  			t.Fatalf("set 144 entry %d: replay %+v, streamed %+v", i, a144[i], b144[i])
  87  		}
  88  	}
  89  	if res.Name != sres.Name || res.Height != sres.Height {
  90  		t.Fatalf("the walks end differently: %s at %d, %s at %d", res.Name, res.Height, sres.Name, sres.Height)
  91  	}
  92  	// Every snapshot file, by height, byte for byte.
  93  	for h := uint32(0); h <= uint32(tip); h += interval {
  94  		a := utxostore.SetFile(h)
  95  		ab, err := os.ReadFile(filepath.Join(replay, "utxo", a))
  96  		if err != nil {
  97  			t.Fatalf("replay snapshot %d: %v", h, err)
  98  		}
  99  		bb, err := os.ReadFile(filepath.Join(streamed, "utxo", a))
 100  		if err != nil {
 101  			t.Fatalf("streamed snapshot %d: %v", h, err)
 102  		}
 103  		if string(ab) != string(bb) {
 104  			t.Fatalf("snapshot %d differs between the walks", h)
 105  		}
 106  	}
 107  	// Every per-block diff, by range.
 108  	diffs, err := filepath.Glob(filepath.Join(replay, "diff", "diff-*.dat"))
 109  	if err != nil || len(diffs) == 0 {
 110  		t.Fatalf("no diffs to compare: %v", err)
 111  	}
 112  	for _, path := range diffs {
 113  		name := filepath.Base(path)
 114  		a, err := os.ReadFile(path)
 115  		if err != nil {
 116  			t.Fatal(err)
 117  		}
 118  		b, err := os.ReadFile(filepath.Join(streamed, "diff", name))
 119  		if err != nil {
 120  			t.Fatalf("streamed diff %s: %v", name, err)
 121  		}
 122  		if string(a) != string(b) {
 123  			t.Fatalf("diff %s differs between the walks", name)
 124  		}
 125  	}
 126  	t.Logf("%d snapshots and %d diffs identical between the in-memory walk and the streamed one, ending at %s",
 127  		int(uint32(tip)/interval)+1, len(diffs), res.Name)
 128  }
 129  
 130  // opHeight is the height of the entry for an outpoint, for a diagnostic line.
 131  func opHeight(set []utxo.Entry, op wire.OutPoint) uint32 {
 132  	for _, e := range set {
 133  		if e.OutPoint == op {
 134  			return e.Height
 135  		}
 136  	}
 137  	return 0
 138  }
 139