• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Info updated!

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 hits per line

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

0.0
/invoices/test_utils.go
1
package invoices
2

3
import (
4
        "crypto/rand"
5
        "encoding/binary"
6
        "encoding/hex"
7
        "sync"
8
        "testing"
9
        "time"
10

11
        "github.com/btcsuite/btcd/btcec/v2"
12
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
13
        "github.com/btcsuite/btcd/chaincfg"
14
        "github.com/btcsuite/btcd/chaincfg/chainhash"
15
        "github.com/lightningnetwork/lnd/chainntnfs"
16
        "github.com/lightningnetwork/lnd/clock"
17
        "github.com/lightningnetwork/lnd/lntypes"
18
        "github.com/lightningnetwork/lnd/lnwire"
19
        "github.com/lightningnetwork/lnd/zpay32"
20
        "github.com/stretchr/testify/require"
21
)
22

23
type mockChainNotifier struct {
24
        chainntnfs.ChainNotifier
25

26
        blockChan chan *chainntnfs.BlockEpoch
27
}
28

29
func newMockNotifier() *mockChainNotifier {
×
30
        return &mockChainNotifier{
×
31
                blockChan: make(chan *chainntnfs.BlockEpoch),
×
32
        }
×
33
}
×
34

35
// RegisterBlockEpochNtfn mocks a block epoch notification, using the mock's
36
// block channel to deliver blocks to the client.
37
func (m *mockChainNotifier) RegisterBlockEpochNtfn(*chainntnfs.BlockEpoch) (
38
        *chainntnfs.BlockEpochEvent, error) {
×
39

×
40
        return &chainntnfs.BlockEpochEvent{
×
41
                Epochs: m.blockChan,
×
42
                Cancel: func() {},
×
43
        }, nil
44
}
45

46
const (
47
        testCurrentHeight = int32(1)
48
)
49

50
var (
51
        testTimeout = 5 * time.Second
52

53
        testTime = time.Date(2018, time.February, 2, 14, 0, 0, 0, time.UTC)
54

55
        testPrivKeyBytes, _ = hex.DecodeString(
56
                "e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2d" +
57
                        "b734",
58
        )
59

60
        testPrivKey, _ = btcec.PrivKeyFromBytes(testPrivKeyBytes)
61

62
        testInvoiceDescription = "coffee"
63

64
        testInvoiceAmount = lnwire.MilliSatoshi(100000)
65

66
        testNetParams = &chaincfg.MainNetParams
67

68
        testMessageSigner = zpay32.MessageSigner{
69
                SignCompact: func(msg []byte) ([]byte, error) {
×
70
                        hash := chainhash.HashB(msg)
×
71
                        sig := ecdsa.SignCompact(testPrivKey, hash, true)
×
72

×
73
                        return sig, nil
×
74
                },
×
75
        }
76

77
        testFeatures = lnwire.NewFeatureVector(
78
                nil, lnwire.Features,
79
        )
80
)
81

82
func newTestInvoice(t *testing.T, preimage lntypes.Preimage,
83
        timestamp time.Time, expiry time.Duration) *Invoice {
×
84

×
85
        t.Helper()
×
86

×
87
        if expiry == 0 {
×
88
                expiry = time.Hour
×
89
        }
×
90

91
        var payAddr [32]byte
×
92
        if _, err := rand.Read(payAddr[:]); err != nil {
×
93
                t.Fatalf("unable to generate payment addr: %v", err)
×
94
        }
×
95

96
        rawInvoice, err := zpay32.NewInvoice(
×
97
                testNetParams,
×
98
                preimage.Hash(),
×
99
                timestamp,
×
100
                zpay32.Amount(testInvoiceAmount),
×
101
                zpay32.Description(testInvoiceDescription),
×
102
                zpay32.Expiry(expiry),
×
103
                zpay32.PaymentAddr(payAddr),
×
104
        )
×
105
        require.NoError(t, err, "Error while creating new invoice")
×
106

×
107
        paymentRequest, err := rawInvoice.Encode(testMessageSigner)
×
108

×
109
        require.NoError(t, err, "Error while encoding payment request")
×
110

×
111
        return &Invoice{
×
112
                Terms: ContractTerm{
×
113
                        PaymentPreimage: &preimage,
×
114
                        PaymentAddr:     payAddr,
×
115
                        Value:           testInvoiceAmount,
×
116
                        Expiry:          expiry,
×
117
                        Features:        testFeatures,
×
118
                },
×
119
                PaymentRequest: []byte(paymentRequest),
×
120
                CreationDate:   timestamp,
×
121
        }
×
122
}
123

124
// invoiceExpiryTestData simply holds generated expired and pending invoices.
125
type invoiceExpiryTestData struct {
126
        expiredInvoices map[lntypes.Hash]*Invoice
127
        pendingInvoices map[lntypes.Hash]*Invoice
128
}
129

