Skip to content

feat: CON-1445 Start remote DKG as soon as request appears in state - #10055

Merged
eichhorl merged 166 commits into
masterfrom
eichhorl/early-dkg-configs
May 12, 2026
Merged

eichhorl merged 166 commits into
masterfrom
eichhorl/early-dkg-configs

Conversation

@eichhorl

@eichhorl eichhorl commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Background

The NiDkgConfig contains the data necessary for a subnet to run one instance of the NiDKG protocol. For remote DKG (where a subnet generates key material for a different subnet), these configs are derived from request contexts stored in the replicated state.

Problem

Before this PR, these remote NiDkgConfigs were only created and included as part of summary blocks. This means, a remote DKG request had to wait in the replicated state until consensus reaches the next summary block, at which point the config is created and the remote DKG protocol is started. This increases the worst case latency of remote DKG requests (i.e. subnet/engine creations) by up to one DKG interval.

Proposed Changes

Instead of waiting until the next summary, we can create the NiDkgConfigs on demand, as soon as the corresponding request appears in the replicated state. We continue to use the start_height of the most recent summary block for these configs, essentially treating them "as if" they had already existed when the last summary was created. The only difference is that the number of remaining blocks to complete the remote DKG may be reduced, since it may be started at any point throughout the interval.

For simplicity, this PR removes the handling of remote DKG as part of summary blocks entirely, which implies that remote NiDkgConfigs are no longer stored in summary blocks. Therefore, when creating/validating dealings and transcripts, we now iterate over the union of local configs in the summary and remote configs derived from the replicated state.

Additionally, this means that config creation errors and timeouts are now also handled by data blocks:

  • Config creation errors are included in the data block, which will generate a reject response to the context.
  • A remote DKG request context increases its attempt counter (stored in the summary), whenever a new summary block is created. Once this counter exceeds the maximum number of attempts, the next data block will include a timeout response.
  • Completed requests of the current interval (successful or unsuccessful) are detected by traversing previous data blocks, such that the same request isn't handled multiple times.
  • Completed requests of the past interval (successful or unsuccessful) are tracked by the summary with an attempt counter of 0. These requests are ignored when creating future NiDkgConfigs, such that the same request isn't handled multiple times.
  • Note that we consider a request completed if any one of it's transcripts were completed.

Future Work

  • Currently, we may start a remote DKG request near the end of the interval, without enough data blocks left to include enough dealings to finish all required transcripts. This means the request will be retried at the start of the next interval. To improve efficiency, we should not create and include dealings for remote DKGs that were received with an insufficient number of data blocks left. Instead, we should only start working on the request at the start of the next interval.

  • Consider increasing the number of dealings per block and the number of prioritized remote DKGs per interval.

@eichhorl
eichhorl requested a review from a team as a code owner May 8, 2026 14:13
@eichhorl

eichhorl commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Can something break on upgrading the subnet if the old replica version included a NiDkgConfig in the CUP's summary block? From what I can tell, it looks like we do not re-validate the CUP's block when starting up (which would fail on the new replica version) but directly insert it into our validated pool instead. So it appears to look fine. Also from NiDKG's PoV, it does not make a difference whether the remote config comes from the state or the summary since merge_configs unifies them.
But more generally, couldn't having an invalid block in our validated pool break stuff elsewhere?

This is not something we usually pay attention to. Technically, I guess every upgrade summary is "invalid" on the new version, because it contains the wrong replica version.

The only place where the upgrade CUP is validated again after the upgrade should be when it is fetched by an orchestrator of a different node that is still on the old version. But in that case we only validate the threshold signature. There, what's more important is that the new replica still returns the exact same bytes that the CUP was created with on the old version, which is why we store the original bytes separately in the pool. Otherwise the byte representation could change if an old CUP was deserialized and serialized again using the new version.

@pierugo-dfinity pierugo-dfinity left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🚀 🚀

