drop resolver caching (#1866)

This commit is contained in:
brenzi authored and GitHub committed 2026-09-11 10:07:07 +01:00
1 parent c2bd7293db
commit 055bf721df
3 files changed
+1 -57

No files matched your search

-4
View File
@@ -310,10 +310,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.
The grace period, the oracle and its curve are cached for `CONSTANTS_TTL`
(5 minutes), so a retune shows up within that. Per-name values are read on
every query.
**Set `SNRC_CONTROLLER_<TLD>` wherever `SNRC_REGISTRAR_<TLD>` is.** Without a
controller there is no oracle, so no name can be priced.
+1 -23
View File
@@ -67,7 +67,6 @@ import hashlib
import json
import os
import sys
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import unquote, urlparse
from urllib.request import Request, urlopen
@@ -190,26 +189,9 @@ def chain_now() -> int:
return decode_uint(block["timestamp"])
# The grace period, the oracle and its curve change only when a contract is
# retuned, so they are read once per TTL rather than on every query. Per-name
# values are never cached.
CONSTANTS_TTL = 300
_constants: dict = {}
def cached(key, read):
"""`read()` at most once per CONSTANTS_TTL for `key`."""
hit = _constants.get(key)
if hit and time.time() - hit[0] < CONSTANTS_TTL:
return hit[1]
value = read()
_constants[key] = (time.time(), value)
return value
def grace_period(registrar: str) -> int:
"""A deployment can configure a different window, so it is read on chain."""
return cached(("grace", registrar), lambda: decode_uint(eth_call(registrar, selector("GRACE_PERIOD()"))))
return decode_uint(eth_call(registrar, selector("GRACE_PERIOD()")))
def expiry_status(expires: int, grace: int, now: int) -> str:
@@ -236,10 +218,6 @@ def reservation_reason(tld: str, token: int) -> int:
def pricing_params(tld: str):
"""What it costs to register a name under this TLD, in US cents, or None
when no controller or price oracle is configured."""
return cached(("pricing", tld), lambda: read_pricing_params(tld))
def read_pricing_params(tld: str):
controller = CONTROLLERS.get(tld)
if not controller:
return None
@@ -97,7 +97,6 @@ class EncodedLabelhashTests(unittest.TestCase):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": ""}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()
def tearDown(self):
snrc.REGISTRARS, snrc.CONTROLLERS, snrc.eth_call, snrc.chain_now = self._saved
@@ -214,7 +213,6 @@ class NameStatusTests(unittest.TestCase):
# Expiry alone; ReservedTests covers a configured controller.
snrc.CONTROLLERS = {"testing": ""}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()
def tearDown(self):
(
@@ -246,20 +244,6 @@ class NameStatusTests(unittest.TestCase):
with self.assertRaises(RuntimeError):
snrc.name_status("alice.testing")
def test_the_grace_period_is_read_once_not_per_query(self):
seen = []
def eth_call(to, data):
seen.append(data[:10])
if data.startswith(snrc.selector("GRACE_PERIOD()")):
return "0x" + snrc.encode_uint(self.GRACE)
return "0x" + snrc.encode_uint(int(time.time()) - 3600)
snrc.eth_call = eth_call
snrc.name_status("alice.testing")
snrc.name_status("alice.testing")
self.assertEqual(seen.count(snrc.selector("GRACE_PERIOD()")), 1)
def test_zero_expiry_means_never_registered(self):
snrc.eth_call = self._expiry(0)
self.assertEqual(
@@ -360,7 +344,6 @@ class ReservedTests(unittest.TestCase):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()
def tearDown(self):
snrc.REGISTRARS, snrc.CONTROLLERS, snrc.eth_call, snrc.chain_now = self._saved
@@ -432,7 +415,6 @@ class ReservedReasonTests(unittest.TestCase):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()
def tearDown(self):
(
@@ -555,7 +537,6 @@ class PricingTests(unittest.TestCase):
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
self.now = int(time.time())
snrc.chain_now = lambda: self.now
snrc._constants.clear()
def tearDown(self):
(
@@ -624,13 +605,6 @@ class PricingTests(unittest.TestCase):
self.assertEqual(snrc.name_status("acme.testing")["status"], "grace")
self.assertEqual(self.oracle_calls, [])
def test_the_oracle_curve_is_read_once_not_per_query(self):
snrc.eth_call = self._chain(self._lapsed(1))
snrc.name_status("acme.testing")
seen_first = len(self.oracle_calls)
snrc.name_status("acme.testing")
self.assertEqual(self.oracle_calls[seen_first:], [])
def test_a_reserved_lapsed_name_keeps_its_reservation(self):
snrc.eth_call = self._chain(self._lapsed(0), reserved=2)
reg = snrc.name_status("acme.testing")
@@ -671,7 +645,6 @@ class EnsOracleTests(unittest.TestCase):
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
self.now = int(time.time())
snrc.chain_now = lambda: self.now
snrc._constants.clear()
def tearDown(self):
(snrc.REGISTRIES, snrc.REGISTRARS, snrc.CONTROLLERS, snrc.eth_call, snrc.chain_now) = self._saved
@@ -732,7 +705,6 @@ class ErrorCodeTests(unittest.TestCase):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": ""}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()
def tearDown(self):
(
@@ -838,7 +810,6 @@ class RegistrationV2Tests(unittest.TestCase):
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
self.now = int(time.time())
snrc.chain_now = lambda: self.now
snrc._constants.clear()
def tearDown(self):
(
@@ -982,7 +953,6 @@ class RegistrationV2Tests(unittest.TestCase):
for expected_type, (chain, keys) in cases.items():
with self.subTest(type=expected_type):
snrc.eth_call = chain
snrc._constants.clear()
_, body = snrc.registration("acme.testing")
self.assertEqual(body["type"], expected_type)
self.assertEqual(set(body), keys)