package relay import ( "context" "testing" "time" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" "git.smesh.lol/btcd-lite/internal/filter" "git.smesh.lol/btcd-lite/internal/store" "git.smesh.lol/btcd-lite/internal/wire" ) // TestFollowSettlesFromTheStore: a header store with a filter index and a // window holding a transaction whose script the block funds. Follow must take // the entry to Seen from the stored filter, and to Spent when the store's // answer says the inputs went there. func TestFollowSettlesFromTheStore(t *testing.T) { dir := t.TempDir() headers, err := store.NewHeaderStore(dir) if err != nil { t.Fatal(err) } defer headers.Close() genesis := btcwire.BlockHeader{} anchor := neutrino.Anchor{Height: 0, BlockHash: genesis.BlockHash(), FilterHeight: 0} if err := headers.SetAnchor(anchor); err != nil { t.Fatal(err) } script := []byte{0x51, 0x33} raw, tx := mkTx(5, script) blk := filterTestBlock(script) // The filter is keyed by the block's own hash, and the header record the // store holds must be that block's header for the match to mean anything. var hdr btcwire.BlockHeader // The header record and the block's own 80 bytes are the same header: the // fixture derives one from the other and puts the previous block in both, // so the hash the filter is keyed by is the hash the store reports. derived := btcwire.BlockHeader{PrevBlock: genesis.BlockHash(), MerkleRoot: blk.MerkleRoot(), Timestamp: 1} enc := derived.Serialize() copy(blk.Header[:], enc[:]) if err := hdr.Deserialize(blk.Header[:]); err != nil { t.Fatal(err) } hdr.PrevBlock = genesis.BlockHash() // The records run from the anchor height, so the genesis header is the // first record and the block that funds the script is the second. if err := headers.PutHeaders(0, []btcwire.BlockHeader{genesis}); err != nil { t.Fatal(err) } if err := headers.PutHeaders(1, []btcwire.BlockHeader{hdr}); err != nil { t.Fatal(err) } flt, err := filter.Build(blk, nil) if err != nil { t.Fatal(err) } fh := filter.Hash(flt) fhdr := filter.Fold(fh, anchor.FilterHeader) if err := headers.PutFilterHeaders(1, []chainhash.Hash{fh}, []chainhash.Hash{fhdr}); err != nil { t.Fatal(err) } if err := headers.PutFilter(1, hdr.BlockHash(), fh, flt); err != nil { t.Fatal(err) } w := NewWindow(1 << 20) w.Add(&Entry{TxID: tx.TxID(), Raw: raw, Scripts: ScriptsOf(tx), Inputs: InputsOf(tx), Size: len(raw)}) r, err := New(RelayConfig{Profile: &chain.Profile{Name: "x", Magic: 0xDAB5BFFA}, Window: w, Peers: []string{"127.0.0.1:1"}}) if err != nil { t.Fatal(err) } // One pass of the loop, driven by a context that ends after the ticker's // interval. spent := LookupFromSet(map[wire.OutPoint]uint32{tx.TxIn[0].PreviousOutPoint: 1}) go r.Follow(context.Background(), headers, spent, 5*time.Millisecond) deadline := time.Now().Add(2 * time.Second) for time.Now().Before(deadline) { if e, ok := w.Get(tx.TxID()); ok && e.State == Spent { return } time.Sleep(10 * time.Millisecond) } e, _ := w.Get(tx.TxID()) t.Fatalf("the entry never settled: %+v", e) }