wtdb: create and remove tower should no longer change session status

This commit is contained in:
Elle Mouton 2023-11-27 16:43:32 +02:00
parent cbf08940ca
commit 41582c0b8b
No known key found for this signature in database
GPG Key ID: D7D916376026F177
3 changed files with 18 additions and 66 deletions

View File

@ -26,10 +26,9 @@ type DB interface {
// RemoveTower modifies a tower's record within the database. If an // RemoveTower modifies a tower's record within the database. If an
// address is provided, then _only_ the address record should be removed // address is provided, then _only_ the address record should be removed
// from the tower's persisted state. Otherwise, we'll attempt to mark // 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 // the tower as inactive. If any of its sessions have unacked updates,
// of its sessions has unacked updates, then ErrTowerUnackedUpdates is // then ErrTowerUnackedUpdates is returned. If the tower doesn't have
// returned. If the tower doesn't have any sessions at all, it'll be // any sessions at all, it'll be completely removed from the database.
// completely removed from the database.
// //
// NOTE: An error is not returned if the tower doesn't exist. // NOTE: An error is not returned if the tower doesn't exist.
RemoveTower(*btcec.PublicKey, net.Addr) error RemoveTower(*btcec.PublicKey, net.Addr) error

View File

@ -394,37 +394,6 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
// address is a duplicate, this will result in no // address is a duplicate, this will result in no
// change. // change.
tower.AddAddress(lnAddr.Address) 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.
towerSessIndex := towerToSessionIndex.NestedReadBucket(
tower.ID.Bytes(),
)
if towerSessIndex == nil {
return ErrTowerNotFound
}
sessions := tx.ReadWriteBucket(cSessionBkt)
if sessions == nil {
return ErrUninitializedDB
}
err = towerSessIndex.ForEach(func(k, _ []byte) error {
session, err := getClientSessionBody(
sessions, k,
)
if err != nil {
return err
}
return markSessionStatus(
sessions, session, CSessionActive,
)
})
if err != nil {
return err
}
} else { } else {
// No such tower exists, create a new tower id for our // No such tower exists, create a new tower id for our
// new tower. The error is unhandled since NextSequence // new tower. The error is unhandled since NextSequence
@ -435,6 +404,7 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
ID: TowerID(towerID), ID: TowerID(towerID),
IdentityKey: lnAddr.IdentityKey, IdentityKey: lnAddr.IdentityKey,
Addresses: []net.Addr{lnAddr.Address}, Addresses: []net.Addr{lnAddr.Address},
Status: TowerStatusActive,
} }
towerIDBytes = tower.ID.Bytes() towerIDBytes = tower.ID.Bytes()
@ -468,10 +438,10 @@ func (c *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*Tower, error) {
// RemoveTower modifies a tower's record within the database. If an address is // 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 // 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 // persisted state. Otherwise, we'll attempt to mark the tower as inactive. If
// marking all of its sessions inactive. If any of its sessions has unacked // any of its sessions has unacked updates, then ErrTowerUnackedUpdates is
// updates, then ErrTowerUnackedUpdates is returned. If the tower doesn't have // returned. If the tower doesn't have any sessions at all, it'll be completely
// any sessions at all, it'll be completely removed from the database. // removed from the database.
// //
// NOTE: An error is not returned if the tower doesn't exist. // NOTE: An error is not returned if the tower doesn't exist.
func (c *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error { func (c *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
@ -570,19 +540,12 @@ func (c *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
return err return err
} }
// We'll mark its sessions as inactive as long as they don't // We'll do a check to ensure that the tower's sessions don't
// have any pending updates to ensure we don't load them upon // have any pending back-ups.
// restarts.
for _, session := range towerSessions { for _, session := range towerSessions {
if committedUpdateCount[session.ID] > 0 { if committedUpdateCount[session.ID] > 0 {
return ErrTowerUnackedUpdates return ErrTowerUnackedUpdates
} }
err := markSessionStatus(
sessions, session, CSessionTerminal,
)
if err != nil {
return err
}
} }
return nil return nil

View File

@ -145,12 +145,9 @@ func (h *clientDBHarness) removeTower(pubKey *btcec.PublicKey, addr net.Addr,
return return
} }
for _, session := range h.listSessions(&tower.ID) { require.EqualValues(
require.Equal(h.t, wtdb.CSessionTerminal, h.t, wtdb.TowerStatusInactive, tower.Status,
session.Status, "expected status for session "+ )
"%v to be %v, got %v", session.ID,
wtdb.CSessionTerminal, session.Status)
}
} }
} }
@ -551,20 +548,13 @@ func testRemoveTower(h *clientDBHarness) {
h.commitUpdate(&session.ID, update, nil) h.commitUpdate(&session.ID, update, nil)
// We should not be able to fully remove it from the database since // We should not be able to fully remove it from the database since
// there's a session and it has unacked updates. // there's a session, and it has unacked updates.
h.removeTower(pk, nil, true, wtdb.ErrTowerUnackedUpdates) h.removeTower(pk, nil, true, wtdb.ErrTowerUnackedUpdates)
// Removing the tower after all sessions no longer have unacked updates // Removing the tower after all sessions no longer have unacked updates
// should result in the sessions becoming inactive. // should succeed.
h.ackUpdate(&session.ID, 1, 1, nil) h.ackUpdate(&session.ID, 1, 1, nil)
h.removeTower(pk, nil, true, nil) h.removeTower(pk, nil, true, nil)
// Creating the tower again should mark all of the sessions active once
// again.
h.createTower(&lnwire.NetAddress{
IdentityKey: pk,
Address: addr1,
}, nil)
} }
// testTowerStatusChange tests that the Tower status is updated accordingly // testTowerStatusChange tests that the Tower status is updated accordingly
@ -613,11 +603,11 @@ func testTowerStatusChange(h *clientDBHarness) {
assertTowerStatus(wtdb.TowerStatusActive) assertTowerStatus(wtdb.TowerStatusActive)
assertSessionStatus(wtdb.CSessionActive) assertSessionStatus(wtdb.CSessionActive)
// Removing the tower should change its status and its session status // Removing the tower should change its status but its session
// to inactive. // status should remain active.
h.removeTower(tower.IdentityKey, nil, true, nil) h.removeTower(tower.IdentityKey, nil, true, nil)
assertTowerStatus(wtdb.TowerStatusInactive) assertTowerStatus(wtdb.TowerStatusInactive)
assertSessionStatus(wtdb.CSessionTerminal) assertSessionStatus(wtdb.CSessionActive)
// Re-adding the tower in some way should re-active it and its session. // Re-adding the tower in some way should re-active it and its session.
h.createTower(towerAddr, nil) h.createTower(towerAddr, nil)