130
// generateInvoiceExpiryTestData generates the specified number of fake expired
131
// and pending invoices anchored to the passed now timestamp.
132
func generateInvoiceExpiryTestData(
133
        t *testing.T, now time.Time,
134
        offset, numExpired, numPending int) invoiceExpiryTestData {
×
135

×
136
        t.Helper()
×
137

×
138
        var testData invoiceExpiryTestData
×
139

×
140
        testData.expiredInvoices = make(map[lntypes.Hash]*Invoice)
×
141
        testData.pendingInvoices = make(map[lntypes.Hash]*Invoice)
×
142

×
143
        expiredCreationDate := now.Add(-24 * time.Hour)
×
144

×
145
        for i := 1; i <= numExpired; i++ {
×
146
                var preimage lntypes.Preimage
×
147
                binary.BigEndian.PutUint32(preimage[:4], uint32(offset+i))
×
148
                duration := (i + offset) % 24
×
149
                expiry := time.Duration(duration) * time.Hour
×
150
                invoice := newTestInvoice(
×
151
                        t, preimage, expiredCreationDate, expiry,
×
152
                )
×
153
                testData.expiredInvoices[preimage.Hash()] = invoice
×
154
        }
×
155

156
        for i := 1; i <= numPending; i++ {
×
157
                var preimage lntypes.Preimage
×
158
                binary.BigEndian.PutUint32(preimage[4:], uint32(offset+i))
×
159
                duration := (i + offset) % 24
×
160
                expiry := time.Duration(duration) * time.Hour
×
161
                invoice := newTestInvoice(t, preimage, now, expiry)
×
162
                testData.pendingInvoices[preimage.Hash()] = invoice
×
163
        }
×
164

165
        return testData
×
166
}
167

168
type hodlExpiryTest struct {
169
        hash         lntypes.Hash
170
        state        ContractState
171
        stateLock    sync.Mutex
172
        mockNotifier *mockChainNotifier
173
        mockClock    *clock.TestClock
174
        cancelChan   chan lntypes.Hash
175
        watcher      *InvoiceExpiryWatcher
176
}
177

178
func (h *hodlExpiryTest) setState(state ContractState) {
×
179
        h.stateLock.Lock()
×
180
        defer h.stateLock.Unlock()
×
181

×
182
        h.state = state
×
183
}
×
184

185
func (h *hodlExpiryTest) announceBlock(t *testing.T, height uint32) {
×
186
        t.Helper()
×
187

×
188
        select {
×
189
        case h.mockNotifier.blockChan <- &chainntnfs.BlockEpoch{
190
                Height: int32(height),
191
        }:
×
192

193
        case <-time.After(testTimeout):
×
194
                t.Fatalf("block %v not consumed", height)
×
195
        }
196
}
197

198
func (h *hodlExpiryTest) assertCanceled(t *testing.T, expected lntypes.Hash) {
×
199
        t.Helper()
×
200

×
201
        select {
×
202
        case actual := <-h.cancelChan:
×
203
                require.Equal(t, expected, actual)
×
204

205
        case <-time.After(testTimeout):
×
206
                t.Fatalf("invoice: %v not canceled", h.hash)
×
207
        }
208
}
209

210
// setupHodlExpiry creates a hodl invoice in our expiry watcher and runs an
211
// arbitrary update function which advances the invoices's state.
212
func setupHodlExpiry(t *testing.T, creationDate time.Time,
213
        expiry time.Duration, heightDelta uint32,
214
        startState ContractState,
215
        startHtlcs []*InvoiceHTLC) *hodlExpiryTest {
×
216

×
217
        t.Helper()
×
218

×
219
        mockNotifier := newMockNotifier()
×
220
        mockClock := clock.NewTestClock(testTime)
×
221

×
222
        test := &hodlExpiryTest{
×
223
                state: startState,
×
224
                watcher: NewInvoiceExpiryWatcher(
×
225
                        mockClock, heightDelta, uint32(testCurrentHeight), nil,
×
226
                        mockNotifier,
×
227
                ),
×
228
                cancelChan:   make(chan lntypes.Hash),
×
229
                mockNotifier: mockNotifier,
×
230
                mockClock:    mockClock,
×
231
        }
×
232

×
233
        // Use an unbuffered channel to block on cancel calls so that the test
×
234
        // does not exit before we've processed all the invoices we expect.
×
235
        cancelImpl := func(paymentHash lntypes.Hash, force bool) error {
×
236
                test.stateLock.Lock()
×
237
                currentState := test.state
×
238
                test.stateLock.Unlock()
×
239

×
240
                if currentState != ContractOpen && !force {
×
241
                        return nil
×
242
                }
×
243

244
                select {
×
245
                case test.cancelChan <- paymentHash:
×
246
                case <-time.After(testTimeout):
×
247
                }
248

249
                return nil
×
250
        }
251

252
        require.NoError(t, test.watcher.Start(cancelImpl))
×
253

×
254
        // We set preimage and hash so that we can use our existing test
×
255
        // helpers. In practice we would only have the hash, but this does not
×
256
        // affect what we're testing at all.
×
257
        preimage := lntypes.Preimage{1}
×
258
        test.hash = preimage.Hash()
×
259

×
260
        invoice := newTestInvoice(t, preimage, creationDate, expiry)
×
261
        invoice.State = startState
×
262
        invoice.HodlInvoice = true
×
263
        invoice.Htlcs = make(map[CircuitKey]*InvoiceHTLC)
×
264

×
265
        // If we have any htlcs, add them with unique circult keys.
×
266
        for i, htlc := range startHtlcs {
×
267
                key := CircuitKey{
×
268
                        HtlcID: uint64(i),
×
269
                }
×
270

×
271
                invoice.Htlcs[key] = htlc
×
272
        }
×
273

274
        // Create an expiry entry for our invoice in its starting state. This
275
        // mimics adding invoices to the watcher on start.
276
        entry := makeInvoiceExpiry(test.hash, invoice)
×
277
        test.watcher.AddInvoices(entry)
×
278

×
279
        return test
×
280
}
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