From ab4c6dfd9425212ea71f59ca69a362a031a8e3ad Mon Sep 17 00:00:00 2001 From: Kyle Hegeman Date: Thu, 27 Jul 2023 19:23:28 -0400 Subject: [PATCH 1/3] Add an option to the invariant decorator to rollback the chain after checking an invariant. --- woke/testing/fuzzing/fuzz_test.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/woke/testing/fuzzing/fuzz_test.py b/woke/testing/fuzzing/fuzz_test.py index 3d4b6cb4a..72c8cf604 100644 --- a/woke/testing/fuzzing/fuzz_test.py +++ b/woke/testing/fuzzing/fuzz_test.py @@ -28,10 +28,11 @@ def decorator(fn): return decorator -def invariant(*, period: int = 1): +def invariant(*, period: int = 1, commitChanges=True): def decorator(fn): fn.invariant = True fn.period = period + fn.commit = commitChanges return fn return decorator @@ -59,11 +60,7 @@ def __get_methods(self, attr: str) -> List[Callable]: return ret def run( - self, - sequences_count: int, - flows_count: int, - *, - dry_run: bool = False, + self, sequences_count: int, flows_count: int, *, dry_run: bool = False, ): chains = get_connected_chains() @@ -128,10 +125,18 @@ def run( self.pre_invariants() for inv in invariants: if invariant_periods[inv] == 0: + isnapshots = [] + # if changes that occur during checking the invariant are not to be committed take a snapshot + if getattr(inv, "commit") == False: + isnapshots = [chain.snapshot() for chain in chains] self.pre_invariant(inv) inv(self) self.post_invariant(inv) + #restore any snapshots saved before the invariant + for snapshot, chain in zip(isnapshots, chains): + chain.revert(snapshot) + invariant_periods[inv] += 1 if invariant_periods[inv] == getattr(inv, "period"): invariant_periods[inv] = 0 From 49b8c8971fc7fd48ef4720dbecfc2c055ee4ce38 Mon Sep 17 00:00:00 2001 From: Kyle Hegeman Date: Thu, 27 Jul 2023 19:23:28 -0400 Subject: [PATCH 2/3] fixup! Add an option to the invariant decorator to rollback the chain after checking an invariant. --- woke/testing/fuzzing/fuzz_test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/woke/testing/fuzzing/fuzz_test.py b/woke/testing/fuzzing/fuzz_test.py index 72c8cf604..be3114bbf 100644 --- a/woke/testing/fuzzing/fuzz_test.py +++ b/woke/testing/fuzzing/fuzz_test.py @@ -60,7 +60,11 @@ def __get_methods(self, attr: str) -> List[Callable]: return ret def run( - self, sequences_count: int, flows_count: int, *, dry_run: bool = False, + self, + sequences_count: int, + flows_count: int, + *, + dry_run: bool = False, ): chains = get_connected_chains() @@ -133,7 +137,7 @@ def run( inv(self) self.post_invariant(inv) - #restore any snapshots saved before the invariant + # restore any snapshots saved before the invariant for snapshot, chain in zip(isnapshots, chains): chain.revert(snapshot) From 415ecc5b0eebd8c4c616c214cb766b929eeae20f Mon Sep 17 00:00:00 2001 From: Kyle Hegeman Date: Fri, 28 Jul 2023 06:30:31 -0400 Subject: [PATCH 3/3] Change default to False for commit changes option on invariants. Rename the option to commit_changes. --- woke/testing/fuzzing/fuzz_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/woke/testing/fuzzing/fuzz_test.py b/woke/testing/fuzzing/fuzz_test.py index be3114bbf..42d1a63ec 100644 --- a/woke/testing/fuzzing/fuzz_test.py +++ b/woke/testing/fuzzing/fuzz_test.py @@ -28,11 +28,11 @@ def decorator(fn): return decorator -def invariant(*, period: int = 1, commitChanges=True): +def invariant(*, period: int = 1, commit_changes=False): def decorator(fn): fn.invariant = True fn.period = period - fn.commit = commitChanges + fn.commit_changes = commit_changes return fn return decorator @@ -131,7 +131,7 @@ def run( if invariant_periods[inv] == 0: isnapshots = [] # if changes that occur during checking the invariant are not to be committed take a snapshot - if getattr(inv, "commit") == False: + if getattr(inv, "commit_changes") == False: isnapshots = [chain.snapshot() for chain in chains] self.pre_invariant(inv) inv(self)