diff --git a/routing/missioncontrol.go b/routing/missioncontrol.go index 39eb99ab4..9d89c8092 100644 --- a/routing/missioncontrol.go +++ b/routing/missioncontrol.go @@ -276,7 +276,7 @@ type paymentResult struct { // newPaymentResult constructs a new paymentResult. func newPaymentResult(id uint64, rt *mcRoute, timeFwd, timeReply time.Time, - failure *paymentFailure) *paymentResult { + failure fn.Option[paymentFailure]) *paymentResult { result := &paymentResult{ id: id, @@ -289,11 +289,13 @@ func newPaymentResult(id uint64, rt *mcRoute, timeFwd, timeReply time.Time, route: tlv.NewRecordT[tlv.TlvType2](*rt), } - if failure != nil { - result.failure = tlv.SomeRecordT( - tlv.NewRecordT[tlv.TlvType3](*failure), - ) - } + failure.WhenSome( + func(f paymentFailure) { + result.failure = tlv.SomeRecordT( + tlv.NewRecordT[tlv.TlvType3](f), + ) + }, + ) return result } @@ -621,7 +623,7 @@ func (m *MissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route, result := newPaymentResult( paymentID, extractMCRoute(rt), timestamp, timestamp, - newPaymentFailure(failureSourceIdx, failure), + fn.Some(newPaymentFailure(failureSourceIdx, failure)), ) return m.processPaymentResult(result) @@ -635,7 +637,8 @@ func (m *MissionControl) ReportPaymentSuccess(paymentID uint64, timestamp := m.cfg.clock.Now() result := newPaymentResult( - paymentID, extractMCRoute(rt), timestamp, timestamp, nil, + paymentID, extractMCRoute(rt), timestamp, timestamp, + fn.None[paymentFailure](), ) _, err := m.processPaymentResult(result) @@ -832,13 +835,13 @@ func (n *namespacedDB) purge() error { // failure with unknown details. Otherwise, the index and failure message are // used to populate the info field of the paymentFailure. func newPaymentFailure(sourceIdx *int, - failureMsg lnwire.FailureMessage) *paymentFailure { + failureMsg lnwire.FailureMessage) paymentFailure { if sourceIdx == nil { - return &paymentFailure{} + return paymentFailure{} } - return &paymentFailure{ + return paymentFailure{ sourceIdx: tlv.SomeRecordT( tlv.NewPrimitiveRecord[tlv.TlvType0]( uint8(*sourceIdx), diff --git a/routing/missioncontrol_store_test.go b/routing/missioncontrol_store_test.go index b83a526e9..b020fcbb4 100644 --- a/routing/missioncontrol_store_test.go +++ b/routing/missioncontrol_store_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/btcsuite/btcwallet/walletdb" + "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lnwire" @@ -85,19 +86,19 @@ func TestMissionControlStore(t *testing.T) { result1 := newPaymentResult( 99, mcStoreTestRoute, testTime, testTime, - newPaymentFailure( + fn.Some(newPaymentFailure( &failureSourceIdx, lnwire.NewFailIncorrectDetails(100, 1000), - ), + )), ) result2 := newPaymentResult( 2, mcStoreTestRoute, testTime.Add(time.Hour), testTime.Add(time.Hour), - newPaymentFailure( + fn.Some(newPaymentFailure( &failureSourceIdx, lnwire.NewFailIncorrectDetails(100, 1000), - ), + )), ) // Store result. @@ -134,7 +135,7 @@ func TestMissionControlStore(t *testing.T) { ) result3.id = 3 result3.failure = tlv.SomeRecordT( - tlv.NewRecordT[tlv.TlvType3](*newPaymentFailure( + tlv.NewRecordT[tlv.TlvType3](newPaymentFailure( &failureSourceIdx, &lnwire.FailMPPTimeout{}, )), ) @@ -153,7 +154,7 @@ func TestMissionControlStore(t *testing.T) { // Also demonstrate the persistence of a success result. result4 := newPaymentResult( 5, mcStoreTestRoute, testTime.Add(3*time.Hour), - testTime.Add(3*time.Hour), nil, + testTime.Add(3*time.Hour), fn.None[paymentFailure](), ) store.AddResult(result4) require.NoError(t, store.storeResults()) @@ -186,7 +187,10 @@ func TestMissionControlStoreFlushing(t *testing.T) { return newPaymentResult( lastID, mcStoreTestRoute, testTime.Add(-time.Hour), testTime, - newPaymentFailure(&failureSourceIdx, failureDetails), + fn.Some(newPaymentFailure( + &failureSourceIdx, + failureDetails, + )), ) } @@ -287,10 +291,10 @@ func BenchmarkMissionControlStoreFlushing(b *testing.B) { result := newPaymentResult( lastID, mcStoreTestRoute, testTimeFwd, testTime, - newPaymentFailure( + fn.Some(newPaymentFailure( &failureSourceIdx, failureDetails, - ), + )), ) store.AddResult(result) } @@ -305,10 +309,10 @@ func BenchmarkMissionControlStoreFlushing(b *testing.B) { results[i] = newPaymentResult( 0, mcStoreTestRoute, testTimeFwd, testTime, - newPaymentFailure( + fn.Some(newPaymentFailure( &failureSourceIdx, failureDetails, - ), + )), ) } diff --git a/routing/result_interpretation_test.go b/routing/result_interpretation_test.go index a0c3e9062..000093b07 100644 --- a/routing/result_interpretation_test.go +++ b/routing/result_interpretation_test.go @@ -732,7 +732,7 @@ func TestResultInterpretation(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { var failure fn.Option[paymentFailure] if !testCase.success { - failure = fn.Some(*newPaymentFailure( + failure = fn.Some(newPaymentFailure( &testCase.failureSrcIdx, testCase.failure, ))