1 package lattice
2 3 import (
4 "fmt"
5 "os"
6 )
7 8 // EmptyLattice is the corrupt .iskr the Go writer in this package wrote while
9 // it was being built, kept as a fixture because of what it disproves.
10 //
11 // iskra/storage.mx saved an empty tree through saveMemoryTree, which called
12 // StorageCreate (root blocks written at index 0..7, nCount 8) and then
13 // StorageClose without ever assigning the created tree's nCount back. The
14 // header was written from a zero Tree, so the file is 72 bytes: magic, version,
15 // eight zero roots, caps 0/0.
16 //
17 // It exists because it is the version-4 copy of exactly that file: 72 bytes
18 // with a version-4 spine and one word of its own at magic+50, a -1 cast to
19 // uint32 where the version-5 layout keeps NodeCap. Read as version 4 the
20 // version-5 offsets land on the last root entry and the free pointer: NodeCap
21 // comes out 0 and FreeHead comes out that same word, so the record region
22 // starts at the header and root 0 is a free block.
23 var EmptyLattice = makeEmptyLattice()
24 25 func makeEmptyLattice() []byte {
26 b := make([]byte, HeaderSize)
27 copy(b[OffMagic:], Magic)
28 le.PutUint16(b[OffVersion:], Version4)
29 // NCount, RCount and PendCount stay zero.
30 // Roots 0..5 are zero. Roots 6 and 7 begin at offset 42, so the word at
31 // offset 46 is the first half of root 7; that is the only root word that
32 // gets overwritten, which is already root 0 of the version-5 reading.
33 le.PutUint32(b[50:], 0xFFFFFFFE)
34 le.PutUint32(b[54:], 0)
35 copy(b[58:72], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
36 return b
37 }
38 39 // CheckEmptyLattice reads the fixture as a version-4 header and reports what a
40 // version-5 reader derives from it. Accessors: CheckEmptyLatticeFacts returns
41 // the facts the test asserts.
42 func CheckEmptyLatticeFacts() (rc RegionCaps, freeHeadAsV5 uint32, nodeCapAsV5 uint32, err error) {
43 if len(EmptyLattice) != HeaderSize {
44 return RegionCaps{}, 0, 0, fmt.Errorf("lattice: EmptyLattice is %d bytes, want %d", len(EmptyLattice), HeaderSize)
45 }
46 hdr := FileHeader{
47 Version: le.Uint16(EmptyLattice[OffVersion:]),
48 NCount: le.Uint32(EmptyLattice[OffNCount:]),
49 RCount: le.Uint32(EmptyLattice[OffRCount:]),
50 FreeHead: le.Uint32(EmptyLattice[OffFreeHead:]),
51 }
52 if hdr.Version != Version4 {
53 return RegionCaps{}, 0, 0, fmt.Errorf("lattice: EmptyLattice version %d, want %d", hdr.Version, Version4)
54 }
55 rc, err = ReadRegionCaps(EmptyLattice)
56 if err != nil {
57 return RegionCaps{}, 0, 0, err
58 }
59 // The version-5 offsets land on the fixture's single nonzero word.
60 fh := le.Uint32(EmptyLattice[OffFreeHead:])
61 nc := le.Uint32(EmptyLattice[OffNodeCap:])
62 return rc, fh, nc, nil
63 }
64 65 // CheckEmptyLattice is the fixture check the test uses: a version-5 reader of
66 // this header resolves the record region at the header itself, sees NodeCap 0,
67 // and reads a free head that names root 0.
68 func CheckEmptyLattice() (RegionCaps, error) {
69 rc, fh, _, err := CheckEmptyLatticeFacts()
70 if err != nil {
71 return RegionCaps{}, err
72 }
73 if rc.RecOff != int64(HeaderSize) || rc.NodeCap != 0 {
74 return RegionCaps{}, fmt.Errorf("lattice: EmptyLattice as v5 gives caps %+v; want NodeCap 0 and records at %d", rc, HeaderSize)
75 }
76 // FreeHead under version-5 offsets is the same word the writer left at
77 // offset 50, and it names root 0: a walk from any branch root would find the
78 // root free, take it, and return an empty block.
79 if fh != 0xFFFFFFFE {
80 return RegionCaps{}, fmt.Errorf("lattice: EmptyLattice free head as v5 is %#x, want the fixture word 0xfffffffe", fh)
81 }
82 return rc, nil
83 }
84 85 // WriteTo writes the fixture to path for a test that needs a corrupt file on
86 // disk.
87 func WriteEmptyLattice(path string) error {
88 return os.WriteFile(path, EmptyLattice, 0o644)
89 }
90