373 lines
17 KiB
Python
373 lines
17 KiB
Python
"""Private exact-rational and whole-forest oracle; no candidate code in this process."""
|
|
import copy
|
|
from fractions import Fraction
|
|
import json
|
|
import math
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
CANDIDATES = ['quota_parse.cpp', 'quota_forest.cpp', 'quota_query.cpp', 'quota_store.cpp']
|
|
MAXIMUM = 10 ** 12
|
|
EMPTY_QUERY = [1, False, [0, 0], [], []]
|
|
|
|
|
|
def parse(text):
|
|
if len(text) > 4096 or any(ord(ch) > 127 for ch in text):
|
|
raise ValueError('text')
|
|
fields = re.findall(r'[^ \t\n\r\v\f]+', text)
|
|
if len(fields) != 2:
|
|
raise ValueError('fields')
|
|
quota, period = fields
|
|
if not re.fullmatch('[0-9]+', period) or not 1 <= int(period) <= MAXIMUM:
|
|
raise ValueError('period')
|
|
if quota == 'max':
|
|
return [True, 0, int(period)]
|
|
if not re.fullmatch('[0-9]+', quota) or not 1 <= int(quota) <= MAXIMUM:
|
|
raise ValueError('quota')
|
|
return [False, int(quota), int(period)]
|
|
|
|
|
|
def identifier(text):
|
|
if not 1 <= len(text) <= 128:
|
|
raise ValueError('identifier')
|
|
|
|
|
|
def compile_forest(raw):
|
|
if len(raw) > 256:
|
|
raise ValueError('node count')
|
|
nodes = {}
|
|
for name, parent, text in raw:
|
|
identifier(name)
|
|
if name in nodes:
|
|
raise ValueError('duplicate')
|
|
if parent is not None:
|
|
identifier(parent)
|
|
nodes[name] = (parent, parse(text))
|
|
for parent, _ in nodes.values():
|
|
if parent is not None and parent not in nodes:
|
|
raise ValueError('missing parent')
|
|
# Per-start parent walks use fresh sets, separately from the C++ color-table walk.
|
|
for start in nodes:
|
|
path = set()
|
|
current = start
|
|
while current is not None:
|
|
if current in path:
|
|
raise ValueError('cycle')
|
|
path.add(current)
|
|
current = nodes[current][0]
|
|
return [[name, parent, rate] for name, (parent, rate) in sorted(nodes.items())]
|
|
|
|
|
|
def evaluate(tree, target):
|
|
nodes = {name: (parent, rate) for name, parent, rate in tree}
|
|
if target not in nodes:
|
|
return copy.deepcopy(EMPTY_QUERY)
|
|
path = []
|
|
cursor = target
|
|
while cursor is not None:
|
|
path.append(cursor)
|
|
cursor = nodes[cursor][0]
|
|
rates = {name: Fraction(rate[1], rate[2]) for name, (_, rate) in nodes.items() if not rate[0]}
|
|
finite = [rates[name] for name in path if name in rates]
|
|
if not finite:
|
|
return [0, False, [0, 0], path, []]
|
|
minimum = min(finite)
|
|
return [0, True, [minimum.numerator, minimum.denominator], path,
|
|
[name for name in path if rates.get(name) == minimum]]
|
|
|
|
|
|
def raw_tree(tree):
|
|
return [[name, parent, ('max' if limit[0] else str(limit[1])) + ' ' + str(limit[2])]
|
|
for name, parent, limit in tree]
|
|
|
|
|
|
class Store:
|
|
def __init__(self, tree):
|
|
self.tree = copy.deepcopy(tree)
|
|
self.revision = 0
|
|
|
|
def snapshot(self):
|
|
return copy.deepcopy([self.revision, self.tree])
|
|
|
|
def apply(self, edits):
|
|
try:
|
|
if len(edits) > 512:
|
|
raise ValueError('edit count')
|
|
# Check every local record before folding, including overwritten puts.
|
|
for op, name, parent, text in edits:
|
|
identifier(name)
|
|
if op == 0:
|
|
if parent is not None:
|
|
identifier(parent)
|
|
parse(text)
|
|
elif op == 1:
|
|
if parent is not None or text != '':
|
|
raise ValueError('erase fields')
|
|
else:
|
|
raise ValueError('operation')
|
|
working = {name: [parent, text] for name, parent, text in raw_tree(self.tree)}
|
|
for op, name, parent, text in edits:
|
|
if op == 1:
|
|
working.pop(name, None)
|
|
else:
|
|
working[name] = [parent, text]
|
|
final = compile_forest([[name, *data] for name, data in working.items()])
|
|
except ValueError:
|
|
return 1
|
|
self.tree = final
|
|
self.revision += 1
|
|
return 0
|
|
|
|
|
|
class Cases:
|
|
def __init__(self):
|
|
self.commands = []
|
|
self.expected = []
|
|
self.labels = []
|
|
self.banks = [dict(raw=[], initial=[], held=[0, []], store=Store([])) for _ in range(3)]
|
|
self.selected = 0
|
|
|
|
@staticmethod
|
|
def hex(text):
|
|
return text.encode('latin1').hex() or '-'
|
|
|
|
def parent(self, value):
|
|
return '0 -' if value is None else '1 ' + self.hex(value)
|
|
|
|
def add(self, wire, expected, label):
|
|
self.commands.append(wire)
|
|
self.expected.append(copy.deepcopy(expected))
|
|
self.labels.append(label)
|
|
|
|
def config(self, raw, label='C08'):
|
|
bank = self.banks[self.selected]
|
|
bank['raw'] = copy.deepcopy(raw)
|
|
try:
|
|
tree = compile_forest(raw)
|
|
bank['initial'] = copy.deepcopy(tree)
|
|
bank['store'] = Store(tree)
|
|
error = 0
|
|
except ValueError:
|
|
tree, error = [], 1
|
|
lines = ['C %d' % len(raw)] + ['N %s %s %s' % (self.hex(name), self.parent(parent), self.hex(text)) for name, parent, text in raw]
|
|
self.add('\n'.join(lines), ['C', error, tree, True], label)
|
|
|
|
def update(self, edits, label='C23'):
|
|
bank = self.banks[self.selected]
|
|
error = bank['store'].apply(edits)
|
|
lines = ['U %d' % len(edits)] + ['D %d %s %s %s' % (op, self.hex(name), self.parent(parent), self.hex(text)) for op, name, parent, text in edits]
|
|
self.add('\n'.join(lines), ['U', error, bank['store'].snapshot(), True], label)
|
|
|
|
def command(self, op, *args, label='C17'):
|
|
bank = self.banks[self.selected]
|
|
if op == 'B':
|
|
tree = copy.deepcopy(bank['store'].tree)
|
|
self.banks[args[0]] = dict(raw=raw_tree(tree), initial=copy.deepcopy(tree), held=[0, []], store=Store(tree))
|
|
self.add('B %d' % args[0], ['B', args[0], self.banks[args[0]]['store'].snapshot()], label)
|
|
elif op == 'V':
|
|
self.selected = args[0]
|
|
self.add('V %d' % args[0], ['V', self.banks[self.selected]['store'].snapshot()], label)
|
|
elif op == 'L':
|
|
try:
|
|
output, error = parse(args[0]), 0
|
|
except ValueError:
|
|
output, error = [False, 0, 0], 1
|
|
self.add('L ' + self.hex(args[0]), ['L', error, output, True], label)
|
|
elif op == 'F':
|
|
divisor = math.gcd(*args)
|
|
self.add('F %d %d' % args, ['F', [args[0] // divisor, args[1] // divisor]], label)
|
|
elif op == 'K':
|
|
a, b, c, d = args
|
|
left, right = a * d, c * b
|
|
self.add('K %d %d %d %d' % args, ['K', (left > right) - (left < right)], label)
|
|
elif op in ('Q', 'E', 'A'):
|
|
if op == 'A':
|
|
try:
|
|
output = evaluate(compile_forest(bank['raw']), args[0])
|
|
except ValueError:
|
|
output = copy.deepcopy(EMPTY_QUERY)
|
|
else:
|
|
output = evaluate(bank['store'].tree if op == 'Q' else bank['initial'], args[0])
|
|
self.add(op + ' ' + self.hex(args[0]), [op, output, True], label)
|
|
elif op == 'O':
|
|
self.add(op, [op, bank['initial']], label)
|
|
else:
|
|
if op == 'H':
|
|
bank['held'] = bank['store'].snapshot()
|
|
self.add(op, [op, bank['held'] if op == 'J' else bank['store'].snapshot()], label)
|
|
|
|
|
|
def cases():
|
|
c = Cases()
|
|
rng = random.Random(20261017018)
|
|
# Public parent replacement transaction must validate the final forest only.
|
|
c.config([['root', None, 'max 100000'], ['leaf', 'root', '50000 100000']])
|
|
c.command('Q', 'leaf', label='C12'); c.command('H')
|
|
c.update([(1, 'root', None, ''), (0, 'root', None, 'max 200000')], label='C23')
|
|
c.command('Q', 'leaf', label='C26'); c.command('J', label='C27'); c.command('O', label='C09')
|
|
for text in ['', '1', '1 2 3', '1 2', '\t00001\n\r0002\v\f', 'max 1', 'max 00001',
|
|
'Max 1', 'max 0', 'max -1', 'max 1000000000001', '1 0', '0 1', '-1 2',
|
|
'+1 2', '1 +2', '1.0 2', '1e2 3', '1\x002', '1\x00 2', '1 2\xff',
|
|
'1\xa02', '1\x1c2', '1\x1d2', '1\x1e2', '1\x1f2', '1000000000000 1000000000000', '1000000000001 1',
|
|
'0' * 4092 + '1 1', '0' * 4093 + '1 1', '0' * 4094 + '1 1']:
|
|
c.command('L', text, label='C03')
|
|
values = [(1, 1), (MAXIMUM, 1), (1, MAXIMUM), (MAXIMUM, MAXIMUM), (MAXIMUM - 1, MAXIMUM)]
|
|
for _ in range(180):
|
|
values.append((rng.randrange(1, MAXIMUM + 1), rng.randrange(1, MAXIMUM + 1)))
|
|
for a, b in values:
|
|
c.command('F', a, b, label='C04')
|
|
c.command('K', a, b, a, b, label='C04')
|
|
c.command('K', a, b, b, a, label='C04')
|
|
x, y = rng.choice(values)
|
|
c.command('K', a, b, x, y, label='C04')
|
|
for vector in [(MAXIMUM, MAXIMUM - 1, MAXIMUM - 1, MAXIMUM),
|
|
(999999999989, 999999999999, 999999999988, 999999999998),
|
|
(2, 3, 4, 6), (1, 3, 2, 6), (1, 2, 2, 3)]:
|
|
c.command('K', *vector, label='C04')
|
|
c.config([], label='C05'); c.command('A', 'missing', label='C10'); c.command('Q', '', label='C10')
|
|
for raw in [
|
|
[['a', None, 'max 1'], ['a', None, '1 1']],
|
|
[['', None, '1 1']], [['a' * 128, None, '1 1']], [['a' * 129, None, '1 1']],
|
|
[['a', '', '1 1']], [['a', 'missing', '1 1']], [['a', 'a', '1 1']],
|
|
[['safe', None, 'max 1'], ['b', 'c', '1 1'], ['c', 'b', '1 1']],
|
|
[['safe', None, 'max 1'], ['unrelated', None, 'bogus']],
|
|
[['safe', None, 'max 1'], ['unrelated', 'unknown', '1 1']]]:
|
|
c.config(raw, label='C07'); c.command('A', 'safe', label='C15')
|
|
opaque = [['z\x00\xff', '\x80root', '2 4'], ['\x80root', 'A', '3 6'], ['A', None, '1 2'], ['a', None, 'max 2']]
|
|
c.config(opaque, label='C05')
|
|
for target in ['z\x00\xff', '\x80root', 'A', 'a', 'z', '', '\x00']:
|
|
c.command('Q', target, label='C14'); c.command('E', target, label='C16'); c.command('A', target, label='C15')
|
|
c.command('S', label='C17')
|
|
c.config([['z', 'm', '2 4'], ['m', 'A', '3 6'], ['A', None, '1 2']], label='C14')
|
|
c.command('Q', 'z', label='C14'); c.command('Q', 'm', label='C14')
|
|
c.config([['leaf', 'middle', '1 2'], ['middle', 'root', 'max 999'], ['root', None, '1 3']])
|
|
c.command('Q', 'leaf', label='C12')
|
|
c.config([['x', 'y', 'max 1'], ['y', 'z', 'max 7'], ['z', None, 'max 9']])
|
|
c.command('Q', 'x', label='C13')
|
|
# Every mutation is checked atomically, followed by old/new reads and snapshot reuse.
|
|
c.config([['a', None, '1 2'], ['b', 'a', 'max 20'], ['c', 'b', '2 3'], ['other', None, '1 7']])
|
|
c.command('H', label='C27')
|
|
c.update([(0, 'a', 'c', '1 5'), (0, 'c', None, '2 7')], label='C23')
|
|
c.command('Q', 'b', label='C26'); c.command('E', 'b', label='C09'); c.command('J', label='C27'); c.command('O', label='C09')
|
|
for edits in [
|
|
[(0, 'new', 'absent', '1 2')],
|
|
[(0, 'a', None, 'bad'), (1, 'a', None, '')],
|
|
[(0, 'temporary', None, '1 0'), (1, 'temporary', None, '')],
|
|
[(1, 'none', None, 'extra')], [(1, 'none', 'a', '')],
|
|
[(2, 'none', None, '')], [(0, '', None, '1 1')],
|
|
[(0, 'q', '', '1 1')], [(0, 'q', None, 'max 0')],
|
|
[(0, 'a', None, '3 7'), (0, 'b', 'c', '4 7'), (0, 'c', 'b', '5 7')]]:
|
|
c.command('H'); c.update(edits, label='C25'); c.command('Q', 'other', label='C25'); c.command('J', label='C27')
|
|
c.update([], label='C21')
|
|
c.update([(1, 'absent', None, '')], label='C20')
|
|
c.update([(0, 'new', 'not-yet', '1 8'), (0, 'not-yet', None, 'max 9')], label='C23')
|
|
c.command('Q', 'new', label='C26')
|
|
c.update([(0, 'repeat', None, '1 2'), (1, 'repeat', None, ''), (0, 'repeat', None, '3 8')], label='C21')
|
|
c.command('Q', 'repeat', label='C21')
|
|
c.update([(1, 'none', None, '')] * 512, label='C18')
|
|
c.update([(1, 'none', None, '')] * 513, label='C18')
|
|
names = ['node_%03d' % n for n in range(256)]
|
|
chain = [[name, names[i - 1] if i else None, '%d %d' % (i + 1, (i + 1) * 3)] for i, name in enumerate(names)]
|
|
c.config(chain, label='C05'); c.command('Q', names[-1], label='C14')
|
|
c.command('H', label='C27')
|
|
c.update([(0, 'extra', None, '1 4'), (1, names[-1], None, '')], label='C22')
|
|
c.command('Q', names[-2], label='C26'); c.command('J', label='C27')
|
|
c.config(chain + [['extra', None, '1 4']], label='C05')
|
|
c.command('A', names[0], label='C15')
|
|
# Reconstruct two stores from one committed Tree before changing either side.
|
|
c.config([['shared_root', None, 'max 17'], ['shared_child', 'shared_root', '0002 0007']])
|
|
c.command('B', 1, label='C27')
|
|
c.update([(0, 'shared_child', 'shared_root', '3 11')], label='C24')
|
|
c.command('V', 1, label='C27'); c.command('Q', 'shared_child', label='C27')
|
|
c.update([(0, 'shared_root', None, '1 13')], label='C27')
|
|
c.command('V', 0, label='C27'); c.command('Q', 'shared_child', label='C27')
|
|
# Different stores and snapshots must not share revisions, parents, or finite rates.
|
|
for bank in range(3):
|
|
c.command('V', bank, label='C27')
|
|
c.config([['shared', None, '%d 11' % (bank + 1)]])
|
|
c.command('H', label='C27')
|
|
for bank in [2, 0, 1]:
|
|
c.command('V', bank, label='C27')
|
|
c.update([(0, 'shared', None, '%d 13' % (bank + 5))], label='C24')
|
|
c.command('Q', 'shared', label='C26'); c.command('J', label='C27'); c.command('O', label='C09')
|
|
c.command('V', 0)
|
|
for trial in range(80):
|
|
size = rng.randrange(2, 18)
|
|
ids = ['trial_%d_%02d' % (trial, n) for n in range(size)]
|
|
raw = []
|
|
for index, name in enumerate(ids):
|
|
parent = rng.choice(ids[:index] + [None])
|
|
quota = rng.randrange(1, MAXIMUM + 1)
|
|
period = rng.randrange(1, MAXIMUM + 1)
|
|
raw.append([name, parent, ('max' if rng.randrange(5) == 0 else str(quota)) + ' ' + str(period)])
|
|
rng.shuffle(raw)
|
|
c.config(raw, label='C08')
|
|
for name in rng.sample(ids, min(5, size)):
|
|
c.command('Q', name, label='C12'); c.command('A', name, label='C15')
|
|
for turn in range(5):
|
|
name = rng.choice(ids)
|
|
other = rng.choice(ids[:ids.index(name)] + ['absent', None])
|
|
edits = [(0, name, other, '%d %d' % (rng.randrange(1, MAXIMUM + 1), rng.randrange(1, MAXIMUM + 1)))]
|
|
if rng.randrange(3) == 0:
|
|
edits.append((0, name, None, 'max 17'))
|
|
c.command('H')
|
|
c.update(edits, label='C25')
|
|
c.command('Q', name, label='C26'); c.command('J', label='C27'); c.command('S', label='C17')
|
|
return c
|
|
|
|
|
|
def main():
|
|
matrix = cases()
|
|
context = json.loads((Path(__file__).resolve().parent / 'context.json').read_text())
|
|
scratch = Path(tempfile.mkdtemp(prefix='embedded-hard-quota-', dir='/tmp'))
|
|
for name, text in context.items():
|
|
(scratch / name).write_text(text)
|
|
for name in CANDIDATES:
|
|
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
|
|
build = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
|
|
'quota_observe.cpp', *CANDIDATES, '-o', 'quota_observe'],
|
|
cwd=scratch, text=True, capture_output=True, timeout=30)
|
|
if build.returncode:
|
|
print('FAILED_CHECKS ["C28"] build failed: ' + build.stderr[-1200:].replace('\n', ' '))
|
|
return 1
|
|
wire = '\n'.join(matrix.commands) + '\n'
|
|
# shortcut: candidate-owned scratch remains until container destruction or a separate
|
|
# trusted-local owner cleanup; this process does not access scratch after execution.
|
|
try:
|
|
result = subprocess.run([str(scratch / 'quota_observe')], cwd=scratch, input=wire,
|
|
text=True, encoding='utf-8', errors='replace', capture_output=True, timeout=15)
|
|
except subprocess.TimeoutExpired:
|
|
print('FAILED_CHECKS ["C29"] candidate exceeded 15 seconds')
|
|
return 1
|
|
if result.returncode:
|
|
print('FAILED_CHECKS ["C30"] candidate returned %d' % result.returncode)
|
|
return 1
|
|
observed = result.stdout.splitlines()
|
|
failures = set()
|
|
examples = []
|
|
for index in range(max(len(observed), len(matrix.expected))):
|
|
label = matrix.labels[index] if index < len(matrix.labels) else 'C30'
|
|
wanted = matrix.expected[index] if index < len(matrix.expected) else '<end>'
|
|
try:
|
|
actual = json.loads(observed[index]) if index < len(observed) else '<missing>'
|
|
except ValueError:
|
|
actual = observed[index]
|
|
if index >= len(observed) or index >= len(matrix.expected) or actual != wanted:
|
|
failures.add(label)
|
|
if len(examples) < 8:
|
|
examples.append('%s observation=%d got=%r expected=%r' % (label, index, repr(actual)[:350], repr(wanted)[:350]))
|
|
if failures:
|
|
print('FAILED_CHECKS ' + json.dumps(sorted(failures)))
|
|
for example in examples: print(example)
|
|
return 1
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|