Skip to content

feat: allow several fallback keypers in KEYPER_HTTP_URL - #141

Open
steven3002 wants to merge 1 commit into
shutter-network:mainfrom
steven3002:feat/multiple-fallback-keypers
Open

steven3002 wants to merge 1 commit into
shutter-network:mainfrom
steven3002:feat/multiple-fallback-keypers

Conversation

@steven3002

Copy link
Copy Markdown

This closes #68.

What was wrong

The API looks for a key in its own database first. If the key is not there, it asks the one keyper in KEYPER_HTTP_URL. The variable holds one URL, and there was no setting for a second one:

  • A comma-separated value is passed to url.Parse whole, so http://a:3000,http://b:3000 becomes one request to the host a:3000,http. That request fails even when b is up.
  • Some lists do not parse at all, and the API does not start: http://a:3000, http://b:3000 fails on the space, and http://a:3000, on the trailing comma.

The fallback request also had no overall time limit. QueryExternalKeyper uses http.DefaultClient, which has no timeout, and the GET key handlers pass the *gin.Context, whose Done() is nil because ContextWithFallback is not set. Go's default transport limits the connection attempt (30 s) and the TLS handshake (10 s), but not the wait for the answer. In the test runs, a keyper that took the request and never answered had not returned after 15 s over http and 40 s over https. With several keypers this matters more: without a limit, a first keyper that never answers keeps the second from being asked.

What changes

  • KEYPER_HTTP_URL takes one URL, as before, or several separated by commas, as P2P_BOOTSTRAP_ADDRESSES does. White space around entries and empty entries are ignored. Each entry is parsed with url.Parse, as before, and an empty value is still accepted.
  • Config.KeyperHTTPURL becomes Config.KeyperHTTPURLs.
  • getDecryptionKeyFromExternalKeyper asks the keypers in the configured order and returns the first key it gets.
    • A keyper whose request fails (for example, it cannot connect, times out, or its answer cannot be read) is skipped, with a warning in the log that gives its index. A keyper that answers without a key (404, or any other status than 200) is skipped without a warning.
    • If no keyper sends a key, the lookup's result is what a single keyper gives today: not found (404) if the request to any keyper succeeded, otherwise the first keyper's error (500), with the same text as before. /decrypt_commitment handles that result as it does today: after a 404 for the event key, it tries the identity as a time-based key.
    • Once the caller's context is done, the remaining keypers are not asked.
  • Each keyper request has a deadline, derived from the request context. A keyper that has another one after it gets 2 s (defaultKeyperFailoverTimeout). The last keyper, or the only one, gets 5 s (defaultKeyperRequestTimeout). Both are set the same way as defaultTransactionSubmissionTimeout. The shorter bound for all but the last keyper is what Lighthouse does for its --beacon-nodes list unless it is set to use long timeouts ("Use quicker timeouts if a fallback beacon node exists").
  • QueryExternalKeyper, both callers and all error texts are unchanged.

What operators see

  • One URL: the same requests, keys, 404s and error texts as today, with three differences, besides the comma limit below:
    • a keyper that has not sent its whole answer within 5 s now fails after 5 s, and the client gets a 500. On main, a keyper that answered later was waited for and its key served (measured at 8 s); a dropped connection attempt failed after 30 s and a stalled TLS handshake after 10 s, the limits of Go's default transport; and a keyper that took the request and never answered held it with no limit;
    • spaces, tabs or a newline around the URL are now trimmed. On main, a leading space, a tab, a newline, or a space after the host or port stopped startup, and a space after a path was kept in the request path (/v1%20/decryptionKey/...);
    • a keyper request that fails (for example, it cannot connect, times out, or its answer cannot be read) now logs a warning.
  • Several URLs:
    • The keypers are asked in order. If one refuses the connection, the next is asked at once.
    • If one hangs, or its host drops the connection attempt, the next is asked after 2 s. Nothing is remembered between lookups, so this happens on every lookup until that keyper recovers.
    • A keyper that has another one after it and takes longer than 2 s to answer is abandoned at 2 s, even if it would have sent the key later, and the next keyper is asked.
    • The longest one key lookup waits for the keypers is 2 s for each keyper but the last, plus 5 s, reached when every keyper hangs: 7 s for two keypers and 9 s for three. /decrypt_commitment can make two lookups (as an event key, then, after a 404, as a time-based key), so it can wait up to twice as long: 13.8 s measured with two keypers, the first answering 404 after 1.9 s and the second hanging.
    • A lookup in which no keyper sends the key asks every keyper, for example for an event key polled before its trigger fires. /time/get_decryption_key makes no lookup before the identity's timestamp.
  • A URL that itself contains a comma is now split at the comma. http://user:pa,ss@keyper:3000, which main accepts, now stops startup, and http://a:1/x?q=1,2 becomes two entries, http://a:1/x?q=1 and 2. .env.example documents the list form, the bounds and this limit. docker-compose.yml needs no change.

