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

lightningnetwork / lnd / 12209990843

07 Dec 2024 02:45AM UTC coverage: 58.977% (+1.1%) from 57.911%
12209990843

Pull #9260

github

yyforyongyu
lnrpc: sort `Invoice.HTLCs` based on `HtlcIndex`

So the returned HTLCs are ordered.
Pull Request #9260: Beat itest [3/3]: fix all itest flakes

4 of 132 new or added lines in 9 files covered. (3.03%)

17 existing lines in 8 files now uncovered.

134149 of 227459 relevant lines covered (58.98%)

19442.29 hits per line

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

0.0
/lntest/harness_miner.go
1
package lntest
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/blockchain"
7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/btcsuite/btcd/chaincfg/chainhash"
9
        "github.com/btcsuite/btcd/wire"
10
        "github.com/lightningnetwork/lnd/lntest/miner"
11
        "github.com/lightningnetwork/lnd/lntest/node"
12
        "github.com/lightningnetwork/lnd/lntest/wait"
13
        "github.com/stretchr/testify/require"
14
)
15

16
// Miner returns the miner instance.
17
//
18
// NOTE: Caller should keep in mind that when using this private instance,
19
// certain states won't be managed by the HarnessTest anymore. For instance,
20
// when mining directly, the nodes managed by the HarnessTest can be out of
21
// sync, and the `HarnessTest.CurrentHeight()` won't be accurate.
22
func (h *HarnessTest) Miner() *miner.HarnessMiner {
×
23
        return h.miner
×
24
}
×
25

26
// MineBlocks mines blocks and asserts all active nodes have synced to the
27
// chain. It assumes no txns are expected in the blocks.
28
//
29
// NOTE: Use `MineBlocksAndAssertNumTxes` if you expect txns in the blocks. Use
30
// `MineEmptyBlocks` if you want to make sure that txns stay unconfirmed.
31
func (h *HarnessTest) MineBlocks(num int) {
×
32
        require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
×
33

×
34
        // Update the harness's current height.
×
35
        defer h.updateCurrentHeight()
×
36

×
37
        // Mine num of blocks.
×
38
        for i := 0; i < num; i++ {
×
39
                block := h.miner.MineBlocks(1)[0]
×
40

×
41
                // Check the block doesn't have any txns except the coinbase.
×
42
                if len(block.Transactions) <= 1 {
×
43
                        // Make sure all the active nodes are synced.
×
44
                        h.AssertActiveNodesSyncedTo(block)
×
45

×
46
                        // Mine the next block.
×
47
                        continue
×
48
                }
49

50
                // Create a detailed description.
51
                desc := fmt.Sprintf("block %v has %d txns:\n",
×
52
                        block.BlockHash(), len(block.Transactions)-1)
×
53

×
54
                // Print all the txns except the coinbase.
×
55
                for _, tx := range block.Transactions {
×
56
                        if blockchain.IsCoinBaseTx(tx) {
×
57
                                continue
×
58
                        }
59

60
                        desc += fmt.Sprintf("%v\n", tx.TxHash())
×
61
                }
62

63
                desc += "Consider using `MineBlocksAndAssertNumTxes` if you " +
×
64
                        "expect txns, or `MineEmptyBlocks` if you want to " +
×
65
                        "keep the txns unconfirmed."
×
66

×
67
                // Raise an error if the block has txns.
×
68
                require.Fail(h, "MineBlocks", desc)
×
69
        }
70
}
71

72
// MineEmptyBlocks mines a given number of empty blocks.
73
//
74
// NOTE: this differs from miner's `MineEmptyBlocks` as it requires the nodes
75
// to be synced.
76
func (h *HarnessTest) MineEmptyBlocks(num int) []*wire.MsgBlock {
×
77
        require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
×
78

×
79
        // Update the harness's current height.
×
80
        defer h.updateCurrentHeight()
×
81

×
82
        blocks := h.miner.MineEmptyBlocks(num)
×
83

×
84
        // Finally, make sure all the active nodes are synced.
×
85
        h.AssertActiveNodesSynced()
×
86

×
87
        return blocks
×
88
}
×
89

