wtdb: add TowerStatus to Tower

This is added as a TLV record meaning that all the towers currently on
disk that don't have this new field will be seen as Active.
This commit is contained in:
Elle Mouton 2023-11-24 12:03:23 +02:00
parent 1ae802812c
commit ffd355c6c4
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 65 additions and 2 deletions

View File

@ -179,6 +179,7 @@ func TestCodec(tt *testing.T) {
obj := wtdb.Tower{
IdentityKey: pk,
Addresses: addrs,
Status: wtdb.TowerStatus(r.Uint32()),
}
v[0] = reflect.ValueOf(obj)

View File

@ -7,6 +7,26 @@ import (
"net"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/tlv"
)
// TowerStatus represents the state of the tower as set by the tower client.
type TowerStatus uint8
const (
// TowerStatusActive is the default state of the tower, and it indicates
// that this tower should be used to attempt session creation.
TowerStatusActive TowerStatus = 0
// TowerStatusInactive indicates that the tower should not be used to
// attempt session creation.
TowerStatusInactive TowerStatus = 1
)
const (
// TowerStatusTLVType is the TLV type number that will be used to store
// the tower's status.
TowerStatusTLVType = tlv.Type(0)
)
// TowerID is a unique 64-bit identifier allocated to each unique watchtower.
@ -41,6 +61,9 @@ type Tower struct {
// Addresses is a list of possible addresses to reach the tower.
Addresses []net.Addr
// Status is the status of this tower as set by the client.
Status TowerStatus
}
// AddAddress adds the given address to the tower's in-memory list of addresses.
@ -88,17 +111,56 @@ func (t *Tower) String() string {
// 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,
err := WriteElements(w,
t.IdentityKey,
t.Addresses,
)
if err != nil {
return err
}
status := uint8(t.Status)
tlvRecords := []tlv.Record{
tlv.MakePrimitiveRecord(TowerStatusTLVType, &status),
}
tlvStream, err := tlv.NewStream(tlvRecords...)
if err != nil {
return err
}
return tlvStream.Encode(w)
}
// 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,
err := ReadElements(r,
&t.IdentityKey,
&t.Addresses,
)
if err != nil {
return err
}
var status uint8
tlvRecords := []tlv.Record{
tlv.MakePrimitiveRecord(TowerStatusTLVType, &status),
}
tlvStream, err := tlv.NewStream(tlvRecords...)
if err != nil {
return err
}
typeMap, err := tlvStream.DecodeWithParsedTypes(r)
if err != nil {
return err
}
if _, ok := typeMap[TowerStatusTLVType]; ok {
t.Status = TowerStatus(status)
}
return nil
}