Пример #1
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_
Пример #2
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_
Пример #3
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_