90
// MineBlocksAndAssertNumTxes mines blocks and asserts the number of
91
// transactions are found in the first block. It also asserts all active nodes
92
// have synced to the chain.
93
//
94
// NOTE: this differs from miner's `MineBlocks` as it requires the nodes to be
95
// synced.
96
func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
97
        numTxs int) []*wire.MsgBlock {
×
98

×
99
        // Update the harness's current height.
×
100
        defer h.updateCurrentHeight()
×
101

×
102
        // If we expect transactions to be included in the blocks we'll mine,
×
103
        // we wait here until they are seen in the miner's mempool.
×
104
        txids := h.AssertNumTxsInMempool(numTxs)
×
105

×
106
        // Mine blocks.
×
107
        blocks := h.miner.MineBlocks(num)
×
108

×
109
        // Assert that all the transactions were included in the first block.
×
110
        for _, txid := range txids {
×
111
                h.miner.AssertTxInBlock(blocks[0], txid)
×
112
        }
×
113

114
        // Make sure the mempool has been updated.
115
        h.miner.AssertTxnsNotInMempool(txids)
×
116

×
117
        // Finally, make sure all the active nodes are synced.
×
118
        bestBlock := blocks[len(blocks)-1]
×
119
        h.AssertActiveNodesSyncedTo(bestBlock)
×
120

×
121
        return blocks
×
122
}
123

124
// ConnectMiner connects the miner with the chain backend in the network.
125
func (h *HarnessTest) ConnectMiner() {
×
NEW
126
        //  Get the current best height.
×
NEW
127
        _, height, err := h.miner.Client.GetBestBlock()
×
NEW
128
        require.NoError(h, err, "miner GetBestBlock")
×
NEW
129

×
NEW
130
        err = h.manager.chainBackend.ConnectMiner(height)
×
131
        require.NoError(h, err, "failed to connect miner")
×
132
}
×
133

134
// DisconnectMiner removes the connection between the miner and the chain
135
// backend in the network.
136
func (h *HarnessTest) DisconnectMiner() {
×
137
        err := h.manager.chainBackend.DisconnectMiner()
×
138
        require.NoError(h, err, "failed to disconnect miner")
×
139
}
×
140

141
// cleanMempool mines blocks till the mempool is empty and asserts all active
142
// nodes have synced to the chain.
143
func (h *HarnessTest) cleanMempool() {
×
144
        _, startHeight := h.GetBestBlock()
×
145

×
146
        // Mining the blocks slow to give `lnd` more time to sync.
×
147
        var bestBlock *wire.MsgBlock
×
148
        err := wait.NoError(func() error {
×
149
                // If mempool is empty, exit.
×
150
                mem := h.miner.GetRawMempool()
×
151
                if len(mem) == 0 {
×
152
                        _, height := h.GetBestBlock()
×
153
                        h.Logf("Mined %d blocks when cleanup the mempool",
×
154
                                height-startHeight)
×
155

×
156
                        return nil
×
157
                }
×
158

159
                // Otherwise mine a block.
160
                blocks := h.miner.MineBlocksSlow(1)
×
161
                bestBlock = blocks[len(blocks)-1]
×
162

×
163
                // Make sure all the active nodes are synced.
×
164
                h.AssertActiveNodesSyncedTo(bestBlock)
×
165

×
166
                return fmt.Errorf("still have %d txes in mempool", len(mem))
×
167
        }, wait.MinerMempoolTimeout)
168
        require.NoError(h, err, "timeout cleaning up mempool")
×
169
}
170

