mpcium icon indicating copy to clipboard operation
mpcium copied to clipboard

Proposal: remove Consul, run on NATS only

Open anhthii opened this issue 4 weeks ago • 1 comments

Summary

Remove Consul and run mpcium on a 3-server NATS cluster only, in two rolling releases with no downtime:

  1. Release 1 (bridge): uses Consul and NATS heartbeats side by side
  2. Release 2: deletes Consul

Why:

  • Single point of failure: if Consul goes down, every node stops signing.
  • Security: anyone with a Consul token can evict nodes or rewrite keyinfo.
  • Operational overhead: Consul is a second cluster to deploy, secure (ACLs, TLS, CA certs), monitor, upgrade and back up. Every deployment (docker-compose, k8s, ECS) and every customer install carries it, even though mpcium only uses it as a small key-value store.

Result: one cluster to run instead of two, peer failures detected in ~10 s instead of ~25 s, no shared store to tamper with, no new volumes.

Related work: PR #141

#141 replaces Consul with three shared NATS KV buckets (mpc-peers, mpc-ready, mpc-keyinfo). It is open and should not be merged as it stands:

  • Merge conflicts with master; a careless merge drops the 5-failures-before-eviction fix
  • Hard switch: a rolling upgrade leaves the cluster never ready
  • KV buckets have no replicas, so losing one NATS server stops all signing
  • Buckets share the messaging NATS credentials, so a leaked credential can rewrite keyinfo
  • Migration script skips failed wallets silently

Reused from #141: the Consul removal, the config cleanup and the ecdsa: → ecdsa- key-rename handling. Readiness and keyinfo use this plan's design (heartbeats, local Badger) instead of shared buckets.

Current architecture

Current architecture

Target architecture

Target architecture
Data Today Target
Peer list Consul mpc_peers/ local peers.json
Who is up Consul ready/ signed NATS heartbeats (in memory)
Wallet keyinfo Consul threshold_keyinfo/ each node's Badger, next to its share
Stream replicas 1 1 on a single NATS server, 3 on a cluster (automatic)

A single NATS server is still supported (1 replica, automatic), but that server is then a single point of failure:

Single NATS server variant

Workstreams

