Exemple #1
0
    def make_channel(
        self,
        token_address=make_address(),
        partner_address=make_address(),
        reveal_timeout=20,
        settle_timeout=800,
        balance=0,
    ):
        our_address = make_address()
        our_balance = balance
        partner_balance = balance
        our_state = ChannelEndState(our_address, our_balance, 1)
        partner_state = ChannelEndState(partner_address, partner_balance, 1)

        block_alarm = list()
        channel_for_hashlock = list()
        netting_channel = NettingChannelMock(make_address())
        external_state = ChannelExternalState(
            block_alarm.append,
            lambda *args: channel_for_hashlock.append(args),
            lambda: 1,
            netting_channel,
        )
        self.tokens.add(token_address)
        return Channel(
            our_state,
            partner_state,
            external_state,
            token_address,
            reveal_timeout,
            settle_timeout,
        )
Exemple #2
0
def channel_list_mock(num_channels):
    channel_list = []
    for _ in range(0, num_channels):
        netting_channel = NettingChannelMock()
        asset_address = make_address()
        privkey1, address1 = make_privkey_address()
        address2 = make_address()

        balance1 = random.randint(0, 2**128)
        balance2 = random.randint(0, 2**128)

        reveal_timeout = random.randint(0, 2**64)
        settle_timeout = reveal_timeout + random.randint(0, 2**64)

        our_state = ChannelEndState(address1, balance1, netting_channel.opened)
        partner_state = ChannelEndState(address2, balance2,
                                        netting_channel.opened)
        external_state = make_external_state()

        test_channel = Channel(
            our_state,
            partner_state,
            external_state,
            asset_address,
            reveal_timeout,
            settle_timeout,
        )

        channel_list.append(test_channel)

    return channel_list
Exemple #3
0
def test_invalid_timeouts():
    netting_channel = NettingChannelMock()
    token_address = make_address()
    reveal_timeout = 5
    settle_timeout = 15

    address1 = make_address()
    address2 = make_address()
    balance1 = 10
    balance2 = 10
    block_number = 10

    our_state = ChannelEndState(address1, balance1, netting_channel.opened())
    partner_state = ChannelEndState(address2, balance2,
                                    netting_channel.opened())
    external_state = make_external_state()

    # do not allow a reveal timeout larger than the settle timeout
    with pytest.raises(ValueError):
        large_reveal_timeout = 50
        small_settle_timeout = 49

        Channel(
            our_state,
            partner_state,
            external_state,
            token_address,
            large_reveal_timeout,
            small_settle_timeout,
            block_number,
        )

    for invalid_value in (-1, 0, 1.1, 1.0, 'a', [], {}):
        with pytest.raises(ValueError):
            Channel(
                our_state,
                partner_state,
                external_state,
                token_address,
                invalid_value,
                settle_timeout,
                block_number,
            )

        with pytest.raises(ValueError):
            Channel(
                our_state,
                partner_state,
                external_state,
                token_address,
                reveal_timeout,
                invalid_value,
                block_number,
            )
Exemple #4
0
def test_channel_close_called_only_once():
    class MockCheckCallsToClose():
        def __init__(self):
            self.address = 'mockcheckcallstoclosemockcheckcallstoclo'
            self.close_calls = 0

        def opened(self):
            return 1

        def closed(self):
            return 0

        def settled(self):
            return 0

        def close(self, transfer):
            self.close_calls += 1

    netting_channel = NettingChannelMock()
    token_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    reveal_timeout = 5
    settle_timeout = 15

    our_state = ChannelEndState(address1, balance1, netting_channel.opened())
    partner_state = ChannelEndState(address2, balance2, netting_channel.opened())

    channel_for_hashlock = list()
    netting_channel = MockCheckCallsToClose()

    external_state = ChannelExternalState(
        lambda *args: channel_for_hashlock.append(args),
        netting_channel,
    )

    test_channel = Channel(
        our_state,
        partner_state,
        external_state,
        token_address,
        reveal_timeout,
        settle_timeout,
    )

    test_channel.external_state.close('')
    test_channel.external_state.close('')

    assert netting_channel.close_calls == 1
