Beispiel #1
0
    def add_token_network_listener(
            self,
            token_network_proxy,
            from_block: typing.BlockSpecification = 'latest',
    ):
        channel_new_filter = token_network_proxy.channelnew_filter(from_block=from_block)
        token_network_address = token_network_proxy.address

        self.add_event_listener(
            'TokenNetwork {}'.format(pex(token_network_address)),
            channel_new_filter,
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_TOKEN_NETWORK),
        )
Beispiel #2
0
    def add_registry_listener(
            self,
            registry_proxy,
            from_block: typing.BlockSpecification = 'latest',
    ):
        tokenadded = registry_proxy.tokenadded_filter(from_block)
        registry_address = registry_proxy.address

        self.add_event_listener(
            'Registry {}'.format(pex(registry_address)),
            tokenadded,
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_REGISTRY),
        )
Beispiel #3
0
    def add_netting_channel_listener(
            self,
            netting_channel_proxy,
            from_block: typing.BlockSpecification = 'latest',
    ):
        netting_channel_events = netting_channel_proxy.all_events_filter(from_block)
        channel_address = netting_channel_proxy.address

        self.add_event_listener(
            'NettingChannel Event {}'.format(pex(channel_address)),
            netting_channel_events,
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_NETTING_CHANNEL),
        )
Beispiel #4
0
    def add_channel_manager_listener(
            self,
            channel_manager_proxy,
            from_block: typing.BlockSpecification = 'latest',
    ):
        channelnew = channel_manager_proxy.channelnew_filter(from_block)
        manager_address = channel_manager_proxy.address

        self.add_event_listener(
            'ChannelManager {}'.format(pex(manager_address)),
            channelnew,
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER),
        )
Beispiel #5
0
def get_all_registry_events(
        chain: BlockChainService,
        registry_address: Address,
        events: List[str] = ALL_EVENTS,
        from_block: BlockSpecification = 0,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    """ Helper to get all events of the Registry contract at `registry_address`. """
    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_REGISTRY),
        registry_address,
        events,
        from_block,
        to_block,
    )
Beispiel #6
0
def get_all_registry_events(chain,
                            registry_address,
                            events=ALL_EVENTS,
                            from_block=0,
                            to_block='latest'):
    """ Helper to get all events of the Registry contract at
    `registry_address`.
    """
    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_REGISTRY),
        registry_address,
        events,
        from_block,
        to_block,
    )
Beispiel #7
0
def get_all_channel_manager_events(
        chain: BlockChainService,
        channel_manager_address: Address,
        events: List[str] = ALL_EVENTS,
        from_block: BlockSpecification = 0,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    """ Helper to get all events of the ChannelManagerContract at `token_address`. """

    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER),
        channel_manager_address,
        events,
        from_block,
        to_block,
    )
Beispiel #8
0
def get_all_netting_channel_events(
        chain: BlockChainService,
        netting_channel_address: Address,
        events: List[str] = ALL_EVENTS,
        from_block: BlockSpecification = 0,
        to_block: BlockSpecification = 'latest',
) -> List[Dict]:
    """ Helper to get all events of a NettingChannelContract. """

    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_NETTING_CHANNEL),
        netting_channel_address,
        events,
        from_block,
        to_block,
    )
Beispiel #9
0
def get_all_channel_manager_events(chain,
                                   channel_manager_address,
                                   events=ALL_EVENTS,
                                   from_block=0,
                                   to_block='latest'):
    """ Helper to get all events of the ChannelManagerContract at
    `token_address`.
    """

    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER),
        channel_manager_address,
        events,
        from_block,
        to_block,
    )
Beispiel #10
0
def get_all_netting_channel_events(chain,
                                   netting_channel_address,
                                   events=ALL_EVENTS,
                                   from_block=0,
                                   to_block='latest'):
    """ Helper to get all events of a NettingChannelContract at
    `channel_identifier`.
    """

    return get_contract_events(
        chain,
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_NETTING_CHANNEL),
        netting_channel_address,
        events,
        from_block,
        to_block,
    )
