package gate import ( "encoding/binary" "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/chunk" "git.smesh.lol/btcd-lite/internal/serve" "git.smesh.lol/btcd-lite/internal/service" "git.smesh.lol/btcd-lite/internal/store" ) // TestSeedFromService fills a body store from a node, serves it over the // service's HTTP contract, and hands the same blocks to a second store through // that contract. The second store's blocks must be the node's bytes. // // The first half needs the node's RPC rather than its peer, because the peer // does not answer on this machine (see TestPeerAcceptsWhatWeDoNot). What it // proves is the seeding path: a chunk cut on one instance, named by its hash, // fetched and checked by another, and filed under the hash the header chain // gives. func TestSeedFromService(t *testing.T) { n := openNode(t) tip, err := n.BlockCount() if err != nil { t.Fatal(err) } if tip < 50 { t.Skipf("the node has %d blocks; the gate wants at least 50", tip) } // Instance A: blocks from the node's RPC, filed under their own hashes, at // their heights. dirA := t.TempDir() headersA, err := store.NewHeaderStore(dirA + "/headers/btc") if err != nil { t.Fatal(err) } defer headersA.Close() bodiesA, err := store.NewBlockStore(dirA + "/blocks") if err != nil { t.Fatal(err) } defer bodiesA.Close() want := map[int32][]byte{} for h := uint32(0); h <= tip; h++ { hashHex, err := n.BlockHash(h) if err != nil { t.Fatal(err) } hash, err := chainhash.NewHashFromStr(hashHex) if err != nil { t.Fatal(err) } raw, err := n.BlockRaw(hashHex) if err != nil { t.Fatal(err) } if err := bodiesA.PutBlock(*hash, raw); err != nil { t.Fatalf("put %d: %v", h, err) } want[int32(h)] = raw } seedHeaderChain(t, headersA, func(h uint32) []byte { return want[int32(h)] }, uint32(tip)) // Instance A serves what it holds. view := serve.New(map[string]*store.HeaderStore{"btc": headersA}, bodiesA) srv := httptest.NewServer(service.Handler(view)) defer srv.Close() // Instance B: a body store filled from the service, checking every chunk // against the name it asked for. bodiesB, err := store.NewBlockStore(t.TempDir() + "/blocks") if err != nil { t.Fatal(err) } defer bodiesB.Close() fill := func(from, to uint32) { names, err := listChunks(srv.URL, "btc", chunk.Blocks.String(), from, to) if err != nil { t.Fatalf("list %d-%d: %v", from, to, err) } for _, name := range names { raw, err := fetchChunk(srv.URL, name) if err != nil { t.Fatalf("chunk %s: %v", name, err) } if got := chunk.Name(raw).String(); got != name { t.Fatalf("chunk %s hashes to %s", name, got) } at := 0 for at < len(raw) { ln := int(binary.LittleEndian.Uint32(raw[at:])) block := raw[at+4 : at+4+ln] at += 4 + ln h := serve.HeaderHash(block) if err := bodiesB.PutBlock(h, block); err != nil && err != store.ErrBlockExists { t.Fatalf("put: %v", err) } } } } fill(0, 30) if bodiesB.Len() != 30 { t.Fatalf("B holds %d blocks after the first seed, want 30", bodiesB.Len()) } // A second fill of the same range is a no-op: the names are the content. fill(0, 30) if bodiesB.Len() != 30 { t.Fatalf("B holds %d blocks after a repeated seed", bodiesB.Len()) } // Every block B holds is the node's, byte for byte, under the node's hash. for h := int32(0); h < 30; h++ { hash := chainhash.DoubleHashH(want[h][:80]) got, err := bodiesB.GetBlock(hash) if err != nil { t.Fatalf("B block %d: %v", h, err) } if string(got) != string(want[h]) { t.Fatalf("B block %d is not the node's", h) } } t.Logf("B was seeded with 30 of the node's %d blocks from A's service, each one the node's bytes", tip) } func listChunks(base, chainName, kind string, from, to uint32) ([]string, error) { u := fmt.Sprintf("%s/v1/chains/%s/%s/%d-%d", base, chainName, kind, from, to) resp, err := http.Get(u) if err != nil { return nil, err } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("%s: %d %s", u, resp.StatusCode, body) } var listed struct { Chunks []string `json:"chunks"` } if err := json.Unmarshal(body, &listed); err != nil { return nil, err } return listed.Chunks, nil } func fetchChunk(base, name string) ([]byte, error) { resp, err := http.Get(base + "/v1/chunks/" + name) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("chunk %s: %d", name, resp.StatusCode) } return io.ReadAll(resp.Body) }