• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.74
/chainntnfs/test_utils.go
1
//go:build dev
2
// +build dev
3

4
package chainntnfs
5

6
import (
7
        "errors"
8
        "fmt"
9
        "testing"
10
        "time"
11

12
        "github.com/btcsuite/btcd/btcec/v2"
13
        "github.com/btcsuite/btcd/btcjson"
14
        "github.com/btcsuite/btcd/btcutil"
15
        "github.com/btcsuite/btcd/chaincfg/chainhash"
16
        "github.com/btcsuite/btcd/integration/rpctest"
17
        "github.com/btcsuite/btcd/txscript"
18
        "github.com/btcsuite/btcd/wire"
19
        "github.com/lightningnetwork/lnd/input"
20
        "github.com/lightningnetwork/lnd/lntest/unittest"
21
        "github.com/stretchr/testify/require"
22
)
23

24
var (
25
        // TrickleInterval is the interval at which the miner should trickle
26
        // transactions to its peers. We'll set it small to ensure the miner
27
        // propagates transactions quickly in the tests.
28
        TrickleInterval = 10 * time.Millisecond
29
)
30

31
// randPubKeyHashScript generates a P2PKH script that pays to the public key of
32
// a randomly-generated private key.
33
func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
200✔
34
        privKey, err := btcec.NewPrivateKey()
200✔
35
        if err != nil {
200✔
36
                return nil, nil, err
×
37
        }
×
38

39
        pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
200✔
40
        addrScript, err := btcutil.NewAddressWitnessPubKeyHash(
200✔
41
                pubKeyHash, unittest.NetParams,
200✔
42
        )
200✔
43
        if err != nil {
200✔
44
                return nil, nil, err
×
45
        }
×
46

47
        pkScript, err := txscript.PayToAddrScript(addrScript)
200✔
48
        if err != nil {
200✔
49
                return nil, nil, err
×
50
        }
×
51

52
        return pkScript, privKey, nil
200✔
53
}
54

55
// GetTestTxidAndScript generate a new test transaction and returns its txid and
56
// the script of the output being generated.
57
func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) {
132✔
58
        pkScript, _, err := randPubKeyHashScript()
132✔
59
        if err != nil {
132✔
60
                return nil, nil, fmt.Errorf("unable to generate pkScript: %w",
×
61
                        err)
×
62
        }
×
63
        output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
132✔
64
        txid, err := h.SendOutputs([]*wire.TxOut{output}, 10)
132✔
65
        if err != nil {
132✔
66
                return nil, nil, err
×
67
        }
×
68

69
        return txid, pkScript, nil
132✔
70
}
71

72
// WaitForMempoolTx waits for the txid to be seen in the miner's mempool.
73
func WaitForMempoolTx(miner *rpctest.Harness, txid *chainhash.Hash) error {
216✔
74
        timeout := time.After(10 * time.Second)
216✔
75
        trickle := time.After(2 * TrickleInterval)
216✔
76
        for {
432✔
77
                // Check for the harness' knowledge of the txid.
216✔
78
                tx, err := miner.Client.GetRawTransaction(txid)
216✔
79
                if err != nil {
216✔
80
                        jsonErr, ok := err.(*btcjson.RPCError)
×
81
                        if ok && jsonErr.Code == btcjson.ErrRPCNoTxInfo {
×
82
                                continue
×
83
                        }
84
                        return err
×
85
                }
86

87
                if tx != nil && tx.Hash().IsEqual(txid) {
432✔
88
                        break
216✔
89
                }
90

91
                select {
×
92
                case <-time.After(100 * time.Millisecond):
×
93
                case <-timeout:
×
94
                        return errors.New("timed out waiting for tx")
×
95
                }
96
        }
97

98
        // To ensure any transactions propagate from the miner to the peers
99
        // before returning, ensure we have waited for at least
100
        // 2*trickleInterval before returning.
101
        select {
216✔
102
        case <-trickle:
216✔
103
        case <-timeout:
×
104
                return errors.New("timeout waiting for trickle interval. " +
×
105
                        "Trickle interval to large?")
×
106
        }
107

108
        return nil
216✔
109
}
110

111
// CreateSpendableOutput creates and returns an output that can be spent later
112
// on.
113
func CreateSpendableOutput(t *testing.T,
114
        miner *rpctest.Harness) (*wire.OutPoint, *wire.TxOut, *btcec.PrivateKey) {
34✔
115

34✔
116
        t.Helper()
34✔
117

34✔
118
        // Create a transaction that only has one output, the one destined for
34✔
119
        // the recipient.
34✔
120
        pkScript, privKey, err := randPubKeyHashScript()
34✔
121
        require.NoError(t, err, "unable to generate pkScript")
34✔
122
        output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
34✔
123
        txid, err := miner.SendOutputsWithoutChange([]*wire.TxOut{output}, 10)
34✔
124
        require.NoError(t, err, "unable to create tx")
34✔
125

34✔
126
        // Mine the transaction to mark the output as spendable.
34✔
127
        if err := WaitForMempoolTx(miner, txid); err != nil {
34✔
128
                t.Fatalf("tx not relayed to miner: %v", err)
×
129
        }
×
130
        if _, err := miner.Client.Generate(1); err != nil {
34✔
131
                t.Fatalf("unable to generate single block: %v", err)
×
132
        }
×
133

134
        return wire.NewOutPoint(txid, 0), output, privKey
34✔
135
}
136

137
// CreateSpendTx creates a transaction spending the specified output.
138
func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
139
        prevOutput *wire.TxOut, privKey *btcec.PrivateKey) *wire.MsgTx {
34✔
140

34✔
141
        t.Helper()
34✔
142

34✔
143
        // Create a new output.
34✔
144
        outputAmt := int64(1e8)
34✔
145
        witnessProgram, _, err := randPubKeyHashScript()
34✔
146
        require.NoError(t, err, "unable to generate pkScript")
34✔
147
        output := wire.NewTxOut(outputAmt, witnessProgram)
34✔
148

34✔
149
        // Create a new tx.
34✔
150
        tx := wire.NewMsgTx(2)
34✔
151
        tx.AddTxIn(wire.NewTxIn(prevOutPoint, nil, nil))
34✔
152
        tx.AddTxOut(output)
34✔
153

34✔
154
        // Generate the witness.
34✔
155
        sigHashes := input.NewTxSigHashesV0Only(tx)
34✔
156
        witnessScript, err := txscript.WitnessSignature(
34✔
157
                tx, sigHashes, 0, prevOutput.Value, prevOutput.PkScript,
34✔
158
                txscript.SigHashAll, privKey, true,
34✔
159
        )
34✔
160
        require.NoError(t, err, "unable to sign tx")
34✔
161

34✔
162
        tx.TxIn[0].Witness = witnessScript
34✔
163

34✔
164
        return tx
34✔
165
}
34✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc