statetrace

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

package ledger implements an append-only ledger where state transitions can be verified

Index

Constants

View Source
const SchemaName = "blobcache/statetrace"

Variables

This section is empty.

Functions

func Constructor

func Constructor(params json.RawMessage, mkSchema schema.Factory) (schema.Schema, error)

Types

type Iterator

type Iterator[State Marshaler] struct {
	// contains filtered or unexported fields
}

Iterator is an iterator over all the intermediate states between two roots.

func (*Iterator[State]) Next

func (it *Iterator[State]) Next(ctx context.Context, dst *Root[State]) error

type Machine

type Machine[State Marshaler] struct {
	HashAlgo blobcache.HashAlgo
	// ParseState parses a State from a byte slice.
	ParseState Parser[State]
	// Verify verifies that prev -> next is a valid transition.
	// Verify must return nil only if the transition is valid, and never needs to be considered again.
	Verify VerifyFunc[State]
}

Machine performs operations on a ledger.

func (*Machine[State]) AndThen

func (m *Machine[State]) AndThen(ctx context.Context, s schema.RW, r Root[State], next State) (Root[State], error)

AndThen creates a new root with the state. History is updated to reflect the previous state. AndThen does not modify the current root, it returns a new root.

func (*Machine[State]) GetPrev

func (m *Machine[State]) GetPrev(ctx context.Context, s schema.RO, root Root[State]) (*Root[State], error)

GetPrev returns the root, immediately previous to the given root.

func (*Machine[State]) GetRoot

func (m *Machine[State]) GetRoot(ctx context.Context, s schema.RO, cid merklelog.CID) (*Root[State], error)

func (*Machine[State]) Initial

func (m *Machine[State]) Initial(initState State) Root[State]

Initial creates a new root with the given state, and an empty history.

func (*Machine[State]) NewIterator

func (m *Machine[State]) NewIterator(s schema.RO, prev Root[State], next Root[State]) *Iterator[State]

func (*Machine[State]) Parse

func (m *Machine[State]) Parse(data []byte) (Root[State], error)

Parse parses a Root from a byte slice.

func (*Machine[State]) PostRoot

func (m *Machine[State]) PostRoot(ctx context.Context, s schema.WO, root Root[State]) (merklelog.CID, error)

func (*Machine[State]) Slot

func (m *Machine[State]) Slot(ctx context.Context, s schema.RO, root Root[State], slot merklelog.Pos) (*Root[State], error)

Slot returns the state at the given position in the history.

func (*Machine[State]) Validate

func (m *Machine[State]) Validate(ctx context.Context, s schema.RO, prev, next Root[State]) error

Validate verifies that prev -> next is a valid transition.

type Marshaler

type Marshaler interface {
	// Marshal appends the marshaled representation of the object to out
	// and returns the new slice (of which out is a prefix).
	Marshal(out []byte) []byte
}

type Parser

type Parser[T any] = func(data []byte) (T, error)

type Root

type Root[State Marshaler] struct {
	// History is the history of all previous states.
	History merklelog.State
	// State is the current state of the system.
	State State
}

Root is contains transitive references to the entire history and current state of the system.

func Parse

func Parse[State Marshaler](data []byte, parseState Parser[State]) (Root[State], error)

Parse parses the Root from data.

func (Root[State]) Len

func (r Root[State]) Len() merklelog.Pos

Len is the number of states in the history. Len will also be the Pos of this root, when it is added to a merkle log.

func (Root[State]) Marshal

func (r Root[State]) Marshal(out []byte) []byte

type Schema

type Schema[T Marshaler] struct {
	Mach Machine[T]
}

func (*Schema[T]) ValidateChange

func (sch *Schema[T]) ValidateChange(ctx context.Context, change schema.Change) error

func (*Schema[T]) ValidateState

func (sch *Schema[T]) ValidateState(ctx context.Context, s schema.RO, root Root[T]) error

type Spec

type Spec struct {
	// HashAlgo is the hash algorithm to use for the ledger.
	HashAlgo blobcache.HashAlgo `json:"hash_algo"`
	// X is the inner schema.
	X blobcache.SchemaSpec `json:"x"`
}

type VerifyFunc

type VerifyFunc[State Marshaler] = func(ctx context.Context, s schema.RO, prev, next State) error

VerifyFunc verifies that prev -> next is a valid transition

Jump to

Keyboard shortcuts

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