package gate import ( "context" "path/filepath" "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/serve" "git.smesh.lol/btcd-lite/internal/staging" "git.smesh.lol/btcd-lite/internal/store" "git.smesh.lol/btcd-lite/internal/v2peer" ) // fillBodies runs a pool for one chain over a connection the header sync // already holds, then processes the staging log, which is the two phases an // instance runs. func fillBodies(t *testing.T, data, chainName string, headers *store.HeaderStore, bodies *store.BlockStore, addr string, conn *v2peer.Peer) { t.Helper() log, err := staging.Open(staging.Dir(data, chainName)) if err != nil { t.Fatalf("%s staging: %v", chainName, err) } defer log.Close() pool, err := filler.NewPool(filler.PoolConfig{ Chain: chainName, Profile: chains.Regtest().Profile, Headers: headers, Bodies: bodies, Staging: log, Peers: []filler.Peer{{Addr: addr}}, Logf: t.Logf, }) if err != nil { t.Fatalf("%s pool: %v", chainName, err) } pool.Reuse(addr, conn) if err := pool.Run(context.Background()); err != nil { t.Fatalf("%s fill: %v", chainName, err) } if err := pool.Close(); err != nil { t.Fatalf("%s close: %v", chainName, err) } if _, err := process.Run(process.Config{ Chain: chainName, Staging: log, Bodies: bodies, Headers: headers, Logf: t.Logf, }); err != nil { t.Fatalf("%s process: %v", chainName, err) } } // TestOneStoreTwoChains: two chains, one body store. // // The two chains share a prefix: the second node is started with -connect, syncs // the first one's 21 heights and then mines its own block on top. That is the // only way to have a shared prefix on this machine, because two regtest nodes // started from scratch agree on genesis and on nothing above it, so a fixture // built that way would put two bodies at every height and would prove nothing // about the store. // // The claim under test is the plan's: the shared prefix is stored once, and a // height resolves to a block through each chain's own header store. func TestOneStoreTwoChains(t *testing.T) { a := nodeOn(t) a.createWallet(t) a.mine(t, 20) tipA := a.height(t) if tipA != 20 { t.Fatalf("the first node is at %d", tipA) } b := nodeConnectedTo(t, a) dir := t.TempDir() headers := map[string]*store.HeaderStore{} for _, name := range []string{"btc", "xbt"} { hs, err := store.NewHeaderStore(filepath.Join(dir, "headers", name)) if err != nil { t.Fatal(err) } t.Cleanup(func() { hs.Close() }) headers[name] = hs } bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatal(err) } t.Cleanup(func() { bodies.Close() }) // The first chain is filled to the shared tip and no further. The second // node then mines its own block, so the second chain is one height longer // and that height's body exists in one chain only. peerA := "127.0.0.1:" + itoa(a.p2p) connA := syncHeaders(t, headers["btc"], peerA) fillBodies(t, dir, "btc", headers["btc"], bodies, peerA, connA) b.createWallet(t) b.mine(t, 1) tipB := b.height(t) if tipB != tipA+1 { t.Fatalf("the second node is at %d, the first at %d", tipB, tipA) } peerB := "127.0.0.1:" + itoa(b.p2p) connB := syncHeaders(t, headers["xbt"], peerB) fillBodies(t, dir, "xbt", headers["xbt"], bodies, peerB, connB) // 21 heights shared and the second chain's one block of its own. wantBodies := tipA + 1 + 1 if got := bodies.Len(); got != wantBodies { t.Fatalf("the store holds %d blocks for %d+%d heights; the shared prefix was copied", got, tipA+1, tipB+1) } // A shared height is the same block on both chains. hdrA, ok := headers["btc"].Header(10) if !ok { t.Fatal("btc has no header at 10") } hdrB, ok := headers["xbt"].Header(10) if !ok { t.Fatal("xbt has no header at 10") } if hdrA.BlockHash() != hdrB.BlockHash() { t.Fatalf("height 10 is %s on btc and %s on xbt", hdrA.BlockHash(), hdrB.BlockHash()) } bodyA, err := bodies.GetBlock(hdrA.BlockHash()) if err != nil { t.Fatalf("the shared body is not stored: %v", err) } if got := serve.HeaderHash(bodyA); got != hdrA.BlockHash() { t.Fatalf("the stored body hashes to %s, not %s", got, hdrA.BlockHash()) } // Each chain's tip is its own: the first stops at the shared height, the // second carries the block mined after it. tipHeightA, _, _ := headers["btc"].HeaderTip() tipHeightB, tipHashB, _ := headers["xbt"].HeaderTip() if tipHeightA != uint32(tipA) { t.Fatalf("the first chain's tip is %d, the node was at %d", tipHeightA, tipA) } if tipHeightB != uint32(tipB) { t.Fatalf("the second chain's tip is %d, the node was at %d", tipHeightB, tipB) } if _, err := bodies.GetBlock(tipHashB); err != nil { t.Fatalf("the second chain's own block is not stored: %v", err) } // A reopen finds the same store and the two chains at their own tips. bodies2, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatalf("reopen: %v", err) } defer bodies2.Close() if got := bodies2.Len(); got != wantBodies { t.Fatalf("after a reopen the store holds %d blocks, not %d", got, wantBodies) } for _, name := range []string{"btc", "xbt"} { hs, err := store.NewHeaderStore(filepath.Join(dir, "headers", name)) if err != nil { t.Fatalf("reopen %s: %v", name, err) } if _, _, ok := hs.HeaderTip(); !ok { t.Fatalf("reopen %s: no tip", name) } hs.Close() } t.Logf("one store of %d blocks served both chains: %d shared heights, the second chain one block longer", bodies.Len(), tipA+1) }