package staging_test import ( "os" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/staging" ) // appendN writes n bodies of 100+i bytes at heights 0..n-1. func appendN(t *testing.T, log *staging.Log, n int) []chainhash.Hash { t.Helper() hashes := make([]chainhash.Hash, n) for i := 0; i < n; i++ { h := chainhash.Hash{byte(i), byte(i >> 8)} hashes[i] = h if err := log.Append(uint32(i), h, make([]byte, 100+i)); err != nil { t.Fatal(err) } } return hashes } // TestAppendAndWalk: what is appended is what comes back, with its claim. func TestAppendAndWalk(t *testing.T) { dir := t.TempDir() log, err := staging.Open(dir) if err != nil { t.Fatal(err) } hashes := appendN(t, log, 3) if log.Len() != 3 { t.Fatalf("%d claims", log.Len()) } if got, want := log.Bytes(), int64(100+101+102); got != want { t.Fatalf("%d bytes, want %d", got, want) } if !log.Has(1) || log.Has(3) { t.Fatal("Has answers the wrong heights") } if err := log.Close(); err != nil { t.Fatal(err) } // A reopen finds the same claims and the same bodies. log2, err := staging.Open(dir) if err != nil { t.Fatal(err) } defer log2.Close() if log2.Len() != 3 { t.Fatalf("after a reopen: %d claims", log2.Len()) } seen := 0 if err := log2.Walk(func(c staging.Claim, body []byte) error { if c.Hash != hashes[seen] { t.Fatalf("claim %d names %s, not %s", seen, c.Hash, hashes[seen]) } if len(body) != 100+seen { t.Fatalf("body %d is %d bytes", seen, len(body)) } seen++ return nil }); err != nil { t.Fatal(err) } if seen != 3 { t.Fatalf("walked %d bodies", seen) } } // TestTornClaimIsCut: a claim recorded without its body, which is what a crash // between the two writes leaves, is dropped and the log reopens one shorter. func TestTornClaimIsCut(t *testing.T) { dir := t.TempDir() log, err := staging.Open(dir) if err != nil { t.Fatal(err) } appendN(t, log, 2) if err := log.Close(); err != nil { t.Fatal(err) } // Half a claim, as a crash mid-write leaves it. claims := filepath.Join(dir, "claims.dat") raw, err := os.ReadFile(claims) if err != nil { t.Fatal(err) } if err := os.WriteFile(claims, append(raw, make([]byte, 20)...), 0o644); err != nil { t.Fatal(err) } log2, err := staging.Open(dir) if err != nil { t.Fatalf("a torn claim stopped the open: %v", err) } defer log2.Close() if log2.Len() != 2 { t.Fatalf("%d claims after a torn append", log2.Len()) } fi, err := os.Stat(claims) if err != nil { t.Fatal(err) } if fi.Size() != 2*44 { t.Fatalf("the claim file is %d bytes, not two claims", fi.Size()) } // And the next append lands on the end of the good one. if err := log2.Append(2, chainhash.Hash{2}, make([]byte, 10)); err != nil { t.Fatal(err) } if log2.Len() != 3 { t.Fatalf("%d claims after appending over the cut", log2.Len()) } } // TestTornBodyIsCut: a claim whose body never finished is dropped, the segment // is cut back, and the body before it survives. func TestTornBodyIsCut(t *testing.T) { dir := t.TempDir() log, err := staging.Open(dir) if err != nil { t.Fatal(err) } appendN(t, log, 2) if err := log.Close(); err != nil { t.Fatal(err) } // Cut the second body in half: the claim is there, the bytes are not. seg := filepath.Join(dir, "00000000.dat") fi, err := os.Stat(seg) if err != nil { t.Fatal(err) } if err := os.Truncate(seg, fi.Size()-50); err != nil { t.Fatal(err) } log2, err := staging.Open(dir) if err != nil { t.Fatal(err) } defer log2.Close() if log2.Len() != 1 { t.Fatalf("%d claims after a torn body", log2.Len()) } if log2.Has(1) { t.Fatal("the height whose body was torn is still claimed") } if fi, err := os.Stat(seg); err != nil || fi.Size() != 4+100 { t.Fatalf("the segment is %v bytes, not one body", fi.Size()) } } // TestDrainEmpties: a drained log reopens empty, with no segment files left. func TestDrainEmpties(t *testing.T) { dir := t.TempDir() log, err := staging.Open(dir) if err != nil { t.Fatal(err) } appendN(t, log, 2) if err := log.Drain(); err != nil { t.Fatal(err) } if log.Len() != 0 || log.Bytes() != 0 { t.Fatalf("after a drain: %d claims, %d bytes", log.Len(), log.Bytes()) } entries, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } if len(entries) != 0 { t.Fatalf("the drained directory holds %d files", len(entries)) } if err := log.Close(); err != nil { t.Fatal(err) } log2, err := staging.Open(dir) if err != nil { t.Fatal(err) } defer log2.Close() if log2.Len() != 0 { t.Fatalf("a drained log reopened with %d claims", log2.Len()) } }