package utxo_test import ( "encoding/hex" "os" "strings" "testing" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestRealBlockTouches: a real regtest block with a spend in it, walked into a // set. The block is the node's own bytes from `getblock ... 0`; the first // transaction is the coinbase and the second spends an output of an earlier // block, which is the case the synthetic tests never had. func TestRealBlockTouches(t *testing.T) { raw, err := os.ReadFile("testdata/regtest-block-105.hex") if err != nil { t.Skip("no captured block") } b, err := wire.ParseBlock(mustHex(t, strings.TrimSpace(string(raw)))) if err != nil { t.Fatalf("parse: %v", err) } if len(b.Txs) != 2 { t.Fatalf("%d transactions", len(b.Txs)) } if b.Txs[0].IsCoinBase() == false { t.Fatal("the first transaction is not the coinbase") } // The set the block is applied to holds the outpoint it spends. spent := b.Txs[1].TxIn[0].PreviousOutPoint prior := []utxo.Entry{{OutPoint: spent, Value: 5_000_000_000, Height: 104}} lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(prior, op) }) ts, err := utxo.BlockTouches(b, 105, lookup) if err != nil { t.Fatalf("touches: %v", err) } var created, spends int for _, tc := range ts { if tc.Spent { spends++ if tc.OutPoint != spent { t.Fatalf("a spend of %s, expected only %s", tc.OutPoint, spent) } continue } created++ if tc.New.Height != 105 { t.Fatalf("created entry at height %d", tc.New.Height) } if tc.New.Coinbase != (tc.New.OutPoint.Hash == b.Txs[0].TxID()) { t.Fatalf("the coinbase flag is wrong for %s", tc.New.OutPoint) } } if spends != 1 || created != len(b.Txs[0].TxOut)+len(b.Txs[1].TxOut) { t.Fatalf("%d spends, %d created", spends, created) } after, err := utxo.ApplyRange(prior, ts) if err != nil { t.Fatalf("apply: %v", err) } if _, ok := utxo.Find(after, spent); ok { t.Fatal("the spent outpoint survived the range") } if err := utxo.Validate(after); err != nil { t.Fatalf("the resulting set is not sorted: %v", err) } t.Logf("block 105: %d created, %d spent, %d entries after", created, spends, len(after)) } func mustHex(t *testing.T, s string) []byte { t.Helper() b, err := hex.DecodeString(s) if err != nil { t.Fatal(err) } return b }