funding+channeldb: rename fwding -> forwarding

This commit is contained in:
Oliver Gugger committed 2023-08-18 09:44:23 +02:00
1 parent eda34a8d2a
commit 36fcf65e97
3 files changed
+55 -41

No files matched your search

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