171
// mineTillForceCloseResolved asserts that the number of pending close channels
172
// are zero. Each time it checks, an empty block is mined, followed by a
173
// mempool check to see if there are any sweeping txns. If found, these txns
174
// are then mined to clean up the mempool.
175
func (h *HarnessTest) mineTillForceCloseResolved(hn *node.HarnessNode) {
×
176
        _, startHeight := h.GetBestBlock()
×
177

×
178
        err := wait.NoError(func() error {
×
179
                resp := hn.RPC.PendingChannels()
×
180
                total := len(resp.PendingForceClosingChannels)
×
181
                if total != 0 {
×
182
                        // Mine an empty block first.
×
183
                        h.MineEmptyBlocks(1)
×
184

×
185
                        // If there are new sweeping txns, mine a block to
×
186
                        // confirm it.
×
187
                        mem := h.GetRawMempool()
×
188
                        if len(mem) != 0 {
×
189
                                h.MineBlocksAndAssertNumTxes(1, len(mem))
×
190
                        }
×
191

192
                        return fmt.Errorf("expected num of pending force " +
×
193
                                "close channel to be zero")
×
194
                }
195

196
                _, height := h.GetBestBlock()
×
197
                h.Logf("Mined %d blocks while waiting for force closed "+
×
198
                        "channel to be resolved", height-startHeight)
×
199

×
200
                return nil
×
201
        }, DefaultTimeout)
202

203
        require.NoErrorf(h, err, "%s: assert force close resolved timeout",
×
204
                hn.Name())
×
205
}
206

207
// AssertTxInMempool asserts a given transaction can be found in the mempool.
208
func (h *HarnessTest) AssertTxInMempool(txid chainhash.Hash) *wire.MsgTx {
×
209
        return h.miner.AssertTxInMempool(txid)
×
210
}
×
211

212
// AssertTxNotInMempool asserts a given transaction cannot be found in the
213
// mempool. It assumes the mempool is not empty.
214
//
215
// NOTE: this should be used after `AssertTxInMempool` to ensure the tx has
216
// entered the mempool before. Otherwise it might give false positive and the
217
// tx may enter the mempool after the check.
218
func (h *HarnessTest) AssertTxNotInMempool(txid chainhash.Hash) {
×
219
        h.miner.AssertTxNotInMempool(txid)
×
220
}
×
221

222
// AssertNumTxsInMempool polls until finding the desired number of transactions
223
// in the provided miner's mempool. It will assert if this number is not met
224
// after the given timeout.
225
func (h *HarnessTest) AssertNumTxsInMempool(n int) []chainhash.Hash {
×
226
        return h.miner.AssertNumTxsInMempool(n)
×
227
}
×
228

229
// AssertOutpointInMempool asserts a given outpoint can be found in the mempool.
230
func (h *HarnessTest) AssertOutpointInMempool(op wire.OutPoint) *wire.MsgTx {
×
231
        return h.miner.AssertOutpointInMempool(op)
×
232
}
×
233

234
// AssertTxInBlock asserts that a given txid can be found in the passed block.
235
func (h *HarnessTest) AssertTxInBlock(block *wire.MsgBlock,
236
        txid chainhash.Hash) {
×
237

×
238
        h.miner.AssertTxInBlock(block, txid)
×
239
}
×
240

241
// GetNumTxsFromMempool polls until finding the desired number of transactions
242
// in the miner's mempool and returns the full transactions to the caller.
243
func (h *HarnessTest) GetNumTxsFromMempool(n int) []*wire.MsgTx {
×
244
        return h.miner.GetNumTxsFromMempool(n)
×
245
}
×
246

247
// GetBestBlock makes a RPC request to miner and asserts.
248
func (h *HarnessTest) GetBestBlock() (*chainhash.Hash, int32) {
×
249
        return h.miner.GetBestBlock()
×
250
}
×
251

