package gate import ( "bytes" "os/exec" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/store" ) // TestFToolSync is the seeding command, run as an operator runs it: a dataset // built from the node's bytes is served by btcd-lite-serve, and ftool sync // fills a second instance from it over the network. // // It runs the two binaries, not their packages, so what it proves is the thing // an operator does on a storage VPS. func TestFToolSync(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skip("go is not on PATH") } bin := t.TempDir() for _, name := range []string{"ftool", "btcd-lite-serve"} { cmd := exec.Command("go", "build", "-o", filepath.Join(bin, name), "./cmd/"+name) cmd.Dir = "../.." if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("build %s: %v\n%s", name, err, out) } } n := openNode(t) tip, err := n.BlockCount() if err != nil { t.Fatal(err) } if tip < 20 { t.Skipf("the node has %d blocks", tip) } // Instance A holds the node's blocks 0..20. a := t.TempDir() bodies, err := store.NewBlockStore(filepath.Join(a, "blocks")) if err != nil { t.Fatal(err) } want := map[int32][]byte{} for h := uint32(0); h < 20; h++ { hashHex, _ := n.BlockHash(h) hash, err := chainhash.NewHashFromStr(hashHex) if err != nil { t.Fatal(err) } raw, err := n.BlockRaw(hashHex) if err != nil { t.Fatal(err) } if err := bodies.PutBlock(*hash, raw); err != nil { t.Fatal(err) } want[int32(h)] = raw } if err := bodies.Close(); err != nil { t.Fatal(err) } headers, err := store.NewHeaderStore(filepath.Join(a, "headers", "btc")) if err != nil { t.Fatal(err) } seedHeaderChain(t, headers, func(h uint32) []byte { return want[int32(h)] }, 19) headers.Close() // A's key, from the same command an operator runs. keyFile := filepath.Join(t.TempDir(), "instance.key") keygen := exec.Command(filepath.Join(bin, "ftool"), "keygen", "-out", keyFile) if out, err := keygen.CombinedOutput(); err != nil { t.Fatalf("ftool keygen: %v\n%s", err, out) } // Serve A. port := freePort(t) srv := exec.Command(filepath.Join(bin, "btcd-lite-serve"), "-data", a, "-listen", "127.0.0.1:"+itoa(port), "-key", keyFile) var log bytes.Buffer srv.Stdout, srv.Stderr = &log, &log if err := srv.Start(); err != nil { t.Fatal(err) } defer func() { _ = srv.Process.Kill() _ = srv.Wait() }() waitHTTP(t, "http://127.0.0.1:"+itoa(port)+"/v1/status") // B is filled by the command. b := t.TempDir() sync := exec.Command(filepath.Join(bin, "ftool"), "sync", "-from", "http://127.0.0.1:"+itoa(port), "-chain", "btc", "-from-height", "0", "-to-height", "20", "-data", b, "-key", keyFile) if out, err := sync.CombinedOutput(); err != nil { t.Fatalf("ftool sync: %v\n%s\nserver: %s", err, out, log.String()) } got, err := store.NewBlockStore(filepath.Join(b, "blocks")) if err != nil { t.Fatal(err) } defer got.Close() if got.Len() != 20 { t.Fatalf("B holds %d blocks", got.Len()) } for h := int32(0); h < 20; h++ { hash := chainhash.DoubleHashH(want[h][:80]) block, err := got.GetBlock(hash) if err != nil { t.Fatalf("B block %d: %v", h, err) } if string(block) != string(want[h]) { t.Fatalf("B block %d is not the node's", h) } } }