twochunk_test.go raw

   1  package utxo_test
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   7  
   8  	"git.smesh.lol/btcd-lite/internal/utxo"
   9  	utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
  10  	"git.smesh.lol/btcd-lite/internal/wire"
  11  )
  12  
  13  // TestTwoRangesSpendAcrossTheBoundary is the builder's situation with no blocks
  14  // in it: a first range of creations written as a checkpoint, a second range
  15  // whose spends name entries the first range created, and the merge of the
  16  // second range compared with the walk over both.
  17  func TestTwoRangesSpendAcrossTheBoundary(t *testing.T) {
  18  	dir := t.TempDir()
  19  	if _, err := utxostore.WriteSet(dir, 0, nil); err != nil {
  20  		t.Fatal(err)
  21  	}
  22  	if _, err := utxostore.Promote(dir, 0); err != nil {
  23  		t.Fatal(err)
  24  	}
  25  	// Range one: five creations at heights 0..4.
  26  	var set []utxo.Entry
  27  	var first []utxo.Touch
  28  	for h := uint32(0); h < 5; h++ {
  29  		id := uint32(100 + h)
  30  		tc := utxo.Touch{OutPoint: opOf(id), Height: h, New: utxo.Entry{OutPoint: opOf(id), Value: int64(id), Height: h}}
  31  		first = append(first, tc)
  32  		var err error
  33  		set, err = utxo.ApplyRange(set, []utxo.Touch{tc})
  34  		if err != nil {
  35  			t.Fatal(err)
  36  		}
  37  	}
  38  	if _, err := utxostore.CutRange(dir, 0, 5, first); err != nil {
  39  		t.Fatal(err)
  40  	}
  41  	d1, _ := utxostore.LoadDiff(dir, 0, 5)
  42  	cp5, err := utxostore.MergeRange(dir, 0, 5, d1, utxostore.NameOf(set))
  43  	if err != nil {
  44  		t.Fatalf("checkpoint 5: %v", err)
  45  	}
  46  	t.Logf("checkpoint 5 is %s", cp5)
  47  	// Range two: spends one of them and creates five more.
  48  	var second []utxo.Touch
  49  	second = append(second, utxo.Touch{OutPoint: opOf(102), Height: 5, Spent: true})
  50  	for h := uint32(5); h < 10; h++ {
  51  		id := uint32(200 + h)
  52  		second = append(second, utxo.Touch{OutPoint: opOf(id), Height: h, New: utxo.Entry{OutPoint: opOf(id), Value: int64(id), Height: h}})
  53  	}
  54  	for _, tc := range second {
  55  		var err error
  56  		set, err = utxo.ApplyRange(set, []utxo.Touch{tc})
  57  		if err != nil {
  58  			t.Fatalf("walk at %d: %v", tc.Height, err)
  59  		}
  60  	}
  61  	if _, err := utxostore.CutRange(dir, 5, 10, second); err != nil {
  62  		t.Fatal(err)
  63  	}
  64  	d2, _ := utxostore.LoadDiff(dir, 5, 10)
  65  	cp10, err := utxostore.MergeRange(dir, 5, 10, d2, utxostore.NameOf(set))
  66  	if err != nil {
  67  		t.Fatalf("checkpoint 10: %v", err)
  68  	}
  69  	t.Logf("checkpoint 10 is %s, entries %d", cp10, len(set))
  70  	_ = chainhash.Hash{}
  71  	_ = wire.OutPoint{}
  72  }
  73