File size: 3,596 Bytes
bbff072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""Deterministic mutation/reopen trace that exceeds the physical arena allocation count."""
import hashlib
import json
from pathlib import Path
import random
import tempfile
import pagefs


def main():
    source = Path(pagefs.__file__)
    before_hash = hashlib.sha256(source.read_bytes()).hexdigest()
    rng = random.Random(54013)
    expected = {f'/file/{i:04d}': bytes([i % 251]) * 128 for i in range(128)}
    tokens = {}
    pool = 1024
    allocations = 0
    reclaimed = 0
    checkpoints = 0
    reopens = 0
    with tempfile.TemporaryDirectory(prefix='nyarion-v7-sequence-') as tmp:
        path = Path(tmp) / 'volume.img'
        store = pagefs.PageStore.create(path, initial_files=expected, pool_blocks=pool)
        fixed_size = path.stat().st_size
        store.reset_stats()
        for i in range(2048):
            changes = {}
            for j in range(2 if i % 11 == 0 else 1):
                key = f'/file/{rng.randrange(160):04d}'
                value = None if i % 13 == 0 else rng.randbytes(128 + (i % 7))
                changes[key] = value
            token = f'sequence-{i:06d}'
            epoch = store.transact(changes, token)
            assert epoch == i + 1
            tokens[token] = epoch
            for key, value in changes.items():
                if value is None:
                    expected.pop(key, None)
                else:
                    expected[key] = value
            assert store.transact(changes, token) == epoch
            if i % 64 == 63:
                store.checkpoint()
                assert path.stat().st_size == fixed_size
                assert store.files() == sorted(expected)
                for key, value in expected.items():
                    assert store.get(key) == value
            if i % 256 == 255:
                allocations += store.stats['page_allocations']
                reclaimed += store.stats['reclaimed_blocks']
                checkpoints += store.stats['checkpoints']
                store.close()
                store = pagefs.PageStore(path)
                reopens += 1
                assert store.epoch == i + 1
                for key, value in expected.items():
                    assert store.get(key) == value
                for token, token_epoch in tokens.items():
                    assert store.query(token)['epoch'] == token_epoch
        allocations += store.stats['page_allocations']
        reclaimed += store.stats['reclaimed_blocks']
        checkpoints += store.stats['checkpoints']
        live = len(store._live_blocks)
        store.close()
        assert allocations > pool, 'must allocate more blocks over time than arena capacity'
        assert reclaimed > 0
    assert hashlib.sha256(source.read_bytes()).hexdigest() == before_hash, 'engine changed during validation'
    result = {'passed': True, 'engine_source_sha256': before_hash, 'transactions': 2048,
              'retry_assertions': 2048, 'reopens': reopens, 'checkpoints': checkpoints,
              'pool_blocks': pool, 'cumulative_physical_block_allocations': allocations,
              'cumulative_reclaimed_blocks': reclaimed, 'final_live_blocks': live,
              'fixed_apparent_image_bytes': fixed_size, 'final_file_count': len(expected),
              'all_file_bytes_and_token_epochs_verified': True,
              'claim': 'finite real-file reuse trace, not indefinite capacity or hardware fault proof'}
    Path('sequence_results.json').write_text(json.dumps(result, indent=2) + '\n')
    print(json.dumps(result, indent=2))


if __name__ == '__main__':
    main()