package verify import ( "context" "errors" "fmt" ) // BatchSource is a source that can answer several lookups in one round. A source that cannot is // still usable, but it sees the lookups one at a time, which is the leak the batch exists to // reduce. type BatchSource interface { Source // TxBatch answers every txid in one request. It may return fewer entries than asked; the // engine treats a missing entry as no answer. TxBatch(ctx context.Context, txids [][32]byte) (map[[32]byte]TxFact, error) } // Batch is one verification round's worth of lookups. // // The point is that a source sees the whole set at once instead of one leg at a time: a trade's // outpoints do not arrive as a distinguishable pair, and the request size is fixed so the shape of // the trade is not in the request either. type Batch struct { facts *Facts pad int order [][32]byte seen map[[32]byte]bool } // NewBatch starts a round. pad is how many dummy lookups are added so every round has the same // size; zero is allowed but then the batch reveals its size. func (f *Facts) NewBatch(pad int) *Batch { if pad < 0 { pad = 0 } return &Batch{facts: f, pad: pad, seen: map[[32]byte]bool{}} } // Add includes a txid in the round. Duplicates are ignored, because asking twice tells a source // that two queries are one. func (b *Batch) Add(txid [32]byte) { if b.seen[txid] { return } b.seen[txid] = true b.order = append(b.order, txid) } // Len is how many real lookups the round covers. func (b *Batch) Len() int { return len(b.order) } // padded is the list actually sent: the real txids followed by a fixed dummy, so the count is // constant and no query in the round stands out. func (b *Batch) padded() [][32]byte { out := make([][32]byte, 0, len(b.order)+b.pad) out = append(out, b.order...) var dummy [32]byte for i := 0; i < b.pad; i++ { out = append(out, dummy) } return out } // Answer is one txid's agreed result in the round. type Answer struct { Txid [32]byte Answer TxAnswer Absent bool } // Run performs the round and returns an answer for every txid added. // // It fails closed: if a quorum cannot be reached for any txid, or the sources contradict each // other, the whole round fails, because a partial answer invites acting on the part that agreed. func (b *Batch) Run(ctx context.Context) (map[[32]byte]Answer, error) { lookups := b.padded() answers := make(map[[32]byte]Answer, len(b.order)) for _, source := range b.facts.quorum.Sources { var ( facts map[[32]byte]TxFact err error ) if batched, ok := source.(BatchSource); ok { facts, err = batched.TxBatch(ctx, lookups) } else { facts = map[[32]byte]TxFact{} for _, txid := range lookups { fact, oneErr := source.Tx(ctx, txid) if oneErr != nil { err = oneErr break } facts[txid] = fact } } if err != nil { return nil, fmt.Errorf("verify: %s: %w", source.Name(), err) } for _, txid := range b.order { fact, ok := facts[txid] if !ok { continue } aggregate(answers, txid, source.Name(), fact) } } for _, txid := range b.order { answer, ok := answers[txid] if !ok { return nil, fmt.Errorf("%w: nothing answered for %s", ErrUnverifiable, Hash(txid)) } if !answer.Answer.Found { return nil, fmt.Errorf("%w: %d source(s) did not find %s", ErrUnverifiable, len(answer.Answer.Dissent), Hash(txid)) } if len(answer.Answer.Dissent) > 0 { return nil, fmt.Errorf("%w: %s found by %v but contradicted by %v", ErrDisagree, Hash(txid), answer.Answer.Agreed, answer.Answer.Dissent) } if len(answer.Answer.Agreed) < b.facts.quorum.Need { return nil, fmt.Errorf("%w: %s has %d of %d agreeing sources, %d needed", ErrUnverifiable, Hash(txid), len(answer.Answer.Agreed), len(b.facts.quorum.Sources), b.facts.quorum.Need) } } return answers, nil } // aggregate folds one source's answer into the running result for one txid. It applies the same // rules as the single lookup: bytes must hash to the txid, and any contradiction is disagreement. func aggregate(answers map[[32]byte]Answer, txid [32]byte, name string, fact TxFact) { entry, ok := answers[txid] if !ok { entry = Answer{Txid: txid} } entry.Absent = entry.Absent && fact.Found _ = entry.Absent switch { case !fact.Found: if len(entry.Answer.Agreed) > 0 { entry.Answer.Dissent = append(entry.Answer.Dissent, name) } else { entry.Answer.Dissent = append(entry.Answer.Dissent, name) } case fact.Found: if len(fact.Raw) == 0 { entry.Answer.Dissent = append(entry.Answer.Dissent, name) break } if got, err := TxidOf(fact.Raw); err != nil || got != txid { entry.Answer.Dissent = append(entry.Answer.Dissent, name) break } entry.Answer.Agreed = append(entry.Answer.Agreed, name) entry.Answer.Found = true entry.Answer.Verified = true entry.Answer.Height = fact.Height } answers[txid] = entry } var _ = errors.New