package verify import ( "context" "errors" "testing" "git.smesh.lol/tinywallet/pkg/tx" ) type batchFake struct { name string raw []byte size int asked [][32]byte } func (b *batchFake) Name() string { return b.name } func (b *batchFake) Tx(context.Context, [32]byte) (TxFact, error) { return TxFact{}, errors.New("not used: this source is batched") } func (b *batchFake) Tip(context.Context) (Tip, error) { return Tip{}, nil } func (b *batchFake) FeeEstimate(context.Context) (int64, error) { return 1, nil } func (b *batchFake) TxBatch(_ context.Context, txids [][32]byte) (map[[32]byte]TxFact, error) { b.size = len(txids) b.asked = append([][32]byte{}, txids...) out := map[[32]byte]TxFact{} for _, txid := range txids { if txid == [32]byte{} { continue } out[txid] = TxFact{Found: true, Raw: b.raw, Height: 800} } return out, nil } func TestBatchRunsOneFixedSizeRoundAndAgrees(t *testing.T) { built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 3, Script: []byte{0x51}}) raw := rawOf(t, built) txid, err := TxidOf(raw) if err != nil { t.Fatal(err) } one := &batchFake{name: "one", raw: raw} two := &batchFake{name: "two", raw: raw} engine, err := NewFacts([]Source{one, two}, 2) if err != nil { t.Fatal(err) } batch := engine.NewBatch(3) batch.Add(txid) batch.Add(txid) // duplicate, must not be asked twice answers, err := batch.Run(context.Background()) if err != nil { t.Fatal(err) } if len(answers) != 1 { t.Fatalf("answers %d, want 1", len(answers)) } if one.size != 1+3 { t.Fatalf("the source was asked %d lookups, want the txid plus 3 padding", one.size) } if len(one.asked) != 1+3 { t.Fatalf("asked %d", len(one.asked)) } // The padding is the zero txid, which a source must treat as a miss, so the batch cannot // be distinguished by size. if one.asked[1] != ([32]byte{}) { t.Fatal("the padding was not the fixed dummy txid") } } func TestBatchFailsWhenOneSourceContradicts(t *testing.T) { built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 3, Script: []byte{0x51}}) raw := rawOf(t, built) txid, err := TxidOf(raw) if err != nil { t.Fatal(err) } engine, err := NewFacts([]Source{ &batchFake{name: "one", raw: raw}, &batchFake{name: "two", raw: rawOf(t, buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 4, Script: []byte{0x51}}))}, }, 2) if err != nil { t.Fatal(err) } batch := engine.NewBatch(0) batch.Add(txid) if _, err := batch.Run(context.Background()); !errors.Is(err, ErrDisagree) { t.Fatalf("err %v, want ErrDisagree", err) } }