Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions scripts/resolver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
24 changes: 1 addition & 23 deletions scripts/resolver/service/snrc-resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
30 changes: 0 additions & 30 deletions scripts/resolver/service/test_snrc_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def setUp(self):
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
Expand Down Expand Up @@ -214,7 +213,6 @@ def setUp(self):
# Expiry alone; ReservedTests covers a configured controller.
snrc.CONTROLLERS = {"testing": ""}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()

def tearDown(self):
(
Expand Down Expand Up @@ -246,20 +244,6 @@ def test_a_registrar_that_is_not_a_contract_is_an_error_not_a_free_name(self):
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(
Expand Down Expand Up @@ -360,7 +344,6 @@ def setUp(self):
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
Expand Down Expand Up @@ -432,7 +415,6 @@ def setUp(self):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()

def tearDown(self):
(
Expand Down Expand Up @@ -555,7 +537,6 @@ def setUp(self):
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
self.now = int(time.time())
snrc.chain_now = lambda: self.now
snrc._constants.clear()

def tearDown(self):
(
Expand Down Expand Up @@ -624,13 +605,6 @@ def test_a_name_in_grace_never_reaches_the_oracle(self):
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")
Expand Down Expand Up @@ -671,7 +645,6 @@ def setUp(self):
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
Expand Down Expand Up @@ -732,7 +705,6 @@ def setUp(self):
snrc.REGISTRARS = {"testing": self.REGISTRAR}
snrc.CONTROLLERS = {"testing": ""}
snrc.chain_now = lambda: int(time.time())
snrc._constants.clear()

def tearDown(self):
(
Expand Down Expand Up @@ -838,7 +810,6 @@ def setUp(self):
snrc.CONTROLLERS = {"testing": self.CONTROLLER}
self.now = int(time.time())
snrc.chain_now = lambda: self.now
snrc._constants.clear()

def tearDown(self):
(
Expand Down Expand Up @@ -982,7 +953,6 @@ def test_each_answer_carries_exactly_its_own_fields(self):
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)
Expand Down
Loading