realrange_test.go raw

   1  package utxo_test
   2  
   3  import (
   4  	"fmt"
   5  	"os"
   6  	"path/filepath"
   7  	"strings"
   8  	"testing"
   9  
  10  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  11  
  12  	"git.smesh.lol/btcd-lite/internal/utxo"
  13  	utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
  14  	"git.smesh.lol/btcd-lite/internal/wire"
  15  )
  16  
  17  // TestMergeMatchesWalkOnRealBlocks drives the merge over a range of real blocks
  18  // and requires the walk to agree with it entry by entry.
  19  //
  20  // The blocks are the node's own bytes for heights 80..105, of which 105 spends
  21  // an output of height 1. The walk and the merge are given the same base, one
  22  // that holds that output's entry, so both sides see a range with a spend of a
  23  // prior output in it - the case the synthetic diffs never had.
  24  func TestMergeMatchesWalkOnRealBlocks(t *testing.T) {
  25  	dir := t.TempDir()
  26  	// Height 1's coinbase: what height 105 spends.
  27  	funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710")
  28  	base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx}, Value: 50_0000_0000, Height: 1, Coinbase: true}}
  29  	if _, err := utxostore.WriteSet(dir, 0, base); err != nil {
  30  		t.Fatal(err)
  31  	}
  32  	if _, err := utxostore.Promote(dir, 0); err != nil {
  33  		t.Fatal(err)
  34  	}
  35  	set := append([]utxo.Entry(nil), base...)
  36  	var touches []utxo.Touch
  37  	for h := uint32(80); h <= 105; h++ {
  38  		raw, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("regtest-block-%d.hex", h)))
  39  		if err != nil {
  40  			t.Skipf("no captured block %d", h)
  41  		}
  42  		b, err := wire.ParseBlock(mustHex(t, strings.TrimSpace(string(raw))))
  43  		if err != nil {
  44  			t.Fatalf("block %d: %v", h, err)
  45  		}
  46  		lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
  47  		ts, err := utxo.BlockTouches(b, h, lookup)
  48  		if err != nil {
  49  			t.Fatalf("block %d touches: %v", h, err)
  50  		}
  51  		touches = append(touches, ts...)
  52  		set, err = utxo.ApplyRange(set, ts)
  53  		if err != nil {
  54  			t.Fatalf("block %d apply: %v", h, err)
  55  		}
  56  	}
  57  	if len(set) != len(base)+26*2 {
  58  		t.Logf("walk holds %d entries", len(set))
  59  	}
  60  	if _, err := utxostore.CutRange(dir, 0, 106, touches); err != nil {
  61  		t.Fatal(err)
  62  	}
  63  	loaded, err := utxostore.LoadDiff(dir, 0, 106)
  64  	if err != nil {
  65  		t.Fatal(err)
  66  	}
  67  	got, err := utxostore.MergeRange(dir, 0, 106, loaded, chainhash.Hash{})
  68  	if err != nil {
  69  		t.Fatalf("merge: %v", err)
  70  	}
  71  	want := utxostore.NameOf(set)
  72  	if got != want {
  73  		// written is the merge's own file, decoded and re-encoded so the
  74  		// comparison is of entries and not of bytes.
  75  		raw, err := os.ReadFile(utxostore.SetFile(106))
  76  		if err != nil {
  77  			raw, err = os.ReadFile(filepath.Join(dir, utxostore.SetFile(106)))
  78  		}
  79  		if err != nil {
  80  			t.Fatal(err)
  81  		}
  82  		written := make([]utxo.Entry, 0, len(raw)/utxostore.EntrySize)
  83  		buf := make([]byte, utxostore.EntrySize)
  84  		for i := 0; i+utxostore.EntrySize <= len(raw); i += utxostore.EntrySize {
  85  			copy(buf, raw[i:i+utxostore.EntrySize])
  86  			written = append(written, utxo.Decode(buf))
  87  		}
  88  		missed, extra := 0, 0
  89  		for _, w := range written {
  90  			if _, ok := utxo.Find(set, w.OutPoint); !ok {
  91  				extra++
  92  			}
  93  		}
  94  		for _, e := range set {
  95  			if _, ok := utxo.Find(written, e.OutPoint); !ok {
  96  				missed++
  97  			}
  98  		}
  99  		t.Logf("walk %d entries, written %d; %d entries only the walk has, %d only the merge has",
 100  			len(set), len(written), missed, extra)
 101  		for _, e := range set {
 102  			if _, ok := utxo.Find(written, e.OutPoint); !ok {
 103  				t.Logf("  only the walk: %s v=%d h=%d", e.OutPoint, e.Value, e.Height)
 104  			}
 105  		}
 106  		for _, w := range written {
 107  			if _, ok := utxo.Find(set, w.OutPoint); !ok {
 108  				t.Logf("  only the merge: %s v=%d h=%d", w.OutPoint, w.Value, w.Height)
 109  			}
 110  		}
 111  		for i := 0; i < len(set) && i < len(written); i++ {
 112  			if set[i] != written[i] {
 113  				lo, hi := i-2, i+3
 114  				if lo < 0 {
 115  					lo = 0
 116  				}
 117  				for j := lo; j < hi && j < len(set) && j < len(written); j++ {
 118  					t.Logf("  %d walk %s v=%d h=%d | written %s v=%d h=%d",
 119  						j, set[j].OutPoint, set[j].Value, set[j].Height,
 120  						written[j].OutPoint, written[j].Value, written[j].Height)
 121  				}
 122  				break
 123  			}
 124  		}
 125  		t.Fatalf("merge %s, walk %s", got, want)
 126  	}
 127  	t.Logf("26 real blocks with a spend: walk and merge agree on %d entries", len(set))
 128  }
 129  
 130  // TestDiffKeysAreTheWalksCreations breaks the disagreement down to the key
 131  // lists: which outpoints the diff says were created, which it says were spent,
 132  // and which the walk's set holds that neither list names.
 133  func TestDiffKeysAreTheWalksCreations(t *testing.T) {
 134  	dir := t.TempDir()
 135  	funderTx, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710")
 136  	base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funderTx}, Value: 50_0000_0000, Height: 1, Coinbase: true}}
 137  	if _, err := utxostore.WriteSet(dir, 0, base); err != nil {
 138  		t.Fatal(err)
 139  	}
 140  	if _, err := utxostore.Promote(dir, 0); err != nil {
 141  		t.Fatal(err)
 142  	}
 143  	set := append([]utxo.Entry(nil), base...)
 144  	var touches []utxo.Touch
 145  	for h := uint32(80); h <= 105; h++ {
 146  		raw, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("regtest-block-%d.hex", h)))
 147  		if err != nil {
 148  			t.Skip("no captured block")
 149  		}
 150  		b, err := wire.ParseBlock(mustHex(t, strings.TrimSpace(string(raw))))
 151  		if err != nil {
 152  			t.Fatal(err)
 153  		}
 154  		lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
 155  		ts, err := utxo.BlockTouches(b, h, lookup)
 156  		if err != nil {
 157  			t.Fatalf("block %d: %v", h, err)
 158  		}
 159  		touches = append(touches, ts...)
 160  		set, err = utxo.ApplyRange(set, ts)
 161  		if err != nil {
 162  			t.Fatalf("block %d apply: %v", h, err)
 163  		}
 164  	}
 165  	// What the diff calls a creation: a touch's outpoint that the walk holds
 166  	// and that has no spend of it in the range.
 167  	created := map[wire.OutPoint]bool{}
 168  	spent := map[wire.OutPoint]bool{}
 169  	for _, tc := range touches {
 170  		if tc.Spent {
 171  			spent[tc.OutPoint] = true
 172  		} else {
 173  			created[tc.OutPoint] = true
 174  		}
 175  	}
 176  	inWalk, notFromTouches := 0, 0
 177  	for _, e := range set {
 178  		if _, ok := utxo.Find(base, e.OutPoint); ok {
 179  			continue
 180  		}
 181  		inWalk++
 182  		if !created[e.OutPoint] {
 183  			notFromTouches++
 184  			if notFromTouches < 5 {
 185  				t.Logf("in the walk, not a touch's creation: %s v=%d h=%d", e.OutPoint, e.Value, e.Height)
 186  			}
 187  		}
 188  	}
 189  	t.Logf("walk holds %d new entries, %d of them not named by a creation touch; diff touches %d, creations %d, spends %d",
 190  		inWalk, notFromTouches, len(touches), len(created), len(spent))
 191  	if notFromTouches != 0 {
 192  		t.Fatalf("%d entries in the walk have no creation touch", notFromTouches)
 193  	}
 194  }
 195