dbg3_test.go raw

   1  package utxostore
   2  
   3  import (
   4  	"encoding/binary"
   5  	"fmt"
   6  	"os"
   7  	"path/filepath"
   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  func e3(i uint32) wire.OutPoint {
  17  	var h [32]byte
  18  	binary.LittleEndian.PutUint32(h[:4], i)
  19  	binary.BigEndian.PutUint32(h[28:], i)
  20  	return wire.OutPoint{Hash: h}
  21  }
  22  
  23  func TestDbg3(t *testing.T) {
  24  	var base []utxo.Entry
  25  	for i := uint32(0); i < 7; i++ {
  26  		e := utxo.Entry{OutPoint: e3(i * 10), Value: 5_000_000_000, Height: i % 4, Coinbase: true}
  27  		if i%2 == 1 {
  28  			e.Value = 0
  29  		}
  30  		base = append(base, e)
  31  	}
  32  	var ts []utxo.Touch
  33  	for i := uint32(0); i < 8; i++ {
  34  		id := 5 + i*10
  35  		e := utxo.Entry{OutPoint: e3(id), Value: 5_000_000_000, Height: 4 + i%4, Coinbase: true}
  36  		if i%2 == 1 {
  37  			e.Value = 0
  38  		}
  39  		ts = append(ts, utxo.Touch{OutPoint: e3(id), Height: 4, New: e})
  40  	}
  41  	want, _ := utxo.ApplyRange(base, ts)
  42  	dir := t.TempDir()
  43  	_, _ = WriteSet(dir, 0, base)
  44  	_, _ = Promote(dir, 0)
  45  	_, _ = CutRange(dir, 0, 1, ts)
  46  	d, _ := LoadDiff(dir, 0, 1)
  47  	keepScratch = true
  48  	got, err := MergeRange(dir, 0, 1, d, chainhash.Hash{})
  49  	keepScratch = false
  50  	raw, rerr := os.ReadFile(filepath.Join(dir, SetFile(1)))
  51  	t.Logf("merge %s walk %s err %v fileReadErr %v", got, NameOf(want), err, rerr)
  52  	buf := make([]byte, EntrySize)
  53  	var gotSet []utxo.Entry
  54  	for i := 0; i+EntrySize <= len(raw); i += EntrySize {
  55  		copy(buf, raw[i:i+EntrySize])
  56  		gotSet = append(gotSet, utxo.Decode(buf))
  57  	}
  58  	t.Logf("walk %d, merge %d", len(want), len(gotSet))
  59  	for i := 0; i < len(want) || i < len(gotSet); i++ {
  60  		var w, g string
  61  		if i < len(want) {
  62  			w = fmt.Sprintf("idx=%02d v=%d h=%d", want[i].OutPoint.Index, want[i].Value, want[i].Height)
  63  		}
  64  		if i < len(gotSet) {
  65  			g = fmt.Sprintf("idx=%02d v=%d h=%d", gotSet[i].OutPoint.Index, gotSet[i].Value, gotSet[i].Height)
  66  		}
  67  		if w != g {
  68  			t.Logf("  DIFF %d walk [%s] merge [%s]", i, w, g)
  69  		}
  70  	}
  71  }
  72