def test_cooperative_wrong_balance_proof( channel_manager: ChannelManager, confirmed_open_channel: Channel, sender_address: str ): channel_id = (confirmed_open_channel.sender, confirmed_open_channel.block) channel_rec = channel_manager.channels[channel_id] sig1 = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig1) sig2 = encode_hex(confirmed_open_channel.create_transfer(1)) with pytest.raises(InvalidBalanceProof): channel_manager.sign_close(sender_address, confirmed_open_channel.block, sig2) assert channel_rec.is_closed is False
def test_cooperative_wrong_balance_proof( channel_manager: ChannelManager, confirmed_open_channel: Channel, sender_address: str ): channel_id = (confirmed_open_channel.sender, confirmed_open_channel.block) channel_rec = channel_manager.channels[channel_id] sig1 = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig1) sig2 = encode_hex(confirmed_open_channel.create_transfer(1)) with pytest.raises(InvalidBalanceProof): channel_manager.sign_close(sender_address, confirmed_open_channel.block, sig2) assert channel_rec.is_closed is False
def test_balances( channel_manager: ChannelManager, confirmed_open_channel: Channel, wait_for_blocks, sender_address: str, use_tester: bool ): blockchain = channel_manager.blockchain initial_liquid_balance = channel_manager.get_liquid_balance() initial_locked_balance = channel_manager.get_locked_balance() if use_tester: assert initial_liquid_balance == 0 assert initial_locked_balance == 0 sig = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig) assert channel_manager.get_liquid_balance() == initial_liquid_balance assert channel_manager.get_locked_balance() == 5 receiver_sig = channel_manager.sign_close(sender_address, confirmed_open_channel.block, 5) confirmed_open_channel.close_cooperatively(receiver_sig) wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) assert channel_manager.get_liquid_balance() == initial_liquid_balance + 5 assert channel_manager.get_locked_balance() == initial_locked_balance
def test_cooperative( channel_manager: ChannelManager, confirmed_open_channel: Channel, receiver_address: str, web3: Web3, token_contract: Contract, wait_for_blocks, sender_address: str ): blockchain = channel_manager.blockchain channel_id = (confirmed_open_channel.sender, confirmed_open_channel.block) sig1 = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig1) receiver_sig = channel_manager.sign_close(sender_address, confirmed_open_channel.block, 5) channel_rec = channel_manager.channels[channel_id] assert channel_rec.is_closed is True block_before = web3.eth.blockNumber confirmed_open_channel.close_cooperatively(receiver_sig) wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) logs = get_logs(token_contract, 'Transfer', from_block=block_before - 1) assert len([l for l in logs if is_same_address(l['args']['_to'], receiver_address) and l['args']['_value'] == 5]) == 1 assert len([l for l in logs if is_same_address(l['args']['_to'], sender_address) and l['args']['_value'] == 5]) == 1 wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) assert channel_id not in channel_manager.channels
def test_balances( channel_manager: ChannelManager, confirmed_open_channel: Channel, wait_for_blocks, sender_address: str, use_tester: bool ): blockchain = channel_manager.blockchain initial_liquid_balance = channel_manager.get_liquid_balance() initial_locked_balance = channel_manager.get_locked_balance() if use_tester: assert initial_liquid_balance == 0 assert initial_locked_balance == 0 sig = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig) assert channel_manager.get_liquid_balance() == initial_liquid_balance assert channel_manager.get_locked_balance() == 5 receiver_sig = channel_manager.sign_close(sender_address, confirmed_open_channel.block, 5) confirmed_open_channel.close_cooperatively(receiver_sig) wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) assert channel_manager.get_liquid_balance() == initial_liquid_balance + 5 assert channel_manager.get_locked_balance() == initial_locked_balance
def test_cooperative( channel_manager: ChannelManager, confirmed_open_channel: Channel, receiver_address: str, web3: Web3, token_contract: Contract, wait_for_blocks, sender_address: str ): blockchain = channel_manager.blockchain channel_id = (confirmed_open_channel.sender, confirmed_open_channel.block) sig1 = encode_hex(confirmed_open_channel.create_transfer(5)) channel_manager.register_payment(sender_address, confirmed_open_channel.block, 5, sig1) receiver_sig = channel_manager.sign_close(sender_address, confirmed_open_channel.block, 5) channel_rec = channel_manager.channels[channel_id] assert channel_rec.is_closed is True block_before = web3.eth.blockNumber confirmed_open_channel.close_cooperatively(receiver_sig) wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) logs = get_logs(token_contract, 'Transfer', from_block=block_before - 1) assert len([l for l in logs if is_same_address(l['args']['_to'], receiver_address) and l['args']['_value'] == 5]) == 1 assert len([l for l in logs if is_same_address(l['args']['_to'], sender_address) and l['args']['_value'] == 5]) == 1 wait_for_blocks(blockchain.n_confirmations) gevent.sleep(blockchain.poll_interval) assert channel_id not in channel_manager.channels
def test_close_settled(client: Client, channel_manager: ChannelManager, web3: Web3, wait_for_blocks): sender = client.context.address receiver = channel_manager.receiver channel = client.open_channel(receiver, 10) wait_for_blocks(channel_manager.n_confirmations + 1) channel_manager.register_payment(sender, channel.block, 2, encode_hex(channel.create_transfer(2))) receiver_sig = channel_manager.sign_close(sender, channel.block, 2) channel.close_cooperatively(receiver_sig) wait_for_blocks(channel_manager.n_confirmations + 1) channel_manager.stop() # don't update state from this point on channel_manager.join() state = channel_manager.state tx_count_before = web3.eth.getTransactionCount(receiver) close_open_channels(channel_manager.private_key, state, channel_manager.channel_manager_contract, wait=lambda: wait_for_blocks(1)) tx_count_after = web3.eth.getTransactionCount(receiver) assert tx_count_after == tx_count_before wait_for_blocks(1)