keyorder_test.go raw

   1  package utxostore
   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  	"git.smesh.lol/btcd-lite/internal/wire"
  14  )
  15  
  16  // TestKeyFileOrder compares the order the diff's creations sort into in memory
  17  // with the order writeKeys puts them on disk, over the captured blocks. A
  18  // difference there is the merge reading keys in one order and walking in
  19  // another.
  20  func TestKeyFileOrder(t *testing.T) {
  21  	dir := t.TempDir()
  22  	funder, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710")
  23  	base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funder}, Value: 50_0000_0000, Height: 1, Coinbase: true}}
  24  	if _, err := WriteSet(dir, 0, base); err != nil {
  25  		t.Fatal(err)
  26  	}
  27  	if _, err := Promote(dir, 0); err != nil {
  28  		t.Fatal(err)
  29  	}
  30  	var touches []utxo.Touch
  31  	set := append([]utxo.Entry(nil), base...)
  32  	for h := uint32(80); h <= 105; h++ {
  33  		raw, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("regtest-block-%d.hex", h)))
  34  		if err != nil {
  35  			t.Skip("no blocks")
  36  		}
  37  		b, err := wire.ParseBlock(mustDecode(t, strings.TrimSpace(string(raw))))
  38  		if err != nil {
  39  			t.Fatal(err)
  40  		}
  41  		lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) })
  42  		ts, err := utxo.BlockTouches(b, h, lookup)
  43  		if err != nil {
  44  			t.Fatal(err)
  45  		}
  46  		touches = append(touches, ts...)
  47  		set, err = utxo.ApplyRange(set, ts)
  48  		if err != nil {
  49  			t.Fatal(err)
  50  		}
  51  	}
  52  	diff := utxo.Diff{From: 0, To: 106, Touches: touches}
  53  	keepScratch = true
  54  	defer func() { keepScratch = false }()
  55  	if _, err := MergeRange(dir, 0, 106, diff, chainhash.Hash{}); err != nil {
  56  		t.Fatalf("merge: %v", err)
  57  	}
  58  	// In memory: every creation, sorted by the package's own comparison.
  59  	var inMemory []wire.OutPoint
  60  	for _, tc := range touches {
  61  		if !tc.Spent {
  62  			inMemory = append(inMemory, tc.OutPoint)
  63  		}
  64  	}
  65  	// The set's comparison, which is the one both sides must agree on.
  66  	sortBySet(inMemory)
  67  	// On disk: the key file the merge reads.
  68  	raw, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("keys-%09d-%09d.created", diff.From, diff.To)))
  69  	if err != nil {
  70  		t.Skip("the merge removed its scratch files; the order is compared on a copy instead")
  71  	}
  72  	onDisk := make([]wire.OutPoint, 0, len(raw)/36)
  73  	onDisk = readKeys(raw)
  74  	t.Logf("%d creations in memory, %d on disk", len(inMemory), len(onDisk))
  75  	for i := 0; i < len(inMemory) && i < len(onDisk); i++ {
  76  		if cmpOp(inMemory[i], onDisk[i]) != 0 {
  77  			for j := i; j < i+3 && j < len(inMemory) && j < len(onDisk); j++ {
  78  				// Printed from the end of the hash, which is where the two
  79  				// outpoints above differ.
  80  				ia, ib := reverse(inMemory[j].Hash), reverse(onDisk[j].Hash)
  81  				t.Logf("  %d memory %x:%d | disk %x:%d", j, ia[:3], inMemory[j].Index, ib[:3], onDisk[j].Index)
  82  			}
  83  			t.Fatalf("the key file's order differs from the sort at %d", i)
  84  		}
  85  	}
  86  	if len(inMemory) != len(onDisk) {
  87  		t.Fatalf("%d in memory, %d on disk", len(inMemory), len(onDisk))
  88  	}
  89  }
  90  
  91  func readKeys(raw []byte) []wire.OutPoint {
  92  	out := make([]wire.OutPoint, 0, len(raw)/36)
  93  	for i := 0; i+36 <= len(raw); i += 36 {
  94  		var op wire.OutPoint
  95  		copy(op.Hash[:], raw[i:i+32])
  96  		op.Index = uint32(raw[i+32]) | uint32(raw[i+33])<<8 | uint32(raw[i+34])<<16 | uint32(raw[i+35])<<24
  97  		out = append(out, op)
  98  	}
  99  	return out
 100  }
 101