stablecoin-games-renpy 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/package.json +1 -1
  2. package/plugin/game/python-packages/scfg_core/__init__.py +1 -1
  3. package/plugin/game/python-packages/scfg_core/amounts.py +57 -4
  4. package/plugin/game/python-packages/scfg_core/backends/base.py +3 -2
  5. package/plugin/game/python-packages/scfg_core/backends/evm.py +2 -2
  6. package/plugin/game/python-packages/scfg_core/backends/solana.py +15 -12
  7. package/plugin/game/python-packages/scfg_core/checkout.py +1 -0
  8. package/plugin/game/python-packages/scfg_core/coio.py +4 -3
  9. package/plugin/game/python-packages/scfg_core/config.py +86 -58
  10. package/plugin/game/python-packages/scfg_core/engine.py +15 -8
  11. package/plugin/game/python-packages/scfg_core/errors.py +19 -2
  12. package/plugin/game/python-packages/scfg_core/evm.py +10 -5
  13. package/plugin/game/python-packages/scfg_core/ints.py +50 -0
  14. package/plugin/game/python-packages/scfg_core/local_server.py +10 -4
  15. package/plugin/game/python-packages/scfg_core/overlay.py +13 -2
  16. package/plugin/game/python-packages/scfg_core/payments.py +10 -7
  17. package/plugin/game/python-packages/scfg_core/receipts.py +8 -8
  18. package/plugin/game/python-packages/scfg_core/restore.py +26 -5
  19. package/plugin/game/python-packages/scfg_core/reverify.py +72 -23
  20. package/plugin/game/python-packages/scfg_core/rpc.py +8 -6
  21. package/plugin/game/python-packages/scfg_core/sessions.py +2 -2
  22. package/plugin/game/python-packages/scfg_core/solana/base58.py +18 -0
  23. package/plugin/game/python-packages/scfg_core/solana/rpc.py +11 -8
  24. package/plugin/game/python-packages/scfg_core/watcher.py +2 -2
  25. package/plugin/game/scfg/scfg_api.rpy +38 -7
  26. package/plugin/game/scfg/web/app.js +35 -3
  27. package/plugin/game/scfg/web/bridge.js +70 -10
  28. package/plugin/game/scfg/web/evm.js +52 -14
  29. package/plugin/game/scfg/web/solana.js +60 -19
@@ -13,9 +13,7 @@ tests).
13
13
  from __future__ import annotations
14
14
 
15
15
  import copy
16
- import sys
17
16
  import time
18
- import traceback
19
17
  from dataclasses import dataclass, replace
20
18
  from decimal import Decimal
21
19
  from typing import Callable
@@ -26,7 +24,7 @@ from .backends.evm import START_BLOCK_MARGIN
26
24
  from .chains import Chain
27
25
  from .coio import run_sync
28
26
  from .config import Config, enabled_chains
29
- from .errors import ConfigError
27
+ from .errors import ConfigError, print_error
30
28
  from .models import PaymentResult
31
29
  from .payments import MIN_TIP, prepare_payment
32
30
  from .receipts import compute_ownership
@@ -54,8 +52,7 @@ class Snapshot:
54
52
  unready: frozenset = frozenset()
55
53
 
56
54
 
57
- def _print_error(exc: BaseException) -> None:
58
- traceback.print_exception(type(exc), exc, exc.__traceback__, file=sys.stderr)
55
+ _print_error = print_error
59
56
 
60
57
 
61
58
  class _LazyRpc:
@@ -113,7 +110,8 @@ class Engine:
113
110
  spec = prepare_payment(self.config, kind, item=item, amount=amount, token=token,
114
111
  title=title)
115
112
  if chain not in spec.chains:
116
- raise ConfigError(f"{spec.token} is not available on chain {chain!r}")
113
+ raise ConfigError(f"{spec.token} is not available on chain {chain!r}",
114
+ code="chain.token_unavailable")
117
115
  chain_obj = self.chain(chain)
118
116
  fields = yield from backend_for(chain_obj).start(
119
117
  rpc=self.rpc(chain), chain=chain_obj, wallet=self.config.wallet_for(chain_obj),
@@ -204,7 +202,7 @@ class Engine:
204
202
  chains = {k: self.chain(k) for k in self.config.registry}
205
203
  wallets = {f: self.config.accepted_wallets_for(f) for f in ("evm", "solana")}
206
204
  removed = yield from reverify_receipts(self.receipts, rpc_for=self.rpc, chains=chains,
207
- wallets=wallets)
205
+ wallets=wallets, on_error=self._report)
208
206
  yield from self._check_ready()
209
207
  return removed
210
208
 
