regtest_test.go raw

   1  package chains
   2  
   3  import "testing"
   4  
   5  // TestRegtestMagic is the constant that cost the most in this project: a peer
   6  // dialling a regtest node with the parent's magic completes the key exchange and
   7  // then has every packet dropped.
   8  func TestRegtestMagic(t *testing.T) {
   9  	rt := Regtest()
  10  	// The profile carries the magic as the bytes on the wire, and the wire
  11  	// magic is btcd's reading of the same four bytes. The exported value is the
  12  	// wire magic, because that is what a harness hands to a btcd type.
  13  	if rt.Profile.Magic != regtestMagicWire {
  14  		t.Fatalf("regtest profile magic is %x, the wire order is %x", rt.Profile.Magic, regtestMagicWire)
  15  	}
  16  	if got := uint32(rt.WireMagic()); got != regtestMagic {
  17  		t.Fatalf("regtest wire magic is %x, the node's is %x", got, regtestMagic)
  18  	}
  19  	if uint32(rt.Exported.Magic) != regtestMagic {
  20  		t.Fatalf("the exported magic is %x, the node's is %x", rt.Exported.Magic, regtestMagic)
  21  	}
  22  	g, err := rt.Genesis()
  23  	if err != nil {
  24  		t.Fatal(err)
  25  	}
  26  	t.Logf("regtest genesis %v, magic %x", g.BlockHash(), rt.Profile.Magic)
  27  }
  28  
  29  // TestRegtestNamedChain keeps the chain's name when a caller renames it: an
  30  // instance started as xbt on regtest must still carry the fork's name.
  31  func TestRegtestNamedChain(t *testing.T) {
  32  	rt := Regtest()
  33  	rt.Name = Fork
  34  	if rt.Name != Fork {
  35  		t.Fatal("the name did not take")
  36  	}
  37  	if rt.Profile.Magic != regtestMagicWire {
  38  		t.Fatalf("renaming changed the magic to %x", rt.Profile.Magic)
  39  	}
  40  }
  41  
  42  // TestCoinbaseMaturityIsPerChain: maturity is a consensus rule, it is not one
  43  // number, and it is not the same on both chains. The fork's numbers are its own
  44  // chainparams, quoted in the comment beside them.
  45  func TestCoinbaseMaturityIsPerChain(t *testing.T) {
  46  	btc, err := ParentChain()
  47  	if err != nil {
  48  		t.Fatal(err)
  49  	}
  50  	if btc.Exported.Maturity.Scheduled() {
  51  		t.Fatalf("btc schedules a long-maturity window: %+v", btc.Exported.Maturity)
  52  	}
  53  	if got := btc.Exported.Maturity.RequiredAt(0, 1_000_000); got != 100 {
  54  		t.Fatalf("btc requires %d blocks of a coinbase, Bitcoin's rule is 100", got)
  55  	}
  56  	xbt, err := ForkChain()
  57  	if err != nil {
  58  		t.Fatal(err)
  59  	}
  60  	m := xbt.Exported.Maturity
  61  	want := MaturityRule{Base: 100, Long: 6480, LongStart: 973440, LongEnforce: 973440, LongRelease: 979920}
  62  	if m != want {
  63  		t.Fatalf("the fork's maturity is %+v, its chainparams say %+v", m, want)
  64  	}
  65  	// The rule's asymmetry, checked at the boundaries the node checks.
  66  	cases := []struct {
  67  		coin, spend, want uint32
  68  		why               string
  69  	}{
  70  		{973439, 973439 + 100, 100, "a coinbase from before the window keeps the base requirement"},
  71  		{973439, 973440, 100, "the window does not raise a coinbase created before it"},
  72  		{973440, 973440 + 100, 6480, "a coinbase from the window's first height needs the long requirement"},
  73  		{973440, 973440 + 6479, 6480, "and one block short of it is still short"},
  74  		{973440, 979919, 6480, "the last block inside the window still enforces it"},
  75  		{973440, 979920, 100, "the release height drops back to the base requirement"},
  76  		{979920, 979920 + 100, 100, "a coinbase from after the window needs only the base"},
  77  	}
  78  	for _, c := range cases {
  79  		if got := m.RequiredAt(c.coin, c.spend); got != c.want {
  80  			t.Fatalf("RequiredAt(%d, %d) = %d, want %d: %s", c.coin, c.spend, got, c.want, c.why)
  81  		}
  82  	}
  83  	if rt := Regtest(); rt.Exported.Maturity.Scheduled() {
  84  		t.Fatalf("regtest schedules a window the rig's node does not: %+v", rt.Exported.Maturity)
  85  	}
  86  }
  87