Documentation
¶
Overview ¶
Package recv is beefcat's far end: it reads a stream of carriers and writes a carrier's data only after that carrier has passed every check.
The checks, in order, for each carrier:
- the frame is within bounds, is Atomic BEEF holding no more than a carrier and what it spends, and parses (package guard);
- the transaction comes with no proof of its own, and is a carrier with one beefcat record;
- the payload follows its rules, the carrier cannot be mined, its input is one canonical signature, its lock is the key the named identity derives, and the field signature verifies over the payload;
- the carrier continues the stream: same sender, same stream, the next sequence;
- it spends a beefcat funding output of a transaction that carries its own Merkle path, a path that leads from that transaction, and an output no earlier carrier of the stream spent;
- SPV: the input satisfies that output, and the path leads to a root the reader's own header source holds;
- when asked, the funding output was mined deep enough, and not longer ago than the reader allows, counted in blocks below the header source's tip;
- when asked, the funding output is not spent by another transaction, which is how a sender retracts;
- when the reader keeps a record of the funding outputs it has accepted carriers on, this output is not in it;
- the sender is one the reader trusts, when the reader pinned any.
Everything but 6, 7 and 8 is arithmetic on the bytes received, and 9 on what the reader itself recorded. A carrier that fails is refused with a code from bcommon's verify vocabulary and nothing of it is written; a stream that ends before its signed last carrier is refused as truncated.
Index ¶
Constants ¶
const ( // RefusedTruncated is a stream that ended before its last carrier. RefusedTruncated verify.Code = "REFUSED-TRUNCATED" // RefusedRetracted is a carrier whose funding output another // transaction spent: the sender retracted it, or it was never the // output's one spender. RefusedRetracted verify.Code = "REFUSED-RETRACTED" // RefusedUntrusted is a sender the reader did not pin. RefusedUntrusted verify.Code = "REFUSED-UNTRUSTED" // RefusedStale is a carrier whose funding output was mined longer ago // than the reader allows, or not deep enough yet. RefusedStale verify.Code = "REFUSED-STALE" // RefusedReplay is a carrier the reader accepted before: the same // carrier, on a funding output in the reader's record. RefusedReplay verify.Code = "REFUSED-REPLAY" )
Codes beefcat adds to bcommon's vocabulary.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// Headers is the reader's own header source. Required.
Headers chaintracker.ChainTracker
// Trusted are the identity keys the reader takes data from. Empty
// takes any sender whose carriers verify, and Summary.Pinned is false.
Trusted [][33]byte
// Spends, when set, is asked whether each funding output is spent by
// another transaction.
Spends nodeapi.SpendSource
// MaxAge, when not zero, refuses a carrier whose funding output was
// mined more than that many blocks below the header source's tip.
MaxAge uint32
// MinConf, when above one, refuses a carrier whose funding output has
// fewer confirmations: the block that mined it counts as the first.
MinConf uint32
// Seen, when set, is asked whether each funding output was accepted
// before, and records each one accepted, durably, before that
// carrier's data is written.
Seen Seen
// Workers is how many carriers are checked at once; zero is one per
// core, at most 16. Each holds one carrier, so it also bounds memory.
// With Seen set, as many verified carriers again may be held while
// their records are made durable together.
Workers int
}
Options are a reader's choices.
type Outpoint ¶ added in v0.2.0
type Outpoint = [36]byte
Outpoint names a funding output: its transaction id, as the chain hashes it, and its output index, big-endian.
type Refusal ¶
Refusal is a verdict: the stream did not verify. Carrier is the index of the carrier refused, from 0.
type Seen ¶ added in v0.2.0
type Seen interface {
// Lookup reports the carrier recorded on an output, if any.
Lookup(o Outpoint) (carrier [32]byte, ok bool)
// Add records a carrier accepted on an output mined at height.
Add(o Outpoint, carrier [32]byte, height uint32) error
// Sync makes every record added so far durable.
Sync() error
// Floor is the height below which the record no longer holds outputs:
// one mined below it may have been seen and forgotten.
Floor() uint32
}
Seen is a reader's record of the funding outputs it has accepted carriers on, kept across streams (internal/seen keeps one in a file). Receive calls it from one goroutine.
type Summary ¶
type Summary struct {
// Identity is the sender's identity key, once a carrier named one that
// its signatures bear out.
Identity [33]byte
HaveIdentity bool
// Pinned reports that Identity is one of Options.Trusted.
Pinned bool
Stream [16]byte
// Carriers and Bytes count what was verified and written.
Carriers int
Bytes int64
// Tree and Height are the funding transaction and block of the last
// carrier verified.
Tree string
Height uint32
// Unspent reports that every funding output was checked unspent.
Unspent bool
// AgeChecked reports that the postage's depth was held to MaxAge or
// MinConf. Tip is then the header source's tip the stream was measured
// against, and Age the most blocks any carrier's funding output lay
// below it.
AgeChecked bool
Tip, Age uint32
// Replay reports that every funding output was checked against, and
// recorded in, the reader's record of outputs seen.
Replay bool
}
Summary is what was verified and written.
func Receive ¶
Receive reads a stream from r and writes its data to w. It returns what it verified and wrote, and nil only when the whole stream verified through its signed last carrier. A *Refusal is a verdict; any other error means the reader could not decide (the header source did not answer, or reading or writing failed), which is never a verdict.
A carrier's own checks need nothing of the carriers around it, so they run on several cores, a few carriers ahead of the one being written. What ties carriers together (the sender, the stream, the sequence, the funding outputs already spent) is checked in order, by the one goroutine that writes, so the verdict is the one a reader working a carrier at a time would reach: the first carrier that fails, for the first check it fails.
Receive reads ahead of what it has written, and a read cannot be called off: when it returns early, on a refusal or an error, a read of r may still be in flight and may take bytes that arrive later. So r is spent once Receive is called. A caller does not read it again, and does not hand it to another reader, though it may close it.