1 package verify
2 3 import (
4 "context"
5 "errors"
6 "fmt"
7 )
8 9 // BatchSource is a source that can answer several lookups in one round. A source that cannot is
10 // still usable, but it sees the lookups one at a time, which is the leak the batch exists to
11 // reduce.
12 type BatchSource interface {
13 Source
14 // TxBatch answers every txid in one request. It may return fewer entries than asked; the
15 // engine treats a missing entry as no answer.
16 TxBatch(ctx context.Context, txids [][32]byte) (map[[32]byte]TxFact, error)
17 }
18 19 // Batch is one verification round's worth of lookups.
20 //
21 // The point is that a source sees the whole set at once instead of one leg at a time: a trade's
22 // outpoints do not arrive as a distinguishable pair, and the request size is fixed so the shape of
23 // the trade is not in the request either.
24 type Batch struct {
25 facts *Facts
26 pad int
27 order [][32]byte
28 seen map[[32]byte]bool
29 }
30 31 // NewBatch starts a round. pad is how many dummy lookups are added so every round has the same
32 // size; zero is allowed but then the batch reveals its size.
33 func (f *Facts) NewBatch(pad int) *Batch {
34 if pad < 0 {
35 pad = 0
36 }
37 38 return &Batch{facts: f, pad: pad, seen: map[[32]byte]bool{}}
39 }
40 41 // Add includes a txid in the round. Duplicates are ignored, because asking twice tells a source
42 // that two queries are one.
43 func (b *Batch) Add(txid [32]byte) {
44 if b.seen[txid] {
45 return
46 }
47 48 b.seen[txid] = true
49 b.order = append(b.order, txid)
50 }
51 52 // Len is how many real lookups the round covers.
53 func (b *Batch) Len() int { return len(b.order) }
54 55 // padded is the list actually sent: the real txids followed by a fixed dummy, so the count is
56 // constant and no query in the round stands out.
57 func (b *Batch) padded() [][32]byte {
58 out := make([][32]byte, 0, len(b.order)+b.pad)
59 out = append(out, b.order...)
60 61 var dummy [32]byte
62 63 for i := 0; i < b.pad; i++ {
64 out = append(out, dummy)
65 }
66 67 return out
68 }
69 70 // Answer is one txid's agreed result in the round.
71 type Answer struct {
72 Txid [32]byte
73 Answer TxAnswer
74 Absent bool
75 }
76 77 // Run performs the round and returns an answer for every txid added.
78 //
79 // It fails closed: if a quorum cannot be reached for any txid, or the sources contradict each
80 // other, the whole round fails, because a partial answer invites acting on the part that agreed.
81 func (b *Batch) Run(ctx context.Context) (map[[32]byte]Answer, error) {
82 lookups := b.padded()
83 answers := make(map[[32]byte]Answer, len(b.order))
84 85 for _, source := range b.facts.quorum.Sources {
86 var (
87 facts map[[32]byte]TxFact
88 err error
89 )
90 91 if batched, ok := source.(BatchSource); ok {
92 facts, err = batched.TxBatch(ctx, lookups)
93 } else {
94 facts = map[[32]byte]TxFact{}
95 96 for _, txid := range lookups {
97 fact, oneErr := source.Tx(ctx, txid)
98 99 if oneErr != nil {
100 err = oneErr
101 break
102 }
103 104 facts[txid] = fact
105 }
106 }
107 108 if err != nil {
109 return nil, fmt.Errorf("verify: %s: %w", source.Name(), err)
110 }
111 112 for _, txid := range b.order {
113 fact, ok := facts[txid]
114 115 if !ok {
116 continue
117 }
118 119 aggregate(answers, txid, source.Name(), fact)
120 }
121 }
122 123 for _, txid := range b.order {
124 answer, ok := answers[txid]
125 126 if !ok {
127 return nil, fmt.Errorf("%w: nothing answered for %s", ErrUnverifiable, Hash(txid))
128 }
129 130 if !answer.Answer.Found {
131 return nil, fmt.Errorf("%w: %d source(s) did not find %s",
132 ErrUnverifiable, len(answer.Answer.Dissent), Hash(txid))
133 }
134 135 if len(answer.Answer.Dissent) > 0 {
136 return nil, fmt.Errorf("%w: %s found by %v but contradicted by %v",
137 ErrDisagree, Hash(txid), answer.Answer.Agreed, answer.Answer.Dissent)
138 }
139 140 if len(answer.Answer.Agreed) < b.facts.quorum.Need {
141 return nil, fmt.Errorf("%w: %s has %d of %d agreeing sources, %d needed",
142 ErrUnverifiable, Hash(txid), len(answer.Answer.Agreed),
143 len(b.facts.quorum.Sources), b.facts.quorum.Need)
144 }
145 }
146 147 return answers, nil
148 }
149 150 // aggregate folds one source's answer into the running result for one txid. It applies the same
151 // rules as the single lookup: bytes must hash to the txid, and any contradiction is disagreement.
152 func aggregate(answers map[[32]byte]Answer, txid [32]byte, name string, fact TxFact) {
153 entry, ok := answers[txid]
154 155 if !ok {
156 entry = Answer{Txid: txid}
157 }
158 159 entry.Absent = entry.Absent && fact.Found
160 _ = entry.Absent
161 162 switch {
163 case !fact.Found:
164 if len(entry.Answer.Agreed) > 0 {
165 entry.Answer.Dissent = append(entry.Answer.Dissent, name)
166 } else {
167 entry.Answer.Dissent = append(entry.Answer.Dissent, name)
168 }
169 case fact.Found:
170 if len(fact.Raw) == 0 {
171 entry.Answer.Dissent = append(entry.Answer.Dissent, name)
172 break
173 }
174 175 if got, err := TxidOf(fact.Raw); err != nil || got != txid {
176 entry.Answer.Dissent = append(entry.Answer.Dissent, name)
177 break
178 }
179 180 entry.Answer.Agreed = append(entry.Answer.Agreed, name)
181 entry.Answer.Found = true
182 entry.Answer.Verified = true
183 entry.Answer.Height = fact.Height
184 }
185 186 answers[txid] = entry
187 }
188 189 var _ = errors.New
190