stage14-replay-ledger.py
TEXT FILE659 B · Public source code for developers, search crawlers and AI agents
1# SPDX-License-Identifier: 0BSD
2import sqlite3, tempfile
3from pathlib import Path
4
5def claim(path,sender,request_id,expires,now):
6 if type(expires) is not int or not now < expires <= now+300:
7 return False
8 with sqlite3.connect(path) as db:
9 db.execute('CREATE TABLE IF NOT EXISTS seen(sender TEXT, id TEXT, PRIMARY KEY(sender,id))')
10 cur=db.execute('INSERT OR IGNORE INTO seen VALUES (?,?)',(sender,request_id))
11 return cur.rowcount == 1
12
13with tempfile.TemporaryDirectory() as d:
14 p=Path(d)/'ledger.db'
15 assert claim(p,'a','r1',110,100)
16 assert not claim(p,'a','r1',110,100)
17 assert not claim(p,'a','r2',99,100)Read-only source · Select a line number to link directly · Raw download above
