"""Read the saved synthetic history; no node/RPC/chain observation or safety threshold.""" import hashlib import json import sys from pathlib import Path def recount(data): if not isinstance(data, dict): raise ValueError('The input must be the saved exercise object.') history = data.get('history') if not isinstance(history, list) or not history: raise ValueError('history must be a non-empty list.') seen = set() results = [] for row in history: if not isinstance(row, dict): raise ValueError('Each history row must be an object.') step = row.get('step') if not isinstance(step, str) or not step.strip() or step in seen: raise ValueError('Each step must have a non-empty unique label.') seen.add(step) if 'included' not in row or 'confirmations' not in row: raise ValueError('An explicit inclusion state and expected result are required.') included = row['included'] if included is not None and type(included) is not bool: raise ValueError('included must be true, false, or null; 0 is not false.') tip = row.get('tip') if type(tip) is not int or tip < 0: raise ValueError('tip must be a non-negative integer.') if included is True: height = row.get('height') if type(height) is not int or not 0 <= height <= tip: raise ValueError('Included height must be an integer from 0 through tip.') block = row.get('block') if not isinstance(block, str) or not block.strip(): raise ValueError('Included rows require their saved block label.') calculated = tip - height + 1 else: if row.get('height') is not None or row.get('block') is not None: raise ValueError('Absent/unknown rows must not retain an old inclusion height or block.') calculated = 0 if included is False else None expected = row['confirmations'] if expected is not None and (type(expected) is not int or expected < 0): raise ValueError('Expected confirmations must be a non-negative integer or null.') if calculated != expected: raise ValueError(f'{step}: calculated {calculated!r}, stored expected {expected!r}.') results.append({'step': step, 'included': included, 'tip': tip, 'height': row.get('height'), 'block': row.get('block'), 'calculated_confirmations': calculated}) return results def main(): if len(sys.argv) != 2: raise ValueError('Usage: python3 recount-confirmation-history.py.txt review-exercises-20261019.txt') raw = Path(sys.argv[1]).read_bytes() data = json.loads(raw) rows = recount(data) print(json.dumps({'kind': 'synthetic_history_recalculation_not_chain_observation', 'source_created': data.get('created'), 'source_sha256': hashlib.sha256(raw).hexdigest(), 'rows': rows}, ensure_ascii=False, indent=2)) if __name__ == '__main__': try: main() except (ValueError, TypeError, OSError) as error: print('ERROR: ' + str(error), file=sys.stderr) sys.exit(2)