diff --git a/scripts/resolver/.env.example b/scripts/resolver/.env.example index 880983d32..d5c629dff 100644 --- a/scripts/resolver/.env.example +++ b/scripts/resolver/.env.example @@ -5,7 +5,7 @@ # Chain settings. # ============================================================================ -# Ethereum network: mainnet (the SNRC `.testing` contracts live on mainnet) +# Ethereum network: mainnet (the SNRC `.testing` and `.simplex` contracts are on mainnet) # or holesky (test). Mainnet full sync needs ~1 day and ~1.2 TB NVMe. NETWORK=mainnet @@ -30,9 +30,3 @@ TRUSTED_NODE_URL=https://mainnet-checkpoint-sync.attestant.io # SNRC_LOG_COLOR=always # coloured `docker compose logs` # SNRC_LOG_LEVEL=debug # also log health checks and idle connections # SNRC_TRUSTED_PROXIES=172.16.0.0/12 # behind a reverse proxy on the host; see README - -# .simplex contracts on mainnet; unset, .simplex names answer 400. See README, -# "Configuring addresses". -# SNRC_REGISTRY_SIMPLEX=0xa7f6497be2f1aa41aa2db55aba8a6e0145dff0d2 -# SNRC_REGISTRAR_SIMPLEX=0x44052f7dc9c1cef20b0319a6b75288988dbb12a1 -# SNRC_CONTROLLER_SIMPLEX=0x8005011c6e966656cdb9a222e9a4792600a37219 # proxy diff --git a/scripts/resolver/README.md b/scripts/resolver/README.md index 0a344face..a4ce4fb0e 100644 --- a/scripts/resolver/README.md +++ b/scripts/resolver/README.md @@ -1,7 +1,7 @@ # Self-hosted SNRC stack One `docker compose up` runs the self-hosted SimpleX Namespace (SNRC) backend -against **Ethereum mainnet** (where the `.testing` contracts live): +against **Ethereum mainnet** (where the `.testing` and `.simplex` contracts are deployed): | # | Component | What it does | |---|---|---| @@ -46,8 +46,10 @@ docker compose logs --tail=20 reth ``` This is the long pole (~1 day on mainnet). Until reth is synced the resolver -returns `502`. A TLD whose controller has a name policy then still answers `502` -until `docker compose restart resolver` (see Configuring addresses). +returns `502`. If the resolver started before the node reached block 26459168, +where the `.simplex` controller's name policy was set, every lookup under +`.simplex` then still returns `502` until `docker compose restart resolver` (see +Configuring addresses). ## Verify @@ -63,8 +65,9 @@ curl -s -X POST http://127.0.0.1:8545 \ **2. resolver is healthy:** ```sh curl -s http://127.0.0.1:8000/health | jq -# → {"ok": true, "rpc": "http://reth:8545", "registries": {"testing": "0x…", "simplex": ""}, -# "blockNumber": 23400000, "chainLagSeconds": 12} +# → {"ok": true, "rpc": "http://reth:8545", "registries": {"testing": "0x58fc46996d975c57883564648bda5206d1a0102b", +# "simplex": "0x3ce23ef6a00d89105299b52aadd72b92681414be"}, +# "blockNumber": 26462784, "chainLagSeconds": 12} ``` `chainLagSeconds` is how far the node is behind the wall clock. A resolver that @@ -80,9 +83,19 @@ curl -s http://127.0.0.1:8000/resolve/foobar.testing | jq **4. the route your router will call** (check 3 passes on an older resolver too): ```sh curl -s http://127.0.0.1:8000/v2/resolve/foobar.testing | jq -# → {"type":"registered","expires":1780000000,"graceUntil":…,"nameRecord":{…}} +# → {"lastBlockTs":…,"registration":{"type":"registered","expires":1780000000,"graceUntil":…,"reservedReason_":…,"nameRecord":{…}}} ``` +**5. a reservation read from `.simplex`'s name policy:** +```sh +curl -s http://127.0.0.1:8000/v2/resolve/simplex.simplex | jq +# → {"lastBlockTs":…,"registration":{"type":"reserved","reservedReason":"internal"}} +``` + +If check 5 returns `502` while check 4 passes, and the `listening` log line +shows `simplex=-` in `name_policies`, run `docker compose restart resolver` once +the node is synced. + **Wire your smp-server:** in its `[NAMES]` section set `resolver_endpoint: http://127.0.0.1:8000` (no auth needed for loopback). @@ -100,6 +113,13 @@ curl -s http://127.0.0.1:8000/v2/resolve/foobar.testing | jq - **All images track `:latest`** (reth, nimbus) — you get upstream fixes on each `docker compose pull`; re-run the verify checks after pulling. +- The resolver image contains the script: after updating this directory, run + `docker compose up -d --build resolver`, then checks 2 and 5. +- A `SNRC_*_SIMPLEX` setting added to `.env`, to `docker-compose.yml`, or to the + environment of a direct run, for the earlier `.simplex` deployment (registry + `0xa7f6497be2f1aa41aa2db55aba8a6e0145dff0d2`) overrides the defaults. Remove + all three; `docker compose exec resolver env | grep _SIMPLEX` then prints + nothing. - All ports bind to loopback; expose only what you put behind a TLS reverse proxy. ## Teardown @@ -119,7 +139,7 @@ The resolver (`snrc-resolve.py`, host `127.0.0.1:8000`) is also runnable standalone for local dev (no Docker), via [`uv`](https://docs.astral.sh/uv/): ```sh -uv run scripts/resolver/service/snrc-resolve.py # defaults to local reth + mainnet .testing +uv run scripts/resolver/service/snrc-resolve.py # defaults to local reth + mainnet .testing and .simplex ``` Tests and lint run from `scripts/resolver/service`: @@ -221,7 +241,7 @@ name already knows when it expires. Both timestamps are Unix seconds, and | `grace` | lapsed, but only the previous owner may renew it, until `graceEnds` | | `expired` | lapsed and past grace; anyone may register it | | `unregistered` | never registered, and free to take | -| `unknown` | no `SNRC_REGISTRAR_` configured, so status could not be read | +| `unknown` | the resolver has no registrar for the TLD, so status could not be read | A reservation is orthogonal to the status: a name held back by the registry carries `reasonCode` and `reason` whether or not it is registered. @@ -266,7 +286,7 @@ look free. | Status | Meaning | |---|---| -| 200 | resolved (`status` is `registered` or `grace`, or `unknown` when no registrar is configured) | +| 200 | resolved (`status` is `registered` or `grace`, or `unknown` when the resolver has no registrar for the TLD) | | 400 | TLD not configured, or not a fully-qualified name | | 404 | `unregistered` | | 410 | `expired`: lapsed and past grace, so anyone may take it | @@ -334,9 +354,6 @@ per second, and charges a premium on a lapsed name that it does not expose. A quote from one is therefore only safe for a name that was never registered: an `expired` name gets no price rather than one below what the registrar charges. -**Set `SNRC_CONTROLLER_` wherever `SNRC_REGISTRAR_` is.** Without a -controller there is no oracle, so no name can be priced. - **Upgrade this service before the routers that query it.** Routers from SMP v22 call `/v2/resolve`, which an older resolver does not serve. Every name then answers `ERR NAME RESOLVER "HTTP 404"` until this service is upgraded, while @@ -371,16 +388,12 @@ is where every expiry field comes from, and `labelOf`, which is how a hashed query is answered with a name. A name registered without recording its label cannot answer one, and `/v2/resolve` refuses it rather than answer with a name the client will reject. Its `subnameHook()` names the subname registrar, whose -`ownerOf` says who holds a subname it owns. With no registrar for a TLD, -`/resolve` still works and reports `"status": "unknown"`, and only subnames the -2LD's holder owns in the registry are answered. The **controller** holds -`reservedNames`, which is where `reasonCode` comes from. With no controller a -held-back name reads as not reserved, and no name can be priced. +`ownerOf` says who holds a subname it owns. The **controller** holds +`reservedNames`, which is where `reasonCode` comes from. -All three default to the mainnet `.testing` deployment. `.simplex` has no -default; `.env.example` lists its mainnet addresses. +All three default to the mainnet deployments of `.testing` and `.simplex`. -Newer contracts, which `.simplex` will run, keep `reservedNames` in a **name +Newer contracts, which the `.simplex` deployment uses, keep `reservedNames` in a **name policy** (`SimplexNamePolicy`), with the same `Reason`, and their controller answers no `reservedNames` call. The resolver reads each controller's `namePolicy()` once at startup, before its workers start, and reads reservations @@ -394,18 +407,27 @@ does not answer, the resolver does not listen and reads again, waiting 1 s, then twice as long each time, up to 30 s. After the controller's owner calls `setNamePolicy`, restart the resolver. -The controller default is the **proxy**, not `SimplexControllerImpl`. Storage -lives in the proxy, so the implementation address answers nothing. The two -deployment files use different names for that proxy: -`deployments.mainnet.testing.json` records it under the ENS role name -`ETHRegistrarController`, and `verification.mainnet.testing.json` calls it -`SimplexControllerProxy`. Both are the same address, and it is the one used -here. +Each default is the address that `deployments.mainnet..json` of +[brenzi/simplex-namespace-contract](https://github.com/brenzi/simplex-namespace-contract/tree/2ecae1a77bd605dcc236ba3c3cd93e690345af2c) +records, the **proxy** where the contract is upgradeable: the controller of +`.testing` and all three of `.simplex`. The implementations, the entries named +`@
` or `SimplexControllerImpl` in its +`verification.mainnet..json`, hold no names: a registrar implementation +returns 0 from `nameExpires` for every name, so every unreserved name is +answered as available. Each setting is the address of one entry of the deployments file: + +| Setting | Entry of `deployments.mainnet..json` | +|---|---| +| `SNRC_REGISTRY_` | `ENSRegistry` | +| `SNRC_REGISTRAR_` | `BaseRegistrarImplementation` | +| `SNRC_CONTROLLER_` | `ETHRegistrarController` | To override any of them, set `SNRC_REGISTRY_`, `SNRC_REGISTRAR_` or `SNRC_CONTROLLER_` on the `resolver` service in `docker-compose.yml`, or as env vars when you run the script directly. With docker compose, the -`.simplex` ones can be set in `.env`. +`.simplex` ones can be set in `.env`. An empty value is replaced by the +default. Override the three settings of a TLD together: an unset setting is left +at its mainnet default, which belongs to a different deployment. ### Load and scaling diff --git a/scripts/resolver/docker-compose.yml b/scripts/resolver/docker-compose.yml index 55c84f739..68a4156b6 100644 --- a/scripts/resolver/docker-compose.yml +++ b/scripts/resolver/docker-compose.yml @@ -154,11 +154,10 @@ services: SNRC_RPC: http://reth:8545 SNRC_BIND: 0.0.0.0 # Registry addresses cascade through the script's own defaults - # (mainnet `.testing`; `.simplex` unconfigured). Set explicitly here + # (mainnet `.testing` and `.simplex`). Set explicitly here # only if you're deploying against a different network or contract. # SNRC_REGISTRY_TESTING: 0x... - # Registrar and controller, same cascade. Without the registrar `status` - # is "unknown"; without the controller a reserved name is "unregistered". + # Registrar and controller, same cascade. # SNRC_REGISTRAR_TESTING: 0x... # SNRC_CONTROLLER_TESTING: 0x... # Set in .env (see .env.example); unset or empty means the resolver's default. diff --git a/scripts/resolver/service/snrc-resolve.py b/scripts/resolver/service/snrc-resolve.py index ae545fcaf..2388c7d81 100755 --- a/scripts/resolver/service/snrc-resolve.py +++ b/scripts/resolver/service/snrc-resolve.py @@ -38,13 +38,14 @@ (default: mainnet, 0x58fc46996d975c57883564648bda5206d1a0102b) SNRC_REGISTRY_SIMPLEX ENSRegistry for the .simplex deployment - (default: empty — TLD not yet deployed) + (default: mainnet, + 0x3ce23ef6a00d89105299b52aadd72b92681414be) SNRC_REGISTRAR_ BaseRegistrar (ERC-721) for the TLD; expiry and status - (default: mainnet for .testing, empty for .simplex) + (default: mainnet) SNRC_CONTROLLER_ SimplexController (proxy) for the TLD; reservations, read from the policy its `namePolicy()` names, if any, and through its `prices()` oracle what registering costs - (default: mainnet for .testing, empty for .simplex) + (default: mainnet) SNRC_PORT Listen port (default: 8000) SNRC_BIND Bind address (default: 0.0.0.0) SNRC_WORKERS Worker processes sharing the port (default: CPU count, at most 4) @@ -118,8 +119,7 @@ ) # Each TLD is its own SNRC deployment with its own ENSRegistry. Dispatch -# happens on the rightmost label of the queried name. Empty / unset means -# "not deployed" — requests for that TLD return 400 with a clear error. +# happens on the rightmost label of the queried name. # `... or "..."` makes the script's defaults the single source of truth: # unset AND empty-string both fall through to the literal. docker-compose # can therefore pass `SNRC_REGISTRY_TESTING=${SNRC_REGISTRY_TESTING:-}` @@ -127,13 +127,15 @@ REGISTRIES = { "testing": os.environ.get("SNRC_REGISTRY_TESTING", "") or "0x58fc46996d975c57883564648bda5206d1a0102b", # mainnet .testing - "simplex": os.environ.get("SNRC_REGISTRY_SIMPLEX", ""), # not deployed yet + "simplex": os.environ.get("SNRC_REGISTRY_SIMPLEX", "") + or "0x3ce23ef6a00d89105299b52aadd72b92681414be", } REGISTRARS = { "testing": os.environ.get("SNRC_REGISTRAR_TESTING", "") or "0xef47eb4384b46c89e4482a677c2cbcbd2a6fd85a", # mainnet .testing - "simplex": os.environ.get("SNRC_REGISTRAR_SIMPLEX", ""), # not deployed yet + "simplex": os.environ.get("SNRC_REGISTRAR_SIMPLEX", "") + or "0x4c0589dbc22862babb25eb72d11bba49d84aabe8", } CONTROLLERS = { @@ -141,7 +143,8 @@ # Proxy address, not SimplexControllerImpl: storage is held by the proxy. # Recorded in deployments.json as ETHRegistrarController. or "0xeeb9b6bf5fb68fb726005f7ba549c2f4b32f2dad", # mainnet .testing - "simplex": os.environ.get("SNRC_CONTROLLER_SIMPLEX", ""), # not deployed yet + "simplex": os.environ.get("SNRC_CONTROLLER_SIMPLEX", "") + or "0x68fab655a0204865a2f3ab3a607037e09648c92b", } # The name policy each TLD's controller names, which holds its reservations in diff --git a/scripts/resolver/service/test_snrc_resolve.py b/scripts/resolver/service/test_snrc_resolve.py index 9bf85e9c0..4beea9218 100644 --- a/scripts/resolver/service/test_snrc_resolve.py +++ b/scripts/resolver/service/test_snrc_resolve.py @@ -745,7 +745,7 @@ def setUp(self): snrc.eth_call, snrc.chain_now, ) - snrc.REGISTRIES = {"testing": self.REGISTRY, "simplex": ""} + snrc.REGISTRIES = {"testing": self.REGISTRY, "nosuchtld": ""} snrc.REGISTRARS = {"testing": self.REGISTRAR} snrc.CONTROLLERS = {"testing": ""} snrc.chain_now = lambda: int(time.time()) @@ -1310,7 +1310,7 @@ def setUp(self): self._saved = (snrc.RPC, snrc.RPC_URL, snrc.REGISTRIES, snrc.REGISTRARS, snrc.CONTROLLERS, snrc.NAME_POLICIES) snrc.RPC = self.node.url snrc.RPC_URL = urlparse(snrc.RPC) - snrc.REGISTRIES = {"testing": FakeChain.REGISTRY, "simplex": ""} + snrc.REGISTRIES = {"testing": FakeChain.REGISTRY} snrc.REGISTRARS = {"testing": FakeChain.REGISTRAR} snrc.CONTROLLERS = {"testing": FakeChain.CONTROLLER} snrc._multicall_failed_logged = False @@ -1486,7 +1486,7 @@ def test_a_controller_from_before_name_policies_keeps_the_reservations_and_is_lo self.assertEqual((record.getMessage(), record.fields["tld"], record.fields["reservations"]), ("no_name_policy", "testing", "controller")) def test_an_unconfigured_controller_is_not_read(self): - snrc.CONTROLLERS = {"simplex": ""} + snrc.CONTROLLERS = {"nosuchtld": ""} self.assertEqual(self.requests_made(snrc.name_policies), ({}, 0)) def test_an_address_without_a_contract_is_read_as_no_policy(self): @@ -1568,36 +1568,36 @@ def request(self, path, headers=None, level="INFO"): return record def test_a_request_is_logged_with_its_outcome(self): - record = self.request("/v2/resolve/x.simplex") + record = self.request("/v2/resolve/x.nosuchtld") fields = record.fields self.assertEqual(record.levelname, "INFO") self.assertEqual( (fields["client"], fields["method"], fields["path"], fields["status"], fields["worker"]), - ("127.0.0.1", "GET", "/v2/resolve/x.simplex", 400, os.getpid()), + ("127.0.0.1", "GET", "/v2/resolve/x.nosuchtld", 400, os.getpid()), ) self.assertGreater(fields["bytes"], 0) self.assertGreaterEqual(fields["ms"], 0) def test_a_trusted_proxy_names_the_client(self): snrc.TRUSTED_PROXIES = (ipaddress.ip_network("127.0.0.1/32"),) - record = self.request("/v2/resolve/x.simplex", {"X-Forwarded-For": "203.0.113.7"}) + record = self.request("/v2/resolve/x.nosuchtld", {"X-Forwarded-For": "203.0.113.7"}) self.assertEqual(record.fields["client"], "203.0.113.7") def test_any_other_peer_cannot_name_itself(self): - record = self.request("/v2/resolve/x.simplex", {"X-Forwarded-For": "203.0.113.7"}) + record = self.request("/v2/resolve/x.nosuchtld", {"X-Forwarded-For": "203.0.113.7"}) self.assertEqual(record.fields["client"], "127.0.0.1") def test_a_bad_request_is_not_logged_with_the_previous_ones_fields(self): """On a kept-alive connection the handler still held the last request's path and size.""" with self.assertLogs("snrc_resolve", "INFO") as logs: with socket.create_connection(self.server.server_address, timeout=5) as sock: - sock.sendall(b"GET /v2/resolve/x.simplex HTTP/1.1\r\nHost: x\r\n\r\n") + sock.sendall(b"GET /v2/resolve/x.nosuchtld HTTP/1.1\r\nHost: x\r\n\r\n") sock.recv(65536) sock.sendall(b"BOGUS\r\n\r\n") while sock.recv(65536): pass first, second = [r for r in logs.records if r.getMessage() == "request"] - self.assertEqual(first.fields["path"], "/v2/resolve/x.simplex") + self.assertEqual(first.fields["path"], "/v2/resolve/x.nosuchtld") self.assertEqual((second.fields["status"], second.fields["path"], second.fields["bytes"], second.fields["ms"]), (400, None, None, None)) def test_health_checks_are_logged_only_at_debug(self): @@ -1631,33 +1631,33 @@ def exchange(self, head: bytes, body: bytes = b"") -> bytes: break return data - SMUGGLED = b"GET /v2/resolve/y.simplex HTTP/1.1\r\nHost: x\r\n\r\n" + SMUGGLED = b"GET /v2/resolve/y.nosuchtld HTTP/1.1\r\nHost: x\r\n\r\n" def test_a_body_is_refused_and_the_connection_closed(self): - data = self.exchange(b"GET /v2/resolve/x.simplex HTTP/1.1\r\nHost: x\r\nContent-Length: %d\r\n\r\n" % len(self.SMUGGLED), self.SMUGGLED) + data = self.exchange(b"GET /v2/resolve/x.nosuchtld HTTP/1.1\r\nHost: x\r\nContent-Length: %d\r\n\r\n" % len(self.SMUGGLED), self.SMUGGLED) self.assertEqual(data.count(b"HTTP/1.1 "), 1) self.assertIn(b"HTTP/1.1 400 ", data) self.assertIn(b"Connection: close", data) - self.assertNotIn(b"y.simplex", data) + self.assertNotIn(b"y.nosuchtld", data) def test_a_length_after_a_malformed_header_line_is_still_seen(self): """Python stops parsing headers at a malformed line and keeps the rest as payload.""" - data = self.exchange(b"GET /v2/resolve/x.simplex HTTP/1.1\r\nX-Junk : 1\r\nContent-Length: %d\r\n\r\n" % len(self.SMUGGLED), self.SMUGGLED) + data = self.exchange(b"GET /v2/resolve/x.nosuchtld HTTP/1.1\r\nX-Junk : 1\r\nContent-Length: %d\r\n\r\n" % len(self.SMUGGLED), self.SMUGGLED) self.assertEqual(data.count(b"HTTP/1.1 "), 1) self.assertIn(b"unexpectedBody", data) - self.assertNotIn(b"y.simplex", data) + self.assertNotIn(b"y.nosuchtld", data) def test_a_chunked_body_is_refused_and_the_connection_closed(self): chunked = b"%x\r\n" % len(self.SMUGGLED) + self.SMUGGLED + b"\r\n0\r\n\r\n" - data = self.exchange(b"GET /v2/resolve/x.simplex HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\n", chunked) + data = self.exchange(b"GET /v2/resolve/x.nosuchtld HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\n", chunked) self.assertEqual(data.count(b"HTTP/1.1 "), 1) - self.assertNotIn(b"y.simplex", data) + self.assertNotIn(b"y.nosuchtld", data) def test_an_empty_body_keeps_the_connection(self): conn = http.client.HTTPConnection(*self.server.server_address, timeout=5) answers = [] for _ in range(2): - conn.request("GET", "/v2/resolve/x.simplex", headers={"Content-Length": "0"}) + conn.request("GET", "/v2/resolve/x.nosuchtld", headers={"Content-Length": "0"}) res = conn.getresponse() answers.append((res.status, json.loads(res.read())["error"], conn.sock)) conn.close() @@ -1765,7 +1765,7 @@ def test_requests_share_one_connection_without_delay(self): conn = http.client.HTTPConnection(*self.server.server_address, timeout=5) start = time.monotonic() for i in range(20): - conn.request("GET", "/v2/resolve/x.simplex") + conn.request("GET", "/v2/resolve/x.nosuchtld") res = conn.getresponse() res.read() self.assertEqual((res.version, res.status), (11, 400)) @@ -2013,3 +2013,11 @@ def test_unknown_settings_are_refused_at_start(self): snrc.setup_logging(self.out) finally: setattr(snrc, setting, saved) + + +class DefaultsTests(unittest.TestCase): + def test_every_tld_has_all_three_contracts_by_default(self): + fresh = importlib.util.module_from_spec(_SPEC) + _SPEC.loader.exec_module(fresh) + for table in (fresh.REGISTRIES, fresh.REGISTRARS, fresh.CONTROLLERS): + self.assertEqual(sorted(tld for tld, address in table.items() if address), ["simplex", "testing"])