Exemple #5
0
def test_invalid_timeouts():
    asset_address = make_address()
    reveal_timeout = 5
    settle_timeout = 15

    address1 = make_address()
    address2 = make_address()
    balance1 = 10
    balance2 = 10

    our_state = ChannelEndState(address1, balance1)
    partner_state = ChannelEndState(address2, balance2)
    external_state = make_external_state()

    # do not allow a reveal timeout larger than the settle timeout
    with pytest.raises(ValueError):
        large_reveal_timeout = 50
        small_settle_timeout = 49

        Channel(
            our_state,
            partner_state,
            external_state,
            asset_address,
            large_reveal_timeout,
            small_settle_timeout,
        )

    for invalid_value in (-1, 0, 1.1, 1.0, 'a', [], {}):
        with pytest.raises(ValueError):
            Channel(
                our_state,
                partner_state,
                external_state,
                asset_address,
                invalid_value,
                settle_timeout,
            )

        with pytest.raises(ValueError):
            Channel(
                our_state,
                partner_state,
                external_state,
                asset_address,
                reveal_timeout,
                invalid_value,
            )
Exemple #6
0
    def open(self,
             asset_address,
             partner_address,
             settle_timeout=None,
             reveal_timeout=None):
        existing_channel = self._get_channel_by_asset_and_partner(
            asset_address, partner_address)
        if existing_channel:
            channel = existing_channel
        else:
            netting_channel_address = make_address()  # the new channel address
            channel = Channel(netting_channel_address,
                              asset_address,
                              partner_address,
                              settle_timeout or DEFAULT_SETTLE_TIMEOUT,
                              reveal_timeout or DEFAULT_REVEAL_TIMEOUT,
                              deposit=0,
                              status='open')

            self._add_channel(channel)

            event = ChannelNew(netting_channel_address, asset_address,
                               partner_address, self.block_number)

            self._queue_event(event)

        self._mine_new_block_try()
        return channel
def test_endpointregistry(private_keys, blockchain_services):
    chain = blockchain_services.blockchain_services[0]
    my_address = privatekey_to_address(private_keys[0])

    endpointregistry_address = chain.deploy_contract(
        'EndpointRegistry',
        get_contract_path('EndpointRegistry.sol'),
    )
    discovery_proxy = chain.discovery(endpointregistry_address)

    contract_discovery = ContractDiscovery(my_address, discovery_proxy)

    unregistered_address = make_address()

    # get should raise for unregistered addresses
    with pytest.raises(KeyError):
        contract_discovery.get(my_address)

    with pytest.raises(KeyError):
        contract_discovery.get(unregistered_address)

    assert contract_discovery.nodeid_by_host_port(('127.0.0.1', 44444)) is None

    contract_discovery.register(my_address, '127.0.0.1', 44444)

    assert contract_discovery.nodeid_by_host_port(('127.0.0.1', 44444)) == my_address
    assert contract_discovery.get(my_address) == ('127.0.0.1', 44444)

    contract_discovery.register(my_address, '127.0.0.1', 88888)

    assert contract_discovery.nodeid_by_host_port(('127.0.0.1', 88888)) == my_address
    assert contract_discovery.get(my_address) == ('127.0.0.1', 88888)

    with pytest.raises(KeyError):
        contract_discovery.get(unregistered_address)
Exemple #8
0
 def deploy_and_register_asset(self,
                               contract_name,
                               contract_file,
                               constructor_parameters=None):
     new_address = make_address()
     self.default_registry.add_asset(new_address)
     return new_address
