Exemple #1
0
def test_get_networks(chain_state, token_network_address):
    orig_chain_state = deepcopy(chain_state)
    token_address = factories.make_address()
    token_network_registry_empty = TokenNetworkRegistryState(
        address=factories.make_address(), token_network_list=[])
    chain_state.identifiers_to_tokennetworkregistries[
        token_network_registry_empty.address] = token_network_registry_empty
    assert get_networks(
        chain_state=chain_state,
        token_network_registry_address=token_network_registry_empty.address,
        token_address=token_address,
    ) == (token_network_registry_empty, None)

    chain_state = orig_chain_state
    token_network = TokenNetworkState(
        address=token_network_address,
        token_address=token_address,
        network_graph=TokenNetworkGraphState(
            token_network_address=token_network_address),
    )
    token_network_registry = TokenNetworkRegistryState(
        address=factories.make_address(), token_network_list=[token_network])
    assert get_networks(
        chain_state=chain_state,
        token_network_registry_address=token_network_registry.address,
        token_address=token_address,
    ) == (None, None)
    chain_state.identifiers_to_tokennetworkregistries[
        token_network_registry.address] = token_network_registry
    assert get_networks(
        chain_state=chain_state,
        token_network_registry_address=token_network_registry.address,
        token_address=token_address,
    ) == (token_network_registry, token_network)
Exemple #2
0
def test_handle_new_token_network(chain_state, token_network_address):
    token_address = factories.make_address()
    token_network = TokenNetworkState(
        address=token_network_address,
        token_address=token_address,
        network_graph=TokenNetworkGraphState(token_network_address=token_network_address),
    )
    token_network_registry_address = factories.make_address()
    state_change = ContractReceiveNewTokenNetwork(
        token_network_registry_address=token_network_registry_address,
        token_network=token_network,
        transaction_hash=factories.make_transaction_hash(),
        block_hash=factories.make_block_hash(),
        block_number=factories.make_block_number(),
    )
    transition_result = handle_contract_receive_new_token_network(
        chain_state=chain_state, state_change=state_change
    )
    new_chain_state = transition_result.new_state
    token_network_registry = new_chain_state.identifiers_to_tokennetworkregistries[
        token_network_registry_address
    ]
    assert token_network_registry.address == token_network_registry_address
    assert not transition_result.events
    assert get_networks(
        chain_state=chain_state,
        token_network_registry_address=token_network_registry_address,
        token_address=token_address,
    ) == (token_network_registry, token_network)
Exemple #3
0
def maybe_add_tokennetwork(
    chain_state: ChainState,
    token_network_registry_address: TokenNetworkRegistryAddress,
    token_network_state: TokenNetworkState,
) -> None:
    token_network_address = token_network_state.address
    token_address = token_network_state.token_address

    token_network_registry_state, token_network_state_previous = views.get_networks(
        chain_state, token_network_registry_address, token_address
    )

    if token_network_registry_state is None:
        token_network_registry_state = TokenNetworkRegistryState(
            token_network_registry_address, [token_network_state]
        )

        ids_to_payments = chain_state.identifiers_to_tokennetworkregistries
        ids_to_payments[token_network_registry_address] = token_network_registry_state

    if token_network_state_previous is None:
        token_network_registry_state.add_token_network(token_network_state)

        mapping = chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses
        mapping[token_network_address] = token_network_registry_address