def test_settle_with_locked_mediated_transfer_for_counterparty( deposit, settle_timeout, reveal_timeout, tester_chain, tester_channels, tester_token): """ Test settle with a locked mediated transfer for the counter party. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] payment_network_identifier = factories.make_address() address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial0 = tester_token.balanceOf(address0, sender=pkey0) initial1 = tester_token.balanceOf(address1, sender=pkey0) transferred_amount0 = 30 increase_transferred_amount( payment_network_identifier, channel0, channel1, transferred_amount0, pkey0, ) expiration0 = tester_chain.block.number + reveal_timeout + 5 new_block = Block(tester_chain.block.number) channel.state_transition(channel0, new_block, new_block.block_number) channel.state_transition(channel1, new_block, new_block.block_number) lock0 = Lock(amount=29, expiration=expiration0, secrethash=sha3(b'lock1')) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, ) nettingchannel.close(sender=pkey0) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.updateTransfer( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey1) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial0 + deposit - transferred_amount0 balance1 = initial1 + transferred_amount0 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1
def test_settle_with_locked_mediated_transfer_for_counterparty( deposit, settle_timeout, reveal_timeout, tester_chain, tester_channels, tester_token): """ Test settle with a locked mediated transfer for the counter party. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial0 = tester_token.balanceOf(address0, sender=pkey0) initial1 = tester_token.balanceOf(address1, sender=pkey0) transferred_amount0 = 30 increase_transferred_amount(channel0, channel1, transferred_amount0) expiration0 = tester_chain.block.number + reveal_timeout + 5 new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(amount=29, expiration=expiration0, hashlock=sha3(b'lock1')) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_chain.block.number, ) nettingchannel.close(sender=pkey0) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.updateTransfer( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey1) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial0 + deposit - transferred_amount0 balance1 = initial1 + transferred_amount0 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1
def test_netting(deposit, settle_timeout, tester_channels, tester_state, tester_token): """ Transferred amount can be larger than the deposit. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) transferred_amount0 = deposit * 3 + 30 increase_transferred_amount(channel0, channel1, transferred_amount0) transferred_amount1 = deposit * 3 + 70 increase_transferred_amount(channel1, channel0, transferred_amount1) amount0 = 10 transferred_amount0 += amount0 block_number = tester_state.block.number direct0 = make_direct_transfer_from_channel( block_number, channel0, channel1, amount0, pkey0, ) direct0_data = str(direct0.packed().data) amount1 = 30 transferred_amount1 += amount1 block_number = tester_state.block.number direct1 = make_direct_transfer_from_channel( block_number, channel1, channel0, amount1, pkey1, ) direct1_data = str(direct1.packed().data) nettingchannel.close(direct1_data, sender=pkey0) nettingchannel.updateTransfer(direct0_data, sender=pkey1) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1
def test_settle_with_locked_mediated_transfer_for_counterparty( deposit, settle_timeout, reveal_timeout, tester_state, tester_channels, tester_token): """ Test settle with a locked mediated transfer for the counter party. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial0 = tester_token.balanceOf(address0, sender=pkey0) initial1 = tester_token.balanceOf(address1, sender=pkey0) transferred_amount0 = 30 increase_transferred_amount(channel0, channel1, transferred_amount0) expiration0 = tester_state.block.number + reveal_timeout + 5 new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(amount=29, expiration=expiration0, hashlock=sha3('lock1')) mediated = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_state.block.number, ) nettingchannel.close('', sender=pkey0) transfer_data = str(mediated.packed().data) nettingchannel.updateTransfer(transfer_data, sender=pkey1) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey1) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial0 + deposit - transferred_amount0 balance1 = initial1 + transferred_amount0 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1
def test_netting(deposit, settle_timeout, tester_channels, tester_chain, tester_token): """ Transferred amount can be larger than the deposit. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] payment_network_identifier = factories.make_address() address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) # increase the transferred amount by three times the deposits for _ in range(3): increase_transferred_amount( payment_network_identifier, channel0, channel1, deposit, pkey0, ) increase_transferred_amount( payment_network_identifier, channel1, channel0, deposit, pkey1, ) transferred_amount0 = deposit * 3 transferred_amount1 = deposit * 3 amount0 = 10 transferred_amount0 += amount0 direct0 = make_direct_transfer_from_channel( payment_network_identifier, channel0, channel1, amount0, pkey0, ) amount1 = 30 transferred_amount1 += amount1 direct1 = make_direct_transfer_from_channel( payment_network_identifier, channel1, channel0, amount1, pkey1, ) direct1_hash = sha3(direct1.packed().data[:-65]) nettingchannel.close( direct1.nonce, direct1.transferred_amount, direct1.locksroot, direct1_hash, direct1.signature, sender=pkey0, ) direct0_hash = sha3(direct0.packed().data[:-65]) nettingchannel.updateTransfer( direct0.nonce, direct0.transferred_amount, direct0.locksroot, direct0_hash, direct0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1
def test_settle_two_locked_mediated_transfer_messages( deposit, settle_timeout, reveal_timeout, tester_chain, tester_channels, tester_token, ): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] payment_network_identifier = factories.make_address() pseudo_random_generator = random.Random() address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) transferred_amount0 = 30 increase_transferred_amount( payment_network_identifier, channel0, channel1, transferred_amount0, pkey0, ) transferred_amount1 = 70 increase_transferred_amount( payment_network_identifier, channel1, channel0, transferred_amount1, pkey1, ) expiration0 = tester_chain.block.number + reveal_timeout + 5 new_block = Block(tester_chain.block.number) channel.state_transition( channel0, new_block, pseudo_random_generator, new_block.block_number, ) channel.state_transition( channel1, new_block, pseudo_random_generator, new_block.block_number, ) lock0 = Lock(amount=29, expiration=expiration0, secrethash=sha3(b'lock1')) mediated0 = make_mediated_transfer( payment_network_identifier, channel0, channel1, address0, address1, lock0, pkey0, ) lock_expiration1 = tester_chain.block.number + reveal_timeout + 5 lock1 = Lock(amount=31, expiration=lock_expiration1, secrethash=sha3(b'lock2')) mediated1 = make_mediated_transfer( payment_network_identifier, channel1, channel0, address1, address0, lock1, pkey1, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) mediated1_hash = sha3(mediated1.packed().data[:-65]) nettingchannel.updateTransfer( mediated1.nonce, mediated1.transferred_amount, mediated1.locksroot, mediated1_hash, mediated1.signature, sender=pkey0, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1
def test_settle_two_locked_mediated_transfer_messages( deposit, settle_timeout, reveal_timeout, tester_state, tester_channels, tester_token): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) transferred_amount0 = 30 increase_transferred_amount(channel0, channel1, transferred_amount0) transferred_amount1 = 70 increase_transferred_amount(channel1, channel0, transferred_amount1) expiration0 = tester_state.block.number + reveal_timeout + 5 new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(amount=29, expiration=expiration0, hashlock=sha3('lock1')) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_state.block.number, ) mediated0_data = str(mediated0.packed().data) lock_expiration1 = tester_state.block.number + reveal_timeout + 5 lock1 = Lock(amount=31, expiration=lock_expiration1, hashlock=sha3('lock2')) mediated1 = make_mediated_transfer( channel1, channel0, address1, address0, lock1, pkey1, tester_state.block.number, ) mediated1_data = str(mediated1.packed().data) nettingchannel.close(mediated0_data, sender=pkey1) nettingchannel.updateTransfer(mediated1_data, sender=pkey0) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1
def test_netting(deposit, settle_timeout, tester_channels, tester_chain, tester_token): """ Transferred amount can be larger than the deposit. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) # increase the transferred amount by three times the deposits for _ in range(3): increase_transferred_amount(channel0, channel1, deposit) increase_transferred_amount(channel1, channel0, deposit) transferred_amount0 = deposit * 3 transferred_amount1 = deposit * 3 amount0 = 10 transferred_amount0 += amount0 block_number = tester_chain.block.number direct0 = make_direct_transfer_from_channel( block_number, channel0, channel1, amount0, pkey0, ) amount1 = 30 transferred_amount1 += amount1 block_number = tester_chain.block.number direct1 = make_direct_transfer_from_channel( block_number, channel1, channel0, amount1, pkey1, ) direct1_hash = sha3(direct1.packed().data[:-65]) nettingchannel.close( direct1.nonce, direct1.transferred_amount, direct1.locksroot, direct1_hash, direct1.signature, sender=pkey0, ) direct0_hash = sha3(direct0.packed().data[:-65]) nettingchannel.updateTransfer( direct0.nonce, direct0.transferred_amount, direct0.locksroot, direct0_hash, direct0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1
def test_settle_two_locked_mediated_transfer_messages( deposit, settle_timeout, reveal_timeout, tester_chain, tester_channels, tester_token): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] address0 = privatekey_to_address(pkey0) address1 = privatekey_to_address(pkey1) initial_balance0 = tester_token.balanceOf(address0, sender=pkey0) initial_balance1 = tester_token.balanceOf(address1, sender=pkey1) transferred_amount0 = 30 increase_transferred_amount(channel0, channel1, transferred_amount0) transferred_amount1 = 70 increase_transferred_amount(channel1, channel0, transferred_amount1) expiration0 = tester_chain.block.number + reveal_timeout + 5 new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(amount=29, expiration=expiration0, hashlock=sha3(b'lock1')) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_chain.block.number, ) lock_expiration1 = tester_chain.block.number + reveal_timeout + 5 lock1 = Lock(amount=31, expiration=lock_expiration1, hashlock=sha3(b'lock2')) mediated1 = make_mediated_transfer( channel1, channel0, address1, address0, lock1, pkey1, tester_chain.block.number, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) mediated1_hash = sha3(mediated1.packed().data[:-65]) nettingchannel.updateTransfer( mediated1.nonce, mediated1.transferred_amount, mediated1.locksroot, mediated1_hash, mediated1.signature, sender=pkey0, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) # the balances only change by transferred_amount because the lock was /not/ unlocked balance0 = initial_balance0 + deposit - transferred_amount0 + transferred_amount1 balance1 = initial_balance1 + deposit + transferred_amount0 - transferred_amount1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey1) == 0 assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey1) == balance1