mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-26 13:42:49 +02:00
multi: fix linter issues
This commit is contained in:
@@ -137,6 +137,7 @@ var (
|
||||
"does not exist")
|
||||
)
|
||||
|
||||
//nolint:ll
|
||||
var (
|
||||
// paymentsRootBucket is the name of the top-level bucket within the
|
||||
// database that stores all data related to payments. Within this
|
||||
@@ -347,7 +348,7 @@ func (p *KVPaymentsDB) InitPayment(paymentHash lntypes.Hash,
|
||||
// are initializing a payment that was attempted earlier, but
|
||||
// left in a state where we could retry.
|
||||
err = bucket.DeleteNestedBucket(paymentHtlcsBucket)
|
||||
if err != nil && err != kvdb.ErrBucketNotFound {
|
||||
if err != nil && !errors.Is(err, kvdb.ErrBucketNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -372,6 +373,7 @@ func (p *KVPaymentsDB) DeleteFailedAttempts(hash lntypes.Hash) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -395,6 +397,7 @@ func createPaymentIndexEntry(tx kvdb.RwTx, sequenceNumber []byte,
|
||||
}
|
||||
|
||||
indexes := tx.ReadWriteBucket(paymentsIndexBucket)
|
||||
|
||||
return indexes.Put(sequenceNumber, b.Bytes())
|
||||
}
|
||||
|
||||
@@ -562,6 +565,7 @@ func (p *KVPaymentsDB) RegisterAttempt(paymentHash lntypes.Hash,
|
||||
|
||||
// Retrieve attempt info for the notification.
|
||||
payment, err = fetchPayment(bucket)
|
||||
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -637,17 +641,20 @@ func (p *KVPaymentsDB) updateHtlcKey(paymentHash lntypes.Hash,
|
||||
return fmt.Errorf("htlcs bucket not found")
|
||||
}
|
||||
|
||||
if htlcsBucket.Get(htlcBucketKey(htlcAttemptInfoKey, aid)) == nil {
|
||||
attemptKey := htlcBucketKey(htlcAttemptInfoKey, aid)
|
||||
if htlcsBucket.Get(attemptKey) == nil {
|
||||
return fmt.Errorf("HTLC with ID %v not registered",
|
||||
attemptID)
|
||||
}
|
||||
|
||||
// Make sure the shard is not already failed or settled.
|
||||
if htlcsBucket.Get(htlcBucketKey(htlcFailInfoKey, aid)) != nil {
|
||||
failKey := htlcBucketKey(htlcFailInfoKey, aid)
|
||||
if htlcsBucket.Get(failKey) != nil {
|
||||
return ErrAttemptAlreadyFailed
|
||||
}
|
||||
|
||||
if htlcsBucket.Get(htlcBucketKey(htlcSettleInfoKey, aid)) != nil {
|
||||
settleKey := htlcBucketKey(htlcSettleInfoKey, aid)
|
||||
if htlcsBucket.Get(settleKey) != nil {
|
||||
return ErrAttemptAlreadySettled
|
||||
}
|
||||
|
||||
@@ -659,6 +666,7 @@ func (p *KVPaymentsDB) updateHtlcKey(paymentHash lntypes.Hash,
|
||||
|
||||
// Retrieve attempt info for the notification.
|
||||
payment, err = fetchPayment(bucket)
|
||||
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -687,7 +695,7 @@ func (p *KVPaymentsDB) Fail(paymentHash lntypes.Hash,
|
||||
|
||||
prefetchPayment(tx, paymentHash)
|
||||
bucket, err := fetchPaymentBucketUpdate(tx, paymentHash)
|
||||
if err == ErrPaymentNotInitiated {
|
||||
if errors.Is(err, ErrPaymentNotInitiated) {
|
||||
updateErr = ErrPaymentNotInitiated
|
||||
return nil
|
||||
} else if err != nil {
|
||||
@@ -802,7 +810,6 @@ func fetchPaymentBucket(tx kvdb.RTx, paymentHash lntypes.Hash) (
|
||||
}
|
||||
|
||||
return bucket, nil
|
||||
|
||||
}
|
||||
|
||||
// fetchPaymentBucketUpdate is identical to fetchPaymentBucket, but it returns a
|
||||
@@ -843,6 +850,7 @@ func (p *KVPaymentsDB) nextPaymentSequence() ([]byte, error) {
|
||||
|
||||
currPaymentSeq = paymentsBucket.Sequence()
|
||||
newUpperBound = currPaymentSeq + paymentSeqBlockSize
|
||||
|
||||
return paymentsBucket.SetSequence(newUpperBound)
|
||||
}, func() {}); err != nil {
|
||||
return nil, err
|
||||
@@ -928,6 +936,7 @@ func (p *KVPaymentsDB) FetchInFlightPayments() ([]*MPPayment, error) {
|
||||
}
|
||||
|
||||
inFlights = append(inFlights, p)
|
||||
|
||||
return nil
|
||||
})
|
||||
}, func() {
|
||||
@@ -952,12 +961,11 @@ func htlcBucketKey(prefix, id []byte) []byte {
|
||||
key := make([]byte, len(prefix)+len(id))
|
||||
copy(key, prefix)
|
||||
copy(key[len(prefix):], id)
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
// FetchPayments returns all sent payments found in the DB.
|
||||
//
|
||||
// nolint: dupl
|
||||
func (p *KVPaymentsDB) FetchPayments() ([]*MPPayment, error) {
|
||||
var payments []*MPPayment
|
||||
|
||||
@@ -993,6 +1001,7 @@ func (p *KVPaymentsDB) FetchPayments() ([]*MPPayment, error) {
|
||||
}
|
||||
|
||||
payments = append(payments, duplicatePayments...)
|
||||
|
||||
return nil
|
||||
})
|
||||
}, func() {
|
||||
@@ -1017,6 +1026,7 @@ func fetchCreationInfo(bucket kvdb.RBucket) (*PaymentCreationInfo, error) {
|
||||
}
|
||||
|
||||
r := bytes.NewReader(b)
|
||||
|
||||
return deserializePaymentCreationInfo(r)
|
||||
}
|
||||
|
||||
@@ -1196,7 +1206,9 @@ func fetchFailedHtlcKeys(bucket kvdb.RBucket) ([][]byte, error) {
|
||||
// QueryPayments is a query to the payments database which is restricted
|
||||
// to a subset of payments by the payments query, containing an offset
|
||||
// index and a maximum number of returned payments.
|
||||
func (p *KVPaymentsDB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
|
||||
func (p *KVPaymentsDB) QueryPayments(query PaymentsQuery) (PaymentsResponse,
|
||||
error) {
|
||||
|
||||
var resp PaymentsResponse
|
||||
|
||||
if err := kvdb.View(p.db, func(tx kvdb.RTx) error {
|
||||
@@ -1263,6 +1275,7 @@ func (p *KVPaymentsDB) QueryPayments(query PaymentsQuery) (PaymentsResponse, err
|
||||
// At this point, we've exhausted the offset, so we'll
|
||||
// begin collecting invoices found within the range.
|
||||
resp.Payments = append(resp.Payments, payment)
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -1294,7 +1307,8 @@ func (p *KVPaymentsDB) QueryPayments(query PaymentsQuery) (PaymentsResponse, err
|
||||
|
||||
// In non-boltdb database backends, there's a faster
|
||||
// ForAll query that allows for batch fetching items.
|
||||
if fastBucket, ok := indexes.(kvdb.ExtendedRBucket); ok {
|
||||
fastBucket, ok := indexes.(kvdb.ExtendedRBucket)
|
||||
if ok {
|
||||
err = fastBucket.ForAll(countFn)
|
||||
} else {
|
||||
err = indexes.ForEach(countFn)
|
||||
@@ -1456,7 +1470,9 @@ func (p *KVPaymentsDB) DeletePayment(paymentHash lntypes.Hash,
|
||||
|
||||
for _, htlcID := range toDelete {
|
||||
err = htlcsBucket.Delete(
|
||||
htlcBucketKey(htlcAttemptInfoKey, htlcID),
|
||||
htlcBucketKey(
|
||||
htlcAttemptInfoKey, htlcID,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1470,7 +1486,9 @@ func (p *KVPaymentsDB) DeletePayment(paymentHash lntypes.Hash,
|
||||
}
|
||||
|
||||
err = htlcsBucket.Delete(
|
||||
htlcBucketKey(htlcSettleInfoKey, htlcID),
|
||||
htlcBucketKey(
|
||||
htlcSettleInfoKey, htlcID,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1485,7 +1503,8 @@ func (p *KVPaymentsDB) DeletePayment(paymentHash lntypes.Hash,
|
||||
return err
|
||||
}
|
||||
|
||||
if err := payments.DeleteNestedBucket(paymentHash[:]); err != nil {
|
||||
err = payments.DeleteNestedBucket(paymentHash[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1505,7 +1524,9 @@ func (p *KVPaymentsDB) DeletePayment(paymentHash lntypes.Hash,
|
||||
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
|
||||
// attempts. The method returns the number of deleted payments, which is always
|
||||
// 0 if failedHtlcsOnly is set.
|
||||
func (p *KVPaymentsDB) DeletePayments(failedOnly, failedHtlcsOnly bool) (int, error) {
|
||||
func (p *KVPaymentsDB) DeletePayments(failedOnly,
|
||||
failedHtlcsOnly bool) (int, error) {
|
||||
|
||||
var numPayments int
|
||||
err := kvdb.Update(p.db, func(tx kvdb.RwTx) error {
|
||||
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
||||
@@ -1585,6 +1606,7 @@ func (p *KVPaymentsDB) DeletePayments(failedOnly, failedHtlcsOnly bool) (int, er
|
||||
|
||||
deleteIndexes = append(deleteIndexes, seqNrs...)
|
||||
numPayments++
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1675,7 +1697,6 @@ func fetchSequenceNumbers(paymentBucket kvdb.RBucket) ([][]byte, error) {
|
||||
return sequenceNumbers, nil
|
||||
}
|
||||
|
||||
// nolint: dupl
|
||||
func serializePaymentCreationInfo(w io.Writer, c *PaymentCreationInfo) error {
|
||||
var scratch [8]byte
|
||||
|
||||
@@ -1697,7 +1718,7 @@ func serializePaymentCreationInfo(w io.Writer, c *PaymentCreationInfo) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := w.Write(c.PaymentRequest[:]); err != nil {
|
||||
if _, err := w.Write(c.PaymentRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1739,7 +1760,7 @@ func deserializePaymentCreationInfo(r io.Reader) (*PaymentCreationInfo,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reqLen := uint32(byteOrder.Uint32(scratch[:4]))
|
||||
reqLen := byteOrder.Uint32(scratch[:4])
|
||||
payReq := make([]byte, reqLen)
|
||||
if reqLen > 0 {
|
||||
if _, err := io.ReadFull(r, payReq); err != nil {
|
||||
@@ -1827,7 +1848,7 @@ func deserializeHTLCAttemptInfo(r io.Reader) (*HTLCAttemptInfo, error) {
|
||||
switch {
|
||||
// Older payment attempts wouldn't have the hash set, in which case we
|
||||
// can just return.
|
||||
case err == io.EOF, err == io.ErrUnexpectedEOF:
|
||||
case errors.Is(err, io.EOF), errors.Is(err, io.ErrUnexpectedEOF):
|
||||
return a, nil
|
||||
|
||||
case err != nil:
|
||||
|
@@ -1405,7 +1405,9 @@ func createTestPayments(t *testing.T, p *KVPaymentsDB, payments []*payment) {
|
||||
// indices for the slice asserts that exactly the same payments in the
|
||||
// slice for the provided indices exist when fetching payments from the
|
||||
// database.
|
||||
func assertPayments(t *testing.T, paymentDB *KVPaymentsDB, payments []*payment) {
|
||||
func assertPayments(t *testing.T, paymentDB *KVPaymentsDB,
|
||||
payments []*payment) {
|
||||
|
||||
t.Helper()
|
||||
|
||||
dbPayments, err := paymentDB.FetchPayments()
|
||||
@@ -1544,7 +1546,9 @@ func testSerializeRoute(t *testing.T, route route.Route) {
|
||||
}
|
||||
|
||||
// deletePayment removes a payment with paymentHash from the payments database.
|
||||
func deletePayment(t *testing.T, db *DB, paymentHash lntypes.Hash, seqNr uint64) {
|
||||
func deletePayment(t *testing.T, db *DB, paymentHash lntypes.Hash,
|
||||
seqNr uint64) {
|
||||
|
||||
t.Helper()
|
||||
|
||||
err := kvdb.Update(db, func(tx kvdb.RwTx) error {
|
||||
@@ -1561,6 +1565,7 @@ func deletePayment(t *testing.T, db *DB, paymentHash lntypes.Hash, seqNr uint64)
|
||||
|
||||
// Delete the index that references this payment.
|
||||
indexes := tx.ReadWriteBucket(paymentsIndexBucket)
|
||||
|
||||
return indexes.Delete(key)
|
||||
}, func() {})
|
||||
|
||||
@@ -1622,10 +1627,12 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
|
||||
|
||||
// Add two duplicates to our second payment.
|
||||
appendDuplicatePayment(
|
||||
t, db, hasDuplicates.PaymentIdentifier, duplicateOneSeqNr, preimg,
|
||||
t, db, hasDuplicates.PaymentIdentifier, duplicateOneSeqNr,
|
||||
preimg,
|
||||
)
|
||||
appendDuplicatePayment(
|
||||
t, db, hasDuplicates.PaymentIdentifier, duplicateTwoSeqNr, preimg,
|
||||
t, db, hasDuplicates.PaymentIdentifier, duplicateTwoSeqNr,
|
||||
preimg,
|
||||
)
|
||||
|
||||
tests := []struct {
|
||||
@@ -1665,7 +1672,8 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
|
||||
expectedErr: ErrDuplicateNotFound,
|
||||
},
|
||||
{
|
||||
name: "lookup duplicate, no duplicates bucket",
|
||||
name: "lookup duplicate, no duplicates " +
|
||||
"bucket",
|
||||
paymentHash: noDuplicates.PaymentIdentifier,
|
||||
sequenceNumber: duplicateTwoSeqNr,
|
||||
expectedErr: ErrNoDuplicateBucket,
|
||||
@@ -1680,12 +1688,15 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) {
|
||||
db, func(tx walletdb.ReadWriteTx) error {
|
||||
var seqNrBytes [8]byte
|
||||
byteOrder.PutUint64(
|
||||
seqNrBytes[:], test.sequenceNumber,
|
||||
seqNrBytes[:],
|
||||
test.sequenceNumber,
|
||||
)
|
||||
|
||||
//nolint:ll
|
||||
_, err := fetchPaymentWithSequenceNumber(
|
||||
tx, test.paymentHash, seqNrBytes[:],
|
||||
)
|
||||
|
||||
return err
|
||||
}, func() {},
|
||||
)
|
||||
|
@@ -432,9 +432,9 @@ func TestQueryPayments(t *testing.T) {
|
||||
}
|
||||
|
||||
if len(allPayments) != 6 {
|
||||
t.Fatalf("Number of payments received does not "+
|
||||
"match expected one. Got %v, want %v.",
|
||||
len(allPayments), 6)
|
||||
t.Fatalf("Number of payments received does "+
|
||||
"not match expected one. Got %v, "+
|
||||
"want %v.", len(allPayments), 6)
|
||||
}
|
||||
|
||||
querySlice, err := paymentDB.QueryPayments(tt.query)
|
||||
@@ -445,7 +445,8 @@ func TestQueryPayments(t *testing.T) {
|
||||
tt.lastIndex != querySlice.LastIndexOffset {
|
||||
|
||||
t.Errorf("First or last index does not match "+
|
||||
"expected index. Want (%d, %d), got (%d, %d).",
|
||||
"expected index. Want (%d, %d), "+
|
||||
"got (%d, %d).",
|
||||
tt.firstIndex, tt.lastIndex,
|
||||
querySlice.FirstIndexOffset,
|
||||
querySlice.LastIndexOffset)
|
||||
@@ -453,14 +454,16 @@ func TestQueryPayments(t *testing.T) {
|
||||
|
||||
if len(querySlice.Payments) != len(tt.expectedSeqNrs) {
|
||||
t.Errorf("expected: %v payments, got: %v",
|
||||
len(tt.expectedSeqNrs), len(querySlice.Payments))
|
||||
len(tt.expectedSeqNrs),
|
||||
len(querySlice.Payments))
|
||||
}
|
||||
|
||||
for i, seqNr := range tt.expectedSeqNrs {
|
||||
q := querySlice.Payments[i]
|
||||
if seqNr != q.SequenceNum {
|
||||
t.Errorf("sequence numbers do not match, "+
|
||||
"got %v, want %v", q.SequenceNum, seqNr)
|
||||
t.Errorf("sequence numbers do not "+
|
||||
"match, got %v, want %v",
|
||||
q.SequenceNum, seqNr)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user