#!/usr/bin/env python3 """Independently verify the published hypothetical examples; no network access.""" import hashlib import json import sys from fractions import Fraction from pathlib import Path data=json.loads(Path(sys.argv[1]).read_text()) assert data['is_hypothetical'] book=data['orderbook'] for name,case in book['cases'].items(): filled=sum(int(row['quantity_tenths']) for row in case['fills']) cost=sum(row['price_won']*int(row['quantity_tenths']) for row in case['fills']) assert filled==int(case['filled_tenths']) assert cost==int(case['cost_tenth_won']) assert filled+int(case['unfilled_tenths'])==int(case['requested_tenths']) assert (str(Fraction(cost,filled)) if filled else None)==(str(Fraction(case['average_price_fraction'])) if case['average_price_fraction'] else None) print(name,'filled=',str(Fraction(filled,10)),'cost_won=',str(Fraction(cost,10))) assert int(book['cases']['buy_1_5']['cost_tenth_won'])==1503700 assert book['cases']['empty_book']['average_price_fraction'] is None market=data['market_cap'] before=int(market['before_price_won'])*int(market['circulating_supply']) after=int(market['after_price_won'])*int(market['circulating_supply']) assert before==int(market['before_market_cap_won']) and after==int(market['after_market_cap_won']) assert after-before==int(market['valuation_difference_won']) chain=data['hash_chain'];h=lambda text:hashlib.sha256(text.encode('utf-8')).hexdigest() for raw,key in [('first','original_hash'),('changed','changed_hash'),('following','following_hash'),('rebuilt_following','rebuilt_following_hash')]:assert h(chain[raw])==chain[key] assert json.loads(chain['following'])['previous']==chain['original_hash'] assert json.loads(chain['rebuilt_following'])['previous']==chain['changed_hash'] assert chain['original_hash']!=chain['changed_hash'] print('Market-cap arithmetic and all four SHA-256 values verified')