package chains import "testing" // TestRegtestMagic is the constant that cost the most in this project: a peer // dialling a regtest node with the parent's magic completes the key exchange and // then has every packet dropped. func TestRegtestMagic(t *testing.T) { rt := Regtest() // The profile carries the magic as the bytes on the wire, and the wire // magic is btcd's reading of the same four bytes. The exported value is the // wire magic, because that is what a harness hands to a btcd type. if rt.Profile.Magic != regtestMagicWire { t.Fatalf("regtest profile magic is %x, the wire order is %x", rt.Profile.Magic, regtestMagicWire) } if got := uint32(rt.WireMagic()); got != regtestMagic { t.Fatalf("regtest wire magic is %x, the node's is %x", got, regtestMagic) } if uint32(rt.Exported.Magic) != regtestMagic { t.Fatalf("the exported magic is %x, the node's is %x", rt.Exported.Magic, regtestMagic) } g, err := rt.Genesis() if err != nil { t.Fatal(err) } t.Logf("regtest genesis %v, magic %x", g.BlockHash(), rt.Profile.Magic) } // TestRegtestNamedChain keeps the chain's name when a caller renames it: an // instance started as xbt on regtest must still carry the fork's name. func TestRegtestNamedChain(t *testing.T) { rt := Regtest() rt.Name = Fork if rt.Name != Fork { t.Fatal("the name did not take") } if rt.Profile.Magic != regtestMagicWire { t.Fatalf("renaming changed the magic to %x", rt.Profile.Magic) } } // TestCoinbaseMaturityIsPerChain: maturity is a consensus rule, it is not one // number, and it is not the same on both chains. The fork's numbers are its own // chainparams, quoted in the comment beside them. func TestCoinbaseMaturityIsPerChain(t *testing.T) { btc, err := ParentChain() if err != nil { t.Fatal(err) } if btc.Exported.Maturity.Scheduled() { t.Fatalf("btc schedules a long-maturity window: %+v", btc.Exported.Maturity) } if got := btc.Exported.Maturity.RequiredAt(0, 1_000_000); got != 100 { t.Fatalf("btc requires %d blocks of a coinbase, Bitcoin's rule is 100", got) } xbt, err := ForkChain() if err != nil { t.Fatal(err) } m := xbt.Exported.Maturity want := MaturityRule{Base: 100, Long: 6480, LongStart: 973440, LongEnforce: 973440, LongRelease: 979920} if m != want { t.Fatalf("the fork's maturity is %+v, its chainparams say %+v", m, want) } // The rule's asymmetry, checked at the boundaries the node checks. cases := []struct { coin, spend, want uint32 why string }{ {973439, 973439 + 100, 100, "a coinbase from before the window keeps the base requirement"}, {973439, 973440, 100, "the window does not raise a coinbase created before it"}, {973440, 973440 + 100, 6480, "a coinbase from the window's first height needs the long requirement"}, {973440, 973440 + 6479, 6480, "and one block short of it is still short"}, {973440, 979919, 6480, "the last block inside the window still enforces it"}, {973440, 979920, 100, "the release height drops back to the base requirement"}, {979920, 979920 + 100, 100, "a coinbase from after the window needs only the base"}, } for _, c := range cases { if got := m.RequiredAt(c.coin, c.spend); got != c.want { t.Fatalf("RequiredAt(%d, %d) = %d, want %d: %s", c.coin, c.spend, got, c.want, c.why) } } if rt := Regtest(); rt.Exported.Maturity.Scheduled() { t.Fatalf("regtest schedules a window the rig's node does not: %+v", rt.Exported.Maturity) } }