session: update session remote pub by session ID

It's a better pattern to refer to sessions in the same way consistently.
So we update the UpdateSessionRemotePubKey method to use a session ID as
a reference to the session instead of local pub key.
This commit is contained in:
Elle Mouton 2025-03-04 17:39:03 +02:00
parent 190d3dc828
commit b82e01edf4
No known key found for this signature in database
GPG key ID: D7D916376026F177
5 changed files with 10 additions and 24 deletions

View file

@ -311,8 +311,8 @@ type Store interface {
error)
// UpdateSessionRemotePubKey can be used to add the given remote pub key
// to the session with the given local pub key.
UpdateSessionRemotePubKey(ctx context.Context, localPubKey,
// to the session with the given ID.
UpdateSessionRemotePubKey(ctx context.Context, id ID,
remotePubKey *btcec.PublicKey) error
// GetSession fetches the session with the given ID.

View file

@ -298,29 +298,19 @@ func (db *BoltStore) NewSession(ctx context.Context, label string, typ Type,
}
// UpdateSessionRemotePubKey can be used to add the given remote pub key
// to the session with the given local pub key.
// to the session with the given ID.
//
// NOTE: this is part of the Store interface.
func (db *BoltStore) UpdateSessionRemotePubKey(_ context.Context, localPubKey,
func (db *BoltStore) UpdateSessionRemotePubKey(_ context.Context, id ID,
remotePubKey *btcec.PublicKey) error {
key := localPubKey.SerializeCompressed()
return db.Update(func(tx *bbolt.Tx) error {
sessionBucket, err := getBucket(tx, sessionBucketKey)
if err != nil {
return err
}
serialisedSession := sessionBucket.Get(key)
if len(serialisedSession) == 0 {
return ErrSessionNotFound
}
session, err := DeserializeSession(
bytes.NewReader(serialisedSession),
)
session, err := getSessionByID(sessionBucket, id)
if err != nil {
return err
}

View file

@ -36,7 +36,7 @@ func newMailboxSession() *mailboxSession {
func (m *mailboxSession) start(session *Session,
serverCreator GRPCServerCreator, authData []byte,
onUpdate func(ctx context.Context, local,
onUpdate func(ctx context.Context, id ID,
remote *btcec.PublicKey) error,
onNewStatus func(s mailbox.ServerStatus)) error {
@ -53,7 +53,7 @@ func (m *mailboxSession) start(session *Session,
keys := mailbox.NewConnData(
ecdh, session.RemotePublicKey, session.PairingSecret[:],
authData, func(key *btcec.PublicKey) error {
return onUpdate(ctx, session.LocalPublicKey, key)
return onUpdate(ctx, session.ID, key)
}, nil,
)
@ -112,7 +112,7 @@ func NewServer(serverCreator GRPCServerCreator) *Server {
}
func (s *Server) StartSession(session *Session, authData []byte,
onUpdate func(ctx context.Context, local,
onUpdate func(ctx context.Context, id ID,
remote *btcec.PublicKey) error,
onNewStatus func(s mailbox.ServerStatus)) (chan struct{}, error) {

View file

@ -101,9 +101,7 @@ func TestBasicSessionStore(t *testing.T) {
require.NoError(t, err)
remotePub := remotePriv.PubKey()
err = db.UpdateSessionRemotePubKey(
ctx, session1.LocalPublicKey, remotePub,
)
err = db.UpdateSessionRemotePubKey(ctx, session1.ID, remotePub)
require.NoError(t, err)
// Assert that the session now does have the remote pub key.

View file

@ -1245,9 +1245,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
"autopilot server: %v", err)
}
err = s.cfg.db.UpdateSessionRemotePubKey(
ctx, sess.LocalPublicKey, remoteKey,
)
err = s.cfg.db.UpdateSessionRemotePubKey(ctx, sess.ID, remoteKey)
if err != nil {
return nil, fmt.Errorf("error setting remote pubkey: %v", err)
}