Skip to content

Commit

Permalink
remove globals.harness_ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
kbhargava-jump committed Oct 21, 2024
1 parent 40f1864 commit 1886a77
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/test_suite/fixture_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ def extract_context_from_fixture(fixture_file: Path):
- int: 1 on success, 0 on failure
"""
try:
fixture = globals.harness_ctx.fixture_type()
fn_entrypoint = extract_metadata(fixture_file).fn_entrypoint
harness_ctx = HARNESS_ENTRYPOINT_MAP[fn_entrypoint]
fixture = harness_ctx.fixture_type()
with open(fixture_file, "rb") as f:
fixture.ParseFromString(f.read())

Expand Down
6 changes: 4 additions & 2 deletions src/test_suite/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# (For fixtures) Whether to only keep passing tests
only_keep_passing = False

# Harness context
harness_ctx: HarnessCtx = None
# Default harness context
default_harness_ctx: HarnessCtx = None

# Whether to run in consensus mode
consensus_mode: bool = False
9 changes: 5 additions & 4 deletions src/test_suite/multiprocessing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ def extract_metadata(fixture_file: Path) -> str | None:
print(f"Failed to parse 'metadata': {e}")
return None

metadata = globals.harness_ctx.metadata_type()
metadata.ParseFromString(f.read())
return getattr(metadata, "fn_entrypoint", None)


Expand Down Expand Up @@ -216,9 +214,9 @@ def decode_single_test_case(harness_ctx: HarnessCtx, test_file: Path) -> int:
return 0

# Encode the input fields to be human readable
instruction_context = globals.harness_ctx.context_type()
instruction_context = harness_ctx.context_type()
instruction_context.ParseFromString(serialized_instruction_context)
globals.harness_ctx.context_human_encode_fn(instruction_context)
harness_ctx.context_human_encode_fn(instruction_context)

with open(globals.output_dir / (test_file.stem + ".txt"), "w") as f:
f.write(
Expand Down Expand Up @@ -334,6 +332,9 @@ def build_test_results(
effects = harness_ctx.effects_type()
effects.ParseFromString(result)

if globals.consensus_mode:
harness_ctx.diff_effect_fn = harness_ctx.consensus_diff_effect_fn

# Note: diff_effect_fn may modify effects in-place
all_passed &= harness_ctx.diff_effect_fn(ref_effects, effects)

Expand Down
37 changes: 14 additions & 23 deletions src/test_suite/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from test_suite.multiprocessing_utils import (
decode_single_test_case,
extract_metadata,
read_fixture,
read_context_serialized,
initialize_process_output_buffers,
process_single_test_case,
Expand Down Expand Up @@ -47,12 +48,6 @@
- ValidateVM
- ElfHarness
"""
# harness_type = os.getenv("HARNESS_TYPE")
# if harness_type:
# globals.harness_ctx = eval(harness_type)
# else:
# globals.harness_ctx = InstrHarness
# harness_type = "InstrHarness"

app = typer.Typer(help=f"Validate effects from clients using Protobuf messages.")

Expand Down Expand Up @@ -384,10 +379,7 @@ def run_tests(
globals.reference_shared_library = reference_shared_library

# Set diff mode to consensus if specified
if consensus_mode:
globals.harness_ctx.diff_effect_fn = (
globals.harness_ctx.consensus_diff_effect_fn
)
globals.consensus_mode = consensus_mode

# Create the output directory, if necessary
if globals.output_dir.exists():
Expand Down Expand Up @@ -858,22 +850,21 @@ def regenerate_fixtures(
globals.target_libraries[shared_library] = lib
initialize_process_output_buffers()

target_features = features_utils.get_sol_compat_features_t(lib)
features_path = pb_utils.find_field_with_type(
globals.harness_ctx.context_type.DESCRIPTOR, context_pb.FeatureSet.DESCRIPTOR
)

# TODO: support multiple FeatureSet fields
assert len(features_path) == 1, "Only one FeatureSet field is supported"
features_path = features_path[0]

test_cases = list(input_path.iterdir()) if input_path.is_dir() else [input_path]
num_regenerated = 0

for file in test_cases:
fixture = globals.harness_ctx.fixture_type()
with open(file, "rb") as f:
fixture.ParseFromString(f.read())
fixture = read_fixture(file)
harness_ctx = HARNESS_ENTRYPOINT_MAP[fixture.metadata.fn_entrypoint]

target_features = features_utils.get_sol_compat_features_t(lib)
features_path = pb_utils.find_field_with_type(
harness_ctx.context_type.DESCRIPTOR, context_pb.FeatureSet.DESCRIPTOR
)

# TODO: support multiple FeatureSet fields
assert len(features_path) == 1, "Only one FeatureSet field is supported"
features_path = features_path[0]

features = pb_utils.access_nested_field_safe(fixture.input, features_path)
feature_set = set(features.features) if features else set()
Expand Down Expand Up @@ -981,7 +972,7 @@ def get_harness_type_for_folder(src, regenerate_folder):
output_dir, os.path.relpath(source_folder, test_vectors)
)
folder_harness_type = get_harness_type_for_folder(test_vectors, source_folder)
globals.harness_ctx = eval(folder_harness_type)
harness_ctx = eval(folder_harness_type) # TODO: can remove this now?
print(
f"Regenerating fixtures for {source_folder} with harness type {folder_harness_type}"
)
Expand Down

0 comments on commit 1886a77

Please sign in to comment.