mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 01:43:16 +01:00
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:
parent
1ae802812c
commit
ffd355c6c4
@ -179,6 +179,7 @@ func TestCodec(tt *testing.T) {
|
|||||||
obj := wtdb.Tower{
|
obj := wtdb.Tower{
|
||||||
IdentityKey: pk,
|
IdentityKey: pk,
|
||||||
Addresses: addrs,
|
Addresses: addrs,
|
||||||
|
Status: wtdb.TowerStatus(r.Uint32()),
|
||||||
}
|
}
|
||||||
|
|
||||||
v[0] = reflect.ValueOf(obj)
|
v[0] = reflect.ValueOf(obj)
|
||||||
|
@ -7,6 +7,26 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"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.
|
// 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 is a list of possible addresses to reach the tower.
|
||||||
Addresses []net.Addr
|
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.
|
// 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
|
// Encode writes the Tower to the passed io.Writer. The TowerID is not
|
||||||
// serialized, since it acts as the key.
|
// serialized, since it acts as the key.
|
||||||
func (t *Tower) Encode(w io.Writer) error {
|
func (t *Tower) Encode(w io.Writer) error {
|
||||||
return WriteElements(w,
|
err := WriteElements(w,
|
||||||
t.IdentityKey,
|
t.IdentityKey,
|
||||||
t.Addresses,
|
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
|
// Decode reads a Tower from the passed io.Reader. The TowerID is meant to be
|
||||||
// decoded from the key.
|
// decoded from the key.
|
||||||
func (t *Tower) Decode(r io.Reader) error {
|
func (t *Tower) Decode(r io.Reader) error {
|
||||||
return ReadElements(r,
|
err := ReadElements(r,
|
||||||
&t.IdentityKey,
|
&t.IdentityKey,
|
||||||
&t.Addresses,
|
&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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user