Skip to content

Commit 95b2fc5

Browse files
authored
Merge pull request #151 from fystack/feat/nats-client-routing
Feat/nats client routing
2 parents bfe405b + b408230 commit 95b2fc5

31 files changed

+983
-110
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ $ mpcium start -n node2
166166

167167
Mpcium supports flexible client authentication through a signer interface, allowing you to use either local keys or AWS KMS for signing operations.
168168

169+
#### Client ID (Result Routing)
170+
171+
When multiple client instances connect to the same MPC cluster, each client **must** set a unique `ClientID` to avoid result routing conflicts. Without distinct client IDs, two clients requesting operations concurrently may race for the same result message, causing one client to receive the other's response.
172+
173+
- `ClientID` scopes the NATS consumer and result subject so each client only receives its own results.
174+
- Allowed characters: alphanumeric, hyphens, and underscores (e.g. `"backend-svc-1"`, `"mobile_api"`).
175+
- If you only run a single client instance, `ClientID` can be omitted (empty string).
176+
169177
#### Local Signer (Ed25519)
170178

171179
```go
@@ -193,10 +201,11 @@ func main() {
193201
logger.Fatal("Failed to create local signer", err)
194202
}
195203

196-
// Create MPC client with signer
204+
// Create MPC client with signer and a unique client ID
197205
mpcClient := client.NewMPCClient(client.Options{
198206
NatsConn: natsConn,
199207
Signer: localSigner,
208+
ClientID: "backend-svc-1", // unique per client instance
200209
})
201210

202211
// Handle wallet creation results
@@ -253,6 +262,7 @@ func main() {
253262
mpcClient := client.NewMPCClient(client.Options{
254263
NatsConn: natsConn,
255264
Signer: kmsSigner,
265+
ClientID: "kms-client-1", // unique per client instance
256266
})
257267
// ... rest of the client code
258268
}

cmd/mpcium-cli/benchmark.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func benchmarkCommand() *cli.Command {
8080
Value: false,
8181
Category: "authentication",
8282
},
83+
&cli.StringFlag{
84+
Name: "client-id",
85+
Usage: "Client ID for result routing (scopes results to this client instance)",
86+
Category: "configuration",
87+
},
8388
&cli.BoolFlag{
8489
Name: "debug",
8590
Usage: "Enable debug logging",
@@ -244,6 +249,7 @@ func createMPCClient(cmd *cli.Command) (client.MPCClient, error) {
244249
opts := client.Options{
245250
NatsConn: nc,
246251
Signer: signer,
252+
ClientID: cmd.String("client-id"),
247253
}
248254
return client.NewMPCClient(opts), nil
249255
}

cmd/mpcium/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,13 @@ func runNode(ctx context.Context, c *cli.Command) error {
216216
}
217217

218218
directMessaging := messaging.NewNatsDirectMessaging(natsConn)
219-
mqManager := messaging.NewNATsMessageQueueManager("mpc", []string{
220-
"mpc.mpc_keygen_result.*",
221-
event.SigningResultTopic,
222-
"mpc.mpc_reshare_result.*",
223-
}, natsConn)
219+
mqManager := messaging.NewNATsMessageQueueManager("mpc", event.ResultStreamSubjects(), natsConn)
224220

225-
genKeyResultQueue := mqManager.NewMessageQueue("mpc_keygen_result")
221+
genKeyResultQueue := mqManager.NewMessageQueue("mpc_keygen_result", event.KeygenResultSubscriptionSubject(""))
226222
defer genKeyResultQueue.Close()
227-
singingResultQueue := mqManager.NewMessageQueue("mpc_signing_result")
223+
singingResultQueue := mqManager.NewMessageQueue("mpc_signing_result", event.SigningResultSubscriptionSubject(""))
228224
defer singingResultQueue.Close()
229-
reshareResultQueue := mqManager.NewMessageQueue("mpc_reshare_result")
225+
reshareResultQueue := mqManager.NewMessageQueue("mpc_reshare_result", event.ReshareResultSubscriptionSubject(""))
230226
defer reshareResultQueue.Close()
231227

232228
logger.Info("Starting mpcium node", "version", Version, "ID", nodeID, "name", nodeName)

e2e/base_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func (s *E2ETestSuite) SetupMPCClient(t *testing.T) {
196196
mpcClient := client.NewMPCClient(client.Options{
197197
NatsConn: s.natsConn,
198198
Signer: localSigner,
199+
ClientID: "e2e-suite",
199200
})
200201
s.mpcClient = mpcClient
201202
t.Log("MPC client created")

e2e/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/fystack/mpcium/e2e
22

3-
go 1.25.5
3+
go 1.25.8
44

55
require (
66
github.com/bnb-chain/tss-lib/v2 v2.0.2

0 commit comments

Comments
 (0)