package domain import ( "encoding/hex" "os" "strings" "testing" ) // loadBlock reads the regtest block 1 fixture, which is the same block pkg/block pins its // parser against. func loadBlock(t *testing.T) []byte { t.Helper() text, err := os.ReadFile("testdata/block1.hex") if err != nil { t.Fatal(err) } raw, err := hex.DecodeString(strings.TrimSpace(string(text))) if err != nil { t.Fatal(err) } return raw } // TestScanBlockAddsNothingItDoesNotOwn: a valid block whose outputs pay somebody else leaves the // coin table alone. func TestScanBlockAddsNothingItDoesNotOwn(t *testing.T) { r := newRig(t) if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil { t.Fatal(err) } added, err := r.domain.ScanBlock("btc", loadBlock(t), 1) if err != nil { t.Fatal(err) } if added != 0 { t.Fatalf("%d coins were added for a block that pays somebody else", added) } coins, err := r.domain.Unspent() if err != nil { t.Fatal(err) } if len(coins) != 0 { t.Fatalf("the table holds %d coins", len(coins)) } } // TestScanBlockRefusesAFabricatedBlock: the merkle root is the only thing tying bytes to a // header, so a block that fails it must add nothing. func TestScanBlockRefusesAFabricatedBlock(t *testing.T) { r := newRig(t) if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil { t.Fatal(err) } raw := loadBlock(t) raw[81] ^= 0xff if _, err := r.domain.ScanBlock("btc", raw, 1); err == nil { t.Fatal("a fabricated block was accepted") } } // TestScanBlockNeedsAWallet: there are no scripts to match before setup. func TestScanBlockNeedsAWallet(t *testing.T) { r := newRig(t) if _, err := r.domain.ScanBlock("btc", loadBlock(t), 1); err == nil { t.Fatal("a block was scanned for a wallet that does not exist") } }