2023-07-17 12:53:21 +02:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import "github.com/lightningnetwork/lnd/kvdb"
|
|
|
|
|
|
|
|
var (
|
2023-07-17 12:53:22 +02:00
|
|
|
// 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",
|
|
|
|
)
|
2023-07-17 12:53:21 +02:00
|
|
|
)
|
|
|
|
|
2023-07-17 12:53:22 +02:00
|
|
|
// SaveInitialForwardingPolicy saves the serialized forwarding policy for the
|
|
|
|
// provided permanent channel id to the initialChannelForwardingPolicyBucket.
|
|
|
|
func (c *ChannelStateDB) SaveInitialForwardingPolicy(chanID,
|
2023-07-17 12:53:21 +02:00
|
|
|
forwardingPolicy []byte) error {
|
|
|
|
|
|
|
|
return kvdb.Update(c.backend, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(
|
2023-07-17 12:53:22 +02:00
|
|
|
initialChannelForwardingPolicyBucket,
|
2023-07-17 12:53:21 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(chanID, forwardingPolicy)
|
|
|
|
}, func() {})
|
|
|
|
}
|
|
|
|
|
2023-07-17 12:53:22 +02:00
|
|
|
// GetInitialForwardingPolicy fetches the serialized forwarding policy for the
|
2023-07-17 12:53:21 +02:00
|
|
|
// provided channel id from the database, or returns ErrChannelNotFound if
|
|
|
|
// a forwarding policy for this channel id is not found.
|
2023-07-17 12:53:22 +02:00
|
|
|
func (c *ChannelStateDB) GetInitialForwardingPolicy(chanID []byte) ([]byte,
|
|
|
|
error) {
|
|
|
|
|
2023-07-17 12:53:21 +02:00
|
|
|
var serializedState []byte
|
|
|
|
err := kvdb.View(c.backend, func(tx kvdb.RTx) error {
|
2023-07-17 12:53:22 +02:00
|
|
|
bucket := tx.ReadBucket(initialChannelForwardingPolicyBucket)
|
2023-07-17 12:53:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-17 12:53:22 +02:00
|
|
|
// DeleteInitialForwardingPolicy removes the forwarding policy for a given
|
|
|
|
// channel from the database.
|
|
|
|
func (c *ChannelStateDB) DeleteInitialForwardingPolicy(chanID []byte) error {
|
2023-07-17 12:53:21 +02:00
|
|
|
return kvdb.Update(c.backend, func(tx kvdb.RwTx) error {
|
2023-07-17 12:53:22 +02:00
|
|
|
bucket := tx.ReadWriteBucket(
|
|
|
|
initialChannelForwardingPolicyBucket,
|
|
|
|
)
|
2023-07-17 12:53:21 +02:00
|
|
|
if bucket == nil {
|
|
|
|
return ErrChannelNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Delete(chanID)
|
|
|
|
}, func() {})
|
|
|
|
}
|