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()
def test_channel_unlock(web3, custom_token, token_network, secret_registry_contract, create_channel, channel_deposit, get_accounts, create_balance_proof, create_balance_proof_update_signature, event_handler): (A, B) = get_accounts(2) settle_timeout = 8 deposit_A = 20 deposit_B = 30 additional_hash = fake_hex(32, '02') locksroot1 = fake_hex(32, '00') locked_amount1 = 0 # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(A, deposit_A, B) channel_deposit(B, deposit_B, A) # Mock pending transfers data pending_transfers_tree = get_pending_transfers_tree( web3, [1, 3, 5], [2, 4], settle_timeout) locksroot_bytes = get_merkle_root(pending_transfers_tree.merkle_tree) locksroot2 = '0x' + locksroot_bytes.hex() locked_amount2 = get_locked_amount(pending_transfers_tree.transfers) # Create balance proofs balance_proof_A = create_balance_proof(channel_identifier, A, 10, locked_amount1, 5, locksroot1, additional_hash) balance_proof_B = create_balance_proof(channel_identifier, B, 5, locked_amount2, 3, locksroot2, additional_hash) balance_proof_update_signature_B = create_balance_proof_update_signature( B, channel_identifier, *balance_proof_A) # Reveal secrets before settlement window ends for lock in pending_transfers_tree.unlockable: secret_registry_contract.functions.registerSecret(lock[3]).transact( {'from': A}) assert secret_registry_contract.functions.getSecretRevealBlockHeight( lock[2]).call() == web3.eth.blockNumber # Close channel and update balance proofs token_network.functions.closeChannel(B, *balance_proof_B).transact( {'from': A}) token_network.functions.updateNonClosingBalanceProof( A, B, *balance_proof_A, balance_proof_update_signature_B).transact({'from': B}) # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) # Settle the channel token_network.functions.settleChannel(A, 10, locked_amount1, locksroot1, B, 5, locked_amount2, locksroot2).transact({'from': A}) 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() # TODO to be moved to a separate test ev_handler = event_handler(token_network) # Unlock the tokens txn_hash = token_network.functions.unlock( A, B, pending_transfers_tree.packed_transfers).transact() unlocked_amount = get_unlocked_amount( secret_registry_contract, pending_transfers_tree.packed_transfers) 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 - locked_amount2 # TODO to be moved to a separate test ev_handler.add( txn_hash, EVENT_CHANNEL_UNLOCKED, check_channel_unlocked(channel_identifier, A, unlocked_amount, locked_amount2 - unlocked_amount)) ev_handler.check()
def test_unlock_channel_event( web3, token_network, secret_registry_contract, create_channel, channel_deposit, get_accounts, close_and_update_channel, event_handler, ): (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues( deposit=20, transferred=5, locked=0, locksroot=EMPTY_MERKLE_ROOT, ) values_B = ChannelValues( deposit=30, transferred=40, ) # Create channel and deposit channel_identifier = create_channel(A, B, settle_timeout)[0] channel_deposit(A, values_A.deposit, B) channel_deposit(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) locksroot_bytes = get_merkle_root(pending_transfers_tree.merkle_tree) values_B.locksroot = '0x' + locksroot_bytes.hex() values_B.locked = get_locked_amount(pending_transfers_tree.transfers) # Reveal secrets before settlement window ends for lock in pending_transfers_tree.unlockable: secret_registry_contract.functions.registerSecret(lock[3]).transact({'from': A}) assert secret_registry_contract.functions.getSecretRevealBlockHeight( lock[2], ).call() == web3.eth.blockNumber close_and_update_channel( A, values_A, B, values_B, ) # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) call_settle(token_network, A, values_A, B, values_B) ev_handler = event_handler(token_network) # Unlock the tokens txn_hash = token_network.functions.unlock( A, B, pending_transfers_tree.packed_transfers, ).transact() unlocked_amount = get_unlocked_amount( secret_registry_contract, pending_transfers_tree.packed_transfers, ) # Add event ev_handler.add(txn_hash, EVENT_CHANNEL_UNLOCKED, check_channel_unlocked( channel_identifier, A, unlocked_amount, values_B.locked - unlocked_amount, )) # Check that event was properly emitted ev_handler.check()
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, ): (A, B) = get_accounts(2) settle_timeout = 8 values_A = ChannelValues( deposit=20, transferred=5, locked=0, locksroot=EMPTY_MERKLE_ROOT, ) values_B = ChannelValues( deposit=30, transferred=40, ) # Create channel and deposit create_channel(A, B, settle_timeout)[0] channel_deposit(A, values_A.deposit, B) channel_deposit(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 = get_locked_amount(pending_transfers_tree.transfers) # Reveal secrets before settlement window ends reveal_secrets(A, pending_transfers_tree.unlockable) close_and_update_channel( A, values_A, B, values_B, ) # Settlement window must be over before settling the channel web3.testing.mine(settle_timeout) call_settle(token_network, A, values_A, B, values_B) ev_handler = event_handler(token_network) # Unlock the tokens txn_hash = token_network.functions.unlock( A, B, pending_transfers_tree.packed_transfers, ).transact() unlocked_amount = get_unlocked_amount( secret_registry_contract, pending_transfers_tree.packed_transfers, ) # Add event ev_handler.add( txn_hash, EVENT_CHANNEL_UNLOCKED, check_channel_unlocked( A, B, values_B.locksroot, unlocked_amount, values_B.locked - unlocked_amount, )) # Check that event was properly emitted ev_handler.check()