Exemplo n.º 1
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,
        )
Exemplo n.º 2
0
    def __init__(self, blockchain, asset_address, peer1, peer2, settle_timeout,  # pylint: disable=too-many-arguments
                 address=None):
        self.address = address or make_address()
        self.blockchain = blockchain

        self.contract = NettingChannelContract(
            asset_address,
            self.address,
            peer1,
            peer2,
            settle_timeout,
        )

        self.newbalance_filters = list()
        self.secretrevealed_filters = list()
        self.channelclose_filters = list()
        self.channelsettle_filters = list()
Exemplo n.º 3
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,
        )
Exemplo n.º 4
0
    def new_netting_contract(self, asset_address, peer1, peer2):
        """ Creates a new netting contract between peer1 and peer2.

        Raises:
            ValueError: If peer1 or peer2 is not a valid address.
        """
        if not isaddress(peer1):
            raise ValueError('The pee1 must be a valid address')

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

        netcontract_address = bytes(sha3(peer1 + peer2)[:20])
        hash_channel = self.asset_hashchannel[asset_address]

        if netcontract_address in hash_channel:
            raise ValueError('netting contract already exists')

        channel = NettingChannelContract(asset_address, netcontract_address,
                                         peer1, peer2)
        hash_channel[netcontract_address] = channel
        return netcontract_address
Exemplo n.º 5
0
class NettingChannelMock(object):
    def __init__(self, blockchain, asset_address, peer1, peer2, settle_timeout,  # pylint: disable=too-many-arguments
                 address=None):
        self.address = address or make_address()
        self.blockchain = blockchain

        self.contract = NettingChannelContract(
            asset_address,
            self.address,
            peer1,
            peer2,
            settle_timeout,
        )

        self.newbalance_filters = list()
        self.secretrevealed_filters = list()
        self.channelclose_filters = list()
        self.channelsettle_filters = list()

    def asset_address(self):
        return self.contract.asset_address

    def settle_timeout(self):
        return self.contract.settle_timeout

    def isopen(self):
        return self.contract.isopen

    def partner(self, our_address):
        return self.contract.partner(our_address)

    def deposit(self, our_address, amount):
        self.contract.deposit(our_address, amount, self.blockchain.block_number())

    def opened(self):
        return self.contract.opened

    def closed(self):
        return self.contract.closed

    def settled(self):
        return self.contract.settled

    def detail(self, our_address):
        partner_address = self.contract.partner(our_address)

        our_balance = self.contract.participants[our_address].deposit
        partner_balance = self.contract.participants[partner_address].deposit

        return {
            'our_address': our_address,
            'our_balance': our_balance,
            'partner_address': partner_address,
            'partner_balance': partner_balance,
            'settle_timeout': self.contract.settle_timeout,
        }

    def close(self, our_address, first_transfer, second_transfer):
        ctx = {
            'block_number': self.blockchain.block_number(),
            'msg.sender': our_address,
        }

        first_encoded = None
        second_encoded = None

        if first_transfer is not None:
            first_encoded = first_transfer.encode()

        if second_transfer is not None:
            second_encoded = second_transfer.encode()

        self.contract.close(
            ctx,
            first_encoded,
            second_encoded,
        )

    def update_transfer(self, our_address, transfer):
        ctx = {
            'block_number': self.blockchain.block_number(),
            'msg.sender': our_address,
        }

        if transfer is not None:
            self.contract.update_transfer(
                ctx,
                transfer.encode(),
            )

    def unlock(self, our_address, unlocked_transfers):
        ctx = {
            'block_number': self.blockchain.block_number(),
            'msg.sender': our_address,
        }

        for merkle_proof, locked_encoded, secret in unlocked_transfers:
            if isinstance(locked_encoded, messages.Lock):
                raise ValueError('unlock must be called with a lock encoded `.as_bytes`')

            merkleproof_encoded = ''.join(merkle_proof)

            self.contract.unlock(
                ctx,
                locked_encoded,
                merkleproof_encoded,
                secret,
            )

    def settle(self):
        ctx = {
            'block_number': self.blockchain.block_number(),
        }
        self.contract.settle(ctx)

    def channelnewbalance_filter(self):
        filter_ = FilterMock(None, next(FILTER_ID_GENERATOR))
        self.newbalance_filters.append(filter_)
        return filter_

    def channelsecretrevealed_filter(self):
        filter_ = FilterMock(None, next(FILTER_ID_GENERATOR))
        self.secretrevealed_filters.append(filter_)
        return filter_

    def channelclosed_filter(self):
        filter_ = FilterMock(None, next(FILTER_ID_GENERATOR))
        self.channelclose_filters.append(filter_)
        return filter_

    def channelsettled_filter(self):
        filter_ = FilterMock(None, next(FILTER_ID_GENERATOR))
        self.channelsettle_filters.append(filter_)
        return filter_
