Beispiel #1
0
def test_connection_issues() -> None:
    # 641 is a port registered with IANA for this service:
    #
    # repcmd            641/tcp
    # repcmd            641/udp
    #
    # This is a comcast utility agent that is unlikely to be running. The numbe
    # was chosen with. `sum(ord(c) for c in 'random')`

    web3 = Web3(HTTPProvider("http://localhost:641"))

    with pytest.raises(RequestsConnectionError):
        JSONRPCClient(web3=web3, privkey=make_privatekey_bin())
Beispiel #2
0
def main(output_directory, network_id, eth_rpc_endpoint, contracts_version):
    web3 = Web3(HTTPProvider(rpc_normalized_endpoint(eth_rpc_endpoint)))

    try:
        check_ethereum_client_is_supported(web3)
    except ConnectionError:
        click.secho(
            f"Couldn't connect to the ethereum node, double check it is running "
            f"on {eth_rpc_endpoint}, this option can be changed with "
            f"{ETH_RPC_CONFIG_OPTION}",
            fg="red",
        )
        return

    check_ethereum_network_id(network_id, web3)

    # This script does not send any transactions, the privatekey is generate
    # just because it is a dependency for JSONRPCClient.
    unecessary_privatekey = factories.make_privatekey_bin()
    rpc_client = JSONRPCClient(web3=web3, privkey=unecessary_privatekey)
    check_synced(rpc_client)

    deployment_data = get_contracts_deployment_info(chain_id=network_id, version=contracts_version)

    if not deployment_data:
        raise RuntimeError(
            f"There is no deployment data available for contracts-version {contracts_version}."
        )

    network_name = ID_TO_CHAINNAME.get(network_id)
    if network_name is None:
        raise RuntimeError(f"Network with id {network_id} is not known.")

    contracts = deployment_data["contracts"]
    token_network_registry_deployed_at = BlockNumber(
        contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["block_number"]
    )
    token_network_registry_address = TokenNetworkRegistryAddress(
        to_canonical_address(contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["address"])
    )
    secret_registry_address = SecretRegistryAddress(
        to_canonical_address(contracts[CONTRACT_SECRET_REGISTRY]["address"])
    )

    contracts_path = contracts_precompiled_path(contracts_version)
    contract_manager = ContractManager(contracts_path)

    current_block_number = rpc_client.block_number()
    confirmed_block = rpc_client.get_block(
        BlockNumber(current_block_number - DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS)
    )

    all_events_for_a_deployment = fetch_all_events_for_a_deployment(
        contract_manager=contract_manager,
        web3=web3,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=secret_registry_address,
        start_block=token_network_registry_deployed_at,
        target_block=confirmed_block["number"],
    )

    target_block_formatted = to_hex(confirmed_block["hash"])
    file_path = os.path.join(
        output_directory,
        (
            f"{network_name}-"
            f"{to_checksum_address(token_network_registry_address)}-"
            f"{target_block_formatted}.json.gz"
        ),
    )

    block_data = {
        "gasLimit": confirmed_block["gasLimit"],
        "gasUsed": confirmed_block["gasUsed"],
        "hash": to_hex(confirmed_block["hash"]),
        "number": confirmed_block["number"],
    }
    block_data_formatted = simplejson.dumps(block_data).encode("utf8")

    with gzip.open(file_path, mode="wb") as handler:
        # Format is `jsonlines` (http://jsonlines.org/), this is used because we
        # don't have to keep all the events in memory to start encoding the data.
        for event in all_events_for_a_deployment:
            format_event_for_serialization(event)
            data = simplejson.dumps(event).encode("utf8")
            handler.write(data + b"\n")

        # Write the block details at the end
        handler.write(block_data_formatted + b"\n")
Beispiel #3
0
def test_subdispatch_by_canonical_id(chain_state):
    our_model, _ = create_model(balance=10, num_pending_locks=1)
    partner_model, _ = create_model(balance=0, num_pending_locks=0)
    channel_state = create_channel_from_models(
        our_model, partner_model, factories.make_privatekey_bin()
    )
    canonical_identifier = channel_state.canonical_identifier
    token_network = TokenNetworkState(
        address=canonical_identifier.token_network_address,
        token_address=factories.make_address(),
        network_graph=TokenNetworkGraphState(
            token_network_address=channel_state.token_network_address
        ),
    )
    token_network.partneraddresses_to_channelidentifiers[
        partner_model.participant_address
    ] = canonical_identifier.channel_identifier
    token_network.channelidentifiers_to_channels[
        canonical_identifier.channel_identifier
    ] = channel_state
    token_network_registry = TokenNetworkRegistryState(
        address=factories.make_address(), token_network_list=[token_network]
    )
    chain_state.identifiers_to_tokennetworkregistries[
        token_network_registry.address
    ] = token_network_registry
    chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses[
        canonical_identifier.token_network_address
    ] = token_network_registry.address
    # dispatching a Block will be ignored
    previous_state = deepcopy(chain_state)
    state_change = Block(
        block_number=chain_state.block_number,
        gas_limit=GAS_LIMIT,
        block_hash=chain_state.block_hash,
    )
    transition_result = subdispatch_by_canonical_id(
        chain_state=chain_state,
        canonical_identifier=canonical_identifier,
        state_change=state_change,
    )
    assert transition_result.new_state == previous_state
    assert transition_result.events == []

    state_change = ActionChannelClose(canonical_identifier=canonical_identifier)

    # dispatching for an unknown canonical_identifier will not emit events
    transition_result = subdispatch_by_canonical_id(
        chain_state=chain_state,
        canonical_identifier=CanonicalIdentifier(
            chain_identifier=chain_state.chain_id,
            token_network_address=factories.make_address(),
            channel_identifier=factories.make_channel_identifier(),
        ),
        state_change=state_change,
    )
    assert not transition_result.events, transition_result

    assert get_status(channel_state) == ChannelState.STATE_OPENED
    transition_result = subdispatch_by_canonical_id(
        chain_state=chain_state,
        canonical_identifier=canonical_identifier,
        state_change=state_change,
    )

    assert get_status(channel_state) == ChannelState.STATE_CLOSING
    assert transition_result.new_state == chain_state, transition_result