# Work Release Main files
W1 Automatic stream replicas: 1 on a single NATS server, 3 on a cluster, optional nats.stream_replicas override. Never lowers replicas automatically. A failed raise logs a warning instead of a logger.Fatal 1 pkg/messaging/*
W3 Local keyinfo + version check: keyinfo stored in Badger. Each node sends its share version in the first signing message; R1 logs a mismatch, R2 aborts the session 1 pkg/keyinfo, pkg/mpc/*_session.go
W4 Heartbeats: each node sends a signed mpc.heartbeat.<id> every 2 s; a peer is up if seen in the last 10 s 1 pkg/mpc/registry.go
W5 Health checks: peer up/down metrics, probes point at the mpcium health server and NATS /healthz 1, 2 pkg/healthcheck
W6 Bridge mode + auto-migration: keeps Consul as today, also sends heartbeats; copies, verifies and self-repairs keyinfo in Badger; marks migration complete (see below) 1 pkg/mpc/registry.go, cmd/mpcium
W2 Remove Consul: client, config, CLI and dependency. register-peers writes peers.json. Keeps a read-only Consul import used only if the migration marker is missing 2 pkg/infra/consul.go, cmd/*, go.mod
W7 Deployment: 3-node NATS cluster with mTLS routes, docker-compose/k8s/ECS guides updated 1, 2 deployments/*

Failure behaviour (3 NATS servers, 3 replicas)

Scenario Result
1 server down ✅ signing continues; keygen and reshare wait
1 server cut off from the network ✅ the other 2 keep signing, no split-brain
2 servers down ⛔ stops (MPC would stop here anyway)
Back to 2 servers up ✅ recovers with no manual steps

Rollout

Rollout plan
  • Every step goes one node at a time: current → R1 → R2
  • No keygen or reshare during a rollout
  • Rollback goes one step back at a time while Consul is still running
  • New installs go straight to the latest release, with no Consul
How mixed-version nodes see each other
Node A Node B Sees peer via
current R1 Consul ready/
R1 R1 Consul + heartbeats
R1 R2 heartbeats
R2 R2 heartbeats
current R2 ❌ not supported

Automatic migration: no script, no manual checks

For the user this is just two normal upgrades. Mpcium moves and checks the data itself.

Release 1 keeps itself correct

  • Copy and read back: at startup, every wallet's keyinfo is copied from Consul into Badger, read back and compared
  • Write to both: keygen and reshare write keyinfo to Consul and Badger in the same operation, so new wallets are never missed
  • Repair loop: every minute or so, R1 compares all of Consul with Badger and re-copies any difference from Consul (still the source of truth)
  • Check against the shares: every share in Badger must have keyinfo whose participant list includes this node and whose threshold matches the share
  • Nodes check each other: each heartbeat carries a hash of the node's keyinfo. When every node has done a clean pass and the hashes match, R1 records migration complete in Badger and the health endpoint reports consul_removable: true

Release 2 only runs when it's safe

  • Marker set (normal path): starts normally
  • Marker missing (R1 skipped): imports keyinfo from Consul read-only, runs the same checks, then starts. If Consul can't be reached, it refuses to start with a clear error
  • Empty Badger (fresh install): starts normally
  • Safety net: the share-version check in the signing handshake turns any remaining mismatch into a clear error, never a wrong signature

What the user does

  1. Rolling upgrade to R1
  2. Rolling upgrade to R2 when convenient
  3. Switch off Consul once the health endpoint shows consul_removable: true

Risks

Risk Mitigation
A node misses a reshare and keeps an old share version check aborts signing with a clear error; resync command
Migration copies bad keyinfo read-back check, repair loop, cross-node hash check; R2 checks the marker before starting
Heartbeats flap on slow links configurable interval/timeout, every state change logged
No Consul UI mpcium-cli commands and health endpoint show the same data

Acceptance criteria

  • [ ] Unit + e2e tests pass with no Consul
  • [ ] Keygen, ECDSA/EdDSA signing and reshare work end to end
  • [ ] 1 server killed or cut off: signing continues within 10 s
  • [ ] 2 servers killed, then 1 restarted: signing resumes automatically
  • [ ] Replicas picked correctly (1 standalone, 3 cluster, starts with a server down)
  • [ ] Share version mismatch gives a clear error, never a wrong signature
  • [ ] Forged or unsigned heartbeat is rejected
  • [ ] Rolling rehearsal current → R1 → R2 loses no signatures; rollback works
  • [ ] Keyinfo corrupted or deleted in Badger under R1 is repaired automatically
  • [ ] R2 on a node that skipped R1 imports keyinfo from Consul and signs correctly; with Consul unreachable it refuses to start

anhthii avatar Nov 04 '26 11:11 anhthii

+1 on the direction. I checked the plan against master; notes below.

1. Keyinfo is write-once today, which simplifies migration Refresh (reshare) isn't used yet, and keygen only runs when all peers are ready, so every node gets the same keyinfo and it never changes. A one-time copy at R1 startup plus dual-write for new wallets is enough; the per-minute repair loop and cross-node hash can be reduced to a one-time count/hash check.

2. Refresh with local keyinfo Reshare is used as a refresh on the fixed 3-node committee, so every node is in both committees and writes the new keyinfo. To handle:

  • R2 should reject a reshare whose new committee differs from the current one. Old-only nodes never save keyinfo (saveData.ECDSAPub != nil in the resharing sessions), so with local keyinfo a removed node would keep its old keyinfo and share.
  • If one node fails mid-refresh, it stays on v1 while the others move to v2. Signing includes every ready participant, so the v2 nodes pull in the stale node and stall while it's online, even though they have t+1 on their own. The W3 version check should exclude the stale node and let the rest sign, not abort the session. The version is already in the party ID key (nodeID:version), so it can be read from msg.From without a new field. Both signing sessions already verify the signature against the stored pubkey, so this is about not stalling, not about wrong signatures.
  • Save share + keyinfo in one Badger transaction (KVStore needs a batch method).

3. Bridge readiness (R1) R1 treats a peer as up when it has Consul ready/ AND is alive: a fresh signed heartbeat, or, for peers still on current, a reply on healthcheck:<id>. R1 keeps answering healthcheck:<id>, since current nodes evict after 5 consecutive no responders. That reply is an unsigned "OK": fine for the bridge, but R2 should rely only on signed heartbeats.

4. Heartbeats

  • NATS uses PingInterval(20s) × MaxPingsOutstanding(3), so a node can take ~60 s to notice a dead connection while peers mark it down at 10 s. Lower these, or treat a missing echo of the node's own heartbeat as a disconnect.
  • Marking a peer down calls RemovePeer, which drops its symmetric key and breaks in-flight sessions. Exclude the peer from new sessions at 10 s; drop the key only on restart (boot_id in the heartbeat).
  • Add boot_id + monotonic seq for replay protection.
  • Send a signed "leaving" heartbeat on shutdown to replace Resign().

5. Rollout

  • R1's repair loop must never delete Badger-only entries. R2 → R1 rollback is only safe if no keygen ran on R2.
  • peers.json is already read locally by the identity store, so dropping Consul mpc_peers/ removes a duplicate source.

6. Stream replicas (W1)

  • Consumers don't set Replicas; verify they follow the stream on R1 → R3, or set it explicitly.
  • ensureStreamExists ignores retention/storage/discard from the broker config. Keep the rest of the config unchanged when adding Replicas, since CreateOrUpdateStream applies the full config.

Extra acceptance criteria

  • [ ] Mixed current/R1 cluster signs correctly at every rollout step
  • [ ] Node killed mid-refresh: the other two keep signing that wallet while it's back online on the old version
  • [ ] Reshare with a different committee is rejected on R2

Separate issue: old shares (<wallet>_v<n>) are never deleted, so a refresh doesn't protect against previously leaked shares. Delete them once every node has confirmed the new version.

vietddude avatar Nov 06 '26 10:11 vietddude