relay_helpers_test.go raw
1 package gate
2
3 import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "strings"
8 "testing"
9 "time"
10
11 "git.smesh.lol/btcd-lite/internal/relay"
12 )
13
14 // submitTx posts a transaction to an instance's relay endpoint.
15 func submitTx(t *testing.T, base, chain, rawHex string, dependsOn []string) (int, relay.SubmitResponse) {
16 t.Helper()
17 body, err := json.Marshal(relay.SubmitRequest{Tx: rawHex, DependsOn: dependsOn})
18 if err != nil {
19 t.Fatal(err)
20 }
21 resp, err := http.Post(base+"/v1/chains/"+chain+"/tx", "application/json", bytes.NewReader(body))
22 if err != nil {
23 t.Fatalf("submit: %v", err)
24 }
25 defer resp.Body.Close()
26 var out relay.SubmitResponse
27 _ = json.NewDecoder(resp.Body).Decode(&out)
28 return resp.StatusCode, out
29 }
30
31 // coinbaseOutpoint picks the outpoint of the first spendable output in the
32 // node's unspent list. Whether it is a coinbase does not matter: what matters
33 // is that no transaction spending it exists yet, so the node cannot be holding
34 // the relayed transaction already.
35 func coinbaseOutpoint(t *testing.T, r *rig, listunspent string) string {
36 t.Helper()
37 var rows []struct {
38 TxID string `json:"txid"`
39 Vout uint32 `json:"vout"`
40 Amount float64 `json:"amount"`
41 Coinbase bool `json:"coinbase"`
42 }
43 unmarshalStrict(t, listunspent, &rows)
44 t.Logf("the node has %d unspent outputs", len(rows))
45 for _, row := range rows {
46 if row.Amount >= 0.5 {
47 return row.TxID
48 }
49 }
50 t.Fatalf("the node has no unspent output of half a coin")
51 return ""
52 }
53
54 // signedHex reads the signed transaction and whether signing completed.
55 func signedHex(t *testing.T, raw string) (string, bool) {
56 t.Helper()
57 var out struct {
58 Hex string `json:"hex"`
59 Complete bool `json:"complete"`
60 }
61 unmarshalStrict(t, raw, &out)
62 return out.Hex, out.Complete
63 }
64
65 // decodeTxID is the txid of a signed transaction, read by the node.
66 func decodeTxID(t *testing.T, r *rig, hexTx string) string {
67 t.Helper()
68 out, err := r.cli("decoderawtransaction", hexTx)
69 if err != nil {
70 t.Fatalf("decoderawtransaction: %v\n%s", err, out)
71 }
72 var d struct {
73 TxID string `json:"txid"`
74 }
75 unmarshalStrict(t, out, &d)
76 return d.TxID
77 }
78
79 // unmarshalStrict decodes one JSON value and refuses trailing output, which is
80 // how a cli error gets caught here rather than at the next step.
81 func unmarshalStrict(t *testing.T, raw string, out any) {
82 t.Helper()
83 dec := json.NewDecoder(strings.NewReader(strings.TrimSpace(raw)))
84 if err := dec.Decode(out); err != nil {
85 t.Fatalf("json: %v\n%s", err, raw)
86 }
87 }
88
89 func min(a, b int) int {
90 if a < b {
91 return a
92 }
93 return b
94 }
95
96 // waitForMempool waits until the node's mempool holds or lacks a transaction.
97 func waitForMempool(t *testing.T, r *rig, txid string, want bool) {
98 t.Helper()
99 deadline := time.Now().Add(30 * time.Second)
100 for {
101 out, err := r.cli("getmempoolentry", txid)
102 has := err == nil
103 if has == want {
104 if want {
105 t.Logf("the node's mempool holds %s: %s", txid, trim(out))
106 }
107 return
108 }
109 if time.Now().After(deadline) {
110 state := "lacks"
111 if want {
112 state = "never held"
113 }
114 t.Fatalf("the node's mempool %s %s", state, txid)
115 }
116 time.Sleep(250 * time.Millisecond)
117 }
118 }
119