twochain_test.go raw

   1  package gate
   2  
   3  import (
   4  	"context"
   5  	"path/filepath"
   6  	"testing"
   7  
   8  	"git.smesh.lol/btcd-lite/internal/chains"
   9  	"git.smesh.lol/btcd-lite/internal/filler"
  10  	"git.smesh.lol/btcd-lite/internal/process"
  11  	"git.smesh.lol/btcd-lite/internal/serve"
  12  	"git.smesh.lol/btcd-lite/internal/staging"
  13  	"git.smesh.lol/btcd-lite/internal/store"
  14  	"git.smesh.lol/btcd-lite/internal/v2peer"
  15  )
  16  
  17  // fillBodies runs a pool for one chain over a connection the header sync
  18  // already holds, then processes the staging log, which is the two phases an
  19  // instance runs.
  20  func fillBodies(t *testing.T, data, chainName string, headers *store.HeaderStore, bodies *store.BlockStore, addr string, conn *v2peer.Peer) {
  21  	t.Helper()
  22  	log, err := staging.Open(staging.Dir(data, chainName))
  23  	if err != nil {
  24  		t.Fatalf("%s staging: %v", chainName, err)
  25  	}
  26  	defer log.Close()
  27  	pool, err := filler.NewPool(filler.PoolConfig{
  28  		Chain:   chainName,
  29  		Profile: chains.Regtest().Profile,
  30  		Headers: headers,
  31  		Bodies:  bodies,
  32  		Staging: log,
  33  		Peers:   []filler.Peer{{Addr: addr}},
  34  		Logf:    t.Logf,
  35  	})
  36  	if err != nil {
  37  		t.Fatalf("%s pool: %v", chainName, err)
  38  	}
  39  	pool.Reuse(addr, conn)
  40  	if err := pool.Run(context.Background()); err != nil {
  41  		t.Fatalf("%s fill: %v", chainName, err)
  42  	}
  43  	if err := pool.Close(); err != nil {
  44  		t.Fatalf("%s close: %v", chainName, err)
  45  	}
  46  	if _, err := process.Run(process.Config{
  47  		Chain: chainName, Staging: log, Bodies: bodies, Headers: headers, Logf: t.Logf,
  48  	}); err != nil {
  49  		t.Fatalf("%s process: %v", chainName, err)
  50  	}
  51  }
  52  
  53  // TestOneStoreTwoChains: two chains, one body store.
  54  //
  55  // The two chains share a prefix: the second node is started with -connect, syncs
  56  // the first one's 21 heights and then mines its own block on top. That is the
  57  // only way to have a shared prefix on this machine, because two regtest nodes
  58  // started from scratch agree on genesis and on nothing above it, so a fixture
  59  // built that way would put two bodies at every height and would prove nothing
  60  // about the store.
  61  //
  62  // The claim under test is the plan's: the shared prefix is stored once, and a
  63  // height resolves to a block through each chain's own header store.
  64  func TestOneStoreTwoChains(t *testing.T) {
  65  	a := nodeOn(t)
  66  	a.createWallet(t)
  67  	a.mine(t, 20)
  68  	tipA := a.height(t)
  69  	if tipA != 20 {
  70  		t.Fatalf("the first node is at %d", tipA)
  71  	}
  72  	b := nodeConnectedTo(t, a)
  73  
  74  	dir := t.TempDir()
  75  	headers := map[string]*store.HeaderStore{}
  76  	for _, name := range []string{"btc", "xbt"} {
  77  		hs, err := store.NewHeaderStore(filepath.Join(dir, "headers", name))
  78  		if err != nil {
  79  			t.Fatal(err)
  80  		}
  81  		t.Cleanup(func() { hs.Close() })
  82  		headers[name] = hs
  83  	}
  84  	bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks"))
  85  	if err != nil {
  86  		t.Fatal(err)
  87  	}
  88  	t.Cleanup(func() { bodies.Close() })
  89  
  90  	// The first chain is filled to the shared tip and no further. The second
  91  	// node then mines its own block, so the second chain is one height longer
  92  	// and that height's body exists in one chain only.
  93  	peerA := "127.0.0.1:" + itoa(a.p2p)
  94  	connA := syncHeaders(t, headers["btc"], peerA)
  95  	fillBodies(t, dir, "btc", headers["btc"], bodies, peerA, connA)
  96  	b.createWallet(t)
  97  	b.mine(t, 1)
  98  	tipB := b.height(t)
  99  	if tipB != tipA+1 {
 100  		t.Fatalf("the second node is at %d, the first at %d", tipB, tipA)
 101  	}
 102  	peerB := "127.0.0.1:" + itoa(b.p2p)
 103  	connB := syncHeaders(t, headers["xbt"], peerB)
 104  	fillBodies(t, dir, "xbt", headers["xbt"], bodies, peerB, connB)
 105  
 106  	// 21 heights shared and the second chain's one block of its own.
 107  	wantBodies := tipA + 1 + 1
 108  	if got := bodies.Len(); got != wantBodies {
 109  		t.Fatalf("the store holds %d blocks for %d+%d heights; the shared prefix was copied", got, tipA+1, tipB+1)
 110  	}
 111  	// A shared height is the same block on both chains.
 112  	hdrA, ok := headers["btc"].Header(10)
 113  	if !ok {
 114  		t.Fatal("btc has no header at 10")
 115  	}
 116  	hdrB, ok := headers["xbt"].Header(10)
 117  	if !ok {
 118  		t.Fatal("xbt has no header at 10")
 119  	}
 120  	if hdrA.BlockHash() != hdrB.BlockHash() {
 121  		t.Fatalf("height 10 is %s on btc and %s on xbt", hdrA.BlockHash(), hdrB.BlockHash())
 122  	}
 123  	bodyA, err := bodies.GetBlock(hdrA.BlockHash())
 124  	if err != nil {
 125  		t.Fatalf("the shared body is not stored: %v", err)
 126  	}
 127  	if got := serve.HeaderHash(bodyA); got != hdrA.BlockHash() {
 128  		t.Fatalf("the stored body hashes to %s, not %s", got, hdrA.BlockHash())
 129  	}
 130  	// Each chain's tip is its own: the first stops at the shared height, the
 131  	// second carries the block mined after it.
 132  	tipHeightA, _, _ := headers["btc"].HeaderTip()
 133  	tipHeightB, tipHashB, _ := headers["xbt"].HeaderTip()
 134  	if tipHeightA != uint32(tipA) {
 135  		t.Fatalf("the first chain's tip is %d, the node was at %d", tipHeightA, tipA)
 136  	}
 137  	if tipHeightB != uint32(tipB) {
 138  		t.Fatalf("the second chain's tip is %d, the node was at %d", tipHeightB, tipB)
 139  	}
 140  	if _, err := bodies.GetBlock(tipHashB); err != nil {
 141  		t.Fatalf("the second chain's own block is not stored: %v", err)
 142  	}
 143  
 144  	// A reopen finds the same store and the two chains at their own tips.
 145  	bodies2, err := store.NewBlockStore(filepath.Join(dir, "blocks"))
 146  	if err != nil {
 147  		t.Fatalf("reopen: %v", err)
 148  	}
 149  	defer bodies2.Close()
 150  	if got := bodies2.Len(); got != wantBodies {
 151  		t.Fatalf("after a reopen the store holds %d blocks, not %d", got, wantBodies)
 152  	}
 153  	for _, name := range []string{"btc", "xbt"} {
 154  		hs, err := store.NewHeaderStore(filepath.Join(dir, "headers", name))
 155  		if err != nil {
 156  			t.Fatalf("reopen %s: %v", name, err)
 157  		}
 158  		if _, _, ok := hs.HeaderTip(); !ok {
 159  			t.Fatalf("reopen %s: no tip", name)
 160  		}
 161  		hs.Close()
 162  	}
 163  	t.Logf("one store of %d blocks served both chains: %d shared heights, the second chain one block longer", bodies.Len(), tipA+1)
 164  }
 165