Exemple #9
0
def test_channel_must_accept_expired_locks():
    """ A node may go offline for an undetermined period of time, and when it
    comes back online it must accept the messages that are waiting, otherwise
    the partner node won't make progress with its queue.

    If a N node goes offline for a number B of blocks, and the partner does not
    close the channel, when N comes back online some of the messages from its
    partner may become expired. Neverthless these messages are ordered and must
    be accepted for the partner to make progress with its queue.

    Note: Accepting a message with an expired lock does *not* imply the token
    transfer happened, and the receiver node must *not* forward the transfer,
    only accept the message allowing the partner to progress with its message
    queue.
    """
    balance1 = 70
    balance2 = 110
    reveal_timeout = 5
    settle_timeout = 15
    privkey1, address1 = make_privkey_address()
    privkey2, address2 = make_privkey_address()
    token_address = make_address()

    netting_channel = NettingChannelMock()
    our_state = ChannelEndState(
        address1,
        balance1,
        netting_channel.opened(),
    )
    partner_state = ChannelEndState(
        address2,
        balance2,
        netting_channel.opened(),
    )
    external_state = make_external_state()

    test_channel = Channel(
        our_state,
        partner_state,
        external_state,
        token_address,
        reveal_timeout,
        settle_timeout,
    )

    block_number = 10
    transfer = make_mediated_transfer(
        nonce=test_channel.partner_state.nonce,
        token=test_channel.token_address,
        expiration=block_number + settle_timeout,
        recipient=address1,
    )
    transfer.sign(privkey2, address2)

    test_channel.register_transfer(
        block_number + settle_timeout + 1,
        transfer,
    )
Exemple #10
0
    def __init__(self, asset_address, peer1, peer2, settle_timeout,  # pylint: disable=too-many-arguments
                 address=None):
        self.address = address or make_address()

        self.contract = NettingChannelContract(
            asset_address,
            self.address,
            peer1,
            peer2,
            settle_timeout,
        )
Exemple #11
0
    def __init__(self, asset_address, peer1, peer2, settle_timeout, address=None):
        # pylint: disable=too-many-arguments

        self.address = address or make_address()

        self.contract = NettingChannelContract(
            asset_address,
            self.address,
            peer1,
            peer2,
            settle_timeout,
        )
Exemple #12
0
def make_external_state():
    block_alarm = list()
    channel_for_hashlock = list()
    netting_channel = NettingChannelMock()
    netting_channel.address = make_address()

    external_state = ChannelExternalState(
        block_alarm.append,
        lambda *args: channel_for_hashlock.append(args),
        lambda: 1,
        netting_channel,
    )

    return external_state
Exemple #13
0
def test_endpointregistry(blockchain_services, poll_timeout):
    chain = blockchain_services[0]
    my_address = chain.node_address

    # deploy discovery contract
    discovery_contract_path = get_contract_path('EndpointRegistry.sol')
    discovery_contracts = _solidity.compile_file(discovery_contract_path,
                                                 libraries=dict())

    endpoinregistry_proxy = chain.client.deploy_solidity_contract(
        my_address,
        'EndpointRegistry',
        discovery_contracts,
        dict(),
        tuple(),
        timeout=poll_timeout,
    )

    endpointregistry_address = endpoinregistry_proxy.address
    contract_discovery = ContractDiscovery(
        chain,
        endpointregistry_address,
    )

    unregistered_address = make_address()

    # get should raise for unregistered addresses
    with pytest.raises(KeyError):
        contract_discovery.get(my_address)

    with pytest.raises(KeyError):
        contract_discovery.get(unregistered_address)

    assert contract_discovery.nodeid_by_host_port(('127.0.0.1', 44444)) is None

    contract_discovery.register(my_address, '127.0.0.1', 44444)

    assert contract_discovery.nodeid_by_host_port(
        ('127.0.0.1', 44444)) == my_address
    assert contract_discovery.get(my_address) == ('127.0.0.1', 44444)

    contract_discovery.register(my_address, '127.0.0.1', 88888)

    assert contract_discovery.nodeid_by_host_port(
        ('127.0.0.1', 88888)) == my_address
    assert contract_discovery.get(my_address) == ('127.0.0.1', 88888)

    with pytest.raises(KeyError):
        contract_discovery.get(unregistered_address)