@@ -231,7 +229,16 @@ class Engine:
231
229
  ordered = list(self.config.chains) + [k for k in registry if k not in self.config.chains]
232
230
  chains = {k: self.chain(k) for k in ordered}
233
231
  return (yield from restore_mod.lookup(tx_hash, config=self.config, chains=chains,
234
- rpc_for=self.rpc, receipts=self.receipts))
232
+ rpc_for=self.rpc, receipts=self.receipts,
233
+ on_error=self._report))
234
+
235
+ def _report(self, exc: BaseException) -> None:
236
+ """The on_error handed to reverify and restore_lookup, which report as they go: it calls
237
+ `on_error` as it is when the error is reported, not as it was when the pass started
238
+ (the Godot autoload replaces its handler at quit, with a pass possibly in flight). A
239
+ handler unset by then (None, or anything falsy) prints the error, as `on_error or
240
+ print_error` does."""
241
+ (self.on_error or _print_error)(exc)
235
242
 
236
243
  def restore_apply(self, candidates, choices: dict | None = None) -> list[dict]:
237
244
  return restore_mod.apply(self.receipts, candidates, choices)
@@ -1,4 +1,7 @@
1
- """Exception types raised to the developer."""
1
+ """Exception types raised to the developer, and the default report of a contained crash."""
2
+
3
+ import sys
4
+ import traceback
2
5
 
3
6
 
4
7
  class ScfgError(Exception):
@@ -6,4 +9,18 @@ class ScfgError(Exception):
6
9
 
7
10
 
8
11
  class ConfigError(ScfgError, ValueError):
9
- """Invalid configuration or programmer error. Raised immediately so it shows up in testing."""
12
+ """Invalid configuration or programmer error. Raised immediately so it shows up in testing.
13
+
14
+ `code` is a stable machine-readable identifier of the reason, `<subject>.<problem>`
15
+ (for example `chains.empty`). Conformance vectors compare it instead of the message.
16
+ """
17
+
18
+ def __init__(self, message: str, *, code: str):
19
+ super().__init__(message)
20
+ self.code = code
21
+
22
+
23
+ def print_error(exc: BaseException) -> None:
24
+ """The default `on_error`: a crash the core contained (one session's step, one stored
25
+ receipt, one restore candidate) is printed to stderr and the work goes on."""
26
+ traceback.print_exception(type(exc), exc, exc.__traceback__, file=sys.stderr)
@@ -8,14 +8,15 @@ from dataclasses import dataclass
8
8
  from .keccak import keccak256
9
9
  from .transfers import Transfer
10
10
 
11
- _HEX40 = re.compile(r"^0x[0-9a-fA-F]{40}$")
11
+ _HEX40 = re.compile(r"0x[0-9a-fA-F]{40}") # used with fullmatch: no trailing newline
12
+ _WORD = re.compile(r"0x[0-9a-fA-F]{64}") # one 32-byte ABI word, as logs carry a uint256
12
13
 
13
14
  TRANSFER_TOPIC = "0x" + keccak256(b"Transfer(address,address,uint256)").hex()
14
15
 
15
16
 
16
17
  def to_checksum_address(address: str) -> str:
17
18
  """Return the EIP-55 mixed-case form of a 20-byte hex address (any input case)."""
18
- if not isinstance(address, str) or not _HEX40.match(address):
19
+ if not isinstance(address, str) or not _HEX40.fullmatch(address):
19
20
  raise ValueError(f"not a 20-byte hex address: {address!r}")
20
21
  lower = address[2:].lower()
21
22
  digest = keccak256(lower.encode("ascii")).hex()
@@ -78,19 +79,23 @@ def parse_transfer_log(log: dict) -> TransferLog | None:
78
79
  """Parse a raw JSON-RPC log into a TransferLog, or None if it isn't an ERC-20 Transfer.
79
80
 
80
81
  ERC-721 Transfer events share topic0 but have 4 topics (the token id is indexed),
81
- so requiring exactly 3 topics filters them out.
82
+ so requiring exactly 3 topics filters them out. The data must be exactly the 32-byte
83
+ value ("0x" and 64 hex digits): an empty, shorter, longer or malformed one is not an
84
+ ERC-20 Transfer either.
82
85
  """
83
86
  topics = log.get("topics") or []
84
87
  if log.get("removed"):
85
88
  return None
86
89
  if len(topics) != 3 or topics[0].lower() != TRANSFER_TOPIC:
87
90
  return None
