def test_withdraw_twice(tester_registry_address, reveal_timeout, tester_channels, tester_chain): """ A lock can be withdrawn only once, the second try must fail. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] pseudo_random_generator = random.Random() lock_expiration = tester_chain.block.number + reveal_timeout + 5 secret = b'secretsecretsecretsecretsecretse' 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, ) lock = Lock(17, lock_expiration, sha3(secret)) mediated0 = make_mediated_transfer( tester_registry_address, channel1, channel0, privatekey_to_address(pkey1), privatekey_to_address(pkey0), lock, pkey1, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) unlock_proofs = channel.get_known_unlocks(channel0.partner_state) proof = unlock_proofs[0] nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, ) with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_mediated_after_direct_transfer(reveal_timeout, settle_timeout, deposit, tester_state, tester_channels, tester_token): """ The transfer types must not change the behavior of the dispute. """ 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=pkey0) first_amount0 = 90 block_number = tester_state.block.number make_direct_transfer_from_channel( block_number, channel0, channel1, first_amount0, pkey0, ) lock_expiration = tester_state.block.number + reveal_timeout + 5 new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock1 = Lock(amount=31, expiration=lock_expiration, hashlock=sha3('lock2')) second_mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock1, pkey0, tester_state.block.number, ) nettingchannel.close(sender=pkey0) second_mediated0_hash = sha3(second_mediated0.packed().data[:-65]) nettingchannel.updateTransfer( second_mediated0.nonce, second_mediated0.transferred_amount, second_mediated0.locksroot, second_mediated0_hash, second_mediated0.signature, 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 - first_amount0 balance1 = initial_balance1 + deposit + first_amount0 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_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_withdraw_expired_lock(reveal_timeout, tester_channels, tester_chain): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] pseudo_random_generator = random.Random() lock_timeout = reveal_timeout + 5 lock_expiration = tester_chain.block.number + lock_timeout secret = b'expiredlockexpiredlockexpiredloc' secrethash = sha3(secret) 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, ) lock1 = Lock(amount=31, expiration=lock_expiration, secrethash=secrethash) mediated0 = make_mediated_transfer( channel1, channel0, privatekey_to_address(pkey0), privatekey_to_address(pkey1), lock1, pkey1, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) # expire the lock tester_chain.mine(number_of_blocks=lock_timeout + 1) unlock_proofs = channel.get_known_unlocks(channel0.partner_state) proof = unlock_proofs[0] with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_withdraw(deposit, settle_timeout, reveal_timeout, tester_channels, tester_state, 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=pkey0) lock_amount = 31 lock_expiration = tester_state.block.number + reveal_timeout + 5 secret = 'secretsecretsecretsecretsecretse' hashlock = sha3(secret) new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(lock_amount, lock_expiration, hashlock) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_state.block.number, secret, ) mediated0_data = str(mediated0.packed().data) proof = channel1.our_state.balance_proof.compute_proof_for_lock( secret, mediated0.lock, ) nettingchannel.close(mediated0_data, sender=pkey1) tester_state.mine(number_of_blocks=1) nettingchannel.withdraw( proof.lock_encoded, ''.join(proof.merkle_proof), proof.secret, sender=pkey1, ) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock0.amount balance1 = initial_balance1 + deposit + lock0.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
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_withdraw_twice(reveal_timeout, tester_channels, tester_chain): """ A lock can be withdrawn only once, the second try must fail. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] lock_expiration = tester_chain.block.number + reveal_timeout + 5 secret = b'secretsecretsecretsecretsecretse' new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock = Lock(17, lock_expiration, sha3(secret)) mediated0 = make_mediated_transfer( channel1, channel0, privatekey_to_address(pkey1), privatekey_to_address(pkey0), lock, pkey1, tester_chain.block.number, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) unlock_proofs = list(channel0.partner_state.get_known_unlocks()) proof = unlock_proofs[0] nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, ) with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_withdraw_twice(reveal_timeout, tester_channels, tester_state): """ A lock can be withdrawn only once, the second try must fail. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] lock_expiration = tester_state.block.number + reveal_timeout + 5 secret = 'secretsecretsecretsecretsecretse' new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock = Lock(17, lock_expiration, sha3(secret)) mediated0 = make_mediated_transfer( channel1, channel0, privatekey_to_address(pkey1), privatekey_to_address(pkey0), lock, pkey1, tester_state.block.number, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) unlock_proofs = list(channel0.our_state.balance_proof.get_known_unlocks()) proof = unlock_proofs[0] nettingchannel.withdraw( proof.lock_encoded, ''.join(proof.merkle_proof), proof.secret, sender=pkey0, ) with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, ''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_withdraw_expired_lock(reveal_timeout, tester_channels, tester_state): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] lock_timeout = reveal_timeout + 5 lock_expiration = tester_state.block.number + lock_timeout secret = 'expiredlockexpiredlockexpiredloc' hashlock = sha3(secret) new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock1 = Lock(amount=31, expiration=lock_expiration, hashlock=hashlock) mediated0 = make_mediated_transfer( channel1, channel0, privatekey_to_address(pkey0), privatekey_to_address(pkey1), lock1, pkey1, tester_state.block.number, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) # expire the lock tester_state.mine(number_of_blocks=lock_timeout + 1) unlock_proofs = list( channel0.partner_state.balance_proof.get_known_unlocks()) proof = unlock_proofs[0] with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, ''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_withdraw_expired_lock(reveal_timeout, tester_channels, tester_chain): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] lock_timeout = reveal_timeout + 5 lock_expiration = tester_chain.block.number + lock_timeout secret = b'expiredlockexpiredlockexpiredloc' hashlock = sha3(secret) new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock1 = Lock(amount=31, expiration=lock_expiration, hashlock=hashlock) mediated0 = make_mediated_transfer( channel1, channel0, privatekey_to_address(pkey0), privatekey_to_address(pkey1), lock1, pkey1, tester_chain.block.number, secret, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey0, ) # expire the lock tester_chain.mine(number_of_blocks=lock_timeout + 1) unlock_proofs = list(channel0.partner_state.get_known_unlocks()) proof = unlock_proofs[0] with pytest.raises(TransactionFailed): nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey0, )
def test_withdraw_both_participants( deposit, settle_timeout, reveal_timeout, tester_channels, tester_state, 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=pkey0) secret = 'secretsecretsecretsecretsecretse' hashlock = sha3(secret) lock_amount = 31 lock01_expiration = tester_state.block.number + settle_timeout - 1 * reveal_timeout lock10_expiration = tester_state.block.number + settle_timeout - 2 * reveal_timeout new_block = Block(tester_state.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) # using the same hashlock and amount is intentional lock01 = Lock(lock_amount, lock01_expiration, hashlock) lock10 = Lock(lock_amount, lock10_expiration, hashlock) mediated01 = make_mediated_transfer( channel0, channel1, address0, address1, lock01, pkey0, tester_state.block.number, secret, ) mediated01_data = str(mediated01.packed().data) mediated10 = make_mediated_transfer( channel1, channel0, address1, address0, lock10, pkey1, tester_state.block.number, secret, ) mediated10_data = str(mediated10.packed().data) nettingchannel.close(mediated01_data, sender=pkey1) tester_state.mine(number_of_blocks=1) nettingchannel.updateTransfer(mediated10_data, sender=pkey0) tester_state.mine(number_of_blocks=1) proof01 = channel1.our_state.balance_proof.compute_proof_for_lock( secret, mediated01.lock, ) nettingchannel.withdraw( proof01.lock_encoded, ''.join(proof01.merkle_proof), proof01.secret, sender=pkey1, ) proof10 = channel0.our_state.balance_proof.compute_proof_for_lock( secret, mediated10.lock, ) nettingchannel.withdraw( proof10.lock_encoded, ''.join(proof10.merkle_proof), proof10.secret, sender=pkey0, ) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock01.amount + lock10.amount balance1 = initial_balance1 + deposit + lock01.amount - lock10.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
def test_mediated_after_direct_transfer( reveal_timeout, settle_timeout, deposit, tester_chain, tester_channels, tester_token): """ The transfer types must not change the behavior of the dispute. """ 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=pkey0) first_amount0 = 90 block_number = tester_chain.block.number make_direct_transfer_from_channel( block_number, channel0, channel1, first_amount0, pkey0, ) lock_expiration = tester_chain.block.number + reveal_timeout + 5 new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock1 = Lock(amount=31, expiration=lock_expiration, hashlock=sha3(b'lock2')) second_mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock1, pkey0, tester_chain.block.number, ) nettingchannel.close(sender=pkey0) second_mediated0_hash = sha3(second_mediated0.packed().data[:-65]) nettingchannel.updateTransfer( second_mediated0.nonce, second_mediated0.transferred_amount, second_mediated0.locksroot, second_mediated0_hash, second_mediated0.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 - first_amount0 balance1 = initial_balance1 + deposit + first_amount0 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_withdraw_lock_with_a_large_expiration(deposit, tester_channels, tester_state, tester_token, settle_timeout): """ Withdraw must accept a lock that expires after the settlement period. """ 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=pkey0) # use a really large expiration lock_expiration = tester_state.block.number + settle_timeout * 5 # work around for the python expiration validation bad_block_number = lock_expiration - 10 channel0.state_transition(Block(bad_block_number)) lock_amount = 29 secret = sha3('test_withdraw_lock_with_a_large_expiration') lock_hashlock = sha3(secret) lock = Lock( amount=lock_amount, expiration=lock_expiration, hashlock=lock_hashlock, ) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock, pkey0, bad_block_number, secret, ) 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, ) unlock_proofs = list(channel1.our_state.balance_proof.get_known_unlocks()) proof = unlock_proofs[0] nettingchannel.withdraw( proof.lock_encoded, ''.join(proof.merkle_proof), proof.secret, sender=pkey1, ) tester_state.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock_amount balance1 = initial_balance1 + deposit + lock_amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
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_withdraw( tester_registry_address, deposit, settle_timeout, reveal_timeout, tester_channels, tester_chain, tester_token, ): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] 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=pkey0) lock_amount = 31 lock_expiration = tester_chain.block.number + reveal_timeout + 5 secret = b'secretsecretsecretsecretsecretse' secrethash = sha3(secret) 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(lock_amount, lock_expiration, secrethash) mediated0 = make_mediated_transfer( tester_registry_address, channel0, channel1, address0, address1, lock0, pkey0, secret, ) # withdraw the pending transfer sent to us by our partner lock_state = lockstate_from_lock(mediated0.lock) proof = channel.compute_proof_for_lock( channel1.partner_state, secret, lock_state, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=1) nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock0.amount balance1 = initial_balance1 + deposit + lock0.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
def test_withdraw_both_participants( tester_registry_address, deposit, settle_timeout, reveal_timeout, tester_channels, tester_chain, tester_token, ): pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] 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=pkey0) secret = b'secretsecretsecretsecretsecretse' secrethash = sha3(secret) lock_amount = 31 lock01_expiration = tester_chain.block.number + settle_timeout - 1 * reveal_timeout lock10_expiration = tester_chain.block.number + settle_timeout - 2 * reveal_timeout 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, ) # using the same secrethash and amount is intentional lock01 = Lock(lock_amount, lock01_expiration, secrethash) lock10 = Lock(lock_amount, lock10_expiration, secrethash) mediated01 = make_mediated_transfer( tester_registry_address, channel0, channel1, address0, address1, lock01, pkey0, secret, ) mediated10 = make_mediated_transfer( tester_registry_address, channel1, channel0, address1, address0, lock10, pkey1, secret, ) mediated01_hash = sha3(mediated01.packed().data[:-65]) nettingchannel.close( mediated01.nonce, mediated01.transferred_amount, mediated01.locksroot, mediated01_hash, mediated01.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=1) mediated10_hash = sha3(mediated10.packed().data[:-65]) nettingchannel.updateTransfer( mediated10.nonce, mediated10.transferred_amount, mediated10.locksroot, mediated10_hash, mediated10.signature, sender=pkey0, ) tester_chain.mine(number_of_blocks=1) lock_state01 = lockstate_from_lock(mediated01.lock) proof01 = channel.compute_proof_for_lock( channel1.partner_state, secret, lock_state01, ) nettingchannel.withdraw( proof01.lock_encoded, b''.join(proof01.merkle_proof), proof01.secret, sender=pkey1, ) lock_state10 = lockstate_from_lock(mediated10.lock) proof10 = channel.compute_proof_for_lock( channel0.partner_state, secret, lock_state10, ) nettingchannel.withdraw( proof10.lock_encoded, b''.join(proof10.merkle_proof), proof10.secret, sender=pkey0, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock01.amount + lock10.amount balance1 = initial_balance1 + deposit + lock01.amount - lock10.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
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_withdraw_lock_with_a_large_expiration( deposit, tester_channels, tester_chain, tester_token, settle_timeout): """ Withdraw must accept a lock that expires after the settlement period. """ 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=pkey0) # use a really large expiration lock_expiration = tester_chain.block.number + settle_timeout * 5 # work around for the python expiration validation bad_block_number = lock_expiration - 10 channel0.state_transition(Block(bad_block_number)) lock_amount = 29 secret = sha3(b'test_withdraw_lock_with_a_large_expiration') lock_hashlock = sha3(secret) lock = Lock( amount=lock_amount, expiration=lock_expiration, hashlock=lock_hashlock, ) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock, pkey0, bad_block_number, secret, ) 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, ) unlock_proofs = list(channel1.partner_state.get_known_unlocks()) proof = unlock_proofs[0] nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock_amount balance1 = initial_balance1 + deposit + lock_amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
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
def test_withdraw( deposit, settle_timeout, reveal_timeout, tester_channels, tester_chain, 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=pkey0) lock_amount = 31 lock_expiration = tester_chain.block.number + reveal_timeout + 5 secret = b'secretsecretsecretsecretsecretse' hashlock = sha3(secret) new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) lock0 = Lock(lock_amount, lock_expiration, hashlock) mediated0 = make_mediated_transfer( channel0, channel1, address0, address1, lock0, pkey0, tester_chain.block.number, secret, ) # withdraw the pending transfer sent to us by our partner proof = channel1.partner_state.compute_proof_for_lock( secret, mediated0.lock, ) mediated0_hash = sha3(mediated0.packed().data[:-65]) nettingchannel.close( mediated0.nonce, mediated0.transferred_amount, mediated0.locksroot, mediated0_hash, mediated0.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=1) nettingchannel.withdraw( proof.lock_encoded, b''.join(proof.merkle_proof), proof.secret, sender=pkey1, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock0.amount balance1 = initial_balance1 + deposit + lock0.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
def test_withdraw_both_participants( deposit, settle_timeout, reveal_timeout, tester_channels, tester_chain, 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=pkey0) secret = b'secretsecretsecretsecretsecretse' hashlock = sha3(secret) lock_amount = 31 lock01_expiration = tester_chain.block.number + settle_timeout - 1 * reveal_timeout lock10_expiration = tester_chain.block.number + settle_timeout - 2 * reveal_timeout new_block = Block(tester_chain.block.number) channel0.state_transition(new_block) channel1.state_transition(new_block) # using the same hashlock and amount is intentional lock01 = Lock(lock_amount, lock01_expiration, hashlock) lock10 = Lock(lock_amount, lock10_expiration, hashlock) mediated01 = make_mediated_transfer( channel0, channel1, address0, address1, lock01, pkey0, tester_chain.block.number, secret, ) mediated10 = make_mediated_transfer( channel1, channel0, address1, address0, lock10, pkey1, tester_chain.block.number, secret, ) mediated01_hash = sha3(mediated01.packed().data[:-65]) nettingchannel.close( mediated01.nonce, mediated01.transferred_amount, mediated01.locksroot, mediated01_hash, mediated01.signature, sender=pkey1, ) tester_chain.mine(number_of_blocks=1) mediated10_hash = sha3(mediated10.packed().data[:-65]) nettingchannel.updateTransfer( mediated10.nonce, mediated10.transferred_amount, mediated10.locksroot, mediated10_hash, mediated10.signature, sender=pkey0, ) tester_chain.mine(number_of_blocks=1) proof01 = channel1.partner_state.compute_proof_for_lock( secret, mediated01.lock, ) nettingchannel.withdraw( proof01.lock_encoded, b''.join(proof01.merkle_proof), proof01.secret, sender=pkey1, ) proof10 = channel0.partner_state.compute_proof_for_lock( secret, mediated10.lock, ) nettingchannel.withdraw( proof10.lock_encoded, b''.join(proof10.merkle_proof), proof10.secret, sender=pkey0, ) tester_chain.mine(number_of_blocks=settle_timeout + 1) nettingchannel.settle(sender=pkey0) balance0 = initial_balance0 + deposit - lock01.amount + lock10.amount balance1 = initial_balance1 + deposit + lock01.amount - lock10.amount assert tester_token.balanceOf(address0, sender=pkey0) == balance0 assert tester_token.balanceOf(address1, sender=pkey0) == balance1 assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0