replay

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 3 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 replay is the walk behind a module.Replayer: how a chain application's Go module asks a peer, with the application's own lookup questions, for what its host lacks, and answers the objects to submit in a valid order, bounded per call.

A walk is a list of phases run in order, each a list of items in ascending key order (Phase). The items are computed afresh at every call from what the host holds, since the host admits what one call answered before it makes the next; a phase that answered anything ends the call, so the next phase reads an index that holds it. The cursor (Cursor) carries the phase, the last item finished, a page inside the next item, and what the walk has answered, so that nothing is answered twice in one walk and a walk always ends.

Two kinds of work fill the phases. Tokens repairs one chain of tokens: every seq up to the peer's latest at which the host holds no token, ascending, each token in the BEEF of the token and its parent (TokenBEEF), the parent read from the host, from the peer's answer, or for a chain's first token from a chain source, never a token before its parent. Each answers what one question finds as the peer gave it: the carriers, sweeps and other records a chain's readers need besides the tokens.

This package decides no admission and logs nothing: it answers objects and counts what it could not answer, and the host admits each through its ordinary path.

Index

Constants

View Source
const KindToken = "token"

KindToken is the kind Tokens answers its tokens as.

View Source
const MaxNotes = 16

MaxNotes bounds the notes one answer carries.

View Source
const MaxParentDepth = 16

MaxParentDepth bounds how far Tokens goes down a branch to answer a token's parent before the token itself, when the parent sits at a seq at which the host holds another token.

Variables

View Source
var ErrBudget = errors.New("replay: the call's bound is reached")

ErrBudget is a call's question or object bound reached: the phase stops where it is and the walk goes on at the next call.

Functions

func Run

func Run(ctx context.Context, r *module.ReplayRequest, phases ...Phase) (*module.ReplayAnswer, error)

Run runs a walk's phases from the request's cursor and answers the call. A phase that answered objects ends the call when it finishes, so that the host admits them before the next phase reads the index; the walk is Done once every phase finished.

func TokenBEEF

func TokenBEEF(token, parent *transaction.Transaction) ([]byte, error)

TokenBEEF is the BEEF a host admits a token in, assembled from the token as a host stores it (alone, with its proof) and its parent (chaintoken.TokenBEEF): each path is first cut to its minimal form (chaintoken.Minimal) when it is not, since a stored path is the merged one a token was published with when it mined in its parent's block. Neither transaction is changed.

Types

type Chain

type Chain struct {
	// Name says which chain, in notes.
	Name string
	// Latest are the peer's latest tokens of the chain, when the caller has
	// them already (a listing page's); otherwise LatestQ is asked.
	Latest []*Output
	// LatestQ is the application's question for the chain's latest tokens.
	LatestQ func() (service string, query map[string]string)
	// AtSeq is the application's question for the chain's tokens at seq.
	AtSeq func(seq uint64) (service string, query map[string]string)
	// Held are the txids of the tokens of the chain the host holds at seq.
	Held func(seq uint64) []chainhash.Hash
	// Local reads a token the host holds, as stored (alone, with its
	// proof); nil, nil when it holds none of that txid.
	Local func(ctx context.Context, txid chainhash.Hash) (*transaction.Transaction, error)
	// Read reads a token of this chain from its transaction, an error when
	// the transaction holds none.
	Read func(tx *transaction.Transaction) (*Token, error)
	// Trees gives a first token's parent; nil when the host has no chain
	// source, and a missing first token is then a gap.
	Trees TreeSource
	// Check holds an assembled token BEEF to the application's admission
	// rules before it is answered, so that a peer's bad answer is not
	// submitted; nil leaves it to the host's admission.
	Check func(ctx context.Context, beef []byte) error
}

Chain is one chain of tokens for Tokens to repair.

type Cursor

type Cursor struct {
	// Phase is the index of the phase the walk is in.
	Phase int
	// Item is the key of the last item the phase finished, "" for none.
	Item string
	// Page is where the next item resumes, its own page cursor.
	Page string
	// Sent are the txids the walk has answered, so that nothing is
	// answered twice in one walk: an object the host did not admit waits
	// for the next walk.
	Sent map[chainhash.Hash]bool
	// Found are keys a phase found for a later one (Find): what the peer
	// lists that the host holds nothing of yet, say.
	Found map[string]bool
}

Cursor is a walk's position, carried from one call to the next in module.ReplayAnswer.Cursor. A host treats it as opaque.

type Output

type Output struct {
	Txid    chainhash.Hash
	Tx      *transaction.Transaction
	Vout    uint32
	BEEF    []byte
	Context []byte
}

Output is one output a peer answered: the BEEF it gave, the subject transaction parsed from it (with its proof when mined), the output's index and its context.

type Phase