88
- data = log.get("data") or "0x"
91
+ data = log.get("data")
92
+ if not isinstance(data, str) or not _WORD.fullmatch(data):
93
+ return None
89
94
  return TransferLog(
90
95
  token=to_checksum_address(log["address"]),
91
96
  sender=_topic_to_address(topics[1]),
92
97
  recipient=_topic_to_address(topics[2]),
93
- value=int(data, 16) if data not in ("0x", "") else 0,
98
+ value=int(data, 16),
94
99
  tx_hash=log["transactionHash"].lower(),
95
100
  log_index=int(log["logIndex"], 16),
96
101
  block_number=int(log["blockNumber"], 16),
@@ -0,0 +1,50 @@
1
+ """Python's int() for text the core reads from storage, a page or an RPC, with CPython's limit on
2
+ the number of decimal digits enforced explicitly (independent of the interpreter version: the
3
+ limit arrived in CPython 3.11 and in the 3.10.7 / 3.9.14 security releases, and older embedded
4
+ interpreters, such as some Ren'Py builds, lack it). Converting a very long digit string costs
5
+ quadratic time; the TypeScript and GDScript cores apply the same limit (pystr.ts, pystr.gd).
6
+ Also: json_loads (an RPC answer's integer literals through int_from) and digit_run_value (a
7
+ number in a node's error message, at most 18 significant digits), Ruling 6."""
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+
13
+ # CPython's default sys.get_int_max_str_digits().
14
+ INT_MAX_STR_DIGITS = 4300
15
+ # A digit run read out of a node's error message (an EVM range limit, a Solana transaction
16
+ # version) is a number only up to this many significant digits (Ruling 6): every range or version
17
+ # a request can carry is below 10**18, and the TypeScript and GDScript cores read the same.
18
+ DIGIT_RUN_LIMIT = 18
19
+
20
+
21
+ def int_from(value) -> int:
22
+ """int(value), refusing a str with more than INT_MAX_STR_DIGITS decimal digits as CPython
23
+ does (ValueError, with CPython's message) before any conversion."""
24
+ if isinstance(value, str) and len(value) > INT_MAX_STR_DIGITS:
25
+ digits = sum(1 for ch in value if ch.isdecimal())
26
+ if digits > INT_MAX_STR_DIGITS:
27
+ raise ValueError(f"Exceeds the limit ({INT_MAX_STR_DIGITS} digits) for integer string "
28
+ f"conversion: value has {digits} digits; use "
29
+ "sys.set_int_max_str_digits() to increase the limit")
30
+ return int(value)
31
+
32
+
33
+ def digit_run_value(run: str) -> int | None:
34
+ """int(run) for a run of Unicode decimal digits (what re's \\d+ matches), or None when it has
35
+ more than DIGIT_RUN_LIMIT significant digits. Linear: leading zeros (of any script) are
36
+ stripped first, so no long text is converted."""
37
+ if len(run) > DIGIT_RUN_LIMIT:
38
+ # every decimal digit is its script's zero plus its value (Nd digits come in runs of ten)
39
+ run = run.lstrip("".join({chr(ord(c) - int(c)) for c in set(run)}))
40
+ if len(run) > DIGIT_RUN_LIMIT:
41
+ return None
42
+ return int(run) if run else 0
43
+
44
+
45
+ def json_loads(text):
46
+ """json.loads(text), its integer literals read with int_from: one of more than
47
+ INT_MAX_STR_DIGITS digits is a ValueError on every interpreter (CPython 3.9.25 and 3.11+
48
+ refuse it themselves; an interpreter without the limit would convert it in quadratic
49
+ time). Fractions and exponents are floats, as json makes them."""
50
+ return json.loads(text, parse_int=int_from)
@@ -25,6 +25,7 @@ from collections.abc import Callable
25
25
  from concurrent.futures import Future
26
26
  from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
27
27
 
28
+ from .ints import int_from
28
29
  from .page_messages import BUILD_TIMEOUT_S, parse_build_message, parse_tx_message
29
30
 
30
31
  MAX_BODY = 4096
@@ -87,13 +88,18 @@ def _make_handler(server: CheckoutServer):
87
88
  def log_message(self, format, *args): # keep the game's console quiet
88
89
  pass
89
90
 
91
+ def end_headers(self):
92
+ # Every answer carries these, also the ones http.server makes itself with
93
+ # send_error (501 for other methods, 400 for a bad request line, ...).
94
+ self.send_header("Cache-Control", "no-store")
95
+ self.send_header("Referrer-Policy", "no-referrer")
96
+ self.send_header("X-Content-Type-Options", "nosniff")
97
+ super().end_headers()
98
+
90
99
  def _send(self, code: int, body: bytes = b"", ctype: str = "text/plain; charset=utf-8"):
91
100
  self.send_response(code)
92
101
  self.send_header("Content-Type", ctype)
93
102
  self.send_header("Content-Length", str(len(body)))
94
- self.send_header("Cache-Control", "no-store")
95
- self.send_header("Referrer-Policy", "no-referrer")
96
- self.send_header("X-Content-Type-Options", "nosniff")
97
103
  self.end_headers()
98
104
  if self.command != "HEAD":
99
105
  self.wfile.write(body)
@@ -146,7 +152,7 @@ def _make_handler(server: CheckoutServer):
146
152
  def _read_json(self) -> dict | None:
147
153
  """The request's JSON object, or None after answering 400/413."""
148
154
  try:
149
- length = int(self.headers.get("Content-Length", "0"))
155
+ length = int_from(self.headers.get("Content-Length", "0"))
150
156
  except ValueError:
151
157
  length = -1
152
158
  if not 0 < length <= MAX_BODY:
@@ -5,7 +5,10 @@ page, so the checkout page (app.js, evm.js, solana.js) is shown over the game in
5
5
  being served on localhost. OverlayCheckout has local_server.CheckoutServer's interface,
6
6
  so CheckoutController drives both alike. The page's messages are read by pump(), which
7
7
  the controller calls every frame; they are validated like the local server's requests,
8
- and a tx hash stays a hint the watcher verifies on-chain.
8
+ and a tx hash stays a hint the watcher verifies on-chain. Once stop() ran (also from inside
9
+ one of the controller's callbacks) no callback is called again, not for the rest of that
10
+ batch of messages either, and a build handed back by a build_tx that stopped the overlay
11
+ is cancelled at once.
9
12
  """
10
13
 
11
14
  from __future__ import annotations
@@ -63,6 +66,8 @@ class OverlayCheckout:
63
66
  return
64
67
  for message in self.ui.take(self.key):
65
68
  self._handle(message)
69
+ if not self._open:
70
+ return # a callback stopped the overlay: the rest of the batch is moot
66
71
  self._finish_builds()
67
72
  self._push_status()
68
73
 
@@ -88,7 +93,13 @@ class OverlayCheckout:
88
93
  except ValueError:
89
94
  self.ui.reply(self.key, rid, 400, {"error": "bad account"})
90
95
  return
91
- self._builds[rid] = (self.build_tx(account), self.clock() + BUILD_TIMEOUT_S)
96
+ fut = self.build_tx(account)
97
+ if not self._open:
98
+ # build_tx stopped the overlay (its controller closed): nobody answers this
99
+ # build, so it must not run
100
+ fut.cancel()
101
+ return
102
+ self._builds[rid] = (fut, self.clock() + BUILD_TIMEOUT_S)
92
103
 
93
104
  def _finish_builds(self) -> None:
94
105
  for rid, (fut, deadline) in list(self._builds.items()):
@@ -32,26 +32,29 @@ def prepare_payment(config: Config, kind: str, *, item: str | None = None, amoun
32
32
  token: str | None = None, title: str | None = None) -> PaymentSpec:
33
33
  if kind == "unlock":
34
34
  if item not in config.items:
35
- raise ConfigError(f"unknown item {item!r}; declare it with scfg.add_item()")
35
+ raise ConfigError(f"unknown item {item!r}; declare it with scfg.add_item()",
36
+ code="item.unknown")
36
37
  it = config.items[item]
37
38
  price = it.price if amount is None else parse_amount(amount)
38
39
  if price < it.min_price:
39
- raise ConfigError(f"amount {price} is below the item's min_price {it.min_price}")
40
+ raise ConfigError(f"amount {price} is below the item's min_price {it.min_price}",
41
+ code="payment.below_min_price")
40
42
  token = token or it.token
41
43
  code, title = it.code, title or it.title
42
44
  elif kind == "tip":
43
45
  if amount is None or token is None:
44
- raise ConfigError("tips need an amount and a token")
46
+ raise ConfigError("tips need an amount and a token", code="tip.needs_amount_and_token")
45
47
  price = parse_amount(amount)
46
48
  if price < MIN_TIP:
47
- raise ConfigError(f"tips must be at least {MIN_TIP}")
49
+ raise ConfigError(f"tips must be at least {MIN_TIP}", code="tip.below_minimum")
48
50
  code, title = 0, title or "Tip"
49
51
  else:
50
- raise ConfigError(f"kind must be 'unlock' or 'tip', not {kind!r}")
52
+ raise ConfigError(f"kind must be 'unlock' or 'tip', not {kind!r}", code="payment.bad_kind")
51
53
  if token not in config.tokens:
52
- raise ConfigError(f"token {token!r} is not enabled")
54
+ raise ConfigError(f"token {token!r} is not enabled", code="token.not_enabled")
53
55
  chains = tuple(chains_for_token(config, token))
54
56
  if not chains:
55
- raise ConfigError(f"{token} is not available on any enabled chain")
57
+ raise ConfigError(f"{token} is not available on any enabled chain",
58
+ code="token.unavailable")
56
59
  return PaymentSpec(kind=kind, item=item, price=price, token=token, code=code,
57
60
  title=title, chains=chains)
@@ -7,9 +7,9 @@ receipts, and receipts are re-checked against the chain at every launch.
7
7
  from __future__ import annotations
8
8
 
9
9
  from collections.abc import MutableMapping
10
- from decimal import Decimal, InvalidOperation
10
+ from decimal import Decimal
11
11
 
12
- from .amounts import units_to_decimal
12
+ from .amounts import stored_amount, units_to_decimal
13
13
  from .evm import TransferLog
14
14
  from .models import Item
15
15
  from .transfers import Transfer
@@ -48,19 +48,19 @@ def remove_receipt(store: MutableMapping, key: str) -> None:
48
48
 
49
49
 
50
50
  def compute_ownership(store: MutableMapping, items: dict[str, Item]) -> tuple[set[str], Decimal]:
51
- """(owned item ids, total tipped). Malformed entries are skipped, never fatal."""
51
+ """(owned item ids, total tipped). Malformed entries are skipped, never fatal: a missing
52
+ field, an amount that is not a number within the stored-amount range (amounts.stored_amount,
53
+ so the tip total stays finite), an unhashable item."""
52
54
  owned: set[str] = set()
53
55
  tips = Decimal(0)
54
56
  for r in store.values():
55
57
  try:
56
58
  kind = r["kind"]
57
- amount = Decimal(r["amount"])
58
- except (KeyError, TypeError, InvalidOperation):
59
- continue
60
- if not amount.is_finite():
59
+ amount = stored_amount(r["amount"])
60
+ item = items.get(r.get("item")) if kind == "unlock" else None
61
+ except (KeyError, TypeError, ValueError, AttributeError):
61
62
  continue
62
63
  if kind == "unlock":
63
- item = items.get(r.get("item"))
64
64
  if item is not None and amount >= item.min_price:
65
65
  owned.add(item.item_id)
66
66
  elif kind == "tip":
@@ -11,12 +11,14 @@ from __future__ import annotations
11
11
 
12
12
  from collections.abc import MutableMapping
13
13
  from dataclasses import dataclass
14
+ from decimal import InvalidOperation
14
15
  from typing import Callable
15
16
 
16
17
  from .amounts import decode, units_to_decimal
17
18
  from .backends import backend_for
18
19
  from .chains import Chain
19
20
  from .config import Config
21
+ from .errors import print_error
20
22
  from .evm import TransferLog
21
23
  from .receipts import add_receipt, make_receipt, receipt_key
22
24
  from .rpc import RpcError, RpcUnavailable
@@ -61,8 +63,21 @@ def classify(config: Config, value: int) -> tuple[str | None, str | None, tuple[
61
63
  return None, None, affordable + (TIP_CHOICE,)
62
64
 
63
65
 
64
- def lookup_evm(tx_hash: str, *, config: Config, chain: Chain, rpc,
65
- receipts: MutableMapping) -> list[RestoreCandidate] | None:
66
+ def classify_or_skip(config: Config, value: int,
67
+ on_error: Callable[[BaseException], None] | None):
68
+ """classify(), or None for a value decode() cannot price (10**32 units or more): such a
69
+ transfer is skipped and reported to `on_error` (by default printed), the rest of the
70
+ transaction is still offered."""
71
+ try:
72
+ return classify(config, value)
73
+ except InvalidOperation as e:
74
+ (on_error or print_error)(e)
75
+ return None
76
+
77
+
78
+ def lookup_evm(tx_hash: str, *, config: Config, chain: Chain, rpc, receipts: MutableMapping,
79
+ on_error: Callable[[BaseException], None] | None = None,
80
+ ) -> list[RestoreCandidate] | None:
66
81
  """Candidates for a normalized tx hash on one EVM chain; None if the chain lacks it."""
67
82
  receipt = yield from rpc.get_transaction_receipt(tx_hash)
68
83
  if receipt is None:
@@ -76,7 +91,10 @@ def lookup_evm(tx_hash: str, *, config: Config, chain: Chain, rpc,
76
91
  if (t is None or t.token not in by_address or t.value <= 0
77
92
  or t.recipient not in config.accepted_wallets):
78
93
  continue
79
- kind, item, choices = classify(config, t.value)
94
+ classified = classify_or_skip(config, t.value, on_error)
95
+ if classified is None:
96
+ continue
97
+ kind, item, choices = classified
80
98
  out.append(RestoreCandidate(
81
99
  chain=chain.key, token=by_address[t.token], transfer=t, kind=kind, item=item,
82
100
  choices=choices,
@@ -87,7 +105,10 @@ def lookup_evm(tx_hash: str, *, config: Config, chain: Chain, rpc,
87
105
 
88
106
 
89
107
  def lookup(tx_hash: str, *, config: Config, chains: dict[str, Chain],
90
- rpc_for: Callable[[str], object], receipts: MutableMapping) -> list[RestoreCandidate]:
108
+ rpc_for: Callable[[str], object], receipts: MutableMapping,
109
+ on_error: Callable[[BaseException], None] | None = None) -> list[RestoreCandidate]:
110
+ """The candidates of the first chain that knows `tx_hash`. A transfer that cannot be
111
+ classified is skipped and reported to `on_error` (see classify_or_skip)."""
91
112
  network_trouble = False
92
113
  understood = False
93
114
  for key, chain in chains.items():
@@ -99,7 +120,7 @@ def lookup(tx_hash: str, *, config: Config, chains: dict[str, Chain],
99
120
  understood = True
100
121
  try:
101
122
  out = yield from backend.lookup(tx, config=config, chain=chain, rpc=rpc_for(key),
102
- receipts=receipts)
123
+ receipts=receipts, on_error=on_error)
103
124
  except (RpcUnavailable, RpcError):
104
125
  network_trouble = True
105
126
  continue
@@ -14,13 +14,14 @@ Only a *definitive* mismatch removes a receipt; network trouble never does.
14
14
  from __future__ import annotations
15
15
 
16
16
  from collections.abc import Mapping, MutableMapping, Sequence
17
- from decimal import Decimal, InvalidOperation
18
17
  from typing import Callable
19
18
 
20
- from .amounts import decimal_to_units
19
+ from .amounts import decimal_to_units, stored_amount
21
20
  from .backends import backend_for
22
21
  from .chains import Chain
22
+ from .errors import print_error
23
23
  from .evm import TRANSFER_TOPIC, address_topic, parse_transfer_log
24
+ from .ints import int_from
24
25
  from .rpc import RpcError, RpcUnavailable
25
26
 
26
27
  FUTURE_TOLERANCE_BLOCKS = 10_000
@@ -28,49 +29,97 @@ FUTURE_TOLERANCE_BLOCKS = 10_000
28
29
 
29
30
  def reverify_receipts(receipts: MutableMapping, *, rpc_for: Callable[[str], object],
30
31
  chains: dict[str, Chain],
31
- wallets: Sequence[str] | Mapping[str, Sequence[str]]) -> list[str]:
32
+ wallets: Sequence[str] | Mapping[str, Sequence[str]],
33
+ on_error: Callable[[BaseException], None] | None = None) -> list[str]:
32
34
  """Remove receipts that the chain contradicts. Returns the removed keys.
33
35
 
34
36
  `wallets` is either a sequence (the developer's wallets, for every family) or a mapping
35
37
  family -> sequence. A family with no wallets keeps its receipts: they cannot be judged.
38
+
39
+ The removed keys come back in the store's iteration order (the order receipts were
40
+ stored in, as loaded), which is deterministic, so every port reports them alike.
41
+
42
+ A stored entry whose own fields are invalid is removed, and the reason is reported to
43
+ `on_error` (by default printed): a forged receipt cannot escape verification by making its
44
+ check fail. Invalid fields, checked before any RPC: a missing field; a chain, token or item
45
+ that cannot be a key; a tx hash that is not a str; a log index or block that is not an
46
+ int >= 0 as ints.int_from reads it (Python's int(), and at most 4300 digits on every
47
+ interpreter); an amount that is not a number of base units within the range of
48
+ amounts.stored_amount; and, on a chain this build knows, a tx id that is not
49
+ its family's (an EVM hash: 0x and 64 hex digits; a Solana signature: 64 base58 bytes, at
50
+ most 88 characters) or has surrounding whitespace. A receipt naming a chain this build does
51
+ not know (a removed custom chain, the other network's) is kept, its family fields
52
+ unchecked: a configuration change must not erase purchases.
53
+
54
+ A check that fails because of what a node returned is not the receipt's fault: that
55
+ receipt is kept as unverifiable, the failure is reported, and the receipts after it are
56
+ still checked.
36
57
  """
37
58
  removed: list[str] = []
59
+ report = on_error or print_error
38
60
 
39
61
  def drop(key: str) -> None:
40
62
  receipts.pop(key, None)
41
63
  removed.append(key)
42
64
 
43
65
  for key, r in list(receipts.items()):
44
- try:
45
- chain = chains.get(r["chain"])
46
- # Malformed receipts are dropped here; the backend then reads the same fields.
47
- r["tx_hash"].lower(), int(r["log_index"]), int(r["block"])
48
- decimal_to_units(Decimal(r["amount"]))
49
- token_name = r["token"]
50
- except (KeyError, TypeError, ValueError, AttributeError, InvalidOperation):
66
+ problem = _invalid_fields(r, chains)
67
+ if problem is not None:
51
68
  drop(key)
69
+ report(ValueError(f"stored receipt {key!r} is invalid and was removed: {problem!r}"))
52
70
  continue
53
- if chain is None:
54
- continue # chain unknown to this build: cannot judge, keep
55
- token = chain.tokens.get(token_name)
56
- if token is None:
57
- drop(key)
71
+ try:
72
+ verdict = yield from _judge(r, rpc_for=rpc_for, chains=chains, wallets=wallets)
73
+ except Exception as e:
74
+ report(e) # node data the check could not read: unverifiable, kept
58
75
  continue
59
- family_wallets = (wallets.get(chain.family, ()) if isinstance(wallets, Mapping)
60
- else wallets)
61
- if not family_wallets:
62
- continue # no wallet of this family configured any more: cannot judge, keep
63
- verdict = yield from backend_for(chain).reverify(
64
- r, rpc=rpc_for(chain.key), chain=chain, wallets=tuple(family_wallets))
65
76
  if verdict == "drop":
66
77
  drop(key)
67
78
  return removed
68
79
 
69
80
 
81
+ def _invalid_fields(r, chains: dict[str, Chain]) -> BaseException | None:
82
+ """Why the stored entry `r` is not a receipt the checks can read, or None. The backends
83
+ then read the same fields. On a chain this build knows, the tx id must also be one of its
84
+ family's (an EVM hash: 0x and 64 hex digits; a Solana signature: 64 base58 bytes), as
85
+ stored, with no surrounding whitespace."""
86
+ try:
87
+ hash(r["chain"]), hash(r["token"]), hash(r.get("item"))
88
+ tx = r["tx_hash"]
89
+ tx.lower()
90
+ if int_from(r["log_index"]) < 0 or int_from(r["block"]) < 0:
91
+ raise ValueError("log index and block must be >= 0")
92
+ decimal_to_units(stored_amount(r["amount"]))
93
+ chain = chains.get(r["chain"])
94
+ if chain is not None:
95
+ if tx != tx.strip():
96
+ raise ValueError(f"tx id with surrounding whitespace: {tx!r}")
97
+ backend_for(chain).normalize_tx(tx)
98
+ except (KeyError, TypeError, ValueError, AttributeError, ArithmeticError) as e:
99
+ return e
100
+ return None
101
+
102
+
103
+ def _judge(r: dict, *, rpc_for, chains, wallets):
104
+ """A well-formed receipt's verdict: "drop" or "keep"."""
105
+ chain = chains.get(r["chain"])
106
+ if chain is None:
107
+ return "keep" # chain unknown to this build: cannot judge, keep
108
+ if chain.tokens.get(r["token"]) is None:
109
+ return "drop"
110
+ family_wallets = (wallets.get(chain.family, ()) if isinstance(wallets, Mapping)
111
+ else wallets)
112
+ if not family_wallets:
113
+ return "keep" # no wallet of this family configured any more: cannot judge, keep
114
+ return (yield from backend_for(chain).reverify(
115
+ r, rpc=rpc_for(chain.key), chain=chain, wallets=tuple(family_wallets)))
116
+
117
+
70
118
  def check_evm_receipt(r: dict, *, rpc, chain: Chain, wallets: Sequence[str]) -> str:
71
119
  """Judge one stored EVM receipt: "drop" if the chain contradicts it, else "keep"."""
72
- tx_hash, log_index, block = r["tx_hash"].lower(), int(r["log_index"]), int(r["block"])
73
- units = decimal_to_units(Decimal(r["amount"]))
120
+ tx_hash, log_index, block = (r["tx_hash"].lower(), int_from(r["log_index"]),
121
+ int_from(r["block"]))
122
+ units = decimal_to_units(stored_amount(r["amount"]))
74
123
  token = chain.tokens[r["token"]]
75
124
  topics = [TRANSFER_TOPIC, None, [address_topic(w) for w in wallets]]
76
125
 
@@ -23,6 +23,7 @@ from collections.abc import Sequence
23
23
  from typing import TYPE_CHECKING, Callable
24
24
 
25
25
  from .coio import Request, TransportError # noqa: F401 (re-exported)
26
+ from .ints import digit_run_value, int_from, json_loads
26
27
 
27
28
  if TYPE_CHECKING:
28
29
  import ssl
@@ -60,10 +61,11 @@ def suggested_range(err: RpcError, tried: int) -> int:
60
61
  """Next eth_getLogs range to try after `err` rejected a `tried`-block request.
61
62
 
62
63
  Uses the limit when the provider states one ("limited to 0 - 50 blocks range"),
63
- otherwise halves.
64
+ otherwise halves. A number of more than 18 significant digits is ignored (Ruling 6: it is
65
+ above any range, and converting a long digit run would cost quadratic time).
64
66
  """
65
- stated = [int(n) for n in _NUMBER.findall(err.message)]
66
- candidates = [n for n in stated if MIN_LOG_RANGE <= n < tried]
67
+ stated = [digit_run_value(n) for n in _NUMBER.findall(err.message)]
68
+ candidates = [n for n in stated if n is not None and MIN_LOG_RANGE <= n < tried]
67
69
  return max(candidates) if candidates else max(MIN_LOG_RANGE, tried // 2)
68
70
 
69
71
 
@@ -75,7 +77,7 @@ def _parse_error(e) -> RpcError:
75
77
  else:
76
78
  code, message = 0, e
77
79
  try:
78
- code = int(code)
80
+ code = int_from(code)
79
81
  except (TypeError, ValueError):
80
82
  code = 0
81
83
  return RpcError(code, str(message))
@@ -117,7 +119,7 @@ class UrllibTransport:
117
119
  # Some providers send JSON-RPC errors (e.g. "range too large") with a 4xx
118
120
  # status. Pass those through so the client can react to the message.
119
121
  try:
120
- body = json.loads(e.read().decode("utf-8"))
122
+ body = json_loads(e.read().decode("utf-8"))
121
123
  except Exception:
122
124
  body = None
123
125
  if isinstance(body, dict) and body.get("error"):
@@ -126,7 +128,7 @@ class UrllibTransport:
126
128
  except (urllib.error.URLError, OSError) as e:
127
129
  raise TransportError(str(e)) from None
128
130
  try:
129
- return json.loads(raw.decode("utf-8"))
131
+ return json_loads(raw.decode("utf-8"))
130
132
  except ValueError:
131
133
  raise TransportError("invalid JSON response") from None
132
134
 
@@ -12,7 +12,7 @@ import secrets
12
12
  from collections.abc import MutableMapping
13
13
  from decimal import Decimal
14
14
 
15
- from .amounts import encode, parse_amount
15
+ from .amounts import encode, parse_amount, stored_amount
16
16
 
17
17
  SESSION_WINDOW_S = 30 * 60
18
18
  SCAN_HORIZON_S = 24 * 3600
@@ -94,4 +94,4 @@ def is_past_horizon(rec: dict, now: float) -> bool:
94
94
 
95
95
 
96
96
  def session_price(rec: dict) -> Decimal:
97
- return Decimal(rec["price"])
97
+ return stored_amount(rec["price"])
@@ -2,6 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import math
6
+
5
7
  ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
6
8
  _INDEX = {c: i for i, c in enumerate(ALPHABET)}
7
9
 
@@ -29,7 +31,23 @@ def b58decode(text: str) -> bytes:
29
31
  return b"\0" * zeros + body
30
32
 
31
33
 
34
+ def max_encoded_length(length: int) -> int:
35
+ """The longest base58 text of `length` bytes: 44 characters for 32, 88 for 64."""
36
+ return math.ceil(length * math.log(256) / math.log(58))
37
+
38
+
32
39
  def decode_fixed(text: str, length: int) -> bytes:
40
+ """`length` bytes from base58 `text`. Decoding is quadratic in the text's length, so text
41
+ longer than any encoding of `length` bytes is refused before it is decoded. Bad input is a
42
+ ValueError, a `length` that is not an int (a bool, a float, a str, ...) included."""
43
+ if isinstance(length, bool) or not isinstance(length, int):
44
+ raise ValueError(f"not a byte count: {length!r}")
45
+ try:
46
+ limit = max_encoded_length(length)
47
+ except OverflowError: # beyond a float: no text is that long
48
+ raise ValueError(f"not a byte count: {length!r}") from None
49
+ if isinstance(text, str) and len(text) > limit:
50
+ raise ValueError(f"too long for {length} bytes: {len(text)} characters")
33
51
  raw = b58decode(text)
34
52
  if len(raw) != length:
35
53
  raise ValueError(f"expected {length} bytes, got {len(raw)}: {text!r}")