Beispiel #11
0
    def __init__(
        self,
        jsonrpc_client,
        token_address,
    ):
        contract = jsonrpc_client.new_contract(
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_HUMAN_STANDARD_TOKEN),
            to_normalized_address(token_address),
        )
        self.proxy = ContractProxy(jsonrpc_client, contract)

        if not is_binary_address(token_address):
            raise ValueError('token_address must be a valid address')

        check_address_has_code(jsonrpc_client, token_address, 'Token')

        self.address = token_address
        self.client = jsonrpc_client
Beispiel #12
0
    def __init__(self,
                 jsonrpc_client,
                 token_address,
                 poll_timeout=DEFAULT_POLL_TIMEOUT):

        if not isaddress(token_address):
            raise ValueError('token_address must be a valid address')

        check_address_has_code(jsonrpc_client, token_address, 'Token')

        proxy = jsonrpc_client.new_contract_proxy(
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_HUMAN_STANDARD_TOKEN),
            address_encoder(token_address),
        )

        self.address = token_address
        self.proxy = proxy
        self.client = jsonrpc_client
        self.poll_timeout = poll_timeout
Beispiel #13
0
    def __init__(self,
                 jsonrpc_client,
                 discovery_address,
                 poll_timeout=DEFAULT_POLL_TIMEOUT):

        if not isaddress(discovery_address):
            raise ValueError('discovery_address must be a valid address')

        check_address_has_code(jsonrpc_client, discovery_address, 'Discovery')

        proxy = jsonrpc_client.new_contract_proxy(
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_ENDPOINT_REGISTRY),
            address_encoder(discovery_address),
        )
        CONTRACT_MANAGER.check_contract_version(
            proxy.call('contract_version').decode(),
            CONTRACT_ENDPOINT_REGISTRY)

        self.address = discovery_address
        self.proxy = proxy
        self.client = jsonrpc_client
        self.poll_timeout = poll_timeout
        self.not_found_address = NULL_ADDRESS
Beispiel #14
0
    def __init__(
            self,
            jsonrpc_client,
            channel_address,
            poll_timeout=DEFAULT_POLL_TIMEOUT):

        self.address = channel_address
        self.client = jsonrpc_client
        self.poll_timeout = poll_timeout
        # Prevents concurrent deposit, close, or settle operations on the same channel
        self.channel_operations_lock = RLock()
        self.client = jsonrpc_client
        self.node_address = privatekey_to_address(self.client.privkey)
        self.proxy = jsonrpc_client.new_contract_proxy(
            CONTRACT_MANAGER.get_contract_abi(CONTRACT_NETTING_CHANNEL),
            address_encoder(channel_address),
        )
        CONTRACT_MANAGER.check_contract_version(
            self.proxy.call('contract_version').decode(),
            CONTRACT_NETTING_CHANNEL)

        # check we are a participant of the given channel
        self.detail()
        self._check_exists()
