state

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 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 an identity's durable record in its home: the funding trees its carriers and vaults spend, the vaults it owns (each vault's chain of mined tokens and every version it published), the objects persisted before they are published, the sweeps it built, every content key commitment it ever published or listed, and, for a reader, the latest token of each vault it has verified.

It is written before anything leaves the machine. A carrier is persisted with the funding output it spends marked used and its commitment recorded, and a transition with the record it signs, before either is sent, so a run that stops part way sends the same bytes again and never makes a second carrier on one funding output. The file is written atomically (a temporary file, fsync, rename, fsync of the directory) at mode 0600, and the home is locked while a command writes.

The file holds no key: a content key is sampled, wrapped and forgotten, and an epoch key is opened from the group's wraps each time it is needed. The identity key the wallet keeps in the same directory opens every version this identity wrote through its self wrap, so the home is kept at mode 0700 and backed up as a secret.

Index

Constants

View Source
const (
	KindSecret = "secret"
	KindToken  = "vault-token"
	KindSweep  = "sweep"
)

The kinds of object a home publishes.

View Source
const (
	TreeSecret = "secret"
	TreeChain  = "chain"
)

The kinds of funding tree, each under its own key and basket (spec section 12.4).

View Source
const File = "state.json"

File is the state file's name in the home.

View Source
const Version = 1

Version is the state file's format version.

Variables

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

ErrLocked is a lock another process holds.

View Source
var TreeKinds = []string{TreeSecret, TreeChain}

TreeKinds are the tree kinds in a fixed order.

Functions

func DropChain

func DropChain(home, crypt, org, vault string)

DropChain forgets the tokens kept of a vault.

func LoadChain

func LoadChain(home, crypt, org, vault string) [][]byte

LoadChain reads the tokens a reader kept of a vault, in seq order from 0; nil when it kept none.

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 SaveChain

func SaveChain(home, crypt, org, vault string, beefs [][]byte) error

SaveChain keeps the tokens of a vault a reader verified, atomically.

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 Payment

type Payment struct {
	Txid string `json:"txid"`
	// Beef is the payment as Atomic BEEF, hex, with its ancestry back to
	// each proof.
	Beef string `json:"beef"`
}

Payment is a payment this identity made that has not mined yet.

type Pending

type Pending struct {
	Kind  string `json:"kind"`
	Crypt string `json:"crypt"`
	Txid  string `json:"txid"`
	// Beef is the object's BEEF, hex.
	Beef string `json:"beef"`
	// The lookup that confirms a host holds it, when the host's answer to
	// the submission admitted nothing: for a secret the owner, vault,
	// secret and version (version class); for a token the owner, vault and
	// seq (vault-seq class); for a sweep one outpoint it spends.
	Org      string `json:"org,omitempty"`
	Vault    string `json:"vault,omitempty"`
	Seq      uint64 `json:"seq,omitempty"`
	Secret   string `json:"secret,omitempty"`
	Version  uint64 `json:"version,omitempty"`
	Outpoint string `json:"outpoint,omitempty"`
	// NotBefore is the Unix time before which it is not published: a mined
	// object waits for the propagation delay (spec section 8.3).
	NotBefore int64 `json:"notBefore,omitempty"`
}

Pending is one object persisted before it is published.

func (Pending) Mined

func (p Pending) Mined() bool

Mined reports whether a pending object is a mined one, which every host named must take.

type PendingTree

