2019-03-15 12:32:40 +03:00
|
|
|
package wtmock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2019-06-08 03:45:11 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-03-15 12:32:40 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-12-02 00:03:18 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/blob"
|
2019-03-15 12:32:40 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type towerPK [33]byte
|
|
|
|
|
2020-12-02 00:03:18 +03:00
|
|
|
type keyIndexKey struct {
|
|
|
|
towerID wtdb.TowerID
|
|
|
|
blobType blob.Type
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
// ClientDB is a mock, in-memory database or testing the watchtower client
|
|
|
|
// behavior.
|
|
|
|
type ClientDB struct {
|
|
|
|
nextTowerID uint64 // to be used atomically
|
|
|
|
|
|
|
|
mu sync.Mutex
|
2019-05-24 06:48:50 +03:00
|
|
|
summaries map[lnwire.ChannelID]wtdb.ClientChanSummary
|
2020-05-15 01:28:49 +03:00
|
|
|
activeSessions map[wtdb.SessionID]wtdb.ClientSession
|
2019-05-24 06:47:22 +03:00
|
|
|
towerIndex map[towerPK]wtdb.TowerID
|
|
|
|
towers map[wtdb.TowerID]*wtdb.Tower
|
2019-04-24 06:04:40 +03:00
|
|
|
|
2020-12-02 00:03:18 +03:00
|
|
|
nextIndex uint32
|
|
|
|
indexes map[keyIndexKey]uint32
|
|
|
|
legacyIndexes map[wtdb.TowerID]uint32
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientDB initializes a new mock ClientDB.
|
|
|
|
func NewClientDB() *ClientDB {
|
|
|
|
return &ClientDB{
|
2019-05-24 06:48:50 +03:00
|
|
|
summaries: make(map[lnwire.ChannelID]wtdb.ClientChanSummary),
|
2020-05-15 01:28:49 +03:00
|
|
|
activeSessions: make(map[wtdb.SessionID]wtdb.ClientSession),
|
2019-05-24 06:47:22 +03:00
|
|
|
towerIndex: make(map[towerPK]wtdb.TowerID),
|
|
|
|
towers: make(map[wtdb.TowerID]*wtdb.Tower),
|
2020-12-02 00:03:18 +03:00
|
|
|
indexes: make(map[keyIndexKey]uint32),
|
|
|
|
legacyIndexes: make(map[wtdb.TowerID]uint32),
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 03:45:11 +03:00
|
|
|
// 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.
|
2019-03-15 12:32:40 +03:00
|
|
|
func (m *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*wtdb.Tower, error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
var towerPubKey towerPK
|
|
|
|
copy(towerPubKey[:], lnAddr.IdentityKey.SerializeCompressed())
|
|
|
|
|
|
|
|
var tower *wtdb.Tower
|
|
|
|
towerID, ok := m.towerIndex[towerPubKey]
|
|
|
|
if ok {
|
|
|
|
tower = m.towers[towerID]
|
|
|
|
tower.AddAddress(lnAddr.Address)
|
2019-06-08 03:45:11 +03:00
|
|
|
|
|
|
|
towerSessions, err := m.listClientSessions(&towerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for id, session := range towerSessions {
|
|
|
|
session.Status = wtdb.CSessionActive
|
2020-05-15 01:28:49 +03:00
|
|
|
m.activeSessions[id] = *session
|
2019-06-08 03:45:11 +03:00
|
|
|
}
|
2019-03-15 12:32:40 +03:00
|
|
|
} else {
|
2019-05-24 06:47:22 +03:00
|
|
|
towerID = wtdb.TowerID(atomic.AddUint64(&m.nextTowerID, 1))
|
2019-03-15 12:32:40 +03:00
|
|
|
tower = &wtdb.Tower{
|
2019-05-24 06:47:22 +03:00
|
|
|
ID: wtdb.TowerID(towerID),
|
2019-03-15 12:32:40 +03:00
|
|
|
IdentityKey: lnAddr.IdentityKey,
|
|
|
|
Addresses: []net.Addr{lnAddr.Address},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.towerIndex[towerPubKey] = towerID
|
|
|
|
m.towers[towerID] = tower
|
|
|
|
|
2019-05-24 06:48:23 +03:00
|
|
|
return copyTower(tower), nil
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-08 03:45:11 +03:00
|
|
|
// 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 (m *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
tower, err := m.loadTower(pubKey)
|
|
|
|
if err == wtdb.ErrTowerNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr != nil {
|
|
|
|
tower.RemoveAddress(addr)
|
2020-11-03 21:27:57 +03:00
|
|
|
if len(tower.Addresses) == 0 {
|
|
|
|
return wtdb.ErrLastTowerAddr
|
|
|
|
}
|
2019-06-08 03:45:11 +03:00
|
|
|
m.towers[tower.ID] = tower
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
towerSessions, err := m.listClientSessions(&tower.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(towerSessions) == 0 {
|
|
|
|
var towerPK towerPK
|
|
|
|
copy(towerPK[:], pubKey.SerializeCompressed())
|
|
|
|
delete(m.towerIndex, towerPK)
|
|
|
|
delete(m.towers, tower.ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, session := range towerSessions {
|
|
|
|
if len(session.CommittedUpdates) > 0 {
|
|
|
|
return wtdb.ErrTowerUnackedUpdates
|
|
|
|
}
|
|
|
|
session.Status = wtdb.CSessionInactive
|
2020-05-15 01:28:49 +03:00
|
|
|
m.activeSessions[id] = *session
|
2019-06-08 03:45:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTower retrieves a tower by its public key.
|
|
|
|
func (m *ClientDB) LoadTower(pubKey *btcec.PublicKey) (*wtdb.Tower, error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return m.loadTower(pubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadTower retrieves a tower by its public key.
|
|
|
|
//
|
|
|
|
// NOTE: This method requires the database's lock to be acquired.
|
|
|
|
func (m *ClientDB) loadTower(pubKey *btcec.PublicKey) (*wtdb.Tower, error) {
|
|
|
|
var towerPK towerPK
|
|
|
|
copy(towerPK[:], pubKey.SerializeCompressed())
|
|
|
|
|
|
|
|
towerID, ok := m.towerIndex[towerPK]
|
|
|
|
if !ok {
|
|
|
|
return nil, wtdb.ErrTowerNotFound
|
|
|
|
}
|
|
|
|
tower, ok := m.towers[towerID]
|
|
|
|
if !ok {
|
|
|
|
return nil, wtdb.ErrTowerNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return copyTower(tower), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTowerByID retrieves a tower by its tower ID.
|
|
|
|
func (m *ClientDB) LoadTowerByID(towerID wtdb.TowerID) (*wtdb.Tower, error) {
|
2019-03-28 02:50:59 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
if tower, ok := m.towers[towerID]; ok {
|
2019-05-24 06:48:23 +03:00
|
|
|
return copyTower(tower), nil
|
2019-03-28 02:50:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, wtdb.ErrTowerNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-08 03:45:11 +03:00
|
|
|
// ListTowers retrieves the list of towers available within the database.
|
|
|
|
func (m *ClientDB) ListTowers() ([]*wtdb.Tower, error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
towers := make([]*wtdb.Tower, 0, len(m.towers))
|
|
|
|
for _, tower := range m.towers {
|
|
|
|
towers = append(towers, copyTower(tower))
|
|
|
|
}
|
|
|
|
|
|
|
|
return towers, nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
// MarkBackupIneligible records that particular commit height is ineligible for
|
|
|
|
// backup. This allows the client to track which updates it should not attempt
|
|
|
|
// to retry after startup.
|
|
|
|
func (m *ClientDB) MarkBackupIneligible(chanID lnwire.ChannelID, commitHeight uint64) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 03:44:55 +03:00
|
|
|
// ListClientSessions returns the set of all client sessions known to the db. An
|
|
|
|
// optional tower ID can be used to filter out any client sessions in the
|
|
|
|
// response that do not correspond to this tower.
|
|
|
|
func (m *ClientDB) ListClientSessions(
|
|
|
|
tower *wtdb.TowerID) (map[wtdb.SessionID]*wtdb.ClientSession, error) {
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2019-06-08 03:45:11 +03:00
|
|
|
return m.listClientSessions(tower)
|
|
|
|
}
|
|
|
|
|
|
|
|
// listClientSessions returns the set of all client sessions known to the db. An
|
|
|
|
// optional tower ID can be used to filter out any client sessions in the
|
|
|
|
// response that do not correspond to this tower.
|
|
|
|
func (m *ClientDB) listClientSessions(
|
|
|
|
tower *wtdb.TowerID) (map[wtdb.SessionID]*wtdb.ClientSession, error) {
|
2019-03-15 12:32:40 +03:00
|
|
|
|
|
|
|
sessions := make(map[wtdb.SessionID]*wtdb.ClientSession)
|
|
|
|
for _, session := range m.activeSessions {
|
2020-05-15 01:28:49 +03:00
|
|
|
session := session
|
2019-06-08 03:44:55 +03:00
|
|
|
if tower != nil && *tower != session.TowerID {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-15 01:28:49 +03:00
|
|
|
sessions[session.ID] = &session
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateClientSession records a newly negotiated client session in the set of
|
|
|
|
// active sessions. The session can be identified by its SessionID.
|
|
|
|
func (m *ClientDB) CreateClientSession(session *wtdb.ClientSession) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2019-05-24 06:48:23 +03:00
|
|
|
// Ensure that we aren't overwriting an existing session.
|
|
|
|
if _, ok := m.activeSessions[session.ID]; ok {
|
|
|
|
return wtdb.ErrClientSessionAlreadyExists
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:03:18 +03:00
|
|
|
key := keyIndexKey{
|
|
|
|
towerID: session.TowerID,
|
|
|
|
blobType: session.Policy.BlobType,
|
|
|
|
}
|
|
|
|
|
2019-04-24 06:06:20 +03:00
|
|
|
// Ensure that a session key index has been reserved for this tower.
|
2020-12-02 00:03:18 +03:00
|
|
|
keyIndex, err := m.getSessionKeyIndex(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-24 06:06:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the session's index matches the reserved index.
|
|
|
|
if keyIndex != session.KeyIndex {
|
|
|
|
return wtdb.ErrIncorrectKeyIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the key index reservation for this tower. Once committed, this
|
|
|
|
// permits us to create another session with this tower.
|
2020-12-02 00:03:18 +03:00
|
|
|
delete(m.indexes, key)
|
|
|
|
if key.blobType == blob.TypeAltruistCommit {
|
|
|
|
delete(m.legacyIndexes, key.towerID)
|
|
|
|
}
|
2019-04-24 06:06:20 +03:00
|
|
|
|
2020-05-15 01:28:49 +03:00
|
|
|
m.activeSessions[session.ID] = wtdb.ClientSession{
|
2019-05-24 06:47:49 +03:00
|
|
|
ID: session.ID,
|
|
|
|
ClientSessionBody: wtdb.ClientSessionBody{
|
|
|
|
SeqNum: session.SeqNum,
|
|
|
|
TowerLastApplied: session.TowerLastApplied,
|
|
|
|
TowerID: session.TowerID,
|
|
|
|
KeyIndex: session.KeyIndex,
|
|
|
|
Policy: session.Policy,
|
|
|
|
RewardPkScript: cloneBytes(session.RewardPkScript),
|
|
|
|
},
|
2019-05-24 06:47:36 +03:00
|
|
|
CommittedUpdates: make([]wtdb.CommittedUpdate, 0),
|
2019-03-15 12:32:40 +03:00
|
|
|
AckedUpdates: make(map[uint16]wtdb.BackupID),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-24 06:04:40 +03:00
|
|
|
// 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
|
|
|
|
// index for that tower can be reserved. Multiple calls to this method before
|
|
|
|
// CreateClientSession is invoked should return the same index.
|
2020-12-02 00:03:18 +03:00
|
|
|
func (m *ClientDB) NextSessionKeyIndex(towerID wtdb.TowerID,
|
|
|
|
blobType blob.Type) (uint32, error) {
|
|
|
|
|
2019-04-24 06:04:40 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2020-12-02 00:03:18 +03:00
|
|
|
key := keyIndexKey{
|
|
|
|
towerID: towerID,
|
|
|
|
blobType: blobType,
|
|
|
|
}
|
|
|
|
|
|
|
|
if index, err := m.getSessionKeyIndex(key); err == nil {
|
2019-04-24 06:04:40 +03:00
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:48:23 +03:00
|
|
|
m.nextIndex++
|
2019-04-24 06:04:40 +03:00
|
|
|
index := m.nextIndex
|
2020-12-02 00:03:18 +03:00
|
|
|
m.indexes[key] = index
|
2019-04-24 06:04:40 +03:00
|
|
|
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:03:18 +03:00
|
|
|
func (m *ClientDB) getSessionKeyIndex(key keyIndexKey) (uint32, error) {
|
|
|
|
if index, ok := m.indexes[key]; ok {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.blobType == blob.TypeAltruistCommit {
|
|
|
|
if index, ok := m.legacyIndexes[key.towerID]; ok {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, wtdb.ErrNoReservedKeyIndex
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
// CommitUpdate persists the CommittedUpdate provided in the slot for (session,
|
|
|
|
// seqNum). This allows the client to retransmit this update on startup.
|
2019-05-24 06:47:36 +03:00
|
|
|
func (m *ClientDB) CommitUpdate(id *wtdb.SessionID,
|
2019-03-15 12:32:40 +03:00
|
|
|
update *wtdb.CommittedUpdate) (uint16, error) {
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
// Fail if session doesn't exist.
|
|
|
|
session, ok := m.activeSessions[*id]
|
|
|
|
if !ok {
|
|
|
|
return 0, wtdb.ErrClientSessionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an update has already been committed for this state.
|
2019-05-24 06:47:36 +03:00
|
|
|
for _, dbUpdate := range session.CommittedUpdates {
|
|
|
|
if dbUpdate.SeqNum == update.SeqNum {
|
|
|
|
// If the breach hint matches, we'll just return the
|
|
|
|
// last applied value so the client can retransmit.
|
|
|
|
if dbUpdate.Hint == update.Hint {
|
|
|
|
return session.TowerLastApplied, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, fail since the breach hint doesn't match.
|
|
|
|
return 0, wtdb.ErrUpdateAlreadyCommitted
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sequence number must increment.
|
2019-05-24 06:47:36 +03:00
|
|
|
if update.SeqNum != session.SeqNum+1 {
|
2019-03-15 12:32:40 +03:00
|
|
|
return 0, wtdb.ErrCommitUnorderedUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the update and increment the sequence number.
|
2019-05-24 06:47:36 +03:00
|
|
|
session.CommittedUpdates = append(session.CommittedUpdates, *update)
|
2019-03-15 12:32:40 +03:00
|
|
|
session.SeqNum++
|
2020-05-15 01:28:49 +03:00
|
|
|
m.activeSessions[*id] = session
|
2019-03-15 12:32:40 +03:00
|
|
|
|
|
|
|
return session.TowerLastApplied, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AckUpdate persists an acknowledgment for a given (session, seqnum) pair. This
|
|
|
|
// removes the update from the set of committed updates, and validates the
|
|
|
|
// lastApplied value returned from the tower.
|
|
|
|
func (m *ClientDB) AckUpdate(id *wtdb.SessionID, seqNum, lastApplied uint16) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
// Fail if session doesn't exist.
|
|
|
|
session, ok := m.activeSessions[*id]
|
|
|
|
if !ok {
|
|
|
|
return wtdb.ErrClientSessionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the returned last applied value does not exceed the highest
|
|
|
|
// allocated sequence number.
|
|
|
|
if lastApplied > session.SeqNum {
|
|
|
|
return wtdb.ErrUnallocatedLastApplied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the last applied value isn't lower than a previous one sent by
|
|
|
|
// the tower.
|
|
|
|
if lastApplied < session.TowerLastApplied {
|
|
|
|
return wtdb.ErrLastAppliedReversion
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:47:36 +03:00
|
|
|
// Retrieve the committed update, failing if none is found. We should
|
|
|
|
// only receive acks for state updates that we send.
|
|
|
|
updates := session.CommittedUpdates
|
|
|
|
for i, update := range updates {
|
|
|
|
if update.SeqNum != seqNum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the committed update from disk and mark the update as
|
|
|
|
// acked. The tower last applied value is also recorded to send
|
|
|
|
// along with the next update.
|
|
|
|
copy(updates[:i], updates[i+1:])
|
|
|
|
updates[len(updates)-1] = wtdb.CommittedUpdate{}
|
|
|
|
session.CommittedUpdates = updates[:len(updates)-1]
|
2019-03-15 12:32:40 +03:00
|
|
|
|
2019-05-24 06:47:36 +03:00
|
|
|
session.AckedUpdates[seqNum] = update.BackupID
|
|
|
|
session.TowerLastApplied = lastApplied
|
|
|
|
|
2020-05-15 01:28:49 +03:00
|
|
|
m.activeSessions[*id] = session
|
2019-05-24 06:47:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return wtdb.ErrCommittedUpdateNotFound
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
// FetchChanSummaries loads a mapping from all registered channels to their
|
|
|
|
// channel summaries.
|
|
|
|
func (m *ClientDB) FetchChanSummaries() (wtdb.ChannelSummaries, error) {
|
2019-03-15 12:32:40 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
summaries := make(map[lnwire.ChannelID]wtdb.ClientChanSummary)
|
|
|
|
for chanID, summary := range m.summaries {
|
|
|
|
summaries[chanID] = wtdb.ClientChanSummary{
|
|
|
|
SweepPkScript: cloneBytes(summary.SweepPkScript),
|
|
|
|
}
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
return summaries, nil
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
// RegisterChannel registers a channel for use within the client database. For
|
|
|
|
// now, all that is stored in the channel summary is the sweep pkscript that
|
|
|
|
// we'd like any tower sweeps to pay into. In the future, this will be extended
|
|
|
|
// to contain more info to allow the client efficiently request historical
|
|
|
|
// states to be backed up under the client's active policy.
|
|
|
|
func (m *ClientDB) RegisterChannel(chanID lnwire.ChannelID,
|
|
|
|
sweepPkScript []byte) error {
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
if _, ok := m.summaries[chanID]; ok {
|
|
|
|
return wtdb.ErrChannelAlreadyRegistered
|
2019-03-15 12:32:40 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 06:48:50 +03:00
|
|
|
m.summaries[chanID] = wtdb.ClientChanSummary{
|
|
|
|
SweepPkScript: cloneBytes(sweepPkScript),
|
|
|
|
}
|
2019-03-15 12:32:40 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneBytes(b []byte) []byte {
|
2019-04-24 06:04:23 +03:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
bb := make([]byte, len(b))
|
|
|
|
copy(bb, b)
|
2019-04-24 06:04:23 +03:00
|
|
|
|
2019-03-15 12:32:40 +03:00
|
|
|
return bb
|
|
|
|
}
|
2019-05-24 06:48:23 +03:00
|
|
|
|
|
|
|
func copyTower(tower *wtdb.Tower) *wtdb.Tower {
|
|
|
|
t := &wtdb.Tower{
|
|
|
|
ID: tower.ID,
|
|
|
|
IdentityKey: tower.IdentityKey,
|
|
|
|
Addresses: make([]net.Addr, len(tower.Addresses)),
|
|
|
|
}
|
|
|
|
copy(t.Addresses, tower.Addresses)
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|