Files
potatium/tests/test_pop_fee_flows.py
T

94 lines
4.0 KiB
Python

"""Pure off-chain accounting and ZIP importer tests."""
from __future__ import annotations
import importlib.util
import json
import sys
import tempfile
import unittest
import zipfile
from pathlib import Path
S = Path(__file__).resolve().parents[1] / 'scripts' / 'analyze_pop_fee_flows.py'
spec = importlib.util.spec_from_file_location('analyze_pop_fee_flows', S)
mod = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = mod
spec.loader.exec_module(mod)
def sample():
return {'parameters': {'target_workers': 1, 'seed': 2, 'operators': 2, 'days': 2,
'other_workers': 0, 'worker_fee_scale': 1.0,
'initial_balance': 1.0, 'others_workers': 0, 'newcomers': 0},
'summary': {'minted': 8.0, 'burned_workers': 2.0, 'burned_windows': 1.0,
'burned_total': 3.0, 'target_net': 2.5,
'target_win_percent': 25., 'gini_operator_wallets': .1},
'operators': [{'wallet': 3.0}, {'wallet': 4.0}]}
class FeeFlowTests(unittest.TestCase):
def test_all_policies_conserve_supply(self):
for p in mod.presets():
r = mod.project(sample(), p, 4)
self.assertAlmostEqual(r['miner_wallets'] + r['paid_validators'] + r['paid_treasury'],
r['outstanding_supply'])
self.assertAlmostEqual(r['burned'] + r['paid_validators'] + r['paid_treasury'], 3)
def test_burn_reduces_outstanding(self):
r = mod.project(sample(), mod.presets()[0], 4)
self.assertEqual((r['outstanding_supply'], r['burned'], r['paid_validators']), (7, 3, 0))
def test_no_burn_increases_outstanding_but_not_miner_net(self):
burn = mod.project(sample(), mod.presets()[0], 4)
hybrid = mod.project(sample(), mod.presets()[3], 4)
self.assertEqual((hybrid['burned'], hybrid['outstanding_supply']), (0, 10))
self.assertAlmostEqual(hybrid['paid_validators'], 2.4)
self.assertAlmostEqual(hybrid['paid_treasury'], .6)
self.assertEqual(hybrid['target_miner_net'], burn['target_miner_net'])
def test_different_destinations_worker_and_window(self):
d = mod.project(sample(), mod.presets()[-1], 2)
self.assertEqual((d['paid_validators'], d['paid_treasury'], d['burned']), (1, 2, 0))
def test_custom_triplets(self):
self.assertEqual(mod.parse_split('0,80,20'), mod.Split(0, .8, .2))
for s in ('70,10,10', 'nan,10,90', '0,10', '1,2,foo'):
with self.assertRaises(ValueError): mod.parse_split(s)
def test_reject_bad_accounting(self):
bad = sample()
bad['operators'][0]['wallet'] = 99
with self.assertRaises(ValueError): mod.project(bad, mod.presets()[0], 4)
def test_grouping_seeds(self):
a = sample()
b = sample()
b['parameters']['seed'] = 4
runs = [mod.project(v, mod.presets()[0], 4) for v in (a, b)]
rows = mod.summarize(runs)
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]['seeds'], 2)
def test_read_zip_without_extracting(self):
with tempfile.TemporaryDirectory() as d:
path = Path(d) / 'results.zip'
with zipfile.ZipFile(path, 'w') as z:
z.writestr('potatium-simulation/pop_cluster_sample.json', json.dumps({'scenarios': [sample()]}))
z.writestr('../should_not_be_extracted.txt', 'nothing')
source, names = mod.load_sources(path)
self.assertEqual(len(source), 1)
self.assertEqual(len(names), 1)
self.assertFalse((Path(d).parent / 'should_not_be_extracted.txt').exists())
def test_output_package(self):
with tempfile.TemporaryDirectory() as d:
runs = [mod.project(sample(), x, 4) for x in mod.presets()]
paths = mod.output(Path(d), mod.summarize(runs), runs, ['a.json'])
self.assertEqual(len(paths), 5)
with zipfile.ZipFile(paths[-1]) as z:
self.assertIsNone(z.testzip())
self.assertEqual(len(z.namelist()), 4)
if __name__ == '__main__':
unittest.main()