repro_age_restricted_refresh_fails.py (3783B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """Reproducer: spending age-restricted coins leaves the change stuck. 15 16 Withdraws with an age restriction, spends part of it, and watches what the 17 exchange says about the refresh that follows. 18 19 cd /opt/taler-sim/driver 20 python3 ../findings/repro_age_restricted_refresh_fails.py [emulator-5554] 21 22 Needs a stack whose exchange offers age-restricted denominations (the 23 simulator's sim.conf does). Exits 0 when the exchange rejects the refresh. 24 """ 25 26 from __future__ import annotations 27 28 import sys 29 import time 30 from pathlib import Path 31 32 sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "driver")) 33 34 import oracles # noqa: E402 35 import scenarios # noqa: E402 36 from device import Device # noqa: E402 37 from stack import Bank, Env, Merchant # noqa: E402 38 from wallet import Wallet # noqa: E402 39 40 CYCLES = int(sys.argv[2]) if len(sys.argv) > 2 else 3 41 42 43 def spend_and_refund(app: Wallet, dev: Device, bank: Bank, merchant: Merchant, 44 env: Env) -> list[str]: 45 """Withdraw, spend part of it, take a refund — each step leaves change to 46 refresh, which is where the exchange rejects the melt.""" 47 app.fresh_start(dev_mode=True) 48 scenarios.withdraw(app, bank, "EUR:10") 49 app.home() 50 dev.logcat(clear=True) 51 52 order = merchant.create_order("EUR:2", summary="Partial spend") 53 app.handle_uri(order["pay_uri"]) 54 dev.tap_text(r"(?i)^Pay\b|confirm payment", timeout=60) 55 for _ in range(25): 56 if merchant.order_status(order["order_id"], 57 order["token"]).get("order_status") == "paid": 58 break 59 time.sleep(2) 60 # A refund puts the money back, which also has to be refreshed. 61 res = merchant.refund(order["order_id"], "EUR:1") 62 if res.get("taler_refund_uri"): 63 app.handle_uri(res["taler_refund_uri"]) 64 65 rejections: list[str] = [] 66 for _ in range(5): 67 time.sleep(15) 68 log = dev.logcat(lines=6000) 69 rejections = [ln for ln in log.splitlines() if '"code":1365' in ln] 70 if rejections: 71 break 72 return rejections 73 74 75 def main() -> int: 76 serial = sys.argv[1] if len(sys.argv) > 1 else "emulator-5554" 77 dev = Device(serial) 78 app = Wallet(dev) 79 env = Env.load() 80 bank, merchant = Bank(env.bank), Merchant(env.merchant) 81 82 for attempt in range(1, CYCLES + 1): 83 print(f"--- cycle {attempt} ---") 84 rejections = spend_and_refund(app, dev, bank, merchant, env) 85 stuck = [f for f in oracles.background_failures(dev) 86 if f.kind in ("stuck-operation", "background-error") 87 and "refresh" in f.detail] 88 print(f"age-commitment rejections: {len(rejections)}") 89 for finding in stuck: 90 print(f" {finding.detail[:140]}") 91 if rejections: 92 print(" " + rejections[0][-220:]) 93 print("\nreproduced: True") 94 return 0 95 print("\nreproduced: False (it is probabilistic: which member of a " 96 "denomination family the wallet picks decides it)") 97 return 1 98 99 100 if __name__ == "__main__": 101 raise SystemExit(main())