Beispiel #15
0
def test_blockchain(
        init_blockchain,
        web3,
        blockchain_rpc_ports,
        private_keys,
        poll_timeout):
    # pylint: disable=too-many-locals

    addresses = [
        privatekey_to_address(priv)
        for priv in private_keys
    ]

    privatekey = private_keys[0]
    address = privatekey_to_address(privatekey)
    total_token = 100

    host = '127.0.0.1'
    jsonrpc_client = JSONRPCClient(
        host,
        blockchain_rpc_ports[0],
        privatekey,
        web3=web3,
    )

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_files_cwd([humantoken_path])
    token_proxy = jsonrpc_client.deploy_solidity_contract(
        'HumanStandardToken',
        humantoken_contracts,
        list(),
        (total_token, 'raiden', 2, 'Rd'),
        contract_path=humantoken_path,
        timeout=poll_timeout,
    )
    token_proxy = Token(jsonrpc_client, to_canonical_address(token_proxy.contract.address))

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_files_cwd([registry_path])
    registry_proxy = jsonrpc_client.deploy_solidity_contract(
        'Registry',
        registry_contracts,
        list(),
        tuple(),
        contract_path=registry_path,
        timeout=poll_timeout,
    )
    registry_proxy = Registry(
        jsonrpc_client,
        to_canonical_address(registry_proxy.contract.address),
    )

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert not log_list

    assert token_proxy.balance_of(address) == total_token
    manager_address = registry_proxy.add_token(
        to_canonical_address(token_proxy.proxy.contract.address),
    )
    assert is_address(manager_address)
    assert len(registry_proxy.token_addresses()) == 1

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_proxy.manager_address_by_token(
        token_proxy.proxy.contract.address,
    )
    channel_manager_address = to_canonical_address(channel_manager_address_encoded)

    log = log_list[0]
    event = decode_event(CONTRACT_MANAGER.get_contract_abi(CONTRACT_REGISTRY), log)
    event_args = event['args']

    assert channel_manager_address == to_canonical_address(event_args['channel_manager_address'])
    assert is_same_address(token_proxy.proxy.contract.address, event_args['token_address'])

    channel_manager_proxy = jsonrpc_client.new_contract_proxy(
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER),
        channel_manager_address,
    )
    channel_manager_proxy = ChannelManager(
        jsonrpc_client,
        to_canonical_address(channel_manager_proxy.contract.address),
    )

    channel_address = channel_manager_proxy.new_netting_channel(
        addresses[1],
        10,
    )
    assert is_address(channel_address)

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2
Beispiel #16
0
def test_blockchain(
        blockchain_backend,  # required to start the geth backend pylint: disable=unused-argument
        blockchain_rpc_ports,
        private_keys,
        poll_timeout):
    # pylint: disable=too-many-locals

    addresses = [privatekey_to_address(priv) for priv in private_keys]

    privatekey = private_keys[0]
    address = privatekey_to_address(privatekey)
    total_token = 100

    host = '127.0.0.1'
    jsonrpc_client = JSONRPCClient(
        host,
        blockchain_rpc_ports[0],
        privatekey,
    )

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_files_cwd([humantoken_path])
    token_proxy = jsonrpc_client.deploy_solidity_contract(
        'HumanStandardToken',
        humantoken_contracts,
        list(),
        (total_token, 'raiden', 2, 'Rd'),
        contract_path=humantoken_path,
        timeout=poll_timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_files_cwd([registry_path])
    registry_proxy = jsonrpc_client.deploy_solidity_contract(
        'Registry',
        registry_contracts,
        list(),
        tuple(),
        contract_path=registry_path,
        timeout=poll_timeout,
    )

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        }, )
    assert not log_list

    assert token_proxy.call('balanceOf', address) == total_token
    transaction_hash = registry_proxy.transact(
        'addToken',
        address,
        token_proxy.contract_address,
    )
    jsonrpc_client.poll(unhexlify(transaction_hash), timeout=poll_timeout)

    assert len(registry_proxy.call('tokenAddresses')) == 1

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        }, )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_proxy.call(
        'channelManagerByToken',
        token_proxy.contract_address,
    )
    channel_manager_address = to_canonical_address(
        channel_manager_address_encoded)

    log = log_list[0]
    event = registry_proxy.decode_event(log)
    event_args = event['args']

    assert channel_manager_address == to_canonical_address(
        event_args['channel_manager_address'])
    assert token_proxy.contract_address == to_canonical_address(
        event_args['token_address'])

    channel_manager_proxy = jsonrpc_client.new_contract_proxy(
        CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER),
        channel_manager_address,
    )

    transaction_hash = channel_manager_proxy.transact(
        'newChannel',
        addresses[1],
        10,
    )
    jsonrpc_client.poll(unhexlify(transaction_hash), timeout=poll_timeout)

    log_list = jsonrpc_client.web3.eth.getLogs(
        {
            'fromBlock': 0,
            'toBlock': 'latest',
            'topics': [],
        }, )
    assert len(log_list) == 2