mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-09 18:52:09 +02:00
watchtower: extend client databse with CRUD operations for towers
These operations are currently unused, but will be integrated into the TowerClient at a later point as future preparation for the WatchtowerClient RPC subserver, which will allow users to add, remove, and list the watchtowers currntly in use.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"math"
|
||||
"net"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/coreos/bbolt"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
@@ -55,6 +56,11 @@ var (
|
||||
// database.
|
||||
ErrTowerNotFound = errors.New("tower not found")
|
||||
|
||||
// ErrTowerUnackedUpdates is an error returned when we attempt to mark a
|
||||
// tower's sessions as inactive, but one of its sessions has unacked
|
||||
// updates.
|
||||
ErrTowerUnackedUpdates = errors.New("tower has unacked updates")
|
||||
|
||||
// ErrCorruptClientSession signals that the client session's on-disk
|
||||
// structure deviates from what is expected.
|
||||
ErrCorruptClientSession = errors.New("client session corrupted")
|
||||
@@ -199,9 +205,11 @@ func (c *ClientDB) Close() error {
|
||||
return c.db.Close()
|
||||
}
|
||||
|
||||
// CreateTower initializes a database entry with the given lightning address. If
|
||||
// the tower exists, the address is append to the list of all addresses used to
|
||||
// that tower previously.
|
||||
// CreateTower initialize an address record used to communicate with a
|
||||
// watchtower. Each Tower is assigned a unique ID, that is used to amortize
|
||||
// storage costs of the public key when used by multiple sessions. If the tower
|
||||
// already exists, the address is appended to the list of all addresses used to
|
||||
// that tower previously and its corresponding sessions are marked as active.
|
||||
func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
|
||||
var towerPubKey [33]byte
|
||||
copy(towerPubKey[:], lnAddr.IdentityKey.SerializeCompressed())
|
||||
@@ -233,6 +241,32 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
|
||||
// address is a duplicate, this will result in no
|
||||
// change.
|
||||
tower.AddAddress(lnAddr.Address)
|
||||
|
||||
// If there are any client sessions that correspond to
|
||||
// this tower, we'll mark them as active to ensure we
|
||||
// load them upon restarts.
|
||||
//
|
||||
// TODO(wilmer): with an index of tower -> sessions we
|
||||
// can avoid the linear lookup.
|
||||
sessions := tx.Bucket(cSessionBkt)
|
||||
if sessions == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
towerID := TowerIDFromBytes(towerIDBytes)
|
||||
towerSessions, err := listClientSessions(
|
||||
sessions, &towerID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, session := range towerSessions {
|
||||
err := markSessionStatus(
|
||||
sessions, session, CSessionActive,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No such tower exists, create a new tower id for our
|
||||
// new tower. The error is unhandled since NextSequence
|
||||
@@ -265,8 +299,89 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
|
||||
return tower, nil
|
||||
}
|
||||
|
||||
// LoadTower retrieves a tower by its tower ID.
|
||||
func (c *ClientDB) LoadTower(towerID TowerID) (*Tower, error) {
|
||||
// RemoveTower modifies a tower's record within the database. If an address is
|
||||
// provided, then _only_ the address record should be removed from the tower's
|
||||
// persisted state. Otherwise, we'll attempt to mark the tower as inactive by
|
||||
// marking all of its sessions inactive. If any of its sessions has unacked
|
||||
// updates, then ErrTowerUnackedUpdates is returned. If the tower doesn't have
|
||||
// any sessions at all, it'll be completely removed from the database.
|
||||
//
|
||||
// NOTE: An error is not returned if the tower doesn't exist.
|
||||
func (c *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
|
||||
return c.db.Update(func(tx *bbolt.Tx) error {
|
||||
towers := tx.Bucket(cTowerBkt)
|
||||
if towers == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
towerIndex := tx.Bucket(cTowerIndexBkt)
|
||||
if towerIndex == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
|
||||
// Don't return an error if the watchtower doesn't exist to act
|
||||
// as a NOP.
|
||||
pubKeyBytes := pubKey.SerializeCompressed()
|
||||
towerIDBytes := towerIndex.Get(pubKeyBytes)
|
||||
if towerIDBytes == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If an address is provided, then we should _only_ remove the
|
||||
// address record from the database.
|
||||
if addr != nil {
|
||||
tower, err := getTower(towers, towerIDBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tower.RemoveAddress(addr)
|
||||
return putTower(towers, tower)
|
||||
}
|
||||
|
||||
// Otherwise, we should attempt to mark the tower's sessions as
|
||||
// inactive.
|
||||
//
|
||||
// TODO(wilmer): with an index of tower -> sessions we can avoid
|
||||
// the linear lookup.
|
||||
sessions := tx.Bucket(cSessionBkt)
|
||||
if sessions == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
towerID := TowerIDFromBytes(towerIDBytes)
|
||||
towerSessions, err := listClientSessions(sessions, &towerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If it doesn't have any, we can completely remove it from the
|
||||
// database.
|
||||
if len(towerSessions) == 0 {
|
||||
if err := towerIndex.Delete(pubKeyBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
return towers.Delete(towerIDBytes)
|
||||
}
|
||||
|
||||
// We'll mark its sessions as inactive as long as they don't
|
||||
// have any pending updates to ensure we don't load them upon
|
||||
// restarts.
|
||||
for _, session := range towerSessions {
|
||||
if len(session.CommittedUpdates) > 0 {
|
||||
return ErrTowerUnackedUpdates
|
||||
}
|
||||
err := markSessionStatus(
|
||||
sessions, session, CSessionInactive,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// LoadTowerByID retrieves a tower by its tower ID.
|
||||
func (c *ClientDB) LoadTowerByID(towerID TowerID) (*Tower, error) {
|
||||
var tower *Tower
|
||||
err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
towers := tx.Bucket(cTowerBkt)
|
||||
@@ -285,6 +400,60 @@ func (c *ClientDB) LoadTower(towerID TowerID) (*Tower, error) {
|
||||
return tower, nil
|
||||
}
|
||||
|
||||
// LoadTower retrieves a tower by its public key.
|
||||
func (c *ClientDB) LoadTower(pubKey *btcec.PublicKey) (*Tower, error) {
|
||||
var tower *Tower
|
||||
err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
towers := tx.Bucket(cTowerBkt)
|
||||
if towers == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
towerIndex := tx.Bucket(cTowerIndexBkt)
|
||||
if towerIndex == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
|
||||
towerIDBytes := towerIndex.Get(pubKey.SerializeCompressed())
|
||||
if towerIDBytes == nil {
|
||||
return ErrTowerNotFound
|
||||
}
|
||||
|
||||
var err error
|
||||
tower, err = getTower(towers, towerIDBytes)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tower, nil
|
||||
}
|
||||
|
||||
// ListTowers retrieves the list of towers available within the database.
|
||||
func (c *ClientDB) ListTowers() ([]*Tower, error) {
|
||||
var towers []*Tower
|
||||
err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
towerBucket := tx.Bucket(cTowerBkt)
|
||||
if towerBucket == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
|
||||
return towerBucket.ForEach(func(towerIDBytes, _ []byte) error {
|
||||
tower, err := getTower(towerBucket, towerIDBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
towers = append(towers, tower)
|
||||
return nil
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return towers, nil
|
||||
}
|
||||
|
||||
// NextSessionKeyIndex reserves a new session key derivation index for a
|
||||
// particular tower id. The index is reserved for that tower until
|
||||
// CreateClientSession is invoked for that tower and index, at which point a new
|
||||
@@ -871,6 +1040,15 @@ func putClientSessionBody(sessions *bbolt.Bucket,
|
||||
return sessionBkt.Put(cSessionBody, b.Bytes())
|
||||
}
|
||||
|
||||
// markSessionStatus updates the persisted state of the session to the new
|
||||
// status.
|
||||
func markSessionStatus(sessions *bbolt.Bucket, session *ClientSession,
|
||||
status CSessionStatus) error {
|
||||
|
||||
session.Status = status
|
||||
return putClientSessionBody(sessions, session)
|
||||
}
|
||||
|
||||
// getChanSummary loads a ClientChanSummary for the passed chanID.
|
||||
func getChanSummary(chanSummaries *bbolt.Bucket,
|
||||
chanID lnwire.ChannelID) (*ClientChanSummary, error) {
|
||||
|
Reference in New Issue
Block a user