def parse_inputs( _cls, test_case_data: Dict[str, Any] ) -> Tuple[BeaconState, Tuple[BeaconBlock, ...]]: return ( from_formatted_dict(test_case_data["pre"], BeaconState), tuple( from_formatted_dict(block_data, BeaconBlock) for block_data in test_case_data["blocks"]), )
def parse_inputs( cls, test_case_data: Dict[str, Any]) -> Tuple[BeaconState, Any]: operation_name = (cls.operation_name if hasattr(cls, "operation_name") else cls.name) return ( from_formatted_dict(test_case_data["pre"], BeaconState), from_formatted_dict(test_case_data[operation_name], cls.operation_type), )
def get_states( test_case: Dict[str, Any], cls_state: Type[BeaconState]) -> Tuple[BeaconState, BeaconState, bool]: pre = from_formatted_dict(test_case['pre'], cls_state) if test_case['post'] is not None: post = from_formatted_dict(test_case['post'], cls_state) is_valid = True else: post = None is_valid = False return pre, post, is_valid
def _deserialize_object_from_dict( data: Dict[str, Any], object_type: Type[HashableContainer]) -> HashableContainer: if object_type == BeaconState: # NOTE: borrowing from `py-ssz` input_kwargs = { field_name: from_formatted_dict(data[field_name], field_type) for field_name, field_type in object_type._meta.fields } # NOTE: we want to inject some additiona kwargs here, # due to non-standard (but still valid) SSZ inputs input_kwargs["validator_and_balance_length_check"] = False return object_type.create(**input_kwargs) else: return from_formatted_dict(data, object_type)
def execute_invalid_ssz_test(test_case, sedes): if "value" in test_case and "ssz" in test_case: raise ValueError( "Test case for invalid inputs contains both value and ssz") if "value" in test_case: value = from_formatted_dict(test_case["value"], sedes, CustomCodec) try: ssz.encode(value, sedes) except SSZException: pass else: raise FailedTestCase( "Serializing invalid value did not yield an exception") elif "ssz" in test_case: serial = decode_hex(test_case["ssz"]) try: ssz.decode(serial, sedes) except SSZException: pass else: raise FailedTestCase( "Deserializing invalid SSZ did not yield an exception") else: raise ValueError( "Test case for invalid inputs contains neither value nor ssz")
def test_not_serializable(): octopi = (octopus, octopus, octopus) sedes = List(Animal, 2**32) output = to_formatted_dict(octopi, sedes) hashable_octopi = HashableList.from_iterable(octopi, sedes) assert hashable_octopi == from_formatted_dict(output, List(Animal, 2**32))
def extract_genesis_state_from_stream( stream: Union[Path, "StreamTextType"] ) -> BeaconState: yaml = YAML(typ="unsafe") genesis_json = yaml.load(stream) state = from_formatted_dict(genesis_json, BeaconState) return state
def _deserialize_object_from_dict( data: Dict[str, Any], object_type: ssz.Serializable) -> ssz.Serializable: if object_type == BeaconState: # NOTE: borrowing from `py-ssz` parse_args = tuple(data[field_name] for field_name in object_type._meta.field_names) input_args = from_formatted_dict(parse_args, object_type._meta.container_sedes) input_kwargs = dict(zip(object_type._meta.field_names, input_args)) # NOTE: we want to inject some additiona kwargs here, # due to non-standard (but still valid) SSZ inputs input_kwargs["validator_and_balance_length_check"] = False return object_type(**input_kwargs) else: return from_formatted_dict(data, object_type)
def get_operation_or_header( test_case: Dict[str, Any], cls_operation_or_header: Type[OperationOrBlockHeader], handler: str) -> Tuple[OperationOrBlockHeader, ...]: if handler in test_case: return from_formatted_dict(test_case[handler], cls_operation_or_header) else: raise NameError(f"Operation {handler} is not supported.")
def execute_state_transtion(test_case, base_db): bls.use_noop_backend() dict_config = test_case['config'] verify_signatures = test_case['verify_signatures'] dict_initial_state = test_case['initial_state'] dict_blocks = test_case['blocks'] dict_expected_state = test_case['expected_state'] # TODO: make it case by case assert verify_signatures is False # Set config config = generate_config_by_dict(dict_config) # Set Vector fields override_vector_lengths(config) # Set pre_state pre_state = from_formatted_dict(dict_initial_state, BeaconState) # Set blocks blocks = () for dict_block in dict_blocks: block = from_formatted_dict(dict_block, SerenityBeaconBlock) blocks += (block, ) sm_class = SerenityStateMachine.configure( __name__='SerenityStateMachineForTesting', config=config, ) chaindb = BeaconChainDB(base_db, Eth2GenesisConfig(config)) attestation_pool = AttestationPool() post_state = pre_state.copy() for block in blocks: sm = sm_class(chaindb, attestation_pool, None, post_state) post_state, _ = sm.import_block(block) # Use dict diff, easier to see the diff dict_post_state = to_formatted_dict(post_state, BeaconState) for key, value in dict_expected_state.items(): if isinstance(value, list): value = tuple(value) assert dict_post_state[key] == value
def get_blocks( test_case: Dict[str, Any], cls_block: Type[BaseBeaconBlock]) -> Tuple[BaseBeaconBlock, ...]: if 'blocks' in test_case: return tuple( from_formatted_dict(block, cls_block) for block in test_case['blocks']) else: return ()
def parse_inputs( _cls, test_case_data: Dict[str, Any] ) -> Tuple[Hash32, Timestamp, Tuple[Deposit, ...]]: return ( cast(Hash32, decode_hex(test_case_data["eth1_block_hash"])), Timestamp(test_case_data["eth1_timestamp"]), tuple( cast(Deposit, from_formatted_dict(deposit_data, Deposit)) for deposit_data in test_case_data["deposits"]), )
def test_custom_codec(): class CustomCodec(DefaultCodec): @staticmethod def format_integer(value, sedes): return encode_hex(sedes.serialize(value)) @staticmethod def unformat_integer(value, sedes): return sedes.deserialize(decode_hex(value)) output = to_formatted_dict(zoo, codec=CustomCodec) read_zoo = from_formatted_dict(output, Zoo, CustomCodec) assert read_zoo == zoo
def parse_genesis_validity_test_case(test_case, handler, index, config): override_lengths(config) bls_setting = get_bls_setting(test_case) genesis = from_formatted_dict(test_case['genesis'], BeaconState) is_valid = test_case['is_valid'] return GenesisValidityTestCase( handler=handler, index=index, bls_setting=bls_setting, description=test_case['description'], genesis=genesis, is_valid=is_valid, )
def parse_genesis_initialization_test_case(test_case, handler, index, config): override_lengths(config) bls_setting = get_bls_setting(test_case) eth1_block_hash = decode_hex(test_case['eth1_block_hash']) eth1_timestamp = test_case['eth1_timestamp'] state = from_formatted_dict(test_case['state'], BeaconState) deposits = get_deposits(test_case, Deposit) return GenesisInitializationTestCase( handler=handler, index=index, bls_setting=bls_setting, description=test_case['description'], eth1_block_hash=eth1_block_hash, eth1_timestamp=eth1_timestamp, state=state, deposits=deposits, )
def execute_valid_ssz_test(test_case, sedes): value = from_formatted_dict(test_case["value"], sedes, CustomCodec) serial = decode_hex(test_case["ssz"]) try: decoded = ssz.decode(serial, sedes) except SSZException: raise FailedTestCase("Deserializing valid SSZ failed") else: if decoded != value: raise FailedTestCase( f"Deserializing SSZ returned wrong result {decoded}") try: encoded = ssz.encode(value, sedes) except SSZException: raise FailedTestCase("Serializing valid value failed") else: if encoded != serial: raise FailedTestCase( f"Serializing value retunred wrong result {encoded}")
def parse_inputs(_cls, test_case_data: Dict[str, Any]) -> BeaconState: return from_formatted_dict(test_case_data["pre"], BeaconState)
def parse_inputs( _cls, test_case_data: Dict[str, Any]) -> Tuple[BeaconState, int]: return ( from_formatted_dict(test_case_data["pre"], BeaconState), test_case_data["slots"], )
def get_deposits(test_case: Dict[str, Any], cls_deposit: Type[Deposit]) -> Tuple[Deposit, ...]: return tuple( from_formatted_dict(deposit, cls_deposit) for deposit in test_case['deposits'])
def execute_tree_hash_test_case(test_case): sedes = parse_type_definition(test_case["type"]) value = from_formatted_dict(test_case["value"], sedes, CustomCodec) expected_root = decode_hex(test_case["root"]) calculated_root = ssz.hash_tree_root(value, sedes) assert calculated_root == expected_root
def test_parsing_and_dumping(): json_str = json.dumps(to_formatted_dict(zoo)) read_zoo = from_formatted_dict(json.loads(json_str), Zoo) assert read_zoo == zoo
def parse_outputs(test_case_data: Dict[str, Any]) -> BeaconState: return from_formatted_dict(test_case_data["post"], BeaconState)
def test_not_serializable(): octopi = (octopus, octopus, octopus) output = to_formatted_dict(octopi, List(Animal)) assert octopi == from_formatted_dict(output, List(Animal))