state

package
v0.6.11 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 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 spend, the trees signed and not yet adopted, the carriers persisted before they are published, what it sent and acknowledged, the sweeps it built, the envelopes it read and the payments it took.

It is written before anything leaves the machine. A carrier is persisted with the funding output it spends marked used before it is submitted, so a run that stops part way republishes the same bytes and never makes a second carrier on the same funding output (spec section 8.2: a host answers one carrier per funding output, over its life). 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.

Index

Constants

View Source
const (
	KindEnvelope = "envelope"
	KindReceipt  = "receipt"
	KindSweep    = "sweep"
)

The kinds of object a home publishes.

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 bbox process")

ErrLocked is a lock another process holds.

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

ErrNoState is a home with no state file yet.

Functions

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 Payment

type Payment struct {
	Txid string `json:"txid"`
	// Beef is the payment as the host was handed it, hex.
	Beef string `json:"beef"`
}

Payment is a payment this identity made for a priced question.

type Pending

type Pending struct {
	Kind string `json:"kind"`
	// Office is the office whose topic it goes to; Offices, for a sweep,
	// every office it retracts carriers in.
	Office  string   `json:"office,omitempty"`
	Offices []string `json:"offices,omitempty"`
	Txid    string   `json:"txid"`
	// Beef is the object's Atomic BEEF, hex.
	Beef string `json:"beef"`
	// Confirm is what a lookup asks to confirm a host holds it, when the
	// host's answer to the submission admitted nothing: for an envelope
	// the recipient, box and created; for a receipt the acknowledging key
	// and one acknowledged txid; for a sweep one outpoint it spends.
	To       string `json:"to,omitempty"`
	Box      string `json:"box,omitempty"`
	Created  uint64 `json:"created,omitempty"`
	By       string `json:"by,omitempty"`
	AckTxid  string `json:"ackTxid,omitempty"`
	Outpoint string `json:"outpoint,omitempty"`
}

Pending is one object persisted before it is published.

type PendingTree

type PendingTree struct {
	// 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 Receipt

type Receipt struct {
	Txid   string   `json:"txid"`
	Office string   `json:"office"`
	Acks   []string `json:"acks"`
	Tree   string   `json:"tree"`
	Vout   uint32   `json:"vout"`
}

Receipt is a receipt this identity published.

type Received

type Received struct {
	Txid    string `json:"txid"`
	Office  string `json:"office"`
	From    string `json:"from"`
	Box     string `json:"box"`
	Created uint64 `json:"created"`
	Expires uint64 `json:"expires,omitempty"`
	// Paid is the satoshis the envelope's payment offers, when it passed
	// the recipient's checks; Internalized the payment's txid once taken.
	// Beef is the envelope's carrier, hex, kept while its payment is not
	// taken: once the envelope is acknowledged no free question answers
	// it, and the payment is still the recipient's to take.
	Paid         uint64 `json:"paid,omitempty"`
	Internalized string `json:"internalized,omitempty"`
	Beef         string `json:"beef,omitempty"`
	// Acked is the receipt that acknowledges it.
	Acked string `json:"acked,omitempty"`
	// Accepted is the payment's txid once it was taken on the network's
	// acceptance (fast, at or below the threshold) and before it is in the
	// pool: the next internalize waits for its proof and pools it.
	Accepted string `json:"accepted,omitempty"`
}

Received is an envelope to this identity it has read.

type Sent

type Sent struct {
	Txid    string `json:"txid"`
	Office  string `json:"office"`
	To      string `json:"to"`
	Box     string `json:"box"`
	Created uint64 `json:"created"`
	Expires uint64 `json:"expires,omitempty"`
	// Tree and Vout are the funding output the carrier spends: a mined
	// spend of it retracts the envelope.
	Tree string `json:"tree"`
	Vout uint32 `json:"vout"`
	// Paid is the satoshis of a payment inside it, and PaymentTxid the
	// payment transaction, never broadcast by the sender.
	Paid        uint64 `json:"paid,omitempty"`
	PaymentTxid string `json:"paymentTxid,omitempty"`
	// Swept is the sweep that retracts it.
	Swept string `json:"swept,omitempty"`
}

Sent is an envelope this identity sent.

type State

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

	// Tree is the funding tree carriers spend now, Trees every tree minted,
	// and Ahead the trees minted ahead and never 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"`
	Ahead []funding.Tree `json:"ahead,omitempty"`
	// PendingTrees are the funding trees signed for a pool coin that the
	// home has not adopted yet, each with 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"`

	// 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"`
	// Sent are the envelopes this identity sent; Receipts the receipts it
	// published; Sweeps the sweeps it built.
	Sent     []Sent    `json:"sent,omitempty"`
	Receipts []Receipt `json:"receipts,omitempty"`
	Sweeps   []Sweep   `json:"sweeps,omitempty"`
	// Received are the envelopes to this identity it has read.
	Received []Received `json:"received,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
	// LastSentMs is when the last envelope was built, Unix milliseconds,
	// for the send rate.
	LastSentMs int64 `json:"lastSentMs,omitempty"`

	// Payments are the payments this identity made for priced questions
	// that have not mined yet, each as the BEEF the host was handed. A
	// payment is recorded before it is sent: from then on the host holds a
	// transaction that spends the coin.
	Payments []Payment `json:"payments,omitempty"`

	// 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) AddOffice

func (s *State) AddOffice(office string)

AddOffice records an office this home created, once.

func (*State) Done

func (s *State) Done(txid 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) InFlight

func (s *State) InFlight() *Sweep

InFlight is the sweep built and not yet finished, if there is one. A sweep the network refused is finished: it failed.

func (*State) KeepPayments

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

KeepPayments drops every payment whose transaction is not among unmined, the transactions whose change the pool still holds unproven: once a payment mined, its change is an ordinary coin and the record is done with. It reports whether anything was dropped.

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.

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) PendingTreeOf

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

PendingTreeOf finds the record of the tree txid.

func (*State) ReceivedByTxid

func (s *State) ReceivedByTxid(txid string) *Received

ReceivedByTxid finds a received envelope.

func (*State) Remember

func (s *State) Remember(r Received) *Received

Remember records a received envelope, or updates the record it has.

func (*State) Save

func (s *State) Save() error

Save writes the state atomically at mode 0600.

func (*State) SentByTxid

func (s *State) SentByTxid(txid string) *Sent

SentByTxid finds a sent envelope.

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 Ahead keeps its record until it is adopted, and is settled.

type Sweep

type Sweep struct {
	Tree    string   `json:"tree"`
	Vouts   []uint32 `json:"vouts"`
	Offices []string `json:"offices"`
	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  string `json:"rawHex"`
	// BumpHex and Height are set once it mined.
	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, so a
	// sweep the network refuses can give it back.
	Fee *bwallet.Output `json:"fee,omitempty"`
	// Failed is why the network refused it, once it did: it is no longer
	// in flight, retracts nothing, and the outputs it named may be swept
	// again.
	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.

Jump to

Keyboard shortcuts

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