state

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 10 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 state is a producer's durable record of one stream: its head (seq and commitment), the carrier persisted before it is published, the commitments not yet anchored, the anchor chain's head and the anchor in flight, and the funding trees carriers and anchors spend.

A producer that loses this file loses its head, and a producer that has lost its head must not restart the stream (docs/spec.md section 12): it creates a new one. So the file is written atomically (a temporary file, fsync, rename, fsync of the directory) at mode 0600, and a stream's directory is locked while a command writes it.

Index

Constants

View Source
const (
	// SweepRetract spends outputs of a batch funding tree, used or not,
	// with bcommon carrier.SweepAt: output 0 is a funding-shaped tombstone,
	// and every carrier that spent one of the outputs is retracted once it
	// mines (docs/spec.md section 6.4).
	SweepRetract = "retract"
	// SweepRetire spends only unused outputs of a tree no longer in use,
	// paying their value back to the coin pool. It retracts nothing.
	SweepRetire = "retire"
)

The kinds of sweep.

View Source
const Version = 1

Version is the state file's format version.

Variables

View Source
var ErrExists = errors.New("state: stream already exists in this home")

ErrExists is a stream this home already holds.

View Source
var ErrLocked = errors.New("state: locked by another blogs process")

ErrLocked is a lock another process holds.

View Source
var ErrNoStream = errors.New("state: no such stream in this home")

ErrNoStream is a stream this home holds no state for.

Functions

func Dir

func Dir(home, stream string) string

Dir is the directory of a stream's state under home.

func List

func List(home string) ([]string, error)

List names the streams under home, sorted.

func Lock

func Lock(path string) (func(), error)

Lock takes an exclusive lock on path (created at 0600), without waiting. The lock is the kernel's and ends with the process, so a crash never leaves it held. Release it with the returned function.

func WriteAtomic

func WriteAtomic(path string, data []byte, perm os.FileMode) (err error)

WriteAtomic writes data to path through a temporary file in the same directory, synced and renamed, then syncs the directory, so a crash leaves either the old file or the new one.

Types

type Anchor

type Anchor struct {
	Interval uint64 `json:"interval"`
	First    uint64 `json:"first"`
	Count    uint64 `json:"count"`
	Txid     string `json:"txid"`
	Vout     uint32 `json:"vout"`
	RawHex   string `json:"rawHex"`
	// BeefHex is kept while the token is unmined; BumpHex and Height once
	// it mined.
	BeefHex string `json:"beefHex,omitempty"`
	BumpHex string `json:"bumpHex,omitempty"`
	Height  uint32 `json:"height,omitempty"`
	// Submitted is set once the settlement leg accepted the token.
	Submitted bool `json:"submitted,omitempty"`
}

Anchor is an anchor token the producer built.

type PendingTree

type PendingTree struct {
	// Kind is which trees it belongs to: "batch" or "anchor".
	Kind string `json:"kind"`
	// Tree is the record the tree is adopted as, without a proof.
	Tree funding.Tree `json:"tree"`
	// Coin is the fee coin the tree spends, as the pool held it.
	Coin bwallet.Output `json:"coin"`
	// ReturnedOnce is set when a command found the tree absent from the
	// node and the coin unspent, and put the coin back: the record is kept
	// for one more command, in case the tree reaches the node after all.
	ReturnedOnce bool `json:"returned_once,omitempty"`
}

PendingTree is a funding tree signed for a pool coin and not yet adopted, keyed by the tree's txid.

type Stream

