def test_update_must_fail_with_a_channel_address(tester_channels, private_keys): """ updateTransfer must not accept a transfer signed with the wrong channel address. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) wrong_channel = make_address() # make a transfer where pkey1 is the target transfer_wrong_recipient = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2 ** 32)), token=channel0.token_address, channel=wrong_channel, transferred_amount=10, recipient=channel1.our_address, locksroot=EMPTY_MERKLE_ROOT, ) our_address = privatekey_to_address(pkey0) our_sign_key = PrivateKey(pkey0) transfer_wrong_recipient.sign(our_sign_key, our_address) nettingchannel.close(sender=pkey0) transfer_wrong_recipient_hash = sha3(transfer_wrong_recipient.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.updateTransfer( transfer_wrong_recipient.nonce, transfer_wrong_recipient.transferred_amount, transfer_wrong_recipient.locksroot, transfer_wrong_recipient_hash, transfer_wrong_recipient.signature, sender=pkey1, )
def test_close_accepts_only_transfer_from_participants(tester_channels, private_keys): """ Close must not accept a transfer signed by a non participant. """ pkey0, _, nettingchannel, channel0, _ = tester_channels[0] nonparticipant_key = private_keys[2] opened_block = nettingchannel.opened(sender=pkey0) # make a transfer where pkey0 is the target transfer_nonparticipant = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2**32)), token=channel0.token_address, channel=channel0.identifier, transferred_amount=10, recipient=channel0.our_state.address, locksroot=EMPTY_MERKLE_ROOT, ) nonparticipant_address = privatekey_to_address(nonparticipant_key) nonparticipant_sign_key = PrivateKey(nonparticipant_key) transfer_nonparticipant.sign(nonparticipant_sign_key, nonparticipant_address) transfer_nonparticipant_hash = sha3( transfer_nonparticipant.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.close( transfer_nonparticipant.nonce, transfer_nonparticipant.transferred_amount, transfer_nonparticipant.locksroot, transfer_nonparticipant_hash, transfer_nonparticipant.signature, sender=pkey0, )
def test_close_wrong_channel(tester_channels): """ Close must not accept a transfer aimed at a different channel. """ pkey0, pkey1, nettingchannel, channel0, _ = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) wrong_address = make_address() # make a transfer where the recipient is totally wrong transfer_wrong_channel = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2**32)), token=channel0.token_address, channel=wrong_address, transferred_amount=10, recipient=channel0.our_state.address, locksroot=EMPTY_MERKLE_ROOT, ) transfer_wrong_channel.sign(PrivateKey(pkey1), privatekey_to_address(pkey1)) transfer_wrong_channel_hash = sha3( transfer_wrong_channel.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.close( transfer_wrong_channel.nonce, transfer_wrong_channel.transferred_amount, transfer_wrong_channel.locksroot, transfer_wrong_channel_hash, transfer_wrong_channel.signature, sender=pkey0, )
def make_receive_transfer_direct( payment_network_identifier, channel_state, privkey, nonce, transferred_amount, locksroot=EMPTY_MERKLE_ROOT): address = privatekey_to_address(privkey.secret) if address not in (channel_state.our_state.address, channel_state.partner_state.address): raise ValueError('Private key does not match any of the participants.') identifier = nonce mediated_transfer_msg = DirectTransfer( identifier, nonce, channel_state.token_address, channel_state.identifier, transferred_amount, channel_state.partner_state.address, locksroot, ) mediated_transfer_msg.sign(privkey, address) balance_proof = balanceproof_from_envelope(mediated_transfer_msg) receive_directtransfer = ReceiveTransferDirect( payment_network_identifier, channel_state.token_address, identifier, balance_proof, ) return receive_directtransfer
def test_close_accepts_only_transfer_from_participants(tester_channels, private_keys): """ Close must not accept a transfer from a non participant. """ pkey0, _, nettingchannel, channel0, _ = tester_channels[0] nonparticipant_key = private_keys[2] opened_block = nettingchannel.opened(sender=pkey0) # make a transfer where pkey0 is the target transfer_nonparticipant = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2**32)), token=channel0.token_address, transferred_amount=10, recipient=channel0.our_address, locksroot='', ) nonparticipant_address = privatekey_to_address(nonparticipant_key) nonparticipant_sign_key = PrivateKey(nonparticipant_key) transfer_nonparticipant.sign(nonparticipant_sign_key, nonparticipant_address) transfer_nonparticipant_data = str(transfer_nonparticipant.packed().data) with pytest.raises(TransactionFailed): nettingchannel.close(transfer_nonparticipant_data, sender=pkey0)
def test_close_accepts_only_transfer_from_participants(tester_channels, private_keys): """ Close must not accept a transfer signed by a non participant. """ pkey0, _, nettingchannel, channel0, _ = tester_channels[0] nonparticipant_key = private_keys[2] opened_block = nettingchannel.opened(sender=pkey0) # make a transfer where pkey0 is the target transfer_nonparticipant = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2 ** 32)), token=channel0.token_address, channel=channel0.channel_address, transferred_amount=10, recipient=channel0.our_address, locksroot=EMPTY_MERKLE_ROOT, ) nonparticipant_address = privatekey_to_address(nonparticipant_key) nonparticipant_sign_key = PrivateKey(nonparticipant_key) transfer_nonparticipant.sign(nonparticipant_sign_key, nonparticipant_address) transfer_nonparticipant_hash = sha3(transfer_nonparticipant.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.close( transfer_nonparticipant.nonce, transfer_nonparticipant.transferred_amount, transfer_nonparticipant.locksroot, transfer_nonparticipant_hash, transfer_nonparticipant.signature, sender=pkey0, )
def test_signature_split(tester_chain, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address) privkey, address = make_privkey_address() message_identifier = random.randint(0, UINT64_MAX) msg = DirectTransfer(message_identifier=message_identifier, payment_identifier=1, nonce=1, registry_address='x' * 20, token='x' * 20, channel=auxiliary.address, transferred_amount=10, recipient='y' * 20, locksroot=HASH) msg.sign(privkey, address) msg = msg.encode() # signature = len(msg) - 65 signature = msg[len(msg) - 65:] signature = signature[:-1] + chr(27).encode() r, s, v = auxiliary.signatureSplit(signature) assert v == 27 assert r == signature[:32] assert s == signature[32:64] signature = signature[:-1] + chr(28).encode() _, _, v = auxiliary.signatureSplit(signature) assert v == 28 with pytest.raises(TransactionFailed): signature = signature[:-1] + chr(4).encode() r, s, v = auxiliary.signatureSplit(signature)
def test_decode_direct_transfer(private_keys, settle_timeout, tester_state, tester_token, tester_events, tester_registry): privatekey0 = tester.DEFAULT_KEY privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) dtester = deploy_decoder_tester(tester_token.address, address0, address1, settle_timeout) locksroot = sha3("Waldemarstr") message = DirectTransfer(identifier=1, nonce=2, asset=tester_token.address, transferred_amount=1337, recipient=address1, locksroot=locksroot) message.sign(PrivateKey(privatekey0, ctx=GLOBAL_CTX, raw=True), address0) _, publickey = wrap_and_validate(message.encode()) recovered_address = address_from_key(publickey) assert recovered_address == address0 assert dtester.testDecodeTransfer(message.encode()) is True assert dtester.decodedNonce() == 2 assert dtester.decodedAsset() == tester_token.address.encode('hex') assert dtester.decodedRecipient() == address1.encode('hex') assert dtester.decodedAmount() == 1337 assert dtester.decodedLocksroot() == locksroot
def test_close_wrong_channel(tester_channels): """ Close must not accept a transfer aimed at a different channel. """ pkey0, pkey1, nettingchannel, channel0, _ = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) wrong_address = make_address() # make a transfer where the recipient is totally wrong transfer_wrong_channel = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2 ** 32)), token=channel0.token_address, channel=wrong_address, transferred_amount=10, recipient=channel0.our_address, locksroot=EMPTY_MERKLE_ROOT, ) transfer_wrong_channel.sign(PrivateKey(pkey1), privatekey_to_address(pkey1)) transfer_wrong_channel_hash = sha3(transfer_wrong_channel.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.close( transfer_wrong_channel.nonce, transfer_wrong_channel.transferred_amount, transfer_wrong_channel.locksroot, transfer_wrong_channel_hash, transfer_wrong_channel.signature, sender=pkey0, )
def test_recoverAddressFromSignature(tester_chain, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address) privkey, address = make_privkey_address() msg = DirectTransfer( identifier=1, nonce=1, token='x' * 20, channel=auxiliary.address, transferred_amount=10, recipient='y' * 20, locksroot=HASH ) msg.sign(privkey, address) data = msg.encode() signature = data[-65:] extra_hash = sha3(data[:-65]) computed_address = auxiliary.recoverAddressFromSignature( msg.nonce, msg.transferred_amount, msg.locksroot, extra_hash, signature ) assert normalize_address(computed_address) == msg.sender
def test_recoverAddressFromSignature(tester_chain, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address) privkey, address = make_privkey_address() message_identifier = random.randint(0, UINT64_MAX) msg = DirectTransfer(message_identifier=message_identifier, payment_identifier=1, nonce=1, registry_address='x' * 20, token='x' * 20, channel=auxiliary.address, transferred_amount=10, recipient='y' * 20, locksroot=HASH) msg.sign(privkey, address) data = msg.encode() signature = data[-65:] extra_hash = sha3(data[:-65]) computed_address = auxiliary.recoverAddressFromSignature( msg.nonce, msg.transferred_amount, msg.locksroot, extra_hash, signature) assert normalize_address(computed_address) == msg.sender
def test_signature_split(tester_chain, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address) privkey, address = make_privkey_address() msg = DirectTransfer( identifier=1, nonce=1, token='x' * 20, channel=auxiliary.address, transferred_amount=10, recipient='y' * 20, locksroot=HASH ) msg.sign(privkey, address) msg = msg.encode() # signature = len(msg) - 65 signature = msg[len(msg) - 65:] signature = signature[:-1] + chr(27).encode() r, s, v = auxiliary.signatureSplit(signature) assert v == 27 assert r == signature[:32] assert s == signature[32:64] signature = signature[:-1] + chr(28).encode() _, _, v = auxiliary.signatureSplit(signature) assert v == 28 with pytest.raises(TransactionFailed): signature = signature[:-1] + chr(4).encode() r, s, v = auxiliary.signatureSplit(signature)
def test_signature_split(tester_state, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_state, tester_nettingchannel_library_address) privkey, address = make_privkey_address() msg = DirectTransfer(identifier=1, nonce=1, token='x' * 20, transferred_amount=10, recipient='y' * 20, locksroot=HASH) msg.sign(privkey, address) msg = msg.encode() # signature = len(msg) - 65 signature = msg[len(msg) - 65:] signature = signature[:-1] + chr(27) r, s, v = auxiliary.signatureSplit(signature) assert v == 27 assert r == signature[:32] assert s == signature[32:64] signature = signature[:-1] + chr(28) _, _, v = auxiliary.signatureSplit(signature) assert v == 28 with pytest.raises(TransactionFailed): signature = signature[:-1] + chr(4) r, s, v = auxiliary.signatureSplit(signature)
def test_update_must_fail_with_a_nonparticipant_transfer( tester_channels, private_keys): """ updateTransfer must not accept a transfer from a non participant. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] nonparticipant_key = private_keys[2] # make a transfer where pkey1 is the target transfer_nonparticipant = DirectTransfer( identifier=1, nonce=1, token=channel0.token_address, transferred_amount=10, recipient=channel1.our_address, locksroot='', ) nonparticipant_address = privatekey_to_address(nonparticipant_key) nonparticipant_sign_key = PrivateKey(nonparticipant_key, ctx=GLOBAL_CTX, raw=True) transfer_nonparticipant.sign(nonparticipant_sign_key, nonparticipant_address) transfer_nonparticipant_data = str(transfer_nonparticipant.packed().data) nettingchannel.close('', sender=pkey0) with pytest.raises(TransactionFailed): nettingchannel.updateTransfer(transfer_nonparticipant_data, sender=pkey1)
def test_update_must_fail_with_a_channel_address(tester_channels): """ updateTransfer must not accept a transfer signed with the wrong channel address. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) wrong_channel = factories.make_address() # make a transfer where pkey1 is the target transfer_wrong_recipient = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2**32)), token=channel0.token_address, channel=wrong_channel, transferred_amount=10, recipient=channel1.our_state.address, locksroot=EMPTY_MERKLE_ROOT, ) our_address = privatekey_to_address(pkey0) our_sign_key = PrivateKey(pkey0) transfer_wrong_recipient.sign(our_sign_key, our_address) nettingchannel.close(sender=pkey0) transfer_wrong_recipient_hash = sha3( transfer_wrong_recipient.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.updateTransfer( transfer_wrong_recipient.nonce, transfer_wrong_recipient.transferred_amount, transfer_wrong_recipient.locksroot, transfer_wrong_recipient_hash, transfer_wrong_recipient.signature, sender=pkey1, )
def test_update_must_fail_with_a_wrong_recipient(tester_channels, private_keys): """ updateTransfer must not accept a transfer from a non participant. """ pkey0, pkey1, nettingchannel, channel0, _ = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) nonparticipant_address = privatekey_to_address(private_keys[2]) # make a transfer where pkey1 is the target transfer_wrong_recipient = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2**32)), token=channel0.token_address, transferred_amount=10, recipient=nonparticipant_address, locksroot='', ) our_address = privatekey_to_address(pkey0) our_sign_key = PrivateKey(pkey0) transfer_wrong_recipient.sign(our_sign_key, our_address) transfer_wrong_recipient_data = str(transfer_wrong_recipient.packed().data) nettingchannel.close('', sender=pkey0) with pytest.raises(TransactionFailed): nettingchannel.updateTransfer(transfer_wrong_recipient_data, sender=pkey1)
def test_direct_transfer(iterations=ITERATIONS): nonce = 1 asset = ADDRESS balance = 1 recipient = ADDRESS locksroot = HASH msg = DirectTransfer(nonce, asset, balance, recipient, locksroot) msg.sign(PRIVKEY) run_timeit('DirectTransfer', msg, iterations=iterations)
def make_receive_transfer_direct( payment_network_identifier, channel_state, privkey, nonce, transferred_amount, locksroot=EMPTY_MERKLE_ROOT, registry_address=UNIT_REGISTRY_IDENTIFIER, locked_amount=None, ): address = privatekey_to_address(privkey.secret) if address not in (channel_state.our_state.address, channel_state.partner_state.address): raise ValueError('Private key does not match any of the participants.') if locked_amount is None: locked_amount = channel.get_amount_locked(channel_state.our_state) message_identifier = random.randint(0, UINT64_MAX) payment_identifier = nonce mediated_transfer_msg = DirectTransfer( message_identifier, payment_identifier, nonce, registry_address, channel_state.token_address, channel_state.identifier, transferred_amount, locked_amount, channel_state.partner_state.address, locksroot, ) mediated_transfer_msg.sign(privkey, address) balance_proof = balanceproof_from_envelope(mediated_transfer_msg) receive_directtransfer = ReceiveTransferDirect( payment_network_identifier, channel_state.token_address, message_identifier, payment_identifier, balance_proof, ) return receive_directtransfer
def test_direct_transfer(iterations=ITERATIONS): identifier = 1 nonce = 1 token = ADDRESS balance = 1 recipient = ADDRESS locksroot = HASH msg = DirectTransfer( identifier, nonce, token, balance, recipient, locksroot, ) msg.sign(PRIVKEY, ADDRESS) run_timeit('DirectTransfer', msg, iterations=iterations)
def test_update_must_fail_with_a_nonparticipant_transfer( tester_registry_address, tester_channels, private_keys, ): """ updateTransfer must not accept a transfer from a non participant. """ pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0] nonparticipant_key = private_keys[2] opened_block = nettingchannel.opened(sender=pkey0) # make a transfer where pkey1 is the target message_identifier = random.randint(0, UINT64_MAX) transfer_nonparticipant = DirectTransfer( message_identifier=message_identifier, payment_identifier=1, nonce=1 + (opened_block * (2**32)), registry_address=tester_registry_address, token=channel0.token_address, channel=channel0.identifier, transferred_amount=10, locked_amount=0, recipient=channel1.our_state.address, locksroot=EMPTY_MERKLE_ROOT, ) nonparticipant_address = privatekey_to_address(nonparticipant_key) nonparticipant_sign_key = PrivateKey(nonparticipant_key) transfer_nonparticipant.sign(nonparticipant_sign_key, nonparticipant_address) nettingchannel.close(sender=pkey0) transfer_nonparticipant_hash = sha3( transfer_nonparticipant.packed().data[:-65]) with pytest.raises(TransactionFailed): nettingchannel.updateTransfer( transfer_nonparticipant.nonce, transfer_nonparticipant.transferred_amount, transfer_nonparticipant.locksroot, transfer_nonparticipant_hash, transfer_nonparticipant.signature, sender=pkey1, )
def test_close_wrong_recipient(tester_channels, private_keys): """ Close must not accept a transfer aimed at a non recipient. """ pkey0, pkey1, nettingchannel, channel0, _ = tester_channels[0] opened_block = nettingchannel.opened(sender=pkey0) nonparticipant_address = privatekey_to_address(private_keys[2]) # make a transfer where the recipient is totally wrong transfer_wrong_recipient = DirectTransfer( identifier=1, nonce=1 + (opened_block * (2 ** 32)), token=channel0.token_address, transferred_amount=10, recipient=nonparticipant_address, locksroot='', ) transfer_wrong_recipient.sign(PrivateKey(pkey1), privatekey_to_address(pkey1)) transfer_wrong_recipient_data = str(transfer_wrong_recipient.packed().data) with pytest.raises(TransactionFailed): nettingchannel.close(transfer_wrong_recipient_data, sender=pkey0)
def test_recoverAddressFromSignature(tester_state, tester_nettingchannel_library_address): auxiliary = deploy_auxiliary_tester(tester_state, tester_nettingchannel_library_address) privkey, address = make_privkey_address() msg = DirectTransfer(identifier=1, nonce=1, token='x' * 20, channel=auxiliary.address, transferred_amount=10, recipient='y' * 20, locksroot=HASH) msg.sign(privkey, address) data = msg.encode() signature = data[-65:] extra_hash = sha3(data[:-65]) computed_address = auxiliary.recoverAddressFromSignature( msg.nonce, msg.transferred_amount, msg.locksroot, extra_hash, signature) assert unhexlify(computed_address) == msg.sender
def test_decode_direct_transfer( private_keys, settle_timeout, tester_state, tester_token, tester_events, tester_registry): privatekey0 = tester.DEFAULT_KEY privatekey1 = private_keys[1] address0 = privatekey_to_address(privatekey0) address1 = privatekey_to_address(privatekey1) dtester = deploy_decoder_tester(tester_token.address, address0, address1, settle_timeout) locksroot = sha3("Waldemarstr") message = DirectTransfer( identifier=1, nonce=2, asset=tester_token.address, transferred_amount=1337, recipient=address1, locksroot=locksroot ) message.sign(PrivateKey(privatekey0, ctx=GLOBAL_CTX, raw=True), address0) _, publickey = wrap_and_validate(message.encode()) recovered_address = address_from_key(publickey) assert recovered_address == address0 assert dtester.testDecodeTransfer(message.encode()) is True assert dtester.decodedNonce() == 2 assert dtester.decodedAsset() == tester_token.address.encode('hex') assert dtester.decodedRecipient() == address1.encode('hex') assert dtester.decodedAmount() == 1337 assert dtester.decodedLocksroot() == locksroot
def test_settle(state, channel, token, asset_amount, events): half_amount = asset_amount / 2 assert token.transfer(tester.a1, half_amount) is True token1 = ABIContract( state, token.translator, token.address, default_key=tester.k1, ) assert token.approve(channel.address, half_amount) is True assert token1.approve(channel.address, half_amount) is True channel1 = ABIContract( state, channel.translator, channel.address, default_key=tester.k1, ) channel.deposit(half_amount) channel1.deposit(half_amount) secret1 = 'x' * 32 hashlock1 = sha3(secret1) lock_amount1 = 29 lock_expiration1 = 1158003 lock1 = Lock(lock_amount1, lock_expiration1, hashlock1) lockhash1 = sha3(lock1.as_bytes) merkleproof1 = [lockhash1] locksroot1 = merkleroot([lockhash1], merkleproof1) nonce1 = 1 asset = token.address transfered_amount1 = 1 recipient = tester.a1 locksroot = locksroot1 msg1 = DirectTransfer( nonce1, asset, transfered_amount1, recipient, locksroot, ) msg1.sign(tester.k0) packed = msg1.packed() direct_transfer1 = str(packed.data) secret2 = 'y' * 32 hashlock2 = sha3(secret2) lock_amount2 = 20 lock_expiration2 = 1158005 lock2 = Lock(lock_amount2, lock_expiration2, hashlock2) lockhash2 = sha3(lock2.as_bytes) merkleproof2 = [lockhash2] locksroot2 = merkleroot([lockhash2], merkleproof2) locksroot = locksroot2 nonce2 = 2 transfered_amount2 = 3 msg2 = DirectTransfer( nonce2, token.address, # asset transfered_amount2, tester.a0, # recipient locksroot, ) msg2.sign(tester.k1) packed = msg2.packed() direct_transfer2 = str(packed.data) # not yet closed. should fail with pytest.raises(TransactionFailed): channel.settle() channel.close(direct_transfer1, direct_transfer2) channel.unlock( str(lock1.as_bytes), ''.join(merkleproof1), secret1, ) channel.unlock(str(lock2.as_bytes), ''.join(merkleproof2), secret2, sender=tester.k1) secret4 = 'k' * 32 hashlock4 = sha3(secret4) lock_amount4 = 23 lock_expiration4 = 31 lock4 = Lock(lock_amount4, lock_expiration4, hashlock4) hashlock4 = sha3(lock4.as_bytes) merkleproof4 = [hashlock4] # has now message, should fail with pytest.raises(TransactionFailed): channel.unlock( str(lock4.as_bytes), ''.join(merkleproof4), secret4, sender=tester.k1, ) # still timeout with pytest.raises(TransactionFailed): channel.settle() state.block.number = state.block.number + 40 # timeout over channel.settle() balance1 = half_amount + (transfered_amount2 - transfered_amount1) + lock_amount1 - lock_amount2 balance2 = half_amount + (transfered_amount1 - transfered_amount2) - lock_amount1 + lock_amount2 assert token.balanceOf(tester.a0) == balance1 assert token.balanceOf(tester.a1) == balance2 # can settle only once with pytest.raises(TransactionFailed): channel.settle() assert len(events) == 6 assert events[0]['_event_type'] == 'ChannelNewBalance' assert events[0]['assetAddress'] == token.address.encode('hex') assert events[0]['participant'] == tester.a0.encode('hex') assert events[0]['balance'] == 50 assert events[1]['_event_type'] == 'ChannelNewBalance' assert events[1]['assetAddress'] == token.address.encode('hex') assert events[1]['participant'] == tester.a1.encode('hex') assert events[1]['balance'] == 50 assert events[2]['_event_type'] == 'ChannelClosed' assert events[2]['closingAddress'] == tester.a0.encode('hex') assert events[2]['blockNumber'] == 1158002 assert events[3]['_event_type'] == 'ChannelSecretRevealed' assert events[3]['secret'] == 'x' * 32 assert events[4]['_event_type'] == 'ChannelSecretRevealed' assert events[4]['secret'] == 'y' * 32 assert events[5]['_event_type'] == 'ChannelSettled' assert events[5]['blockNumber'] == state.block.number
def test_two_messages_direct_transfer(state, token, channel, events): # test tokens and distribute tokens assert token.balanceOf(tester.a0) == 10000 assert token.balanceOf(tester.a1) == 0 assert token.transfer(tester.a1, 5000) is True assert token.balanceOf(tester.a0) == 5000 assert token.balanceOf(tester.a1) == 5000 # test global variables assert channel.settleTimeout() == 30 assert channel.assetAddress() == token.address.encode('hex') assert channel.opened() == 0 assert channel.closed() == 0 assert channel.settled() == 0 hashlock1 = sha3(tester.k0) lock_amount1 = 29 lock_expiration1 = 31 lock1 = Lock(lock_amount1, lock_expiration1, hashlock1) locksroot1 = merkleroot([ sha3(lock1.as_bytes), ]) nonce = 1 asset = token.address transfered_amount = 1 recipient = tester.a1 locksroot = locksroot1 msg1 = DirectTransfer( nonce, asset, transfered_amount, recipient, locksroot, ) msg1.sign(tester.k0) packed = msg1.packed() direct_transfer1 = str(packed.data) hashlock2 = sha3(tester.k1) lock_amount2 = 29 lock_expiration2 = 31 lock2 = Lock(lock_amount2, lock_expiration2, hashlock2) locksroot2 = merkleroot([ sha3(lock2.as_bytes), ]) locksroot = locksroot2 msg2 = DirectTransfer( 2, # nonce token.address, # asset 3, # transfered_amount tester.a0, # recipient locksroot, ) msg2.sign(tester.k1) packed = msg2.packed() direct_transfer2 = str(packed.data) channel.close(direct_transfer1, direct_transfer2) assert len(events) == 1 assert events[0]['_event_type'] == 'ChannelClosed' assert events[0]['closingAddress'] == tester.a0.encode('hex') assert events[0]['blockNumber'] == state.block.number with pytest.raises(TransactionFailed): # not participant channel.close(direct_transfer1, direct_transfer2, sender=tester.k2) # Test with message sender tester.a0 assert channel.closed() == state.block.number assert channel.closingAddress() == tester.a0.encode('hex') # assert channel.participants(0)[10] == 1 # assert channel.participants(0)[11] == token.address.encode('hex') # assert channel.participants(0)[9] == tester.a0.encode('hex') # assert channel.participants(0)[12] == tester.a1.encode('hex') # assert channel.participants(0)[3] == 1 # assert channel.participants(0)[13] == locksroot1 # assert channel.participants(0)[7] == '\x00' * 32 # Test with message sender tester.a1 assert channel.closed() == state.block.number assert channel.closingAddress() == tester.a0.encode('hex')
def test_close_single_direct_transfer(state, channel, token, events): # pylint: disable=too-many-locals,too-many-statements # test tokens and distribute tokens assert token.balanceOf(tester.a0) == 10000 assert token.balanceOf(tester.a1) == 0 assert token.transfer(tester.a1, 5000) is True assert token.balanceOf(tester.a0) == 5000 assert token.balanceOf(tester.a1) == 5000 # test global variables assert channel.settleTimeout() == 30 assert channel.assetAddress() == token.address.encode('hex') assert channel.opened() == 0 assert channel.closed() == 0 assert channel.settled() == 0 # test participants variables changed when constructing assert channel.addressAndBalance()[0] == tester.a0.encode('hex') assert channel.addressAndBalance()[2] == tester.a1.encode('hex') # test atIndex() # private must be removed from the function in order to work # assert channel.atIndex(sha3('address1')[:20]) == 0 # assert channel.atIndex(sha3('address2')[:20]) == 1 # test deposit(uint) with pytest.raises(TransactionFailed): channel.deposit(30, sender=tester.k2) # not participant assert token.balanceOf(channel.address) == 0 assert token.approve(channel.address, 30) is True # allow the contract do deposit assert channel.addressAndBalance()[1] == 0 with pytest.raises(TransactionFailed): channel.deposit(5001) channel.deposit(30) assert channel.addressAndBalance()[1] == 30 assert token.balanceOf(channel.address) == 30 assert token.balanceOf(tester.a0) == 4970 assert channel.opened() == state.block.number # test open() # private must be removed from the function in order to work # assert channel.opened() == 0 # channel is not yet opened # channel.open() # assert channel.opened() > 0 # assert channel.opened() <= state.block.number # test partner(address) # private must be removed from the function in order to work # assert channel.partner(sha3('address1')[:20]) == sha3('address2')[:20].encode('hex') # assert channel.partner(sha3('address2')[:20]) == sha3('address1')[:20].encode('hex') # test addressAndBalance() a1, d1, a2, d2 = channel.addressAndBalance() assert a1 == tester.a0.encode('hex') assert a2 == tester.a1.encode('hex') assert d1 == 30 assert d2 == 0 # test close(message) initiator_privkey = tester.k0 recipient_privkey = tester.k1 recipient_address = privtoaddr(recipient_privkey) asset_address = token.address hashlock = sha3(initiator_privkey) lock_amount = 29 lock_expiration = 31 lock = Lock(lock_amount, lock_expiration, hashlock) locksroot = merkleroot([ sha3(lock.as_bytes), ]) nonce = 1 asset = asset_address transfered_amount = 1 recipient = recipient_address locksroot = locksroot msg = DirectTransfer( nonce, asset, transfered_amount, recipient, locksroot, ) msg.sign(initiator_privkey) packed = msg.packed() direct_transfer = str(packed.data) channel.closeSingleTransfer(direct_transfer) with pytest.raises(TransactionFailed): channel.closeSingleTransfer(direct_transfer, sender=tester.k2) # not participant assert channel.closed() == state.block.number assert channel.closingAddress() == tester.a0.encode('hex') # assert channel.participants(0)[10] == 1 # assert channel.participants(0)[11] == token.address.encode('hex') # assert channel.participants(0)[9] == tester.a0.encode('hex') # assert channel.participants(0)[12] == tester.a1.encode('hex') # assert channel.participants(0)[3] == 1 # assert channel.participants(0)[13] == locksroot # assert channel.participants(0)[7] == '\x00' * 32 assert len(events) == 2 assert events[0]['_event_type'] == 'ChannelNewBalance' assert events[0]['assetAddress'] == token.address.encode('hex') assert events[0]['balance'] == 30 assert events[0]['participant'] == tester.a0.encode('hex') assert events[1]['_event_type'] == 'ChannelClosed' assert events[1]['closingAddress'] == tester.a0.encode('hex') assert events[1]['blockNumber'] == state.block.number
def test_update_mediated_transfer(state, token, channel, events): # test tokens and distribute tokens assert token.balanceOf(tester.a0) == 10000 assert token.balanceOf(tester.a1) == 0 assert token.transfer(tester.a1, 5000) is True assert token.balanceOf(tester.a0) == 5000 assert token.balanceOf(tester.a1) == 5000 # test global variables assert channel.settleTimeout() == 30 assert channel.assetAddress() == token.address.encode('hex') assert channel.opened() == 0 assert channel.closed() == 0 assert channel.settled() == 0 hashlock1 = sha3(tester.k0) lock_amount1 = 29 lock_expiration1 = 31 lock1 = Lock(lock_amount1, lock_expiration1, hashlock1) locksroot1 = merkleroot([ sha3(lock1.as_bytes), ]) hashlock2 = sha3(tester.k1) lock_amount2 = 29 lock_expiration2 = 31 lock2 = Lock(lock_amount2, lock_expiration2, hashlock2) locksroot2 = merkleroot([ sha3(lock2.as_bytes), ]) nonce = 1 asset = token.address transfered_amount = 3 recipient = tester.a2 locksroot = locksroot1 target = tester.a1 initiator = tester.a0 msg1 = MediatedTransfer( nonce, asset, transfered_amount, recipient, locksroot, lock1, target, initiator, fee=0, ) msg1.sign(tester.k0) packed = msg1.packed() mediated_transfer1 = str(packed.data) nonce = 2 asset = token.address transfered_amount = 4 recipient = tester.a2 locksroot = locksroot2 target = tester.a0 initiator = tester.a1 msg2 = MediatedTransfer( nonce, asset, transfered_amount, recipient, locksroot, lock2, target, initiator, fee=0, ) msg2.sign(tester.k1) packed = msg2.packed() mediated_transfer2 = str(packed.data) # not yet closed with pytest.raises(TransactionFailed): channel.updateTransfer(mediated_transfer1, sender=tester.k1) channel.close(mediated_transfer1, mediated_transfer2) # Test with message sender tester.a0 assert channel.closed() == state.block.number assert channel.closingAddress() == tester.a0.encode('hex') # assert channel.participants(0)[10] == 1 # assert channel.participants(0)[11] == token.address.encode('hex') # assert channel.participants(0)[9] == tester.a0.encode('hex') # assert channel.participants(0)[12] == tester.a1.encode('hex') # assert channel.participants(0)[3] == 1 # assert channel.participants(0)[13] == locksroot1 # assert channel.participants(0)[7] == '\x00' * 32 # Test with message sender tester.a1 assert channel.closed() == state.block.number assert channel.closingAddress() == tester.a0.encode('hex') # assert channel.participants(1)[10] == 2 # assert channel.participants(1)[11] == token.address.encode('hex') # assert channel.participants(1)[9] == tester.a1.encode('hex') # assert channel.participants(1)[12] == tester.a0.encode('hex') # assert channel.participants(1)[3] == 3 # assert channel.participants(1)[13] == locksroot2 # assert channel.participants(1)[7] == '\x00' * 32 hashlock3 = sha3(tester.k1) lock_amount3 = 29 lock_expiration3 = 31 lock3 = Lock(lock_amount3, lock_expiration3, hashlock3) locksroot3 = merkleroot([ sha3(lock3.as_bytes), ]) nonce = 3 asset = token.address transfered_amount = 4 recipient = tester.a2 locksroot = locksroot3 target = tester.a0 initiator = tester.a1 msg3 = MediatedTransfer( nonce, asset, transfered_amount, recipient, locksroot, lock3, target, initiator, fee=0, ) msg3.sign(tester.k1) packed = msg3.packed() mediated_transfer3 = str(packed.data) # closingAddress == getSender(message) with pytest.raises(TransactionFailed): channel.updateTransfer(mediated_transfer1) channel.updateTransfer(mediated_transfer3, sender=tester.k1) # Test with message sender tester.a1 # assert channel.participants(1)[10] == 3 # assert channel.participants(1)[11] == token.address.encode('hex') # assert channel.participants(1)[9] == tester.a1.encode('hex') # assert channel.participants(1)[12] == tester.a0.encode('hex') # assert channel.participants(1)[3] == 5 # assert channel.participants(1)[13] == locksroot3 # assert channel.participants(1)[7] == '\x00' * 32 msg4 = DirectTransfer( 1, # nonce token.address, # asset 5, # transfered_amount tester.a0, # recipient locksroot, ) msg4.sign(tester.k1) packed = msg4.packed() direct_transfer4 = str(packed.data) # nonce too low with pytest.raises(TransactionFailed): channel.updateTransfer(direct_transfer4, sender=tester.k1) # settleTimeout overdue state.block.number = 1158041 with pytest.raises(TransactionFailed): channel.updateTransfer(mediated_transfer3, sender=tester.k1) assert len(events) == 1 assert events[0]['_event_type'] == 'ChannelClosed' assert events[0]['blockNumber'] == 1158002 assert events[0]['closingAddress'] == tester.a0.encode('hex')
def test_unlock(token, channel, events, state): secret1 = 'x' * 32 hashlock1 = sha3(secret1) lock_amount1 = 29 lock_expiration1 = 1158003 lock1 = Lock(lock_amount1, lock_expiration1, hashlock1) lockhash1 = sha3(lock1.as_bytes) merkleproof1 = [lockhash1] locksroot1 = merkleroot([ lockhash1, ], merkleproof1) nonce = 10 asset = token.address transfered_amount = 1 recipient = tester.a1 msg1 = DirectTransfer( nonce, asset, transfered_amount, recipient, locksroot1, ) msg1.sign(tester.k0) packed = msg1.packed() direct_transfer1 = str(packed.data) secret2 = 'y' * 32 hashlock2 = sha3(secret2) lock_amount2 = 20 lock_expiration2 = 1158030 lock2 = Lock(lock_amount2, lock_expiration2, hashlock2) lockhash2 = sha3(lock2.as_bytes) merkleproof2 = [lockhash2] locksroot2 = merkleroot([ lockhash2, ], merkleproof2) msg2 = DirectTransfer( 2, # nonce asset, 3, # transfered_amount tester.a0, # recipient '', ) msg2.sign(tester.k1) packed = msg2.packed() direct_transfer2 = str(packed.data) channel.close(direct_transfer1, direct_transfer2) channel.unlock( str(lock1.as_bytes), ''.join(merkleproof1), secret1, ) # TODO: it must not be possible to unlock the same lock twive # with pytest.raises(TransactionFailed): # channel.unlock( # str(lock1.as_bytes), # ''.join(merkleproof1), # secret1, # ) # expiration has passed, should fail state.block.number = 1158031 with pytest.raises(TransactionFailed): channel.unlock( str(lock2.as_bytes), ''.join(merkleproof2), secret2, )