watchtower/wtdb: add Encode/Decode methods to wtclient structs
This commit is contained in:
4 files changed
+284
-12
No files matched your search
@@ -2,8 +2,8 @@ package wtdb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
@@ -47,18 +47,15 @@ type Tower struct {
|
||||
|
||||
// Addresses is a list of possible addresses to reach the tower.
|
||||
Addresses []net.Addr
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// AddAddress adds the given address to the tower's in-memory list of addresses.
|
||||
// If the address's string is already present, the Tower will be left
|
||||
// unmodified. Otherwise, the adddress is prepended to the beginning of the
|
||||
// Tower's addresses, on the assumption that it is fresher than the others.
|
||||
//
|
||||
// NOTE: This method is NOT safe for concurrent use.
|
||||
func (t *Tower) AddAddress(addr net.Addr) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
// Ensure we don't add a duplicate address.
|
||||
addrStr := addr.String()
|
||||
for _, existingAddr := range t.Addresses {
|
||||
@@ -75,10 +72,9 @@ func (t *Tower) AddAddress(addr net.Addr) {
|
||||
// LNAddrs generates a list of lnwire.NetAddress from a Tower instance's
|
||||
// addresses. This can be used to have a client try multiple addresses for the
|
||||
// same Tower.
|
||||
//
|
||||
// NOTE: This method is NOT safe for concurrent use.
|
||||
func (t *Tower) LNAddrs() []*lnwire.NetAddress {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
|
||||
addrs := make([]*lnwire.NetAddress, 0, len(t.Addresses))
|
||||
for _, addr := range t.Addresses {
|
||||
addrs = append(addrs, &lnwire.NetAddress{
|
||||
@@ -89,3 +85,21 @@ func (t *Tower) LNAddrs() []*lnwire.NetAddress {
|
||||
|
||||
return addrs
|
||||
}
|
||||
|
||||
// Encode writes the Tower to the passed io.Writer. The TowerID is not
|
||||
// serialized, since it acts as the key.
|
||||
func (t *Tower) Encode(w io.Writer) error {
|
||||
return WriteElements(w,
|
||||
t.IdentityKey,
|
||||
t.Addresses,
|
||||
)
|
||||
}
|
||||
|
||||
// Decode reads a Tower from the passed io.Reader. The TowerID is meant to be
|
||||
// decoded from the key.
|
||||
func (t *Tower) Decode(r io.Reader) error {
|
||||
return ReadElements(r,
|
||||
&t.IdentityKey,
|
||||
&t.Addresses,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user