refactor: migrate to modern NewHandler protocol API with logging support

Update all protocol handlers to use NewHandler() instead of legacy
NewMultiHandler() compatibility wrapper. This enables proper context
cancellation, protocol-level logging, and session ID tracking.

Changes:
- Add context.Background() and session IDs to all handler creations
- Add log.Logger fields to CGGMP21Protocol and FROSTProtocol structs
- Use protocol.DefaultConfig() for handler configuration
- Update handler types from *MultiHandler to *Handler
This commit is contained in:
2025-12-25 09:51:41 -08:00
parent e680368177
commit efbcf5a8be
4 changed files with 157 additions and 44 deletions
+25 -9
View File
@@ -1,10 +1,13 @@
package mpc
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/luxfi/log"
"github.com/luxfi/log/level"
"github.com/luxfi/threshold/pkg/math/curve"
"github.com/luxfi/threshold/pkg/party"
"github.com/luxfi/threshold/pkg/pool"
@@ -28,13 +31,14 @@ type KeyGenSession interface {
type cggmp21KeygenSession struct {
session
handler *protocol.MultiHandler
pool *pool.Pool
config *config.Config
messagesCh chan *protocol.Message
resultMutex sync.Mutex
done bool
resultErr error
handler *protocol.Handler
pool *pool.Pool
config *config.Config
messagesCh chan *protocol.Message
resultMutex sync.Mutex
done bool
resultErr error
protocolLogger log.Logger
}
func newCGGMP21KeygenSession(
@@ -88,15 +92,27 @@ func newCGGMP21KeygenSession(
func (s *cggmp21KeygenSession) Init() {
s.logger.Info().
Str("walletID", s.walletID).
Int("threshold", s.threshold).
Interface("partyIDs", s.partyIDs).
Msg("Initializing CGGMP21 keygen session")
// Create protocol logger
s.protocolLogger = log.NewTestLogger(level.Info)
// Create CGGMP21 keygen protocol
startFunc := cmp.Keygen(curve.Secp256k1{}, s.selfPartyID, s.partyIDs, s.threshold, s.pool)
// Create handler
handler, err := protocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
handler, err := protocol.NewHandler(
ctx,
s.protocolLogger,
nil, // No prometheus registry
startFunc,
[]byte(s.walletID),
protocol.DefaultConfig(),
)
if err != nil {
s.logger.Fatal().Err(err).Msg("Failed to create keygen handler")
return
+28 -13
View File
@@ -1,11 +1,14 @@
package mpc
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"sync"
"github.com/luxfi/log"
"github.com/luxfi/log/level"
"github.com/luxfi/threshold/pkg/ecdsa"
"github.com/luxfi/threshold/pkg/party"
"github.com/luxfi/threshold/pkg/pool"
@@ -30,17 +33,18 @@ type SignSession interface {
type cggmp21SigningSession struct {
session
handler *protocol.MultiHandler
pool *pool.Pool
config *config.Config
signature *ecdsa.Signature
messagesCh chan *protocol.Message
resultMutex sync.Mutex
done bool
resultErr error
messageHash []byte
signerIDs []party.ID
useBroadcast bool
handler *protocol.Handler
pool *pool.Pool
config *config.Config
signature *ecdsa.Signature
messagesCh chan *protocol.Message
resultMutex sync.Mutex
done bool
resultErr error
messageHash []byte
signerIDs []party.ID
useBroadcast bool
protocolLogger log.Logger
}
func newCGGMP21SigningSession(
@@ -119,11 +123,22 @@ func (s *cggmp21SigningSession) Init() {
Bool("useBroadcast", s.useBroadcast).
Msg("Initializing CGGMP21 signing session")
// Create protocol logger
s.protocolLogger = log.NewTestLogger(level.Info)
// Create CGGMP21 signing protocol
startFunc := cmp.Sign(s.config, s.signerIDs, s.messageHash, s.pool)
// Create handler
handler, err := protocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
handler, err := protocol.NewHandler(
ctx,
s.protocolLogger,
nil, // No prometheus registry
startFunc,
[]byte(s.sessionID),
protocol.DefaultConfig(),
)
if err != nil {
s.logger.Fatal().Err(err).Msg("Failed to create signing handler")
return
+63 -13
View File
@@ -1,6 +1,7 @@
package cggmp21
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
@@ -8,6 +9,8 @@ import (
"math/big"
"sync"
"github.com/luxfi/log"
"github.com/luxfi/log/level"
mpsEcdsa "github.com/luxfi/threshold/pkg/ecdsa"
"github.com/luxfi/threshold/pkg/math/curve"
"github.com/luxfi/threshold/pkg/party"
@@ -21,13 +24,15 @@ import (
// CGGMP21Protocol implements the Protocol interface using CGGMP21
type CGGMP21Protocol struct {
pool *pool.Pool
pool *pool.Pool
logger log.Logger
}
// NewCGGMP21Protocol creates a new CGGMP21 protocol adapter
func NewCGGMP21Protocol() *CGGMP21Protocol {
return &CGGMP21Protocol{
pool: pool.NewPool(0), // Use max threads
pool: pool.NewPool(0), // Use max threads
logger: log.NewTestLogger(level.Info),
}
}
@@ -54,8 +59,17 @@ func (p *CGGMP21Protocol) KeyGen(selfID string, partyIDs []string, threshold int
// Create the keygen protocol
startFunc := cmp.Keygen(curve.Secp256k1{}, party.ID(selfID), ids, threshold, p.pool)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("cggmp21-keygen-%s", selfID))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create keygen handler: %w", err)
}
@@ -77,8 +91,17 @@ func (p *CGGMP21Protocol) Refresh(cfg protocol.KeyGenConfig) (protocol.Party, er
// Create refresh protocol
startFunc := cmp.Refresh(cmpConfig, p.pool)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("cggmp21-refresh-%s", cfg.GetPartyID()))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create refresh handler: %w", err)
}
@@ -106,8 +129,17 @@ func (p *CGGMP21Protocol) Sign(cfg protocol.KeyGenConfig, signers []string, mess
// Create sign protocol
startFunc := cmp.Sign(cmpConfig, signerIDs, messageHash, p.pool)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("cggmp21-sign-%s-%x", cfg.GetPartyID(), messageHash[:8]))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create sign handler: %w", err)
}
@@ -135,8 +167,17 @@ func (p *CGGMP21Protocol) PreSign(cfg protocol.KeyGenConfig, signers []string) (
// Create presign protocol
startFunc := cmp.Presign(cmpConfig, signerIDs, p.pool)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("cggmp21-presign-%s", cfg.GetPartyID()))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create presign handler: %w", err)
}
@@ -163,8 +204,17 @@ func (p *CGGMP21Protocol) PreSignOnline(cfg protocol.KeyGenConfig, preSig protoc
// Create presign online protocol
startFunc := cmp.PresignOnline(cmpConfig, cmpPreSig.preSig, messageHash, p.pool)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("cggmp21-presign-online-%s-%x", cfg.GetPartyID(), messageHash[:8]))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create presign online handler: %w", err)
}
@@ -177,7 +227,7 @@ func (p *CGGMP21Protocol) PreSignOnline(cfg protocol.KeyGenConfig, preSig protoc
// partyAdapter adapts mpsProtocol.Handler to protocol.Party
type partyAdapter struct {
handler *mpsProtocol.MultiHandler
handler *mpsProtocol.Handler
selfID string
mu sync.Mutex
done bool
+41 -9
View File
@@ -1,6 +1,7 @@
package frost
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
@@ -8,6 +9,8 @@ import (
"math/big"
"sync"
"github.com/luxfi/log"
"github.com/luxfi/log/level"
"github.com/luxfi/threshold/pkg/math/curve"
"github.com/luxfi/threshold/pkg/party"
"github.com/luxfi/threshold/pkg/pool"
@@ -19,13 +22,15 @@ import (
// FROSTProtocol implements the Protocol interface using FROST for EdDSA
type FROSTProtocol struct {
pool *pool.Pool
pool *pool.Pool
logger log.Logger
}
// NewFROSTProtocol creates a new FROST protocol adapter
func NewFROSTProtocol() *FROSTProtocol {
return &FROSTProtocol{
pool: pool.NewPool(0), // Use max threads
pool: pool.NewPool(0), // Use max threads
logger: log.NewTestLogger(level.Info),
}
}
@@ -52,8 +57,17 @@ func (p *FROSTProtocol) KeyGen(selfID string, partyIDs []string, threshold int)
// Create the FROST keygen protocol for Ed25519/Taproot
startFunc := frost.KeygenTaproot(party.ID(selfID), ids, threshold)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("frost-keygen-%s", selfID))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create FROST keygen handler: %w", err)
}
@@ -83,8 +97,17 @@ func (p *FROSTProtocol) Refresh(cfg protocol.KeyGenConfig) (protocol.Party, erro
// Create refresh protocol
startFunc := frost.Refresh(frostConfig, ids)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("frost-refresh-%s", cfg.GetPartyID()))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create FROST refresh handler: %w", err)
}
@@ -113,8 +136,17 @@ func (p *FROSTProtocol) Sign(cfg protocol.KeyGenConfig, signers []string, messag
// Create sign protocol
startFunc := frost.Sign(frostConfig, signerIDs, messageHash)
// Create handler
handler, err := mpsProtocol.NewMultiHandler(startFunc, nil)
// Create handler with proper context, logging, and session ID
ctx := context.Background()
sessionID := []byte(fmt.Sprintf("frost-sign-%s-%x", cfg.GetPartyID(), messageHash[:8]))
handler, err := mpsProtocol.NewHandler(
ctx,
p.logger,
nil, // No prometheus registry
startFunc,
sessionID,
mpsProtocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("failed to create FROST sign handler: %w", err)
}
@@ -140,7 +172,7 @@ func (p *FROSTProtocol) PreSignOnline(cfg protocol.KeyGenConfig, preSig protocol
// frostPartyAdapter adapts mpsProtocol.Handler to protocol.Party
type frostPartyAdapter struct {
handler *mpsProtocol.MultiHandler
handler *mpsProtocol.Handler
selfID string
isTaproot bool
mu sync.Mutex