type Phase struct {
	Name string
	// Items lists the phase's item keys, from what the host holds and what
	// the walk found (Found).
	Items func(ctx context.Context, w *Walk) ([]string, error)
	// Do works on one item. It answers done when the item is finished;
	// ErrBudget (or false) stops the call where it is, and *page says where
	// the item resumes at the next call ("" from its start).
	Do func(ctx context.Context, w *Walk, item string, page *string) (done bool, err error)
}

Phase is one phase of a walk: its items, by key in ascending order, computed at each call from what the host holds, and the work on one.

type Token

type Token struct {
	Seq uint64
	// Prev is the txid of the token it spends, the one at Seq - 1; zero at
	// seq 0.
	Prev chainhash.Hash
	// Tree is, at seq 0, the outpoint of the chain tree it spends: its
	// parent, which no host keeps.
	Tree transaction.Outpoint
}

Token is what Tokens needs of a chain token, read from its transaction alone, as a host stores it.

type TreeSource

type TreeSource interface {
	TxRaw(ctx context.Context, txid string) ([]byte, error)
	Proof(ctx context.Context, txid string) (*transaction.MerklePath, uint32, error)
}

TreeSource gives a transaction's bytes and its proof by txid: a chain source (nodeapi.Asset, nodeapi.WoC and nodeapi.Sources are one). A chain tree comes from one, since no host keeps it and a node drops a transaction once every output is spent, which a chain tree is once each of its outputs paid for a chain's first token.

type Walk

type Walk struct {

	// MaxBEEF bounds the BEEF of each answered output; guard.DefaultBound
	// when zero.
	MaxBEEF int
	// contains filtered or unexported fields
}

Walk is one call of a replayer: the request and its bounds, the cursor it continues, and the objects found, in order.

func New

func New(r *module.ReplayRequest) *Walk

New starts a call on r, continuing the walk its cursor names.

func (*Walk) Ask

func (w *Walk) Ask(ctx context.Context, service string, query map[string]string) ([]*Output, error)

Ask asks the peer one question of the application's lookup service. A refusal is counted and answered as nothing (the item has nothing the walk can use); a peer that does not answer is an error that ends the call; ErrBudget when the call has no question left. An output whose BEEF does not parse is left out, with a note.

func (*Walk) Each

func (w *Walk) Each(ctx context.Context, kind, service string, query map[string]string, keep func(*Output) bool) ([]*Output, error)

Each asks one question and answers every output's transaction the walk wants (Wanted), as the peer gave it, in the peer's order, kind naming them; keep, when not nil, leaves out what it refuses. It returns every output the peer answered, for a caller that pages.

func (*Walk) Emit

func (w *Walk) Emit(o module.ReplayObject) error

Emit answers one object; ErrBudget when the call has no object left.

func (*Walk) Find

func (w *Walk) Find(key string)

Find keeps a key for a later phase of the walk (Found).

func (*Walk) Found

func (w *Walk) Found(prefix string) []string

Found are the keys the walk found that begin with prefix, sorted.

func (*Walk) Gap

func (w *Walk) Gap(note string)

Gap counts something the peer holds that this host lacks and the walk cannot answer now, and why.

func (*Walk) Note

func (w *Walk) Note(note string)

Note keeps a line for the host's log, at most MaxNotes a call.

func (*Walk) Request

func (w *Walk) Request() *module.ReplayRequest

Request is the call's request.

func (*Walk) Room

func (w *Walk) Room() bool

Room reports whether the call can ask one more question and answer one more object.

func (*Walk) Run

func (w *Walk) Run(ctx context.Context, phases ...Phase) (*module.ReplayAnswer, error)

Run is the package's Run on this walk.

func (*Walk) Sent

func (w *Walk) Sent(txid chainhash.Hash) bool

Sent reports whether the walk answered txid, in this call or an earlier one.

func (*Walk) SentNow

func (w *Walk) SentNow(txid chainhash.Hash) bool

SentNow reports whether this call answered txid.

func (*Walk) Tokens

func (w *Walk) Tokens(ctx context.Context, c *Chain, page *string) (done bool, err error)

Tokens repairs one chain: every seq from 0 to the peer's latest at which the host holds no token, ascending, and the peer's latest tokens. Each token there that the walk wants (Wanted) is answered in the BEEF of the token and its parent (TokenBEEF), after its parent: the parent is read from the host, or from the peer's answer at seq - 1 and answered first, or for a first token from the chain source. The chain stops at the first token it cannot answer (counted in Gaps), and at a token whose parent the walk answered and the host did not admit, so no token is ever answered without its parent held or answered before it.

done is false when the call's bound stopped it. *page then keeps the peer's latest seq, so the next call goes on without asking for the latest again (it asks for the tokens at that seq instead) and finds what the host admitted meanwhile; the caller passes the phase's page.

func (*Walk) Wanted

func (w *Walk) Wanted(ctx context.Context, txid chainhash.Hash) (bool, error)

Wanted reports whether txid is one to answer: not answered in this walk and not applied by the host's topic.

Jump to

Keyboard shortcuts

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