From 28bf49807e0722549e45c82eeca72d58ea418871 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 24 May 2019 17:04:07 -0700 Subject: [PATCH] watchtower/wtdb: add CSessionStatus field to ClientSession This commit adds persisted status bit-field to ClientSessions, that can be used to modify behavior of their handling in the client. Currently, only a default CSessionActive status is defined. However, the intention is that this could later be used to signal that a session is abandoned without needing to perform a db migration to add the field. As we move forward with testing, this will likely be useful if a session gets borked and we need a simple method of the client to temporarily ignore certain sessions. The field may be useful in signaling other types of status changes, though this was the primary motivation that warranted the addition. --- channeldb/codec.go | 10 ++++++++++ watchtower/wtdb/client_session.go | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/channeldb/codec.go b/channeldb/codec.go index ec6e165b..ca5cfeed 100644 --- a/channeldb/codec.go +++ b/channeldb/codec.go @@ -128,6 +128,11 @@ func WriteElement(w io.Writer, element interface{}) error { return err } + case uint8: + if err := binary.Write(w, byteOrder, e); err != nil { + return err + } + case bool: if err := binary.Write(w, byteOrder, e); err != nil { return err @@ -289,6 +294,11 @@ func ReadElement(r io.Reader, element interface{}) error { return err } + case *uint8: + if err := binary.Read(r, byteOrder, e); err != nil { + return err + } + case *bool: if err := binary.Read(r, byteOrder, e); err != nil { return err diff --git a/watchtower/wtdb/client_session.go b/watchtower/wtdb/client_session.go index 34e2168b..cb59ca57 100644 --- a/watchtower/wtdb/client_session.go +++ b/watchtower/wtdb/client_session.go @@ -8,6 +8,16 @@ import ( "github.com/lightningnetwork/lnd/watchtower/wtpolicy" ) +// CSessionStatus is a bit-field representing the possible statuses of +// ClientSessions. +type CSessionStatus uint8 + +const ( + // CSessionActive indicates that the ClientSession is active and can be + // used for backups. + CSessionActive CSessionStatus = 0 +) + // ClientSession encapsulates a SessionInfo returned from a successful // session negotiation, and also records the tower and ephemeral secret used for // communicating with the tower. @@ -76,6 +86,9 @@ type ClientSessionBody struct { // Policy holds the negotiated session parameters. Policy wtpolicy.Policy + // Status indicates the current state of the ClientSession. + Status CSessionStatus + // RewardPkScript is the pkscript that the tower's reward will be // deposited to if a sweep transaction confirms and the sessions // specifies a reward output. @@ -89,6 +102,7 @@ func (s *ClientSessionBody) Encode(w io.Writer) error { s.TowerLastApplied, uint64(s.TowerID), s.KeyIndex, + uint8(s.Status), s.Policy, s.RewardPkScript, ) @@ -96,12 +110,16 @@ func (s *ClientSessionBody) Encode(w io.Writer) error { // Decode reads a ClientSessionBody from the passed io.Reader. func (s *ClientSessionBody) Decode(r io.Reader) error { - var towerID uint64 + var ( + towerID uint64 + status uint8 + ) err := ReadElements(r, &s.SeqNum, &s.TowerLastApplied, &towerID, &s.KeyIndex, + &status, &s.Policy, &s.RewardPkScript, ) @@ -110,6 +128,7 @@ func (s *ClientSessionBody) Decode(r io.Reader) error { } s.TowerID = TowerID(towerID) + s.Status = CSessionStatus(status) return nil }