signedhead

package
v0.3.0-rc.53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package signedhead is the signed snapshot of a table's live rows: a name, a monotonic epoch, and the SHA-256 of each stored row. The signature is HMAC-SHA256 with a type prefix and length-prefixed fields, so holding the database file alone cannot widen a limit by putting rows back, deleting them, or rolling the head to an older pair. An epochs file beside the secrets is the rollback anchor; Beacon does not rebuild a head by scanning rows except on the explicit Rebuild command.

Index

Constants

View Source
const TypePrefix = "beacon/signed-head/v1\n"

TypePrefix is written first in the HMAC so a signature for something else cannot be reused.

Variables

View Source
var (
	ErrFailsafe = errors.New("signed head is failsafe")
	ErrNoKey    = errors.New("no signature key")
)

Errors the tables return.

Functions

func Accept

func Accept(result LoadResult, key string, row []byte) bool

Accept reports whether row (the exact stored bytes, or nil if missing) matches the head.

func HashRow

func HashRow(row []byte) string

HashRow is the hex SHA-256 of the exact stored row bytes.

func Sign

func Sign(sk []byte, h Head) []byte

Sign is HMAC-SHA256 over the type prefix and the length-prefixed canonical fields.

func Verify

func Verify(sk []byte, h Head) bool

Verify reports whether h.Sig matches Sign(sk, h). An empty key never verifies.

Types

type FileAnchor

type FileAnchor struct {
	Dir string
}

FileAnchor is the rollback anchor: Dir+"/epochs", one line per head ("<name> <epoch>\n").

func (FileAnchor) Epoch

func (a FileAnchor) Epoch(name string) uint64

Epoch is name's recorded epoch, or 0 when the file or the name is not there. A corrupt file is 0; Load treats a read error as failsafe instead of calling this.

func (FileAnchor) Record

func (a FileAnchor) Record(name string, epoch uint64) error

Record writes name's epoch, keeping the other heads, and fsyncs the file and the directory.

func (FileAnchor) Tracks

func (a FileAnchor) Tracks(name string) (bool, error)

Tracks reports whether the epochs file contains an entry for name, including epoch zero.

type Head struct {
	Name    string
	Epoch   uint64
	Entries map[string]string
	Sig     []byte
}

Head is one signed snapshot. Entries maps a row key to the hex SHA-256 of the exact stored bytes. Sig is the HMAC; it is not part of the canonical form.

type LoadResult

type LoadResult struct {
	Status Status
	Head   Head
}

LoadResult is Load's answer.

type Status

type Status int

Status is how Load judged the stored head against the epochs file.

const (
	// StatusEmpty is a first start: no head and the epochs file does not expect one.
	StatusEmpty Status = iota
	// StatusOK is a verified head whose epoch matches the file (or the file has caught up).
	StatusOK
	// StatusCatchUp is a verified head newer than the file (crash after the DB commit); Load
	// rewrites the file and Accept treats it as OK.
	StatusCatchUp
	// StatusFailsafe is a missing, bad, or stale head: the whole table is unusable until Rebuild.
	StatusFailsafe
)

type Table

type Table struct {
	Name    string
	Bucket  []byte
	HeadKey []byte
	SK      func() []byte
	Anchor  FileAnchor
	Alert   func(string)
}

Table is one signed table: the head row in Bucket at HeadKey, signed with SK, anchored in Dir.

func (*Table) AfterCommit

func (t *Table) AfterCommit(h Head) error

AfterCommit writes the epochs file. Call it after the database transaction has committed.

func (*Table) Commit

func (t *Table) Commit(tx kv.Tx, put map[string][]byte, del []string) (Head, error)

Commit updates the head for the keys this transaction wrote or deleted, signs it, and writes it in the same transaction. It does not scan the bucket. A failsafe head is an error.

func (*Table) CommitFrom

func (t *Table) CommitFrom(tx kv.Tx, res LoadResult, put map[string][]byte, del []string) (Head, error)

CommitFrom is Commit using a head already loaded and verified (a key rotation that must verify under the previous key and sign under the new one).

func (*Table) Load

func (t *Table) Load(tx kv.Tx) LoadResult

Load reads the head, verifies it, and compares its epoch to the epochs file.

func (*Table) Rebuild

func (t *Table) Rebuild(tx kv.Tx, rows map[string][]byte) (Head, error)

Rebuild writes a new signed head from rows (the only scan-based write) and advances the epoch past both the stored head and the epochs file, so a recovery is not itself stale.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL