20261117 固化13步自然回放、数据哈希与第二版性能基线
This commit is contained in:
@@ -21,7 +21,8 @@ def uri_key(uri: str) -> tuple:
|
||||
|
||||
|
||||
class ReplayState:
|
||||
def __init__(self, root: Path, epoch_path: Path, scope_path: Path | None = None):
|
||||
def __init__(self, root: Path, epoch_path: Path, scope_path: Path | None = None,
|
||||
bundle_manifest: Path | None = None, bundle_sha256: str | None = None):
|
||||
self.archive = Archive(root, read_only=True)
|
||||
self.epoch_path = epoch_path
|
||||
self.epoch_hash = digest_bytes(epoch_path.read_bytes())
|
||||
@@ -31,6 +32,14 @@ class ReplayState:
|
||||
raise ValueError("capture contract hash mismatch")
|
||||
self.scope = None
|
||||
self.scope_hash = None
|
||||
self.subset = None
|
||||
if bundle_manifest is not None or bundle_sha256 is not None:
|
||||
from replay_bundle import epoch_subset
|
||||
if bundle_manifest is None or scope_path is None or bundle_sha256 is None:
|
||||
raise ValueError("subset requires scope, manifest and pinned SHA256")
|
||||
self.subset = epoch_subset(root, epoch_path, scope_path, bundle_manifest, bundle_sha256)
|
||||
self.allowed = ({uri_key(r["uri"]): r for r in self.subset["http_entities"]}
|
||||
if self.subset is not None else None)
|
||||
self.failures = {}
|
||||
if scope_path is not None:
|
||||
from scope import select_epoch
|
||||
@@ -64,6 +73,8 @@ class ReplayState:
|
||||
event = read_json(root / "events" / f"{event_id}.json")
|
||||
self.check_event(event, uri)
|
||||
self.rsync_events.append(event)
|
||||
if self.allowed is not None and set(self.entries) != set(self.allowed):
|
||||
raise ValueError("consumed HTTP entities are not closed by original metadata")
|
||||
|
||||
def check_event(self, event: dict, uri: str) -> None:
|
||||
if event["uri"] != uri or not event["complete"] or event["completed_ms"] > self.epoch["validation_time_ms"]:
|
||||
@@ -71,6 +82,12 @@ class ReplayState:
|
||||
|
||||
def add(self, uri: str, ref: dict, content_type: str) -> None:
|
||||
key = uri_key(uri)
|
||||
if self.allowed is not None:
|
||||
selected = self.allowed.get(key)
|
||||
if selected is None:
|
||||
return
|
||||
if selected["sha256"] != ref["sha256"] or selected["bytes"] != ref["size"]:
|
||||
raise ValueError("consumed entity differs from original metadata")
|
||||
existing = self.entries.get(key)
|
||||
if existing and existing["sha256"] != ref["sha256"]:
|
||||
raise ValueError(f"conflicting contents for one epoch URI: {uri}")
|
||||
@@ -140,6 +157,8 @@ def main():
|
||||
parser.add_argument("--archive", type=Path, required=True)
|
||||
parser.add_argument("--epoch", type=Path, required=True)
|
||||
parser.add_argument("--scope", type=Path)
|
||||
parser.add_argument("--bundle-manifest", type=Path)
|
||||
parser.add_argument("--bundle-sha256")
|
||||
parser.add_argument("--bind", default="127.0.0.1")
|
||||
parser.add_argument("--http-port", type=int, action="append", default=[])
|
||||
parser.add_argument("--https-port", type=int, action="append", default=[])
|
||||
@@ -151,7 +170,7 @@ def main():
|
||||
if args.scope:
|
||||
from scope import require_closure
|
||||
require_closure(args.archive, args.scope)
|
||||
state = ReplayState(args.archive, args.epoch, args.scope)
|
||||
state = ReplayState(args.archive, args.epoch, args.scope, args.bundle_manifest, args.bundle_sha256)
|
||||
if not args.http_port and not args.https_port:
|
||||
parser.error("at least one server port is required")
|
||||
servers, listeners = [], []
|
||||
|
||||
Reference in New Issue
Block a user