コード例 #1
0
def test_can_get_nodes_blocks_node_overrides_genesis_state_node(
    blockchain_directory, blockchain_genesis_state, user_account_key_pair
):
    blockchain = FileBlockchain(base_directory=blockchain_directory)

    account_number = user_account_key_pair.public
    blockchain_state_node = baker.make(
        Node, network_addresses=['https://192.168.0.32:8555/'], identifier=account_number
    )
    blockchain_genesis_state.account_states[account_number] = AccountState(node=blockchain_state_node)

    blockchain.add_blockchain_state(blockchain_genesis_state)
    blockchain.validate()

    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://127.0.0.1:8555/'], fee_amount=3, signing_key=user_account_key_pair.private
    )
    blocks_node = request.message.node
    assert blocks_node.identifier

    block = Block.create_from_signed_change_request(blockchain, request, get_node_signing_key())
    blockchain.add_block(block)

    assert blocks_node != blockchain_state_node
    assert list(blockchain.yield_nodes()) == [blocks_node]
コード例 #2
0
def test_node_serializes_with_identifier_in_account_state():
    node = baker.make(Node)
    assert node.identifier

    account_state = AccountState(node=node)
    serialized = account_state.serialize_to_dict()
    assert 'identifier' in serialized['node']
    assert serialized['node']['identifier'] == node.identifier
コード例 #3
0
def test_can_get_nodes_single_node_from_blockchain_genesis_state(
    blockchain_directory, blockchain_genesis_state, user_account_key_pair
):
    blockchain = FileBlockchain(base_directory=blockchain_directory)

    account_number = user_account_key_pair.public
    node = baker.make(Node, identifier=account_number)
    blockchain_genesis_state.account_states[account_number] = AccountState(node=node)

    blockchain.add_blockchain_state(blockchain_genesis_state)
    blockchain.validate()

    assert list(blockchain.yield_nodes()) == [node]
コード例 #4
0
def test_node_serializes_without_identifier_in_blockchain_state():
    node = baker.make(Node)
    account_number = node.identifier
    assert account_number
    account_state = AccountState(node=node)
    blockchain_state = BlockchainState(
        account_states={account_number: account_state})

    serialized = blockchain_state.serialize_to_dict()
    assert 'identifier' not in serialized['account_states'][account_number][
        'node']

    deserialized = BlockchainState.deserialize_from_dict(serialized)
    deserialized_node = deserialized.account_states[account_number].node
    assert deserialized_node is not node
    assert deserialized_node.identifier == account_number
コード例 #5
0
def test_node_serializes_without_identifier_in_block(memory_blockchain,
                                                     user_account_key_pair):
    account_number = user_account_key_pair.public
    node = baker.make(Node, identifier=account_number)

    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.34:8555/'],
        fee_amount=3,
        signing_key=user_account_key_pair.private)
    block = Block.create_from_signed_change_request(memory_blockchain, request,
                                                    get_node_signing_key())

    serialized = block.serialize_to_dict()
    assert 'identifier' not in serialized['message']['updated_account_states'][
        account_number]['node']

    deserialized = Block.deserialize_from_dict(serialized)
    deserialized_node = deserialized.message.updated_account_states[
        account_number].node
    assert deserialized_node is not node
    assert deserialized_node.identifier == account_number
コード例 #6
0
def test_can_get_nodes_from_different_block_numbers(
    blockchain_directory, blockchain_genesis_state, user_account_key_pair
):
    account_number = user_account_key_pair.public

    blockchain = FileBlockchain(base_directory=blockchain_directory)
    blockchain_state_node = baker.make(
        Node, network_addresses=['https://192.168.0.29:8555/'], identifier=account_number
    )
    blockchain_genesis_state.account_states[account_number] = AccountState(node=blockchain_state_node)
    blockchain.add_blockchain_state(blockchain_genesis_state)
    blockchain.validate()

    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.30:8555/'], fee_amount=3, signing_key=user_account_key_pair.private
    )
    node0 = request.message.node

    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.31:8555/'], fee_amount=3, signing_key=user_account_key_pair.private
    )

    node1 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.32:8555/'], fee_amount=3, signing_key=user_account_key_pair.private
    )
    node2 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    assert list(blockchain.yield_nodes()) == [node2]
    assert list(blockchain.yield_nodes(block_number=2)) == [node2]
    assert list(blockchain.yield_nodes(block_number=1)) == [node1]
    assert list(blockchain.yield_nodes(block_number=0)) == [node0]
    assert list(blockchain.yield_nodes(block_number=-1)) == [blockchain_state_node]
コード例 #7
0
def test_can_get_nodes_from_complex_blockchain(blockchain_directory, blockchain_genesis_state):
    key_pair1 = generate_key_pair()
    key_pair2 = generate_key_pair()
    key_pair3 = generate_key_pair()
    key_pair4 = generate_key_pair()
    key_pair5 = generate_key_pair()
    key_pair6 = generate_key_pair()
    assert len({
        key_pair1.public, key_pair2.public, key_pair3.public, key_pair4.public, key_pair5.public, key_pair6.public
    }) == 6

    blockchain = FileBlockchain(base_directory=blockchain_directory)
    node1 = baker.make(Node, network_addresses=['https://192.168.0.29:8555/'], identifier=key_pair1.public)
    blockchain_genesis_state.account_states[node1.identifier] = AccountState(node=node1)
    blockchain.add_blockchain_state(blockchain_genesis_state)
    blockchain.validate()

    # Block 0
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.30:8555/'], fee_amount=3, signing_key=key_pair2.private
    )
    node2 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    # Block 1
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.31:8555/'], fee_amount=3, signing_key=key_pair3.private
    )
    node3_old = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    # Block 2
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.32:8555/'], fee_amount=3, signing_key=key_pair4.private
    )
    node4 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    def sort_key(value):
        return value.network_addresses

    def sort_me(list_):
        return sorted(list_, key=sort_key)

    assert sort_me(blockchain.yield_nodes(block_number=2)) == sort_me([node4, node3_old, node2, node1])

    blockchain.snapshot_blockchain_state()

    # Block 3
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.33:8555/'], fee_amount=3, signing_key=key_pair3.private
    )
    node3 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    # Block 4
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.34:8555/'], fee_amount=3, signing_key=key_pair5.private
    )
    node5 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    # Block 5
    request = NodeDeclarationSignedChangeRequest.create(
        network_addresses=['https://192.168.0.35:8555/'], fee_amount=3, signing_key=key_pair6.private
    )
    node6 = request.message.node
    blockchain.add_block(Block.create_from_signed_change_request(blockchain, request, get_node_signing_key()))

    assert sort_me(blockchain.yield_nodes()) == sort_me([node6, node5, node3, node4, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=5)) == sort_me([node6, node5, node3, node4, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=4)) == sort_me([node5, node3, node4, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=3)) == sort_me([node3, node4, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=2)) == sort_me([node4, node3_old, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=1)) == sort_me([node3_old, node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=0)) == sort_me([node2, node1])
    assert sort_me(blockchain.yield_nodes(block_number=-1)) == sort_me([node1])
コード例 #8
0
def test_node_serializes_with_identifier_standalone():
    node = baker.make(Node)
    assert node.identifier
    serialized = node.serialize_to_dict()
    assert 'identifier' in serialized
    assert serialized['identifier'] == node.identifier