diff --git a/backend/mpc-system/services/server-party-co-managed/cmd/server/main.go b/backend/mpc-system/services/server-party-co-managed/cmd/server/main.go index aed62243..58ca8296 100644 --- a/backend/mpc-system/services/server-party-co-managed/cmd/server/main.go +++ b/backend/mpc-system/services/server-party-co-managed/cmd/server/main.go @@ -189,6 +189,7 @@ func main() { eventHandler := createCoManagedSessionEventHandler( ctx, partyID, + messageRouter, participateKeygenUC, ) @@ -304,11 +305,12 @@ func startHTTPServer(cfg *config.Config) error { // createCoManagedSessionEventHandler creates a handler specifically for co_managed_keygen sessions // Two-phase event handling: -// Phase 1 (session_created): Store join token and wait +// Phase 1 (session_created): JoinSession immediately + store session info // Phase 2 (session_started): Execute TSS protocol (same timing as user clients receiving all_joined) func createCoManagedSessionEventHandler( ctx context.Context, partyID string, + messageRouter *grpcclient.MessageRouterClient, participateKeygenUC *use_cases.ParticipateKeygenUseCase, ) func(*router.SessionEvent) { return func(event *router.SessionEvent) { @@ -350,7 +352,7 @@ func createCoManagedSessionEventHandler( return } - // Phase 1: Store session info and wait for session_started + // Phase 1: Get join token joinToken, exists := event.JoinTokens[partyID] if !exists { logger.Error("No join token found for party in session_created", @@ -359,6 +361,22 @@ func createCoManagedSessionEventHandler( return } + // Immediately call JoinSession (this is required to trigger session_started) + joinCtx, joinCancel := context.WithTimeout(ctx, 30*time.Second) + _, err := messageRouter.JoinSession(joinCtx, sessionID, partyID, joinToken) + joinCancel() + if err != nil { + logger.Error("Failed to join session", + zap.String("session_id", event.SessionId), + zap.String("party_id", partyID), + zap.Error(err)) + return + } + + logger.Info("Successfully joined session, waiting for session_started", + zap.String("session_id", event.SessionId), + zap.String("party_id", partyID)) + // Store pending session for later use when session_started arrives pendingSessionCache.Store(event.SessionId, &PendingSession{ SessionID: sessionID, @@ -367,10 +385,6 @@ func createCoManagedSessionEventHandler( CreatedAt: time.Now(), }) - logger.Info("Session created event received, waiting for session_started", - zap.String("session_id", event.SessionId), - zap.String("party_id", partyID)) - case "session_started": // Phase 2: All participants have joined, now execute TSS protocol pendingSession, exists := pendingSessionCache.Get(event.SessionId) @@ -386,6 +400,7 @@ func createCoManagedSessionEventHandler( zap.String("party_id", partyID)) // Execute TSS keygen protocol in goroutine + // Timeout starts NOW (when session_started is received), not at session_created go func() { // 10 minute timeout for TSS protocol execution participateCtx, cancel := context.WithTimeout(ctx, 10*time.Minute)