From ffd355c6c4e1b81a4e98016d190ef292d635a717 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 24 Nov 2023 12:03:23 +0200 Subject: [PATCH] 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. --- watchtower/wtdb/codec_test.go | 1 + watchtower/wtdb/tower.go | 66 +++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/watchtower/wtdb/codec_test.go b/watchtower/wtdb/codec_test.go index 228d2a0ec..3f2e59864 100644 --- a/watchtower/wtdb/codec_test.go +++ b/watchtower/wtdb/codec_test.go @@ -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) diff --git a/watchtower/wtdb/tower.go b/watchtower/wtdb/tower.go index ca9dbeb28..9a0e052b8 100644 --- a/watchtower/wtdb/tower.go +++ b/watchtower/wtdb/tower.go @@ -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 }