Exemple #14
0
def test_mock_registry_api_compliance():
    address = make_address()
    contract_discovery_instance = Discovery()

    # `get` for unknown address raises
    with pytest.raises(KeyError):
        contract_discovery_instance.get(address)

    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 44444)) is None

    # `update_endpoint` and 'classic' `register` do the same
    contract_discovery_instance.register(address, '127.0.0.1', 44444)
    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 44444)) == address
    assert contract_discovery_instance.get(address) == ('127.0.0.1', 44444)

    # `register`ing twice does update do the same
    contract_discovery_instance.register(address, '127.0.0.1', 88888)
    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 88888)) == address
    assert contract_discovery_instance.get(address) == ('127.0.0.1', 88888)
Exemple #15
0
def test_mock_registry_api_compliance():
    address = make_address()
    contract_discovery_instance = Discovery()

    # `get` for unknown address raises
    with pytest.raises(InvalidAddress):
        contract_discovery_instance.get(address)

    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 44444)) is None

    # `update_endpoint` and 'classic' `register` do the same
    contract_discovery_instance.register(address, '127.0.0.1', 44444)
    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 44444)) == address
    assert contract_discovery_instance.get(address) == ('127.0.0.1', 44444)

    # `register`ing twice does update do the same
    contract_discovery_instance.register(address, '127.0.0.1', 88888)
    assert contract_discovery_instance.nodeid_by_host_port(('127.0.0.1', 88888)) == address
    assert contract_discovery_instance.get(address) == ('127.0.0.1', 88888)