type PendingTree struct {
	// Kind is the tree's kind.
	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 SecretVersion

type SecretVersion struct {
	// Name is the secret's name; ID its identifier, hex.
	Name    string `json:"name"`
	ID      string `json:"id"`
	Version uint64 `json:"version"`
	// ContentID and Commitment are the version's, hash byte order, hex.
	ContentID  string `json:"contentId"`
	Commitment string `json:"commitment"`
	Epoch      uint64 `json:"epoch"`
	Label      string `json:"label"`
	// Txid is the carrier's txid, display order.
	Txid string `json:"txid"`
	// Tree and Vout are the secret-tree output the carrier spends: a sweep
	// of it retracts the version.
	Tree string `json:"tree"`
	Vout uint32 `json:"vout"`
	// Raw is the carrier's transaction, hex: its one output holds the
	// record, with the ciphertext when it is carried. Emptied once the
	// version is swept. The funding tree it spends is kept with the trees,
	// and the BEEF it is published in stays in the outbox only until every
	// host named took it.
	Raw string `json:"raw,omitempty"`
	// Hash is the ciphertext's SHA-256, hex; Stored is set when the
	// ciphertext is not carried and was written to the content directory.
	Hash   string `json:"hash"`
	Stored bool   `json:"stored,omitempty"`
	// Listed is set once a mined token of the vault lists the version, with
	// that token's seq.
	Listed bool   `json:"listed,omitempty"`
	Seq    uint64 `json:"seq,omitempty"`
	// Ended is set once a later token replaced or retired the version.
	Ended bool `json:"ended,omitempty"`
	// Swept is the sweep that spent its funding output.
	Swept string `json:"swept,omitempty"`
	// Created is the record's claim; RotateEvery and NotAfter the
	// payload's, kept so that what is due is computed without a read.
	Created     uint64 `json:"created"`
	RotateEvery uint64 `json:"rotateEvery,omitempty"`
	NotAfter    uint64 `json:"notAfter,omitempty"`
}

SecretVersion is one version of one secret, as its owner keeps it.

type Seen

type Seen struct {
	Crypt string `json:"crypt"`
	Org   string `json:"org"`
	Vault string `json:"vault"`
	Seq   uint64 `json:"seq"`
	Txid  string `json:"txid"`
}

Seen is the latest token of one vault this identity verified.

type SeenGroup

type SeenGroup struct {
	Hall  string `json:"hall"`
	Org   string `json:"org"`
	Chain string `json:"chain"`
	Seq   uint64 `json:"seq"`
	Txid  string `json:"txid"`
}

SeenGroup is the latest token of one group's chain this identity verified: borg's hall, the organisation, and the chain borg's reader names (a group id).

type State

type State struct {
	Version int `json:"version"`
	// Identity is the identity key, compressed hex.
	Identity string `json:"identity"`
	// Crypts are the crypts this home created (crypt new).
	Crypts []string `json:"crypts,omitempty"`

	// Trees are the funding trees by kind.
	Trees map[string]*TreeSet `json:"trees,omitempty"`

	// PendingTrees are the funding trees signed for a pool coin that the
	// home 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"`

	// Vaults are the vaults this identity owns.
	Vaults []Vault `json:"vaults,omitempty"`

	// Outbox holds every object persisted and not yet confirmed published,
	// oldest first. A command that finds one publishes the same bytes
	// before it builds anything new.
	Outbox []Pending `json:"outbox,omitempty"`
	// Sweeps are the sweeps this home built.
	Sweeps []Sweep `json:"sweeps,omitempty"`

	// Commitments are every content key commitment this home published or
	// listed, in any vault (hash byte order, hex). A version whose
	// commitment is here is never published (spec section 6.2).
	Commitments []string `json:"commitments,omitempty"`

	// Seen is the latest token of each vault this identity verified as a
	// reader: a host that answers an older one is answering an old state.
	Seen []Seen `json:"seen,omitempty"`
	// SeenGroups is the latest token of each group's chain this identity
	// verified through borg's reader: hosts of the hall that answer the
	// group only below it are answering an old state, in which a member
	// since removed is still a member.
	SeenGroups []SeenGroup `json:"seenGroups,omitempty"`

	// Payments are the payments this identity made for priced questions
	// that have not mined yet, each as the BEEF the host was handed: the
	// next payment may spend the change of one, and then carries it.
	Payments []Payment `json:"payments,omitempty"`

	// Book is what payee settle did for this identity as a host's payee:
	// the payments it settled ("settled") and those the network refused
	// for good ("unsettleable").
	payee.Book

	// Taking is the coin pool as it stood when a command that spends from
	// it last saved this state: the record made before a coin is taken. A
	// coin leaves the pool's own file the moment it is taken, before the
	// transaction that spends it is recorded here, so a run that stops in
	// between leaves a coin in neither. The next command finds it in this
	// list and in no transaction the home records, and puts it back when
	// the node shows it unspent. It is empty whenever no command is
	// running.
	Taking []bwallet.Output `json:"taking,omitempty"`
	// contains filtered or unexported fields
}

State is one identity's state.

func Load

func Load(home, identity string) (*State, error)

Load reads the home's state, or a new one for identity when the home has none yet.

func (*State) AddCrypt

func (s *State) AddCrypt(crypt string)

AddCrypt records a crypt this home created, once.

func (*State) Commit

func (s *State) Commit(c string)

Commit records a commitment, once.

func (*State) Committed

func (s *State) Committed(c string) bool

Committed reports whether a commitment (hash byte order, hex) was published or listed by this home before.

func (*State) Done

func (s *State) Done(txid, crypt string)

Done removes a published object from the outbox.

func (*State) DropPendingTree

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

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

func (*State) Forget

func (s *State) Forget(crypt, org, vault string)

Forget drops what was seen of a vault: the token remembered is no longer mined, so a reorganisation displaced it (spec section 14).

func (*State) ForgetGroup

func (s *State) ForgetGroup(hall, org, chain string)

ForgetGroup drops what was seen of a group's chain: the token remembered is no longer mined, so a reorganisation displaced it.

func (*State) InFlight

func (s *State) InFlight() *Sweep

InFlight is the sweep built and not yet finished, if there is one.

func (*State) KeepPayments

func (s *State) KeepPayments(waiting []string) bool

KeepPayments drops every payment whose txid is not in waiting: it mined, and its change needs no ancestry any more. It reports whether any went.

func (*State) KeepPendingTree

func (s *State) KeepPendingTree(p PendingTree)

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

func (*State) MintedAhead

func (s *State) MintedAhead(txid string) *funding.Tree

MintedAhead finds txid among the trees minted ahead and not adopted, of every kind.

func (*State) OnSave

func (s *State) OnSave(f func())

OnSave sets what runs before every Save: a publisher records the pool with it. Nil clears it.

func (*State) PaymentOf

func (s *State) PaymentOf(txid string) *Payment

PaymentOf finds an unmined payment of this identity's.

func (*State) PendingTreeOf

func (s *State) PendingTreeOf(txid string) *PendingTree

PendingTreeOf finds the record of the tree txid.

func (*State) Queued

func (s *State) Queued(txid, crypt string) bool

Queued reports whether txid is in the outbox for crypt. A sweep is published in every crypt it retracts a version of, so an object is one transaction in one crypt.

func (*State) Save

func (s *State) Save() error

Save writes the state atomically at mode 0600.

func (*State) See

func (s *State) See(v Seen)

See records a verified latest token, never moving a vault backwards.

func (*State) SeeGroup

func (s *State) SeeGroup(v SeenGroup)

SeeGroup records a verified latest token of a group's chain, never moving the group backwards.

func (*State) SeenGroupOf

func (s *State) SeenGroupOf(hall, org, chain string) *SeenGroup

SeenGroupOf is the latest token of a group's chain this identity verified, or nil.

func (*State) SeenOf

func (s *State) SeenOf(crypt, org, vault string) *Seen

SeenOf is the latest token of a vault this identity verified, or nil.

func (*State) Set

func (s *State) Set(kind string) *TreeSet

Set is the tree set of kind, made when absent.

func (*State) TreeOf

func (s *State) TreeOf(txid string) (*funding.Tree, string, bool)

TreeOf finds a funding tree of any kind the home recorded, and its kind.

func (*State) UnsettledTrees

func (s *State) UnsettledTrees() []PendingTree

UnsettledTrees are the records of trees the home records nowhere else: those a command has yet to ask the node about. A tree minted ahead and recorded in its kind's Ahead keeps its record until it is adopted, and is settled.

func (*State) VaultByID

func (s *State) VaultByID(id string) *Vault

VaultByID finds one of the home's vaults by identifier.

type Sweep

type Sweep struct {
	Tree  string   `json:"tree"`
	Vouts []uint32 `json:"vouts"`
	Crypt string   `json:"crypt"`
	// Crypts are every crypt the sweep is published in: Crypt, and every
	// other crypt that holds a version funded by an output it spends. A
	// funding tree is the identity's, not a crypt's.
	Crypts []string `json:"crypts,omitempty"`
	Txid   string   `json:"txid"`
	// Adopted is set for a spend this home did not build, or built and
	// gave up on: the node named it as the spender of an output, and the
	// home took it as the sweep it is.
	Adopted bool `json:"adopted,omitempty"`
	// RawHex is the signed sweep; BumpHex and Height are set once it mined.
	RawHex    string `json:"rawHex"`
	BumpHex   string `json:"bumpHex,omitempty"`
	Height    uint32 `json:"height,omitempty"`
	Submitted bool   `json:"submitted,omitempty"`
	// Done is set once it is mined and handed to the outbox.
	Done bool `json:"done,omitempty"`
	// Fee is the pool coin that pays its fee, as the pool held it.
	Fee *bwallet.Output `json:"fee,omitempty"`
	// Failed is why the network refused it, once it did.
	Failed string `json:"failed,omitempty"`
}

Sweep is one mined transaction spending funding-tree outputs: output 0 a funding-shaped tombstone, so a host admits it, and every carrier that spent one of Vouts retracted once it mines.

type Token

type Token struct {
	Seq    uint64 `json:"seq"`
	Txid   string `json:"txid"`
	RawHex string `json:"rawHex"`
	// BumpHex is its proof and Height its block's height.
	BumpHex string `json:"bumpHex"`
	Height  uint32 `json:"height"`
	// Record is the token's vault record, hex; Vout its token output.
	Record string `json:"record"`
	Vout   uint32 `json:"vout"`
	// Parent is the transaction its predecessor input spends: the token at
	// seq - 1, or at seq 0 the chain tree.
	Parent string `json:"parent"`
	// Published is set once every host named took it.
	Published bool `json:"published,omitempty"`
}

Token is one mined vault token.

func Latest

func Latest(ts []Token) *Token

Latest is the chain's last token, or nil.

type Transition

type Transition struct {
	// Vault is the vault's identifier.
	Vault string `json:"vault"`
	Seq   uint64 `json:"seq"`
	Txid  string `json:"txid"`
	// RawHex is the signed transaction, Record its vault record.
	RawHex string `json:"rawHex"`
	Record string `json:"record"`
	Vout   uint32 `json:"vout"`
	// Parent is the transaction its predecessor input spends, and
	// ParentVout the output.
	Parent     string `json:"parent"`
	ParentVout uint32 `json:"parentVout"`
	// Fee is the pool coin that pays its fee, as the pool held it.
	Fee *bwallet.Output `json:"fee,omitempty"`
	// Submitted is set once the settlement leg took it.
	Submitted bool `json:"submitted,omitempty"`
	// What describes it in notes.
	What string `json:"what,omitempty"`
}

Transition is a vault token built and persisted before it is sent.

type TreeSet

type TreeSet struct {
	Tree  *funding.Tree  `json:"tree,omitempty"`
	Trees []funding.Tree `json:"trees,omitempty"`
	Ahead []funding.Tree `json:"ahead,omitempty"`
}

TreeSet is one kind's funding trees: the one carriers spend now, every tree minted, and the trees minted ahead and never adopted when a run ended (on the chain, in no other list, kept so a sweep finds them).

type Vault

type Vault struct {
	// ID is the vault identifier, 64 lowercase hex digits.
	ID    string `json:"id"`
	Crypt string `json:"crypt"`
	Name  string `json:"name"`
	// Hall and Group name the borg group whose epochs key the vault.
	Hall   string   `json:"hall"`
	Group  string   `json:"group"`
	Anchor uint64   `json:"anchor"`
	Labels []string `json:"labels"`
	// Tokens are its chain, mined, in seq order.
	Tokens []Token `json:"tokens,omitempty"`
	// Pending is a transition built and persisted, not yet known mined.
	Pending *Transition `json:"pending,omitempty"`
	// Versions are every version of every secret this home published in
	// the vault, oldest first.
	Versions []SecretVersion `json:"versions,omitempty"`
}

Vault is one vault this identity owns.

func (*Vault) ByTxid

func (v *Vault) ByTxid(txid string) *SecretVersion

ByTxid finds a version by its carrier's txid.

func (*Vault) VersionOf

func (v *Vault) VersionOf(id, cid string) *SecretVersion

VersionOf finds a version of a secret (by identifier, hex) the home published: the one with the given content id when cid is not empty, else the highest listed and not ended.

Jump to

Keyboard shortcuts

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