watchtower/wtdb: add encode/decode to session info + updates

This commit is contained in:
Conner Fromknecht 2019-04-26 17:20:52 -07:00
parent dccef4c8bf
commit 7ba197c6a7
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 47 additions and 0 deletions

@ -2,6 +2,7 @@ package wtdb
import ( import (
"errors" "errors"
"io"
"github.com/lightningnetwork/lnd/watchtower/wtpolicy" "github.com/lightningnetwork/lnd/watchtower/wtpolicy"
) )
@ -59,6 +60,28 @@ type SessionInfo struct {
// TODO(conner): store client metrics, DOS score, etc // TODO(conner): store client metrics, DOS score, etc
} }
// Encode serializes the session info to the given io.Writer.
func (s *SessionInfo) Encode(w io.Writer) error {
return WriteElements(w,
s.ID,
s.Policy,
s.LastApplied,
s.ClientLastApplied,
s.RewardAddress,
)
}
// Decode deserializes the session infor from the given io.Reader.
func (s *SessionInfo) Decode(r io.Reader) error {
return ReadElements(r,
&s.ID,
&s.Policy,
&s.LastApplied,
&s.ClientLastApplied,
&s.RewardAddress,
)
}
// AcceptUpdateSequence validates that a state update's sequence number and last // AcceptUpdateSequence validates that a state update's sequence number and last
// applied are valid given our past history with the client. These checks ensure // applied are valid given our past history with the client. These checks ensure
// that clients are properly in sync and following the update protocol properly. // that clients are properly in sync and following the update protocol properly.

@ -1,5 +1,7 @@
package wtdb package wtdb
import "io"
// SessionStateUpdate holds a state update sent by a client along with its // SessionStateUpdate holds a state update sent by a client along with its
// SessionID. // SessionID.
type SessionStateUpdate struct { type SessionStateUpdate struct {
@ -21,3 +23,25 @@ type SessionStateUpdate struct {
// hint is braodcast. // hint is braodcast.
EncryptedBlob []byte EncryptedBlob []byte
} }
// Encode serializes the state update into the provided io.Writer.
func (u *SessionStateUpdate) Encode(w io.Writer) error {
return WriteElements(w,
u.ID,
u.SeqNum,
u.LastApplied,
u.Hint,
u.EncryptedBlob,
)
}
// Decode deserializes the target state update from the provided io.Reader.
func (u *SessionStateUpdate) Decode(r io.Reader) error {
return ReadElements(r,
&u.ID,
&u.SeqNum,
&u.LastApplied,
&u.Hint,
&u.EncryptedBlob,
)
}