Exemple #16
0
def test_python_channel():
    netting_channel = NettingChannelMock()
    token_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    reveal_timeout = 5
    settle_timeout = 15

    our_state = ChannelEndState(address1, balance1, netting_channel.opened)
    partner_state = ChannelEndState(address2, balance2, netting_channel.opened)
    external_state = make_external_state()

    test_channel = Channel(
        our_state,
        partner_state,
        external_state,
        token_address,
        reveal_timeout,
        settle_timeout,
    )

    assert test_channel.contract_balance == our_state.contract_balance
    assert test_channel.balance == our_state.balance(partner_state)
    assert test_channel.transferred_amount == our_state.transferred_amount
    assert test_channel.distributable == our_state.distributable(partner_state)
    assert test_channel.outstanding == our_state.locked()
    assert test_channel.outstanding == 0
    assert test_channel.locked == partner_state.locked()
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0

    with pytest.raises(ValueError):
        test_channel.create_directtransfer(
            -10,
            1  # TODO: fill in identifier
        )

    with pytest.raises(ValueError):
        test_channel.create_directtransfer(
            balance1 + 10,
            1  # TODO: fill in identifier
        )

    amount1 = 10
    directtransfer = test_channel.create_directtransfer(
        amount1,
        1  # TODO: fill in identifier
    )
    directtransfer.sign(privkey1, address1)
    test_channel.register_transfer(directtransfer)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1
    assert test_channel.transferred_amount == amount1
    assert test_channel.distributable == balance1 - amount1
    assert test_channel.outstanding == 0
    assert test_channel.locked == 0
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0

    secret = sha3('test_channel')
    hashlock = sha3(secret)
    amount2 = 10
    fee = 0
    expiration = settle_timeout - 5
    mediatedtransfer = test_channel.create_mediatedtransfer(
        address1,
        address2,
        fee,
        amount2,
        1,  # TODO: fill in identifier
        expiration,
        hashlock,
    )
    mediatedtransfer.sign(privkey1, address1)

    test_channel.register_transfer(mediatedtransfer)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1
    assert test_channel.transferred_amount == amount1
    assert test_channel.distributable == balance1 - amount1 - amount2
    assert test_channel.outstanding == 0
    assert test_channel.locked == amount2
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == amount2

    test_channel.release_lock(secret)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1 - amount2
    assert test_channel.transferred_amount == amount1 + amount2
    assert test_channel.distributable == balance1 - amount1 - amount2
    assert test_channel.outstanding == 0
    assert test_channel.locked == 0
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0
Exemple #17
0
def test_channel():
    class NettingChannelMock(object):
        def opened(self):
            return 1

        def closed(self):
            return 0

        def settled(self):
            return 0

    asset_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    reveal_timeout = 5
    settle_timeout = 15

    our_state = ChannelEndState(address1, balance1)
    partner_state = ChannelEndState(address2, balance2)

    block_alarm = list()
    channel_for_hashlock = list()
    netting_channel = NettingChannelMock()

    external_state = ChannelExternalState(
        block_alarm.append,
        lambda *args: channel_for_hashlock.append(args),
        lambda: 1,
        netting_channel,
    )

    channel = Channel(
        our_state,
        partner_state,
        external_state,
        asset_address,
        reveal_timeout,
        settle_timeout,
    )

    assert channel.contract_balance == our_state.contract_balance
    assert channel.balance == our_state.balance(partner_state)
    assert channel.transfered_amount == our_state.transfered_amount
    assert channel.distributable == our_state.distributable(partner_state)
    assert channel.outstanding == our_state.locked()
    assert channel.outstanding == 0
    assert channel.locked == partner_state.locked()
    assert channel.our_state.locked() == 0
    assert channel.partner_state.locked() == 0

    with pytest.raises(ValueError):
        channel.create_directtransfer(-10)

    with pytest.raises(ValueError):
        channel.create_directtransfer(balance1 + 10)

    amount1 = 10
    directtransfer = channel.create_directtransfer(amount1)
    directtransfer.sign(privkey1, address1)
    channel.register_transfer(directtransfer)

    assert channel.contract_balance == balance1
    assert channel.balance == balance1 - amount1
    assert channel.transfered_amount == amount1
    assert channel.distributable == balance1 - amount1
    assert channel.outstanding == 0
    assert channel.locked == 0
    assert channel.our_state.locked() == 0
    assert channel.partner_state.locked() == 0

    secret = sha3('test_channel')
    hashlock = sha3(secret)
    amount2 = 10
    fee = 0
    expiration = settle_timeout - 5
    mediatedtransfer = channel.create_mediatedtransfer(
        address1,
        address2,
        fee,
        amount2,
        expiration,
        hashlock,
    )
    mediatedtransfer.sign(privkey1, address1)

    channel.register_transfer(mediatedtransfer)

    assert channel.contract_balance == balance1
    assert channel.balance == balance1 - amount1
    assert channel.transfered_amount == amount1
    assert channel.distributable == balance1 - amount1 - amount2
    assert channel.outstanding == 0
    assert channel.locked == amount2
    assert channel.our_state.locked() == 0
    assert channel.partner_state.locked() == amount2

    channel.claim_lock(secret)

    assert channel.contract_balance == balance1
    assert channel.balance == balance1 - amount1 - amount2
    assert channel.transfered_amount == amount1 + amount2
    assert channel.distributable == balance1 - amount1 - amount2
    assert channel.outstanding == 0
    assert channel.locked == 0
    assert channel.our_state.locked() == 0
    assert channel.partner_state.locked() == 0
Exemple #18
0
 def deploy_and_register_asset(self, contract_name, contract_file, constructor_parameters=None):
     new_address = make_address()
     self.default_registry.add_asset(new_address)
     return new_address
Exemple #19
0
    def __init__(self, address=None):
        self.address = address or make_address()

        self.asset_manager = dict()
        self.address_asset = dict()
Exemple #20
0
    def __init__(self, asset_address, address=None):
        self.address = address or make_address()

        self.asset_address_ = asset_address
        self.pair_channel = dict()
        self.participant_channels = defaultdict(list)
Exemple #21
0
 def __init__(self, address=None):
     self.address = address or make_address()
     self.address_endpoint = dict()
     self.endpoint_address = dict()
Exemple #22
0
 def register_token(self, token_address):
     self.tokens.add(token_address)
     manager_address = make_address()
     self.tokens_to_manager_address[token_address] = manager_address
     return manager_address