How I checked it

All Go runs used Go 1.26.11 in containers with no network. The stand-in keypers are httptest servers on loopback, except two plain loopback listeners: one that drops connection attempts (below) and one that accepts connections and never writes, for the TLS handshake.

  • On the current main, the new tests in keyper_fallback_test.go show the problem: two URLs fail with lookup 127.0.0.1:<port>,http: no such host, the list with spaces in ListWithSpacesAndTrailingComma fails to parse, and a hung keyper does not return within 8 s. The single-URL tests (key, down, 404, empty value) pass there, and they still pass with this change.
  • With this change, 21 new tests pass, also under -race. The timing tests check each bound from above as well as below. Among them:
    • first down and second answers; first answering 404, 500 or 502 and second answers; all down; down plus not found;
    • order: the second keyper gets no request when the first answers;
    • first hangs, and the second answers after 2 s;
    • first hangs, and the last answers after 3 s more: its key is served, after 5 s in all;
    • two hung keypers fail after 7 s;
    • one keyper alone gets the full 5 s: one that answers after 3 s is served, and one that hangs fails after 5 s;
    • three hung keypers, with the bounds set to 100 ms and 1 s, return after 1.2 s;
    • first refused and second timed out returns the first keyper's error, not the timeout;
    • a caller deadline stops the loop, and no keyper after it is asked;
    • list parsing, including a bad entry.
  • I broke the change on purpose 37 ways, among them swapping the two bounds, giving the last keyper 2 s too, giving every keyper 5 s, raising the 2 s bound to 2.6 s or the 5 s bound to 5.6 s, removing the deadline, asking only the first keyper, and parsing the value as one URL again. The new tests caught all but one of them. That one stops the loop before the next request instead of after it, and differs only by one log line, when the caller's context ends just after a keyper answered without a key.
  • A host that drops connection attempts, made with a loopback listener whose accept queue is full: on main one such keyper fails after 30 s, Go's default dial timeout. With this change, a list with such a keyper first returns the second keyper's key after 2 s, and one such keyper alone fails after 5 s.
  • gofmt on the changed files, go build, go vet ./... and go test on every unit package pass.
  • I ran the tests/ suite, with its database pointed at a local Postgres instead of testcontainers, on main and with this change. On both, the same 3 registration tests fail in every run, and in some runs other tests fail in the test helper makeKeys. Both causes are in the tests' own fixture and helper. TestDecryptionCommitmentNotFound, the one suite test that reaches the fallback, passed in every run in which its makeKeys call did.
  • updated error messages #71's code change still applies on top of this branch (patch --dry-run).

What I did not check

  • I did not test against a real keyper. The stand-ins answer in the format of rolling-shutter's GET /decryptionKey/{eon}/{epochID}.
  • How long production keypers take to answer. The 2 s and 5 s bounds are fixed, not configurable.
  • tests/integration needs live keypers and a chain, so I did not run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add multiple keypers to fall-back to

1 participant