package process_test import ( "os" "path/filepath" "strings" "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" "git.smesh.lol/btcd-lite/internal/process" "git.smesh.lol/btcd-lite/internal/staging" "git.smesh.lol/btcd-lite/internal/store" ) // headerAt is a distinct valid header per height. func headerAt(i uint32) btcwire.BlockHeader { return btcwire.BlockHeader{ Version: 1, MerkleRoot: chainhash.Hash{byte(i), byte(i >> 8)}, Timestamp: 1231006505 + i, Bits: 0x1d00ffff, Nonce: i, } } // fixture is a chain of n heights in a header store, an empty body store and a // staging log, which is what a fill leaves behind. func fixture(t *testing.T, n int) (process.Config, *staging.Log, *store.BlockStore) { t.Helper() dir := t.TempDir() headers, err := store.NewHeaderStore(filepath.Join(dir, "headers")) if err != nil { t.Fatal(err) } t.Cleanup(func() { headers.Close() }) hdrs := make([]btcwire.BlockHeader, 0, n) for i := 0; i < n; i++ { hdrs = append(hdrs, headerAt(uint32(i))) } if err := headers.SetAnchor(neutrino.Anchor{Height: 0, BlockHash: hdrs[0].BlockHash(), FilterHeight: -1}); err != nil { t.Fatal(err) } if err := headers.PutHeaders(0, hdrs); err != nil { t.Fatal(err) } bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatal(err) } t.Cleanup(func() { bodies.Close() }) log, err := staging.Open(filepath.Join(dir, "staging")) if err != nil { t.Fatal(err) } t.Cleanup(func() { log.Close() }) return process.Config{ Chain: "btc", Staging: log, Bodies: bodies, Headers: headers, Missing: filepath.Join(dir, "missing.log"), Logf: t.Logf, }, log, bodies } // body is the bytes a fill would have had for a header: the header and filler. func body(h btcwire.BlockHeader) []byte { return append(h.SerializeAll(), make([]byte, 24)...) } // TestStagedBodiesBecomeBlocks: a body whose claim and header agree is stored, // and the log is empty afterwards. func TestStagedBodiesBecomeBlocks(t *testing.T) { cfg, log, bodies := fixture(t, 4) for i := uint32(0); i < 4; i++ { h := headerAt(i) if err := log.Append(i, h.BlockHash(), body(h)); err != nil { t.Fatal(err) } } res, err := process.Run(cfg) if err != nil { t.Fatal(err) } if res.Kept != 4 || res.Dropped != 0 { t.Fatalf("%d kept, %d dropped", res.Kept, res.Dropped) } if bodies.Len() != 4 { t.Fatalf("the store holds %d blocks", bodies.Len()) } if log.Len() != 0 { t.Fatalf("the log holds %d bodies after the pass", log.Len()) } // The stored block is the staged bytes, under the header's hash. h2 := headerAt(2) got, err := bodies.GetBlock(h2.BlockHash()) if err != nil { t.Fatalf("height 2 is not stored: %v", err) } if string(got) != string(body(h2)) { t.Fatal("the stored bytes are not the staged ones") } } // TestWrongClaimIsDropped: a body filed under a hash it does not commit to is // dropped, the rest of the fill survives, and the height is written down. func TestWrongClaimIsDropped(t *testing.T) { cfg, log, bodies := fixture(t, 3) good := headerAt(0) if err := log.Append(0, good.BlockHash(), body(good)); err != nil { t.Fatal(err) } h1, h2 := headerAt(1), headerAt(2) // Height 1's body arrives under height 1's hash but the body is height 2's. if err := log.Append(1, h1.BlockHash(), body(h2)); err != nil { t.Fatal(err) } if err := log.Append(2, h2.BlockHash(), body(h2)); err != nil { t.Fatal(err) } res, err := process.Run(cfg) if err != nil { t.Fatal(err) } if res.Kept != 2 || res.Dropped != 1 { t.Fatalf("%d kept, %d dropped", res.Kept, res.Dropped) } if len(res.Failed) != 1 || res.Failed[0] != 1 { t.Fatalf("failed heights %v", res.Failed) } if bodies.HasBlock(h1.BlockHash()) { t.Fatal("the refused body was stored") } if !bodies.HasBlock(h2.BlockHash()) { t.Fatal("the body after the refused one was not stored") } raw, err := os.ReadFile(cfg.Missing) if err != nil { t.Fatalf("no record of the drop: %v", err) } if !strings.Contains(string(raw), "btc 1 ") { t.Fatalf("the record does not name the height: %q", raw) } } // TestClaimAgainstTheWrongChainIsDropped: a body that is a real block but not // this chain's at that height is refused. That is the check the split exists // for: the downloader asked a peer, and a peer can answer with anything. func TestClaimAgainstTheWrongChainIsDropped(t *testing.T) { cfg, log, bodies := fixture(t, 3) // A body from another chain: a header the store does not hold. other := btcwire.BlockHeader{Version: 1, Nonce: 999, MerkleRoot: chainhash.Hash{9}} if err := log.Append(1, other.BlockHash(), body(other)); err != nil { t.Fatal(err) } res, err := process.Run(cfg) if err != nil { t.Fatal(err) } if res.Dropped != 1 || bodies.Len() != 0 { t.Fatalf("%d dropped, %d stored", res.Dropped, bodies.Len()) } } // TestMissingHeaderIsDropped: a claim at a height the chain does not have is // dropped rather than stored. func TestMissingHeaderIsDropped(t *testing.T) { cfg, log, _ := fixture(t, 2) h := headerAt(2) if err := log.Append(2, h.BlockHash(), body(h)); err != nil { t.Fatal(err) } res, err := process.Run(cfg) if err != nil { t.Fatal(err) } if res.Dropped != 1 { t.Fatalf("%d dropped", res.Dropped) } if !strings.Contains(string(mustRead(t, cfg.Missing)), "height") { t.Logf("recorded: %s", mustRead(t, cfg.Missing)) } } func mustRead(t *testing.T, path string) string { t.Helper() raw, err := os.ReadFile(path) if err != nil { t.Fatalf("read %s: %v", path, err) } return string(raw) }