filler_test.go raw
1 package gate
2
3 import (
4 "context"
5 "encoding/hex"
6 "fmt"
7 "path/filepath"
8 "strconv"
9 "testing"
10 "time"
11
12 "git.smesh.lol/btcd-lite/internal/chains"
13 "git.smesh.lol/btcd-lite/internal/filler"
14 "git.smesh.lol/btcd-lite/internal/process"
15 "git.smesh.lol/btcd-lite/internal/staging"
16 "git.smesh.lol/btcd-lite/internal/store"
17 )
18
19 // TestFillerFromPeer mines a chain on a node started for the test, syncs the
20 // header chain over p2p, then fills the body store from the same peer and
21 // requires every stored block to be the node's bytes under the node's hash.
22 //
23 // It exercises the whole path the service's backfill takes: a handshake, a
24 // getheaders chain, one getdata per height, and the body store's placement and
25 // integrity checks.
26 func TestFillerFromPeer(t *testing.T) {
27 r := nodeOn(t)
28 r.createWallet(t)
29 r.mine(t, 120)
30 tip, err := strconv.Atoi(r.must(t, "getblockcount"))
31 if err != nil {
32 t.Fatal(err)
33 }
34
35 dir := t.TempDir()
36 headers, err := store.NewHeaderStore(filepath.Join(dir, "headers"))
37 if err != nil {
38 t.Fatal(err)
39 }
40 defer headers.Close()
41 bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks"))
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer bodies.Close()
46
47 peer := fmt.Sprintf("127.0.0.1:%d", r.p2p)
48 synced := syncHeaders(t, headers, peer)
49 t.Logf("headers synced")
50
51 prof, err := chains.ByName(chains.Parent)
52 if err != nil {
53 t.Fatal(err)
54 }
55 log, err := staging.Open(staging.Dir(dir, "btc"))
56 if err != nil {
57 t.Fatal(err)
58 }
59 defer log.Close()
60 pool, err := filler.NewPool(filler.PoolConfig{
61 Chain: "btc",
62 Profile: prof.Profile,
63 Headers: headers,
64 Bodies: bodies,
65 Staging: log,
66 Peers: []filler.Peer{{Addr: peer}},
67 Logf: t.Logf,
68 })
69 if err != nil {
70 t.Fatal(err)
71 }
72 // The bodies come over the connection the headers did, as they do in
73 // btcd-lited, so the node is not asked for a second connection seconds
74 // after the first.
75 pool.Reuse(peer, synced)
76 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
77 defer cancel()
78 if err := pool.Run(ctx); err != nil {
79 t.Fatalf("pool: %v", err)
80 }
81 _ = pool.Close()
82 if _, err := process.Run(process.Config{
83 Chain: "btc", Staging: log, Bodies: bodies, Headers: headers, Logf: t.Logf,
84 }); err != nil {
85 t.Fatalf("process: %v", err)
86 }
87
88 if got, want := bodies.Len(), tip+1; got != want {
89 t.Fatalf("the store holds %d blocks, the node has %d heights", got, want)
90 }
91 for h := 0; h <= tip; h++ {
92 hash := r.must(t, "getblockhash", strconv.Itoa(h))
93 want, err := hex.DecodeString(r.must(t, "getblock", hash, "0"))
94 if err != nil {
95 t.Fatalf("height %d: %v", h, err)
96 }
97 hdr, ok := headers.Header(uint32(h))
98 if !ok {
99 t.Fatalf("height %d: no header", h)
100 }
101 got, err := bodies.GetBlock(hdr.BlockHash())
102 if err != nil {
103 t.Fatalf("height %d: %v", h, err)
104 }
105 if string(got) != string(want) {
106 t.Fatalf("height %d: the stored block is not the node's", h)
107 }
108 }
109 t.Logf("the filler stored %d blocks from the peer, byte-identical to the node", tip+1)
110 }
111