Skip to content

Commit 7989c0d

Browse files
committed
session: add context to UpdateSessionRemotePubKey
1 parent a1017d1 commit 7989c0d

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

session/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ type Store interface {
215215

216216
// UpdateSessionRemotePubKey can be used to add the given remote pub key
217217
// to the session with the given local pub key.
218-
UpdateSessionRemotePubKey(localPubKey,
218+
UpdateSessionRemotePubKey(ctx context.Context, localPubKey,
219219
remotePubKey *btcec.PublicKey) error
220220

221221
// GetSessionByID fetches the session with the given ID.

session/kvdb_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (db *BoltStore) NewSession(_ context.Context, label string, typ Type,
290290
// to the session with the given local pub key.
291291
//
292292
// NOTE: this is part of the Store interface.
293-
func (db *BoltStore) UpdateSessionRemotePubKey(localPubKey,
293+
func (db *BoltStore) UpdateSessionRemotePubKey(_ context.Context, localPubKey,
294294
remotePubKey *btcec.PublicKey) error {
295295

296296
key := localPubKey.SerializeCompressed()

session/server.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package session
22

33
import (
4+
"context"
45
"crypto/tls"
56
"fmt"
67
"sync"
78
"time"
89

910
"github.com/btcsuite/btcd/btcec/v2"
1011
"github.com/lightninglabs/lightning-node-connect/mailbox"
12+
"github.com/lightninglabs/taproot-assets/fn"
1113
"github.com/lightningnetwork/lnd/keychain"
1214
"google.golang.org/grpc"
1315
"google.golang.org/grpc/credentials"
@@ -21,8 +23,9 @@ type GRPCServerCreator func(opts ...grpc.ServerOption) *grpc.Server
2123
type mailboxSession struct {
2224
server *grpc.Server
2325

24-
wg sync.WaitGroup
25-
quit chan struct{}
26+
cancel fn.Option[context.CancelFunc]
27+
wg sync.WaitGroup
28+
quit chan struct{}
2629
}
2730

2831
func newMailboxSession() *mailboxSession {
@@ -33,7 +36,8 @@ func newMailboxSession() *mailboxSession {
3336

3437
func (m *mailboxSession) start(session *Session,
3538
serverCreator GRPCServerCreator, authData []byte,
36-
onUpdate func(local, remote *btcec.PublicKey) error,
39+
onUpdate func(ctx context.Context, local,
40+
remote *btcec.PublicKey) error,
3741
onNewStatus func(s mailbox.ServerStatus)) error {
3842

3943
tlsConfig := &tls.Config{}
@@ -43,10 +47,13 @@ func (m *mailboxSession) start(session *Session,
4347

4448
ecdh := &keychain.PrivKeyECDH{PrivKey: session.LocalPrivateKey}
4549

50+
ctx, cancel := context.WithCancel(context.Background())
51+
m.cancel = fn.Some(cancel)
52+
4653
keys := mailbox.NewConnData(
4754
ecdh, session.RemotePublicKey, session.PairingSecret[:],
4855
authData, func(key *btcec.PublicKey) error {
49-
return onUpdate(session.LocalPublicKey, key)
56+
return onUpdate(ctx, session.LocalPublicKey, key)
5057
}, nil,
5158
)
5259

@@ -81,6 +88,7 @@ func (m *mailboxSession) run(mailboxServer *mailbox.Server) {
8188
}
8289

8390
func (m *mailboxSession) stop() {
91+
m.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
8492
m.server.Stop()
8593
close(m.quit)
8694
m.wg.Wait()
@@ -104,7 +112,8 @@ func NewServer(serverCreator GRPCServerCreator) *Server {
104112
}
105113

106114
func (s *Server) StartSession(session *Session, authData []byte,
107-
onUpdate func(local, remote *btcec.PublicKey) error,
115+
onUpdate func(ctx context.Context, local,
116+
remote *btcec.PublicKey) error,
108117
onNewStatus func(s mailbox.ServerStatus)) (chan struct{}, error) {
109118

110119
s.activeSessionsMtx.Lock()

session/store_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ func TestBasicSessionStore(t *testing.T) {
9595
require.NoError(t, err)
9696
remotePub := remotePriv.PubKey()
9797

98-
err = db.UpdateSessionRemotePubKey(session1.LocalPublicKey, remotePub)
98+
err = db.UpdateSessionRemotePubKey(
99+
ctx, session1.LocalPublicKey, remotePub,
100+
)
99101
require.NoError(t, err)
100102

101103
// Assert that the session now does have the remote pub key.

session_rpcserver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,9 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
12031203
"autopilot server: %v", err)
12041204
}
12051205

1206-
err = s.cfg.db.UpdateSessionRemotePubKey(sess.LocalPublicKey, remoteKey)
1206+
err = s.cfg.db.UpdateSessionRemotePubKey(
1207+
ctx, sess.LocalPublicKey, remoteKey,
1208+
)
12071209
if err != nil {
12081210
return nil, fmt.Errorf("error setting remote pubkey: %v", err)
12091211
}

0 commit comments

Comments
 (0)