relay_node_test.go raw
1 package gate
2
3 import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strconv"
8 "testing"
9 "time"
10 )
11
12 // TestRelayAnnouncesToTheNode is the broadcast requirement measured against a
13 // node: a client posts a transaction to the instance's relay and the node's
14 // mempool holds it afterwards.
15 //
16 // The transaction is signed and never submitted: the instance is the only party
17 // that can put it on the node. Its input is a coinbase the wallet has not spent,
18 // which is a transaction the node cannot be holding already.
19 func TestRelayAnnouncesToTheNode(t *testing.T) {
20 if _, err := exec.LookPath("go"); err != nil {
21 t.Skip("go is not on PATH")
22 }
23 r := nodeOn(t)
24 r.createWallet(t)
25 r.mine(t, 120)
26 // A coinbase output nobody has spent. The node's wallet sees it as immature
27 // until 100 blocks later, which the 120 blocks above cover.
28 unspent := r.must(t, "listunspent", "1", "9999999")
29 t.Logf("unspent: %s", trim(unspent)[:min(120, len(trim(unspent)))])
30 raw, err := r.cli("createrawtransaction", `[{"txid":"`+coinbaseOutpoint(t, r, unspent)+`","vout":0}]`,
31 `[{"`+trim(r.must(t, "getnewaddress"))+`":0.4}]`)
32 if err != nil {
33 t.Fatalf("createrawtransaction: %v\n%s", err, raw)
34 }
35 fin, err := r.cli("signrawtransactionwithwallet", trim(raw))
36 if err != nil {
37 t.Fatalf("sign: %v\n%s", err, fin)
38 }
39 hexTx, complete := signedHex(t, fin)
40 if !complete {
41 t.Fatalf("the node did not sign its own transaction: %s", fin)
42 }
43 txid := decodeTxID(t, r, hexTx)
44 // The node has never seen it, so a mempool entry after the relay is the
45 // relay's doing and nothing else's.
46 if _, err := r.cli("getmempoolentry", txid); err == nil {
47 t.Fatalf("the node already holds %s before the relay ran", txid)
48 }
49 bin := t.TempDir()
50 build := exec.Command("go", "build", "-o", filepath.Join(bin, "btcd-lited"), "./cmd/btcd-lited")
51 build.Dir = "../.."
52 if out, err := build.CombinedOutput(); err != nil {
53 t.Fatalf("build: %v\n%s", err, out)
54 }
55 data := t.TempDir()
56 port := freePort(t)
57 inst := startProcess(t, filepath.Join(bin, "btcd-lited"),
58 "-data", data, "-listen", "127.0.0.1:"+itoa(port),
59 "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-relay", "127.0.0.1:"+itoa(r.p2p), "-regtest")
60 base := "http://127.0.0.1:" + itoa(port)
61 tip, _ := strconv.Atoi(r.must(t, "getblockcount"))
62 waitForBlocks(t, inst, filepath.Join(data, "instance.log"), base, tip+1)
63 deadline := time.Now().Add(30 * time.Second)
64 for {
65 code, resp := submitTx(t, base, "btc", hexTx, nil)
66 if code == 200 && resp.Peers > 0 {
67 t.Logf("the instance took %s and announced it to %d peer(s)", resp.TxID, resp.Peers)
68 break
69 }
70 if time.Now().After(deadline) {
71 logged, _ := os.ReadFile(filepath.Join(data, "instance.log"))
72 t.Fatalf("submit got %d %+v; log:\n%s", code, resp, logged)
73 }
74 time.Sleep(300 * time.Millisecond)
75 }
76 waitForMempool(t, r, txid, true)
77 t.Logf("the node's mempool holds %s after the instance relayed it", txid)
78 }
79