package utxo_test import ( "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" utxostore "git.smesh.lol/btcd-lite/internal/utxo/store" "git.smesh.lol/btcd-lite/internal/wire" ) // TestTwoRangesSpendAcrossTheBoundary is the builder's situation with no blocks // in it: a first range of creations written as a checkpoint, a second range // whose spends name entries the first range created, and the merge of the // second range compared with the walk over both. func TestTwoRangesSpendAcrossTheBoundary(t *testing.T) { dir := t.TempDir() if _, err := utxostore.WriteSet(dir, 0, nil); err != nil { t.Fatal(err) } if _, err := utxostore.Promote(dir, 0); err != nil { t.Fatal(err) } // Range one: five creations at heights 0..4. var set []utxo.Entry var first []utxo.Touch for h := uint32(0); h < 5; h++ { id := uint32(100 + h) tc := utxo.Touch{OutPoint: opOf(id), Height: h, New: utxo.Entry{OutPoint: opOf(id), Value: int64(id), Height: h}} first = append(first, tc) var err error set, err = utxo.ApplyRange(set, []utxo.Touch{tc}) if err != nil { t.Fatal(err) } } if _, err := utxostore.CutRange(dir, 0, 5, first); err != nil { t.Fatal(err) } d1, _ := utxostore.LoadDiff(dir, 0, 5) cp5, err := utxostore.MergeRange(dir, 0, 5, d1, utxostore.NameOf(set)) if err != nil { t.Fatalf("checkpoint 5: %v", err) } t.Logf("checkpoint 5 is %s", cp5) // Range two: spends one of them and creates five more. var second []utxo.Touch second = append(second, utxo.Touch{OutPoint: opOf(102), Height: 5, Spent: true}) for h := uint32(5); h < 10; h++ { id := uint32(200 + h) second = append(second, utxo.Touch{OutPoint: opOf(id), Height: h, New: utxo.Entry{OutPoint: opOf(id), Value: int64(id), Height: h}}) } for _, tc := range second { var err error set, err = utxo.ApplyRange(set, []utxo.Touch{tc}) if err != nil { t.Fatalf("walk at %d: %v", tc.Height, err) } } if _, err := utxostore.CutRange(dir, 5, 10, second); err != nil { t.Fatal(err) } d2, _ := utxostore.LoadDiff(dir, 5, 10) cp10, err := utxostore.MergeRange(dir, 5, 10, d2, utxostore.NameOf(set)) if err != nil { t.Fatalf("checkpoint 10: %v", err) } t.Logf("checkpoint 10 is %s, entries %d", cp10, len(set)) _ = chainhash.Hash{} _ = wire.OutPoint{} }