def test_channelnew_event(settle_timeout, tester_state, tester_events, tester_registry, tester_token): privatekey0 = tester.DEFAULT_KEY address0 = tester.DEFAULT_ACCOUNT address1 = tester.a1 channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) netting_channel_address1_hex = channel_manager.newChannel( address1, settle_timeout, sender=privatekey0, ) last_event = tester_events[-1] assert last_event == { '_event_type': 'ChannelNew', 'netting_channel': netting_channel_address1_hex, 'participant1': encode_hex(address0), 'participant2': encode_hex(address1), 'settle_timeout': settle_timeout, }
def tester_channelmanager(private_keys, tester_state, tester_events, tester_registry, tester_token): privatekey0 = private_keys[0] channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) return channel_manager
def test_channelmanager_start_with_zero_entries(private_keys, tester_events, tester_registry, tester_state, tester_token): """ A new channel manager must start empty. """ pkey0 = private_keys[0] channel_manager = new_channelmanager( pkey0, tester_state, tester_events.append, tester_registry, tester_token.address, ) # pylint: disable=no-member assert not channel_manager.getChannelsAddresses(sender=pkey0) assert not channel_manager.getChannelsParticipants(sender=pkey0)
def test_channelnew_event(settle_timeout, tester_state, tester_events, tester_registry, tester_token): privatekey0 = tester.DEFAULT_KEY address0 = tester.DEFAULT_ACCOUNT address1 = tester.a1 channel_manager = new_channelmanager(privatekey0, tester_state, tester_events.append, tester_registry, tester_token) netting_channel_address1_hex = channel_manager.newChannel(address1, settle_timeout, sender=privatekey0) last_event = tester_events[-1] assert last_event == { "_event_type": "ChannelNew", "netting_channel": netting_channel_address1_hex, "participant1": encode_hex(address0), "participant2": encode_hex(address1), "settle_timeout": settle_timeout, }
def test_channelmanager_start_with_zero_entries( private_keys, tester_events, tester_registry, tester_chain, tester_token): """ A new channel manager must start empty. """ pkey0 = private_keys[0] channel_manager = new_channelmanager( pkey0, tester_chain, tester_events.append, tester_registry, tester_token.address, ) # pylint: disable=no-member assert not channel_manager.getChannelsAddresses(sender=pkey0) assert not channel_manager.getChannelsParticipants(sender=pkey0)
def test_channeldeposit(private_keys, settle_timeout, tester_state, tester_token, tester_events, tester_registry): """ Guarantee the correct tracking of each participant deposits, checks the initial state (pre-deposit) and state changes for each deposits. """ # not using the tester_nettingcontracts fixture to control the # transfer/deposits privatekey0 = private_keys[0] privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) unknow_key = tester.k3 channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) channel = new_nettingcontract( privatekey0, privatekey1, tester_state, tester_events.append, channel_manager, settle_timeout, ) # check initial state, needs to be zeroed out assert channel.settleTimeout(sender=privatekey0) == settle_timeout assert channel.tokenAddress(sender=privatekey0) == encode_hex( tester_token.address) assert channel.opened(sender=privatekey0) == 0 assert channel.closed(sender=privatekey0) == 0 assert channel.settled(sender=privatekey0) == 0 assert channel.addressAndBalance( sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == 0 assert channel.addressAndBalance( sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 initial_balance0 = tester_token.balanceOf(address0, sender=privatekey0) deposit_amount = initial_balance0 // 10 # try to make invalid deposits with pytest.raises(TransactionFailed): channel.deposit(1, sender=unknow_key) # not participant assert tester_token.approve( channel.address, deposit_amount * 2, sender=privatekey0) is True assert channel.deposit(deposit_amount * 2 + 1, sender=privatekey0) is False with pytest.raises(abi.ValueOutOfBounds): channel.deposit(-1, sender=privatekey0) # create a first deposit with half of the allowance assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount assert tester_token.balanceOf( address0, sender=privatekey0) == initial_balance0 - deposit_amount # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance( sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == deposit_amount assert channel.addressAndBalance( sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 # check a second depoist with the rest of the allowance assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount * 2 assert tester_token.balanceOf( address0, sender=privatekey0) == initial_balance0 - deposit_amount * 2 # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance( sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance( sender=privatekey0)[1] == deposit_amount * 2 assert channel.addressAndBalance( sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 # allowance zeroed, we cant make a new deposit assert channel.deposit(deposit_amount, sender=privatekey0) is False # needs to be able to add aditional token assert tester_token.approve( channel.address, deposit_amount, sender=privatekey0) is True assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount * 3 assert tester_token.balanceOf( address0, sender=privatekey0) == initial_balance0 - deposit_amount * 3 # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance( sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance( sender=privatekey0)[1] == deposit_amount * 3 assert channel.addressAndBalance( sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0
def test_channelnewbalance_event(private_keys, settle_timeout, tester_state, tester_events, tester_token, tester_registry): """ Check the correct events are generated for deposit calls. """ privatekey0 = private_keys[0] privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) nettingchannel = new_nettingcontract( privatekey0, privatekey1, tester_state, tester_events.append, channel_manager, settle_timeout, ) initial_balance0 = tester_token.balanceOf(address0, sender=privatekey0) deposit_amount = initial_balance0 // 10 assert tester_token.approve( nettingchannel.address, deposit_amount, sender=privatekey0) is True assert tester_token.approve( nettingchannel.address, deposit_amount, sender=privatekey1) is True previous_events = list(tester_events) assert nettingchannel.deposit(deposit_amount, sender=privatekey0) is True assert len(previous_events) + 2 == len(tester_events) block_number = tester_state.block.number transfer_event = tester_events[-2] assert transfer_event == { '_event_type': 'Transfer', '_from': encode_hex(address0), '_to': nettingchannel.address, '_value': deposit_amount, } newbalance_event = tester_events[-1] assert newbalance_event == { '_event_type': 'ChannelNewBalance', 'token_address': encode_hex(tester_token.address), 'participant': encode_hex(address0), 'balance': deposit_amount, 'block_number': block_number, } previous_events = list(tester_events) assert nettingchannel.deposit(deposit_amount, sender=privatekey1) is True assert len(previous_events) + 2 == len(tester_events) block_number = tester_state.block.number transfer_event = tester_events[-2] assert transfer_event == { '_event_type': 'Transfer', '_from': encode_hex(address1), '_to': nettingchannel.address, '_value': deposit_amount, } newbalance_event = tester_events[-1] assert newbalance_event == { '_event_type': 'ChannelNewBalance', 'token_address': encode_hex(tester_token.address), 'participant': encode_hex(address1), 'balance': deposit_amount, 'block_number': block_number, }
def __init__(self): super(NettingChannelStateMachine, self).__init__() deploy_key = sha3('deploy_key') gas_limit = 10**10 self.private_keys = [ sha3('p1'), sha3('p2'), sha3('p3' ), # third key used to generate signed but invalid transfers ] self.addresses = map(privatekey_to_address, self.private_keys) self.log = list() self.tester_state = tester_state( deploy_key, self.private_keys, gas_limit, ) self.settle_timeout = 50 self.token_amount = 1000 self.tokens = [ new_token( deploy_key, self.tester_state, self.token_amount, self.log.append, ), new_token( deploy_key, self.tester_state, self.token_amount, self.log.append, ), ] self.token = self.tokens[0] self.token_addresses = [token.address for token in self.tokens] self.nettingchannel_library_address = deploy_nettingchannel_library( deploy_key, self.tester_state, ) self.channel_manager_library_address = deploy_channelmanager_library( deploy_key, self.tester_state, self.nettingchannel_library_address, ) self.registry = new_registry( deploy_key, self.tester_state, self.channel_manager_library_address, self.log.append, ) self.channelmanager = new_channelmanager( deploy_key, self.tester_state, self.log.append, self.registry, self.token.address, ) self.netting_channel = new_nettingcontract( self.private_keys[0], self.private_keys[1], self.tester_state, self.log.append, self.channelmanager, self.settle_timeout, ) address_and_balance = self.netting_channel.addressAndBalance( # pylint: disable=no-member sender=self.private_keys[0], ) self.closing_address = None self.update_transfer_called = False self.participant_addresses = { unhexlify(address_and_balance[0]), unhexlify(address_and_balance[2]), } self.channel_addresses = [ unhexlify(self.netting_channel.address), make_address(), # used to test invalid transfers ]
def test_channeldeposit(private_keys, settle_timeout, tester_state, tester_token, tester_events, tester_registry): """ Guarantee the correct tracking of each participant deposits, checks the initial state (pre-deposit) and state changes for each deposits. """ # not using the tester_nettingcontracts fixture to control the # transfer/deposits privatekey0 = private_keys[0] privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) unknow_key = tester.k3 channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) channel = new_nettingcontract( privatekey0, privatekey1, tester_state, tester_events.append, channel_manager, settle_timeout, ) # check initial state, needs to be zeroed out assert channel.settleTimeout(sender=privatekey0) == settle_timeout assert channel.assetAddress(sender=privatekey0) == encode_hex(tester_token.address) assert channel.opened(sender=privatekey0) == 0 assert channel.closed(sender=privatekey0) == 0 assert channel.settled(sender=privatekey0) == 0 assert channel.addressAndBalance(sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == 0 assert channel.addressAndBalance(sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 initial_balance0 = tester_token.balanceOf(address0, sender=privatekey0) deposit_amount = initial_balance0 // 10 # try to make invalid deposits with pytest.raises(TransactionFailed): channel.deposit(1, sender=unknow_key) # not participant assert tester_token.approve(channel.address, deposit_amount * 2, sender=privatekey0) is True assert channel.deposit(deposit_amount * 2 + 1, sender=privatekey0) is False with pytest.raises(abi.ValueOutOfBounds): channel.deposit(-1, sender=privatekey0) # create a first deposit with half of the allowance assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount assert tester_token.balanceOf(address0, sender=privatekey0) == initial_balance0 - deposit_amount # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance(sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == deposit_amount assert channel.addressAndBalance(sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 # check a second depoist with the rest of the allowance assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount * 2 assert tester_token.balanceOf(address0, sender=privatekey0) == initial_balance0 - deposit_amount * 2 # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance(sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == deposit_amount * 2 assert channel.addressAndBalance(sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0 # allowance zeroed, we cant make a new deposit assert channel.deposit(deposit_amount, sender=privatekey0) is False # needs to be able to add aditional asset assert tester_token.approve(channel.address, deposit_amount, sender=privatekey0) is True assert channel.deposit(deposit_amount, sender=privatekey0) is True assert tester_token.balanceOf(channel.address, sender=privatekey0) == deposit_amount * 3 assert tester_token.balanceOf(address0, sender=privatekey0) == initial_balance0 - deposit_amount * 3 # noqa assert channel.opened(sender=privatekey0) == tester_state.block.number assert channel.addressAndBalance(sender=privatekey0)[0] == encode_hex(address0) assert channel.addressAndBalance(sender=privatekey0)[1] == deposit_amount * 3 assert channel.addressAndBalance(sender=privatekey0)[2] == encode_hex(address1) assert channel.addressAndBalance(sender=privatekey0)[3] == 0
def test_channelnewbalance_event(private_keys, settle_timeout, tester_state, tester_events, tester_token, tester_registry): """ Check the correct events are generated for deposit calls. """ privatekey0 = private_keys[0] privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) channel_manager = new_channelmanager( privatekey0, tester_state, tester_events.append, tester_registry, tester_token, ) nettingchannel = new_nettingcontract( privatekey0, privatekey1, tester_state, tester_events.append, channel_manager, settle_timeout, ) initial_balance0 = tester_token.balanceOf(address0, sender=privatekey0) deposit_amount = initial_balance0 // 10 assert tester_token.approve(nettingchannel.address, deposit_amount, sender=privatekey0) is True assert tester_token.approve(nettingchannel.address, deposit_amount, sender=privatekey1) is True previous_events = list(tester_events) assert nettingchannel.deposit(deposit_amount, sender=privatekey0) is True assert len(previous_events) + 2 == len(tester_events) block_number = tester_state.block.number transfer_event = tester_events[-2] assert transfer_event == { '_event_type': 'Transfer', '_from': encode_hex(address0), '_to': nettingchannel.address, '_value': deposit_amount, } newbalance_event = tester_events[-1] assert newbalance_event == { '_event_type': 'ChannelNewBalance', 'asset_address': encode_hex(tester_token.address), 'participant': encode_hex(address0), 'balance': deposit_amount, 'block_number': block_number, } previous_events = list(tester_events) assert nettingchannel.deposit(deposit_amount, sender=privatekey1) is True assert len(previous_events) + 2 == len(tester_events) block_number = tester_state.block.number transfer_event = tester_events[-2] assert transfer_event == { '_event_type': 'Transfer', '_from': encode_hex(address1), '_to': nettingchannel.address, '_value': deposit_amount, } newbalance_event = tester_events[-1] assert newbalance_event == { '_event_type': 'ChannelNewBalance', 'asset_address': encode_hex(tester_token.address), 'participant': encode_hex(address1), 'balance': deposit_amount, 'block_number': block_number, }