Exemple #23
0
def test_end_state():
    asset_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    lock_secret = sha3('test_end_state')
    lock_amount = 30
    lock_expiration = 10
    lock_hashlock = sha3(lock_secret)

    state1 = ChannelEndState(address1, balance1)
    state2 = ChannelEndState(address2, balance2)

    assert state1.contract_balance == balance1
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == 0

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == ''

    lock = Lock(
        lock_amount,
        lock_expiration,
        lock_hashlock,
    )
    lock_hash = sha3(lock.as_bytes)

    transferred_amount = 0
    locksroot = state2.compute_merkleroot_with(lock)

    locked_transfer = LockedTransfer(
        1,  # TODO: fill in identifier
        nonce=state1.nonce,
        asset=asset_address,
        transferred_amount=transferred_amount,
        recipient=state2.address,
        locksroot=locksroot,
        lock=lock,
    )

    transfer_target = make_address()
    transfer_initiator = make_address()
    fee = 0
    mediated_transfer = locked_transfer.to_mediatedtransfer(
        transfer_target,
        transfer_initiator,
        fee,
    )
    mediated_transfer.sign(privkey1, address1)

    state2.register_locked_transfer(mediated_transfer)

    assert state1.contract_balance == balance1
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is True

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    with pytest.raises(ValueError):
        state1.update_contract_balance(balance1 - 10)

    state1.update_contract_balance(balance1 + 10)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount + 10
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is True

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    # registering the secret should not change the locked amount
    state2.register_secret(lock_secret)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount + 10
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    state2.release_lock(state1, lock_secret)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10 - lock_amount
    assert state2.balance(state1) == balance2 + lock_amount

    assert state1.distributable(state2) == balance1 + 10 - lock_amount
    assert state2.distributable(state1) == balance2 + lock_amount

    assert state1.locked() == 0
    assert state2.locked() == 0

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == ''
Exemple #24
0
    def __init__(self, address=None):
        self.address = address or make_address()

        self.token_manager = dict()
        self.address_token = dict()
Exemple #25
0
def test_python_channel():
    asset_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    reveal_timeout = 5
    settle_timeout = 15

    our_state = ChannelEndState(address1, balance1)
    partner_state = ChannelEndState(address2, balance2)
    external_state = make_external_state()

    test_channel = Channel(
        our_state, partner_state, external_state,
        asset_address, reveal_timeout, settle_timeout,
    )

    assert test_channel.contract_balance == our_state.contract_balance
    assert test_channel.balance == our_state.balance(partner_state)
    assert test_channel.transferred_amount == our_state.transferred_amount
    assert test_channel.distributable == our_state.distributable(partner_state)
    assert test_channel.outstanding == our_state.locked()
    assert test_channel.outstanding == 0
    assert test_channel.locked == partner_state.locked()
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0

    with pytest.raises(ValueError):
        test_channel.create_directtransfer(
            -10,
            1  # TODO: fill in identifier
        )

    with pytest.raises(ValueError):
        test_channel.create_directtransfer(
            balance1 + 10,
            1  # TODO: fill in identifier
        )

    amount1 = 10
    directtransfer = test_channel.create_directtransfer(
        amount1,
        1  # TODO: fill in identifier
    )
    directtransfer.sign(privkey1, address1)
    test_channel.register_transfer(directtransfer)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1
    assert test_channel.transferred_amount == amount1
    assert test_channel.distributable == balance1 - amount1
    assert test_channel.outstanding == 0
    assert test_channel.locked == 0
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0

    secret = sha3('test_channel')
    hashlock = sha3(secret)
    amount2 = 10
    fee = 0
    expiration = settle_timeout - 5
    mediatedtransfer = test_channel.create_mediatedtransfer(
        address1,
        address2,
        fee,
        amount2,
        1,  # TODO: fill in identifier
        expiration,
        hashlock,
    )
    mediatedtransfer.sign(privkey1, address1)

    test_channel.register_transfer(mediatedtransfer)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1
    assert test_channel.transferred_amount == amount1
    assert test_channel.distributable == balance1 - amount1 - amount2
    assert test_channel.outstanding == 0
    assert test_channel.locked == amount2
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == amount2

    test_channel.release_lock(secret)

    assert test_channel.contract_balance == balance1
    assert test_channel.balance == balance1 - amount1 - amount2
    assert test_channel.transferred_amount == amount1 + amount2
    assert test_channel.distributable == balance1 - amount1 - amount2
    assert test_channel.outstanding == 0
    assert test_channel.locked == 0
    assert test_channel.our_state.locked() == 0
    assert test_channel.partner_state.locked() == 0
