Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invariant decorator option to revert state changes to the blockchain #266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion woke/testing/fuzzing/fuzz_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def decorator(fn):
return decorator


def invariant(*, period: int = 1):
def invariant(*, period: int = 1, commit_changes=False):
def decorator(fn):
fn.invariant = True
fn.period = period
fn.commit_changes = commit_changes
return fn

return decorator
Expand Down Expand Up @@ -128,10 +129,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_changes") == 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
Expand Down