Fix duel double refund on expiry; idempotent weekly top-referrer prizes
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 03:19:14 +03:00
co-authored by Claude Opus 5.5
parent 07405e68f4
commit 97d125122c
2 changed files with 39 additions and 2 deletions
+2 -2
View File
@@ -5662,7 +5662,7 @@ async def econ_worker(bot):
for r in con.execute("SELECT id,challenger,units FROM duels WHERE status='expired' AND created>=? AND created<?", for r in con.execute("SELECT id,challenger,units FROM duels WHERE status='expired' AND created>=? AND created<?",
(now - 90000, now - 86400)).fetchall(): (now - 90000, now - 86400)).fetchall():
if _persona_kv("du_ref:%d" % r["id"]) != "1": if _persona_kv("du_ref:%d" % r["id"]) != "1":
econ_mint(r["challenger"], r["units"], "duel:expire") econ_mint(r["challenger"], r["units"], "duel:expire", ref="duel:%d:expire" % r["id"]) # dedupes vs duel_expire_worker
_persona_kv("du_ref:%d" % r["id"], "1") _persona_kv("du_ref:%d" % r["id"], "1")
# --- [ECON-V4] недельные квесты + ачивки (авто-выплата, недельный бюджет) # --- [ECON-V4] недельные квесты + ачивки (авто-выплата, недельный бюджет)
try: try:
@@ -12379,7 +12379,7 @@ async def toprefs_awarder(bot):
paid = [] paid = []
for i, rr in enumerate(rows): for i, rr in enumerate(rows):
prize = TOPREF_PRIZES[i] prize = TOPREF_PRIZES[i]
got = econ_mint(int(rr["uid"]), prize * 500000, "top3week:" + wk) got = econ_mint(int(rr["uid"]), prize * 500000, "top3week:" + wk, ref="%d" % int(rr["uid"]))
if got: if got:
paid.append((int(rr["uid"]), prize)) paid.append((int(rr["uid"]), prize))
try: try:
+37
View File
@@ -0,0 +1,37 @@
"""Every duel-expire refund must carry a ref, so econ_guard dedupes repeat refunds."""
import ast
from pathlib import Path
import unittest
SRC = Path(__file__).resolve().parents[1] / "bot" / "grok_bot.py"
def _calls(type_):
tree = ast.parse(SRC.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if (isinstance(node, ast.Call) and getattr(node.func, "id", None) in ("econ_mint", "econ_burn")
and len(node.args) >= 3 and isinstance(node.args[2], ast.Constant)
and node.args[2].value == type_):
yield node
class RefundRefTest(unittest.TestCase):
def test_duel_expire_refunds_are_idempotent(self):
calls = list(_calls("duel:expire"))
self.assertGreaterEqual(len(calls), 3)
for c in calls:
self.assertIn("ref", [k.arg for k in c.keywords], "line %d" % c.lineno)
def test_tax_burn_is_idempotent(self):
calls = list(_calls("tax"))
self.assertTrue(calls)
for c in calls:
self.assertIn("ref", [k.arg for k in c.keywords], "line %d" % c.lineno)
def test_weekly_top_referrer_prize_is_idempotent(self):
src = SRC.read_text(encoding="utf-8")
self.assertIn('"top3week:" + wk, ref=', src)
if __name__ == "__main__":
unittest.main()