Exemple #26
0
 def __init__(self, address=None):
     self.address = address or make_address()
     self.contract_allowance = defaultdict(int)
Exemple #27
0
    def __init__(self, address=None):
        self.address = address or make_address()

        self.asset_manager = dict()
        self.address_asset = dict()
Exemple #28
0
def test_end_state():
    netting_channel = NettingChannelMock()
    token_address = make_address()
    privkey1, address1 = make_privkey_address()
    address2 = make_address()

    balance1 = 70
    balance2 = 110

    lock_secret = sha3('test_end_state')
    lock_amount = 30
    lock_expiration = 10
    lock_hashlock = sha3(lock_secret)

    state1 = ChannelEndState(address1, balance1, netting_channel.opened)
    state2 = ChannelEndState(address2, balance2, netting_channel.opened)

    assert state1.contract_balance == balance1
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == 0

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == ''

    lock = Lock(
        lock_amount,
        lock_expiration,
        lock_hashlock,
    )
    lock_hash = sha3(lock.as_bytes)

    transferred_amount = 0
    locksroot = state2.compute_merkleroot_with(lock)

    locked_transfer = LockedTransfer(
        1,  # TODO: fill in identifier
        nonce=state1.nonce,
        token=token_address,
        transferred_amount=transferred_amount,
        recipient=state2.address,
        locksroot=locksroot,
        lock=lock,
    )

    transfer_target = make_address()
    transfer_initiator = make_address()
    fee = 0
    mediated_transfer = locked_transfer.to_mediatedtransfer(
        transfer_target,
        transfer_initiator,
        fee,
    )
    mediated_transfer.sign(privkey1, address1)

    state2.register_locked_transfer(mediated_transfer)

    assert state1.contract_balance == balance1
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is True

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    with pytest.raises(ValueError):
        state1.update_contract_balance(balance1 - 10)

    state1.update_contract_balance(balance1 + 10)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount + 10
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is True

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    # registering the secret should not change the locked amount
    state2.register_secret(lock_secret)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10
    assert state2.balance(state1) == balance2

    assert state1.distributable(state2) == balance1 - lock_amount + 10
    assert state2.distributable(state1) == balance2

    assert state1.locked() == 0
    assert state2.locked() == lock_amount

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash

    state2.release_lock(state1, lock_secret)

    assert state1.contract_balance == balance1 + 10
    assert state2.contract_balance == balance2
    assert state1.balance(state2) == balance1 + 10 - lock_amount
    assert state2.balance(state1) == balance2 + lock_amount

    assert state1.distributable(state2) == balance1 + 10 - lock_amount
    assert state2.distributable(state1) == balance2 + lock_amount

    assert state1.locked() == 0
    assert state2.locked() == 0

    assert state1.balance_proof.is_pending(lock_hashlock) is False
    assert state2.balance_proof.is_pending(lock_hashlock) is False

    assert state1.balance_proof.merkleroot_for_unclaimed() == ''
    assert state2.balance_proof.merkleroot_for_unclaimed() == ''
Exemple #29
0
 def __init__(self, address=None):
     self.address = address or make_address()
     self.contract_allowance = defaultdict(int)
Exemple #30
0
 def __init__(self, address=None):
     self.address = address or make_address()
     self.address_endpoint = dict()
     self.endpoint_address = dict()
Exemple #31
0
    def __init__(self, asset_address, address=None):
        self.address = address or make_address()

        self.asset_address_ = asset_address
        self.pair_channel = dict()
        self.participant_channels = defaultdict(list)