From f4db249cb8be71f65c8f0a6d2af0285f879f2ebc Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 14 Jul 2017 20:40:42 +0200 Subject: [PATCH] htlcswitch: handle update_fee message received from peer. This commit makes the channellink update a channel's fee if an update_fee message is received from the peer. --- htlcswitch/link.go | 22 ++++++++++++++++++++++ htlcswitch/link_test.go | 2 +- htlcswitch/test_utils.go | 21 +++++++++++++-------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 3272969f..acfee55a 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -638,6 +638,15 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { } } }() + case *lnwire.UpdateFee: + // We received fee update from peer. If we are the initator we will fail the + // channel, if not we will apply the update. + fee := msg.FeePerKw + if err := l.channel.ReceiveUpdateFee(fee); err != nil { + log.Errorf("error receiving fee update: %v", err) + l.cfg.Peer.Disconnect() + return + } } } @@ -810,6 +819,19 @@ func (l *channelLink) HandleChannelUpdate(message lnwire.Message) { } } +// updateChannelFee updates the commitment fee-per-kw on this channel by +// committing to an update_fee message. +func (l *channelLink) updateChannelFee(feePerKw btcutil.Amount) error { + // Update local fee. + if err := l.channel.UpdateFee(feePerKw); err != nil { + return err + } + + // Send fee update to remote. + msg := lnwire.NewUpdateFee(l.ChanID(), feePerKw) + return l.cfg.Peer.SendMessage(msg) +} + // processLockedInHtlcs serially processes each of the log updates which have // been "locked-in". An HTLC is considered locked-in once it has been fully // committed to in both the remote and local commitment state. Once a channel diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index a2b6235d..34c4d758 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -252,7 +252,7 @@ func TestChannelLinkBidirectionalOneHopPayments(t *testing.T) { } // TestChannelLinkMultiHopPayment checks the ability to send payment over two -// hopes. In this test we send the payment from Carol to Alice over Bob peer. +// hops. In this test we send the payment from Carol to Alice over Bob peer. // (Carol -> Bob -> Alice) and checking that HTLC was settled properly and // balances were changed in two channels. func TestChannelLinkMultiHopPayment(t *testing.T) { diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 8c2a6d78..6d402425 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -46,8 +46,8 @@ func generateRandomBytes(n int) ([]byte, error) { // createTestChannel creates the channel and returns our and remote channels // representations. -func createTestChannel(alicePrivKey, bobPrivKey []byte, - aliceAmount, bobAmount btcutil.Amount, chanID lnwire.ShortChannelID) ( +func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceAmount, bobAmount, + feePerKw btcutil.Amount, chanID lnwire.ShortChannelID) ( *lnwallet.LightningChannel, *lnwallet.LightningChannel, func(), error) { aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(btcec.S256(), alicePrivKey) @@ -58,6 +58,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, bobDustLimit := btcutil.Amount(800) csvTimeoutAlice := uint32(5) csvTimeoutBob := uint32(4) + commitFee := (feePerKw * btcutil.Amount(724)) / 1000 witnessScript, _, err := lnwallet.GenFundingPkScript( aliceKeyPub.SerializeCompressed(), @@ -107,7 +108,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, bobKeyPub, aliceRevokeKey, csvTimeoutAlice, - aliceAmount, + aliceAmount-commitFee, bobAmount, lnwallet.DefaultDustLimit(), ) @@ -121,7 +122,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, bobRevokeKey, csvTimeoutBob, bobAmount, - aliceAmount, + aliceAmount-commitFee, lnwallet.DefaultDustLimit(), ) if err != nil { @@ -146,13 +147,15 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, aliceChannelState := &channeldb.OpenChannel{ IdentityPub: aliceKeyPub, ChanID: prevOut, + CommitFee: commitFee, + FeePerKw: feePerKw, ChanType: channeldb.SingleFunder, IsInitiator: true, StateHintObsfucator: obsfucator, OurCommitKey: aliceKeyPub, TheirCommitKey: bobKeyPub, Capacity: channelCapacity, - OurBalance: aliceAmount, + OurBalance: aliceAmount - commitFee, TheirBalance: bobAmount, OurCommitTx: aliceCommitTx, OurCommitSig: bytes.Repeat([]byte{1}, 71), @@ -173,6 +176,8 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, bobChannelState := &channeldb.OpenChannel{ IdentityPub: bobKeyPub, ChanID: prevOut, + CommitFee: commitFee, + FeePerKw: feePerKw, ChanType: channeldb.SingleFunder, IsInitiator: false, StateHintObsfucator: obsfucator, @@ -180,7 +185,7 @@ func createTestChannel(alicePrivKey, bobPrivKey []byte, TheirCommitKey: aliceKeyPub, Capacity: channelCapacity, OurBalance: bobAmount, - TheirBalance: aliceAmount, + TheirBalance: aliceAmount - commitFee, OurCommitTx: bobCommitTx, OurCommitSig: bytes.Repeat([]byte{1}, 71), FundingOutpoint: prevOut, @@ -502,13 +507,13 @@ func newThreeHopNetwork(t *testing.T, aliceToBob, // Create lightning channels between Alice<->Bob and Bob<->Carol aliceChannel, firstBobChannel, fCleanUp, err := createTestChannel( - alicePrivKey, bobPrivKey, aliceToBob, aliceToBob, firstChanID) + alicePrivKey, bobPrivKey, aliceToBob, aliceToBob, 0, firstChanID) if err != nil { t.Fatalf("unable to create alice<->bob channel: %v", err) } secondBobChannel, carolChannel, sCleanUp, err := createTestChannel( - bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, secondChanID) + bobPrivKey, carolPrivKey, bobToCarol, bobToCarol, 0, secondChanID) if err != nil { t.Fatalf("unable to create bob<->carol channel: %v", err) }