From 9b1bc9fd79581d0d11d002520eea74a81bba7d48 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 23 Apr 2019 20:05:47 -0700 Subject: [PATCH] watchtower/wtwire/create_session_reply: return last applied --- watchtower/wtserver/create_session.go | 22 ++++++++++++---------- watchtower/wtwire/create_session_reply.go | 8 ++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/watchtower/wtserver/create_session.go b/watchtower/wtserver/create_session.go index 53cc9cf6..2f89b85a 100644 --- a/watchtower/wtserver/create_session.go +++ b/watchtower/wtserver/create_session.go @@ -29,14 +29,14 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, log.Debugf("Already have session for %s", id) return s.replyCreateSession( peer, id, wtwire.CreateSessionCodeAlreadyExists, - existingInfo.RewardAddress, + existingInfo.LastApplied, existingInfo.RewardAddress, ) // Some other database error occurred, return a temporary failure. case err != wtdb.ErrSessionNotFound: log.Errorf("unable to load session info for %s", id) return s.replyCreateSession( - peer, id, wtwire.CodeTemporaryFailure, nil, + peer, id, wtwire.CodeTemporaryFailure, 0, nil, ) } @@ -45,7 +45,8 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, log.Debugf("Rejecting CreateSession from %s, unsupported blob "+ "type %s", id, req.BlobType) return s.replyCreateSession( - peer, id, wtwire.CreateSessionCodeRejectBlobType, nil, + peer, id, wtwire.CreateSessionCodeRejectBlobType, 0, + nil, ) } @@ -61,7 +62,7 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, log.Errorf("Unable to generate reward addr for %s: %v", id, err) return s.replyCreateSession( - peer, id, wtwire.CodeTemporaryFailure, nil, + peer, id, wtwire.CodeTemporaryFailure, 0, nil, ) } @@ -72,7 +73,7 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, log.Errorf("Unable to generate reward script for "+ "%s: %v", id, err) return s.replyCreateSession( - peer, id, wtwire.CodeTemporaryFailure, nil, + peer, id, wtwire.CodeTemporaryFailure, 0, nil, ) } } @@ -99,14 +100,14 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, if err != nil { log.Errorf("unable to create session for %s", id) return s.replyCreateSession( - peer, id, wtwire.CodeTemporaryFailure, nil, + peer, id, wtwire.CodeTemporaryFailure, 0, nil, ) } log.Infof("Accepted session for %s", id) return s.replyCreateSession( - peer, id, wtwire.CodeOK, rewardScript, + peer, id, wtwire.CodeOK, 0, rewardScript, ) } @@ -115,11 +116,12 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID, // Otherwise, this method returns a connection error to ensure we don't continue // communication with the client. func (s *Server) replyCreateSession(peer Peer, id *wtdb.SessionID, - code wtwire.ErrorCode, data []byte) error { + code wtwire.ErrorCode, lastApplied uint16, data []byte) error { msg := &wtwire.CreateSessionReply{ - Code: code, - Data: data, + Code: code, + LastApplied: lastApplied, + Data: data, } err := s.sendMessage(peer, msg) diff --git a/watchtower/wtwire/create_session_reply.go b/watchtower/wtwire/create_session_reply.go index 9b63b08c..82d2b6ab 100644 --- a/watchtower/wtwire/create_session_reply.go +++ b/watchtower/wtwire/create_session_reply.go @@ -43,6 +43,12 @@ type CreateSessionReply struct { // Code will be non-zero if the watchtower rejected the session init. Code CreateSessionCode + // LastApplied is the tower's last accepted sequence number for the + // session. This is useful when the session already exists but the + // client doesn't realize it's already used the session, such as after a + // restoration. + LastApplied uint16 + // Data is a byte slice returned the caller of the message, and is to be // interpreted according to the error Code. When the response is // CreateSessionCodeOK, data encodes the reward address to be included in @@ -63,6 +69,7 @@ var _ Message = (*CreateSessionReply)(nil) func (m *CreateSessionReply) Decode(r io.Reader, pver uint32) error { return ReadElements(r, &m.Code, + &m.LastApplied, &m.Data, ) } @@ -74,6 +81,7 @@ func (m *CreateSessionReply) Decode(r io.Reader, pver uint32) error { func (m *CreateSessionReply) Encode(w io.Writer, pver uint32) error { return WriteElements(w, m.Code, + m.LastApplied, m.Data, ) }