From f576a74bf640a1aee1ed3cad04ba4f6d73c3daa6 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 24 Sep 2017 20:17:09 -0700 Subject: [PATCH] lnwallet: modify SettleHTLC to return value of HTLC settled This commit adds an additional return value to SettleHTLC in order to make way for an upcoming change to modify the way bandwidth status from the link to the switch is reported. --- lnwallet/channel.go | 10 ++++++---- lnwallet/channel_test.go | 14 +++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index dc8c26c8..b7eaef34 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3049,15 +3049,17 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, err // SettleHTLC attempts to settle an existing outstanding received HTLC. The // remote log index of the HTLC settled is returned in order to facilitate // creating the corresponding wire message. In the case the supplied preimage -// is invalid, an error is returned. -func (lc *LightningChannel) SettleHTLC(preimage [32]byte) (uint64, error) { +// is invalid, an error is returned. Additionally, the value of the settled +// HTLC is also returned. +func (lc *LightningChannel) SettleHTLC(preimage [32]byte) (uint64, + lnwire.MilliSatoshi, error) { lc.Lock() defer lc.Unlock() paymentHash := sha256.Sum256(preimage[:]) targetHTLCs, ok := lc.rHashMap[paymentHash] if !ok { - return 0, fmt.Errorf("invalid payment hash(%v)", + return 0, 0, fmt.Errorf("invalid payment hash(%v)", hex.EncodeToString(paymentHash[:])) } targetHTLC := targetHTLCs[0] @@ -3079,7 +3081,7 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte) (uint64, error) { } lc.availableLocalBalance += pd.Amount - return targetHTLC.Index, nil + return targetHTLC.Index, targetHTLC.Amount, nil } // ReceiveHTLCSettle attempts to settle an existing outgoing HTLC indexed by an diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 0a9313b6..ff3780e1 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -562,7 +562,7 @@ func TestSimpleAddSettleWorkflow(t *testing.T) { // HTLC once he learns of the preimage. var preimage [32]byte copy(preimage[:], paymentPreimage) - settleIndex, err := bobChannel.SettleHTLC(preimage) + settleIndex, _, err := bobChannel.SettleHTLC(preimage) if err != nil { t.Fatalf("bob unable to settle inbound htlc: %v", err) } @@ -730,7 +730,7 @@ func TestCheckCommitTxSize(t *testing.T) { for i := 10; i >= 1; i-- { _, preimage := createHTLC(i, lnwire.MilliSatoshi(1e7)) - settleIndex, err := bobChannel.SettleHTLC(preimage) + settleIndex, _, err := bobChannel.SettleHTLC(preimage) if err != nil { t.Fatalf("bob unable to settle inbound htlc: %v", err) } @@ -924,7 +924,7 @@ func TestForceClose(t *testing.T) { } // Settle HTLC and sign new commitment. - settleIndex, err := aliceChannel.SettleHTLC(preimage) + settleIndex, _, err := aliceChannel.SettleHTLC(preimage) if err != nil { t.Fatalf("bob unable to settle inbound htlc: %v", err) } @@ -1126,7 +1126,7 @@ func TestHTLCDustLimit(t *testing.T) { } // Settle HTLC and create a new commitment state. - settleIndex, err := bobChannel.SettleHTLC(preimage) + settleIndex, _, err := bobChannel.SettleHTLC(preimage) if err != nil { t.Fatalf("bob unable to settle inbound htlc: %v", err) } @@ -1190,7 +1190,7 @@ func TestChannelBalanceDustLimit(t *testing.T) { if err := forceStateTransition(aliceChannel, bobChannel); err != nil { t.Fatalf("state transition error: %v", err) } - settleIndex, err := bobChannel.SettleHTLC(preimage) + settleIndex, _, err := bobChannel.SettleHTLC(preimage) if err != nil { t.Fatalf("bob unable to settle inbound htlc: %v", err) } @@ -1438,7 +1438,7 @@ func TestStateUpdatePersistence(t *testing.T) { // Now settle all the HTLCs, then force a state update. The state // update should succeed as both sides have identical. for i := 0; i < 3; i++ { - settleIndex, err := bobChannelNew.SettleHTLC(alicePreimage) + settleIndex, _, err := bobChannelNew.SettleHTLC(alicePreimage) if err != nil { t.Fatalf("unable to settle htlc: %v", err) } @@ -1447,7 +1447,7 @@ func TestStateUpdatePersistence(t *testing.T) { t.Fatalf("unable to settle htlc: %v", err) } } - settleIndex, err := aliceChannelNew.SettleHTLC(bobPreimage) + settleIndex, _, err := aliceChannelNew.SettleHTLC(bobPreimage) if err != nil { t.Fatalf("unable to settle htlc: %v", err) }