From 3d934d0978946fe6313d6d1ddf258f3ae16adc86 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 19 Mar 2019 19:38:09 -0700 Subject: [PATCH] watchtower/wtwire: add DeleteSession and DeleteSessionReply message --- watchtower/wtwire/delete_session.go | 45 ++++++++++++++++ watchtower/wtwire/delete_session_reply.go | 66 +++++++++++++++++++++++ watchtower/wtwire/message.go | 15 ++++++ watchtower/wtwire/wtwire_test.go | 12 +++++ 4 files changed, 138 insertions(+) create mode 100644 watchtower/wtwire/delete_session.go create mode 100644 watchtower/wtwire/delete_session_reply.go diff --git a/watchtower/wtwire/delete_session.go b/watchtower/wtwire/delete_session.go new file mode 100644 index 00000000..fbe8cfb0 --- /dev/null +++ b/watchtower/wtwire/delete_session.go @@ -0,0 +1,45 @@ +package wtwire + +import "io" + +// DeleteSession is sent from the client to the tower to signal that the tower +// can delete all session state for the session key used to authenticate the +// brontide connection. This should be done by the client once all channels that +// have state updates in the session have been resolved on-chain. +type DeleteSession struct{} + +// Compile-time constraint to ensure DeleteSession implements the wtwire.Message +// interface. +var _ Message = (*DeleteSession)(nil) + +// Decode deserializes a serialized DeleteSession message stored in the passed +// io.Reader observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSession) Decode(r io.Reader, pver uint32) error { + return nil +} + +// Encode serializes the target DeleteSession message into the passed io.Writer +// observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSession) Encode(w io.Writer, pver uint32) error { + return nil +} + +// MsgType returns the integer uniquely identifying this message type on the +// wire. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSession) MsgType() MessageType { + return MsgDeleteSession +} + +// MaxPayloadLength returns the maximum allowed payload size for a DeleteSession +// message observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSession) MaxPayloadLength(uint32) uint32 { + return 0 +} diff --git a/watchtower/wtwire/delete_session_reply.go b/watchtower/wtwire/delete_session_reply.go new file mode 100644 index 00000000..059d189e --- /dev/null +++ b/watchtower/wtwire/delete_session_reply.go @@ -0,0 +1,66 @@ +package wtwire + +import "io" + +// DeleteSessionCode is an error code returned by a watchtower in response to a +// DeleteSession message. +type DeleteSessionCode = ErrorCode + +const ( + // DeleteSessionCodeNotFound is returned when the watchtower does not + // know of the requested session. This may indicate an error on the + // client side, or that the tower had already deleted the session in a + // prior request that the client may not have received. + DeleteSessionCodeNotFound DeleteSessionCode = 80 + + // TODO(conner): add String method after wtclient is merged +) + +// DeleteSessionReply is a message sent in response to a client's DeleteSession +// request. The message indicates whether or not the deletion was a success or +// failure. +type DeleteSessionReply struct { + // Code will be non-zero if the watchtower was not able to delete the + // requested session. + Code DeleteSessionCode +} + +// A compile time check to ensure DeleteSessionReply implements the +// wtwire.Message interface. +var _ Message = (*DeleteSessionReply)(nil) + +// Decode deserializes a serialized DeleteSessionReply message stored in the +// passed io.Reader observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSessionReply) Decode(r io.Reader, pver uint32) error { + return ReadElements(r, + &m.Code, + ) +} + +// Encode serializes the target DeleteSessionReply into the passed io.Writer +// observing the protocol version specified. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSessionReply) Encode(w io.Writer, pver uint32) error { + return WriteElements(w, + m.Code, + ) +} + +// MsgType returns the integer uniquely identifying this message type on the +// wire. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSessionReply) MsgType() MessageType { + return MsgDeleteSessionReply +} + +// MaxPayloadLength returns the maximum allowed payload size for a +// DeleteSessionReply complete message observing the specified protocol version. +// +// This is part of the wtwire.Message interface. +func (m *DeleteSessionReply) MaxPayloadLength(uint32) uint32 { + return 2 +} diff --git a/watchtower/wtwire/message.go b/watchtower/wtwire/message.go index 364a2dab..192a397c 100644 --- a/watchtower/wtwire/message.go +++ b/watchtower/wtwire/message.go @@ -40,6 +40,13 @@ const ( // MsgStateUpdateReply identifies an encoded StateUpdateReply message. MsgStateUpdateReply MessageType = 305 + + // MsgDeleteSession identifies an encoded DeleteSession message. + MsgDeleteSession MessageType = 306 + + // MsgDeleteSessionReply identifies an encoded DeleteSessionReply + // message. + MsgDeleteSessionReply MessageType = 307 ) // String returns a human readable description of the message type. @@ -55,6 +62,10 @@ func (m MessageType) String() string { return "MsgStateUpdate" case MsgStateUpdateReply: return "MsgStateUpdateReply" + case MsgDeleteSession: + return "MsgDeleteSession" + case MsgDeleteSessionReply: + return "MsgDeleteSessionReply" case MsgError: return "Error" default: @@ -97,6 +108,10 @@ func makeEmptyMessage(msgType MessageType) (Message, error) { msg = &StateUpdate{} case MsgStateUpdateReply: msg = &StateUpdateReply{} + case MsgDeleteSession: + msg = &DeleteSession{} + case MsgDeleteSessionReply: + msg = &DeleteSessionReply{} case MsgError: msg = &Error{} default: diff --git a/watchtower/wtwire/wtwire_test.go b/watchtower/wtwire/wtwire_test.go index 1dfef1a2..e9b37a55 100644 --- a/watchtower/wtwire/wtwire_test.go +++ b/watchtower/wtwire/wtwire_test.go @@ -126,6 +126,18 @@ func TestWatchtowerWireProtocol(t *testing.T) { return mainScenario(&m) }, }, + { + msgType: wtwire.MsgDeleteSession, + scenario: func(m wtwire.DeleteSession) bool { + return mainScenario(&m) + }, + }, + { + msgType: wtwire.MsgDeleteSessionReply, + scenario: func(m wtwire.DeleteSessionReply) bool { + return mainScenario(&m) + }, + }, { msgType: wtwire.MsgError, scenario: func(m wtwire.Error) bool {