package gate import ( "bytes" "context" "encoding/json" "path/filepath" "strconv" "testing" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/filler" "git.smesh.lol/btcd-lite/internal/process" "git.smesh.lol/btcd-lite/internal/service" "git.smesh.lol/btcd-lite/internal/staging" "git.smesh.lol/btcd-lite/internal/store" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/utxobuild" "git.smesh.lol/btcd-lite/internal/wire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // TestUTXOSetMatchesTheNode builds a UTXO chain out of the bodies the filler // pulled from a node, and asks the node what it thinks of every outpoint the // build produced. // // This is the plan's phase 6 cross-check in miniature: a checkpoint's answer // against `gettxout` for the same outpoint, at the tip the checkpoint was // built at. Every coinbase output of every mined block is in the set and the // node agrees it is unspent; an outpoint that was never created is in neither. func TestUTXOSetMatchesTheNode(t *testing.T) { r := nodeOn(t) r.createWallet(t) r.mine(t, 30) tip, err := strconv.Atoi(r.must(t, "getblockcount")) if err != nil { t.Fatal(err) } // The chain's own maturity, not a constant: the node is on the parent chain // here, and the fork states a different number. // The largest requirement the chain can impose at this tip, which is what // decides whether the node would call a coinbase at height h immature. maturity := chains.Regtest().Exported.Maturity.RequiredAt(uint32(tip), uint32(tip)) dir := t.TempDir() headers, err := store.NewHeaderStore(filepath.Join(dir, "headers")) if err != nil { t.Fatal(err) } defer headers.Close() bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatal(err) } defer bodies.Close() peer := "127.0.0.1:" + itoa(r.p2p) synced := syncHeaders(t, headers, peer) prof, err := chains.ByName(chains.Parent) if err != nil { t.Fatal(err) } log, err := staging.Open(staging.Dir(dir, "btc")) if err != nil { t.Fatal(err) } defer log.Close() pool, err := filler.NewPool(filler.PoolConfig{ Chain: "btc", Profile: prof.Profile, Headers: headers, Bodies: bodies, Staging: log, Peers: []filler.Peer{{Addr: peer}}, Logf: t.Logf, }) if err != nil { t.Fatal(err) } pool.Reuse(peer, synced) if err := pool.Run(context.Background()); err != nil { t.Fatalf("fill: %v", err) } _ = pool.Close() if _, err := process.Run(process.Config{ Chain: "btc", Staging: log, Bodies: bodies, Headers: headers, Logf: t.Logf, }); err != nil { t.Fatalf("process: %v", err) } // A checkpoint every ten blocks over what was filled. utxoDir := filepath.Join(dir, "utxo") res, err := utxobuild.Build(utxobuild.Config{ Dir: utxoDir, Interval: 10, From: 0, To: uint32(tip + 1), Blocks: utxobuild.StoreSource{S: bodies, Headers: headers}, Logf: t.Logf, }) if err != nil { t.Fatalf("build: %v", err) } t.Logf("checkpoint chain ends at %d with set %s", tip+1, res.Name) // Every coinbase outpoint of every block, answered by the checkpoint the // build wrote and by the node's gettxout, and required to agree. src := &service.DiskUTXO{Dirs: []string{utxoDir}} set, at, err := src.Set(uint32(tip + 1)) if err != nil { t.Fatalf("set: %v", err) } t.Logf("the set at %d holds %d entries", at, len(set)) agreed, missing := 0, 0 for h := 0; h <= tip; h++ { hash := r.must(t, "getblockhash", strconv.Itoa(h)) blk := r.must(t, "getblock", hash, "2") var decoded struct { Tx []struct { TxID string `json:"txid"` Vout []struct { N uint32 `json:"n"` } `json:"vout"` } `json:"tx"` } if err := json.Unmarshal([]byte(blk), &decoded); err != nil { t.Fatal(err) } txid := decoded.Tx[0].TxID vout := decoded.Tx[0].Vout[0].N inSet := hasOutpoint(set, txid, vout) node := bytes.TrimSpace([]byte(r.must(t, "gettxout", txid, strconv.FormatUint(uint64(vout), 10)))) onNode := len(node) > 0 && string(node) != "null" if inSet == onNode { agreed++ continue } // A coinbase from the last blocks can be absent on the node at its own // tip only because it is not mature yet; anything else is a disagreement // worth stopping on. The window is the chain's own maturity, not a // constant: maturity is a per-chain rule. if uint32(h)+maturity <= uint32(tip) { t.Fatalf("height %d: %s:%d is in the set=%v, on the node=%v", h, txid, vout, inSet, onNode) } missing++ } if agreed < tip+1-int(maturity) { t.Fatalf("%d agreements from %d heights", agreed, tip+1) } t.Logf("%d coinbase outpoints agree with gettxout, %d in the last 100 blocks were not checked", agreed, missing) } // hasOutpoint finds a txid:vout in a sorted set. func hasOutpoint(set []utxo.Entry, txid string, vout uint32) bool { h, err := chainhash.NewHashFromStr(txid) if err != nil { return false } _, ok := utxo.Find(set, wire.OutPoint{Hash: *h, Index: vout}) return ok }