def test_can_serialize_coin_transfer_block():
    signed_change_request = baker.make(CoinTransferSignedChangeRequest)
    block_message = baker.make(BlockMessage, block_type='ct', signed_change_request=signed_change_request)
    block = baker.make(Block, message=block_message)
    block_dict = block.serialize_to_dict()
    assert isinstance(block_dict, dict)
    assert block_dict.keys() == {'signer', 'message', 'hash', 'signature'}
    assert isinstance(block_dict['signer'], str)
    assert isinstance(block_dict['message'], dict)
    assert isinstance(block_dict['hash'], str)
    assert isinstance(block_dict['signature'], str)

    block_message = block_dict['message']
    assert block_message.keys() == {
        'block_type', 'signed_change_request', 'timestamp', 'block_number', 'block_identifier',
        'updated_account_states'
    }
    assert block_message['block_type'] == 'ct'
    assert isinstance(block_message['signed_change_request'], dict)
    assert isinstance(block_message['timestamp'], str)
    assert isinstance(block_message['block_number'], int)
    assert isinstance(block_message['block_identifier'], str)
    assert isinstance(block_message['updated_account_states'], dict)

    signed_change_request = block_message['signed_change_request']
    assert signed_change_request.keys() == {'signer', 'message', 'signature'}
    assert isinstance(signed_change_request['signer'], str)
    assert isinstance(signed_change_request['message'], dict)
    assert isinstance(signed_change_request['signature'], str)

    signed_change_request_message = signed_change_request['message']
    assert signed_change_request_message.keys() == {'balance_lock', 'txs'}
    assert isinstance(signed_change_request_message['balance_lock'], str)
    assert isinstance(signed_change_request_message['txs'], list)
    for transaction in signed_change_request_message['txs']:
        assert isinstance(transaction, dict)
        if 'is_fee' in transaction:
            assert transaction.keys() == {'recipient', 'amount', 'is_fee', 'memo'}
            assert isinstance(transaction['is_fee'], bool)
        else:
            assert transaction.keys() == {'recipient', 'amount', 'memo'}

        assert isinstance(transaction['recipient'], str)
        assert isinstance(transaction['amount'], int)
        assert isinstance(transaction['memo'], str)

    updated_account_states = block_message['updated_account_states']
    for key, value in updated_account_states.items():
        assert isinstance(key, str)
        assert isinstance(value, dict)
        assert value.keys() == {'balance', 'balance_lock', 'node'}
        assert isinstance(value['balance'], int)
        assert isinstance(value['balance_lock'], str)
        assert isinstance(value['node'], dict)
        node = value['node']
        assert 'identifier' not in node
        assert isinstance(node['network_addresses'], list)
        assert isinstance(node['fee_amount'], int)
        assert isinstance(node['fee_account'], str)
def make_block(block_type, **kwargs):
    if 'message' not in kwargs:
        request_class = SIGNED_CHANGE_REQUEST_TYPE_MAP.get(block_type)
        if not request_class:
            raise NotImplementedError(
                f'Block type {block_type} is not supported yet')

        signed_change_request = baker.make(request_class)
        message = baker.make(BlockMessage,
                             block_type=block_type,
                             signed_change_request=signed_change_request)
        for account_number, account_state in message.updated_account_states.items(
        ):
            account_state.node.identifier = account_number

        kwargs['message'] = message

    return baker.make(Block, **kwargs)
def test_can_serialize_deserialize_coin_transfer_signed_change_request_message():
    message = baker.make(CoinTransferSignedChangeRequestMessage)
    serialized = message.serialize_to_dict()
    deserialized = CoinTransferSignedChangeRequestMessage.deserialize_from_dict(serialized)
    assert deserialized == message
    assert deserialized is not message
def test_all_blockchain_state_keys_are_compacted():
    blockchain_state = baker.make(BlockchainState)
    compacted_dict = blockchain_state.to_compact_dict()
    assert_dict_compacted(compacted_dict)