From 1886a7791e05ebcd93909afffdaa1af1de0893ed Mon Sep 17 00:00:00 2001 From: Kunal Bhargava Date: Mon, 21 Oct 2024 23:01:22 +0000 Subject: [PATCH] remove globals.harness_ctx --- src/test_suite/fixture_utils.py | 4 ++- src/test_suite/globals.py | 6 ++-- src/test_suite/multiprocessing_utils.py | 9 +++--- src/test_suite/test_suite.py | 37 ++++++++++--------------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/test_suite/fixture_utils.py b/src/test_suite/fixture_utils.py index 35bd6ee..5f9aa02 100644 --- a/src/test_suite/fixture_utils.py +++ b/src/test_suite/fixture_utils.py @@ -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()) diff --git a/src/test_suite/globals.py b/src/test_suite/globals.py index 577fda2..54d5229 100644 --- a/src/test_suite/globals.py +++ b/src/test_suite/globals.py @@ -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 diff --git a/src/test_suite/multiprocessing_utils.py b/src/test_suite/multiprocessing_utils.py index e902c00..ca71b3f 100644 --- a/src/test_suite/multiprocessing_utils.py +++ b/src/test_suite/multiprocessing_utils.py @@ -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) @@ -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( @@ -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) diff --git a/src/test_suite/test_suite.py b/src/test_suite/test_suite.py index 6efc46e..5a0cce3 100644 --- a/src/test_suite/test_suite.py +++ b/src/test_suite/test_suite.py @@ -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, @@ -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.") @@ -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(): @@ -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() @@ -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}" )