def test_channel_must_accept_expired_locks(): """ A node may go offline for an undetermined period of time, and when it comes back online it must accept the messages that are waiting, otherwise the partner node won't make progress with its queue. If a N node goes offline for a number B of blocks, and the partner does not close the channel, when N comes back online some of the messages from its partner may become expired. Neverthless these messages are ordered and must be accepted for the partner to make progress with its queue. Note: Accepting a message with an expired lock does *not* imply the token transfer happened, and the receiver node must *not* forward the transfer, only accept the message allowing the partner to progress with its message queue. """ balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 privkey1, address1 = make_privkey_address() privkey2, address2 = make_privkey_address() token_address = make_address() netting_channel = NettingChannelMock() our_state = ChannelEndState( address1, balance1, netting_channel.opened(), ) partner_state = ChannelEndState( address2, balance2, netting_channel.opened(), ) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) block_number = 10 transfer = make_mediated_transfer( nonce=test_channel.partner_state.nonce, token=test_channel.token_address, expiration=block_number + settle_timeout, recipient=address1, ) transfer.sign(privkey2, address2) test_channel.register_transfer( block_number + settle_timeout + 1, transfer, )
def channel_list_mock(num_channels): channel_list = [] for _ in range(0, num_channels): netting_channel = NettingChannelMock() asset_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = random.randint(0, 2**128) balance2 = random.randint(0, 2**128) reveal_timeout = random.randint(0, 2**64) settle_timeout = reveal_timeout + random.randint(0, 2**64) our_state = ChannelEndState(address1, balance1, netting_channel.opened) partner_state = ChannelEndState(address2, balance2, netting_channel.opened) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, asset_address, reveal_timeout, settle_timeout, ) channel_list.append(test_channel) return channel_list
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_channel_close_called_only_once(): class MockCheckCallsToClose(): def __init__(self): self.address = 'mockcheckcallstoclosemockcheckcallstoclo' self.close_calls = 0 def opened(self): return 1 def closed(self): return 0 def settled(self): return 0 def close(self, transfer): self.close_calls += 1 netting_channel = NettingChannelMock() token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 our_state = ChannelEndState(address1, balance1, netting_channel.opened()) partner_state = ChannelEndState(address2, balance2, netting_channel.opened()) channel_for_hashlock = list() netting_channel = MockCheckCallsToClose() external_state = ChannelExternalState( lambda *args: channel_for_hashlock.append(args), netting_channel, ) test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) test_channel.external_state.close('') test_channel.external_state.close('') assert netting_channel.close_calls == 1
# -*- coding: utf-8 -*- import pytest from raiden.messages import Ping, Ack, decode, Lock, MediatedTransfer from raiden.utils import make_privkey_address, sha3 PRIVKEY, ADDRESS = make_privkey_address() INVALID_ADDRESSES = [ ' ', ' ' * 19, ' ' * 21, ] def make_lock_with_amount(amount): hashlock = 'a' * 32 return Lock(amount, 1, hashlock) def make_mediated_transfer( identifier=0, nonce=1, token=ADDRESS, transferred_amount=0, amount=1, locksroot='', recipient=ADDRESS, target=ADDRESS, initiator=ADDRESS, fee=0): return MediatedTransfer(
def test_end_state(): netting_channel = NettingChannelMock() token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 lock_secret = sha3('test_end_state') lock_amount = 30 lock_expiration = 10 lock_hashlock = sha3(lock_secret) state1 = ChannelEndState(address1, balance1, netting_channel.opened) state2 = ChannelEndState(address2, balance2, netting_channel.opened) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == '' lock = Lock( lock_amount, lock_expiration, lock_hashlock, ) lock_hash = sha3(lock.as_bytes) transferred_amount = 0 locksroot = state2.compute_merkleroot_with(lock) locked_transfer = LockedTransfer( 1, # TODO: fill in identifier nonce=state1.nonce, token=token_address, transferred_amount=transferred_amount, recipient=state2.address, locksroot=locksroot, lock=lock, ) transfer_target = make_address() transfer_initiator = make_address() fee = 0 mediated_transfer = locked_transfer.to_mediatedtransfer( transfer_target, transfer_initiator, fee, ) mediated_transfer.sign(privkey1, address1) state2.register_locked_transfer(mediated_transfer) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is True assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash with pytest.raises(ValueError): state1.update_contract_balance(balance1 - 10) state1.update_contract_balance(balance1 + 10) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is True assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash # registering the secret should not change the locked amount state2.register_secret(lock_secret) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash state2.release_lock(state1, lock_secret) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 - lock_amount assert state2.balance(state1) == balance2 + lock_amount assert state1.distributable(state2) == balance1 + 10 - lock_amount assert state2.distributable(state1) == balance2 + lock_amount assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == ''
def test_python_channel(): netting_channel = NettingChannelMock() token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 our_state = ChannelEndState(address1, balance1, netting_channel.opened) partner_state = ChannelEndState(address2, balance2, netting_channel.opened) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) assert test_channel.contract_balance == our_state.contract_balance assert test_channel.balance == our_state.balance(partner_state) assert test_channel.transferred_amount == our_state.transferred_amount assert test_channel.distributable == our_state.distributable(partner_state) assert test_channel.outstanding == our_state.locked() assert test_channel.outstanding == 0 assert test_channel.locked == partner_state.locked() assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0 with pytest.raises(ValueError): test_channel.create_directtransfer( -10, 1 # TODO: fill in identifier ) with pytest.raises(ValueError): test_channel.create_directtransfer( balance1 + 10, 1 # TODO: fill in identifier ) amount1 = 10 directtransfer = test_channel.create_directtransfer( amount1, 1 # TODO: fill in identifier ) directtransfer.sign(privkey1, address1) test_channel.register_transfer(directtransfer) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0 secret = sha3('test_channel') hashlock = sha3(secret) amount2 = 10 fee = 0 expiration = settle_timeout - 5 mediatedtransfer = test_channel.create_mediatedtransfer( address1, address2, fee, amount2, 1, # TODO: fill in identifier expiration, hashlock, ) mediatedtransfer.sign(privkey1, address1) test_channel.register_transfer(mediatedtransfer) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == amount2 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == amount2 test_channel.release_lock(secret) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 - amount2 assert test_channel.transferred_amount == amount1 + amount2 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0
def test_end_state(): asset_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 lock_secret = sha3('test_end_state') lock_amount = 30 lock_expiration = 10 lock_hashlock = sha3(lock_secret) state1 = ChannelEndState(address1, balance1) state2 = ChannelEndState(address2, balance2) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == '' lock = Lock( lock_amount, lock_expiration, lock_hashlock, ) lock_hash = sha3(lock.as_bytes) transferred_amount = 0 locksroot = state2.compute_merkleroot_with(lock) locked_transfer = LockedTransfer( 1, # TODO: fill in identifier nonce=state1.nonce, asset=asset_address, transferred_amount=transferred_amount, recipient=state2.address, locksroot=locksroot, lock=lock, ) transfer_target = make_address() transfer_initiator = make_address() fee = 0 mediated_transfer = locked_transfer.to_mediatedtransfer( transfer_target, transfer_initiator, fee, ) mediated_transfer.sign(privkey1, address1) state2.register_locked_transfer(mediated_transfer) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is True assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash with pytest.raises(ValueError): state1.update_contract_balance(balance1 - 10) state1.update_contract_balance(balance1 + 10) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is True assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash # registering the secret should not change the locked amount state2.register_secret(lock_secret) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == lock_amount assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == lock_hash state2.release_lock(state1, lock_secret) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 - lock_amount assert state2.balance(state1) == balance2 + lock_amount assert state1.distributable(state2) == balance1 + 10 - lock_amount assert state2.distributable(state1) == balance2 + lock_amount assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == '' assert state2.balance_proof.merkleroot_for_unclaimed() == ''
def test_python_channel(): asset_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 our_state = ChannelEndState(address1, balance1) partner_state = ChannelEndState(address2, balance2) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, asset_address, reveal_timeout, settle_timeout, ) assert test_channel.contract_balance == our_state.contract_balance assert test_channel.balance == our_state.balance(partner_state) assert test_channel.transferred_amount == our_state.transferred_amount assert test_channel.distributable == our_state.distributable(partner_state) assert test_channel.outstanding == our_state.locked() assert test_channel.outstanding == 0 assert test_channel.locked == partner_state.locked() assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0 with pytest.raises(ValueError): test_channel.create_directtransfer( -10, 1 # TODO: fill in identifier ) with pytest.raises(ValueError): test_channel.create_directtransfer( balance1 + 10, 1 # TODO: fill in identifier ) amount1 = 10 directtransfer = test_channel.create_directtransfer( amount1, 1 # TODO: fill in identifier ) directtransfer.sign(privkey1, address1) test_channel.register_transfer(directtransfer) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0 secret = sha3('test_channel') hashlock = sha3(secret) amount2 = 10 fee = 0 expiration = settle_timeout - 5 mediatedtransfer = test_channel.create_mediatedtransfer( address1, address2, fee, amount2, 1, # TODO: fill in identifier expiration, hashlock, ) mediatedtransfer.sign(privkey1, address1) test_channel.register_transfer(mediatedtransfer) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == amount2 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == amount2 test_channel.release_lock(secret) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 - amount2 assert test_channel.transferred_amount == amount1 + amount2 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.locked() == 0
def test_channel(): class NettingChannelMock(object): def opened(self): return 1 def closed(self): return 0 def settled(self): return 0 asset_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 our_state = ChannelEndState(address1, balance1) partner_state = ChannelEndState(address2, balance2) block_alarm = list() channel_for_hashlock = list() netting_channel = NettingChannelMock() external_state = ChannelExternalState( block_alarm.append, lambda *args: channel_for_hashlock.append(args), lambda: 1, netting_channel, ) channel = Channel( our_state, partner_state, external_state, asset_address, reveal_timeout, settle_timeout, ) assert channel.contract_balance == our_state.contract_balance assert channel.balance == our_state.balance(partner_state) assert channel.transfered_amount == our_state.transfered_amount assert channel.distributable == our_state.distributable(partner_state) assert channel.outstanding == our_state.locked() assert channel.outstanding == 0 assert channel.locked == partner_state.locked() assert channel.our_state.locked() == 0 assert channel.partner_state.locked() == 0 with pytest.raises(ValueError): channel.create_directtransfer(-10) with pytest.raises(ValueError): channel.create_directtransfer(balance1 + 10) amount1 = 10 directtransfer = channel.create_directtransfer(amount1) directtransfer.sign(privkey1, address1) channel.register_transfer(directtransfer) assert channel.contract_balance == balance1 assert channel.balance == balance1 - amount1 assert channel.transfered_amount == amount1 assert channel.distributable == balance1 - amount1 assert channel.outstanding == 0 assert channel.locked == 0 assert channel.our_state.locked() == 0 assert channel.partner_state.locked() == 0 secret = sha3('test_channel') hashlock = sha3(secret) amount2 = 10 fee = 0 expiration = settle_timeout - 5 mediatedtransfer = channel.create_mediatedtransfer( address1, address2, fee, amount2, expiration, hashlock, ) mediatedtransfer.sign(privkey1, address1) channel.register_transfer(mediatedtransfer) assert channel.contract_balance == balance1 assert channel.balance == balance1 - amount1 assert channel.transfered_amount == amount1 assert channel.distributable == balance1 - amount1 - amount2 assert channel.outstanding == 0 assert channel.locked == amount2 assert channel.our_state.locked() == 0 assert channel.partner_state.locked() == amount2 channel.claim_lock(secret) assert channel.contract_balance == balance1 assert channel.balance == balance1 - amount1 - amount2 assert channel.transfered_amount == amount1 + amount2 assert channel.distributable == balance1 - amount1 - amount2 assert channel.outstanding == 0 assert channel.locked == 0 assert channel.our_state.locked() == 0 assert channel.partner_state.locked() == 0
# -*- coding: utf-8 -*- from raiden.messages import Ping, Ack, decode, Lock, MediatedTransfer from raiden.utils import make_privkey_address, sha3 PRIVKEY, ADDRESS = make_privkey_address() def test_signature(): ping = Ping(nonce=0) ping.sign(PRIVKEY, ADDRESS) assert ping.sender == ADDRESS def test_encoding(): ping = Ping(nonce=0) ping.sign(PRIVKEY, ADDRESS) decoded_ping = decode(ping.encode()) assert isinstance(decoded_ping, Ping) assert decoded_ping.sender == ADDRESS == ping.sender assert ping.nonce == decoded_ping.nonce assert ping.signature == decoded_ping.signature assert ping.cmdid == decoded_ping.cmdid assert ping.hash == decoded_ping.hash def test_hash(): ping = Ping(nonce=0) ping.sign(PRIVKEY, ADDRESS) data = ping.encode() msghash = sha3(data) decoded_ping = decode(data)