From 36fcf65e97096c0f4fdb8c8a4a49286761da5663 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 17 Jul 2023 12:53:22 +0200 Subject: [PATCH] funding+channeldb: rename fwding -> forwarding --- channeldb/forwarding_policy.go | 36 +++++++++++++++----------- funding/manager.go | 46 ++++++++++++++++++---------------- funding/manager_test.go | 14 +++++++---- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/channeldb/forwarding_policy.go b/channeldb/forwarding_policy.go index e158341dd..8d86b8624 100644 --- a/channeldb/forwarding_policy.go +++ b/channeldb/forwarding_policy.go @@ -3,20 +3,22 @@ package channeldb import "github.com/lightningnetwork/lnd/kvdb" var ( - // initialChannelFwdingPolicyBucket is the database bucket used to store - // the forwarding policy for each permanent channel that is currently - // in the process of being opened. - initialChannelFwdingPolicyBucket = []byte("initialChannelFwdingPolicy") + // initialChannelForwardingPolicyBucket is the database bucket used to + // store the forwarding policy for each permanent channel that is + // currently in the process of being opened. + initialChannelForwardingPolicyBucket = []byte( + "initialChannelFwdingPolicy", + ) ) -// SaveInitialFwdingPolicy saves the serialized forwarding policy for the -// provided permanent channel id to the initialChannelFwdingPolicyBucket. -func (c *ChannelStateDB) SaveInitialFwdingPolicy(chanID, +// SaveInitialForwardingPolicy saves the serialized forwarding policy for the +// provided permanent channel id to the initialChannelForwardingPolicyBucket. +func (c *ChannelStateDB) SaveInitialForwardingPolicy(chanID, forwardingPolicy []byte) error { return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { bucket, err := tx.CreateTopLevelBucket( - initialChannelFwdingPolicyBucket, + initialChannelForwardingPolicyBucket, ) if err != nil { return err @@ -26,13 +28,15 @@ func (c *ChannelStateDB) SaveInitialFwdingPolicy(chanID, }, func() {}) } -// GetInitialFwdingPolicy fetches the serialized forwarding policy for the +// GetInitialForwardingPolicy fetches the serialized forwarding policy for the // provided channel id from the database, or returns ErrChannelNotFound if // a forwarding policy for this channel id is not found. -func (c *ChannelStateDB) GetInitialFwdingPolicy(chanID []byte) ([]byte, error) { +func (c *ChannelStateDB) GetInitialForwardingPolicy(chanID []byte) ([]byte, + error) { + var serializedState []byte err := kvdb.View(c.backend, func(tx kvdb.RTx) error { - bucket := tx.ReadBucket(initialChannelFwdingPolicyBucket) + bucket := tx.ReadBucket(initialChannelForwardingPolicyBucket) if bucket == nil { // If the bucket does not exist, it means we // never added a channel fees to the db, so @@ -54,11 +58,13 @@ func (c *ChannelStateDB) GetInitialFwdingPolicy(chanID []byte) ([]byte, error) { return serializedState, err } -// DeleteInitialFwdingPolicy removes the forwarding policy for a given channel -// from the database. -func (c *ChannelStateDB) DeleteInitialFwdingPolicy(chanID []byte) error { +// DeleteInitialForwardingPolicy removes the forwarding policy for a given +// channel from the database. +func (c *ChannelStateDB) DeleteInitialForwardingPolicy(chanID []byte) error { return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { - bucket := tx.ReadWriteBucket(initialChannelFwdingPolicyBucket) + bucket := tx.ReadWriteBucket( + initialChannelForwardingPolicyBucket, + ) if bucket == nil { return ErrChannelNotFound } diff --git a/funding/manager.go b/funding/manager.go index 9d8c300a5..5469fe1f8 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -2372,7 +2372,7 @@ func (f *Manager) handleFundingCreated(peer lnpeer.Peer, // With a permanent channel id established we can save the respective // forwarding policy in the database. In the channel announcement phase // this forwarding policy is retrieved and applied. - err = f.saveInitialFwdingPolicy(cid.chanID, &forwardingPolicy) + err = f.saveInitialForwardingPolicy(cid.chanID, &forwardingPolicy) if err != nil { log.Errorf("Unable to store the forwarding policy: %v", err) } @@ -2475,7 +2475,9 @@ func (f *Manager) handleFundingSigned(peer lnpeer.Peer, // We have to store the forwardingPolicy before the reservation context // is deleted. The policy will then be read and applied in // newChanAnnouncement. - err = f.saveInitialFwdingPolicy(permChanID, &resCtx.forwardingPolicy) + err = f.saveInitialForwardingPolicy( + permChanID, &resCtx.forwardingPolicy, + ) if err != nil { log.Errorf("Unable to store the forwarding policy: %v", err) } @@ -3315,7 +3317,7 @@ func (f *Manager) annAfterSixConfs(completeChan *channeldb.OpenChannel, // For private channels we do not announce the channel policy // to the network but still need to delete them from the // database. - err = f.deleteInitialFwdingPolicy(chanID) + err = f.deleteInitialForwardingPolicy(chanID) if err != nil { log.Infof("Could not delete channel fees "+ "for chanId %x.", chanID) @@ -3818,7 +3820,7 @@ func (f *Manager) newChanAnnouncement(localPubKey, // forwarding policy to be announced. If no persisted initial policy // values are found, then we will use the default policy values in the // channel announcement. - storedFwdingPolicy, err := f.getInitialFwdingPolicy(chanID) + storedFwdingPolicy, err := f.getInitialForwardingPolicy(chanID) if err != nil && !errors.Is(err, channeldb.ErrChannelNotFound) { return nil, errors.Errorf("unable to generate channel "+ "update announcement: %v", err) @@ -3950,7 +3952,7 @@ func (f *Manager) announceChannel(localIDKey, remoteIDKey *btcec.PublicKey, // After the fee parameters have been stored in the announcement // we can delete them from the database. - err = f.deleteInitialFwdingPolicy(chanID) + err = f.deleteInitialForwardingPolicy(chanID) if err != nil { log.Infof("Could not delete channel fees for chanId %x.", chanID) @@ -4634,7 +4636,7 @@ func (f *Manager) defaultForwardingPolicy( // saveInitialForwardingPolicy saves the forwarding policy for the provided // chanPoint in the channelOpeningStateBucket. -func (f *Manager) saveInitialFwdingPolicy(permChanID lnwire.ChannelID, +func (f *Manager) saveInitialForwardingPolicy(permChanID lnwire.ChannelID, forwardingPolicy *htlcswitch.ForwardingPolicy) error { chanID := make([]byte, 32) @@ -4647,48 +4649,50 @@ func (f *Manager) saveInitialFwdingPolicy(permChanID lnwire.ChannelID, byteOrder.PutUint64(scratch[24:32], uint64(forwardingPolicy.FeeRate)) byteOrder.PutUint32(scratch[32:], forwardingPolicy.TimeLockDelta) - return f.cfg.ChannelDB.SaveInitialFwdingPolicy(chanID, scratch) + return f.cfg.ChannelDB.SaveInitialForwardingPolicy(chanID, scratch) } -// getInitialFwdingPolicy fetches the initial forwarding policy for a given +// getInitialForwardingPolicy fetches the initial forwarding policy for a given // channel id from the database which will be applied during the channel // announcement phase. -func (f *Manager) getInitialFwdingPolicy(permChanID lnwire.ChannelID) ( - *htlcswitch.ForwardingPolicy, error) { +func (f *Manager) getInitialForwardingPolicy( + permChanID lnwire.ChannelID) (*htlcswitch.ForwardingPolicy, error) { chanID := make([]byte, 32) copy(chanID, permChanID[:]) - value, err := f.cfg.ChannelDB.GetInitialFwdingPolicy(chanID) + value, err := f.cfg.ChannelDB.GetInitialForwardingPolicy(chanID) if err != nil { return nil, err } - var fwdingPolicy htlcswitch.ForwardingPolicy - fwdingPolicy.MinHTLCOut = lnwire.MilliSatoshi( + var forwardingPolicy htlcswitch.ForwardingPolicy + forwardingPolicy.MinHTLCOut = lnwire.MilliSatoshi( byteOrder.Uint64(value[:8]), ) - fwdingPolicy.MaxHTLC = lnwire.MilliSatoshi( + forwardingPolicy.MaxHTLC = lnwire.MilliSatoshi( byteOrder.Uint64(value[8:16]), ) - fwdingPolicy.BaseFee = lnwire.MilliSatoshi( + forwardingPolicy.BaseFee = lnwire.MilliSatoshi( byteOrder.Uint64(value[16:24]), ) - fwdingPolicy.FeeRate = lnwire.MilliSatoshi( + forwardingPolicy.FeeRate = lnwire.MilliSatoshi( byteOrder.Uint64(value[24:32]), ) - fwdingPolicy.TimeLockDelta = byteOrder.Uint32(value[32:36]) + forwardingPolicy.TimeLockDelta = byteOrder.Uint32(value[32:36]) - return &fwdingPolicy, nil + return &forwardingPolicy, nil } -// deleteInitialFwdingPolicy removes channel fees for this chanID from +// deleteInitialForwardingPolicy removes channel fees for this chanID from // the database. -func (f *Manager) deleteInitialFwdingPolicy(permChanID lnwire.ChannelID) error { +func (f *Manager) deleteInitialForwardingPolicy( + permChanID lnwire.ChannelID) error { + chanID := make([]byte, 32) copy(chanID, permChanID[:]) - return f.cfg.ChannelDB.DeleteInitialFwdingPolicy(chanID) + return f.cfg.ChannelDB.DeleteInitialForwardingPolicy(chanID) } // saveChannelOpeningState saves the channelOpeningState for the provided diff --git a/funding/manager_test.go b/funding/manager_test.go index 3fc9dc4ba..a12eeb796 100644 --- a/funding/manager_test.go +++ b/funding/manager_test.go @@ -1347,7 +1347,7 @@ func assertInitialFwdingPolicyNotFound(t *testing.T, node *testNode, if i > 0 { time.Sleep(testPollSleepMs * time.Millisecond) } - fwdingPolicy, err = node.fundingMgr.getInitialFwdingPolicy( + fwdingPolicy, err = node.fundingMgr.getInitialForwardingPolicy( *chanID, ) require.ErrorIs(t, err, channeldb.ErrChannelNotFound) @@ -3173,13 +3173,13 @@ func TestFundingManagerCustomChannelParameters(t *testing.T) { // After the funding is signed and before the channel announcement // we expect Alice and Bob to store their respective fees in the // database. - forwardingPolicy, err := alice.fundingMgr.getInitialFwdingPolicy( + forwardingPolicy, err := alice.fundingMgr.getInitialForwardingPolicy( fundingSigned.ChanID, ) require.NoError(t, err) require.NoError(t, assertFees(forwardingPolicy, 42, 1337)) - forwardingPolicy, err = bob.fundingMgr.getInitialFwdingPolicy( + forwardingPolicy, err = bob.fundingMgr.getInitialForwardingPolicy( fundingSigned.ChanID, ) require.NoError(t, err) @@ -3269,13 +3269,17 @@ func TestFundingManagerCustomChannelParameters(t *testing.T) { // After the announcement we expect Alice and Bob to have cleared // the fees for the channel from the database. - _, err = alice.fundingMgr.getInitialFwdingPolicy(fundingSigned.ChanID) + _, err = alice.fundingMgr.getInitialForwardingPolicy( + fundingSigned.ChanID, + ) if err != channeldb.ErrChannelNotFound { err = fmt.Errorf("channel fees were expected to be deleted" + " but were not") t.Fatal(err) } - _, err = bob.fundingMgr.getInitialFwdingPolicy(fundingSigned.ChanID) + _, err = bob.fundingMgr.getInitialForwardingPolicy( + fundingSigned.ChanID, + ) if err != channeldb.ErrChannelNotFound { err = fmt.Errorf("channel fees were expected to be deleted" + " but were not")