package gate import ( "os" "os/exec" "path/filepath" "strconv" "testing" "time" ) // TestRelayAnnouncesToTheNode is the broadcast requirement measured against a // node: a client posts a transaction to the instance's relay and the node's // mempool holds it afterwards. // // The transaction is signed and never submitted: the instance is the only party // that can put it on the node. Its input is a coinbase the wallet has not spent, // which is a transaction the node cannot be holding already. func TestRelayAnnouncesToTheNode(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skip("go is not on PATH") } r := nodeOn(t) r.createWallet(t) r.mine(t, 120) // A coinbase output nobody has spent. The node's wallet sees it as immature // until 100 blocks later, which the 120 blocks above cover. unspent := r.must(t, "listunspent", "1", "9999999") t.Logf("unspent: %s", trim(unspent)[:min(120, len(trim(unspent)))]) raw, err := r.cli("createrawtransaction", `[{"txid":"`+coinbaseOutpoint(t, r, unspent)+`","vout":0}]`, `[{"`+trim(r.must(t, "getnewaddress"))+`":0.4}]`) if err != nil { t.Fatalf("createrawtransaction: %v\n%s", err, raw) } fin, err := r.cli("signrawtransactionwithwallet", trim(raw)) if err != nil { t.Fatalf("sign: %v\n%s", err, fin) } hexTx, complete := signedHex(t, fin) if !complete { t.Fatalf("the node did not sign its own transaction: %s", fin) } txid := decodeTxID(t, r, hexTx) // The node has never seen it, so a mempool entry after the relay is the // relay's doing and nothing else's. if _, err := r.cli("getmempoolentry", txid); err == nil { t.Fatalf("the node already holds %s before the relay ran", txid) } bin := t.TempDir() build := exec.Command("go", "build", "-o", filepath.Join(bin, "btcd-lited"), "./cmd/btcd-lited") build.Dir = "../.." if out, err := build.CombinedOutput(); err != nil { t.Fatalf("build: %v\n%s", err, out) } data := t.TempDir() port := freePort(t) inst := startProcess(t, filepath.Join(bin, "btcd-lited"), "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-relay", "127.0.0.1:"+itoa(r.p2p), "-regtest") base := "http://127.0.0.1:" + itoa(port) tip, _ := strconv.Atoi(r.must(t, "getblockcount")) waitForBlocks(t, inst, filepath.Join(data, "instance.log"), base, tip+1) deadline := time.Now().Add(30 * time.Second) for { code, resp := submitTx(t, base, "btc", hexTx, nil) if code == 200 && resp.Peers > 0 { t.Logf("the instance took %s and announced it to %d peer(s)", resp.TxID, resp.Peers) break } if time.Now().After(deadline) { logged, _ := os.ReadFile(filepath.Join(data, "instance.log")) t.Fatalf("submit got %d %+v; log:\n%s", code, resp, logged) } time.Sleep(300 * time.Millisecond) } waitForMempool(t, r, txid, true) t.Logf("the node's mempool holds %s after the instance relayed it", txid) }