252
// MineBlockWithTx mines a single block to include the specifies tx only.
253
func (h *HarnessTest) MineBlockWithTx(tx *wire.MsgTx) *wire.MsgBlock {
×
254
        return h.miner.MineBlockWithTx(tx)
×
255
}
×
256

257
// ConnectToMiner connects the miner to a temp miner.
258
func (h *HarnessTest) ConnectToMiner(tempMiner *miner.HarnessMiner) {
×
259
        h.miner.ConnectMiner(tempMiner)
×
260
}
×
261

262
// DisconnectFromMiner disconnects the miner from the temp miner.
263
func (h *HarnessTest) DisconnectFromMiner(tempMiner *miner.HarnessMiner) {
×
264
        h.miner.DisconnectMiner(tempMiner)
×
265
}
×
266

267
// GetRawMempool makes a RPC call to the miner's GetRawMempool and
268
// asserts.
269
func (h *HarnessTest) GetRawMempool() []chainhash.Hash {
×
270
        return h.miner.GetRawMempool()
×
271
}
×
272

273
// GetRawTransaction makes a RPC call to the miner's GetRawTransaction and
274
// asserts.
275
func (h *HarnessTest) GetRawTransaction(txid chainhash.Hash) *btcutil.Tx {
×
276
        return h.miner.GetRawTransaction(txid)
×
277
}
×
278

279
// NewMinerAddress creates a new address for the miner and asserts.
280
func (h *HarnessTest) NewMinerAddress() btcutil.Address {
×
281
        return h.miner.NewMinerAddress()
×
282
}
×
283

284
// SpawnTempMiner creates a temp miner and syncs it with the current miner.
285
// Once miners are synced, the temp miner is disconnected from the original
286
// miner and returned.
287
func (h *HarnessTest) SpawnTempMiner() *miner.HarnessMiner {
×
288
        return h.miner.SpawnTempMiner()
×
289
}
×
290

291
// CreateTransaction uses the miner to create a transaction using the given
292
// outputs using the specified fee rate and returns the transaction.
293
func (h *HarnessTest) CreateTransaction(outputs []*wire.TxOut,
294
        feeRate btcutil.Amount) *wire.MsgTx {
×
295

×
296
        return h.miner.CreateTransaction(outputs, feeRate)
×
297
}
×
298

299
// SendOutputsWithoutChange uses the miner to send the given outputs using the
300
// specified fee rate and returns the txid.
301
func (h *HarnessTest) SendOutputsWithoutChange(outputs []*wire.TxOut,
302
        feeRate btcutil.Amount) *chainhash.Hash {
×
303

×
304
        return h.miner.SendOutputsWithoutChange(outputs, feeRate)
×
305
}
×
306

307
// AssertMinerBlockHeightDelta ensures that tempMiner is 'delta' blocks ahead
308
// of miner.
309
func (h *HarnessTest) AssertMinerBlockHeightDelta(
310
        tempMiner *miner.HarnessMiner, delta int32) {
×
311

×
312
        h.miner.AssertMinerBlockHeightDelta(tempMiner, delta)
×
313
}
×
314

315
// SendRawTransaction submits the encoded transaction to the server which will
316
// then relay it to the network.
317
func (h *HarnessTest) SendRawTransaction(tx *wire.MsgTx,
318
        allowHighFees bool) (chainhash.Hash, error) {
×
319

×
320
        txid, err := h.miner.Client.SendRawTransaction(tx, allowHighFees)
×
321
        require.NoError(h, err)
×
322

×
323
        return *txid, nil
×
324
}
×
325

326
// CurrentHeight returns the current block height.
327
func (h *HarnessTest) CurrentHeight() uint32 {
×
328
        return h.currentHeight
×
329
}
×
330

331
// updateCurrentHeight set the harness's current height to the best known
332
// height.
333
func (h *HarnessTest) updateCurrentHeight() {
×
334
        _, height := h.GetBestBlock()
×
335
        h.currentHeight = uint32(height)
×
336
}
×
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