def test_serialization_of_submessages_to_dict(): message = LedgerStatus(1, 10, None, None, "AwgQhPR9cgRubttBGjRruCRMLhZFBffbejbPipj7WBBm", CURRENT_PROTOCOL_VERSION) message_rep = MessageRep( **{ f.MSG_TYPE.nm: "LEDGER_STATUS", f.PARAMS.nm: { "ledger_id": 1, f.PROTOCOL_VERSION.nm: CURRENT_PROTOCOL_VERSION }, f.MSG.nm: message, }) serialized_message = ZStack.serializeMsg(message).decode() serialized_message_reply = ZStack.serializeMsg(message_rep).decode() # check that submessage (LedgerStatus) is serialized to the same dict as # it were a common message assert serialized_message in serialized_message_reply # check that de-serialized into the same message deserialized_message = LedgerStatus( **ZStack.deserializeMsg(serialized_message)) deserialized_submessage = LedgerStatus( **ZStack.deserializeMsg(serialized_message_reply)[f.MSG.nm]) assert message == deserialized_message assert message_rep.msg == deserialized_submessage assert message == deserialized_submessage
def _serialize_deserialize(self, msg): serialized_msg = Batched().prepForSending(msg) serialized_msg = ZStack.serializeMsg(serialized_msg) new_msg = node_message_factory.get_instance( **ZStack.deserializeMsg(serialized_msg)) if not isinstance(msg, MessageRep): assert MessageProcessor().toDict(msg) == MessageProcessor().toDict(new_msg), \ "\n {} \n {}".format(MessageProcessor().toDict(msg), MessageProcessor().toDict(new_msg)) return new_msg
def _serialize_deserialize(self, msg): serialized_msg = Batched().prepForSending(msg) serialized_msg = ZStack.serializeMsg(serialized_msg) new_msg = node_message_factory.get_instance(**ZStack.deserializeMsg(serialized_msg)) # TODO: Figure out why BatchIDs are not deserialized back if not isinstance(msg, (MessageRep, OldViewPrePrepareRequest, OldViewPrePrepareReply)): assert MessageProcessor().toDict(msg) == MessageProcessor().toDict(new_msg), \ "\n {} \n {}".format(MessageProcessor().toDict(msg), MessageProcessor().toDict(new_msg)) return new_msg
def test_process_pre_prepare_validation_old_schema_no_audit( replica_with_requests, pre_prepare, mock_schema_audit_txn_root): serialized_pp = ZStack.serializeMsg(pre_prepare) deserialized_pp = ZStack.deserializeMsg(serialized_pp) assert f.AUDIT_TXN_ROOT_HASH.nm not in PrePrepare.schema pp = PrePrepare(**deserialized_pp) replica_with_requests.processPrePrepare(pp, replica_with_requests.primaryName)
def test_process_pre_prepare_validation_old_schema_no_pool( replica_with_requests, pre_prepare, mock_schema_pool_state_root): serialized_pp = ZStack.serializeMsg(pre_prepare) deserialized_pp = ZStack.deserializeMsg(serialized_pp) assert f.POOL_STATE_ROOT_HASH.nm not in PrePrepare.schema pp = PrePrepare(**deserialized_pp) replica_with_requests.processPrePrepare(pp, replica_with_requests.primaryName)
def test_that_service_fields_not_being_serialized(): """ Checks that service fields of validators, like 'typename' and 'schema' ] are excluded from serialized message """ message = LedgerStatus(1,10,None,None,"AwgQhPR9cgRubttBGjRruCRMLhZFBffbejbPipj7WBBm") serialized = ZStack.serializeMsg(message) deserialized = ZStack.deserializeMsg(serialized) service_fields = {'typename', 'schema', 'optional', 'nullable'} assert service_fields - set(deserialized) == service_fields
def test_that_service_fields_not_being_serialized(): """ Checks that service fields of validators, like 'typename' and 'schema' ] are excluded from serialized message """ message = LedgerStatus( 1, 10, None, None, "AwgQhPR9cgRubttBGjRruCRMLhZFBffbejbPipj7WBBm", CURRENT_PROTOCOL_VERSION) serialized = ZStack.serializeMsg(message) deserialized = ZStack.deserializeMsg(serialized) service_fields = {'typename', 'schema', 'optional', 'nullable'} assert service_fields - set(deserialized) == service_fields
def test_serialization_of_submessages_to_dict(): message = LedgerStatus( 1, 10, None, None, "AwgQhPR9cgRubttBGjRruCRMLhZFBffbejbPipj7WBBm", CURRENT_PROTOCOL_VERSION) message_rep = MessageRep(**{ f.MSG_TYPE.nm: "LEDGER_STATUS", f.PARAMS.nm: {"ledger_id": 1, f.PROTOCOL_VERSION.nm: CURRENT_PROTOCOL_VERSION}, f.MSG.nm: message, }) serialized_message = ZStack.serializeMsg(message).decode() serialized_message_reply = ZStack.serializeMsg(message_rep).decode() # check that submessage (LedgerStatus) is serialized to the same dict as # it were a common message assert serialized_message in serialized_message_reply # check that de-serialized into the same message deserialized_message = LedgerStatus( **ZStack.deserializeMsg(serialized_message)) deserialized_submessage = LedgerStatus( **ZStack.deserializeMsg(serialized_message_reply)[f.MSG.nm]) assert message == deserialized_message assert message_rep.msg == deserialized_submessage assert message == deserialized_submessage
def test_process_pre_prepare_validation_old_schema(fake_replica, pre_prepare, pool_state_root, fake_state_root_hash): serialized_pp = ZStack.serializeMsg(pre_prepare) deserialized_pp = ZStack.deserializeMsg(serialized_pp) new_schema = copy(PrePrepare.schema) PrePrepare.schema = tuple(y for y in PrePrepare.schema if y[0] != f.POOL_STATE_ROOT_HASH.nm) assert f.POOL_STATE_ROOT_HASH.nm not in PrePrepare.schema pp = PrePrepare(**deserialized_pp) state_roots = [pool_state_root, fake_state_root_hash] fake_replica.stateRootHash = lambda ledger, to_str=False: state_roots[ ledger] def reportSuspiciousNodeEx(ex): assert False, ex fake_replica.node.reportSuspiciousNodeEx = reportSuspiciousNodeEx fake_replica.processPrePrepare(pp, fake_replica.primaryName) PrePrepare.schema = new_schema