stream_test.go raw

   1  package utxostore
   2  
   3  import (
   4  	"encoding/binary"
   5  	"testing"
   6  
   7  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   8  
   9  	"git.smesh.lol/btcd-lite/internal/utxo"
  10  	"git.smesh.lol/btcd-lite/internal/wire"
  11  )
  12  
  13  // testOp is an outpoint whose key is its id in the first four bytes, which is
  14  // what a test needs to tell one key from another in a failure message.
  15  func testOp(i uint32) wire.OutPoint {
  16  	var h [32]byte
  17  	binary.LittleEndian.PutUint32(h[:4], i)
  18  	binary.BigEndian.PutUint32(h[28:], i)
  19  	return wire.OutPoint{Hash: h}
  20  }
  21  
  22  // TestTwoRounds: two ranges of creations only, merged one after the other,
  23  // each reproducing the replay's set name. It is the case the registration
  24  // fixture exercised and the one the earlier streaming test missed, because
  25  // that test created and spent within one range.
  26  func TestTwoRounds(t *testing.T) {
  27  	dir := t.TempDir()
  28  	if _, err := WriteSet(dir, 0, nil); err != nil {
  29  		t.Fatal(err)
  30  	}
  31  	if _, err := Promote(dir, 0); err != nil {
  32  		t.Fatal(err)
  33  	}
  34  	var set []utxo.Entry
  35  	prev := uint32(0)
  36  	for round := 0; round < 2; round++ {
  37  		var ts []utxo.Touch
  38  		for h := 0; h < 10; h++ {
  39  			var block []utxo.Touch
  40  			for k := 0; k < 5; k++ {
  41  				id := uint32(round*50 + h*5 + k)
  42  				e := utxo.Entry{OutPoint: testOp(id), Value: int64(id), Height: uint32(h)}
  43  				block = append(block, utxo.Touch{OutPoint: e.OutPoint, Height: uint32(h), New: e})
  44  			}
  45  			ts = append(ts, block...)
  46  			var err error
  47  			set, err = utxo.ApplyRange(set, block)
  48  			if err != nil {
  49  				t.Fatal(err)
  50  			}
  51  		}
  52  		to := prev + 10
  53  		if _, err := CutRange(dir, prev, to, ts); err != nil {
  54  			t.Fatal(err)
  55  		}
  56  		d, err := LoadDiff(dir, prev, to)
  57  		if err != nil {
  58  			t.Fatal(err)
  59  		}
  60  		got, err := MergeRange(dir, prev, to, d, NameOf(set))
  61  		if err != nil {
  62  			t.Fatalf("round %d: %v", round, err)
  63  		}
  64  		if got != NameOf(set) {
  65  			t.Fatalf("round %d: merge gives %s, replay gives %s", round, got, NameOf(set))
  66  		}
  67  		prev = to
  68  	}
  69  	_ = chainhash.Hash{}
  70  }
  71