package gate import ( "encoding/binary" "fmt" "io" "net/http" "os/exec" "path/filepath" "testing" ) // TestOneInstanceServesBothChains: one process, one data directory, two chains. // // The two rig nodes are on the same chain, so heights 0..20 are the same blocks // on both and the instance must store them once. What the test asks of the // service is the property that follows: a block chunk for a shared range is the // same chunk whichever chain asked for it, and both chains answer. func TestOneInstanceServesBothChains(t *testing.T) { a := nodeOn(t) a.createWallet(t) a.mine(t, 20) tip := a.height(t) b := nodeConnectedTo(t, a) bin := t.TempDir() buildBinary(t, bin, "btcd-lited") data := t.TempDir() port := freePort(t) logPath := filepath.Join(data, "instance.log") cmd := startProcess(t, filepath.Join(bin, "btcd-lited"), "-chains", "btc,xbt", "-peer", fmt.Sprintf("btc=127.0.0.1:%d,xbt=127.0.0.1:%d", a.p2p, b.p2p), "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-regtest") base := "http://127.0.0.1:" + itoa(port) waitForBlocks(t, cmd, logPath, base, tip+1) var status struct { Chains map[string]struct { Start uint32 `json:"start"` Tip uint32 `json:"tip"` } `json:"chains"` Blocks int `json:"blocks"` } get(t, base+"/v1/status", &status) for _, name := range []string{"btc", "xbt"} { c, ok := status.Chains[name] if !ok { t.Fatalf("the status does not name %q: %+v", name, status.Chains) } if c.Tip != uint32(tip) { t.Fatalf("%s tips at %d, the node at %d", name, c.Tip, tip) } } if want := tip + 1; status.Blocks != want { t.Fatalf("the instance holds %d blocks for two chains of %d heights", status.Blocks, want) } // The same range of both chains is the same chunk, cut from one copy. btcNames := rangeNames(t, base, "btc", 0, 6) xbtNames := rangeNames(t, base, "xbt", 0, 6) if len(btcNames) != 1 || len(xbtNames) != 1 { t.Fatalf("one chunk expected each, got %d and %d", len(btcNames), len(xbtNames)) } if btcNames[0] != xbtNames[0] { t.Fatalf("the shared range cuts to %s on btc and %s on xbt", btcNames[0], xbtNames[0]) } btcBody := chunkAt(t, base, btcNames[0]) xbtBody := chunkAt(t, base, xbtNames[0]) if string(btcBody) != string(xbtBody) { t.Fatal("the two chains served different bytes for the same chunk") } // Six blocks, each length-prefixed, and the first is genesis's. if n := len(splitRecords(t, btcBody)); n != 6 { t.Fatalf("the chunk holds %d blocks for heights 0..5", n) } // A chain the instance does not hold is refused, not silently answered. resp, err := http.Get(base + "/v1/chains/ltc/blk/0-6") if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { t.Fatal("a chain the instance does not hold answered a range") } t.Logf("one instance served both chains from %d bodies; the shared range cut to one chunk", status.Blocks) } // buildBinary builds one command of this module into dir. func buildBinary(t *testing.T, dir, name string) { t.Helper() cmd := exec.Command("go", "build", "-o", filepath.Join(dir, name), "./cmd/"+name) cmd.Dir = "../.." if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("build %s: %v\n%s", name, err, out) } } // rangeNames asks the service for the block chunk names of a range. func rangeNames(t *testing.T, base, chainName string, from, to uint32) []string { t.Helper() return rangeNamesOf(t, base, chainName, "blk", from, to) } // rangeNamesOf asks for the chunk names of one kind over a range. func rangeNamesOf(t *testing.T, base, chainName, kind string, from, to uint32) []string { t.Helper() var listed struct { Chunks []string `json:"chunks"` } get(t, fmt.Sprintf("%s/v1/chains/%s/%s/%d-%d", base, chainName, kind, from, to), &listed) return listed.Chunks } // chunkAt fetches a chunk's bytes. func chunkAt(t *testing.T, base, name string) []byte { t.Helper() resp, err := http.Get(base + "/v1/chunks/" + name) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("chunk %s: %d", name, resp.StatusCode) } body, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } return body } // splitRecords counts the length-prefixed records in a stream. func splitRecords(t *testing.T, raw []byte) [][]byte { t.Helper() var out [][]byte for at := 0; at < len(raw); { n := int(binary.LittleEndian.Uint32(raw[at:])) out = append(out, raw[at+4:at+4+n]) at += 4 + n } return out }