Skip to content

Commit

Permalink
input: use multmutex to increase concurrency for musig session manager
Browse files Browse the repository at this point in the history
By using the multimutex here, we'll no longer rely on a single mutex for
the entire musig session set like we used to. Instead, we can use the
session ID to key into a map of mutexes and use those directly.
  • Loading branch information
Roasbeef committed Jun 3, 2023
1 parent f45738a commit 7c2ffc0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
3 changes: 0 additions & 3 deletions funding/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,6 @@ func (f *Manager) failFundingFlow(peer lnpeer.Peer, tempChanID [32]byte,
log.Debugf("Failing funding flow for pending_id=%x: %v",
tempChanID, fundingErr)

fmt.Printf("Failing funding flow for pending_id=%x: %v\n",
tempChanID, fundingErr)

ctx, err := f.cancelReservationCtx(
peer.IdentityKey(), tempChanID, false,
)
Expand Down
39 changes: 22 additions & 17 deletions input/musig2_session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package input
import (
"crypto/sha256"
"fmt"
"sync"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/multimutex"
)

// MuSig2State is a struct that holds on to the internal signing session state
Expand All @@ -34,18 +34,20 @@ type PrivKeyFetcher func(*keychain.KeyDescriptor) (*btcec.PrivateKey, error)
// sessions. Each session is identified by a unique session ID which is used by
// callers to interact with a given session.
type MusigSessionManager struct {
sync.Mutex

keyFetcher PrivKeyFetcher

sessionMtx *multimutex.Mutex[MuSig2SessionID]

musig2Sessions map[MuSig2SessionID]*MuSig2State
}

// NewMusigSessionManager creates a new musig manager given an abstract key
// fetcher.
func NewMusigSessionManager(keyFetcher PrivKeyFetcher) *MusigSessionManager {
return &MusigSessionManager{
keyFetcher: keyFetcher,
keyFetcher: keyFetcher,
musig2Sessions: make(map[MuSig2SessionID]*MuSig2State),
sessionMtx: multimutex.NewMutex[MuSig2SessionID](),
}
}

Expand All @@ -70,7 +72,7 @@ func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
KeyLocator: keyLoc,
})
if err != nil {
return nil, fmt.Errorf("error deriving private key: %v", err)
return nil, fmt.Errorf("error deriving private key: %w", err)
}

// Create a signing context and session with the given private key and
Expand Down Expand Up @@ -98,7 +100,7 @@ func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
// Register the new session.
combinedKey, err := musigContext.CombinedKey()
if err != nil {
return nil, fmt.Errorf("error getting combined key: %v", err)
return nil, fmt.Errorf("error getting combined key: %w", err)
}
session := &MuSig2State{
MuSig2SessionInfo: MuSig2SessionInfo{
Expand All @@ -120,7 +122,7 @@ func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
if tweaks.HasTaprootTweak() {
internalKey, err := musigContext.TaprootInternalKey()
if err != nil {
return nil, fmt.Errorf("error getting internal key: %v",
return nil, fmt.Errorf("error getting internal key: %w",
err)
}
session.TaprootInternalKey = internalKey
Expand All @@ -129,9 +131,12 @@ func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
// Since we generate new nonces for every session, there is no way that
// a session with the same ID already exists. So even if we call the API
// twice with the same signers, we still get a new ID.
m.Lock()
//
// We'll use just all zeroes as the session ID for the mutex, as this
// is a "global" action.
m.sessionMtx.Lock(MuSig2SessionID{})
m.musig2Sessions[session.SessionID] = session
m.Unlock()
m.sessionMtx.Unlock(MuSig2SessionID{})

return &session.MuSig2SessionInfo, nil
}
Expand All @@ -149,8 +154,8 @@ func (m *MusigSessionManager) MuSig2Sign(sessionID MuSig2SessionID,
// We hold the lock during the whole operation, we don't want any
// interference with calls that might come through in parallel for the
// same session.
m.Lock()
defer m.Unlock()
m.sessionMtx.Lock(sessionID)
defer m.sessionMtx.Unlock(sessionID)

session, ok := m.musig2Sessions[sessionID]
if !ok {
Expand Down Expand Up @@ -190,8 +195,8 @@ func (m *MusigSessionManager) MuSig2CombineSig(sessionID MuSig2SessionID,
// We hold the lock during the whole operation, we don't want any
// interference with calls that might come through in parallel for the
// same session.
m.Lock()
defer m.Unlock()
m.sessionMtx.Lock(sessionID)
defer m.sessionMtx.Unlock(sessionID)

session, ok := m.musig2Sessions[sessionID]
if !ok {
Expand Down Expand Up @@ -237,8 +242,8 @@ func (m *MusigSessionManager) MuSig2Cleanup(sessionID MuSig2SessionID) error {
// We hold the lock during the whole operation, we don't want any
// interference with calls that might come through in parallel for the
// same session.
m.Lock()
defer m.Unlock()
m.sessionMtx.Lock(sessionID)
defer m.sessionMtx.Unlock(sessionID)

_, ok := m.musig2Sessions[sessionID]
if !ok {
Expand All @@ -259,8 +264,8 @@ func (m *MusigSessionManager) MuSig2RegisterNonces(sessionID MuSig2SessionID,
// We hold the lock during the whole operation, we don't want any
// interference with calls that might come through in parallel for the
// same session.
m.Lock()
defer m.Unlock()
m.sessionMtx.Lock(sessionID)
defer m.sessionMtx.Unlock(sessionID)

session, ok := m.musig2Sessions[sessionID]
if !ok {
Expand Down

0 comments on commit 7c2ffc0

Please sign in to comment.