Comment thread rs/consensus/dkg/src/lib.rs Resolved
Comment thread rs/consensus/dkg/src/lib.rs Resolved
Comment thread rs/consensus/dkg/src/lib.rs Resolved
Comment thread rs/consensus/dkg/src/lib.rs Resolved
Comment thread rs/consensus/dkg/src/lib.rs Resolved
Comment thread rs/consensus/dkg/src/payload_builder.rs Resolved
Comment thread rs/consensus/dkg/src/payload_builder.rs Resolved
Comment thread rs/types/types/src/consensus/dkg.rs Resolved
Comment thread rs/consensus/dkg/src/payload_builder.rs Resolved
@pierugo-dfinity

Copy link
Copy Markdown
Contributor

every upgrade summary is "invalid" on the new version, because it contains the wrong replica version.

Good point. Then, this addresses my concern.

There, what's more important is that the new replica still returns the exact same bytes that the CUP was created with

Good to know, right, thanks!

@eichhorl eichhorl removed the CI_ALL_BAZEL_TARGETS Runs all bazel targets label May 12, 2026
@eichhorl
eichhorl enabled auto-merge May 12, 2026 08:03
@eichhorl
eichhorl disabled auto-merge May 12, 2026 09:23
@eichhorl
eichhorl added this pull request to the merge queue May 12, 2026
Merged via the queue into master with commit 3af058d May 12, 2026
87 of 89 checks passed
@eichhorl
eichhorl deleted the eichhorl/early-dkg-configs branch May 12, 2026 09:43
pull Bot pushed a commit to bit-cook/ic that referenced this pull request May 19, 2026
…ty#10250)

Instead of the verbose `(NiDkgId, CallbackId, Result<NiDkgTranscript,
String>)` triple, this PR introduces a dedicated struct for remote NiDKG
transcript creation results.

Additionally, we remove all mentions of "early" remote transcripts.
After dfinity#10055, "early" transcript
creation (i.e. as part of data blocks) is the only way for remote
transcripts to be created, since they can no longer be part of summary
blocks. Therefore, we can drop the "early" prefix.

