package utxobuild_test import ( "os" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" utxostore "git.smesh.lol/btcd-lite/internal/utxo/store" "git.smesh.lol/btcd-lite/internal/utxobuild" ) // bodies is the fixture chain, as a block source. type bodies map[uint32][]byte func (b bodies) Block(height uint32) ([]byte, error) { raw, ok := b[height] if !ok { return nil, os.ErrNotExist } return raw, nil } // TestBuildOverBodiesWithSpends builds a checkpoint chain over bodies that // create outputs and bodies that spend them, with spends falling in later // ranges than the creations. // // It is the builder's whole path over a chain where the two paths have // something to disagree about. A chain of creations only cannot fail this way, // and one round was lost to a fixture that spent nothing while claiming to. func TestBuildOverBodiesWithSpends(t *testing.T) { raw, spends := chainOfBodies(40) src := bodies{} for h := uint32(0); h < uint32(len(raw)); h++ { src[h] = raw[h] } for h := range spends { if h < 40 && (h+1)%10 != 0 { t.Logf("a spend at %d, which is inside a checkpoint range", h) } } dir := t.TempDir() utxobuild.Debug(func(height uint32, entries int, name chainhash.Hash) { if height == 19 || height == 9 { t.Logf("walk at %d: %d entries %s", height, entries, name) } }, func(to uint32, merged, walked chainhash.Hash) { t.Logf("checkpoint %d: merged %s walked %s", to, merged, walked) }) defer utxobuild.Debug(nil, nil) res, err := utxobuild.Build(utxobuild.Config{ Dir: dir, Interval: 10, From: 0, To: 40, Blocks: src, Logf: t.Logf, }) if err != nil { t.Fatalf("build: %v", err) } if res.Name == (chainhash.Hash{}) { t.Fatal("the build returned no name") } // The last snapshot is the one before the range's end, because the end is // exclusive: 40 is not covered, 30 is. if res.Height != 40 { t.Fatalf("the walk ended at %d", res.Height) } want, err := utxostore.SetName(dir, 30) if err != nil { t.Fatal(err) } if want != res.Name { t.Fatalf("the build reported %s, its file at 30 is %s", res.Name, want) } }