process_test.go raw

   1  package gate
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  	"os"
   7  	"path/filepath"
   8  	"strings"
   9  	"testing"
  10  )
  11  
  12  // TestStagingIsNotServed: a fill writes bodies to the staging log and nothing
  13  // else. Until a processing pass has checked them, the instance serves no block:
  14  // the unverified bytes are not reachable from any serving path.
  15  func TestStagingIsNotServed(t *testing.T) {
  16  	r := nodeOn(t)
  17  	r.createWallet(t)
  18  	r.mine(t, 20)
  19  	tip := r.height(t)
  20  
  21  	bin := t.TempDir()
  22  	buildBinary(t, bin, "btcd-lited")
  23  	data := t.TempDir()
  24  	port := freePort(t)
  25  	base := "http://127.0.0.1:" + itoa(port)
  26  	cmd := startProcess(t, filepath.Join(bin, "btcd-lited"),
  27  		"-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest",
  28  		"-process-after-fill=false", "-data", data, "-listen", "127.0.0.1:"+itoa(port))
  29  	t.Cleanup(func() {
  30  		_ = cmd.Process.Kill()
  31  		_ = cmd.Wait()
  32  	})
  33  	waitForHTTP(t, base+"/v1/status")
  34  
  35  	var status struct {
  36  		Blocks int `json:"blocks"`
  37  	}
  38  	get(t, base+"/v1/status", &status)
  39  	if status.Blocks != 0 {
  40  		t.Fatalf("the instance serves %d blocks with no processing pass", status.Blocks)
  41  	}
  42  	// The staging log holds the whole fill.
  43  	claims := filepath.Join(data, "blocks", "staging", "btc", "claims.dat")
  44  	fi, err := os.Stat(claims)
  45  	if err != nil {
  46  		t.Fatalf("no staging claims after the fill: %v", err)
  47  	}
  48  	if got, want := fi.Size(), int64((tip+1)*44); got != want {
  49  		t.Fatalf("claims.dat is %d bytes for %d heights", got, tip+1)
  50  	}
  51  	resp, err := http.Get(base + "/v1/chains/btc/blk/0-2")
  52  	if err != nil {
  53  		t.Fatal(err)
  54  	}
  55  	defer resp.Body.Close()
  56  	if resp.StatusCode == http.StatusOK {
  57  		t.Fatal("a block range was answered from unprocessed bodies")
  58  	}
  59  	t.Logf("staged %d bodies and served none: %s", tip+1, resp.Status)
  60  }
  61  
  62  // TestProcessIsSeparate: the download and the processing are two runs. The
  63  // second one needs no peer, and a body whose claim it cannot verify is dropped
  64  // and written down.
  65  func TestProcessIsSeparate(t *testing.T) {
  66  	r := nodeOn(t)
  67  	r.createWallet(t)
  68  	r.mine(t, 20)
  69  	tip := r.height(t)
  70  
  71  	bin := t.TempDir()
  72  	buildBinary(t, bin, "btcd-lited")
  73  	data := t.TempDir()
  74  	port := freePort(t)
  75  	cmd := startProcess(t, filepath.Join(bin, "btcd-lited"),
  76  		"-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest",
  77  		"-process-after-fill=false", "-data", data, "-listen", "127.0.0.1:"+itoa(port))
  78  	waitForHTTP(t, "http://127.0.0.1:"+itoa(port)+"/v1/status")
  79  	_ = cmd.Process.Kill()
  80  	_ = cmd.Wait()
  81  
  82  	// Corrupt one staged body: the first record's bytes, after its length
  83  	// prefix. Its claim still names the hash the downloader asked for, so the
  84  	// pass must refuse it.
  85  	seg := filepath.Join(data, "blocks", "staging", "btc", "00000000.dat")
  86  	raw, err := os.ReadFile(seg)
  87  	if err != nil {
  88  		t.Fatalf("no staged segment: %v", err)
  89  	}
  90  	if len(raw) < 8 {
  91  		t.Fatalf("the staged segment is %d bytes", len(raw))
  92  	}
  93  	// Inside the 80-byte header, which is what the hash commits to.
  94  	raw[4+10] ^= 0xff
  95  	if err := os.WriteFile(seg, raw, 0o644); err != nil {
  96  		t.Fatal(err)
  97  	}
  98  
  99  	port2 := freePort(t)
 100  	base2 := "http://127.0.0.1:" + itoa(port2)
 101  	cmd2 := startProcess(t, filepath.Join(bin, "btcd-lited"),
 102  		"-chain", "btc", "-regtest", "-process-only",
 103  		"-data", data, "-listen", "127.0.0.1:"+itoa(port2))
 104  	t.Cleanup(func() {
 105  		_ = cmd2.Process.Kill()
 106  		_ = cmd2.Wait()
 107  	})
 108  	waitForBlocks(t, cmd2, "", base2, tip)
 109  
 110  	var status struct {
 111  		Blocks int `json:"blocks"`
 112  	}
 113  	get(t, base2+"/v1/status", &status)
 114  	if status.Blocks != tip {
 115  		t.Fatalf("the pass stored %d blocks, one body was corrupt and %d are good", status.Blocks, tip)
 116  	}
 117  	// The staging log is drained, and the failure is written down.
 118  	if _, err := os.Stat(filepath.Join(data, "blocks", "staging", "btc", "claims.dat")); !os.IsNotExist(err) {
 119  		t.Fatalf("the staging claims survived the pass: %v", err)
 120  	}
 121  	missing, err := os.ReadFile(filepath.Join(data, "blocks", "missing", "btc.log"))
 122  	if err != nil {
 123  		t.Fatalf("the refused height was not recorded: %v", err)
 124  	}
 125  	if !strings.Contains(string(missing), "btc 0 ") {
 126  		t.Fatalf("the record does not name height 0: %q", missing)
 127  	}
 128  	// The stored blocks are the node's, byte for byte, for every height the
 129  	// pass kept.
 130  	name := rangeNames(t, base2, "btc", 1, uint32(tip+1))
 131  	if len(name) != 1 {
 132  		t.Fatalf("%d chunks for heights 1..%d", len(name), tip+1)
 133  	}
 134  	got := splitRecords(t, chunkAt(t, base2, name[0]))
 135  	if len(got) != tip {
 136  		t.Fatalf("%d blocks served, %d expected", len(got), tip)
 137  	}
 138  	for i, h := 1, 0; i <= tip; i, h = i+1, h+1 {
 139  		hash := r.must(t, "getblockhash", itoa(i))
 140  		want, err := r.cli("getblock", hash, "0")
 141  		if err != nil {
 142  			t.Fatal(err)
 143  		}
 144  		if strings.TrimSpace(want) != fmt.Sprintf("%x", got[h]) {
 145  			t.Fatalf("height %d is not the node's block", i)
 146  		}
 147  	}
 148  	t.Logf("processed %d of %d bodies with no peer; the corrupt one is recorded in blocks/missing", tip, tip+1)
 149  }
 150