Note that, although we change the type of the summary payload, this
change is backward/forward compatible (see passing
`cup_compatibility_test`). This is because the hash function of the
triple is equal to the one of the struct, therefore no migration is
necessary.
AleDema pushed a commit to AleDema/ic that referenced this pull request Oct 18, 2026
`//rs/tests/consensus:cup_explorer_test` flaked 4 times in 182 runs
since dfinity#11786 (2026-10-09), across `_farm`, `_local` and
`_head_nns_farm`, and always the same way. The `test` task fails with
`assertion left == right failed, left: 5, right: 4` because the
`RecoverSubnet` proposal ends `Failed` instead of `Executed`. Its
failure reason: the registry canister traps at
`do_recover_subnet.rs:124`, because the system subnet answered the
recovery's `setup_initial_dkg` request with `Attempts to run this DKG
repeatedly failed`.

### Root cause

The system subnet rejects the request because it uses up its attempts
while it still cannot use the request's registry version:

1. `do_recover_subnet` sends `SetupInitialDKG` with the registry
canister's latest version (`pre_call_registry_version`). The test
created that version 3.5-4.2 s earlier with its "Update DKG length"
proposal, the one that puts a registry version between halting and
recovery.
2. Blocks only include dealings and transcripts for a remote DKG request
once the block's registry version has reached the request's version
(`build_callback_id_config_map`, `remote.rs:185`). The block maker only
uses a version that has been in the replica's registry client for at
least `POLLING_PERIOD + poll_delay_duration_ms` = 10 s
(`get_stable_registry_version`). Before that, the orchestrator's
registry replicator has to store the version (it polls every 5 s), and
the replica's registry client has to pick it up from the local store
(also every 5 s). So the request cannot be served until 10-20 s after
the version is created. Each failed replicator poll adds up to ~10 s.
3. Meanwhile, every DKG summary increments the request's attempt
counter, whether or not the request could be served
(`get_updated_remote_dkg_attempts`). The request is rejected at the 5th
summary after it arrives (`MAX_REMOTE_DKG_ATTEMPTS`), i.e. after 4 to 5
DKG intervals.
4. dfinity#11786 set the system subnet's DKG interval to 14 so that the subnet
generates its vetKD key quickly. On this fast single-node subnet a
15-block interval lasts 3.5-4 s, so the request was rejected only 15-17
s after it arrived.
5. Measured from the creation of the version, that budget is ~20-21 s,
minus the ~3 blocks the DKG needs once it can use the version. That is
about the worst case of point 2, so the request is rejected whenever
both pollers pick the version up late.

That is what happened in the 4 failures:
- The replicator stored the new version 4.5-5.3 s after its creation,
the top of its range.
- The version became usable at most ~1.2 s too late for the DKG to
finish before the 5th summary.
- In the 4 passing retries, the remote transcripts were created
10.9-15.6 s after the version was stored, as point 2 predicts.
- Apart from the two errors that carry the rejection itself, no
system-node log shows any DKG dealing or transcript error.

### Fix

Give the system subnet its own DKG interval of 49 instead of 14. The app
subnet keeps 14, so it still halts quickly.

- **Not shorter.** The subnet makes at most 5 blocks/s, because a single
node notarizes after `initial_notary_delay` = 200 ms. At that rate, 4
intervals of 50 blocks give the request at least ~40 s. That is about
twice the 10-20 s of point 2, with room left for one failed replicator
poll.
- **Not much longer.** The system subnet's first signed CUP has to exist
before `test_extract_master_public_keys` downloads its first CUP, 40-50
s after genesis. While no CUP verifies, `explore()` writes nothing, and
`extract_master_public_keys` then panics on the empty file instead of
retrying.
- With 49, that first CUP is at height 50. It exists 12-19 s after
genesis and already holds all keys, the vetKD key included.
- With 99, the first CUP would be at height 100. In the slowest attempts
it would come only a few seconds before the download.

### History

This test has hit the same failure before:

- dfinity#10055 (2026-05-12) started counting every interval as an attempt.
Before it, a request was only counted in intervals in which its DKG
configs were created.
- Back then the system subnet used interval 14, and the test flaked with
the same error in June. dfinity#10515 fixed that by returning the system subnet
to the default interval of 499.
- dfinity#11786 set the interval back to 14 to get the vetKD key quickly.

The new comment above the constant explains why the interval must not be
shortened again.

### Verification

On a devenv, `bazel test --runs_per_test=3` of the 4 non-manual variants
passed 12 out of 12 runs: `_farm`, `_head_nns_farm`, `_local` and
`_head_nns_local`.

- **The recovery's remote DKG.** In the 6 `_local` runs, the test log
includes the system node's journal. There the remote DKG transcripts
were created 8.6-12.6 s after the request, before or during the first
DKG interval that counted it. The 5th summary would only have come 65-90
s after the request; the devenv makes fewer blocks per second than CI.
- One run hit the failures' unlucky phase: the version was stored 0.3 s
after the request arrived. It still finished after 12.6 s.
- **The key extraction.** In all 12 runs,
`test_extract_master_public_keys` got a CUP holding all keys (height
100) on its first attempt.

### Possible follow-ups (not in this PR)

- **Consensus:** `get_updated_remote_dkg_attempts` could stop counting a
request while the summary's registry version hasn't reached the
request's version, as before dfinity#10055. Requests would then no longer be
rejected as "repeatedly failed" without being attempted. This matters
little for subnets with long DKG intervals, such as the 499-block
default, where 4 intervals last far longer than the ~20 s registry
delay.
- **CUP explorer:** `explore()` could report whether it wrote a CUP, and
`read_and_verify_cup` could return errors instead of panicking.
`test_extract_master_public_keys` could then retry while no CUP verifies
yet, which would lift the upper bound on the system subnet's DKG
interval in this test.

This PR was created following the steps in
`.claude/skills/fix-flaky-tests/SKILL.md`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Leo Eichhorn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants