def test_channel_unlock_with_a_large_expiration( web3, custom_token, token_network, create_channel, channel_deposit, get_accounts, close_and_update_channel, reveal_secrets, ): """ unlock() should still work after a delayed settleChannel() call """ (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues(deposit=20, transferred=5) values_B = ChannelValues(deposit=30, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Mock pending transfers data with large expiration date pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout + 100) values_B.locksroot = pending_transfers_tree.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree.transfers)) # Reveal secrets before settlement window ends reveal_secrets(A, pending_transfers_tree.unlockable) close_and_update_channel(channel_identifier, A, values_A, B, values_B) # Settle channel after a "long" time web3.testing.mine(settle_timeout + 50) call_settle(token_network, channel_identifier, A, values_A, B, values_B) pre_balance_A = custom_token.functions.balanceOf(A).call() pre_balance_B = custom_token.functions.balanceOf(B).call() pre_balance_contract = custom_token.functions.balanceOf( token_network.address).call() # Unlock the tokens must still work token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call_and_transact() balance_A = custom_token.functions.balanceOf(A).call() balance_B = custom_token.functions.balanceOf(B).call() balance_contract = custom_token.functions.balanceOf( token_network.address).call() assert balance_A == pre_balance_A + 9 assert balance_B == pre_balance_B + 6 assert balance_contract == pre_balance_contract - values_B.locked_amounts.locked
def test_channel_unlock_both_participants( web3, custom_token, token_network, secret_registry_contract, create_channel, channel_deposit, get_accounts, close_and_update_channel, reveal_secrets, ): """ A scenario where both parties get some of the pending transfers """ (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues(deposit=100, transferred=5) values_B = ChannelValues(deposit=100, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Mock pending transfers data for A pending_transfers_tree_A = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout) values_A.locksroot = pending_transfers_tree_A.merkle_root values_A.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree_A.transfers)) # Reveal A's secrets before settlement window ends reveal_secrets(A, pending_transfers_tree_A.unlockable) # Mock pending transfers data for B pending_transfers_tree_B = get_pending_transfers_tree( web3, [2, 4, 6], [5, 10], settle_timeout) values_B.locksroot = pending_transfers_tree_B.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree_B.transfers)) # Reveal B's secrets before settlement window ends reveal_secrets(B, pending_transfers_tree_B.unlockable) close_and_update_channel(channel_identifier, A, values_A, B, values_B) # Settle channel web3.testing.mine(settle_timeout) call_settle(token_network, channel_identifier, A, values_A, B, values_B) pre_balance_A = custom_token.functions.balanceOf(A).call() pre_balance_B = custom_token.functions.balanceOf(B).call() pre_balance_contract = custom_token.functions.balanceOf( token_network.address).call() # A unlock's token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree_B.packed_transfers).call_and_transact() # B unlock's token_network.functions.unlock( channel_identifier, B, A, pending_transfers_tree_A.packed_transfers).call_and_transact() balance_A = custom_token.functions.balanceOf(A).call() balance_B = custom_token.functions.balanceOf(B).call() balance_contract = custom_token.functions.balanceOf( token_network.address).call() # Unlocked pending transfers A -> B, that belong to B unlockable_A = get_unlocked_amount( secret_registry_contract, pending_transfers_tree_A.packed_transfers) # Expired pending transfers A -> B, that belong to A expired_A = get_locked_amount(pending_transfers_tree_A.expired) # Unlocked pending transfers B -> A, that belong to A unlockable_B = get_unlocked_amount( secret_registry_contract, pending_transfers_tree_B.packed_transfers) # Expired pending transfers B -> A, that belong to B expired_B = get_locked_amount(pending_transfers_tree_B.expired) # check that A and B both received the expected amounts assert balance_contract == (pre_balance_contract - values_B.locked_amounts.locked - values_A.locked_amounts.locked) assert balance_A == pre_balance_A + unlockable_B + expired_A assert balance_B == pre_balance_B + unlockable_A + expired_B
def test_channel_unlock_before_settlement_fails( web3, custom_token, token_network, create_channel, channel_deposit, get_accounts, close_and_update_channel, reveal_secrets, ): """ unlock() should not work before settlement """ (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues(deposit=20, transferred=5) values_B = ChannelValues(deposit=30, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] # Mock pending transfers data pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout) values_B.locksroot = pending_transfers_tree.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree.transfers)) # Reveal secrets before settlement window ends reveal_secrets(A, pending_transfers_tree.unlockable) # Unlock fails before channel is not settled with pytest.raises(TransactionFailed): token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call() channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Unlock fails before channel is not settled with pytest.raises(TransactionFailed): token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call() close_and_update_channel(channel_identifier, A, values_A, B, values_B) # Unlock fails before settlement window is over and channel is not settled with pytest.raises(TransactionFailed): token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call() # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) # Unlock fails before settle is called with pytest.raises(TransactionFailed): token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call() # settle channel call_settle(token_network, channel_identifier, A, values_A, B, values_B) pre_balance_A = custom_token.functions.balanceOf(A).call() pre_balance_B = custom_token.functions.balanceOf(B).call() pre_balance_contract = custom_token.functions.balanceOf( token_network.address).call() # Unlock works after channel is settled token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call_and_transact() balance_A = custom_token.functions.balanceOf(A).call() balance_B = custom_token.functions.balanceOf(B).call() balance_contract = custom_token.functions.balanceOf( token_network.address).call() assert balance_A == pre_balance_A + 9 assert balance_B == pre_balance_B + 6 assert balance_contract == pre_balance_contract - values_B.locked_amounts.locked
def test_channel_unlock_registered_expired_lock_refunds( web3, custom_token, token_network, secret_registry_contract, create_channel, channel_deposit, get_accounts, close_and_update_channel, ): """ unlock() should refund tokens locked with secrets revealed after the expiration """ (A, B) = get_accounts(2) max_lock_expiration = 3 settle_timeout = 8 values_A = ChannelValues(deposit=20, transferred=5) values_B = ChannelValues(deposit=30, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Mock pending transfers data pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], min_expiration_delta=max_lock_expiration - 2, max_expiration_delta=max_lock_expiration, ) values_B.locksroot = pending_transfers_tree.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree.transfers)) # Locks expire web3.testing.mine(max_lock_expiration) # Secrets are revealed before settlement window, but after expiration for (_, _, secrethash, secret) in pending_transfers_tree.unlockable: secret_registry_contract.functions.registerSecret( secret).call_and_transact({"from": A}) assert (secret_registry_contract.functions.getSecretRevealBlockHeight( secrethash).call() == web3.eth.blockNumber) close_and_update_channel(channel_identifier, A, values_A, B, values_B) web3.testing.mine(settle_timeout) # settle channel call_settle(token_network, channel_identifier, A, values_A, B, values_B) pre_balance_A = custom_token.functions.balanceOf(A).call() pre_balance_B = custom_token.functions.balanceOf(B).call() pre_balance_contract = custom_token.functions.balanceOf( token_network.address).call() # Unlock works after channel is settled token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call_and_transact() balance_A = custom_token.functions.balanceOf(A).call() balance_B = custom_token.functions.balanceOf(B).call() balance_contract = custom_token.functions.balanceOf( token_network.address).call() # check that all tokens have been refunded, as locks have expired already assert balance_A == pre_balance_A assert balance_B == pre_balance_B + values_B.locked_amounts.locked assert balance_contract == pre_balance_contract - values_B.locked_amounts.locked
def test_channel_unlock( web3, custom_token, token_network, create_channel, channel_deposit, get_accounts, close_and_update_channel, reveal_secrets, ): """ unlock() on pending transfers with unlockable and expired locks should split the locked amount accordingly, to both parties """ (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues(deposit=20, transferred=5) values_B = ChannelValues(deposit=30, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Mock pending transfers data pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout) values_B.locksroot = pending_transfers_tree.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree.transfers)) # Reveal secrets before settlement window ends reveal_secrets(A, pending_transfers_tree.unlockable) close_and_update_channel(channel_identifier, A, values_A, B, values_B) # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) call_settle(token_network, channel_identifier, A, values_A, B, values_B) pre_balance_A = custom_token.functions.balanceOf(A).call() pre_balance_B = custom_token.functions.balanceOf(B).call() pre_balance_contract = custom_token.functions.balanceOf( token_network.address).call() info_B = token_network.functions.getChannelParticipantInfo( channel_identifier, B, A).call() assert info_B[ParticipantInfoIndex.LOCKSROOT] == values_B.locksroot assert info_B[ ParticipantInfoIndex.LOCKED_AMOUNT] == values_B.locked_amounts.locked # Unlock the tokens token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call_and_transact() info_B = token_network.functions.getChannelParticipantInfo( channel_identifier, B, A).call() assert info_B[ParticipantInfoIndex.LOCKSROOT] == EMPTY_LOCKSROOT assert info_B[ParticipantInfoIndex.LOCKED_AMOUNT] == 0 balance_A = custom_token.functions.balanceOf(A).call() balance_B = custom_token.functions.balanceOf(B).call() balance_contract = custom_token.functions.balanceOf( token_network.address).call() assert balance_A == pre_balance_A + 9 assert balance_B == pre_balance_B + 6 assert balance_contract == pre_balance_contract - values_B.locked_amounts.locked
def test_unlock_channel_event( web3, token_network, secret_registry_contract, create_channel, channel_deposit, get_accounts, close_and_update_channel, reveal_secrets, event_handler, ): """ Successful unlock() should cause an UNLOCKED event """ (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues(deposit=20, transferred=5) values_B = ChannelValues(deposit=30, transferred=40) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(channel_identifier, A, values_A.deposit, B) channel_deposit(channel_identifier, B, values_B.deposit, A) # Mock pending transfers data pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout + 100) values_B.locksroot = pending_transfers_tree.merkle_root values_B.locked_amounts = LockedAmounts( claimable_locked=get_locked_amount(pending_transfers_tree.transfers)) # Reveal secrets before settlement window ends reveal_secrets(A, pending_transfers_tree.unlockable) close_and_update_channel(channel_identifier, A, values_A, B, values_B) # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) call_settle(token_network, channel_identifier, A, values_A, B, values_B) ev_handler = event_handler(token_network) # Unlock the tokens txn_hash = token_network.functions.unlock( channel_identifier, A, B, pending_transfers_tree.packed_transfers).call_and_transact() unlocked_amount = get_unlocked_amount( secret_registry_contract, pending_transfers_tree.packed_transfers) # Add event ev_handler.add( txn_hash, ChannelEvent.UNLOCKED, check_channel_unlocked( channel_identifier, A, B, values_B.locksroot, unlocked_amount, values_B.locked_amounts.locked - unlocked_amount, ), ) # Check that event was properly emitted ev_handler.check()