Documentation
¶
Overview ¶
Package module is the engine-neutral contract an application's overlay module satisfies in Go: the topic managers and lookup services an overlay host mounts, the payloads the host hands them, and the host they are built against. It is the Go twin of the TypeScript package's engine-types (ts/src/engine-types.ts).
A module never imports an engine. It is written against these shapes, expressed in go-sdk's own types (a parsed BEEF, an outpoint, admittance instructions, a lookup question and answer), and an engine's host adapts them to whatever that engine calls. So the same module runs on any Go overlay host that carries the adapter, and a widening of an engine's interface cannot break a module's build.
The twins ¶
The TypeScript contract is declared structurally and marks some members optional. Go has neither, so an optional member is an optional interface a host asserts for, and a union payload is one struct with a Mode field that says which members are set.
TypeScript (engine-types.ts) Go (this package)
AdmittanceInstructions overlay.AdmittanceInstructions (go-sdk)
TopicManager TopicManager
identifyAdmissibleOutputs IdentifyAdmissibleOutputs
identifyNeededInputs (optional) InputIdentifier.IdentifyNeededInputs
getDocumentation GetDocumentation
getMetaData GetMetaData (*overlay.MetaData)
OutputAdmittedByTopic OutputAdmittedByTopic
mode Mode (AdmissionMode)
txid, outputIndex Outpoint (transaction.Outpoint)
topic Topic
satoshis Satoshis
lockingScript LockingScript
atomicBEEF AtomicBEEF
offChainValues (optional) OffChainValues (nil when absent)
OutputSpent OutputSpent
mode Mode (SpendNotificationMode)
txid, outputIndex Outpoint
topic Topic
spendingTxid SpendingTxid
inputIndex InputIndex
unlockingScript UnlockingScript
sequenceNumber SequenceNumber
spendingAtomicBEEF SpendingAtomicBEEF
offChainValues (optional) OffChainValues (nil when absent)
LookupFormula []lookup.LookupFormula (go-sdk)
LookupQuestion *lookup.LookupQuestion (go-sdk)
LookupServiceMetaData *overlay.MetaData (go-sdk)
LookupService LookupService
admissionMode AdmissionMode()
spendNotificationMode SpendNotificationMode()
outputAdmittedByTopic OutputAdmittedByTopic
outputSpent (optional) SpendListener.OutputSpent
outputNoLongerRetainedInHistory RetentionListener.OutputNoLongerRetainedInHistory
(optional)
outputEvicted OutputEvicted
lookup Lookup (*lookup.LookupAnswer)
getDocumentation GetDocumentation
getMetaData GetMetaData
restore (optional) Restorer.Restore
Output Output
txid, outputIndex Outpoint
outputScript OutputScript
satoshis Satoshis
topic Topic
spent Spent
outputsConsumed OutputsConsumed
consumedBy ConsumedBy
beef (optional) Beef (nil when absent)
blockHeight (optional) BlockHeight (0 when unmined)
score (optional) Score
ModuleHost Host
log Log
metrics.inc Metrics().Inc
metrics.preset Metrics().Preset
metrics.gauge Metrics().Gauge
RestoreStorage RestoreStorage
findOutput FindOutput
Module Module
topics Topics
lookups Lookups
default export (host) => Module Factory
Two TypeScript arguments have no Go member. identifyAdmissibleOutputs takes offChainValues and a mode on the TypeScript side; a Go engine hands a manager neither, so a module that admits on either is not portable and does not exist. The TypeScript lookupFormula's context has no go-sdk field either; no module sets one.
A lookup answer's metadata (name, shortDescription, iconURL, version, informationURL) and admittance instructions (outputsToAdmit, coinsToRetain, coinsRemoved) cross the wire as JSON, and go-sdk's types carry exactly the TypeScript field names as their JSON tags; the package's test holds them to it, and holds this table to the TypeScript source.
Go only ¶
Some members have no TypeScript twin, because the TypeScript host is frozen and never needed them: WithSubmission and Submission (the submission's bytes a wire-form manager decides on), HydratedOutput (an output's context on a self-hydrated answer), and Replayer with Ask, ReplayRequest, ReplayObject and ReplayAnswer (ordered replay, by which a chain application's lookup service repairs its host's gaps from a peer through the application's own questions; see replay.go and package github.com/lightwebinc/bcommon/replay). None is in the table above, and the test does not ask for one.
Index ¶
- Constants
- Variables
- func Submission(ctx context.Context) ([]byte, bool)
- func WithSubmission(ctx context.Context, raw []byte) context.Context
- type AdmissionMode
- type Ask
- type Factory
- type Host
- type HydratedOutput
- type InputIdentifier
- type LookupService
- type Metrics
- type Module
- type Output
- type OutputAdmittedByTopic
- type OutputSpent
- type ReplayAnswer
- type ReplayObject
- type ReplayRequest
- type Replayer
- type RestoreStorage
- type Restorer
- type RetentionListener
- type SpendListener
- type SpendNotificationMode
- type TopicManager
Constants ¶
const ( DefaultReplayQuestions = 256 DefaultReplayObjects = 256 )
The bounds of one call when a request names none.
Variables ¶
var ErrNoReplay = errors.New("module: the topic is not repaired by replay")
ErrNoReplay is a replayer's answer for a topic it does not repair.
var ErrRefused = errors.New("module: the peer refused the question")
ErrRefused wraps a peer's refusal of a question: the peer answered, and the answer was an error.
Functions ¶
func Submission ¶ added in v0.28.0
Submission is the submission's bytes as they arrived, when the host put them in ctx (WithSubmission).
func WithSubmission ¶ added in v0.28.0
WithSubmission carries a submission's bytes, exactly as they arrived at the host, in ctx for the topic managers the host then calls; Submission reads them. A host that verifies and parses the bytes before a manager runs still hands them whole, because some managers decide on the wire form (which transactions a BEEF carries, in what order, with which proofs), which a parsed BEEF, merged with what the host already held, no longer shows. The TypeScript engine hands a manager the bytes themselves; this is the Go host's way of doing the same.
Types ¶
type AdmissionMode ¶
type AdmissionMode string
AdmissionMode is the shape a lookup service wants an admitted output in.
const ( // AdmitLockingScript: the host hands the output's locking script, // satoshis and outpoint. AdmitLockingScript AdmissionMode = "locking-script" // AdmitWholeTx: the host hands the whole transaction as Atomic BEEF. AdmitWholeTx AdmissionMode = "whole-tx" )
The admission modes.
type Ask ¶ added in v0.31.0
type Ask func(ctx context.Context, q *lookup.LookupQuestion) (*lookup.LookupAnswer, error)
Ask asks one question of a peer's lookup service and answers as the peer's BRC-24 route does: an output-list answer's outputs with their BEEF, and, when the peer gives contexts, Result set to a []HydratedOutput. A question the peer answers with a refusal is an error wrapping ErrRefused; any other error is the peer not answering.
type Host ¶
Host is what a module is built against: the host's log and metrics. A module keeps no logger or registry of its own.
type HydratedOutput ¶ added in v0.27.0
HydratedOutput is one output of an answer a lookup service hydrated itself: its BEEF (Atomic BEEF), its index, and the context the class gives it (nil when none). A service that must put a context on an output (the TypeScript engine carries a formula's context to the output; go-sdk's OutputListItem has no such member) answers a LookupAnswer of type output-list with Outputs filled as usual and Result set to a []HydratedOutput; a host that serves the answer over HTTP renders Result when it is one, and Outputs otherwise. An engine passes an output-list answer through untouched, so the convention costs it nothing.
type InputIdentifier ¶
type InputIdentifier interface {
IdentifyNeededInputs(ctx context.Context, beef *transaction.Beef, txid *chainhash.Hash) ([]*transaction.Outpoint, error)
}
InputIdentifier is a TopicManager that can name inputs the host must fetch before admission (TypeScript identifyNeededInputs). A manager that needs none leaves it out.
type LookupService ¶
type LookupService interface {
// AdmissionMode is the shape OutputAdmittedByTopic arrives in.
AdmissionMode() AdmissionMode
// SpendNotificationMode is how much OutputSpent carries. A service
// that answers SpendNone need not be a SpendListener.
SpendNotificationMode() SpendNotificationMode
// OutputAdmittedByTopic indexes one admitted output.
OutputAdmittedByTopic(ctx context.Context, payload *OutputAdmittedByTopic) error
// OutputEvicted removes an output from every index; it must never be
// answered again.
OutputEvicted(ctx context.Context, outpoint *transaction.Outpoint) error
// Lookup answers one question.
Lookup(ctx context.Context, question *lookup.LookupQuestion) (*lookup.LookupAnswer, error)
// GetDocumentation is the service's documentation, Markdown.
GetDocumentation() string
// GetMetaData names the service.
GetMetaData() *overlay.MetaData
}
LookupService indexes admitted outputs and answers questions about them.
type Metrics ¶
type Metrics interface {
// Inc adds by to a counter, creating it on first use.
Inc(name string, labels map[string]string, by float64)
// Preset creates a counter series at zero, so an alert over it finds a
// series before the first event.
Preset(name string, labels map[string]string)
// Gauge registers a gauge read from fn at each scrape.
Gauge(name string, fn func() float64)
}
Metrics is a module's view of the host's metrics. Names are the module's own, under its own prefix; the host owns the registry.
type Module ¶
type Module struct {
Topics map[string]TopicManager
Lookups map[string]LookupService
}
Module is what a factory returns: topic managers keyed by topic name and lookup services keyed by service name. Either map may be empty.
type Output ¶
type Output struct {
Outpoint transaction.Outpoint
OutputScript *script.Script
Satoshis uint64
Topic string
Spent bool
OutputsConsumed []*transaction.Outpoint
ConsumedBy []*transaction.Outpoint
Beef []byte
BlockHeight uint32
Score float64
}
Output is one row of a host's outputs table, as the host hands it to a restore. Beef is nil when the host did not include it; BlockHeight is zero while the output's transaction is unmined.
type OutputAdmittedByTopic ¶
type OutputAdmittedByTopic struct {
Mode AdmissionMode
Outpoint transaction.Outpoint
Topic string
Satoshis uint64
LockingScript *script.Script
AtomicBEEF []byte
OffChainValues []byte
}
OutputAdmittedByTopic is what a lookup service is told when a topic manager admits an output. Mode says which members are set: under AdmitLockingScript the outpoint, satoshis and locking script; under AdmitWholeTx the outpoint and the Atomic BEEF. Topic and OffChainValues are set under both.
type OutputSpent ¶
type OutputSpent struct {
Mode SpendNotificationMode
Outpoint transaction.Outpoint
Topic string
SpendingTxid *chainhash.Hash
InputIndex uint32
UnlockingScript *script.Script
SequenceNumber uint32
SpendingAtomicBEEF []byte
OffChainValues []byte
}
OutputSpent is what a lookup service is told when an output it indexed is spent. Mode says which members are set: the outpoint and topic always; SpendingTxid under SpendTxid, SpendScript and SpendWholeTx; InputIndex, UnlockingScript and SequenceNumber under SpendScript; SpendingAtomicBEEF under SpendWholeTx.
type ReplayAnswer ¶ added in v0.31.0
type ReplayAnswer struct {
// Objects are what to submit, in order.
Objects []ReplayObject
// Cursor continues the walk; the host passes it to the next call.
Cursor any
// Done says the walk is over: everything the peer answered is applied
// here, answered in this walk, or counted in Gaps.
Done bool
// Questions is how many questions this call asked of the peer, and
// Refused how many of them the peer refused.
Questions, Refused int
// Gaps counts what the peer holds and this host lacks that the replayer
// could not answer now: a chain's first token whose parent no chain
// source gives, a token whose parent the peer did not answer, a token
// whose parent was offered and is not held. A later walk tries again;
// until then only the application's own fill repairs it.
Gaps int
// Notes say why, a line each, for the host's log.
Notes []string
}
ReplayAnswer is one call's answer.
type ReplayObject ¶ added in v0.31.0
type ReplayObject struct {
// Txid is the subject's.
Txid chainhash.Hash
// BEEF is the submission, exactly as the host admits it: a token in
// the BEEF of the token and its parent, anything else as the peer
// answered it.
BEEF []byte
// Kind says what it is ("token", "entry", "sweep" ...), for the host's
// counts.
Kind string
// After are objects earlier in the same answer that must be admitted
// (or found already applied) before this one is submitted: a token's
// parent token. The host submits nothing whose After it did not admit.
After []chainhash.Hash
}
ReplayObject is one object to submit to the topic.
type ReplayRequest ¶ added in v0.31.0
type ReplayRequest struct {
// Topic is the topic to repair, one the module manages.
Topic string
// Peer names the peer Ask reaches, for the replayer's notes.
Peer string
// Ask reaches the peer's lookup service.
Ask Ask
// Applied reports whether this host's topic has applied a transaction:
// admitted it, or decided it and kept that record. A replayer offers
// nothing the topic applied.
Applied func(ctx context.Context, txid chainhash.Hash) (bool, error)
// Cursor is the previous answer's cursor of the same walk, nil to start
// one. It is the replayer's own: the host passes it back unchanged, in
// the same process, and starts a new walk after an answer that is Done.
Cursor any
// MaxQuestions and MaxObjects bound this call: at most that many
// questions asked of the peer and that many objects answered. Zero or
// less takes DefaultReplayQuestions and DefaultReplayObjects.
MaxQuestions, MaxObjects int
}
ReplayRequest is one call of a Replayer.
type Replayer ¶ added in v0.31.0
type Replayer interface {
// Replay answers the next objects to submit to r.Topic from what the
// peer holds and this host lacks. A topic the replayer does not repair
// is ErrNoReplay.
Replay(ctx context.Context, r *ReplayRequest) (*ReplayAnswer, error)
}
Replayer is a LookupService that repairs its host's gaps on the topics its module manages by ordered replay. It is optional: a host asserts for it on the lookup services of the module that manages a topic, and repairs by replay only the topics whose application forbids an unordered synchronization.
type RestoreStorage ¶
type RestoreStorage interface {
FindOutput(ctx context.Context, outpoint *transaction.Outpoint, topic string) (*Output, error)
}
RestoreStorage is the one storage call a restore may make: the host's own lookup of one output, spent or not. It is what lets a lookup service read an output that is not among the unspent rows it was handed: a spent funding output, with Spent and ConsumedBy saying who spent it. An empty topic means any topic. An output the host does not hold is (nil, nil).
type Restorer ¶
type Restorer interface {
Restore(ctx context.Context, outputs []Output, storage RestoreStorage) (int, error)
}
Restorer is a LookupService that rebuilds its index from the host's unspent rows at startup (TypeScript restore). It answers how many it indexed.
type RetentionListener ¶
type RetentionListener interface {
OutputNoLongerRetainedInHistory(ctx context.Context, outpoint *transaction.Outpoint, topic string) error
}
RetentionListener is a LookupService told when a topic no longer retains an output's history (TypeScript outputNoLongerRetainedInHistory).
type SpendListener ¶
type SpendListener interface {
OutputSpent(ctx context.Context, payload *OutputSpent) error
}
SpendListener is a LookupService told of spends (TypeScript outputSpent).
type SpendNotificationMode ¶
type SpendNotificationMode string
SpendNotificationMode is how much a lookup service wants to hear about a spend of an output it indexed.
const ( // SpendNone: the service is not told of spends. SpendNone SpendNotificationMode = "none" // SpendTxid: the spending transaction's id. SpendTxid SpendNotificationMode = "txid" // SpendScript: the spending input's script, index and sequence. SpendScript SpendNotificationMode = "script" // SpendWholeTx: the spending transaction as Atomic BEEF. SpendWholeTx SpendNotificationMode = "whole-tx" )
The spend notification modes.
type TopicManager ¶
type TopicManager interface {
// IdentifyAdmissibleOutputs answers which outputs of the transaction
// txid names in beef the topic admits, and which of the previous coins
// (the inputs' outpoints the host already holds on this topic, by input
// index) it retains. A transaction that admits nothing is an empty
// answer, never an error: an error is the manager failing, not the
// transaction.
IdentifyAdmissibleOutputs(ctx context.Context, beef *transaction.Beef, txid *chainhash.Hash, previousCoins []uint32) (overlay.AdmittanceInstructions, error)
// GetDocumentation is the topic's documentation, Markdown.
GetDocumentation() string
// GetMetaData names the topic.
GetMetaData() *overlay.MetaData
}
TopicManager decides which outputs of a transaction a topic admits.