watchtower/wtwire/create_session_reply: return last applied

This commit is contained in:
Conner Fromknecht 2019-04-23 20:05:47 -07:00
parent 9aa775e64e
commit 9b1bc9fd79
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 20 additions and 10 deletions

@ -29,14 +29,14 @@ func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID,
log.Debugf("Already have session for %s", id) log.Debugf("Already have session for %s", id)
return s.replyCreateSession( return s.replyCreateSession(
peer, id, wtwire.CreateSessionCodeAlreadyExists, peer, id, wtwire.CreateSessionCodeAlreadyExists,
existingInfo.RewardAddress, existingInfo.LastApplied, existingInfo.RewardAddress,
) )
// Some other database error occurred, return a temporary failure. // Some other database error occurred, return a temporary failure.
case err != wtdb.ErrSessionNotFound: case err != wtdb.ErrSessionNotFound:
log.Errorf("unable to load session info for %s", id) log.Errorf("unable to load session info for %s", id)
return s.replyCreateSession( 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 "+ log.Debugf("Rejecting CreateSession from %s, unsupported blob "+
"type %s", id, req.BlobType) "type %s", id, req.BlobType)
return s.replyCreateSession( 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", log.Errorf("Unable to generate reward addr for %s: %v",
id, err) id, err)
return s.replyCreateSession( 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 "+ log.Errorf("Unable to generate reward script for "+
"%s: %v", id, err) "%s: %v", id, err)
return s.replyCreateSession( 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 { if err != nil {
log.Errorf("unable to create session for %s", id) log.Errorf("unable to create session for %s", id)
return s.replyCreateSession( return s.replyCreateSession(
peer, id, wtwire.CodeTemporaryFailure, nil, peer, id, wtwire.CodeTemporaryFailure, 0, nil,
) )
} }
log.Infof("Accepted session for %s", id) log.Infof("Accepted session for %s", id)
return s.replyCreateSession( 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 // Otherwise, this method returns a connection error to ensure we don't continue
// communication with the client. // communication with the client.
func (s *Server) replyCreateSession(peer Peer, id *wtdb.SessionID, 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{ msg := &wtwire.CreateSessionReply{
Code: code, Code: code,
Data: data, LastApplied: lastApplied,
Data: data,
} }
err := s.sendMessage(peer, msg) err := s.sendMessage(peer, msg)

@ -43,6 +43,12 @@ type CreateSessionReply struct {
// Code will be non-zero if the watchtower rejected the session init. // Code will be non-zero if the watchtower rejected the session init.
Code CreateSessionCode 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 // 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 // interpreted according to the error Code. When the response is
// CreateSessionCodeOK, data encodes the reward address to be included in // 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 { func (m *CreateSessionReply) Decode(r io.Reader, pver uint32) error {
return ReadElements(r, return ReadElements(r,
&m.Code, &m.Code,
&m.LastApplied,
&m.Data, &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 { func (m *CreateSessionReply) Encode(w io.Writer, pver uint32) error {
return WriteElements(w, return WriteElements(w,
m.Code, m.Code,
m.LastApplied,
m.Data, m.Data,
) )
} }