package lattice import ( "fmt" "os" ) // EmptyLattice is the corrupt .iskr the Go writer in this package wrote while // it was being built, kept as a fixture because of what it disproves. // // iskra/storage.mx saved an empty tree through saveMemoryTree, which called // StorageCreate (root blocks written at index 0..7, nCount 8) and then // StorageClose without ever assigning the created tree's nCount back. The // header was written from a zero Tree, so the file is 72 bytes: magic, version, // eight zero roots, caps 0/0. // // It exists because it is the version-4 copy of exactly that file: 72 bytes // with a version-4 spine and one word of its own at magic+50, a -1 cast to // uint32 where the version-5 layout keeps NodeCap. Read as version 4 the // version-5 offsets land on the last root entry and the free pointer: NodeCap // comes out 0 and FreeHead comes out that same word, so the record region // starts at the header and root 0 is a free block. var EmptyLattice = makeEmptyLattice() func makeEmptyLattice() []byte { b := make([]byte, HeaderSize) copy(b[OffMagic:], Magic) le.PutUint16(b[OffVersion:], Version4) // NCount, RCount and PendCount stay zero. // Roots 0..5 are zero. Roots 6 and 7 begin at offset 42, so the word at // offset 46 is the first half of root 7; that is the only root word that // gets overwritten, which is already root 0 of the version-5 reading. le.PutUint32(b[50:], 0xFFFFFFFE) le.PutUint32(b[54:], 0) copy(b[58:72], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) return b } // CheckEmptyLattice reads the fixture as a version-4 header and reports what a // version-5 reader derives from it. Accessors: CheckEmptyLatticeFacts returns // the facts the test asserts. func CheckEmptyLatticeFacts() (rc RegionCaps, freeHeadAsV5 uint32, nodeCapAsV5 uint32, err error) { if len(EmptyLattice) != HeaderSize { return RegionCaps{}, 0, 0, fmt.Errorf("lattice: EmptyLattice is %d bytes, want %d", len(EmptyLattice), HeaderSize) } hdr := FileHeader{ Version: le.Uint16(EmptyLattice[OffVersion:]), NCount: le.Uint32(EmptyLattice[OffNCount:]), RCount: le.Uint32(EmptyLattice[OffRCount:]), FreeHead: le.Uint32(EmptyLattice[OffFreeHead:]), } if hdr.Version != Version4 { return RegionCaps{}, 0, 0, fmt.Errorf("lattice: EmptyLattice version %d, want %d", hdr.Version, Version4) } rc, err = ReadRegionCaps(EmptyLattice) if err != nil { return RegionCaps{}, 0, 0, err } // The version-5 offsets land on the fixture's single nonzero word. fh := le.Uint32(EmptyLattice[OffFreeHead:]) nc := le.Uint32(EmptyLattice[OffNodeCap:]) return rc, fh, nc, nil } // CheckEmptyLattice is the fixture check the test uses: a version-5 reader of // this header resolves the record region at the header itself, sees NodeCap 0, // and reads a free head that names root 0. func CheckEmptyLattice() (RegionCaps, error) { rc, fh, _, err := CheckEmptyLatticeFacts() if err != nil { return RegionCaps{}, err } if rc.RecOff != int64(HeaderSize) || rc.NodeCap != 0 { return RegionCaps{}, fmt.Errorf("lattice: EmptyLattice as v5 gives caps %+v; want NodeCap 0 and records at %d", rc, HeaderSize) } // FreeHead under version-5 offsets is the same word the writer left at // offset 50, and it names root 0: a walk from any branch root would find the // root free, take it, and return an empty block. if fh != 0xFFFFFFFE { return RegionCaps{}, fmt.Errorf("lattice: EmptyLattice free head as v5 is %#x, want the fixture word 0xfffffffe", fh) } return rc, nil } // WriteTo writes the fixture to path for a test that needs a corrupt file on // disk. func WriteEmptyLattice(path string) error { return os.WriteFile(path, EmptyLattice, 0o644) }