diff --git a/channeldb/db.go b/channeldb/db.go index 7515d1ae5..18a66c46e 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -312,11 +312,6 @@ var ( // channelOpeningState for each channel that is currently in the process // of being opened. channelOpeningStateBucket = []byte("channelOpeningState") - - // 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") ) // DB is the primary datastore for the lnd daemon. The database stores @@ -1433,64 +1428,6 @@ func (c *ChannelStateDB) AbandonChannel(chanPoint *wire.OutPoint, return dbChan.CloseChannel(summary, ChanStatusLocalCloseInitiator) } -// SaveInitialFwdingPolicy saves the serialized forwarding policy for the -// provided permanent channel id to the initialChannelFwdingPolicyBucket. -func (c *ChannelStateDB) SaveInitialFwdingPolicy(chanID, - forwardingPolicy []byte) error { - - return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { - bucket, err := tx.CreateTopLevelBucket( - initialChannelFwdingPolicyBucket, - ) - if err != nil { - return err - } - - return bucket.Put(chanID, forwardingPolicy) - }, func() {}) -} - -// GetInitialFwdingPolicy 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) { - var serializedState []byte - err := kvdb.View(c.backend, func(tx kvdb.RTx) error { - bucket := tx.ReadBucket(initialChannelFwdingPolicyBucket) - if bucket == nil { - // If the bucket does not exist, it means we - // never added a channel fees to the db, so - // return ErrChannelNotFound. - return ErrChannelNotFound - } - - stateBytes := bucket.Get(chanID) - if stateBytes == nil { - return ErrChannelNotFound - } - - serializedState = append(serializedState, stateBytes...) - - return nil - }, func() { - serializedState = nil - }) - return serializedState, err -} - -// DeleteInitialFwdingPolicy removes the forwarding policy for a given channel -// from the database. -func (c *ChannelStateDB) DeleteInitialFwdingPolicy(chanID []byte) error { - return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { - bucket := tx.ReadWriteBucket(initialChannelFwdingPolicyBucket) - if bucket == nil { - return ErrChannelNotFound - } - - return bucket.Delete(chanID) - }, func() {}) -} - // SaveChannelOpeningState saves the serialized channel state for the provided // chanPoint to the channelOpeningStateBucket. func (c *ChannelStateDB) SaveChannelOpeningState(outPoint, diff --git a/channeldb/forwarding_policy.go b/channeldb/forwarding_policy.go new file mode 100644 index 000000000..e158341dd --- /dev/null +++ b/channeldb/forwarding_policy.go @@ -0,0 +1,68 @@ +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") +) + +// SaveInitialFwdingPolicy saves the serialized forwarding policy for the +// provided permanent channel id to the initialChannelFwdingPolicyBucket. +func (c *ChannelStateDB) SaveInitialFwdingPolicy(chanID, + forwardingPolicy []byte) error { + + return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { + bucket, err := tx.CreateTopLevelBucket( + initialChannelFwdingPolicyBucket, + ) + if err != nil { + return err + } + + return bucket.Put(chanID, forwardingPolicy) + }, func() {}) +} + +// GetInitialFwdingPolicy 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) { + var serializedState []byte + err := kvdb.View(c.backend, func(tx kvdb.RTx) error { + bucket := tx.ReadBucket(initialChannelFwdingPolicyBucket) + if bucket == nil { + // If the bucket does not exist, it means we + // never added a channel fees to the db, so + // return ErrChannelNotFound. + return ErrChannelNotFound + } + + stateBytes := bucket.Get(chanID) + if stateBytes == nil { + return ErrChannelNotFound + } + + serializedState = append(serializedState, stateBytes...) + + return nil + }, func() { + serializedState = nil + }) + return serializedState, err +} + +// DeleteInitialFwdingPolicy removes the forwarding policy for a given channel +// from the database. +func (c *ChannelStateDB) DeleteInitialFwdingPolicy(chanID []byte) error { + return kvdb.Update(c.backend, func(tx kvdb.RwTx) error { + bucket := tx.ReadWriteBucket(initialChannelFwdingPolicyBucket) + if bucket == nil { + return ErrChannelNotFound + } + + return bucket.Delete(chanID) + }, func() {}) +}