Exemplo n.º 6
0
class NettingChannelMock(object):
    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,
        )

    def asset_address(self):
        return self.contract.asset_address

    def settle_timeout(self):
        return self.contract.settle_timeout

    def isopen(self):
        return self.contract.isopen

    def partner(self, our_address):
        return self.contract.partner(our_address)

    def deposit(self, our_address, amount):
        self.contract.deposit(
            our_address,
            amount,
            BlockChainServiceMock.block_number(),
        )

        our_data = self.contract.participants[our_address]
        data = {
            '_event_type': 'ChannelNewBalance',
            'asset_address': self.contract.asset_address,
            'participant': our_address,
            'balance': our_data.deposit,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(
            CHANNELNEWBALANCE_EVENTID,
            CHANNELNEWBALANCE_EVENT,
            data,
            self.address,
        )

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def transferred_amount(self, participant_address):
        # TODO: Make this work if we don't drop support for the mock client
        raise RuntimeError('transferredAmount not implemented for mock client')
        return self.contract._get_transferred_amount(None, None)

    def opened(self):
        return self.contract.opened

    def closed(self):
        return self.contract.closed

    def settled(self):
        return self.contract.settled

    def detail(self, our_address):
        partner_address = self.contract.partner(our_address)

        our_balance = self.contract.participants[our_address].deposit
        partner_balance = self.contract.participants[partner_address].deposit

        return {
            'our_address': our_address,
            'our_balance': our_balance,
            'partner_address': partner_address,
            'partner_balance': partner_balance,
            'settle_timeout': self.contract.settle_timeout,
        }

    def close(self, our_address, first_transfer, second_transfer):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        first_encoded = None
        second_encoded = None

        if first_transfer is not None:
            first_encoded = first_transfer.encode()

        if second_transfer is not None:
            second_encoded = second_transfer.encode()

        self.contract.close(
            ctx,
            first_encoded,
            second_encoded,
        )

        data = {
            '_event_type': 'ChannelClosed',
            'closing_address': our_address,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELCLOSED_EVENTID, CHANNELCLOSED_EVENT, data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def update_transfer(self, our_address, first_transfer, second_transfer=None):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        if first_transfer is not None:
            self.contract.update_transfer(
                ctx,
                first_transfer.encode(),
                second_transfer.encode() if second_transfer is not None else ""
            )

    def unlock(self, our_address, unlock_proofs):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        for merkle_proof, locked_encoded, secret in unlock_proofs:
            if isinstance(locked_encoded, messages.Lock):
                raise ValueError('unlock must be called with a lock encoded `.as_bytes`')

            merkleproof_encoded = ''.join(merkle_proof)

            self.contract.unlock(
                ctx,
                locked_encoded,
                merkleproof_encoded,
                secret,
            )

            data = {
                '_event_type': 'ChannelSecretRevealed',
                'secret': secret,
            }
            event = ethereum_event(
                CHANNELSECRETREVEALED_EVENTID,
                CHANNELSECRETREVEALED_EVENT,
                data,
                self.address,
            )

            for filter_ in BlockChainServiceMock.filters[self.address]:
                filter_.event(event)

    def settle(self):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
        }
        self.contract.settle(ctx)

        data = {
            '_event_type': 'ChannelSettled',
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELSETTLED_EVENTID, CHANNELSETTLED_EVENT, data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def all_events_filter(self):
        topics = None
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_
Exemplo n.º 7
0
class NettingChannelMock(object):
    """Shared state implementation of a pure python netting channel. You should not call this
    instance directly, but instead instantiate a raiden.tests.utils.OwnedNettingChannel with a
    fixed address.
    """
    def __init__(self,
                 token_address,
                 peer1,
                 peer2,
                 settle_timeout,
                 address=None):
        # pylint: disable=too-many-arguments

        self.address = address or make_address()

        self.contract = NettingChannelContract(
            token_address,
            self.address,
            peer1,
            peer2,
            settle_timeout,
        )

    def token_address(self):
        return self.contract.token_address

    def settle_timeout(self):
        return self.contract.settle_timeout

    def can_transfer(self, our_address):
        return (self.contract.opened > 0 and self.contract.closed == 0
                and self.detail(our_address)['our_balance'] > 0)

    def deposit(self, our_address, amount):
        self.contract.deposit(
            our_address,
            amount,
            BlockChainServiceMock.block_number(),
        )

        our_data = self.contract.participants[our_address]
        data = {
            '_event_type': 'ChannelNewBalance',
            'token_address': self.contract.token_address,
            'participant': our_address,
            'balance': our_data.deposit,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(
            CHANNELNEWBALANCE_EVENTID,
            CHANNELNEWBALANCE_EVENT,
            data,
            self.address,
        )

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def opened(self):
        return self.contract.opened

    def closed(self):
        return self.contract.closed

    def closing_address(self):
        closing_address = self.contract.closingAddress
        assert len(closing_address) == 20, "Expected binary address"
        return closing_address

    def settled(self):
        return self.contract.settled

    def detail(self, our_address):
        our_balance = self.contract.participants[our_address].deposit

        partner_address = self.contract.partner(our_address)
        partner_balance = self.contract.participants[partner_address].deposit

        return {
            'our_address': our_address,
            'our_balance': our_balance,
            'partner_address': partner_address,
            'partner_balance': partner_balance,
            'settle_timeout': self.contract.settle_timeout,
        }

    def close(self, our_address, their_transfer):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        their_encoded = None

        if their_transfer is not None:
            their_encoded = their_transfer.encode()

        self.contract.close(
            ctx,
            their_encoded,
        )

        data = {
            '_event_type': 'ChannelClosed',
            'closing_address': our_address,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELCLOSED_EVENTID, CHANNELCLOSED_EVENT,
                               data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def update_transfer(self,
                        our_address,
                        first_transfer,
                        second_transfer=None):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        if first_transfer is not None:
            self.contract.update_transfer(
                ctx, first_transfer.encode(),
                second_transfer.encode()
                if second_transfer is not None else "")

    def withdraw(self, our_address, unlock_proofs):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        for merkle_proof, locked_encoded, secret in unlock_proofs:
            if isinstance(locked_encoded, messages.Lock):
                raise ValueError(
                    'withdraw must be called with a lock encoded `.as_bytes`')

            merkleproof_encoded = ''.join(merkle_proof)

            self.contract.withdraw(
                ctx,
                locked_encoded,
                merkleproof_encoded,
                secret,
            )

            data = {
                '_event_type': 'ChannelSecretRevealed',
                'secret': secret,
            }
            event = ethereum_event(
                CHANNELSECRETREVEALED_EVENTID,
                CHANNELSECRETREVEALED_EVENT,
                data,
                self.address,
            )

            for filter_ in BlockChainServiceMock.filters[self.address]:
                filter_.event(event)

    def settle(self):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
        }
        self.contract.settle(ctx)

        data = {
            '_event_type': 'ChannelSettled',
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELSETTLED_EVENTID, CHANNELSETTLED_EVENT,
                               data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def all_events_filter(self):
        topics = None
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_
Exemplo n.º 8
0
class NettingChannelMock(object):
    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,
        )

    def asset_address(self):
        return self.contract.asset_address

    def settle_timeout(self):
        return self.contract.settle_timeout

    def isopen(self):
        return self.contract.isopen

    def partner(self, our_address):
        return self.contract.partner(our_address)

    def deposit(self, our_address, amount):
        self.contract.deposit(
            our_address,
            amount,
            BlockChainServiceMock.block_number(),
        )

        our_data = self.contract.participants[our_address]
        data = {
            '_event_type': 'ChannelNewBalance',
            'asset_address': self.contract.asset_address,
            'participant': our_address,
            'balance': our_data.deposit,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(
            CHANNELNEWBALANCE_EVENTID,
            CHANNELNEWBALANCE_EVENT,
            data,
            self.address,
        )

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def opened(self):
        return self.contract.opened

    def closed(self):
        return self.contract.closed

    def settled(self):
        return self.contract.settled

    def detail(self, our_address):
        partner_address = self.contract.partner(our_address)

        our_balance = self.contract.participants[our_address].deposit
        partner_balance = self.contract.participants[partner_address].deposit

        return {
            'our_address': our_address,
            'our_balance': our_balance,
            'partner_address': partner_address,
            'partner_balance': partner_balance,
            'settle_timeout': self.contract.settle_timeout,
        }

    def close(self, our_address, first_transfer, second_transfer):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        first_encoded = None
        second_encoded = None

        if first_transfer is not None:
            first_encoded = first_transfer.encode()

        if second_transfer is not None:
            second_encoded = second_transfer.encode()

        self.contract.close(
            ctx,
            first_encoded,
            second_encoded,
        )

        data = {
            '_event_type': 'ChannelClosed',
            'closing_address': our_address,
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELCLOSED_EVENTID, CHANNELCLOSED_EVENT, data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def update_transfer(self, our_address, transfer):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        if transfer is not None:
            self.contract.update_transfer(
                ctx,
                transfer.encode(),
            )

    def unlock(self, our_address, unlock_proofs):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
            'msg.sender': our_address,
        }

        for merkle_proof, locked_encoded, secret in unlock_proofs:
            if isinstance(locked_encoded, messages.Lock):
                raise ValueError('unlock must be called with a lock encoded `.as_bytes`')

            merkleproof_encoded = ''.join(merkle_proof)

            self.contract.unlock(
                ctx,
                locked_encoded,
                merkleproof_encoded,
                secret,
            )

            data = {
                '_event_type': 'ChannelSecretRevealed',
                'secret': secret,
            }
            event = ethereum_event(
                CHANNELSECRETREVEALED_EVENTID,
                CHANNELSECRETREVEALED_EVENT,
                data,
                self.address,
            )

            for filter_ in BlockChainServiceMock.filters[self.address]:
                filter_.event(event)

    def settle(self):
        ctx = {
            'block_number': BlockChainServiceMock.block_number(),
        }
        self.contract.settle(ctx)

        data = {
            '_event_type': 'ChannelSettled',
            'block_number': BlockChainServiceMock.block_number(),
        }
        event = ethereum_event(CHANNELSETTLED_EVENTID, CHANNELSETTLED_EVENT, data, self.address)

        for filter_ in BlockChainServiceMock.filters[self.address]:
            filter_.event(event)

    def channelnewbalance_filter(self):
        topics = [CHANNELNEWBALANCE_EVENTID]
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_

    def channelsecretrevealed_filter(self):
        topics = [CHANNELSECRETREVEALED_EVENTID]
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_

    def channelclosed_filter(self):
        topics = [CHANNELCLOSED_EVENTID]
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_

    def channelsettled_filter(self):
        topics = [CHANNELSETTLED_EVENTID]
        filter_ = FilterMock(topics, next(FILTER_ID_GENERATOR))
        BlockChainServiceMock.filters[self.address].append(filter_)
        return filter_