type Stream struct {
	Version int `json:"version"`
	// Stream is the full identifier <name>_<suffix>.
	Stream string `json:"stream"`
	// Identity is the producer's identity key, compressed hex.
	Identity string `json:"identity"`

	// Seq is the last seq built; Head its commitment, hash byte order hex.
	// Zero and empty before the first batch.
	Seq  uint64 `json:"seq"`
	Head string `json:"head,omitempty"`
	// Pending is the carrier of Seq while it may not have reached the
	// facade: persisted before the publish, cleared after it. A run that
	// finds it publishes these same bytes again, never a new carrier for
	// the same seq.
	Pending string `json:"pending,omitempty"`

	// Unanchored are the commitments of seq NextFirst to Seq, hash byte
	// order hex, in seq order: what the next anchors cover.
	Unanchored []string `json:"unanchored,omitempty"`
	// NextFirst is the first seq no mined and published anchor covers.
	NextFirst uint64 `json:"nextFirst"`

	// Anchor is the last anchor token mined and published: the head of the
	// anchor chain. Nil before the first.
	Anchor *Anchor `json:"anchor,omitempty"`
	// Inflight is the anchor built and handed to the settlement leg that
	// has not yet been published. At most one is in flight.
	Inflight *Anchor `json:"inflight,omitempty"`
	// LastAnchorMs is when the last anchor was built, Unix milliseconds.
	LastAnchorMs int64 `json:"lastAnchorMs,omitempty"`

	// Tree and Trees are the current batch funding tree and every one
	// minted; AnchorTree and AnchorTrees the same for the trees anchors
	// spend their fee from. Ahead are trees minted ahead and not adopted
	// when a run ended: on the chain, in no other list, kept so a sweep
	// finds them.
	Tree        *funding.Tree  `json:"tree,omitempty"`
	Trees       []funding.Tree `json:"trees,omitempty"`
	AnchorTree  *funding.Tree  `json:"anchorTree,omitempty"`
	AnchorTrees []funding.Tree `json:"anchorTrees,omitempty"`
	Ahead       []funding.Tree `json:"ahead,omitempty"`

	// PendingTrees are the funding trees signed for a pool coin that the
	// stream has not adopted yet, each with its kind and the coin it
	// spends: the record made before a tree reaches the settlement leg, so
	// a run that stops before the tree is adopted leaves what the next
	// command needs to ask the node what became of it. Adopting a tree
	// drops its record in the same save.
	PendingTrees []PendingTree `json:"pendingTrees,omitempty"`
	// Unpublished are the txids of trees adopted into Trees or AnchorTrees
	// whose publish to the hosts failed: publish pending. The next command
	// that spends publishes them before anything else.
	Unpublished []string `json:"unpublished,omitempty"`

	// Sweeps are the transactions built to spend funding-tree outputs, in
	// the order they were built: retractions and retirements. One neither
	// Done nor Failed is in flight, and the next sweep resumes it before
	// anything else.
	Sweeps []Sweep `json:"sweeps,omitempty"`

	// Tail is where a file being followed was read to.
	Tail *Tail `json:"tail,omitempty"`
	// contains filtered or unexported fields
}

Stream is one stream's state.

func Create

func Create(home, stream, identity string) (*Stream, error)

Create writes the state of a new stream and refuses one that exists.

func Load

func Load(home, stream string) (*Stream, error)

Load reads a stream's state.

func (*Stream) DropPendingTree

func (s *Stream) DropPendingTree(txid string) bool

DropPendingTree drops the record of the tree txid, and reports whether there was one.

func (*Stream) KeepPendingTree

func (s *Stream) KeepPendingTree(p PendingTree)

KeepPendingTree records p, replacing a record of the same tree.

func (*Stream) MintedAhead

func (s *Stream) MintedAhead(txid string) bool

MintedAhead reports whether txid is among the trees minted ahead and not adopted.

func (*Stream) PendingTree

func (s *Stream) PendingTree(txid string) *PendingTree

PendingTree finds the record of the tree txid, or nil.

func (*Stream) Save

func (s *Stream) Save() error

Save writes the state atomically at mode 0600.

type Sweep

type Sweep struct {
	Kind  string   `json:"kind"`
	Tree  string   `json:"tree"`
	Vouts []uint32 `json:"vouts"`
	// Seqs are the batches it retracts, when they were named.
	Seqs   []uint64 `json:"seqs,omitempty"`
	Txid   string   `json:"txid"`
	RawHex string   `json:"rawHex"`
	// BeefHex is kept while it is unmined; BumpHex and Height once it
	// mined.
	BeefHex string `json:"beefHex,omitempty"`
	BumpHex string `json:"bumpHex,omitempty"`
	Height  uint32 `json:"height,omitempty"`
	// Publish is set when the hosts hold what it spends, so it goes to the
	// stream's topic once mined; a tree minted ahead and never published
	// is retired without it.
	Publish   bool `json:"publish,omitempty"`
	Submitted bool `json:"submitted,omitempty"`
	Done      bool `json:"done,omitempty"`
	// Fee is the pool coin that pays a retraction's fee, as the pool held
	// it, so a retraction the network refuses can give it back. A
	// retirement pays its fee from the outputs it spends and has none.
	Fee *bwallet.Output `json:"fee,omitempty"`
	// Failed is why the network refused it, once it did: the node named
	// another transaction as the spender of one of its inputs. It is no
	// longer in flight, retracts or retires nothing, and is never Done.
	Failed string `json:"failed,omitempty"`
}

Sweep is one mined transaction spending funding-tree outputs.

func (Sweep) InFlight

func (s Sweep) InFlight() bool

InFlight reports whether the sweep is built and neither finished nor given up on.

type Tail

type Tail struct {
	Path   string `json:"path"`
	Dev    uint64 `json:"dev"`
	Ino    uint64 `json:"ino"`
	Offset int64  `json:"offset"`
}

Tail is a followed file's position.

Jump to

Keyboard shortcuts

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